Skip to content

Commit

Permalink
[MIG] purchase_invoice_plan: Migration to 15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Saran440 authored and AungKoKoLin1997 committed Sep 15, 2023
1 parent 8410771 commit 2552359
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
2 changes: 1 addition & 1 deletion purchase_invoice_plan/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"name": "Purchase Invoice Plan",
"summary": "Add to purchases order, ability to manage future invoice plan",
"version": "14.0.1.3.1",
"version": "15.0.1.0.0",
"author": "Ecosoft,Odoo Community Association (OCA)",
"license": "AGPL-3",
"website": "https://github.com/OCA/purchase-workflow",
Expand Down
14 changes: 4 additions & 10 deletions purchase_invoice_plan/models/purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class PurchaseOrder(models.Model):
copy=False,
)
use_invoice_plan = fields.Boolean(
string="Use Invoice Plan",
default=False,
copy=False,
)
Expand Down Expand Up @@ -158,11 +157,8 @@ class PurchaseInvoicePlan(models.Model):
store=True,
index=True,
)
installment = fields.Integer(
string="Installment",
)
installment = fields.Integer()
plan_date = fields.Date(
string="Plan Date",
required=True,
)
invoice_type = fields.Selection(
Expand All @@ -177,12 +173,10 @@ class PurchaseInvoicePlan(models.Model):
help="Last installment will create invoice use remaining amount",
)
percent = fields.Float(
string="Percent",
digits="Purchase Invoice Plan Percent",
help="This percent will be used to calculate new quantity",
)
amount = fields.Float(
string="Amount",
digits="Product Price",
compute="_compute_amount",
inverse="_inverse_amount",
Expand Down Expand Up @@ -295,7 +289,7 @@ def _compute_new_invoice_quantity(self, invoice_move):
if self.last: # For last install, let the system do the calc.
return
percent = self.percent
move = invoice_move.with_context({"check_move_validity": False})
move = invoice_move.with_context(**{"check_move_validity": False})
for line in move.invoice_line_ids:
self._update_new_quantity(line, percent)
move.line_ids.filtered("exclude_from_invoice_tab").unlink()
Expand All @@ -308,10 +302,10 @@ def _update_new_quantity(self, line, percent):
if float_compare(abs(plan_qty), abs(line.quantity), prec) == 1:
raise ValidationError(
_(
"Plan quantity: %s, exceed invoiceable quantity: %s"
"Plan quantity: %(plan)s, exceed invoiceable quantity: %(qty)s"
"\nProduct should be delivered before invoice"
)
% (plan_qty, line.quantity)
% {"plan": plan_qty, "qty": line.quantity}
)
line.write({"quantity": plan_qty})

Expand Down
52 changes: 43 additions & 9 deletions purchase_invoice_plan/tests/test_purchase_invoice_plan.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright 2019 Ecosoft (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from freezegun import freeze_time

from odoo import fields
from odoo.exceptions import ValidationError
from odoo.exceptions import UserError, ValidationError
from odoo.tests.common import Form, TransactionCase


Expand Down Expand Up @@ -70,7 +72,7 @@ def test_invoice_plan(self):
with Form(self.PurchaseInvoicePlan) as p:
p.num_installment = 5
purchase_plan = p.save()
purchase_plan.with_context(ctx).purchase_create_invoice_plan()
purchase_plan.with_context(**ctx).purchase_create_invoice_plan()
# Change plan, so that the 1st installment is 1000 and 5th is 3000
self.assertEqual(len(self.test_po_product.invoice_plan_ids), 5)
self.test_po_product.invoice_plan_ids[0].amount = 1000
Expand All @@ -82,7 +84,7 @@ def test_invoice_plan(self):
receive.move_ids_without_package.quantity_done = 10.0
receive._action_done()
purchase_create = self.env["purchase.make.planned.invoice"].create({})
purchase_create.with_context(ctx).create_invoices_by_plan()
purchase_create.with_context(**ctx).create_invoices_by_plan()
self.assertEqual(
self.test_po_product.amount_total,
sum(self.test_po_product.invoice_ids.mapped("amount_total")),
Expand All @@ -96,7 +98,7 @@ def test_unlink_invoice_plan(self):
with Form(self.PurchaseInvoicePlan) as p:
p.num_installment = 5
plan = p.save()
plan.with_context(ctx).purchase_create_invoice_plan()
plan.with_context(**ctx).purchase_create_invoice_plan()
# Remove it
self.test_po_product.remove_invoice_plan()
self.assertFalse(self.test_po_product.invoice_plan_ids)
Expand All @@ -107,28 +109,40 @@ def test_error(self):
"active_ids": [self.test_po_product.id],
"all_remain_invoices": True,
}
# UserError Use Invoice Plan selected, but no plan created
with self.assertRaises(UserError):
self.test_po_product.button_confirm()
# ValidationError Number of Installment <= 1
with self.assertRaises(ValidationError) as e:
with Form(self.PurchaseInvoicePlan) as p:
p.num_installment = 0
p.save()
p.save()
error_message = "Number Installment must greater than 1"
self.assertEqual(e.exception.args[0], error_message)
# Create purchase plan
with Form(self.PurchaseInvoicePlan) as p:
p.num_installment = 5
purchase_plan = p.save()
purchase_plan.with_context(ctx).purchase_create_invoice_plan()
purchase_plan.with_context(**ctx).purchase_create_invoice_plan()
# Test exceed percent
with self.assertRaises(UserError):
self.test_po_product.invoice_plan_ids[0].percent = 99
self.test_po_product._check_ip_total_percent()
self.test_po_product.button_confirm()
self.assertEqual(self.test_po_product.state, "purchase")
# UserError Please fill percentage for all invoice plan lines
with self.assertRaises(UserError):
for per in self.test_po_product.invoice_plan_ids:
per.percent = 0
self.test_po_product._check_invoice_plan()
# Receive product 1 unit
receive = self.test_po_product.picking_ids.filtered(lambda l: l.state != "done")
receive.move_ids_without_package.quantity_done = 1.0
receive._action_done()
# ValidationError Create all invoice plan - Receive < Invoice require
purchase_create = self.env["purchase.make.planned.invoice"].create({})
with self.assertRaises(ValidationError) as e:
purchase_create.with_context(ctx).create_invoices_by_plan()
purchase_create.with_context(**ctx).create_invoices_by_plan()
error_message = (
"Plan quantity: 2.0, exceed invoiceable quantity: 1.0"
"\nProduct should be delivered before invoice"
Expand All @@ -148,7 +162,7 @@ def test_invoice_plan_po_edit(self):
with Form(self.PurchaseInvoicePlan) as p:
p.num_installment = 5
purchase_plan = p.save()
purchase_plan.with_context(ctx).purchase_create_invoice_plan()
purchase_plan.with_context(**ctx).purchase_create_invoice_plan()
# Change plan, so that the 1st installment is 1000 and 5th is 3000
self.assertEqual(len(self.test_po_product.invoice_plan_ids), 5)
first_install = self.test_po_product.invoice_plan_ids[0]
Expand All @@ -162,7 +176,7 @@ def test_invoice_plan_po_edit(self):
receive._action_done()
purchase_create = self.env["purchase.make.planned.invoice"].create({})
# Create only the 1st invoice, amount should be 1000, and percent is 10
purchase_create.with_context(ctx).create_invoices_by_plan()
purchase_create.with_context(**ctx).create_invoices_by_plan()
self.assertEqual(first_install.amount, 1000)
self.assertEqual(first_install.percent, 10)
# Add new PO line with amount = 1000, check that only percent is changed
Expand All @@ -189,3 +203,23 @@ def test_invoice_plan_po_edit(self):
self.test_po_product.invoice_plan_ids._compute_amount()
self.assertEqual(first_install.amount, 1000)
self.assertEqual(first_install.percent, 9.090909)
with self.assertRaises(UserError):
self.test_po_product.remove_invoice_plan()

@freeze_time("2022-01-01")
def test_next_date(self):
ctx = {
"active_id": self.test_po_product.id,
"active_ids": [self.test_po_product.id],
"all_remain_invoices": False,
}
# Create purchase plan
for item in ["day", "month", "year"]:
with Form(self.PurchaseInvoicePlan) as p:
p.num_installment = 5
p.interval = 5
p.interval_type = item
purchase_plan = p.save()
purchase_plan.with_context(**ctx).purchase_create_invoice_plan()
self.assertEqual(len(self.test_po_product.invoice_plan_ids), 5)
self.test_po_product.remove_invoice_plan()
3 changes: 0 additions & 3 deletions purchase_invoice_plan/wizard/purchase_create_invoice_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@ class PurchaseCreateInvoicePlan(models.TransientModel):
required=True,
)
installment_date = fields.Date(
string="Installment Date",
default=fields.Date.context_today,
required=True,
)
interval = fields.Integer(
string="Interval",
default=1,
required=True,
)
interval_type = fields.Selection(
[("day", "Day"), ("month", "Month"), ("year", "Year")],
string="Interval Type",
default="month",
required=True,
)
Expand Down

0 comments on commit 2552359

Please sign in to comment.