Create activity invoice in the name of an other contact in case of minor member

This commit is contained in:
Youssef Elouahby 2018-10-29 22:21:22 +00:00
parent 2ae88b9905
commit 95426c0e90
2 changed files with 27 additions and 2 deletions

View File

@ -21,6 +21,7 @@
import logging import logging
from math import ceil from math import ceil
from odoo import models, fields, api, _ from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
from odoo.exceptions import UserError from odoo.exceptions import UserError
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -50,6 +51,11 @@ class GolemActivityRegistrationInvoicing(models.TransientModel):
ondelete='cascade') ondelete='cascade')
member_id = fields.Many2one('golem.member', 'Member', required=True, member_id = fields.Many2one('golem.member', 'Member', required=True,
ondelete='cascade') ondelete='cascade')
partner_ids = fields.Many2many('res.partner', compute='_compute_partner_ids')
on_the_name_of = fields.Many2one('res.partner', 'On the Name of',
ondelete='cascade',
domain="[('id', '=', partner_ids[0][2])]")
is_minor = fields.Boolean(related='member_id.is_minor')
line_ids = fields.One2many('golem.activity.registration.invoicing.line', line_ids = fields.One2many('golem.activity.registration.invoicing.line',
'invoicing_id', string='Activities') 'invoicing_id', string='Activities')
schedule_id = fields.Many2one('golem.payment.schedule', 'Payment schedule', schedule_id = fields.Many2one('golem.payment.schedule', 'Payment schedule',
@ -65,6 +71,14 @@ class GolemActivityRegistrationInvoicing(models.TransientModel):
ondelete='cascade') ondelete='cascade')
payment_ids = fields.Many2many('account.payment', string='Generated payments') payment_ids = fields.Many2many('account.payment', string='Generated payments')
@api.depends('member_id')
def _compute_partner_ids(self):
for rec in self:
partner_ids = rec.member_id.legal_guardian_ids.mapped('legal_guardian_id').ids
if hasattr(rec.member_id, 'family_member_ids'):
partner_ids += rec.member_id.family_member_ids.filtered(
lambda r: r.id != self.member_id.partner_id.id).ids
rec.partner_ids = [(6, 0, partner_ids)]
def _create_invoice_line(self, product, price, invoice): def _create_invoice_line(self, product, price, invoice):
""" Create invoice line : needs cache record for onchange, then real """ Create invoice line : needs cache record for onchange, then real
@ -84,7 +98,10 @@ class GolemActivityRegistrationInvoicing(models.TransientModel):
def _create_invoice(self): def _create_invoice(self):
""" Create invoice and lines """ """ Create invoice and lines """
self.ensure_one() self.ensure_one()
partner = self.member_id.partner_id if self.is_minor:
partner = self.on_the_name_of
else:
partner = self.member_id.partner_id
#check if there is a draft invoice for the current customer #check if there is a draft invoice for the current customer
member_line = partner.member_lines.filtered( member_line = partner.member_lines.filtered(
lambda ml: (ml.membership_id.membership_season_id == self.season_id lambda ml: (ml.membership_id.membership_season_id == self.season_id
@ -136,6 +153,10 @@ class GolemActivityRegistrationInvoicing(models.TransientModel):
def validate(self): def validate(self):
""" Validate and create invoice and payments """ """ Validate and create invoice and payments """
self.ensure_one() self.ensure_one()
if self.is_minor and not self.on_the_name_of:
err = _('This member is a minor, please fill on the name of so you '
'invoice this registration')
raise ValidationError(err)
self[0].line_ids.mapped('registration_id').write({'state': 'confirmed'}) self[0].line_ids.mapped('registration_id').write({'state': 'confirmed'})
invoice = self._create_invoice() invoice = self._create_invoice()
self.invoice_id = invoice self.invoice_id = invoice

View File

@ -33,7 +33,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<group states="init"> <group states="init">
<group name="lines" colspan="2"> <group name="lines" colspan="2">
<field name="season_id" readonly="1" /> <field name="season_id" readonly="1" />
<field name="member_id" readonly="1" /> <field name="member_id"/><!-- readonly="1" />-->
<field name='on_the_name_of' attrs="{'invisible': [('is_minor', '=', False)]}"
options="{'no_create': True}"/>
<field name='is_minor' invisible='1'/>
<field name="partner_ids" invisible='1'/>
<field name="line_ids" string="Activities and Prices"> <field name="line_ids" string="Activities and Prices">
<tree delete="false" create="false" edit="true" editable="bottom"> <tree delete="false" create="false" edit="true" editable="bottom">
<field name="activity_id" readonly="1" /> <field name="activity_id" readonly="1" />