Small code enhancements to purchase_suggest
WARNING : this code changes the method to select the location_id and pick_type_id on PO
This commit is contained in:
@@ -53,26 +53,27 @@ class PurchaseSuggestionGenerate(models.TransientModel):
|
|||||||
# I cannot filter on 'date_order' because it is not a stored field
|
# I cannot filter on 'date_order' because it is not a stored field
|
||||||
porderline_id = porderlines and porderlines[0].id or False
|
porderline_id = porderlines and porderlines[0].id or False
|
||||||
sline = {
|
sline = {
|
||||||
'company_id': qty_dict['orderpoint'].company_id.id,
|
'company_id':
|
||||||
|
qty_dict['orderpoint'] and qty_dict['orderpoint'].company_id.id,
|
||||||
'product_id': product_id,
|
'product_id': product_id,
|
||||||
'seller_id': qty_dict['product'].seller_id.id or False,
|
'seller_id': qty_dict['product'].seller_id.id or False,
|
||||||
'qty_available': qty_dict['qty_available'],
|
'qty_available': qty_dict['qty_available'],
|
||||||
'incoming_qty': qty_dict['incoming_qty'],
|
'incoming_qty': qty_dict['incoming_qty'],
|
||||||
'outgoing_qty': qty_dict['outgoing_qty'],
|
'outgoing_qty': qty_dict['outgoing_qty'],
|
||||||
'draft_po_qty': qty_dict['draft_po_qty'],
|
'draft_po_qty': qty_dict['draft_po_qty'],
|
||||||
'orderpoint_id': qty_dict['orderpoint'].id,
|
'orderpoint_id':
|
||||||
|
qty_dict['orderpoint'] and qty_dict['orderpoint'].id,
|
||||||
|
'location_id': self.location_id.id,
|
||||||
'min_qty': qty_dict['min_qty'],
|
'min_qty': qty_dict['min_qty'],
|
||||||
'last_po_line_id': porderline_id,
|
'last_po_line_id': porderline_id,
|
||||||
}
|
}
|
||||||
return sline
|
return sline
|
||||||
|
|
||||||
@api.multi
|
@api.model
|
||||||
def run(self):
|
def generate_products_dict(self):
|
||||||
self.ensure_one()
|
|
||||||
pso = self.env['purchase.suggest']
|
|
||||||
polo = self.env['purchase.order.line']
|
|
||||||
swoo = self.env['stock.warehouse.orderpoint']
|
|
||||||
ppo = self.env['product.product']
|
ppo = self.env['product.product']
|
||||||
|
swoo = self.env['stock.warehouse.orderpoint']
|
||||||
|
products = {}
|
||||||
op_domain = [
|
op_domain = [
|
||||||
('suggest', '=', True),
|
('suggest', '=', True),
|
||||||
('company_id', '=', self.env.user.company_id.id),
|
('company_id', '=', self.env.user.company_id.id),
|
||||||
@@ -89,12 +90,6 @@ class PurchaseSuggestionGenerate(models.TransientModel):
|
|||||||
products_subset = ppo.search(product_domain)
|
products_subset = ppo.search(product_domain)
|
||||||
op_domain.append(('product_id', 'in', products_subset.ids))
|
op_domain.append(('product_id', 'in', products_subset.ids))
|
||||||
ops = swoo.search(op_domain)
|
ops = swoo.search(op_domain)
|
||||||
p_suggest_lines = []
|
|
||||||
products = {}
|
|
||||||
# key = product_id
|
|
||||||
# value = {'virtual_qty': 1.0, 'draft_po_qty': 4.0, 'min_qty': 6.0}
|
|
||||||
# TODO : handle the uom
|
|
||||||
logger.info('Starting to compute the purchase suggestions')
|
|
||||||
for op in ops:
|
for op in ops:
|
||||||
if op.product_id.id not in products:
|
if op.product_id.id not in products:
|
||||||
products[op.product_id.id] = {
|
products[op.product_id.id] = {
|
||||||
@@ -111,6 +106,19 @@ class PurchaseSuggestionGenerate(models.TransientModel):
|
|||||||
products[op.product_id.id]['orderpoint'].name,
|
products[op.product_id.id]['orderpoint'].name,
|
||||||
op.name,
|
op.name,
|
||||||
self.location_id.complete_name))
|
self.location_id.complete_name))
|
||||||
|
return products
|
||||||
|
|
||||||
|
@api.multi
|
||||||
|
def run(self):
|
||||||
|
self.ensure_one()
|
||||||
|
pso = self.env['purchase.suggest']
|
||||||
|
polo = self.env['purchase.order.line']
|
||||||
|
p_suggest_lines = []
|
||||||
|
products = self.generate_products_dict()
|
||||||
|
# key = product_id
|
||||||
|
# value = {'virtual_qty': 1.0, 'draft_po_qty': 4.0, 'min_qty': 6.0}
|
||||||
|
# TODO : handle the uom
|
||||||
|
logger.info('Starting to compute the purchase suggestions')
|
||||||
logger.info('Min qty computed on %d products', len(products))
|
logger.info('Min qty computed on %d products', len(products))
|
||||||
polines = polo.search([
|
polines = polo.search([
|
||||||
('state', '=', 'draft'), ('product_id', 'in', products.keys())])
|
('state', '=', 'draft'), ('product_id', 'in', products.keys())])
|
||||||
@@ -135,10 +143,11 @@ class PurchaseSuggestionGenerate(models.TransientModel):
|
|||||||
'Min. qty = %s',
|
'Min. qty = %s',
|
||||||
product_id, qty_dict['virtual_available'],
|
product_id, qty_dict['virtual_available'],
|
||||||
qty_dict['draft_po_qty'], qty_dict['min_qty'])
|
qty_dict['draft_po_qty'], qty_dict['min_qty'])
|
||||||
if float_compare(
|
compare = float_compare(
|
||||||
qty_dict['virtual_available'] + qty_dict['draft_po_qty'],
|
qty_dict['virtual_available'] + qty_dict['draft_po_qty'],
|
||||||
qty_dict['min_qty'],
|
qty_dict['min_qty'],
|
||||||
precision_rounding=op.product_uom.rounding) < 0:
|
precision_rounding=qty_dict['product'].uom_id.rounding)
|
||||||
|
if compare < 0:
|
||||||
vals = self._prepare_suggest_line(product_id, qty_dict)
|
vals = self._prepare_suggest_line(product_id, qty_dict)
|
||||||
if vals:
|
if vals:
|
||||||
p_suggest_lines.append(vals)
|
p_suggest_lines.append(vals)
|
||||||
@@ -201,6 +210,8 @@ class PurchaseSuggest(models.TransientModel):
|
|||||||
orderpoint_id = fields.Many2one(
|
orderpoint_id = fields.Many2one(
|
||||||
'stock.warehouse.orderpoint', string='Re-ordering Rule',
|
'stock.warehouse.orderpoint', string='Re-ordering Rule',
|
||||||
readonly=True)
|
readonly=True)
|
||||||
|
location_id = fields.Many2one(
|
||||||
|
'stock.location', string='Stock Location', readonly=True)
|
||||||
min_qty = fields.Float(
|
min_qty = fields.Float(
|
||||||
string="Min Quantity", readonly=True,
|
string="Min Quantity", readonly=True,
|
||||||
digits=dp.get_precision('Product Unit of Measure'))
|
digits=dp.get_precision('Product Unit of Measure'))
|
||||||
@@ -223,9 +234,14 @@ class PurchaseSuggestPoCreate(models.TransientModel):
|
|||||||
pick_type_dom = [
|
pick_type_dom = [
|
||||||
('code', '=', 'incoming'),
|
('code', '=', 'incoming'),
|
||||||
('warehouse_id.company_id', '=', company.id)]
|
('warehouse_id.company_id', '=', company.id)]
|
||||||
|
|
||||||
pick_types = spto.search(
|
pick_types = spto.search(
|
||||||
pick_type_dom + [('default_location_dest_id', '=', location.id)])
|
pick_type_dom + [(
|
||||||
|
'default_location_dest_id',
|
||||||
|
'child_of',
|
||||||
|
location.location_id.id)])
|
||||||
|
# I use location.parent_id.id to support 2 step-receptions
|
||||||
|
# where the stock.location .type is linked to Warehouse > Receipt
|
||||||
|
# but location is Warehouse > Stock
|
||||||
if not pick_types:
|
if not pick_types:
|
||||||
pick_types = spto.search(pick_type_dom)
|
pick_types = spto.search(pick_type_dom)
|
||||||
if not pick_types:
|
if not pick_types:
|
||||||
@@ -233,11 +249,8 @@ class PurchaseSuggestPoCreate(models.TransientModel):
|
|||||||
"Make sure you have at least an incoming picking "
|
"Make sure you have at least an incoming picking "
|
||||||
"type defined"))
|
"type defined"))
|
||||||
po_vals['picking_type_id'] = pick_types[0].id
|
po_vals['picking_type_id'] = pick_types[0].id
|
||||||
pick_type_dict = ponull.onchange_picking_type_id(pick_types.id)
|
pick_type_dict = ponull.onchange_picking_type_id(pick_types[0].id)
|
||||||
po_vals.update(pick_type_dict['value'])
|
po_vals.update(pick_type_dict['value'])
|
||||||
# I do that at the very end because onchange_picking_type_id()
|
|
||||||
# returns a default location_id
|
|
||||||
po_vals['location_id'] = location.id
|
|
||||||
return po_vals
|
return po_vals
|
||||||
|
|
||||||
def _prepare_purchase_order_line(self, partner, product, qty_to_order):
|
def _prepare_purchase_order_line(self, partner, product, qty_to_order):
|
||||||
@@ -307,7 +320,7 @@ class PurchaseSuggestPoCreate(models.TransientModel):
|
|||||||
location = False
|
location = False
|
||||||
for line in self.env['purchase.suggest'].browse(psuggest_ids):
|
for line in self.env['purchase.suggest'].browse(psuggest_ids):
|
||||||
if not location:
|
if not location:
|
||||||
location = line.orderpoint_id.location_id
|
location = line.location_id
|
||||||
if not line.qty_to_order:
|
if not line.qty_to_order:
|
||||||
continue
|
continue
|
||||||
if not line.product_id.seller_id:
|
if not line.product_id.seller_id:
|
||||||
|
|||||||
Reference in New Issue
Block a user