1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
26 from mx import DateTime
30 class mrp_procurement(osv.osv):
31 _inherit = 'mrp.procurement'
33 def _procure_confirm(self, cr, uid, ids=None, use_new_cursor=False, context=None):
35 use_new_cursor: False or the dbname
41 cr = pooler.get_db(use_new_cursor).cursor()
42 wf_service = netsvc.LocalService("workflow")
44 procurement_obj = self.pool.get('mrp.procurement')
46 ids = procurement_obj.search(cr, uid, [], order="date_planned")
48 wf_service.trg_validate(uid, 'mrp.procurement', id, 'button_restart', cr)
52 company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
53 maxdate = DateTime.now() + DateTime.RelativeDateTime(days=company.schedule_range)
54 start_date = time.strftime('%Y-%m-%d, %Hh %Mm %Ss')
61 cr.execute('select id from mrp_procurement where state=%s and procure_method=%s order by date_planned limit 500 offset %s', ('confirmed', 'make_to_order', offset))
62 ids = map(lambda x: x[0], cr.fetchall())
63 for proc in procurement_obj.browse(cr, uid, ids):
64 if (maxdate.strftime('%Y-%m-%d')>=proc.date_planned):
65 wf_service.trg_validate(uid, 'mrp.procurement', proc.id, 'button_check', cr)
69 for proc in procurement_obj.browse(cr, uid, ids):
70 if proc.state == 'exception':
71 report.append('PROC %d: on order - %3.2f %-5s - %s' % \
72 (proc.id, proc.product_qty, proc.product_uom.name,
73 proc.product_id.name))
85 ids = self.pool.get('mrp.procurement').search(cr, uid, [('state', '=', 'confirmed'), ('procure_method', '=', 'make_to_stock')], offset=offset)
86 for proc in procurement_obj.browse(cr, uid, ids):
87 if ((maxdate).strftime('%Y-%m-%d') >= proc.date_planned) :
88 wf_service.trg_validate(uid, 'mrp.procurement', proc.id, 'button_check', cr)
89 report_ids.append(proc.id)
93 for proc in procurement_obj.browse(cr, uid, report_ids):
94 if proc.state == 'exception':
95 report.append('PROC %d: from stock - %3.2f %-5s - %s' % \
96 (proc.id, proc.product_qty, proc.product_uom.name,
97 proc.product_id.name,))
103 end_date = time.strftime('%Y-%m-%d, %Hh %Mm %Ss')
105 request = self.pool.get('res.request')
106 summary = '''Here is the procurement scheduling report.
108 Computation Started; %s
109 Computation Finished; %s
111 Total procurement: %d
112 Exception procurement: %d
113 Not run now procurement: %d
116 '''% (start_date, end_date, report_total, report_except, report_later)
117 summary += '\n'.join(report)
118 request.create(cr, uid,
119 {'name': "Procurement calculation report.",
129 def create_automatic_op(self, cr, uid, context=None):
132 product_obj = self.pool.get('product.product')
133 proc_obj = self.pool.get('mrp.procurement')
134 warehouse_obj = self.pool.get('stock.warehouse')
135 wf_service = netsvc.LocalService("workflow")
137 warehouse_ids = warehouse_obj.search(cr, uid, [], context=context)
139 cr.execute('select id from product_product')
140 products_id = [x for x, in cr.fetchall()]
142 for warehouse in warehouse_obj.browse(cr, uid, warehouse_ids, context=context):
143 context['warehouse'] = warehouse
144 for product in self.pool.get('product.product').browse(cr, uid, products_id, context=context):
145 if product.virtual_available >= 0.0:
148 newdate = DateTime.now()
149 if product.supply_method == 'buy':
150 location_id = warehouse.lot_input_id.id
151 elif product.supply_method == 'produce':
152 location_id = warehouse.lot_stock_id.id
155 proc_id = proc_obj.create(cr, uid, {
156 'name': 'Automatic OP: %s' % product.name,
157 'origin': 'SCHEDULER',
158 'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
159 'product_id': product.id,
160 'product_qty': -product.virtual_available,
161 'product_uom': product.uom_id.id,
162 'location_id': location_id,
163 'procure_method': 'make_to_order',
165 wf_service.trg_validate(uid, 'mrp.procurement', proc_id, 'button_confirm', cr)
166 wf_service.trg_validate(uid, 'mrp.procurement', proc_id, 'button_check', cr)
168 def _procure_orderpoint_confirm(self, cr, uid, automatic=False,\
169 use_new_cursor=False, context=None, user_id=False):
171 use_new_cursor: False or the dbname
176 cr = pooler.get_db(use_new_cursor).cursor()
177 orderpoint_obj = self.pool.get('stock.warehouse.orderpoint')
178 location_obj = self.pool.get('stock.location')
179 procurement_obj = self.pool.get('mrp.procurement')
180 request_obj = self.pool.get('res.request')
181 wf_service = netsvc.LocalService("workflow")
186 self.create_automatic_op(cr, uid, context=context)
188 ids = orderpoint_obj.search(cr, uid, [], offset=offset, limit=100)
189 for op in orderpoint_obj.browse(cr, uid, ids):
190 if op.procurement_id and op.procurement_id.purchase_id and op.procurement_id.purchase_id.state in ('draft', 'confirmed'):
192 prods = location_obj._product_virtual_get(cr, uid,
193 op.location_id.id, [op.product_id.id],
194 {'uom': op.product_uom.id})[op.product_id.id]
195 if prods < op.product_min_qty:
196 qty = max(op.product_min_qty, op.product_max_qty)-prods
197 reste = qty % op.qty_multiple
199 qty += op.qty_multiple - reste
200 newdate = DateTime.now() + DateTime.RelativeDateTime(
201 days=int(op.product_id.seller_delay))
202 if op.product_id.supply_method == 'buy':
203 location_id = op.warehouse_id.lot_input_id
204 elif op.product_id.supply_method == 'produce':
205 location_id = op.warehouse_id.lot_stock_id
210 if op.product_id.type not in ('consu'):
211 proc_id = procurement_obj.create(cr, uid, {
212 'name': 'OP:' + str(op.id),
213 'date_planned': newdate.strftime('%Y-%m-%d'),
214 'product_id': op.product_id.id,
216 'product_uom': op.product_uom.id,
217 'location_id': op.warehouse_id.lot_input_id.id,
218 'procure_method': 'make_to_order',
221 wf_service.trg_validate(uid, 'mrp.procurement', proc_id,
222 'button_confirm', cr)
223 wf_service.trg_validate(uid, 'mrp.procurement', proc_id,
225 orderpoint_obj.write(cr, uid, [op.id],
226 {'procurement_id': proc_id})
230 if user_id and report:
231 request_obj.create(cr, uid, {
232 'name': 'Orderpoint report.',
235 'body': '\n'.join(report)
242 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: