From 38db0da20a33e464edf393087d16533ede703fcb Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Mon, 25 Nov 2019 17:13:03 +0100 Subject: [PATCH] Update sale_down_payment --- sale_down_payment/__manifest__.py | 5 +- sale_down_payment/models/__init__.py | 1 + sale_down_payment/models/account_payment.py | 59 +++++++++++++++++++++ sale_down_payment/models/sale.py | 9 ++++ sale_down_payment/views/account_payment.xml | 23 ++++++++ sale_down_payment/views/sale.xml | 12 +++++ 6 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 sale_down_payment/models/account_payment.py create mode 100644 sale_down_payment/views/account_payment.xml diff --git a/sale_down_payment/__manifest__.py b/sale_down_payment/__manifest__.py index 844856e..68b82ef 100644 --- a/sale_down_payment/__manifest__.py +++ b/sale_down_payment/__manifest__.py @@ -12,9 +12,9 @@ Sale Down Payment ================= -This module adds a link between payment and sale orders. It allows to see advanced payment directly on the sale order form view. +This module adds a link between payments and sale orders. It allows to see down payments directly on the sale order form view. -After processing a bank statement, you can start a wizard to link unreconciled incoming payments to a sale order. +After processing a bank statement, you can start a wizard to link unreconciled incoming payments to a sale order. There is also a button *Register Payment* on the sale order. This module targets B2B companies that don't want to generate a down payment invoice for an advanced payment. @@ -29,6 +29,7 @@ This module has been written by Alexis de Lattre from Akretion 'views/account_bank_statement.xml', 'views/sale.xml', 'views/account_move_line.xml', + 'views/account_payment.xml', ], 'installable': True, } diff --git a/sale_down_payment/models/__init__.py b/sale_down_payment/models/__init__.py index 33896fc..a967445 100644 --- a/sale_down_payment/models/__init__.py +++ b/sale_down_payment/models/__init__.py @@ -1,2 +1,3 @@ from . import sale from . import account_move_line +from . import account_payment diff --git a/sale_down_payment/models/account_payment.py b/sale_down_payment/models/account_payment.py new file mode 100644 index 0000000..a10af15 --- /dev/null +++ b/sale_down_payment/models/account_payment.py @@ -0,0 +1,59 @@ +# Copyright 2019 Akretion France (http://www.akretion.com) +# @author Alexis de Lattre +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models +from odoo.tools import float_round + + +class AccountPayment(models.Model): + _inherit = 'account.payment' + + sale_id = fields.Many2one('sale.order', string='Sale Order') + + def action_validate_invoice_payment(self): + if self.sale_id: + self.post() + else: + return super(AccountPayment, self).\ + action_validate_invoice_payment() + + def _get_counterpart_move_line_vals(self, invoice=False): + res = super(AccountPayment, self)._get_counterpart_move_line_vals( + invoice=invoice) + if self.sale_id: + res['sale_id'] = self.sale_id.id + return res + + +class AccountAbstractPayment(models.AbstractModel): + _inherit = "account.abstract.payment" + + def default_get(self, fields_list): + res = super(AccountAbstractPayment, self).default_get(fields_list) + if ( + self._context.get('active_model') == 'sale.order' and + self._context.get('active_id')): + so = self.env['sale.order'].browse(self._context['active_id']) + res.update({ + 'amount': so.amount_total, + 'currency_id': so.currency_id.id, + 'payment_type': 'inbound', + 'partner_id': so.partner_invoice_id.commercial_partner_id.id, + 'partner_type': 'customer', + 'communication': so.name, + 'sale_id': so.id, + }) + return res + + def _compute_payment_amount(self, invoices=None, currency=None): + amount = super(AccountAbstractPayment, self)._compute_payment_amount( + invoices=invoices, currency=currency) + if self.sale_id: + payment_currency = currency + if not payment_currency: + payment_currency = self.sale_id.currency_id + amount = float_round( + self.sale_id.amount_total - self.sale_id.amount_down_payment, + precision_rounding=payment_currency.rounding) + return amount diff --git a/sale_down_payment/models/sale.py b/sale_down_payment/models/sale.py index b09bdc2..687cc5d 100644 --- a/sale_down_payment/models/sale.py +++ b/sale_down_payment/models/sale.py @@ -3,6 +3,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo import api, fields, models +from odoo.tools import float_round class SaleOrder(models.Model): @@ -13,6 +14,9 @@ class SaleOrder(models.Model): readonly=True) amount_down_payment = fields.Monetary( compute='_compute_amount_down_payment', string='Down Payment Amount') + # amount_residual : only used to hide 'Register Payment' button + amount_residual = fields.Monetary( + compute='_compute_amount_down_payment', string='Residual') @api.depends( 'payment_line_ids.credit', 'payment_line_ids.debit', @@ -36,4 +40,9 @@ class SaleOrder(models.Model): down_payment -= sale.company_id.currency_id._convert( pl.balance, sale_currency, sale.company_id, pl.date) + down_payment = float_round( + down_payment, precision_rounding=sale.currency_id.rounding) sale.amount_down_payment = down_payment + sale.amount_residual = float_round( + sale.amount_total - down_payment, + precision_rounding=sale.currency_id.rounding) diff --git a/sale_down_payment/views/account_payment.xml b/sale_down_payment/views/account_payment.xml new file mode 100644 index 0000000..e833311 --- /dev/null +++ b/sale_down_payment/views/account_payment.xml @@ -0,0 +1,23 @@ + + + + + + + + account.payment.sale.form + account.payment + + + + + + + + + + diff --git a/sale_down_payment/views/sale.xml b/sale_down_payment/views/sale.xml index 8190535..bbe7448 100644 --- a/sale_down_payment/views/sale.xml +++ b/sale_down_payment/views/sale.xml @@ -8,6 +8,14 @@ + + Register Payment + account.payment + form + + new + + advance_payment.sale.order.form sale.order @@ -16,6 +24,7 @@ + @@ -24,6 +33,9 @@ +