|
| 1 | +# Copyright 2025 Ecosoft Co., Ltd. (http://ecosoft.co.th) |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). |
| 3 | + |
| 4 | +import logging |
| 5 | + |
| 6 | +from odoo import api, fields, models |
| 7 | + |
| 8 | +_logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +class MrpBom(models.Model): |
| 12 | + _name = "mrp.bom" |
| 13 | + _inherit = ["mrp.bom", "zort.api"] |
| 14 | + |
| 15 | + qty_available = fields.Float( |
| 16 | + related="product_tmpl_id.qty_available", |
| 17 | + string="On Hand Quantity", |
| 18 | + readonly=True, |
| 19 | + ) |
| 20 | + |
| 21 | + @api.model |
| 22 | + def update_bom_qty_to_zort(self): |
| 23 | + """Update BOM quantity to Zort as the product's available quantity.""" |
| 24 | + |
| 25 | + warehousecode = ( |
| 26 | + self.env["ir.config_parameter"] |
| 27 | + .sudo() |
| 28 | + .get_param("zort_connector.warehouse_code") |
| 29 | + or "W0001" |
| 30 | + ) |
| 31 | + |
| 32 | + stocks_dict = {} |
| 33 | + boms = self.search([("product_tmpl_id.zort_product_id", "!=", False)]) |
| 34 | + for bom in boms: |
| 35 | + product_id = bom.product_tmpl_id.zort_product_id |
| 36 | + if product_id not in stocks_dict: |
| 37 | + stocks_dict[product_id] = { |
| 38 | + "productid": product_id, |
| 39 | + "stock": bom.qty_available, |
| 40 | + } |
| 41 | + |
| 42 | + # Convert dictionary values to list |
| 43 | + all_stocks = list(stocks_dict.values()) |
| 44 | + |
| 45 | + if all_stocks: |
| 46 | + data = {"stocks": all_stocks} |
| 47 | + try: |
| 48 | + self._update_product_available_stock_list(warehousecode, data) |
| 49 | + _logger.info( |
| 50 | + "Updated %d products' available stock to Zort", len(all_stocks) |
| 51 | + ) |
| 52 | + except Exception as e: |
| 53 | + _logger.error( |
| 54 | + "Failed to update products' available stock to Zort: %s", str(e) |
| 55 | + ) |
0 commit comments