Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions purchase_deposit/tests/test_purchase_deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,14 @@ def test_deposit_invoice_update_price_and_taxes(self):
deposit_line = self.po.order_line.filtered(lambda p: p.is_deposit)
self.assertEqual(deposit_line.price_unit, 500.0)
self.assertEqual(deposit_line.tax_ids.id, self.tax.id)

def test_not_allow_deposit_greater_than_po_total(self):
self.assertEqual(len(self.po.order_line), 1)
# We create invoice from expense
f = self.create_advance_payment_form()
f.advance_payment_method = "fixed"
wizard = f.save()
wizard.amount = 5000.0
wizard.deposit_account_id = self.account_deposit
with self.assertRaises(UserError):
wizard.create_invoices()
36 changes: 29 additions & 7 deletions purchase_deposit/wizard/purchase_make_invoice_advance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import time
from datetime import datetime

from odoo import api, fields, models
from odoo import Command, api, fields, models
from odoo.exceptions import UserError
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT, float_compare


class PurchaseAdvancePaymentInv(models.TransientModel):
Expand Down Expand Up @@ -106,9 +106,7 @@ def _prepare_deposit_val(self, order, po_line, amount):
"move_type": "in_invoice",
"partner_id": order.partner_id.id,
"invoice_line_ids": [
(
0,
0,
Command.create(
{
"name": name,
"account_id": account_id,
Expand All @@ -117,7 +115,7 @@ def _prepare_deposit_val(self, order, po_line, amount):
"product_uom_id": product.uom_id.id,
"product_id": product.id,
"purchase_line_id": po_line.id,
"tax_ids": [(6, 0, tax_ids)],
"tax_ids": [Command.set(tax_ids)],
"analytic_distribution": po_line.analytic_distribution,
},
)
Expand Down Expand Up @@ -171,6 +169,30 @@ def create_invoices(self):
amount = self.amount
if self.advance_payment_method == "percentage": # Case percent
amount = self.amount / 100 * order.amount_untaxed
# calculate all deposit lines
sum_deposit = (
sum(
line.price_unit
for line in order.order_line
if (line.is_deposit and line.invoice_lines)
)
or 0.00
)
if (
float_compare(
sum_deposit + amount,
order.amount_total,
precision_rounding=order.currency_id.rounding,
)
> 0
):
raise UserError(
self.env._(
"The amount to be registered as deposit can't be "
"greater than total amount of %(name)s.",
name=order.name,
)
)
if product.purchase_method != "purchase":
raise UserError(
self.env._(
Expand Down Expand Up @@ -213,6 +235,6 @@ def _prepare_deposit_product(self):
"type": "service",
"purchase_method": "purchase",
"property_account_expense_id": self.deposit_account_id.id,
"supplier_taxes_id": [(6, 0, self.deposit_taxes_id.ids)],
"supplier_taxes_id": [Command.set(self.deposit_taxes_id.ids)],
"company_id": False,
}
Loading