Skip to content

Commit

Permalink
Merge pull request #49 from akretion/16-mig-account-invoice-subcontra…
Browse files Browse the repository at this point in the history
…ctor

16 mig account invoice subcontractor
  • Loading branch information
florian-dacosta authored Aug 1, 2024
2 parents adb32aa + 7a5b88a commit 3734190
Show file tree
Hide file tree
Showing 14 changed files with 377 additions and 528 deletions.
8 changes: 4 additions & 4 deletions account_invoice_subcontractor/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

{
"name": "Account Invoice Subcontractor",
"version": "14.0.0.0.2",
"version": "16.0.1.0.0",
"category": "Generic Modules/Others",
"license": "AGPL-3",
"author": "Akretion",
"website": "https://github.com/akretion/subcontractor",
"depends": [
"hr",
"account_invoice_inter_company",
"onchange_helper",
"sale",
],
"data": [
"data/cron_data.xml",
"security/security.xml",
"security/ir.model.access.csv",
"views/hr_view.xml",
"views/hr_employee_view.xml",
"views/subcontractor_work_view.xml",
"views/invoice_view.xml",
"views/account_move_view.xml",
"views/account_move_line_view.xml",
"wizard/subcontractor_invoice_work_view.xml",
"views/product_view.xml",
"wizard/res_config_settings.xml",
Expand Down

This file was deleted.

This file was deleted.

8 changes: 4 additions & 4 deletions account_invoice_subcontractor/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from . import subcontractor_work
from . import hr
from . import invoice
from . import product
from . import sale
from . import hr_employee
from . import account_move
from . import account_move_line
from . import product_template
from . import res_company
Original file line number Diff line number Diff line change
Expand Up @@ -7,138 +7,6 @@
from odoo.tools import float_compare


class AccountMoveLine(models.Model):
_inherit = "account.move.line"

_map_type = {
"in_invoice": "supplier_invoice_line_id",
"out_invoice": "subcontractor_invoice_line_id",
"in_refund": "supplier_invoice_line_id",
"out_refund": "subcontractor_invoice_line_id",
}

subcontracted = fields.Boolean()
subcontractor_work_ids = fields.One2many(
"subcontractor.work", "invoice_line_id", string="Subcontractor Work", copy=True
)
subcontractor_work_invoiced_id = fields.Many2one(
"subcontractor.work",
compute="_compute_subcontractor_work_invoiced_id",
inverse="_inverse_subcontractor_work_invoiced_id",
string="Invoiced Work",
store=True,
)
invalid_work_amount = fields.Boolean(
compute="_compute_invalid_work_amount", string="Work Amount Invalid", store=True
)
subcontractors = fields.Char(string="Sub C.", compute="_compute_subcontractors")

def _compute_subcontractors(self):
for rec in self:
rec.subcontractors = " / ".join(
list({x.employee_id.name[0:4] for x in rec.subcontractor_work_ids})
)

@api.onchange("product_id")
def _onchange_product_id(self):
super()._onchange_product_id()
for rec in self:
rec.subcontracted = rec.product_id.subcontracted

@api.depends("move_id", "move_id.move_type")
def _compute_subcontractor_work_invoiced_id(self):
for line in self:
field = self._map_type.get(line.move_id.move_type, False)
if field:
work_obj = self.env["subcontractor.work"]
work = work_obj.search([[field, "=", line.id]])
if work:
line.subcontractor_work_invoiced_id = work.id

def _inverse_subcontractor_work_invoiced_id(self):
for line in self:
work = line.subcontractor_work_invoiced_id
if work:
field = self._map_type.get(line.move_id.move_type, False)
if field:
work.sudo().write({field: line.id})

@api.depends(
"move_id",
"move_id.move_type",
"move_id.company_id",
"move_id.partner_id",
"subcontractor_work_invoiced_id",
"subcontractor_work_invoiced_id.cost_price",
"subcontractor_work_ids",
"subcontractor_work_ids.sale_price",
"price_subtotal",
"subcontracted",
)
def _compute_invalid_work_amount(self):
for line in self:
if line.subcontracted:
if line.move_id.move_type in ["in_invoice", "in_refund"]:
line.invalid_work_amount = line._check_in_invoice_amount()
else:
line.invalid_work_amount = line._check_out_invoice_amount()
else:
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 (
abs(self.subcontractor_work_invoiced_id.cost_price - self.price_subtotal)
> 0.1
)

def _check_out_invoice_amount(self):
# TODO FIXME
if self.move_id.company_id.id != 1:
# this mean Akretion
if self.move_id.partner_id.id == 1:
return (
abs(
self.subcontractor_work_invoiced_id.cost_price
- self.price_subtotal
)
> 0.1
)
else:
subtotal = sum(self.subcontractor_work_ids.mapped("sale_price"))
# we use a bigger diff to avoid issue with rounding
return abs(subtotal - self.price_subtotal) > 5

@api.model
def _prepare_account_move_line(self, dest_invoice, dest_company, form=False):
res = super()._prepare_account_move_line(dest_invoice, dest_company, form=form)
res["subcontractor_work_invoiced_id"] = self.subcontractor_work_invoiced_id.id
return res

def edit_subcontractor(self):
view = {
"name": ("Details"),
"view_type": "form",
"view_mode": "form",
"res_model": "account.move.line",
"view_id": self.env.ref(
"account_invoice_subcontractor.view_move_line_subcontractor_form"
).id,
"type": "ir.actions.act_window",
"target": "new",
"res_id": self.id,
}
return view

@api.onchange("quantity")
def _onchange_quantity(self):
for line in self:
if len(line.subcontractor_work_ids) == 1:
line.subcontractor_work_ids.quantity = line.quantity


class AccountMove(models.Model):
_inherit = "account.move"

Expand Down
Loading

0 comments on commit 3734190

Please sign in to comment.