# Copyright 2019 ForgeFlow, S.L. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0) from odoo import _, api, fields, models class PurchaseRequestAllocation(models.Model): _name = "purchase.request.allocation" _description = "Purchase Request Allocation" purchase_request_line_id = fields.Many2one( string="Purchase Request Line", comodel_name="purchase.request.line", required=True, ondelete="cascade", copy=True, index=True, ) company_id = fields.Many2one( string="Company", comodel_name="res.company", readonly=True, related="purchase_request_line_id.request_id.company_id", store=True, index=True, ) stock_move_id = fields.Many2one( string="Stock Move", comodel_name="stock.move", ondelete="cascade", index=True, ) purchase_line_id = fields.Many2one( string="Purchase Line", comodel_name="purchase.order.line", copy=True, ondelete="cascade", help="Service Purchase Order Line", index=True, ) product_id = fields.Many2one( string="Product", comodel_name="product.product", related="purchase_request_line_id.product_id", readonly=True, ) product_uom_id = fields.Many2one( string="UoM", comodel_name="uom.uom", related="purchase_request_line_id.product_uom_id", readonly=True, required=True, ) requested_product_uom_qty = fields.Float( string="Requested Quantity", help="Quantity of the purchase request line allocated to the" "stock move, in the UoM of the Purchase Request Line", ) allocated_product_qty = fields.Float( string="Allocated Quantity", copy=False, help="Quantity of the purchase request line allocated to the stock" "move, in the default UoM of the product", ) open_product_qty = fields.Float( string="Open Quantity", compute="_compute_open_product_qty" ) purchase_state = fields.Selection(related="purchase_line_id.state") @api.depends( "requested_product_uom_qty", "allocated_product_qty", "stock_move_id", "stock_move_id.state", "stock_move_id.product_uom_qty", "stock_move_id.move_line_ids.qty_done", "purchase_line_id", "purchase_line_id.qty_received", "purchase_state", ) def _compute_open_product_qty(self): for rec in self: if rec.purchase_state in ["cancel", "done"]: rec.open_product_qty = 0.0 else: rec.open_product_qty = ( rec.requested_product_uom_qty - rec.allocated_product_qty ) if rec.open_product_qty < 0.0: rec.open_product_qty = 0.0 @api.model def _purchase_request_confirm_done_message_content(self, message_data): message = "" message += _( "From last reception this quantity has been " "allocated to this purchase request" ) message += "