Skip to content

Commit

Permalink
[14.0][IMP] purchase_ivoice_plan, non-editable installment
Browse files Browse the repository at this point in the history
  • Loading branch information
kittiu authored and AungKoKoLin1997 committed Sep 15, 2023
1 parent d02337f commit 4302f38
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
1 change: 1 addition & 0 deletions purchase_invoice_plan/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"depends": ["purchase_open_qty", "purchase_stock"],
"data": [
"security/ir.model.access.csv",
"data/purchase_data.xml",
"wizard/purchase_create_invoice_plan_view.xml",
"wizard/purchase_make_planned_invoice_view.xml",
"views/purchase_view.xml",
Expand Down
6 changes: 6 additions & 0 deletions purchase_invoice_plan/data/purchase_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<odoo noupdate="1">
<record id="decimal_price" model="decimal.precision" forcecreate="True">
<field name="name">Purchase Invoice Plan Percent</field>
<field name="digits">6</field>
</record>
</odoo>
38 changes: 34 additions & 4 deletions purchase_invoice_plan/models/purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class PurchaseOrder(models.Model):
inverse_name="purchase_id",
string="Invoice Plan",
copy=False,
readonly=True,
states={"draft": [("readonly", False)]},
)
use_invoice_plan = fields.Boolean(
string="Use Invoice Plan",
Expand Down Expand Up @@ -52,6 +50,14 @@ def _compute_ip_invoice_plan(self):
and len(rec.invoice_plan_ids.filtered(lambda l: not l.invoiced))
)

@api.constrains("invoice_plan_ids")
def _check_ip_total_percent(self):
for rec in self:
installments = rec.invoice_plan_ids.filtered("installment")
ip_total_percent = sum(installments.mapped("percent"))
if float_round(ip_total_percent, 0) > 100:
raise UserError(_("Invoice plan total percentage must not exceed 100%"))

@api.constrains("state")
def _check_invoice_plan(self):
for rec in self:
Expand All @@ -73,7 +79,7 @@ def create_invoice_plan(
self.invoice_plan_ids.unlink()
invoice_plans = []
Decimal = self.env["decimal.precision"]
prec = Decimal.precision_get("Product Unit of Measure")
prec = Decimal.precision_get("Purchase Invoice Plan Percent")
percent = float_round(1.0 / num_installment * 100, prec)
percent_last = 100 - (percent * (num_installment - 1))
for i in range(num_installment):
Expand Down Expand Up @@ -171,7 +177,7 @@ class PurchaseInvoicePlan(models.Model):
)
percent = fields.Float(
string="Percent",
digits="Product Unit of Measure",
digits="Purchase Invoice Plan Percent",
help="This percent will be used to calculate new quantity",
)
amount = fields.Float(
Expand Down Expand Up @@ -201,6 +207,9 @@ class PurchaseInvoicePlan(models.Model):
help="If this line already invoiced",
store=True,
)
no_edit = fields.Boolean(
compute="_compute_no_edit",
)

@api.depends("percent")
def _compute_amount(self):
Expand Down Expand Up @@ -256,6 +265,14 @@ def _compute_last(self):
last = max(rec.purchase_id.invoice_plan_ids.mapped("installment"))
rec.last = rec.installment == last

def _no_edit(self):
self.ensure_one()
return self.invoiced

def _compute_no_edit(self):
for rec in self:
rec.no_edit = rec._no_edit()

def _compute_new_invoice_quantity(self, invoice_move):
self.ensure_one()
if self.last: # For last install, let the system do the calc.
Expand Down Expand Up @@ -284,3 +301,16 @@ def _update_new_quantity(self, line, percent):
def _get_plan_qty(self, order_line, percent):
plan_qty = order_line.product_qty * (percent / 100)
return plan_qty

def unlink(self):
lines = self.filtered("no_edit")
if lines:
installments = [str(x) for x in lines.mapped("installment")]
raise UserError(
_(
"Installment %s: already used and not allowed to delete.\n"
"Please discard changes."
)
% ", ".join(installments)
)
return super().unlink()
30 changes: 24 additions & 6 deletions purchase_invoice_plan/views/purchase_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,29 @@
<field name="model">purchase.invoice.plan</field>
<field name="arch" type="xml">
<tree editable="bottom">
<field name="installment" />
<field name="plan_date" />
<field name="invoice_type" />
<field name="percent" optional="show" />
<field name="amount" optional="show" />
<field name="no_edit" invisible="1" />
<field
name="installment"
attrs="{'readonly': [('no_edit', '=', True)]}"
/>
<field
name="plan_date"
attrs="{'readonly': [('no_edit', '=', True)]}"
/>
<field
name="invoice_type"
attrs="{'readonly': [('no_edit', '=', True)]}"
/>
<field
name="percent"
optional="show"
attrs="{'readonly': [('no_edit', '=', True)]}"
/>
<field
name="amount"
optional="show"
attrs="{'readonly': [('no_edit', '=', True)]}"
/>
<field name="to_invoice" />
<field name="invoiced" />
<field name="invoice_ids" optional="hide" widget="many2many_tags" />
Expand Down Expand Up @@ -85,7 +103,7 @@
<field
name="invoice_plan_ids"
context="{'tree_view_ref': 'view_purchase_invoice_plan_tree'}"
attrs="{'invisible': [('invoice_plan_ids', '=', [])], 'readonly': [('invoice_count', '>', 0)]}"
attrs="{'invisible': [('invoice_plan_ids', '=', [])]}"
/>
<group class="oe_subtotal_footer oe_right">
<field name="ip_total_percent" />
Expand Down

0 comments on commit 4302f38

Please sign in to comment.