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
25 changes: 22 additions & 3 deletions purchase_invoice_plan/models/purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ def action_view_invoice(self, invoices=False):
return super().action_view_invoice(invoices=invoices)


class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"

qty_invoiced_before_plan = fields.Float(
string="Invoiced Quantity Before Plan",
digits="Product Unit of Measure",
)


class PurchaseInvoicePlan(models.Model):
_name = "purchase.invoice.plan"
_description = "Invoice Planning Detail"
Expand Down Expand Up @@ -214,7 +223,11 @@ class PurchaseInvoicePlan(models.Model):
@api.depends("percent")
def _compute_amount(self):
for rec in self:
amount_untaxed = rec.purchase_id._origin.amount_untaxed
amount_invoiced = sum(
line.qty_invoiced_before_plan * line.price_unit
for line in rec.purchase_id._origin.order_line
)
amount_untaxed = rec.purchase_id._origin.amount_untaxed - amount_invoiced
# With invoice already created, no recompute
if rec.invoiced:
rec.amount = rec.amount_invoiced
Expand All @@ -234,14 +247,19 @@ def _compute_amount(self):
def _inverse_amount(self):
for rec in self:
if rec.purchase_id.amount_untaxed != 0:
amount_invoiced = sum(
line.qty_invoiced_before_plan * line.price_unit
for line in rec.purchase_id.order_line
)
amount_untaxed = rec.purchase_id.amount_untaxed - amount_invoiced
if rec.last:
installments = rec.purchase_id.invoice_plan_ids.filtered(
lambda l: l.invoice_type == "installment"
)
prev_percent = sum((installments - rec).mapped("percent"))
rec.percent = 100 - prev_percent
continue
rec.percent = rec.amount / rec.purchase_id.amount_untaxed * 100
rec.percent = rec.amount / amount_untaxed * 100
continue
rec.percent = 0

Expand Down Expand Up @@ -310,7 +328,8 @@ def _update_new_quantity(self, line, percent):

@api.model
def _get_plan_qty(self, order_line, percent):
plan_qty = order_line.product_qty * (percent / 100)
product_qty = order_line.product_qty - order_line.qty_invoiced_before_plan
plan_qty = product_qty * (percent / 100)
return plan_qty

@api.ondelete(at_uninstall=False)
Expand Down
5 changes: 2 additions & 3 deletions purchase_invoice_plan/views/purchase_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,19 @@
string="⇒ Create Invoice Plan"
type="action"
class="oe_link"
attrs="{'invisible': ['|', ('invoice_plan_ids', '!=', []), ('invoice_count', '>', 0)]}"
attrs="{'invisible': [('invoice_plan_ids', '!=', [])]}"
/>
<button
name="remove_invoice_plan"
string="⇒ Remove Invoice Plan"
type="object"
class="oe_link"
attrs="{'invisible': ['|', ('invoice_plan_ids', '=', []), ('invoice_count', '>', 0)]}"
attrs="{'invisible': [('invoice_plan_ids', '=', [])]}"
confirm="Are you sure to remove this invoice plan?"
/>
<field
name="invoice_plan_ids"
context="{'tree_view_ref': 'purchase_invoice_plan.view_purchase_invoice_plan_tree'}"
attrs="{'invisible': [('invoice_plan_ids', '=', [])]}"
/>
<group class="oe_subtotal_footer oe_right">
<field name="ip_total_percent" />
Expand Down
2 changes: 2 additions & 0 deletions purchase_invoice_plan/wizard/purchase_create_invoice_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def _check_num_installment(self):
def purchase_create_invoice_plan(self):
purchase = self.env["purchase.order"].browse(self._context.get("active_id"))
self.ensure_one()
for line in purchase.order_line:
line.qty_invoiced_before_plan = line.qty_invoiced
purchase.create_invoice_plan(
self.num_installment,
self.installment_date,
Expand Down