From c4256b30052fac3951651d5ae0a13a2afc0bac18 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Thu, 11 May 2017 00:29:22 +0200 Subject: [PATCH] Add multi-company support hr_holidays_usability Add modules hr_holidays_lunch_voucher and hr_holidays_lunch_voucher_natixis --- hr_holidays_lunch_voucher_natixis/__init__.py | 4 + .../__openerp__.py | 20 +++++ hr_holidays_lunch_voucher_natixis/company.py | 15 ++++ .../company_view.xml | 26 ++++++ .../wizard/__init__.py | 3 + .../wizard/lunch_voucher_purchase.py | 81 +++++++++++++++++++ 6 files changed, 149 insertions(+) create mode 100644 hr_holidays_lunch_voucher_natixis/__init__.py create mode 100644 hr_holidays_lunch_voucher_natixis/__openerp__.py create mode 100644 hr_holidays_lunch_voucher_natixis/company.py create mode 100644 hr_holidays_lunch_voucher_natixis/company_view.xml create mode 100644 hr_holidays_lunch_voucher_natixis/wizard/__init__.py create mode 100644 hr_holidays_lunch_voucher_natixis/wizard/lunch_voucher_purchase.py diff --git a/hr_holidays_lunch_voucher_natixis/__init__.py b/hr_holidays_lunch_voucher_natixis/__init__.py new file mode 100644 index 0000000..ece3401 --- /dev/null +++ b/hr_holidays_lunch_voucher_natixis/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- + +from . import company +from . import wizard diff --git a/hr_holidays_lunch_voucher_natixis/__openerp__.py b/hr_holidays_lunch_voucher_natixis/__openerp__.py new file mode 100644 index 0000000..c0e41cf --- /dev/null +++ b/hr_holidays_lunch_voucher_natixis/__openerp__.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# © 2017 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + 'name': 'HR Holidays Lunch Voucher Natixis', + 'version': '10.0.1.0.0', + 'category': 'Human Resources', + 'license': 'AGPL-3', + 'summary': 'Generate order file for Natixis lunch vouchers', + 'description': '', + 'author': 'Akretion', + 'website': 'http://www.akretion.com', + 'depends': ['hr_holidays_lunch_voucher'], + 'data': [ + 'company_view.xml', + ], + 'installable': True, +} diff --git a/hr_holidays_lunch_voucher_natixis/company.py b/hr_holidays_lunch_voucher_natixis/company.py new file mode 100644 index 0000000..d68d8c7 --- /dev/null +++ b/hr_holidays_lunch_voucher_natixis/company.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +# © 2017 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import models, fields + + +class ResCompany(models.Model): + _inherit = 'res.company' + + lunch_voucher_natixis_customer_code = fields.Char( + string='Natixis Customer Ref', size=7) + lunch_voucher_natixis_delivery_code = fields.Char( + string='Natixis Delivery Code', size=7) diff --git a/hr_holidays_lunch_voucher_natixis/company_view.xml b/hr_holidays_lunch_voucher_natixis/company_view.xml new file mode 100644 index 0000000..8c1ded6 --- /dev/null +++ b/hr_holidays_lunch_voucher_natixis/company_view.xml @@ -0,0 +1,26 @@ + + + + + + + + + natixis.lunch_voucher.company.form + res.company + + + + + + + + + + + + diff --git a/hr_holidays_lunch_voucher_natixis/wizard/__init__.py b/hr_holidays_lunch_voucher_natixis/wizard/__init__.py new file mode 100644 index 0000000..fa75146 --- /dev/null +++ b/hr_holidays_lunch_voucher_natixis/wizard/__init__.py @@ -0,0 +1,3 @@ +# -*- encoding: utf-8 -*- + +from . import lunch_voucher_purchase diff --git a/hr_holidays_lunch_voucher_natixis/wizard/lunch_voucher_purchase.py b/hr_holidays_lunch_voucher_natixis/wizard/lunch_voucher_purchase.py new file mode 100644 index 0000000..09edb20 --- /dev/null +++ b/hr_holidays_lunch_voucher_natixis/wizard/lunch_voucher_purchase.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- +# © 2017 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import models, fields, api, _ +from openerp.exceptions import Warning as UserError + + +class LunchVoucherPurchase(models.TransientModel): + _inherit = 'lunch.voucher.purchase' + + @api.multi + def run(self): + self.ensure_one() + action = super(LunchVoucherPurchase, self).run() + company = self.env.user.company_id + lvao = self.env['lunch.voucher.attribution'] + assert self._context.get('active_model')\ + == 'lunch.voucher.attribution', 'wrong source model' + assert self._context.get('active_ids'), 'missing active_ids in ctx' + if not company.lunch_voucher_natixis_customer_code: + raise UserError(_( + "Missing Natixis Customer Ref on company '%s'.") + % company.name) + if not company.lunch_voucher_natixis_delivery_code: + raise UserError(_( + "Missing Natixis Delivery Code on company '%s'.") + % company.name) + if len(company.lunch_voucher_natixis_customer_code) != 7: + raise UserError(_( + "Natixis Customer Ref '%s' on company '%s' should " + "have 7 characters/digits.") + % (company.lunch_voucher_natixis_customer_code, company.name)) + if len(company.lunch_voucher_natixis_delivery_code) != 7: + raise UserError(_( + "Natixis Delivery Code on company '%s' should " + "have 7 characters/digits.") + % (company.lunch_voucher_natixis_delivery_code, company.name)) + if not company.lunch_voucher_employer_price: + raise UserError(_( + "Lunch Voucher Employer Price not set on company '%s'.") + % company.name) + lvouchers = lvao.browse(self._context['active_ids']) + of = u'' + tmp = {} + for lvoucher in lvouchers: + if lvoucher.qty > 0: + if lvoucher.qty not in tmp: + tmp[lvoucher.qty] = 1 + else: + tmp[lvoucher.qty] += 1 + for vouchers_per_pack, pack_qty in tmp.iteritems(): + if vouchers_per_pack > 99: + raise UserError(_( + "Cannot order more than 99 vouchers per pack")) + line = u'%s%s%s%s%s%s%s%s\n' % ( + company.lunch_voucher_natixis_delivery_code, + company.lunch_voucher_natixis_customer_code, + unicode(pack_qty).zfill(3), + unicode(vouchers_per_pack).zfill(2), + unicode(pack_qty * vouchers_per_pack).zfill(5), + '{:05.2f}'.format( + company.lunch_voucher_product_id.standard_price), + '{:05.2f}'.format(company.lunch_voucher_employer_price), + ' ' * 64) + of += line + today_dt = fields.Date.from_string( + fields.Date.context_today(self)) + filename = 'E%s_%s.txt' % ( + company.lunch_voucher_natixis_customer_code, + today_dt.strftime('%d%m%Y')) + self.env['ir.attachment'].create({ + 'name': filename, + 'res_id': action['res_id'], + 'res_model': 'purchase.order', + 'datas': of.encode('base64'), + 'datas_fname': filename, + 'type': 'binary', + }) + return action