Add multi-company support hr_holidays_usability

Add modules hr_holidays_lunch_voucher and hr_holidays_lunch_voucher_natixis
This commit is contained in:
Alexis de Lattre
2017-05-11 00:29:22 +02:00
parent 361eb380e4
commit c5776557cc
18 changed files with 623 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
# -*- encoding: utf-8 -*-
from . import hr_holidays_post
from . import lunch_voucher_purchase

View File

@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
# © 2017 Akretion (http://www.akretion.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from openerp import models, fields, api
from dateutil.relativedelta import relativedelta
import logging
logger = logging.getLogger(__name__)
class HrHolidaysPost(models.TransientModel):
_inherit = 'hr.holidays.post'
lunch_voucher_po = fields.Boolean(
string='Generate Lunch Voucher Purchase Order')
work_days = fields.Integer(string='Work Days')
@api.model
def default_get(self, fields_list):
res = super(HrHolidaysPost, self).default_get(fields_list)
hhpo = self.env['hr.holidays.public']
company = self.env.user.company_id
if company.lunch_voucher_product_id:
res['lunch_voucher_po'] = True
today = fields.Date.context_today(self)
today_dt = fields.Date.from_string(today)
cur_month = today_dt.month
# last day of month
date_dt = today_dt + relativedelta(day=31)
work_days = date_dt.day
logger.info('Number of days in the month: %d', work_days)
# from last day of month to the first
while date_dt.month == cur_month:
if hhpo.is_public_holiday(date_dt):
work_days -= 1
logger.info(
"%s is a bank holiday, don't count", date_dt)
# if it's a saturday/sunday
elif date_dt.weekday() in (5, 6):
work_days -= 1
logger.info(
"%s is a saturday/sunday, don't count", date_dt)
date_dt += relativedelta(days=-1)
logger.info('Number of work days in the month: %d', work_days)
res['work_days'] = work_days
return res
@api.multi
def run(self):
self.ensure_one()
lvao = self.env['lunch.voucher.attribution']
today = fields.Date.context_today(self)
employees = self.env['hr.employee'].search([
('lunch_voucher', '=', True),
('company_id', '=', self.env.user.company_id.id),
])
lv_dict = {}
for employee in employees:
lv_dict[employee.id] = []
for hol in self.holidays_to_post_ids:
if not hol.lunch_voucher_id and hol.employee_id.id in lv_dict:
lv_dict[hol.employee_id.id].append(hol.id)
for employee_id, hol_ids in lv_dict.iteritems():
vals = {
'employee_id': employee_id,
'date': today,
'month_work_days': self.work_days,
}
if hol_ids:
vals['holiday_ids'] = [(6, 0, hol_ids)]
lvao.create(vals)
return super(HrHolidaysPost, self).run()

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
© 2017 Akretion (http://www.akretion.com/)
@author: Alexis de Lattre <alexis.delattre@akretion.com>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->
<openerp>
<data>
<record id="hr_holidays_post_form" model="ir.ui.view">
<field name="name">hr_holidays_post_form</field>
<field name="model">hr.holidays.post</field>
<field name="inherit_id" ref="hr_holidays_usability.hr_holidays_post_form"/>
<field name="arch" type="xml">
<field name="holidays_to_post_ids" position="before">
<field name="lunch_voucher_po" states="done"/>
<field name="work_days"
attrs="{'invisible': ['|', ('state', '!=', 'done'), ('lunch_voucher_po', '=', False)]}"/>
</field>
</field>
</record>
</data>
</openerp>

View File

@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-
# © 2017 Akretion (http://www.akretion.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from openerp import models, api, _
from openerp.exceptions import Warning as UserError
class LunchVoucherPurchase(models.TransientModel):
_name = 'lunch.voucher.purchase'
_description = 'Purchase Lunch Vouchers Wizard'
@api.multi
def run(self):
self.ensure_one()
company = self.env.user.company_id
if not company.lunch_voucher_product_id:
raise UserError(_(
"Lunch Voucher Product not configured on company %s")
% company.name)
if not company.lunch_voucher_product_id.seller_id:
raise UserError(_(
"Missing supplier on Product '%s'.")
% company.lunch_voucher_product_id.name)
poo = self.env['purchase.order']
polo = self.env['purchase.order.line']
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'
lvouchers = lvao.browse(self._context['active_ids'])
total_qty = 0
for lvoucher in lvouchers:
if lvoucher.purchase_id:
raise UserError(_(
"One of the Lunch Voucher Attributions you selected "
"related to employee '%s' is already linked to a "
"purchase order.") % lvoucher.employee_id.name)
if lvoucher.qty < 0:
raise UserError(_(
"One of the Lunch Voucher Attributions you selected "
"related to employee '%s' has a negative quantity.")
% lvoucher.employee_id.name)
total_qty += lvoucher.qty
supplier = company.lunch_voucher_product_id.seller_id
pick_type_id = poo.default_get(['picking_type_id'])['picking_type_id']
onchange_ptype_vals = poo.browse(False).onchange_picking_type_id(
pick_type_id)
vals = onchange_ptype_vals['value']
onchange_vals = poo.browse(False).onchange_partner_id(supplier.id)
vals.update(onchange_vals['value'])
vals['partner_id'] = supplier.id
product = company.lunch_voucher_product_id
onchange_product_vals = polo.browse(False).onchange_product_id(
vals.get('pricelist_id'), product.id, total_qty, False,
supplier.id, fiscal_position_id=vals.get('fiscal_position_id'))
lvals = onchange_product_vals['value']
lvals['product_id'] = product.id
lvals['product_qty'] = total_qty
if lvals['taxes_id']:
lvals['taxes_id'] = [(6, 0, lvals['taxes_id'])]
vals['order_line'] = [(0, 0, lvals)]
po = poo.create(vals)
lvouchers.write({'purchase_id': po.id})
action = self.env['ir.actions.act_window'].for_xml_id(
'purchase', 'purchase_rfq')
action.update({
'res_id': po.id,
'view_mode': 'form,tree',
'views': False,
})
return action

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
© 2017 Akretion (http://www.akretion.com/)
@author Alexis de Lattre <alexis.delattre@akretion.com>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->
<openerp>
<data>
<record id="lunch_voucher_purchase_form" model="ir.ui.view">
<field name="name">lunch_voucher_purchase_form</field>
<field name="model">lunch.voucher.purchase</field>
<field name="arch" type="xml">
<form string="Create Purchase Order of Lunch Vouchers">
<group name="main">
<label string="Click on the button below to create the purchase order for the selected lunch vouchers attributions." colspan="2"/>
</group>
<footer>
<button name="run" type="object" string="Create"
class="oe_highlight"/>
<button special="cancel" string="Cancel" class="oe_link"/>
</footer>
</form>
</field>
</record>
<record id="lunch_voucher_purchase_action" model="ir.actions.act_window">
<field name="name">Create PO for Lunch Vouchers</field>
<field name="res_model">lunch.voucher.purchase</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
</data>
</openerp>