31
# Cache for product/location
32
34
class procurement_order(osv.osv):
33
35
_name = 'procurement.order'
34
36
_inherit = 'procurement.order'
36
def run_automatic_supply(self, cr, uid, use_new_cursor=False, batch_id=False, context=None):
38
def run_automatic_supply(self, cr, uid, use_new_cursor=False, context={}):
38
40
Create procurement on fixed date
43
45
request_obj = self.pool.get('res.request')
44
46
auto_sup_obj = self.pool.get('stock.warehouse.automatic.supply')
45
47
proc_obj = self.pool.get('procurement.order')
46
freq_obj = self.pool.get('stock.frequence')
48
start_date = time.strftime('%Y-%m-%d %H:%M:%S')
49
auto_sup_ids = auto_sup_obj.search(cr, uid, [('next_date', '<=', datetime.now())])
49
auto_sup_ids = auto_sup_obj.search(cr, uid, [('next_date', '=', datetime.today().strftime('%Y-%m-%d')), ('product_id', '=', False)])
55
start_date = datetime.now()
55
57
# We start with only category Automatic Supply
56
58
for auto_sup in auto_sup_obj.browse(cr, uid, auto_sup_ids):
57
59
# We define the replenish location
60
62
location_id = auto_sup.warehouse_id.lot_input_id.id
62
64
location_id = auto_sup.location_id.id
64
# We create a procurement order for each line of the rule
65
66
for line in auto_sup.line_ids:
66
67
proc_id = self.create_proc_order(cr, uid, auto_sup, line.product_id,
67
68
line.product_uom_id.id, line.product_qty,
68
69
location_id, context=context)
69
# If a procurement has been created, add it to the list
71
71
created_proc.append(proc_id)
73
# Update the frequence to save the date of the last run
74
if auto_sup.frequence_id:
75
freq_obj.write(cr, uid, auto_sup.frequence_id.id, {'last_run': datetime.now()})
79
# Add created document and exception in a request
81
created_doc = '''################################
82
Created documents : \n'''
73
# Next, for one product automatic supply
74
auto_sup_ids = auto_sup_obj.search(cr, uid, [('next_date', '=', datetime.today().strftime('%Y-%m-%d')), ('product_id', '!=', False)])
75
for auto_sup in auto_sup_obj.browse(cr, uid, auto_sup_ids):
76
# We define the replenish location
78
if not auto_sup.location_id or not auto_sup.location_id.id:
79
location_id = auto_sup.warehouse_id.lot_input_id.id
81
location_id = auto_sup.location_id.id
83
proc_id = self.create_proc_order(cr, uid, auto_sup, auto_sup.product_id, auto_sup.product_uom_id.id,
84
auto_sup.product_qty, location_id, context)
86
created_proc.append(proc_id)
84
88
for proc in proc_obj.browse(cr, uid, created_proc):
85
89
if proc.state == 'exception':
87
91
(proc.id, proc.product_qty, proc.product_uom.name,
88
92
proc.product_id.name,))
90
elif proc.purchase_id:
91
created_doc += " * %s => %s \n" % (proc.name, proc.purchase_id.name)
93
end_date = time.strftime('%Y-%m-%d %H:%M:%S')
95
end_date = datetime.now()
95
97
summary = '''Here is the procurement scheduling report for Automatic Supplies
99
101
Total Procurements processed: %d
100
102
Procurements with exceptions: %d
102
\n %s \n Exceptions: \n'''% (start_date, end_date, len(created_proc), report_except, len(created_proc) > 0 and created_doc or '')
104
Exceptions:\n'''% (start_date, end_date, len(created_proc), report_except)
104
105
summary += '\n'.join(report)
106
self.pool.get('procurement.batch.cron').write(cr, uid, batch_id, {'last_run_on': time.strftime('%Y-%m-%d %H:%M:%S')})
107
old_request = request_obj.search(cr, uid, [('batch_id', '=', batch_id), ('name', '=', 'Procurement Processing Report (Automatic supplies).')])
108
request_obj.write(cr, uid, old_request, {'batch_id': False})
109
106
request_obj.create(cr, uid,
110
{'name': "Procurement Processing Report (Automatic supplies).",
107
{'name': "Procurement Processing Report.",
113
'batch_id': batch_id,
116
# UF-952 : Requests should be in consistent state
118
# request_obj.request_send(cr, uid, [req_id])
120
113
if use_new_cursor:
126
def create_proc_order(self, cr, uid, auto_sup, product_id, product_uom, qty, location_id, context=None):
119
def create_proc_order(self, cr, uid, auto_sup, product_id, product_uom, qty, location_id, context={}):
128
121
Creates a procurement order for a product and a location
130
123
proc_obj = self.pool.get('procurement.order')
131
124
auto_sup_obj = self.pool.get('stock.warehouse.automatic.supply')
132
125
wf_service = netsvc.LocalService("workflow")
129
# Enter the stock location in cache to know which products has been already replenish for this location
130
if not cache.get(location_id, False):
131
cache.update({location_id: []})
133
if product_id.id not in cache.get(location_id):
136
134
newdate = datetime.today()
137
135
proc_id = proc_obj.create(cr, uid, {
138
136
'name': _('Automatic Supply: %s') % (auto_sup.name,),
147
145
wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_confirm', cr)
148
146
wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_check', cr)
149
147
auto_sup_obj.write(cr, uid, [auto_sup.id], {'procurement_id': proc_id}, context=context)
150
cache.get(location_id).append(product_id.id)
153
def _hook_product_type_consu(self, cr, uid, *args, **kwargs):
155
kwargs['op'] is the current min/max rule
159
154
procurement_order()
161
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
156
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
b'\\ No newline at end of file'