Skip to content

Commit

Permalink
[IMP] sale_invoice_plan: report on remaining installments
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeddies authored and dreispt committed Dec 27, 2023
1 parent 8899940 commit 8ad05ae
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 5 deletions.
62 changes: 60 additions & 2 deletions sale_invoice_plan/models/sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SaleOrder(models.Model):
invoice_plan_ids = fields.One2many(
comodel_name="sale.invoice.plan",
inverse_name="sale_id",
string="Inovice Plan",
string="Invoice Plan",
copy=False,
)
use_invoice_plan = fields.Boolean(
Expand All @@ -33,14 +33,41 @@ class SaleOrder(models.Model):
compute="_compute_invoice_plan_total",
string="Total Amount",
)
# Reporting
next_installment_id = fields.Many2one(
"sale.invoice.plan",
string="Next Installmen To Invoice",
compute="_compute_next_to_invoice_plan_id",
store=True,
copy=False,
)
next_installment_date = fields.Date(
related="next_installment_id.plan_date",
string="Next Installment Date",
store=True,
)
invoiced_installment_amount = fields.Monetary(
compute="_compute_invoiced_installment_amount",
store=True,
)
pending_installment_amount = fields.Monetary(
compute="_compute_pending_installment_amount",
store=True,
)

@api.depends("invoice_plan_ids")
@api.depends("invoice_plan_ids.percent", "invoice_plan_ids.amount")
def _compute_invoice_plan_total(self):
for rec in self:
installments = rec.invoice_plan_ids.filtered("installment")
rec.invoice_plan_total_percent = sum(installments.mapped("percent"))
rec.invoice_plan_total_amount = sum(installments.mapped("amount"))

@api.depends(
"use_invoice_plan",
"state",
"invoice_status",
"invoice_plan_ids.invoiced",
)
def _compute_invoice_plan_process(self):
for rec in self:
has_invoice_plan = rec.use_invoice_plan and rec.invoice_plan_ids
Expand All @@ -52,6 +79,37 @@ def _compute_invoice_plan_process(self):
and rec.invoice_status in ["to invoice", "no"]
)

@api.depends("invoice_plan_ids.invoice_move_ids", "invoice_plan_ids.invoiced")
def _compute_next_to_invoice_plan_id(self):
for sale in self:
last_invoiced = sale.invoice_plan_ids.filtered("invoice_move_ids")[-1:]
last_installment = last_invoiced.installment or 0
next_to_invoice = sale.invoice_plan_ids.filtered(
lambda x: x.installment == last_installment + 1
)
sale.next_installment_id = next_to_invoice

@api.depends(
"next_installment_id",
"invoice_plan_ids.amount_invoiced",
"invoice_plan_ids.invoice_move_ids.state",
)
def _compute_invoiced_installment_amount(self):
for sale in self:
# Invoice Plan computations are not always triggered when changes happen
# So we need to force that recomputation
sale.invoice_plan_ids._compute_invoiced()
invoiced_plans = sale.invoice_plan_ids.filtered("invoiced")
invoiced_amount = sum(invoiced_plans.mapped("amount_invoiced"))
sale.invoiced_installment_amount = invoiced_amount

@api.depends("invoice_plan_total_amount", "invoiced_installment_amount")
def _compute_pending_installment_amount(self):
for sale in self:
sale.pending_installment_amount = (
sale.invoice_plan_total_amount - sale.invoiced_installment_amount
)

@api.constrains("invoice_plan_ids")
def _check_invoice_plan_total_percent(self):
for rec in self:
Expand Down
58 changes: 56 additions & 2 deletions sale_invoice_plan/views/sale_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@
<label for="use_invoice_plan" />
</div>
</xpath>
<xpath expr="//button[@name='payment_action_capture']" position="before">
<xpath expr="//button[@name='action_quotation_send']" position="before">
<field name="invoice_plan_process" invisible="1" />
<button
name="%(action_view_sale_make_planned_invoice)d"
string="Create Invoice by Plan"
type="action"
class="btn-primary"
attrs="{'invisible': [('invoice_plan_process', '=', False)]}"
/>
</xpath>
Expand Down Expand Up @@ -121,6 +120,61 @@
</xpath>
</field>
</record>
<record id="view_order_tree_invoice_plan" model="ir.ui.view">
<field name="name">view.order.tree.invoice.plan</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_quotation_tree" />
<field name="arch" type="xml">
<tree position="inside">
<field name="next_installment_date" optional="hide" />
<field name="invoiced_installment_amount" optional="hide" />
<field name="pending_installment_amount" optional="hide" />
</tree>
</field>
</record>
<record id="view_order_search_invoice_plan" model="ir.ui.view">
<field name="name">view.order.search.invoice.plan</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_sales_order_filter" />
<field name="arch" type="xml">
<xpath expr="//filter[@name='my_sale_orders_filter']" position="after">
<separator />
<filter
string="Has Invoice Plan"
name="has_invoice_plan"
domain="[('invoice_plan_ids','!=', False)]"
/>
<filter
string="Has Pending Installment Amount"
name="has_pending_installment_amount"
domain="[('pending_installment_amount','!=', 0)]"
/>
<filter
string="This Months Installments"
name="this_months_installments"
domain="[
('next_installment_date', '>=', (context_today() - relativedelta(day=1)).strftime('%Y-%m-%d')),
('next_installment_date', '&lt;=', (context_today() + relativedelta(months=1, day=1, days=-1)).strftime('%Y-%m-%d'))]"
/>
<filter
string="Previous Month Installments"
name="last_months_installments"
domain="[
('next_installment_date', '>=', (context_today() - relativedelta(months=1, day=1)).strftime('%Y-%m-%d')),
('next_installment_date', '&lt;=', (context_today() - relativedelta(day=1, days=1)).strftime('%Y-%m-%d'))]"
/>
<separator />
</xpath>
<group position="inside">
<filter
string="Next Installment Date"
name="groupby_next_installment_date"
domain="[]"
context="{'group_by': 'next_installment_date'}"
/>
</group>
</field>
</record>
<!-- Invoice Plan Lines -->
<record id="view_sale_invoice_plan_filter" model="ir.ui.view">
<field name="name">view.sale.invoice.plan.filter</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</field>
</record>
<record id="action_view_sale_make_planned_invoice" model="ir.actions.act_window">
<field name="name">Invoice Order</field>
<field name="name">Invoice Installments</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">sale.make.planned.invoice</field>
<field name="binding_view_types">form</field>
Expand Down

0 comments on commit 8ad05ae

Please sign in to comment.