diff --git a/sale_payment_web/models/payment.py b/sale_payment_web/models/payment.py index 275fbbd5..b205575c 100644 --- a/sale_payment_web/models/payment.py +++ b/sale_payment_web/models/payment.py @@ -1,4 +1,4 @@ -from odoo import api, models +from odoo import api, fields, models class PaymentTransaction(models.Model): @@ -10,3 +10,9 @@ class PaymentTransaction(models.Model): if active_ids and self._context.get('active_model') == 'sale.order': values['sale_order_ids'] = [(6, 0, active_ids)] return super(PaymentTransaction, self).create(values) + + +class AccountPayment(models.Model): + _inherit = 'account.payment' + + sale_order_id = fields.Many2one('sale.order', string="Sale Order") diff --git a/sale_payment_web/models/sale.py b/sale_payment_web/models/sale.py index 667c1bcd..eaa7b9fc 100644 --- a/sale_payment_web/models/sale.py +++ b/sale_payment_web/models/sale.py @@ -1,9 +1,29 @@ -from odoo import models, _ +from odoo import api, fields, models, _ +from odoo.tools.safe_eval import safe_eval class SaleOrder(models.Model): _inherit = 'sale.order' + manual_payment_ids = fields.One2many('account.payment', 'sale_order_id', string='Manual Payments') + manual_amount_registered_payment = fields.Monetary('Manually Registered Amount', compute='_compute_manual_amount_registered_payment') + manual_amount_remaining = fields.Monetary('Remaining Amount Due', compute='_compute_manual_amount_registered_payment') + + @api.depends('manual_payment_ids.amount', 'amount_total') + def _compute_manual_amount_registered_payment(self): + for so in self: + so.manual_amount_registered_payment = sum(so.manual_payment_ids.mapped('amount')) + so.manual_amount_remaining = so.amount_total - so.manual_amount_registered_payment + + + def action_manual_payments(self): + action = self.env.ref('account.action_account_payments').read()[0] + domain = action['domain'] or '[]' + domain = safe_eval(domain) + domain.append(('id', 'in', self.manual_payment_ids.ids)) + action['domain'] = domain + return action + def action_payment_register(self): return { 'name': _('Register Payment'), diff --git a/sale_payment_web/tests/test_sale_payment.py b/sale_payment_web/tests/test_sale_payment.py index e5255f1a..38bd2edf 100644 --- a/sale_payment_web/tests/test_sale_payment.py +++ b/sale_payment_web/tests/test_sale_payment.py @@ -1,4 +1,5 @@ from odoo.addons.sale.tests.test_sale_to_invoice import TestSaleToInvoice +from odoo.exceptions import UserError class TestSalePayment(TestSaleToInvoice): @@ -13,6 +14,17 @@ class TestSalePayment(TestSaleToInvoice): def test_payment(self): self.sale_order.action_confirm() + + payment_wizard = self.env['account.payment.register'].with_context(self.context).create({'amount': -15}) + self.assertTrue(payment_wizard.journal_id) + with self.assertRaises(UserError): + payment_wizard.create_payments() + + payment_wizard = self.env['account.payment.register'].with_context(self.context).create({'amount': 0}) + self.assertTrue(payment_wizard.journal_id) + with self.assertRaises(UserError): + payment_wizard.create_payments() + payment_wizard = self.env['account.payment.register'].with_context(self.context).create({}) self.assertTrue(payment_wizard.journal_id) @@ -21,3 +33,10 @@ class TestSalePayment(TestSaleToInvoice): payment = self.env[payment_action['res_model']].browse(payment_action['res_id']) self.assertTrue(payment.exists()) self.assertEqual(payment.amount, self.sale_order.amount_total) + + self.assertEqual(payment.sale_order_id, self.sale_order) + + payment_wizard = self.env['account.payment.register'].with_context(self.context).create({'amount': 15}) + self.assertTrue(payment_wizard.journal_id) + with self.assertRaises(UserError): + payment_wizard.create_payments() diff --git a/sale_payment_web/views/sale_views.xml b/sale_payment_web/views/sale_views.xml index b95fdff1..3842dc5b 100644 --- a/sale_payment_web/views/sale_views.xml +++ b/sale_payment_web/views/sale_views.xml @@ -7,7 +7,15 @@ -