Skip to content

Commit

Permalink
Use the remove_move_reconcile and instead of the button method to res…
Browse files Browse the repository at this point in the history
…et the bankstatement line. This allows to keep the amount in the reconcilable and re-reconcile with other if needed.
  • Loading branch information
davidwul committed Jul 1, 2024
1 parent c5e7998 commit 27f87ae
Showing 1 changed file with 29 additions and 90 deletions.
119 changes: 29 additions & 90 deletions account_offbalance_sponsorship/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,7 @@ def js_remove_outstanding_partial(self, partial_id):
invoice.
"""
mv = self.env["account.partial.reconcile"].browse(partial_id)
(
account_offbalance_receivable,
account_offbalance_asset,
) = self.line_ids.get_account_offbalance(mv.company_id)
pmt_move = mv.credit_move_id.move_id
rem_lines = self.env["account.move.line"].search(
[
("account_id", "=", account_offbalance_asset),
("move_id", "=", pmt_move.id),
("debit", "=", mv.amount),
]
)
if rem_lines.statement_line_id:
rem_lines.statement_line_id.with_delay().button_undo_reconciliation()
mv.debit_move_id.remove_off_balance_lines(mv.debit_move_id.move_id, self)
res = super().js_remove_outstanding_partial(partial_id)
return res

Expand All @@ -52,18 +39,19 @@ def get_account_offbalance(self, company):

def reconcile(self):
#check if there is a currency diff and relaod the invoice with the rate received
filtered_item = self.filtered(lambda l: l.journal_id != self.company_id.currency_exchange_journal_id)
total = total_curr = 0
for l in self:
total=l.debit-l.credit+total
total_curr=l.amount_currency+total_curr
for l in filtered_item:
total = l.debit - l.credit + total
total_curr = l.amount_currency + total_curr
if total != 0 and total_curr == 0:
inv_to_refresh=self.filtered(lambda l:l.move_id.journal_id.type == 'sale').move_id
nr = self.filtered(lambda l: l.move_id.journal_id.type != 'sale')
new_rate=sum(n.amount_currency for n in nr)/sum(n.debit-n.credit for n in nr)
inv_to_refresh = filtered_item.filtered(lambda l: l.move_id.journal_id.type == 'sale').move_id
nr = filtered_item.filtered(lambda l: l.move_id.journal_id.type != 'sale')
new_rate = sum(n.amount_currency for n in nr) / sum(n.debit - n.credit for n in nr)
inv_to_refresh.button_draft()
for line in inv_to_refresh.line_ids:
line.debit = abs(line.amount_currency/new_rate) if line.amount_currency>0 else 0
line.credit = abs(line.amount_currency/new_rate) if line.amount_currency<0 else 0
line.debit = abs(line.amount_currency / new_rate) if line.amount_currency > 0 else 0
line.credit = abs(line.amount_currency / new_rate) if line.amount_currency < 0 else 0
inv_to_refresh.action_post()
#reconcile
res = super().reconcile()
Expand All @@ -83,6 +71,19 @@ def reconcile(self):
)
return res

def remove_off_balance_lines(self, inv_move, pmt_move):
rec_lines = pmt_move.line_ids
off_rec, off_ass = rec_lines.get_account_offbalance(inv_move.company_id)
if rec_lines.filtered(lambda r: r.account_id.id == off_rec):
pmt_move.with_context(skip_account_move_synchronization=True).write({"state": "draft"})
ids_to_unlink = self.env["account.move.line"]
for move_name in inv_move.mapped("name"):
ids_to_unlink += rec_lines.filtered(lambda l: l.account_id.id != off_rec and l.name == move_name)
pmt_move.line_ids -= ids_to_unlink
pmt_move.write({"state": "posted"})

return True

def add_off_balance_lines(
self, mv, account_offbalance_receivable, account_offbalance_asset, company
):
Expand Down Expand Up @@ -111,7 +112,9 @@ def add_off_balance_lines(
.create(
{
"account_id": account_offbalance_asset,
"name": mv.debit_move_id.move_id.name,
"move_id": pmt_move.id,
"partner_id": pmt_move.partner_id.id,
"debit": closed_amount,
"credit": 0,
}
Expand Down Expand Up @@ -141,6 +144,7 @@ def add_off_balance_lines(
"account_id": inc_acc.id,
"move_id": pmt_move.id,
"debit": 0,
"name": mv.debit_move_id.move_id.name,
"product_id": inv_line.product_id.id,
"partner_id": inv_line.partner_id.id,
"credit": amount_line,
Expand All @@ -151,72 +155,7 @@ def add_off_balance_lines(

def remove_move_reconcile(self):
"""Undo a reconciliation"""
if self.move_id:
part = self.matched_debit_ids + self.matched_credit_ids
(
account_offbalance_receivable,
account_offbalance_asset,
) = self.get_account_offbalance(self[0].move_id.company_id)
if part and (
part[0].debit_move_id.account_id.id == account_offbalance_receivable
):
for mv in part:
rec_invoice_line_ids = mv.debit_move_id.move_id.line_ids.filtered(
lambda mvl: mvl.account_id.code.startswith("93")
)
counterpart_credit_amount = sum(
inv_line.credit for inv_line in rec_invoice_line_ids
)
closed_amount = mv.amount
pmt_move = mv.credit_move_id.move_id
rem_lines = self.env["account.move.line"].search(
[
("account_id", "=", account_offbalance_asset),
("move_id", "=", pmt_move.id),
("debit", "=", closed_amount),
]
)
if self.filtered(lambda s: s.statement_line_id is not False):
(
rem_lines.statement_line_id.with_delay().button_undo_reconciliation()
)
else:
total_amount_lines = 0
for inv_line in rec_invoice_line_ids:
inc_acc = self.env["account.account"].search(
[
("code", "=", inv_line.account_id.code[1:]),
("company_id", "=", self.company_id.id),
]
)
amount_line = round(
closed_amount
/ counterpart_credit_amount
* inv_line.credit,
2,
)
if (
abs(
counterpart_credit_amount
- total_amount_lines
- amount_line
)
< 0.1
):
# to avoid rounding issues
amount_line = closed_amount - total_amount_lines
else:
total_amount_lines += amount_line
rem_lines += self.env["account.move.line"].search(
[
("account_id", "=", inc_acc.id),
("move_id", "=", pmt_move.id),
("product_id", "=", inv_line.product_id.id),
("partner_id", "=", inv_line.partner_id.id),
("credit", "=", amount_line),
]
)
pmt_move.write({"state": "draft", "is_move_sent": False})
pmt_move.line_ids -= rem_lines
pmt_move.action_post()
inv_move = self.matched_debit_ids.debit_move_id.move_id+self.matched_credit_ids.debit_move_id.move_id
pmt_move = self.matched_credit_ids.credit_move_id.move_id+self.matched_debit_ids.credit_move_id.move_id
self.remove_off_balance_lines(inv_move, pmt_move)
super().remove_move_reconcile()

0 comments on commit 27f87ae

Please sign in to comment.