1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
25 from mx import DateTime
29 class mrp_procurement(osv.osv):
30 _inherit = 'mrp.procurement'
32 def _procure_confirm(self, cr, uid, ids=None, use_new_cursor=False, context=None):
34 use_new_cursor: False or the dbname
40 cr = pooler.get_db(use_new_cursor).cursor()
41 wf_service = netsvc.LocalService("workflow")
43 procurement_obj = self.pool.get('mrp.procurement')
45 ids = procurement_obj.search(cr, uid, [], order="date_planned")
47 wf_service.trg_validate(uid, 'mrp.procurement', id, 'button_restart', cr)
51 company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
52 maxdate = DateTime.now() + DateTime.RelativeDateTime(days=company.schedule_range)
53 start_date = time.strftime('%Y-%m-%d, %Hh %Mm %Ss')
60 cr.execute('select id from mrp_procurement where state=%s and procure_method=%s order by priority,date_planned limit 500 offset %s', ('confirmed', 'make_to_order', offset))
61 ids = map(lambda x: x[0], cr.fetchall())
62 for proc in procurement_obj.browse(cr, uid, ids):
63 if (maxdate.strftime('%Y-%m-%d')>=proc.date_planned):
64 wf_service.trg_validate(uid, 'mrp.procurement', proc.id, 'button_check', cr)
68 for proc in procurement_obj.browse(cr, uid, ids):
69 if proc.state == 'exception':
70 report.append('PROC %d: on order - %3.2f %-5s - %s' % \
71 (proc.id, proc.product_qty, proc.product_uom.name,
72 proc.product_id.name))
84 ids = self.pool.get('mrp.procurement').search(cr, uid, [('state', '=', 'confirmed'), ('procure_method', '=', 'make_to_stock')], offset=offset)
85 for proc in procurement_obj.browse(cr, uid, ids):
86 if ((maxdate).strftime('%Y-%m-%d') >= proc.date_planned) :
87 wf_service.trg_validate(uid, 'mrp.procurement', proc.id, 'button_check', cr)
88 report_ids.append(proc.id)
92 for proc in procurement_obj.browse(cr, uid, report_ids):
93 if proc.state == 'exception':
94 report.append('PROC %d: from stock - %3.2f %-5s - %s' % \
95 (proc.id, proc.product_qty, proc.product_uom.name,
96 proc.product_id.name,))
102 end_date = time.strftime('%Y-%m-%d, %Hh %Mm %Ss')
104 request = self.pool.get('res.request')
105 summary = '''Here is the procurement scheduling report.
107 Computation Started; %s
108 Computation Finished; %s
110 Total procurement: %d
111 Exception procurement: %d
112 Not run now procurement: %d
115 '''% (start_date, end_date, report_total, report_except, report_later)
116 summary += '\n'.join(report)
117 request.create(cr, uid,
118 {'name': "Requisition calculation report.",
128 def create_automatic_op(self, cr, uid, context=None):
131 product_obj = self.pool.get('product.product')
132 proc_obj = self.pool.get('mrp.procurement')
133 warehouse_obj = self.pool.get('stock.warehouse')
134 wf_service = netsvc.LocalService("workflow")
136 warehouse_ids = warehouse_obj.search(cr, uid, [], context=context)
138 cr.execute('select id from product_product')
139 products_id = [x for x, in cr.fetchall()]
141 for warehouse in warehouse_obj.browse(cr, uid, warehouse_ids, context=context):
142 context['warehouse'] = warehouse
143 for product in self.pool.get('product.product').browse(cr, uid, products_id, context=context):
144 if product.virtual_available >= 0.0:
147 newdate = DateTime.now()
148 if product.supply_method == 'buy':
149 location_id = warehouse.lot_input_id.id
150 elif product.supply_method == 'produce':
151 location_id = warehouse.lot_stock_id.id
154 proc_id = proc_obj.create(cr, uid, {
155 'name': 'Automatic OP: %s' % product.name,
156 'origin': 'SCHEDULER',
157 'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
158 'product_id': product.id,
159 'product_qty': -product.virtual_available,
160 'product_uom': product.uom_id.id,
161 'location_id': location_id,
162 'procure_method': 'make_to_order',
164 wf_service.trg_validate(uid, 'mrp.procurement', proc_id, 'button_confirm', cr)
165 wf_service.trg_validate(uid, 'mrp.procurement', proc_id, 'button_check', cr)
167 def _procure_orderpoint_confirm(self, cr, uid, automatic=False,\
168 use_new_cursor=False, context=None, user_id=False):
170 use_new_cursor: False or the dbname
175 cr = pooler.get_db(use_new_cursor).cursor()
176 orderpoint_obj = self.pool.get('stock.warehouse.orderpoint')
177 location_obj = self.pool.get('stock.location')
178 procurement_obj = self.pool.get('mrp.procurement')
179 request_obj = self.pool.get('res.request')
180 wf_service = netsvc.LocalService("workflow")
185 self.create_automatic_op(cr, uid, context=context)
187 ids = orderpoint_obj.search(cr, uid, [], offset=offset, limit=100)
188 for op in orderpoint_obj.browse(cr, uid, ids):
189 if op.procurement_id and op.procurement_id.purchase_id and op.procurement_id.purchase_id.state in ('draft', 'confirmed'):
191 prods = location_obj._product_virtual_get(cr, uid,
192 op.location_id.id, [op.product_id.id],
193 {'uom': op.product_uom.id})[op.product_id.id]
194 if prods < op.product_min_qty:
195 qty = max(op.product_min_qty, op.product_max_qty)-prods
196 reste = qty % op.qty_multiple
198 qty += op.qty_multiple - reste
199 newdate = DateTime.now() + DateTime.RelativeDateTime(
200 days=int(op.product_id.seller_delay))
201 if op.product_id.supply_method == 'buy':
202 location_id = op.warehouse_id.lot_input_id
203 elif op.product_id.supply_method == 'produce':
204 location_id = op.warehouse_id.lot_stock_id
209 if op.product_id.type not in ('consu'):
210 proc_id = procurement_obj.create(cr, uid, {
211 'name': 'OP:' + str(op.id),
212 'date_planned': newdate.strftime('%Y-%m-%d'),
213 'product_id': op.product_id.id,
215 'product_uom': op.product_uom.id,
216 'location_id': op.warehouse_id.lot_input_id.id,
217 'procure_method': 'make_to_order',
220 wf_service.trg_validate(uid, 'mrp.procurement', proc_id,
221 'button_confirm', cr)
222 wf_service.trg_validate(uid, 'mrp.procurement', proc_id,
224 orderpoint_obj.write(cr, uid, [op.id],
225 {'procurement_id': proc_id})
229 if user_id and report:
230 request_obj.create(cr, uid, {
231 'name': 'Orderpoint report.',
234 'body': '\n'.join(report)
241 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: