| 1 |
|
| 2 |
|
| 3 |
from odoo import api, fields, models |
| 4 |
from odoo.fields import Domain |
| 5 |
|
| 6 |
|
| 7 |
class ProductReplenish(models.TransientModel): |
| 8 |
_inherit = 'product.replenish' |
| 9 |
|
| 10 |
@api.model |
| 11 |
def default_get(self, fields): |
| 12 |
res = super().default_get(fields) |
| 13 |
if res.get('product_id'): |
| 14 |
product_id = self.env['product.product'].browse(res['product_id']) |
| 15 |
product_tmpl_id = product_id.product_tmpl_id |
| 16 |
company = product_tmpl_id.company_id or self.env.company |
| 17 |
if 'warehouse_id' not in res: |
| 18 |
res['warehouse_id'] = self.env['stock.warehouse'].search([ |
| 19 |
*self.env['stock.warehouse']._check_company_domain(company), |
| 20 |
], limit=1).id |
| 21 |
orderpoint = self.env['stock.warehouse.orderpoint'].search([('product_id', 'in', [product_tmpl_id.product_variant_id.id, product_id.id]), ("warehouse_id", "=", res['warehouse_id'])], limit=1) |
| 22 |
if orderpoint.route_id: |
| 23 |
res['route_id'] = orderpoint.route_id.id |
| 24 |
if orderpoint.supplier_id: |
| 25 |
res['supplier_id'] = orderpoint.supplier_id.id |
| 26 |
return res |
| 27 |
|
| 28 |
@api.onchange('route_id') |
| 29 |
def _onchange_supplier_id(self): |
| 30 |
if self.show_vendor and not self.supplier_id and self.product_tmpl_id.seller_ids: |
| 31 |
self.supplier_id = self.product_tmpl_id.seller_ids[0].id |
| 32 |
elif not self.show_vendor: |
| 33 |
self.supplier_id = False |
| 34 |
|
| 35 |
@api.depends('route_id', 'supplier_id') |
| 36 |
def _compute_date_planned(self): |
| 37 |
super()._compute_date_planned() |
| 38 |
for rec in self: |
| 39 |
if 'buy' in rec.route_id.rule_ids.mapped('action'): |
| 40 |
rec.date_planned = rec._get_date_planned(rec.route_id, supplier=rec.supplier_id, show_vendor=rec.show_vendor) |
| 41 |
|
| 42 |
def _prepare_run_values(self): |
| 43 |
res = super()._prepare_run_values() |
| 44 |
if self.supplier_id: |
| 45 |
res['supplierinfo_id'] = self.supplier_id |
| 46 |
|
| 47 |
return res |
| 48 |
|
| 49 |
def action_stock_replenishment_info(self): |
| 50 |
self.ensure_one() |
| 51 |
orderpoint = self.env["stock.warehouse.orderpoint"].search([("product_id", "=", self.product_id.id), ("warehouse_id", "=", self.warehouse_id.id)], limit=1) |
| 52 |
if not orderpoint: |
| 53 |
orderpoint = self.env["stock.warehouse.orderpoint"].create({ |
| 54 |
"product_id": self.product_id.id, |
| 55 |
"warehouse_id": self.warehouse_id.id, |
| 56 |
}) |
| 57 |
action = orderpoint.action_stock_replenishment_info() |
| 58 |
|
| 59 |
action["context"] = { |
| 60 |
'default_orderpoint_id': orderpoint.id, |
| 61 |
'replenish_id': self.id, |
| 62 |
} |
| 63 |
return action |
| 64 |
|
| 65 |
def _get_record_to_notify(self, date): |
| 66 |
order_line = self.env['purchase.order.line'].search([('write_date', '>=', date)], limit=1) |
| 67 |
return order_line or super()._get_record_to_notify(date) |
| 68 |
|
| 69 |
def _get_replenishment_order_notification_link(self, order_line): |
| 70 |
if order_line._name == 'purchase.order.line': |
| 71 |
return [{ |
| 72 |
'label': order_line.order_id.display_name, |
| 73 |
'url': f'/odoo/action-purchase.action_rfq_form/{order_line.order_id.id}', |
| 74 |
}] |
| 75 |
return super()._get_replenishment_order_notification_link(order_line) |
| 76 |
|
| 77 |
def _get_date_planned(self, route_id, **kwargs): |
| 78 |
date = super()._get_date_planned(route_id, **kwargs) |
| 79 |
if 'buy' not in route_id.rule_ids.mapped('action'): |
| 80 |
return date |
| 81 |
|
| 82 |
supplier = kwargs.get('supplier') |
| 83 |
show_vendor = kwargs.get('show_vendor') |
| 84 |
if not show_vendor or not supplier: |
| 85 |
return date |
| 86 |
|
| 87 |
delay = supplier.delay + self.env.company.days_to_purchase |
| 88 |
|
| 89 |
return fields.Datetime.add(date, days=delay) |
| 90 |
|
| 91 |
def _get_route_domain(self, product_tmpl_id): |
| 92 |
domain = super()._get_route_domain(product_tmpl_id) |
| 93 |
buy_route = self.env.ref('purchase_stock.route_warehouse0_buy', raise_if_not_found=False) |
| 94 |
if buy_route and not product_tmpl_id.seller_ids: |
| 95 |
domain = Domain.AND([domain, Domain('id', '!=', buy_route.id)]) |
| 96 |
return domain |