30
from mx.DateTime import *
32
32
class procurement_order(osv.osv):
33
33
_name = 'procurement.order'
34
34
_inherit = 'procurement.order'
36
def run_threshold_value(self, cr, uid, use_new_cursor=False, batch_id=False, context=None):
36
def run_threshold_value(self, cr, uid, use_new_cursor=False, context={}):
38
38
Creates procurement for products where real stock is under threshold value
45
41
cr = pooler.get_db(use_new_cursor).cursor()
47
43
request_obj = self.pool.get('res.request')
48
44
threshold_obj = self.pool.get('threshold.value')
49
45
proc_obj = self.pool.get('procurement.order')
46
product_obj = self.pool.get('product.product')
51
48
threshold_ids = threshold_obj.search(cr, uid, [], context=context)
56
start_date = time.strftime('%Y-%m-%d %H:%M:%S')
53
start_date = datetime.now()
58
55
wf_service = netsvc.LocalService("workflow")
60
57
for threshold in threshold_obj.browse(cr, uid, threshold_ids, context=context):
61
for line in threshold.line_ids:
62
if line.threshold_value >= line.product_id.virtual_available and line.product_qty > 0.00:
58
# Set location_id in context for tre AMC calculation
59
context.update({'location_id': threshold.location_id and threshold.location_id.id or False})
61
# Set the product list according to the category or product defined in the rule
63
if threshold.category_id:
64
products.extend(product_obj.search(cr, uid, [('categ_id', '=', threshold.category_id.id)], context=context))
65
elif threshold.product_id:
66
products.append(threshold.product_id.id)
68
# Set different data by products for calculation
69
for product_id in products:
70
product = product_obj.browse(cr, uid, product_id, context=context)
71
amc = product_obj.compute_amc(cr, uid, product_id, context=context)
73
# Set lead time according to choices in threshold rule (supplier or manual lead time)
74
lead_time = threshold.supplier_lt and float(product.seller_delay)/30.0 or threshold.lead_time
76
# Compute the threshold value
77
threshold_value = threshold.threshold_manual_ok and threshold.threshold_value or amc * (lead_time + threshold.safety_month)
78
threshold_value = self.pool.get('product.uom')._compute_qty(cr, uid, product.uom_id.id, threshold_value, product.uom_id.id)
80
# Check if the quantity in stock needs a supply of products or not
81
if product.virtual_available <= threshold_value:
82
# Compute the quantity to re-order
83
qty_to_order = threshold.qty_order_manual_ok and threshold.qty_to_order \
84
or amc * (threshold.frequency + lead_time + threshold.safety_month)\
85
- product.qty_available + product.incoming_qty - product.outgoing_qty
86
qty_to_order = self.pool.get('product.uom')._compute_qty(cr, uid, threshold.uom_id and \
87
threshold.uom_id.id or product.uom_id.id, qty_to_order,\
63
90
proc_id = proc_obj.create(cr, uid, {
64
91
'name': _('Threshold value: %s') % (threshold.name,),
65
92
'origin': threshold.name,
66
93
'date_planned': time.strftime('%Y-%m-%d %H:%M:%S'),
67
'product_id': line.product_id.id,
68
'product_qty': line.product_qty,
69
'product_uom': line.product_id.uom_id.id,
94
'product_id': product.id,
95
'product_qty': qty_to_order,
96
'product_uom': product.uom_id.id,
70
97
'location_id': threshold.location_id.id,
71
98
'procure_method': 'make_to_order',
96
120
Total Procurements processed: %d
97
121
Procurements with exceptions: %d
98
\n %s \n Exceptions: \n'''% (start_date, end_date, len(created_proc), report_except, len(created_proc) > 0 and created_doc or '')
122
\n'''% (start_date, end_date, len(created_proc), report_except)
99
123
summary += '\n'.join(report)
101
self.pool.get('procurement.batch.cron').write(cr, uid, batch_id, {'last_run_on': time.strftime('%Y-%m-%d %H:%M:%S')})
102
old_request = request_obj.search(cr, uid, [('batch_id', '=', batch_id), ('name', '=', 'Procurement Processing Report (Threshold values).')])
103
request_obj.write(cr, uid, old_request, {'batch_id': False})
104
124
req_id = request_obj.create(cr, uid,
105
125
{'name': "Procurement Processing Report (Threshold values).",
108
'batch_id': batch_id,
111
# UF-952 : Requests should be in consistent state
113
# request_obj.request_send(cr, uid, [req_id])
131
request_obj.request_send(cr, uid, [req_id])
115
133
if use_new_cursor: