Skip to content

Commit

Permalink
[MIG] account_payment_group_financial_surcharge: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pablohmontenegro authored and jjscarafia committed Sep 1, 2023
1 parent f60cb16 commit 7005cc1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
4 changes: 2 additions & 2 deletions account_payment_group_financial_surcharge/__manifest__.py
@@ -1,6 +1,6 @@
{
"name": "Payment Groups with Financial Surchange",
"version": "15.0.1.0.1",
"version": "16.0.1.0.0",
"author": "ADHOC SA",
"license": "AGPL-3",
"category": "Payment",
Expand All @@ -20,6 +20,6 @@
],
'images': [
],
'installable': False,
'installable': True,
'auto_install': False,
}
Expand Up @@ -62,6 +62,7 @@ def _onchange_instalment(self):
for rec in self:
rec._inverse_net_amount()

@api.onchange('net_amount')
def _inverse_net_amount(self):
for rec in self:
rec.with_context(skip_account_move_synchronization=True).amount = rec.net_amount * (rec.installment_id.surcharge_coefficient or 1)
Expand Down
Expand Up @@ -26,32 +26,38 @@ def post(self):
raise UserError(
"To validate payment with finacing plan is necessary to have a product surcharge in the "
"company of the payment. Please check this in the Account Config")

taxes = product.taxes_id.filtered(lambda t: t.company_id.id == self.company_id.id)
journal = self.env['account.journal'].search([('type', '=', 'sale'), ('company_id', '=', self.company_id.id)], limit=1)
wiz = self.env['account.payment.group.invoice.wizard'].with_context(
is_automatic_subcharge=True, active_id=self.id, internal_type='debit_note').create({
'journal_id': journal.id,
'product_id': product.id,
'tax_ids': [(6, 0, taxes.ids)],
'amount_total': self.financing_surcharge,
})
wiz._onchange_journal_id()
wiz.change_payment_group()
wiz.amount_total = self.financing_surcharge
wiz.confirm()

# If we are registering a payment of a draft invoice then we need to remove the invoice from the debts of the payment group
# in order to be able to post/reconcile the payment group (this is needed because in odoo 15 we are not able to renconcile
# draft account.move. only can reconile posted ones)
if self.env.context.get('open_invoice_payment'):
move_line_ids = self._context.get('to_pay_move_line_ids')
move_lines = move_line_ids and self.env['account.move.line'].browse(move_line_ids) or self.env['account.move.line']
if not move_lines:
move_lines = self.to_pay_move_line_ids
draft_invoices = move_lines and move_lines.mapped('move_id').filtered(lambda x: x.state == 'draft')
if draft_invoices:
# remove draft invoice from debt
self.to_pay_move_line_ids -= self.to_pay_move_line_ids.filtered(lambda aml: aml.move_id in draft_invoices)
## Obtengo las notas de debito relacionadas con el grupo de pago.
## y computo la suma del precio toal del producto de surchage y si es menor al residual de las notas de debito
## lo seteo como monto a facturar restandolo de el recargo caculado (esperado)
related_debit_note = self.to_pay_move_line_ids.mapped('move_id').filtered(lambda x: x.l10n_latam_document_type_id.internal_type == 'debit_note')
surchage_products_total = sum(related_debit_note.mapped('line_ids').filtered(lambda x: x.product_id == product).mapped('price_total'))
financing_surcharge_to_invoice = self.financing_surcharge - (min(surchage_products_total, sum(related_debit_note.mapped('amount_residual'))))
if financing_surcharge_to_invoice > 0:
taxes = product.taxes_id.filtered(lambda t: t.company_id.id == self.company_id.id)
journal = self.env['account.journal'].search([('type', '=', 'sale'), ('company_id', '=', self.company_id.id)], limit=1)
wiz = self.env['account.payment.group.invoice.wizard'].with_context(
is_automatic_subcharge=True, active_id=self.id, internal_type='debit_note').create({
'journal_id': journal.id,
'product_id': product.id,
'tax_ids': [(6, 0, taxes.ids)],
'amount_total': financing_surcharge_to_invoice,
})
wiz._onchange_journal_id()
wiz.change_payment_group()
wiz.amount_total = financing_surcharge_to_invoice
wiz.confirm()

# If we are registering a payment of a draft invoice then we need to remove the invoice from the debts of the payment group
# in order to be able to post/reconcile the payment group (this is needed because in odoo 16 we are not able to renconcile
# draft account.move. only can reconcile posted ones)
if self.env.context.get('open_invoice_payment'):
move_line_ids = self._context.get('to_pay_move_line_ids')
move_lines = move_line_ids and self.env['account.move.line'].browse(move_line_ids) or self.env['account.move.line']
if not move_lines:
move_lines = self.to_pay_move_line_ids
draft_invoices = move_lines and move_lines.mapped('move_id').filtered(lambda x: x.state == 'draft')
if draft_invoices:
# remove draft invoice from debt
self.to_pay_move_line_ids -= self.to_pay_move_line_ids.filtered(lambda aml: aml.move_id in draft_invoices)

return super().post()
Expand Up @@ -7,7 +7,7 @@
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<button id="account_invoice_payment_btn" position="attributes">
<attribute name="attrs">{'invisible': ['|', '|', ('state', '=', 'cancel'), ('payment_state', 'not in', ['not_paid', 'patial']), ('move_type', 'not in', ('out_invoice', 'out_refund', 'in_invoice', 'in_refund', 'out_receipt', 'in_receipt'))]}</attribute>
<attribute name="attrs">{'invisible': ['|', '|', ('state', '=', 'cancel'), ('payment_state', 'not in', ['not_paid', 'partial']), ('move_type', 'not in', ('out_invoice', 'out_refund', 'in_invoice', 'in_refund', 'out_receipt', 'in_receipt'))]}</attribute>
<attribute name="context">{'open_invoice_payment': True}</attribute>
</button>
</field>
Expand Down

0 comments on commit 7005cc1

Please sign in to comment.