diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2025-07-12 10:40:06 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2025-07-12 10:40:06 +0700 |
| commit | ad1f21b27dff6eca4eb90d2a4496cd9ff80701c4 (patch) | |
| tree | d3335e6a6d1be85a903ef5699b07cc303619c954 /fixco_custom/models/manage_stock.py | |
| parent | f12beff2f1e4da1244e7a8e014e73e5e5023aa9d (diff) | |
purchasing job, requisition, reordering v2
Diffstat (limited to 'fixco_custom/models/manage_stock.py')
| -rw-r--r-- | fixco_custom/models/manage_stock.py | 75 |
1 files changed, 74 insertions, 1 deletions
diff --git a/fixco_custom/models/manage_stock.py b/fixco_custom/models/manage_stock.py index 4375ad2..e654b4e 100644 --- a/fixco_custom/models/manage_stock.py +++ b/fixco_custom/models/manage_stock.py @@ -15,7 +15,80 @@ class ManageStock(models.Model): min_stock = fields.Float(string='Min Stock', required=True) buffer_stock = fields.Float(string='Buffer Stock', required=True) vendor_id = fields.Many2one('res.partner', string="Vendor", required=True) + qty_available = fields.Float(string='Qty Available', compute='_compute_qty_available') + qty_onhand = fields.Float(string='Qty Onhand', compute='_compute_qty_available') _sql_constraints = [ ('product_unique', 'unique (product_id)', 'This product already has a stock management rule!'), - ]
\ No newline at end of file + ] + + def _compute_qty_available(self): + for record in self: + quant_records = self.env['stock.quant'].search([ + ('product_id', '=', record.product_id.id), + # ('id','in', [80,81]), + ('location_id', '=', 55) + ]) + + total_available = quant_records.available_quantity or 0.0 + total_onhand = quant_records.quantity or 0.0 + record.qty_available = total_available + record.qty_onhand = total_onhand + + def create_automatic_purchase(self): + if not self: + raise UserError("No stock records selected.") + + automatic_purchase = self.env['automatic.purchase'].create({ + 'apo_type': 'reordering', + }) + + lines_to_create = [] + + for stock in self: + location_id = 55 + + quant_records = self.env['stock.quant'].search([ + ('product_id', '=', stock.product_id.id), + ('location_id', '=', location_id) + ]) + + total_available = quant_records.quantity or 0.0 + + qty_incoming = stock.product_id.incoming_qty or 0.0 + + qty_purchase = stock.buffer_stock - (total_available + qty_incoming) + + qty_purchase = max(qty_purchase, 0.0) + + pricelist = self.env['purchase.pricelist'].search([ + ('product_id', '=', stock.product_id.id), + ('vendor_id', '=', stock.vendor_id.id) + ], limit=1) + + price = pricelist.price if pricelist else 0.0 + subtotal = qty_purchase * price + + lines_to_create.append({ + 'automatic_purchase_id': automatic_purchase.id, + 'product_id': stock.product_id.id, + 'qty_purchase': qty_purchase, + 'qty_min': stock.min_stock, + 'qty_buffer': stock.buffer_stock, + 'partner_id': stock.vendor_id.id, + 'taxes_id': stock.vendor_id.tax_id.id if stock.vendor_id.tax_id else False, + 'price': price, + 'subtotal': subtotal, + }) + + self.env['automatic.purchase.line'].create(lines_to_create) + + return { + 'type': 'ir.actions.act_window', + 'res_model': 'automatic.purchase', + 'view_mode': 'form', + 'res_id': automatic_purchase.id, + 'target': 'current', + } + + |
