Sélectionner une révision Git
sale_intervention.py
account_invoice.py 9,81 Kio
# © 2020 Le Filament (<http://www.le-filament.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import models, fields, api, _
from odoo.exceptions import UserError
class ScopAccountInvoice(models.Model):
_inherit = "account.invoice"
@api.model
def default_get(self, fields):
"""
Attribue la valeur du journal des cotisations par défaut si la facture
est de type 'is_contribution'
Affecte le type 'is_contribution' par défaut si la facture est sur le
journal des cotisations
:param fields:
:return:
"""
res = super(ScopAccountInvoice, self).default_get(fields)
if res.get('is_contribution'):
res['journal_id'] =\
self.env.user.company_id.contribution_journal_id.id
else:
if res.get('journal_id') == \
self.env.user.company_id.contribution_journal_id.id:
res['is_contribution'] = True
return res
liasse_fiscale_id = fields.Many2one(
comodel_name='scop.liasse.fiscale',
string='Liasse Fiscale')
year = fields.Integer('Année de cotisation')
is_contribution = fields.Boolean("Cotisation", default=False)
type_contribution_id = fields.Many2one(
comodel_name="scop.contribution.type",
string="Type de cotisation",
readonly=True)
partner_ur_id = fields.Many2one(
comodel_name='union.regionale',
string='UR Adhérent',
related='partner_id.ur_id',
store=True
)
nb_quarter = fields.Selection(
string='Nombre de trimestres de cotisation',
selection=[(1, '1'),
(2, '2'),
(3, '3'),
(4, '4')],
default=4,
required=True)
is_sdd = fields.Boolean(
'Au prélèvement',
compute='compute_is_sdd',
search='_search_is_sdd')
# ------------------------------------------------------
# Computed field
# ------------------------------------------------------
@api.multi
def compute_is_sdd(self):
sdd_id = self.env.ref(
'account_banking_sepa_direct_debit.sepa_direct_debit').id
for invoice in self:
if invoice.payment_mode_id and invoice.payment_mode_id.payment_method_id.id == sdd_id:
invoice.is_sdd = True
else:
invoice.is_sdd = False
@api.multi
def _search_is_sdd(self, operator, value):
recs = self.search([]).filtered(lambda x: x.is_sdd is True)
if recs:
return [('id', 'in', [x.id for x in recs])]
# ------------------------------------------------------
# Override Parent
# ------------------------------------------------------
def get_last_maturity_date(self, months, account_move_line_ids):
"""
Get the last maturity date from account_move_line
for a certain period (months = [])
:param months:
:param account_move_line_ids:
:return: last date_maturity
"""
line_ids = account_move_line_ids.filtered(
lambda l: l.date_maturity.month in months)
return line_ids[-1].date_maturity if line_ids else None
@api.multi
def action_invoice_open(self):
"""
Création d'une ligne dans scop.contribution
quand une facture cotisation devient valide
"""
results = super(ScopAccountInvoice, self).action_invoice_open()
for inv in self:
if inv.is_contribution and inv.type == 'out_invoice':
inv.set_scop_contribution()
return results
@api.multi
def action_move_create(self):
"""
Complete override parent
Pass invoice in payment_term.compute function to generate payment
schedule
:return: True
"""
account_move = self.env['account.move']
for inv in self:
if not inv.journal_id.sequence_id:
raise UserError(_('Please define sequence on the journal related to this invoice.'))
if not inv.invoice_line_ids.filtered(lambda line: line.account_id):
raise UserError(_('Please add at least one invoice line.'))
if inv.move_id:
continue
if not inv.date_invoice:
inv.write({'date_invoice': fields.Date.context_today(self)})
if not inv.date_due:
inv.write({'date_due': inv.date_invoice})
company_currency = inv.company_id.currency_id
# create move lines
# (one per invoice line + eventual taxes and analytic lines)
iml = inv.invoice_line_move_line_get()
iml += inv.tax_line_move_line_get()
diff_currency = inv.currency_id != company_currency
# create one move line for the total and possibly adjust
# the other lines amount
total, total_currency, iml = inv.compute_invoice_totals(
company_currency, iml)
name = inv.name or ''
if inv.payment_term_id:
totlines = inv.payment_term_id.with_context(
currency_id=company_currency.id).compute(total, inv.date_invoice, inv)[0]
res_amount_currency = total_currency
for i, t in enumerate(totlines):
if inv.currency_id != company_currency:
amount_currency = company_currency._convert(
t[1],
inv.currency_id,
inv.company_id,
inv._get_currency_rate_date() or fields.Date.today())
else:
amount_currency = False
# last line: add the diff
res_amount_currency -= amount_currency or 0
if i + 1 == len(totlines):
amount_currency += res_amount_currency
iml.append({
'type': 'dest',
'name': name,
'price': t[1],
'account_id': inv.account_id.id,
'date_maturity': t[0],
'amount_currency': diff_currency and amount_currency,
'currency_id': diff_currency and inv.currency_id.id,
'invoice_id': inv.id
})
else:
iml.append({
'type': 'dest',
'name': name,
'price': total,
'account_id': inv.account_id.id,
'date_maturity': inv.date_due,
'amount_currency': diff_currency and total_currency,
'currency_id': diff_currency and inv.currency_id.id,
'invoice_id': inv.id
})
part = self.env['res.partner']._find_accounting_partner(
inv.partner_id)
line = [(0, 0, self.line_get_convert(l, part.id)) for l in iml]
line = inv.group_lines(iml, line)
line = inv.finalize_invoice_move_lines(line)
date = inv.date or inv.date_invoice
move_vals = {
'ref': inv.reference,
'line_ids': line,
'journal_id': inv.journal_id.id,
'date': date,
'narration': inv.comment,
}
move = account_move.create(move_vals)
# Pass invoice in method post: used if you want to get the same
# account move reference when creating the same invoice
# after a cancelled one:
move.post(invoice = inv)
# make the invoice point to that move
vals = {
'move_id': move.id,
'date': date,
'move_name': move.name,
}
inv.write(vals)
return True
# ------------------------------------------------------
# Common Function
# ------------------------------------------------------
@api.multi
def set_scop_contribution(self):
"""
Création d'une ligne dans scop.contribution
"""
for inv in self:
if inv.is_contribution:
year = inv.year
account_type_receivable_ids = inv.env[
'account.account'].search([
('user_type_id', '=', inv.env.ref(
'account.data_account_type_receivable').id)
]).mapped('id')
account_move_line_ids = inv.env['account.move.line'].search([
('invoice_id', '=', inv.id),
('account_id', 'in', account_type_receivable_ids)
]).filtered(lambda l: l.date_maturity.year == year)
inv.env['scop.contribution'].create({
'partner_id': inv.partner_id.id,
'type_id': inv.type_contribution_id.id,
'year': inv.year,
'calculation_date': fields.Datetime.now(),
'amount_calculated': inv.amount_total,
'amount_called': inv.amount_total,
'spreading': inv.nb_quarter,
'spreading': len(
account_move_line_ids.mapped('date_maturity')),
'quarter_1': inv.get_last_maturity_date(
[1, 2, 3], account_move_line_ids),
'quarter_2': inv.get_last_maturity_date(
[4, 5, 6], account_move_line_ids),
'quarter_3': inv.get_last_maturity_date(
[7, 8, 9], account_move_line_ids),
'quarter_4': inv.get_last_maturity_date(
[10, 11, 12], account_move_line_ids),
'invoice_id': inv.id,
})
return True