From 0b3c3297da5376c59b0979a49ee90c8c8a0a0c2a Mon Sep 17 00:00:00 2001 From: Fabien Bourgeois Date: Thu, 15 Jun 2017 01:14:20 +0200 Subject: [PATCH] [ADD]Registration state : wizard to create invoice and payment, functional state (but not complete ;)) --- .../models/golem_activity_registration.py | 6 +- .../golem_activity_registration_invoicing.py | 55 +++++++++++++++++++ .../golem_activity_registration_invoicing.xml | 3 + 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/golem_activity_registration_state/models/golem_activity_registration.py b/golem_activity_registration_state/models/golem_activity_registration.py index c0b0205b..0be6b8b2 100644 --- a/golem_activity_registration_state/models/golem_activity_registration.py +++ b/golem_activity_registration_state/models/golem_activity_registration.py @@ -52,6 +52,7 @@ class GolemMember(models.Model): line_obj = self.env['golem.activity.registration.invoicing.line'] for reg in draft_registrations: line_obj.create({'invoicing_id': invoicing.id, + 'registration_id': reg.id, 'activity_id': reg.activity_id.id, 'price': reg.activity_id.list_price}) return {'name': _('Registration invoicing'), @@ -104,17 +105,16 @@ class GolemActivityRegistration(models.Model): state = fields.Selection([('draft', 'Draft'), ('confirmed', 'Confirmed'), ('canceled', 'Canceled')], required=True, default='draft') - invoice_id = fields.Many2one('account.invoice', string='Invoice', - ondelete='set null') invoice_line_id = fields.Many2one('account.invoice.line', string='Invoice line', ondelete='set null') + invoice_id = fields.Many2one(related='invoice_line_id.invoice_id') @api.multi def write(self, values): """ Recomputes values linked to registrations when state change """ res = super(GolemActivityRegistration, self).write(values) - if values['state']: + if values.get('state'): for registration in self: registration.activity_id.compute_places_used() return res diff --git a/golem_activity_registration_state/wizard/golem_activity_registration_invoicing.py b/golem_activity_registration_state/wizard/golem_activity_registration_invoicing.py index 751b63a4..92498e32 100644 --- a/golem_activity_registration_state/wizard/golem_activity_registration_invoicing.py +++ b/golem_activity_registration_state/wizard/golem_activity_registration_invoicing.py @@ -17,7 +17,9 @@ """ GOLEM Activity Registration Invoicing Wizard """ +import logging from odoo import models, fields, api +_LOGGER = logging.getLogger(__name__) class GolemActivityRegistrationInvoicingLine(models.TransientModel): """ GOLEM Activity Registration Invoicing Lines """ @@ -26,6 +28,7 @@ class GolemActivityRegistrationInvoicingLine(models.TransientModel): invoicing_id = fields.Many2one('golem.activity.registration.invoicing', required=True) + registration_id = fields.Many2one('golem.activity.registration', required=True) activity_id = fields.Many2one('golem.activity', required=True, readonly=True) price = fields.Float('Price') @@ -43,6 +46,56 @@ class GolemActivityRegistrationInvoicing(models.TransientModel): help='If no schedule is selected, only the ' 'invoice will be create. Otherwise, draft ' 'payments will be generated.') + journal_id = fields.Many2one('account.journal', 'Journal', + domain=[('type', 'in', ('bank', 'cash'))]) + + @api.multi + def _create_invoice(self): + """ Create invoice and lines """ + self.ensure_one() + partner = self.member_id.partner_id + invoice = self.env['account.invoice'].create({ + 'partner_id': partner.id, + 'account_id': partner.property_account_receivable_id.id, + 'fiscal_position_id': partner.property_account_position_id.id + }) + for line in self.line_ids: + product = line.activity_id.product_id + # Handling of invoice lines : needs cache record for onchange, then + # real writing... + invoice_line = self.env['account.invoice.line'].new({ + 'product_id': product.id, + 'invoice_id': invoice.id + }) + invoice_line._onchange_product_id() + line_values = dict(invoice_line._cache) + line_values['price_unit'] = line.price + invoice_line = self.env['account.invoice.line'].create(line_values) + invoice.compute_taxes() + line.registration_id.invoice_line_id = invoice_line.id + return invoice + + @api.multi + def _create_payments(self, invoice): + """ Create payment if schedule has been chosen """ + self.ensure_one() + if self.schedule_id and self.schedule_id.occurences > 0: + # TODO: make more intelligent price cut + amount = invoice.amount_total + amount_per_occurence = amount / self.schedule_id.occurences + for day in self.schedule_id.day_ids: + payment = self.env['account.payment'].new({ + 'payment_type': 'inbound', + 'partner_type': 'customer', + 'partner_id': self.member_id.partner_id.id, + 'amount': amount_per_occurence, + 'payment_date': day.day, + 'journal_id': self.journal_id.id, + }) + payment._onchange_journal() + payment_values = dict(payment._cache) + payment = self.env['account.payment'].create(payment_values) + payment.invoice_ids = [(4, invoice.id, False)] @api.multi def validate(self): @@ -51,3 +104,5 @@ class GolemActivityRegistrationInvoicing(models.TransientModel): draft_registrations = self.member_id.activity_registration_ids.filtered( lambda r: r.state == 'draft') draft_registrations.write({'state': 'confirmed'}) + invoice = self._create_invoice() + self._create_payments(invoice) diff --git a/golem_activity_registration_state/wizard/golem_activity_registration_invoicing.xml b/golem_activity_registration_state/wizard/golem_activity_registration_invoicing.xml index edef458b..42a74c95 100644 --- a/golem_activity_registration_state/wizard/golem_activity_registration_invoicing.xml +++ b/golem_activity_registration_state/wizard/golem_activity_registration_invoicing.xml @@ -37,6 +37,9 @@ along with this program. If not, see . +