|
| 1 | +# © 2025 Deltatech |
| 2 | +# Dorin Hongu <dhongu(@)gmail(.)com |
| 3 | +# See README.rst file on addons root folder for license details |
| 4 | + |
| 5 | + |
| 6 | +from odoo import api, models |
| 7 | + |
| 8 | + |
| 9 | +class PurchaseOrder(models.Model): |
| 10 | + _inherit = "purchase.order" |
| 11 | + |
| 12 | + def show_order_lines(self): |
| 13 | + """Override to show order lines in the XLS report.""" |
| 14 | + self.ensure_one() |
| 15 | + |
| 16 | + tree_view_id = self.env.ref("deltatech_purchase_xls.purchase_order_line_tree").id |
| 17 | + return { |
| 18 | + "type": "ir.actions.act_window", |
| 19 | + "name": "Purchase Order Lines", |
| 20 | + "res_model": "purchase.order.line", |
| 21 | + "view_mode": "tree,form", |
| 22 | + "views": [(tree_view_id, "tree")], |
| 23 | + "domain": [("order_id", "=", self.id)], |
| 24 | + "context": {"default_order_id": self.id, "create": True, "edit": True}, |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | +class PurchaseOrderLine(models.Model): |
| 29 | + _inherit = "purchase.order.line" |
| 30 | + |
| 31 | + def _parse_import_data(self, data, import_fields, options): |
| 32 | + return super()._parse_import_data(data, import_fields, options) |
| 33 | + |
| 34 | + |
| 35 | + # def _load_records(self, data_list, update=False): |
| 36 | + # order = self.env["purchase.order"] |
| 37 | + # order_id = self.env.context.get("default_order_id", False) or self.env.context.get("active_id", False) |
| 38 | + # if order_id: |
| 39 | + # order = self.env["purchase.order"].browse(order_id) |
| 40 | + # if order: |
| 41 | + # |
| 42 | + # for data in data_list: |
| 43 | + # |
| 44 | + # product_id = data["values"].get("product_id", "") |
| 45 | + # |
| 46 | + # |
| 47 | + # line = order.order_line.filtered(lambda l: l.product_id.id == product_id) |
| 48 | + # if line: |
| 49 | + # data["values"]["id"] = str(line[0].id) |
| 50 | + # |
| 51 | + # |
| 52 | + # |
| 53 | + # return super()._load_records(data_list, update) |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + @api.model |
| 58 | + def load(self, fields, data): |
| 59 | + order = self.env["purchase.order"] |
| 60 | + order_id = self.env.context.get("default_order_id", False) or self.env.context.get("active_id", False) |
| 61 | + if order_id: |
| 62 | + order = self.env["purchase.order"].browse(order_id) |
| 63 | + |
| 64 | + if not order: |
| 65 | + order_index = fields.index.get("order_id", False) |
| 66 | + if order_index: |
| 67 | + order_id = data[0][order_index] |
| 68 | + order = self.env["purchase.order"].browse(order_id) |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + if order: |
| 73 | + |
| 74 | + product_index = fields.index("product_id") if "product_id" in fields else -1 |
| 75 | + fields.append(".id") |
| 76 | + index_id = fields.index(".id") |
| 77 | + for record in data: |
| 78 | + record.append("") |
| 79 | + |
| 80 | + if product_index != -1: |
| 81 | + for record in data: |
| 82 | + product_name = record[product_index] |
| 83 | + product = self.env["product.product"] |
| 84 | + # extrage codul din numele produsului care este intre paranteze [] |
| 85 | + if "[" in product_name and "]" in product_name: |
| 86 | + product_code = product_name.split("[")[-1].split("]")[0].strip() |
| 87 | + product = self.env["product.product"].search([('default_code', '=', product_code)], limit=1) |
| 88 | + if not product: |
| 89 | + product_name = product_name.split("[")[0].strip() |
| 90 | + product = self.env["product.product"].search([('name', '=', product_name)], limit=1) |
| 91 | + |
| 92 | + if not product: |
| 93 | + data.remove(record) |
| 94 | + continue |
| 95 | + if product: |
| 96 | + line = order.order_line.filtered(lambda l: l.product_id.id == product.id) |
| 97 | + if line: |
| 98 | + record[index_id] = str(line.id) |
| 99 | + else: |
| 100 | + data.remove(record) |
| 101 | + |
| 102 | + |
| 103 | + return super().load(fields, data) |
0 commit comments