diff --git a/.gitignore b/.gitignore index a6d076d8..59c99089 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,36 @@ coverage.xml # Sphinx documentation docs/_build/ + +### macOS ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### macOS Patch ### +# iCloud generated files +*.icloud diff --git a/purchase_order_type_ux/models/account_move.py b/purchase_order_type_ux/models/account_move.py index 0c2c2116..e7f05b1d 100644 --- a/purchase_order_type_ux/models/account_move.py +++ b/purchase_order_type_ux/models/account_move.py @@ -38,6 +38,20 @@ def _compute_purchase_type_id(self): ) if purchase_type: record.purchase_type_id = purchase_type + if ( + record.purchase_type_id + and record.purchase_type_id.journal_id.company_id.id not in record.env.companies.ids + and not record.partner_id + ): + record.purchase_type_id = self.env["purchase.order.type"].search( + [ + ("company_id", "in", [record.company_id.id, False]), + "|", + ("journal_id", "=", False), + ("journal_id.company_id", "=", record.company_id.id), + ], + limit=1, + ) @api.depends("purchase_type_id") def _compute_invoice_payment_term_id(self): @@ -51,4 +65,16 @@ def _compute_journal_id(self): res = super()._compute_journal_id() for move in self.filtered("purchase_type_id.journal_id"): move.journal_id = move.purchase_type_id.journal_id + if move.purchase_type_id.journal_id: + move._onchange_journal() return res + + @api.onchange("journal_id") + def _onchange_journal(self): + if self.journal_id and self.journal_id.currency_id: + new_currency = self.journal_id.currency_id + if new_currency != self.currency_id: + self.currency_id = new_currency + self._compute_currency_rate() + if self.state == "draft" and self._get_last_sequence() and self.name and self.name != "/": + self.name = "/" diff --git a/purchase_order_type_ux/models/purchase_order.py b/purchase_order_type_ux/models/purchase_order.py index cbcaae0f..63a91f5b 100644 --- a/purchase_order_type_ux/models/purchase_order.py +++ b/purchase_order_type_ux/models/purchase_order.py @@ -7,7 +7,7 @@ class PurchaseOrder(models.Model): _inherit = "purchase.order" - @api.constrains("order_type") + @api.onchange("order_type") def onchange_order_type(self): super().onchange_order_type() for order in self: @@ -17,9 +17,44 @@ def onchange_order_type(self): order.picking_type_id = order.order_type.picking_type_id def _prepare_invoice(self): + if not self.order_type.journal_id: + return super()._prepare_invoice() res = super()._prepare_invoice() - if self.order_type.journal_id: - res["journal_id"] = self.order_type.journal_id.id + company = self.order_type.journal_id.company_id + self = self.with_company(company.id) + if company != self.company_id: + res["company_id"] = company.id + # En purchase, partner_bank_id es del proveedor, no de la compañía + partner_bank_id = self.partner_id.commercial_partner_id.bank_ids.filtered_domain( + ["|", ("company_id", "=", False), ("company_id", "=", company.id)] + )[:1] + res["partner_bank_id"] = partner_bank_id.id + # agregamos para que recompute term y cond si la nueva compañia los tiene por defecto + if "narration" in res and not res["narration"]: + del res["narration"] + po_fiscal_position = self.env["account.fiscal.position"].browse(res["fiscal_position_id"]) + if not po_fiscal_position or (po_fiscal_position.company_id and po_fiscal_position.company_id != company): + partner_invoice = self.env["res.partner"].browse(self.partner_id.address_get(["invoice"])["invoice"]) + res["fiscal_position_id"] = ( + self.env["account.fiscal.position"] + .with_company(company.id) + ._get_fiscal_position(partner_invoice) + .id + ) if self.order_type: res["purchase_type_id"] = self.order_type.id return res + + def action_create_invoice(self): + """ + Overrides the `action_create_invoice` method to ensure that taxes are correctly computed + for the company of the invoice. In cases where the company has a localization + (e.g., l10n_ar), this ensures that the taxes from `l10n_ar_tax_ids` are applied. + """ + action = super().action_create_invoice() + invoices = self.invoice_ids.filtered(lambda m: m.state == "draft") + for line in invoices.invoice_line_ids: + purchase_line = line.purchase_line_id + if purchase_line and line.move_id.company_id != purchase_line.order_id.company_id: + line.tax_ids = line._get_computed_taxes() + return action diff --git a/purchase_order_type_ux/models/purchase_order_line.py b/purchase_order_type_ux/models/purchase_order_line.py index 7ddc4ec3..9dc84685 100644 --- a/purchase_order_type_ux/models/purchase_order_line.py +++ b/purchase_order_type_ux/models/purchase_order_line.py @@ -2,12 +2,14 @@ # For copyright and license notices, see __manifest__.py file in module root # directory ############################################################################## -from odoo import api, models +from odoo import api, fields, models class PurchaseOrderLine(models.Model): _inherit = "purchase.order.line" + taxes_id = fields.Many2many(check_company=False) + @api.depends("qty_invoiced", "qty_received", "order_id.state", "qty_returned", "order_id.order_type") def _compute_qty_invoiced(self): super()._compute_qty_invoiced() @@ -17,3 +19,24 @@ def _compute_qty_invoiced(self): line.qty_to_invoice = line.product_qty - line.qty_invoiced - line.qty_returned else: line.qty_to_invoice = line.qty_received - line.qty_invoiced + + def _prepare_account_move_line(self, move=False): + """ + Forzamos compania de diario de purchase type + """ + if not self.order_id.order_type.journal_id: + return super()._prepare_account_move_line(move=move) + company = self.order_id.order_type.journal_id.company_id + self = self.with_company(company.id) + res = super()._prepare_account_move_line(move=move) + + if company != self.company_id: + # Because we not have the access to the invoice, we obtain the fiscal position who + # has the invoice really + partner_invoice = self.env["res.partner"].browse(self.partner_id.address_get(["invoice"])["invoice"]) + fpos = self.env["account.fiscal.position"].with_company(company.id)._get_fiscal_position(partner_invoice) + taxes = self.product_id.supplier_taxes_id.filtered(lambda r: company == r.company_id) + taxes = fpos.map_tax(taxes) if fpos else taxes + + res["tax_ids"] = [(6, 0, taxes.ids)] + return res diff --git a/purchase_order_type_ux/models/purchase_order_type.py b/purchase_order_type_ux/models/purchase_order_type.py index acfe32ee..117d5bfb 100644 --- a/purchase_order_type_ux/models/purchase_order_type.py +++ b/purchase_order_type_ux/models/purchase_order_type.py @@ -21,9 +21,9 @@ class PurchaseOrderType(models.Model): ) journal_id = fields.Many2one( "account.journal", - domain="[('type', '=', 'purchase'), '|', ('company_id', '=', False), ('company_id', '=', company_id)]", + domain="[('type', '=', 'purchase')]", string="Billing Journal", - check_company=True, + check_company=False, ) purchase_method = fields.Selection( [