From 107f676bac796eddd6c9490a969d56a5fdbe0e49 Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Mon, 15 Jul 2024 10:42:41 +0200 Subject: [PATCH 1/6] account_invoice_subcontractor : pre-commit stuff --- .../models/subcontractor_work.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/account_invoice_subcontractor/models/subcontractor_work.py b/account_invoice_subcontractor/models/subcontractor_work.py index f319c7a..06fc35d 100644 --- a/account_invoice_subcontractor/models/subcontractor_work.py +++ b/account_invoice_subcontractor/models/subcontractor_work.py @@ -218,8 +218,8 @@ def check(self, work_type=False): if dest_invoice_company not in self.env.companies: raise UserError( _( - "You can't generate an invoice for a company you have no access : %s" - % dest_invoice_company.name + "You can't generate an invoice for a company you have no access" + " : %s" % dest_invoice_company.name ) ) if partner_id != work.customer_id.id: @@ -259,7 +259,8 @@ def _prepare_invoice(self): if orig_invoice.move_type not in ("out_invoice", "out_refund"): raise UserError( _( - "You can only invoice the subcontractors on a customer invoice/refund" + "You can only invoice the subcontractors on a customer " + "invoice/refund" ) ) company = self._get_dest_invoice_company() @@ -328,7 +329,7 @@ def _prepare_invoice_line(self, invoice): line_vals = { "product_id": self.sudo().invoice_line_id.product_id.id, "quantity": self.quantity, - "name": "Client final {} :{}".format(self.end_customer_id.name, self.name), + "name": f"Client final {self.end_customer_id.name} :{self.name}", "price_unit": self.cost_price_unit, "move_id": invoice.id, "subcontractor_work_invoiced_id": self.id, @@ -421,16 +422,6 @@ def _scheduler_action_subcontractor_invoice_create(self): for subcontractor in subcontractors: dest_company = subcontractor.subcontractor_company_id user = subcontractor.user_id - # # TOFIX - # old_company = False - # if user.company_id != dest_company: - # if dest_company.id in user.company_ids.ids: - # old_company = user.company_id - # user.company_id = subcontractor.subcontractor_company_id - # else: - # user = self.env["res.users"].search( - # [("company_id", "=", dest_company.id)], limit=1 - # ) subcontractor_works = ( self.with_user(user) .with_company(dest_company) From d7af702058f0f28a82f3a0265cca603312769bb2 Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Mon, 15 Jul 2024 10:51:59 +0200 Subject: [PATCH 2/6] [MIG] rename files --- account_invoice_subcontractor/__manifest__.py | 8 +- .../migrations/14.0.0.0.1/post-migration.py | 89 --------- .../migrations/14.0.0.0.1/pre-migration.py | 44 ----- .../models/__init__.py | 9 +- .../models/{invoice.py => account_move.py} | 132 -------------- .../models/account_move_line.py | 138 ++++++++++++++ .../models/{hr.py => hr_employee.py} | 1 - .../{product.py => product_template.py} | 0 .../models/{sale.py => sale_order_line.py} | 0 ...ce_view.xml => account_move_line_view.xml} | 146 --------------- .../views/account_move_view.xml | 171 ++++++++++++++++++ .../{hr_view.xml => hr_employee_view.xml} | 0 12 files changed, 318 insertions(+), 420 deletions(-) delete mode 100644 account_invoice_subcontractor/migrations/14.0.0.0.1/post-migration.py delete mode 100644 account_invoice_subcontractor/migrations/14.0.0.0.1/pre-migration.py rename account_invoice_subcontractor/models/{invoice.py => account_move.py} (50%) create mode 100644 account_invoice_subcontractor/models/account_move_line.py rename account_invoice_subcontractor/models/{hr.py => hr_employee.py} (96%) rename account_invoice_subcontractor/models/{product.py => product_template.py} (100%) rename account_invoice_subcontractor/models/{sale.py => sale_order_line.py} (100%) rename account_invoice_subcontractor/views/{invoice_view.xml => account_move_line_view.xml} (57%) create mode 100644 account_invoice_subcontractor/views/account_move_view.xml rename account_invoice_subcontractor/views/{hr_view.xml => hr_employee_view.xml} (100%) diff --git a/account_invoice_subcontractor/__manifest__.py b/account_invoice_subcontractor/__manifest__.py index 3dae256..84751b1 100644 --- a/account_invoice_subcontractor/__manifest__.py +++ b/account_invoice_subcontractor/__manifest__.py @@ -4,7 +4,7 @@ { "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", @@ -12,16 +12,16 @@ "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", diff --git a/account_invoice_subcontractor/migrations/14.0.0.0.1/post-migration.py b/account_invoice_subcontractor/migrations/14.0.0.0.1/post-migration.py deleted file mode 100644 index bf6707b..0000000 --- a/account_invoice_subcontractor/migrations/14.0.0.0.1/post-migration.py +++ /dev/null @@ -1,89 +0,0 @@ -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from openupgradelib import openupgrade - - -@openupgrade.migrate() -def migrate(env, version): - openupgrade.logged_query( - env.cr, - """ - UPDATE subcontractor_work - SET - invoice_line_id = aml.id - FROM account_invoice_line ail - JOIN account_move_line aml ON aml.old_invoice_line_id = ail.id - WHERE subcontractor_work.old_invoice_line_id = ail.id - AND subcontractor_work.old_invoice_line_id IS NOT NULL - """, - ) - openupgrade.logged_query( - env.cr, - """ - UPDATE subcontractor_work - SET - invoice_id = am.id, - invoice_date = am.invoice_date - FROM account_invoice ai - JOIN account_move am ON am.old_invoice_id = ai.id - WHERE subcontractor_work.old_invoice_id = ai.id - AND subcontractor_work.old_invoice_id IS NOT NULL - """, - ) - - openupgrade.logged_query( - env.cr, - """ - UPDATE subcontractor_work - SET - supplier_invoice_line_id = aml.id - FROM account_invoice_line ail - JOIN account_move_line aml ON aml.old_invoice_line_id = ail.id - WHERE subcontractor_work.old_supplier_invoice_line_id = ail.id - AND subcontractor_work.old_supplier_invoice_line_id IS NOT NULL - """, - ) - openupgrade.logged_query( - env.cr, - """ - UPDATE subcontractor_work - SET - supplier_invoice_id = am.id - FROM account_invoice ai - JOIN account_move am ON am.old_invoice_id = ai.id - WHERE subcontractor_work.old_supplier_invoice_id = ai.id - AND subcontractor_work.old_supplier_invoice_id IS NOT NULL - """, - ) - - openupgrade.logged_query( - env.cr, - """ - UPDATE subcontractor_work - SET - subcontractor_invoice_line_id = aml.id - FROM account_invoice_line ail - JOIN account_move_line aml ON aml.old_invoice_line_id = ail.id - WHERE subcontractor_work.old_subcontractor_invoice_line_id = ail.id - AND subcontractor_work.old_subcontractor_invoice_line_id IS NOT NULL - """, - ) - - openupgrade.logged_query( - env.cr, - """ - UPDATE subcontractor_work - SET - state = 'posted' - WHERE state = 'open' - """, - ) - - openupgrade.logged_query( - env.cr, - """ - UPDATE subcontractor_work - SET - subcontractor_state = 'posted' - WHERE subcontractor_state = 'open' - """, - ) diff --git a/account_invoice_subcontractor/migrations/14.0.0.0.1/pre-migration.py b/account_invoice_subcontractor/migrations/14.0.0.0.1/pre-migration.py deleted file mode 100644 index 7562805..0000000 --- a/account_invoice_subcontractor/migrations/14.0.0.0.1/pre-migration.py +++ /dev/null @@ -1,44 +0,0 @@ -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from openupgradelib import openupgrade - - -@openupgrade.migrate() -def migrate(env, version): - # Rename column so we keep the data but leave the update of the module create - # a new column with a foreign key towards account.move - # in post migration we'll manage the migraiton of the data from old field to new one - openupgrade.logged_query( - env.cr, - """ - ALTER TABLE subcontractor_work - RENAME COLUMN invoice_line_id TO old_invoice_line_id; - """, - ) - openupgrade.logged_query( - env.cr, - """ - ALTER TABLE subcontractor_work - RENAME COLUMN invoice_id TO old_invoice_id; - """, - ) - openupgrade.logged_query( - env.cr, - """ - ALTER TABLE subcontractor_work - RENAME COLUMN supplier_invoice_line_id TO old_supplier_invoice_line_id; - """, - ) - openupgrade.logged_query( - env.cr, - """ - ALTER TABLE subcontractor_work - RENAME COLUMN supplier_invoice_id TO old_supplier_invoice_id; - """, - ) - openupgrade.logged_query( - env.cr, - """ - ALTER TABLE subcontractor_work - RENAME COLUMN subcontractor_invoice_line_id TO old_subcontractor_invoice_line_id; - """, - ) diff --git a/account_invoice_subcontractor/models/__init__.py b/account_invoice_subcontractor/models/__init__.py index d1309a4..e76c3c8 100644 --- a/account_invoice_subcontractor/models/__init__.py +++ b/account_invoice_subcontractor/models/__init__.py @@ -1,6 +1,7 @@ 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 sale_order_line from . import res_company diff --git a/account_invoice_subcontractor/models/invoice.py b/account_invoice_subcontractor/models/account_move.py similarity index 50% rename from account_invoice_subcontractor/models/invoice.py rename to account_invoice_subcontractor/models/account_move.py index cc85732..213da29 100644 --- a/account_invoice_subcontractor/models/invoice.py +++ b/account_invoice_subcontractor/models/account_move.py @@ -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" diff --git a/account_invoice_subcontractor/models/account_move_line.py b/account_invoice_subcontractor/models/account_move_line.py new file mode 100644 index 0000000..09a7de9 --- /dev/null +++ b/account_invoice_subcontractor/models/account_move_line.py @@ -0,0 +1,138 @@ +# © 2015 Akretion +# @author Sébastien BEAU +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import api, fields, models + + +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): + res = super()._onchange_product_id() + for rec in self: + rec.subcontracted = rec.product_id.subcontracted + return res + + @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 diff --git a/account_invoice_subcontractor/models/hr.py b/account_invoice_subcontractor/models/hr_employee.py similarity index 96% rename from account_invoice_subcontractor/models/hr.py rename to account_invoice_subcontractor/models/hr_employee.py index 145981d..b7cfece 100644 --- a/account_invoice_subcontractor/models/hr.py +++ b/account_invoice_subcontractor/models/hr_employee.py @@ -20,7 +20,6 @@ def _get_subcontractor_type(self): "res.company", string="Subcontractor Company" ) subcontractor_type = fields.Selection( - string="Subcontractor Type", selection="_get_subcontractor_type", required=True, default="internal", diff --git a/account_invoice_subcontractor/models/product.py b/account_invoice_subcontractor/models/product_template.py similarity index 100% rename from account_invoice_subcontractor/models/product.py rename to account_invoice_subcontractor/models/product_template.py diff --git a/account_invoice_subcontractor/models/sale.py b/account_invoice_subcontractor/models/sale_order_line.py similarity index 100% rename from account_invoice_subcontractor/models/sale.py rename to account_invoice_subcontractor/models/sale_order_line.py diff --git a/account_invoice_subcontractor/views/invoice_view.xml b/account_invoice_subcontractor/views/account_move_line_view.xml similarity index 57% rename from account_invoice_subcontractor/views/invoice_view.xml rename to account_invoice_subcontractor/views/account_move_line_view.xml index 98e8dad..f07d680 100644 --- a/account_invoice_subcontractor/views/invoice_view.xml +++ b/account_invoice_subcontractor/views/account_move_line_view.xml @@ -37,108 +37,6 @@ --> - - account.move - - -
-
- - - - - - - - - - - - - invalid_work_amount == True - - - {'readonly': 0} - - - - - - -