2017-06-14 23:50:35 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-07-31 19:59:56 +02:00
|
|
|
# Copyright 2017-2018 Fabien Bourgeois <fabien@yaltik.com>
|
2018-10-23 18:22:04 +02:00
|
|
|
# Copyright 2018 Youssef El Ouahby <youssef@yaltik.com>
|
2017-06-14 23:50:35 +02:00
|
|
|
#
|
2018-07-31 19:59:56 +02:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
2017-06-14 23:50:35 +02:00
|
|
|
#
|
2018-07-31 19:59:56 +02:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
2017-06-14 23:50:35 +02:00
|
|
|
#
|
2018-07-31 19:59:56 +02:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-06-14 23:50:35 +02:00
|
|
|
|
|
|
|
""" GOLEM Activity Registration Invoicing Wizard """
|
|
|
|
|
2017-06-15 01:14:20 +02:00
|
|
|
import logging
|
2018-07-17 10:11:10 +02:00
|
|
|
from math import ceil
|
2018-07-17 11:32:00 +02:00
|
|
|
from odoo import models, fields, api, _
|
2018-10-29 23:21:22 +01:00
|
|
|
from odoo.exceptions import ValidationError
|
2018-07-17 11:32:00 +02:00
|
|
|
from odoo.exceptions import UserError
|
2017-06-15 01:14:20 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-06-14 23:50:35 +02:00
|
|
|
|
|
|
|
class GolemActivityRegistrationInvoicingLine(models.TransientModel):
|
|
|
|
""" GOLEM Activity Registration Invoicing Lines """
|
|
|
|
_name = 'golem.activity.registration.invoicing.line'
|
2018-07-31 19:59:56 +02:00
|
|
|
_description = 'GOLEM Activity Registration Invoicing Lines'
|
2017-06-14 23:50:35 +02:00
|
|
|
|
|
|
|
invoicing_id = fields.Many2one('golem.activity.registration.invoicing',
|
2018-09-26 11:42:35 +02:00
|
|
|
required=True, ondelete='cascade')
|
|
|
|
registration_id = fields.Many2one('golem.activity.registration', required=True,
|
|
|
|
ondelete='cascade')
|
|
|
|
activity_id = fields.Many2one('golem.activity', required=True, readonly=True,
|
|
|
|
ondelete='cascade')
|
2018-08-04 01:09:58 +02:00
|
|
|
price = fields.Monetary()
|
|
|
|
currency_id = fields.Many2one(related='activity_id.currency_id')
|
2018-07-17 09:42:58 +02:00
|
|
|
|
2017-06-14 23:50:35 +02:00
|
|
|
|
|
|
|
class GolemActivityRegistrationInvoicing(models.TransientModel):
|
|
|
|
""" GOLEM Activity Registration Invoicing Wizard """
|
|
|
|
_name = 'golem.activity.registration.invoicing'
|
|
|
|
_description = 'GOLEM Activity Registration Invoicing Wizard'
|
|
|
|
|
2018-07-17 11:32:00 +02:00
|
|
|
state = fields.Selection([('init', 'init'), ('final', 'final')],
|
|
|
|
default='init', required=True)
|
|
|
|
season_id = fields.Many2one('golem.season', 'Season', required=True,
|
|
|
|
ondelete='cascade')
|
|
|
|
member_id = fields.Many2one('golem.member', 'Member', required=True,
|
|
|
|
ondelete='cascade')
|
2018-10-29 23:21:22 +01:00
|
|
|
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')
|
2017-06-14 23:50:35 +02:00
|
|
|
line_ids = fields.One2many('golem.activity.registration.invoicing.line',
|
|
|
|
'invoicing_id', string='Activities')
|
|
|
|
schedule_id = fields.Many2one('golem.payment.schedule', 'Payment schedule',
|
|
|
|
domain='[("season_id", "=", season_id)]',
|
2018-07-17 11:32:00 +02:00
|
|
|
ondelete='cascade',
|
2017-06-14 23:50:35 +02:00
|
|
|
help='If no schedule is selected, only the '
|
|
|
|
'invoice will be create. Otherwise, draft '
|
|
|
|
'payments will be generated.')
|
2018-08-01 18:14:45 +02:00
|
|
|
journal_id = fields.Many2one('account.journal', string='Payment method',
|
2018-07-17 11:32:00 +02:00
|
|
|
domain=[('type', 'in', ('bank', 'cash'))],
|
|
|
|
ondelete='cascade')
|
|
|
|
invoice_id = fields.Many2one('account.invoice', string='Generated invoice',
|
|
|
|
ondelete='cascade')
|
|
|
|
payment_ids = fields.Many2many('account.payment', string='Generated payments')
|
2017-06-15 01:14:20 +02:00
|
|
|
|
2018-10-29 23:21:22 +01:00
|
|
|
@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)]
|
2018-08-05 05:51:38 +02:00
|
|
|
|
|
|
|
def _create_invoice_line(self, product, price, invoice):
|
|
|
|
""" Create invoice line : 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'] = price
|
|
|
|
invoice_line = self.env['account.invoice.line'].create(line_values)
|
|
|
|
invoice.compute_taxes()
|
|
|
|
return invoice_line
|
|
|
|
|
2017-06-15 01:14:20 +02:00
|
|
|
@api.multi
|
|
|
|
def _create_invoice(self):
|
|
|
|
""" Create invoice and lines """
|
|
|
|
self.ensure_one()
|
2018-10-30 11:48:44 +01:00
|
|
|
if self[0].is_minor:
|
|
|
|
partner = self[0].on_the_name_of
|
2018-10-29 23:21:22 +01:00
|
|
|
else:
|
2018-10-30 11:48:44 +01:00
|
|
|
partner = self[0].member_id.partner_id
|
2018-10-03 23:02:01 +02:00
|
|
|
#check if there is a draft invoice for the current customer
|
|
|
|
member_line = partner.member_lines.filtered(
|
2018-10-23 18:22:04 +02:00
|
|
|
lambda ml: (ml.membership_id.membership_season_id == self.season_id
|
|
|
|
and ml.account_invoice_id.state == 'draft')
|
2018-10-03 23:02:01 +02:00
|
|
|
)
|
|
|
|
if member_line:
|
|
|
|
invoice = member_line[0].account_invoice_id
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
})
|
2018-10-30 11:48:44 +01:00
|
|
|
if self[0].is_minor:
|
|
|
|
invoice.write({'is_minor_invoice': True,
|
|
|
|
'partner_ids': [(6, 0, [self[0].on_the_name_of.id,
|
|
|
|
self[0].member_id.partner_id.id])]})
|
2017-06-15 01:14:20 +02:00
|
|
|
for line in self.line_ids:
|
2018-10-03 23:02:01 +02:00
|
|
|
product = line.activity_id.product_id.product_variant_id
|
2018-08-05 05:51:38 +02:00
|
|
|
invoice_line = self._create_invoice_line(product, line.price, invoice)
|
2017-06-15 01:14:20 +02:00
|
|
|
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()
|
2018-07-17 11:32:00 +02:00
|
|
|
payments = self.env['account.payment']
|
2017-06-15 01:14:20 +02:00
|
|
|
if self.schedule_id and self.schedule_id.occurences > 0:
|
|
|
|
amount = invoice.amount_total
|
2018-07-17 10:11:10 +02:00
|
|
|
amount_per_occurence = ceil(amount / self.schedule_id.occurences)
|
|
|
|
for index, day in enumerate(self.schedule_id.day_ids):
|
|
|
|
payment_amount = (amount_per_occurence if index !=
|
|
|
|
(len(self.schedule_id.day_ids.ids) - 1)
|
|
|
|
else amount)
|
2017-06-15 01:14:20 +02:00
|
|
|
payment = self.env['account.payment'].new({
|
|
|
|
'payment_type': 'inbound',
|
|
|
|
'partner_type': 'customer',
|
|
|
|
'partner_id': self.member_id.partner_id.id,
|
2018-07-17 10:11:10 +02:00
|
|
|
'amount': payment_amount,
|
2017-06-15 01:14:20 +02:00
|
|
|
'payment_date': day.day,
|
|
|
|
'journal_id': self.journal_id.id,
|
|
|
|
})
|
2018-07-17 10:11:10 +02:00
|
|
|
amount -= amount_per_occurence
|
2017-06-15 01:14:20 +02:00
|
|
|
payment._onchange_journal()
|
|
|
|
payment_values = dict(payment._cache)
|
|
|
|
payment = self.env['account.payment'].create(payment_values)
|
|
|
|
payment.invoice_ids = [(4, invoice.id, False)]
|
2018-07-17 11:32:00 +02:00
|
|
|
payments |= payment
|
|
|
|
return payments
|
2017-06-14 23:50:35 +02:00
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def validate(self):
|
|
|
|
""" Validate and create invoice and payments """
|
|
|
|
self.ensure_one()
|
2018-10-29 23:21:22 +01:00
|
|
|
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)
|
2018-08-05 05:51:38 +02:00
|
|
|
self[0].line_ids.mapped('registration_id').write({'state': 'confirmed'})
|
2017-06-15 01:14:20 +02:00
|
|
|
invoice = self._create_invoice()
|
2018-07-17 11:32:00 +02:00
|
|
|
self.invoice_id = invoice
|
|
|
|
payments = self._create_payments(invoice)
|
|
|
|
self.payment_ids |= payments
|
|
|
|
self.write({'state': 'final'})
|
|
|
|
return {
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
'view_mode': 'form',
|
|
|
|
'res_model': self._name,
|
|
|
|
'res_id': self[0].id,
|
|
|
|
'target': 'new'
|
|
|
|
}
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def go_invoice(self):
|
|
|
|
""" Navigate to generated invoice """
|
|
|
|
self.ensure_one()
|
|
|
|
if not self.invoice_id:
|
|
|
|
uerr = _('There is no generated invoice.')
|
|
|
|
raise UserError(uerr)
|
|
|
|
return {
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
'name': _('Generated invoice'),
|
|
|
|
'view_mode': 'form,tree',
|
|
|
|
'res_model': 'account.invoice',
|
|
|
|
'res_id': self[0].invoice_id.id
|
|
|
|
}
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def go_payments(self):
|
|
|
|
""" Navigate to generated payments """
|
|
|
|
self.ensure_one()
|
|
|
|
if not self.payment_ids:
|
|
|
|
uerr = _('There is no generated payments.')
|
|
|
|
raise UserError(uerr)
|
|
|
|
return {
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
'name': _('Generated payments'),
|
|
|
|
'view_mode': 'tree,form',
|
|
|
|
'res_model': 'account.payment',
|
|
|
|
'domain': [('id', 'in', self.payment_ids.ids)]
|
|
|
|
}
|