Skip to content

Commit

Permalink
Merge branch '14.0' into 14-clean-delete-and-precommit
Browse files Browse the repository at this point in the history
  • Loading branch information
NoeBerdoz authored Jul 11, 2024
2 parents 019fc1b + 3192f94 commit 645e0da
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
3 changes: 2 additions & 1 deletion account_invoice_split_invoice/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
"author": "Compassion CH",
"website": "https://github.com/CompassionCH/test-repo",
"category": "Accounting",
"depends": ["account"],
"depends": ["account", "sponsorship_compassion"],
"external_dependencies": {},
"data": [
"security/ir.model.access.csv",
"views/account_invoice_split_wizard_view.xml",
],
"demo": [],
Expand Down
2 changes: 2 additions & 0 deletions account_invoice_split_invoice/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_split_invoice_wizard,access.split.invoice.wizard,model_account_invoice_split_wizard,,1,1,1,1
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
<field name="move_id" invisible="1" />
<field
name="invoice_line_ids"
domain="[('move_id', '=', move_id)]"
domain="[('move_id', '=', move_id), ('exclude_from_invoice_tab', '=', False)]"
context="{'tree_view_ref': 'sponsorship_compassion.view_invoice_line_partner_tree'}"
>
<tree editable="bottom" delete="false">
<field name="product_id" readonly="1" />
<field name="name" readonly="1" />
<field name="account_id" readonly="1" />
<field name="quantity" readonly="1" />
<field name="price_unit" readonly="1" />
<field name="price_subtotal" readonly="1" />
<tree>
<field name="product_id" />
<field name="name" />
<field name="contract_id" />
<field name="account_id" />
<field name="quantity" />
<field name="price_unit" />
<field name="price_subtotal" />
</tree>
</field>
</sheet>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,64 @@ def split_invoice(self):
invoice = False

if self.invoice_line_ids:
# Get Receivable line
move_ids = self.invoice_line_ids.mapped("move_id").ids
all_lines = self.env["account.move.line"].search(
[("move_id", "in", move_ids)]
)
old_receivable_line = all_lines.filtered(
lambda line: line.account_id.internal_type == "receivable"
)

# Compute debit val for old/new Receivable line
amount_total_new_receivable = 0.0
for price_tmp in self.invoice_line_ids:
amount_total_new_receivable += price_tmp.price_total
amount_total_old_receivable = (
old_receivable_line.price_unit + amount_total_new_receivable
)

# Get old invoice and copy it for new invoice
old_invoice = self.invoice_line_ids[0].move_id
if old_invoice.state in ("draft", "posted"):
invoice = self._copy_invoice(old_invoice)

# Create new Receivable line for the new invoice
tmp = (
self.env["account.move.line"]
.with_context(check_move_validity=False)
.create(
{
"price_unit": -amount_total_new_receivable,
"move_id": invoice.id,
"quantity": 1,
"exclude_from_invoice_tab": True,
"account_id": old_receivable_line.account_id.id,
}
)
)

# Set new invoices lines with correct IDs from new invoice
self.invoice_line_ids = self.invoice_line_ids + tmp
was_open = old_invoice.state == "posted"
if was_open:
old_invoice.button_draft()
old_invoice.env.clear()
self.invoice_line_ids.write({"move_id": invoice.id})

# Update old invoice receivable line
old_receivable_line.write({"price_unit": amount_total_old_receivable})
if was_open:
old_invoice.action_post()
invoice.action_post()

return invoice

def _copy_invoice(self, old_invoice):
# Create new invoice
new_invoice = old_invoice.copy(
default={"invoice_date": old_invoice.invoice_date}
)
new_invoice.line_ids.unlink()
new_invoice.invoice_line_ids.unlink()
return new_invoice

0 comments on commit 645e0da

Please sign in to comment.