Skip to content

Commit

Permalink
Fix subcontractor work amount and block move action post if invalid w…
Browse files Browse the repository at this point in the history
…ork amount. Add link between subcontractor invoices and smart button and block modification
  • Loading branch information
bguillot committed Jan 11, 2024
1 parent 7489b8a commit 5d6b3dd
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 8 deletions.
49 changes: 48 additions & 1 deletion account_invoice_subcontractor/models/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# @author Sébastien BEAU <sebastien.beau@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import UserError


class AccountMoveLine(models.Model):
Expand Down Expand Up @@ -139,6 +140,21 @@ class AccountMove(models.Model):
invalid_work_amount = fields.Boolean(
compute="_compute_invalid_work_amount", store=True
)
customer_invoice_id = fields.Many2one(
comodel_name="account.move", string="Customer invoice", readonly=True
)
subcontractor_invoice_ids = fields.One2many(
comodel_name="account.move",
inverse_name="customer_invoice_id",
string="Supplier invoices",
readonly=True,
)
origin_customer_invoice_id = fields.Many2one(
comodel_name="account.move",
string="Original customer invoice",
readonly=True,
prefetch=False,
)

@api.depends(
"invoice_line_ids",
Expand Down Expand Up @@ -166,3 +182,34 @@ def _compute_invalid_work_amount(self):
invoice.invalid_work_amount = any(
[line.invalid_work_amount for line in invoice.invoice_line_ids]
)

def action_view_subcontractor_invoices(self):
self.ensure_one()
action = self.env.ref("account.action_move_out_invoice_type").sudo().read()[0]
action["domain"] = [("id", "in", self.subcontractor_invoice_ids.ids)]
action["context"] = {
"default_move_type": "out_invoice",
"move_type": "out_invoice",
"journal_type": "sale",
"active_test": False,
}
return action

def already_subcontracted(self):
invoices = self.subcontractor_invoice_ids
invoices |= self.sudo().search([("origin_customer_invoice_id", "in", self.ids)])
return invoices and True or False

def button_draft(self):
if self.already_subcontracted():
raise UserError(
_("You can't set to draft an invoice already invoiced by subcontractor")
)
return super().button_draft()

def button_cancel(self):
if self.already_subcontracted():
raise UserError(
_("You can't cancel an invoice already invoiced by subcontractor")
)
return super().button_cancel()
16 changes: 14 additions & 2 deletions account_invoice_subcontractor/models/subcontractor_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ def _compute_price(self):
cost_price_unit = sale_price_unit * rate
work.sale_price_unit = sale_price_unit
work.cost_price_unit = cost_price_unit
work.cost_price = work.quantity * cost_price_unit
work.sale_price = work.quantity * sale_price_unit
work.cost_price = work.quantity * work.cost_price_unit
work.sale_price = work.quantity * work.sale_price_unit

@api.onchange("employee_id")
def employee_id_onchange(self):
Expand Down Expand Up @@ -308,6 +308,10 @@ def _prepare_invoice(self):
"user_id": user.id,
}
)
if invoice_type in ["out_invoice", "out_refund"]:
invoice_vals["origin_customer_invoice_id"] = orig_invoice.id
elif invoice_type in ["in_invoice", "in_refund"]:
invoice_vals["customer_invoice_id"] = orig_invoice.id
return invoice_vals

@api.model
Expand Down Expand Up @@ -464,3 +468,11 @@ def unlink(self):
)
)
return super().unlink()

def action_post(self):
invalid_invoice = self.filtered(lambda m: m.invalid_work_amount)
if invalid_invoice:
raise UserError(
_("You can't validate an invoice with invalid work amount!")
)
return super().action_post()
18 changes: 18 additions & 0 deletions account_invoice_subcontractor/views/invoice_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form" />
<field name="arch" type="xml">
<div name="button_box" position="inside">
<button
name="action_view_subcontractor_invoices"
type="object"
string="Subcontractor Invoices"
class="oe_stat_button"
icon="fa-pencil-square-o"
attrs="{'invisible': [('move_type', 'not in', ['out_invoice', 'out_refund'])]}"
/>
</div>
<field name="partner_id" position="after">
<field
name="customer_invoice_id"
attrs="{'invisible': [('customer_invoice_id', '=', False)]}"
/>
<field name="subcontractor_invoice_ids" invisible="1" />
</field>
<xpath expr="//tree/field[@name='name']" position="after">
<field
name="subcontractors"
Expand Down Expand Up @@ -83,6 +100,7 @@
name="edit_subcontractor"
type="object"
string="Edit Sub."
attrs="{'invisible': [('parent.move_type', 'not in', [('out_invoice', 'out_refund')])]}"
/>
<field name="subcontracted" optional="hide" />
<field name="subcontractor_work_ids" invisible="1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<field name="arch" type="xml">
<search string="Subcontractor work">
<field name="invoice_id" />
<field name="supplier_invoice_id" />
<field name="employee_id" />
<separator />
<filter
Expand Down
24 changes: 19 additions & 5 deletions project_invoicing_subcontractor/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,30 @@ def action_view_subcontractor(self):
.sudo()
.read()[0]
)
action["context"] = {
"search_default_invoice_id": self.id,
"search_default_subcontractor": 1,
}
if self.move_type in ["out_invoice", "out_refund"]:
action["context"] = {
"search_default_invoice_id": self.id,
"search_default_subcontractor": 1,
}
elif self.move_type in ["in_invoice", "in_refund"]:
action["context"] = {
"search_default_supplier_invoice_id": self.id,
}
return action

def action_view_analytic_line(self):
self.ensure_one()
action = self.env.ref("hr_timesheet.act_hr_timesheet_line").sudo().read()[0]
action["domain"] = [("invoice_id", "=", self.id)]
if self.move_type in ["out_invoice", "out_refund"]:
action["domain"] = [("invoice_id", "=", self.id)]
elif self.move_type in ["in_invoice", "in_refund"]:
action["domain"] = [
(
"id",
"=",
self.invoice_line_ids.subcontractor_work_invoiced_id.timesheet_line_ids.ids,
)
]
return action

def _move_autocomplete_invoice_lines_values(self):
Expand Down

0 comments on commit 5d6b3dd

Please sign in to comment.