Skip to content

Commit

Permalink
[MIG] hr_expense_cancel: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ilo committed May 15, 2024
1 parent 03f0d6e commit c059b8a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
14 changes: 8 additions & 6 deletions hr_expense_cancel/models/hr_expense.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class HrExpenseSheet(models.Model):

def action_cancel(self):
for sheet in self:
account_move = sheet.account_move_id
sheet.account_move_id = False
account_move = sheet.account_move_ids
sheet.account_move_ids = False
payments = sheet.payment_ids.filtered(lambda line: line.state != "cancel")
# case : cancel invoice from hr_expense
self._remove_reconcile_hr_invoice(account_move)
# If the sheet is paid then remove payments
if sheet.state == "done":
if sheet.state in ("done", "approve"):
if sheet.expense_line_ids[:1].payment_mode == "own_account":
self._remove_move_reconcile(payments, account_move)
self._cancel_payments(payments)
Expand All @@ -27,8 +27,9 @@ def action_cancel(self):
# (if the expense sheet is paid and payment_mode == 'own_account')
# it has not been deleted
if account_move.exists():
if account_move.state != "draft":
account_move.button_cancel()
move_to_cancel = account_move.filtered(lambda move: move.state != "draft")
if move_to_cancel:
move_to_cancel.button_cancel()
account_move.with_context(force_delete=True).unlink()
sheet.state = "submit"

Expand All @@ -38,7 +39,7 @@ def _remove_reconcile_hr_invoice(self, account_move):
aml = self.env["account.move.line"].search(
[("full_reconcile_id", "in", reconcile.ids)]
)
exp_move_line = aml.filtered(lambda line: line.move_id.id != account_move.id)
exp_move_line = aml.filtered(lambda line: line.move_id.id not in account_move.ids)
# set state to cancel
exp_move_line.move_id.button_draft()
exp_move_line.move_id.button_cancel()
Expand All @@ -56,4 +57,5 @@ def _remove_move_reconcile(self, payments, account_move):

def _cancel_payments(self, payments):
for rec in payments:
rec.move_id.button_draft()
rec.move_id.button_cancel()
1 change: 1 addition & 0 deletions hr_expense_cancel/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
- Danh Vo \<<https://github.com/danhvophuong>\>
- Saran Lim. \<<saranl@ecosoft.co.th>\>
- Manuel Regidor \<<manuel.regidor@sygel.es>\>
- Italo LOPES \<<italo.lopes@camptocamp.com>\>
10 changes: 5 additions & 5 deletions hr_expense_cancel/tests/test_hr_expense_cancel.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def setUp(self):
"journal_id": self.expense_journal.id,
}
)
self.expense_sheet.approve_expense_sheets()
self.expense_sheet.action_approve_expense_sheets()

self.expense = self.env["hr.expense"].create(
{
Expand Down Expand Up @@ -62,12 +62,12 @@ def test_action_cancel_posted(self):
self.expense_sheet.action_sheet_move_create()

self.assertFalse(len(self.expense_sheet.payment_ids), 1)
self.assertTrue(self.expense_sheet.account_move_id)
self.assertTrue(self.expense_sheet.account_move_ids)

self.expense_sheet.action_cancel()

self.assertFalse(self.expense_sheet.payment_ids)
self.assertFalse(self.expense_sheet.account_move_id)
self.assertFalse(self.expense_sheet.account_move_ids)

def test_action_cancel_no_update_posted(self):
journals = self.payment_journal | self.expense_journal
Expand All @@ -79,9 +79,9 @@ def test_action_cancel_no_update_posted(self):
def test_action_cancel_company_account(self):
self.expense.payment_mode = "company_account"
self.expense_sheet.action_sheet_move_create()
self.assertTrue(self.expense_sheet.account_move_id)
self.assertTrue(self.expense_sheet.account_move_ids)
self.expense_sheet.action_cancel()
self.assertFalse(self.expense_sheet.account_move_id)
self.assertFalse(self.expense_sheet.account_move_ids)

def test_action_cancel_own_account(self):
self.expense_sheet.action_sheet_move_create()
Expand Down
2 changes: 1 addition & 1 deletion hr_expense_cancel/views/hr_expense_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<xpath expr="//header/field[@name='state']" position="before">
<button
name="action_cancel"
states="post,done"
invisible="state not in ('post', 'done')"
string="Cancel all related operations"
type="object"
groups="account.group_account_manager"
Expand Down

0 comments on commit c059b8a

Please sign in to comment.