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 ##############################################################################
24 from datetime import datetime
25 from dateutil.relativedelta import relativedelta
27 from openerp.osv import fields, osv
28 import openerp.addons.decimal_precision as dp
29 from openerp.tools.translate import _
32 class procurement_group(osv.osv):
34 The procurement requirement class is used to group products together
35 when computing procurements. (tasks, physical products, ...)
37 The goal is that when you have one sale order of several products
38 and the products are pulled from the same or several location(s), to keep
39 having the moves grouped into pickings that represent the sale order.
41 Used in: sales order (to group delivery order lines like the so), pull/push
42 rules (to pack like the delivery order), on orderpoints (e.g. for wave picking
43 all the similar products together).
45 Grouping is made only if the source and the destination is the same.
46 Suppose you have 4 lines on a picking from Output where 2 lines will need
47 to come from Input (crossdock) and 2 lines coming from Stock -> Output As
48 the four procurement orders will have the same group ids from the SO, the
49 move from input will have a stock.picking with 2 grouped lines and the move
50 from stock will have 2 grouped lines also.
52 The name is usually the name of the original document (sale order) or a
53 sequence computed if created manually.
55 _name = 'procurement.group'
56 _description = 'Procurement Requisition'
59 'name': fields.char('Reference'),
62 'name': lambda self,cr,uid,c: self.pool.get('ir.sequence').get(cr,uid,'procurement.group') or ''
65 class procurement_rule(osv.osv):
67 A rule describe what a procurement should do; produce, buy, move, ...
69 _name = 'procurement.rule'
70 _description = "Procurement Rule"
72 def _get_action(self, cr, uid, context=None):
73 return [('move', 'Move')]
76 'name': fields.char('Name', required=True,
77 help="This field will fill the packing origin and the name of its moves"),
78 'group_id': fields.many2one('procurement.group', 'Procurement Group'),
79 'action': fields.selection(selection=lambda s, cr, uid, context=None: s._get_action(cr, uid, context=context),
80 string='Action', required=True)
84 class procurement_order(osv.osv):
88 _name = "procurement.order"
89 _description = "Procurement"
90 _order = 'priority desc,date_planned'
91 _inherit = ['mail.thread']
94 'name': fields.text('Description', required=True),
96 'origin': fields.char('Source Document', size=64,
97 help="Reference of the document that created this Procurement.\n"
98 "This is automatically completed by OpenERP."),
99 'company_id': fields.many2one('res.company','Company',required=True),
101 # These two fields are used for shceduling
102 'priority': fields.selection([('0','Not urgent'),('1','Normal'),('2','Urgent'),('3','Very Urgent')], 'Priority', required=True, select=True),
103 'date_planned': fields.datetime('Scheduled date', required=True, select=True),
105 'group_id':fields.many2one('procurement.group', 'Procurement Requisition'),
106 'rule_id' :fields.many2one('procurement.rule', 'Rule'),
108 'product_id': fields.many2one('product.product', 'Product', required=True, states={'draft':[('readonly',False)]}, readonly=True),
109 'product_qty': fields.float('Quantity', digits_compute=dp.get_precision('Product Unit of Measure'), required=True, states={'draft':[('readonly',False)]}, readonly=True),
110 'product_uom': fields.many2one('product.uom', 'Product Unit of Measure', required=True, states={'draft':[('readonly',False)]}, readonly=True),
112 'product_uos_qty': fields.float('UoS Quantity', states={'draft':[('readonly',False)]}, readonly=True),
113 'product_uos': fields.many2one('product.uom', 'Product UoS', states={'draft':[('readonly',False)]}, readonly=True),
115 'procure_method': fields.selection([('make_to_stock','Make to Stock'),('make_to_order','Make to Order')],
116 'Procurement Method', states={'draft':[('readonly',False)], 'confirmed':[('readonly',False)]},
117 readonly=True, required=True, help="If you encode manually a Procurement, you probably want to use" \
118 " a make to order method."),
120 'state': fields.selection([
121 ('cancel','Cancelled'),
122 ('confirmed','Confirmed'),
123 ('exception','Exception'),
124 ('running','Running'),
126 ], 'Status', required=True, track_visibility='onchange'),
130 'state': 'confirmed',
132 'date_planned': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
133 'procure_method': 'make_to_order',
134 'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'procurement.order', context=c)
136 def onchange_product_id(self, cr, uid, ids, product_id, context=None):
137 """ Finds UoM and UoS of changed product.
138 @param product_id: Changed id of product.
139 @return: Dictionary of values.
142 w = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
144 'product_uom': w.uom_id.id,
145 'product_uos': w.uos_id and w.uos_id.id or w.uom_id.id
150 def run(self, cr, uid, ids, context=None):
151 for procurement in self.browse(cr, uid, ids, context=context or {}):
152 if procurement.procure_method=='make_to_order':
153 rule = self._assign(cr, uid, procurement, context=context)
155 self.write(cr, uid, [procurement.id], {'rule_id', rule.id}, context=context)
156 procurement.refresh()
157 self._run(cr, uid, procurement, context=context or {})
160 self.message_post(cr, uid, [procurement.id], body=_('No rule matching this procurement'), context=context)
165 self.write(cr, uid, [procurement.id], {'state': 'running'}, context=context)
167 self.write(cr, uid, [procurement.id], {'state': 'exception'}, context=context)
170 def check(self, cr, uid, ids, context=None):
172 for procurement in self.browse(cr, uid, ids, context=context or {}):
173 result = self._check(cr, uid, procurement.id, context=context or {})
175 self.write(cr, uid, [procurement.id], {'state': 'done'}, context=context)
176 done.append(procurement.id)
180 # Method to overwrite in different procurement modules
182 def _assign(self, cr, uid, procurement, context=None):
185 def _run(self, cr, uid, procurement, context=None):
188 def _check(self, cr, uid, procurement, context=None):
194 def run_scheduler(self, cr, uid, use_new_cursor=False, context=None):
196 Call the scheduler to check the procurement order
198 @param self: The object pointer
199 @param cr: The current row, from the database cursor,
200 @param uid: The current user ID for security checks
201 @param ids: List of selected IDs
202 @param use_new_cursor: False or the dbname
203 @param context: A standard dictionary for contextual values
204 @return: Dictionary of values
210 cr = openerp.registry(use_new_cursor).db.cursor()
212 company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
213 maxdate = (datetime.today() + relativedelta(days=company.schedule_range)).strftime('%Y-%m-%d %H:%M:%S')
215 # Run confirmed procurements
217 ids = self.search(cr, uid, [('state', '=', 'confirmed'),('date_planned','<=', maxdate)], context=context)
219 self.run(cr, uid, ids, context=context)
223 # Check if running procurements are done
226 ids = self.search(cr, uid, [('state', '=', 'running'),('date_planned','<=', maxdate)], offset=offset, context=context)
228 done = self.check(cr, uid, ids, context=context)
229 offset += len(ids) - len(done)
230 if use_new_cursor and len(done):