Skip to content

Commit

Permalink
Add split invoice Action and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Gordack committed Jul 10, 2024
1 parent 6b1ae79 commit 9d0e9ec
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions account_invoice_split_invoice/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"depends": ["account"],
"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,7 +11,7 @@
<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)]"
>
<tree editable="bottom" delete="false">
<field name="product_id" readonly="1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,54 @@ 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 l: l.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 9d0e9ec

Please sign in to comment.