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 ##############################################################################
23 from datetime import datetime
24 from dateutil.relativedelta import relativedelta
26 from tools.translate import _
31 class procurement_order(osv.osv):
32 _inherit = 'procurement.order'
34 def _procure_confirm(self, cr, uid, ids=None, use_new_cursor=False, context=None):
36 Call the scheduler to check the procurement order
38 @param self: The object pointer
39 @param cr: The current row, from the database cursor,
40 @param uid: The current user ID for security checks
41 @param ids: List of selected IDs
42 @param use_new_cursor: False or the dbname
43 @param context: A standard dictionary for contextual values
44 @return: Dictionary of values
51 cr = pooler.get_db(use_new_cursor).cursor()
52 wf_service = netsvc.LocalService("workflow")
54 procurement_obj = self.pool.get('procurement.order')
56 ids = procurement_obj.search(cr, uid, [], order="date_planned")
58 wf_service.trg_validate(uid, 'procurement.order', id, 'button_restart', cr)
61 company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
62 maxdate = (datetime.today() + relativedelta(days=company.schedule_range)).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
63 start_date = time.strftime('%Y-%m-%d, %Hh %Mm %Ss')
70 cr.execute("select id from procurement_order where state='confirmed' and procure_method='make_to_order' order by priority,date_planned limit 500 offset %s", (offset,))
71 ids = map(lambda x: x[0], cr.fetchall())
72 for proc in procurement_obj.browse(cr, uid, ids, context=context):
73 if maxdate >= proc.date_planned:
74 wf_service.trg_validate(uid, 'procurement.order', proc.id, 'button_check', cr)
78 for proc in procurement_obj.browse(cr, uid, ids, context=context):
79 if proc.state == 'exception':
80 report.append('PROC %d: on order - %3.2f %-5s - %s' % \
81 (proc.id, proc.product_qty, proc.product_uom.name,
82 proc.product_id.name))
93 ids = procurement_obj.search(cr, uid, [('state', '=', 'confirmed'), ('procure_method', '=', 'make_to_stock')], offset=offset)
94 for proc in procurement_obj.browse(cr, uid, ids):
95 if maxdate >= proc.date_planned:
96 wf_service.trg_validate(uid, 'procurement.order', proc.id, 'button_check', cr)
97 report_ids.append(proc.id)
101 for proc in procurement_obj.browse(cr, uid, report_ids, context=context):
102 if proc.state == 'exception':
103 report.append('PROC %d: from stock - %3.2f %-5s - %s' % \
104 (proc.id, proc.product_qty, proc.product_uom.name,
105 proc.product_id.name,))
111 end_date = time.strftime('%Y-%m-%d, %Hh %Mm %Ss')
113 request = self.pool.get('res.request')
114 summary = '''Here is the procurement scheduling report.
118 Total Procurements processed: %d
119 Procurements with exceptions: %d
120 Skipped Procurements (scheduled date outside of scheduler range) %d
122 Exceptions:\n'''% (start_date, end_date, report_total, report_except, report_later)
123 summary += '\n'.join(report)
124 request.create(cr, uid,
125 {'name': "Procurement Processing Report.",
141 def create_automatic_op(self, cr, uid, context=None):
143 Create procurement of virtual stock < 0
145 @param self: The object pointer
146 @param cr: The current row, from the database cursor,
147 @param uid: The current user ID for security checks
148 @param context: A standard dictionary for contextual values
149 @return: Dictionary of values
153 product_obj = self.pool.get('product.product')
154 proc_obj = self.pool.get('procurement.order')
155 warehouse_obj = self.pool.get('stock.warehouse')
156 wf_service = netsvc.LocalService("workflow")
158 warehouse_ids = warehouse_obj.search(cr, uid, [], context=context)
160 cr.execute('select p.id from product_product p \
161 join product_template t on (p.product_tmpl_id=t.id) \
162 where p.active=True and t.purchase_ok=True')
163 products_id = [x for x, in cr.fetchall()]
165 for warehouse in warehouse_obj.browse(cr, uid, warehouse_ids, context=context):
166 context['warehouse'] = warehouse
167 for product in product_obj.browse(cr, uid, products_id, context=context):
168 if product.virtual_available >= 0.0:
171 newdate = datetime.today()
172 if product.supply_method == 'buy':
173 location_id = warehouse.lot_input_id.id
174 elif product.supply_method == 'produce':
175 location_id = warehouse.lot_stock_id.id
178 proc_id = proc_obj.create(cr, uid, {
179 'name': _('Automatic OP: %s') % (product.name,),
180 'origin': _('SCHEDULER'),
181 'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
182 'product_id': product.id,
183 'product_qty': -product.virtual_available,
184 'product_uom': product.uom_id.id,
185 'location_id': location_id,
186 'procure_method': 'make_to_order',
188 wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_confirm', cr)
189 wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_check', cr)
191 def _procure_orderpoint_confirm(self, cr, uid, automatic=False,\
192 use_new_cursor=False, context=None, user_id=False):
194 Create procurement based on Orderpoint
195 use_new_cursor: False or the dbname
197 @param self: The object pointer
198 @param cr: The current row, from the database cursor,
199 @param user_id: The current user ID for security checks
200 @param context: A standard dictionary for contextual values
201 @param param: False or the dbname
202 @return: Dictionary of values
208 cr = pooler.get_db(use_new_cursor).cursor()
209 orderpoint_obj = self.pool.get('stock.warehouse.orderpoint')
210 location_obj = self.pool.get('stock.location')
211 procurement_obj = self.pool.get('procurement.order')
212 request_obj = self.pool.get('res.request')
213 wf_service = netsvc.LocalService("workflow")
218 self.create_automatic_op(cr, uid, context=context)
220 ids = orderpoint_obj.search(cr, uid, [], offset=offset, limit=100)
221 for op in orderpoint_obj.browse(cr, uid, ids, context=context):
222 if op.procurement_id.state != 'exception':
223 if op.procurement_id and op.procurement_id.purchase_id and op.procurement_id.purchase_id.state in ('draft', 'confirmed'):
225 prods = location_obj._product_virtual_get(cr, uid,
226 op.location_id.id, [op.product_id.id],
227 {'uom': op.product_uom.id})[op.product_id.id]
229 if prods < op.product_min_qty:
230 qty = max(op.product_min_qty, op.product_max_qty)-prods
232 reste = qty % op.qty_multiple
234 qty += op.qty_multiple - reste
236 newdate = datetime.today() + relativedelta(
237 days = int(op.product_id.seller_delay))
240 if op.product_id.type not in ('consu'):
241 if op.procurement_draft_ids:
242 # Check draft procurement related to this order point
243 pro_ids = [x.id for x in op.procurement_draft_ids]
244 cr.execute('select id, product_qty from procurement_order where id in %s order by product_qty desc', (tuple(pro_ids), ))
245 procure_datas = cr.dictfetchall()
247 for proc_data in procure_datas:
248 if to_generate >= proc_data['product_qty']:
249 wf_service.trg_validate(uid, 'procurement.order', proc_data['id'], 'button_confirm', cr)
250 procurement_obj.write(cr, uid, [proc_data['id']], {'origin': op.name}, context=context)
251 to_generate -= proc_data['product_qty']
257 proc_id = procurement_obj.create(cr, uid, {
259 'date_planned': newdate.strftime('%Y-%m-%d'),
260 'product_id': op.product_id.id,
262 'product_uom': op.product_uom.id,
263 'location_id': op.location_id.id,
264 'procure_method': 'make_to_order',
267 wf_service.trg_validate(uid, 'procurement.order', proc_id,
268 'button_confirm', cr)
269 wf_service.trg_validate(uid, 'procurement.order', proc_id,
271 orderpoint_obj.write(cr, uid, [op.id],
272 {'procurement_id': proc_id}, context=context)
276 if user_id and report:
277 request_obj.create(cr, uid, {
278 'name': 'Orderpoint report.',
281 'body': '\n'.join(report)
290 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: