Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] Avoid subcontractor on invoice line without subcontracted product #45

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion account_invoice_subcontractor/models/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ def _compute_invalid_work_amount(self):
else:
line.invalid_work_amount = line._check_out_invoice_amount()
else:
line.invalid_work_amount = False
if line.subcontractor_work_ids:
line.invalid_work_amount = line.price_subtotal
else:
line.invalid_work_amount = False

def _check_in_invoice_amount(self):
return (
Expand Down
29 changes: 15 additions & 14 deletions account_invoice_subcontractor/models/subcontractor_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,27 @@ def _prepare_invoice(self):
invoice_type = orig_invoice.move_type
journal_type = "sale"
partner = self.customer_id
original_invoice_date = orig_invoice.invoice_date
last_invoices = self.env["account.move"].search(
[
("move_type", "=", invoice_type),
("company_id", "=", company.id),
("invoice_date", ">", original_invoice_date),
("name", "!=", False),
],
order="invoice_date desc",
)
if not last_invoices:
invoice_date = original_invoice_date
else:
invoice_date = last_invoices[0].invoice_date
elif self.subcontractor_type == "external":
invoice_type = (
orig_invoice.move_type == "out_invoice" and "in_invoice" or "in_refund"
)
journal_type = "purchase"
partner = self.employee_id._get_employee_invoice_partner()
invoice_date = False
if invoice_type in ["out_invoice", "out_refund"]:
user = self.env["res.users"].search(
[("company_id", "=", company.id)], limit=1
Expand All @@ -292,20 +307,6 @@ def _prepare_invoice(self):
invoice_vals = self.env["account.move"].play_onchanges(
invoice_vals, ["partner_id"]
)
original_invoice_date = orig_invoice.invoice_date
last_invoices = self.env["account.move"].search(
[
("move_type", "=", invoice_type),
("company_id", "=", company.id),
("invoice_date", ">", original_invoice_date),
("name", "!=", False),
],
order="invoice_date desc",
)
if not last_invoices:
invoice_date = original_invoice_date
else:
invoice_date = last_invoices[0].invoice_date
invoice_vals.update(
{
"invoice_date": invoice_date,
Expand Down
5 changes: 4 additions & 1 deletion project_invoicing_subcontractor/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,12 @@ def _check_invoice_mode_validity(self):
)

def _post(self, soft=True):
res = super()._post(soft=soft)
for move in self:
if move.subcontractor_state_color == "danger":
raise exceptions.UserError(move.subcontractor_state_message)
move._check_invoice_mode_validity()
res = super()._post(soft=soft)
for move in self:
if move.is_supplier_prepaid:
move._manage_prepaid_lines()
move.compute_enought_analytic_amount(partner_id=move.customer_id.id)
Expand Down
14 changes: 8 additions & 6 deletions project_invoicing_subcontractor/tests/test_invoicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,8 @@ def test_prepaid_invoicing_process_same_project(self):
self.assertTrue(demo_invoice.to_pay)

# set admin invoice one day later to be sure demo invoice has priority
admin_invoice.write({"invoice_date": date.today() + timedelta(days=1)})
admin_invoice.action_post()
self.env["account.move"].compute_enought_analytic_amount()
self.assertFalse(admin_invoice.to_pay)
self.assertTrue(demo_invoice.to_pay)

# Add customer invoice so there is enough amount to validate the admin invoice
# but it is not paid
customer_invoice2 = self._create_prepaid_customer_invoice(
1.99, self.line_5_2.project_id.analytic_account_id
)
Expand All @@ -230,6 +226,12 @@ def test_prepaid_invoicing_process_same_project(self):
0.01, self.line_5_2.project_id.analytic_account_id
)
customer_invoice3.action_post()
admin_invoice.write({"invoice_date": date.today() + timedelta(days=1)})
admin_invoice.action_post()
self.env["account.move"].compute_enought_analytic_amount()
self.assertFalse(admin_invoice.to_pay)
self.assertTrue(demo_invoice.to_pay)

self.env["account.payment.register"].with_context(
active_ids=customer_invoice2.ids, active_model="account.move"
).create({})._create_payments()
Expand Down
Loading