[FIX] Set the good url redirect in "Sign In" button from the cart
[odoo/odoo.git] / addons / procurement / schedulers.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from datetime import datetime
23 from dateutil.relativedelta import relativedelta
24
25 import openerp
26 from openerp.osv import osv
27 from openerp.osv import fields
28 from openerp.tools.translate import _
29 from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
30 from openerp import tools
31
32 class procurement_order(osv.osv):
33     _inherit = 'procurement.order'
34
35     def run_scheduler(self, cr, uid, automatic=False, use_new_cursor=False, skip_exception=False, context=None):
36         ''' Runs through scheduler.
37         @param use_new_cursor: False or the dbname
38         '''
39         if use_new_cursor:
40             use_new_cursor = cr.dbname
41         self._procure_confirm(cr, uid, use_new_cursor=use_new_cursor, skip_exception=skip_exception, context=context)
42         self._procure_orderpoint_confirm(cr, uid, automatic=automatic,\
43                 use_new_cursor=use_new_cursor, context=context)
44
45     def _procure_confirm(self, cr, uid, ids=None, use_new_cursor=False, skip_exception=False, context=None):
46         '''
47         Call the scheduler to check the procurement order
48
49         @param self: The object pointer
50         @param cr: The current row, from the database cursor,
51         @param uid: The current user ID for security checks
52         @param ids: List of selected IDs
53         @param use_new_cursor: False or the dbname
54         @param skip_exception: boolean
55         @param context: A standard dictionary for contextual values
56         @return:  Dictionary of values
57         '''
58         if context is None:
59             context = {}
60         try:
61             if use_new_cursor:
62                 cr = openerp.registry(use_new_cursor).db.cursor()
63
64             procurement_obj = self.pool.get('procurement.order')
65             if not skip_exception:
66                 if not ids:
67                     ids = procurement_obj.search(cr, uid, [('state', '=', 'exception')], order="date_planned")
68                 self.signal_button_restart(cr, uid, ids)
69                 if use_new_cursor:
70                     cr.commit()
71             company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
72             maxdate = (datetime.today() + relativedelta(days=company.schedule_range)).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
73             offset = 0
74             while True:
75                 ids = procurement_obj.search(cr, uid, [('state', '=', 'confirmed'), ('procure_method', '=', 'make_to_order')], offset=offset, limit=500, order='priority, date_planned', context=context)
76                 for proc in procurement_obj.browse(cr, uid, ids, context=context):
77                     if maxdate >= proc.date_planned:
78                         self.signal_button_check(cr, uid, [proc.id])
79                     else:
80                         offset += 1
81
82                 if use_new_cursor:
83                     cr.commit()
84                 if not ids:
85                     break
86             offset = 0
87             ids = []
88             while True:
89                 report_ids = []
90                 ids = procurement_obj.search(cr, uid, [('state', '=', 'confirmed'), ('procure_method', '=', 'make_to_stock')], offset=offset)
91                 for proc in procurement_obj.browse(cr, uid, ids):
92                     if maxdate >= proc.date_planned:
93                         self.signal_button_check(cr, uid, [proc.id])
94                         report_ids.append(proc.id)
95
96                 if use_new_cursor:
97                     cr.commit()
98                 offset += len(ids)
99                 if not ids: break
100
101             if use_new_cursor:
102                 cr.commit()
103         finally:
104             if use_new_cursor:
105                 try:
106                     cr.close()
107                 except Exception:
108                     pass
109         return {}
110
111     def _prepare_automatic_op_procurement(self, cr, uid, product, warehouse, location_id, context=None):
112         return {'name': _('Automatic OP: %s') % (product.name,),
113                 'origin': _('SCHEDULER'),
114                 'date_planned': datetime.today().strftime(DEFAULT_SERVER_DATETIME_FORMAT),
115                 'product_id': product.id,
116                 'product_qty': -product.virtual_available,
117                 'product_uom': product.uom_id.id,
118                 'location_id': location_id,
119                 'company_id': warehouse.company_id.id,
120                 'procure_method': 'make_to_order',}
121
122     def create_automatic_op(self, cr, uid, context=None):
123         """
124         Create procurement of  virtual stock < 0
125
126         @param self: The object pointer
127         @param cr: The current row, from the database cursor,
128         @param uid: The current user ID for security checks
129         @param context: A standard dictionary for contextual values
130         @return:  Dictionary of values
131         """
132         if context is None:
133             context = {}
134         product_obj = self.pool.get('product.product')
135         proc_obj = self.pool.get('procurement.order')
136         warehouse_obj = self.pool.get('stock.warehouse')
137
138         warehouse_ids = warehouse_obj.search(cr, uid, [], context=context)
139         products_ids = product_obj.search(cr, uid, [], order='id', context=context)
140
141         for warehouse in warehouse_obj.browse(cr, uid, warehouse_ids, context=context):
142             context['warehouse'] = warehouse
143             # Here we check products availability.
144             # We use the method 'read' for performance reasons, because using the method 'browse' may crash the server.
145             for product_read in product_obj.read(cr, uid, products_ids, ['virtual_available'], context=context):
146                 if product_read['virtual_available'] >= 0.0:
147                     continue
148
149                 product = product_obj.browse(cr, uid, [product_read['id']], context=context)[0]
150                 if product.supply_method == 'buy':
151                     location_id = warehouse.lot_input_id.id
152                 elif product.supply_method == 'produce':
153                     location_id = warehouse.lot_stock_id.id
154                 else:
155                     continue
156                 proc_id = proc_obj.create(cr, uid,
157                             self._prepare_automatic_op_procurement(cr, uid, product, warehouse, location_id, context=context),
158                             context=context)
159                 self.signal_button_confirm(cr, uid, [proc_id])
160                 self.signal_button_check(cr, uid, [proc_id])
161         return True
162
163     def _get_orderpoint_date_planned(self, cr, uid, orderpoint, start_date, context=None):
164         date_planned = start_date + \
165                        relativedelta(days=orderpoint.product_id.seller_delay or 0.0)
166         return date_planned.strftime(DEFAULT_SERVER_DATE_FORMAT)
167
168     def _prepare_orderpoint_procurement(self, cr, uid, orderpoint, product_qty, context=None):
169         return {'name': orderpoint.name,
170                 'date_planned': self._get_orderpoint_date_planned(cr, uid, orderpoint, datetime.today(), context=context),
171                 'product_id': orderpoint.product_id.id,
172                 'product_qty': product_qty,
173                 'company_id': orderpoint.company_id.id,
174                 'product_uom': orderpoint.product_uom.id,
175                 'location_id': orderpoint.location_id.id,
176                 'procure_method': 'make_to_order',
177                 'origin': orderpoint.name}
178         
179     def _product_virtual_get(self, cr, uid, order_point):
180         location_obj = self.pool.get('stock.location')
181         return location_obj._product_virtual_get(cr, uid,
182                 order_point.location_id.id, [order_point.product_id.id],
183                 {'uom': order_point.product_uom.id})[order_point.product_id.id]
184
185     def _procure_orderpoint_confirm(self, cr, uid, automatic=False,\
186             use_new_cursor=False, context=None, user_id=False):
187         '''
188         Create procurement based on Orderpoint
189         use_new_cursor: False or the dbname
190
191         @param self: The object pointer
192         @param cr: The current row, from the database cursor,
193         @param user_id: The current user ID for security checks
194         @param context: A standard dictionary for contextual values
195         @param param: False or the dbname
196         @return:  Dictionary of values
197         """
198         '''
199         if context is None:
200             context = {}
201         if use_new_cursor:
202             cr = openerp.registry(use_new_cursor).db.cursor()
203         orderpoint_obj = self.pool.get('stock.warehouse.orderpoint')
204         
205         procurement_obj = self.pool.get('procurement.order')
206         offset = 0
207         ids = [1]
208         if automatic:
209             self.create_automatic_op(cr, uid, context=context)
210         while ids:
211             ids = orderpoint_obj.search(cr, uid, [], offset=offset, limit=100)
212             for op in orderpoint_obj.browse(cr, uid, ids, context=context):
213                 prods = self._product_virtual_get(cr, uid, op)
214                 if prods is None:
215                     continue
216                 if prods < op.product_min_qty:
217                     qty = max(op.product_min_qty, op.product_max_qty)-prods
218
219                     reste = qty % op.qty_multiple
220                     if reste > 0:
221                         qty += op.qty_multiple - reste
222
223                     if qty <= 0:
224                         continue
225                     if op.product_id.type not in ('consu'):
226                         if op.procurement_draft_ids:
227                         # Check draft procurement related to this order point
228                             pro_ids = [x.id for x in op.procurement_draft_ids]
229                             procure_datas = procurement_obj.read(
230                                 cr, uid, pro_ids, ['id', 'product_qty'], context=context)
231                             to_generate = qty
232                             for proc_data in procure_datas:
233                                 if to_generate >= proc_data['product_qty']:
234                                     self.signal_button_confirm(cr, uid, [proc_data['id']])
235                                     procurement_obj.write(cr, uid, [proc_data['id']],  {'origin': op.name}, context=context)
236                                     to_generate -= proc_data['product_qty']
237                                 if not to_generate:
238                                     break
239                             qty = to_generate
240
241                     if qty:
242                         proc_id = procurement_obj.create(cr, uid,
243                                                          self._prepare_orderpoint_procurement(cr, uid, op, qty, context=context),
244                                                          context=context)
245                         self.signal_button_confirm(cr, uid, [proc_id])
246                         self.signal_button_check(cr, uid, [proc_id])
247                         orderpoint_obj.write(cr, uid, [op.id],
248                                 {'procurement_id': proc_id}, context=context)
249             offset += len(ids)
250             if use_new_cursor:
251                 cr.commit()
252         if use_new_cursor:
253             cr.commit()
254             cr.close()
255         return {}
256
257 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: