Bugfix, BUG #714
[odoo/odoo.git] / addons / purchase / purchase.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 # WARNING: This program as such is intended to be used by professional
6 # programmers who take the whole responsability of assessing all potential
7 # consequences resulting from its eventual inadequacies and bugs
8 # End users who are looking for a ready-to-use solution with commercial
9 # garantees and support are strongly adviced to contract a Free Software
10 # Service Company
11 #
12 # This program is Free Software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 2
15 # of the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25 #
26 ##############################################################################
27
28 from osv import fields
29 from osv import osv
30 import time
31 import netsvc
32
33 import ir
34 from mx import DateTime
35 import pooler
36 from tools import config
37
38 #
39 # Model definition
40 #
41 class purchase_order(osv.osv):
42         def _calc_amount(self, cr, uid, ids, prop, unknow_none, unknow_dict):
43                 res = {}
44                 for order in self.browse(cr, uid, ids):
45                         res[order.id] = 0
46                         for oline in order.order_line:
47                                 res[order.id] += oline.price_unit * oline.product_qty
48                 return res
49
50         def _amount_untaxed(self, cr, uid, ids, field_name, arg, context):
51                 res = {}
52                 cur_obj=self.pool.get('res.currency')
53                 for purchase in self.browse(cr, uid, ids):
54                         res[purchase.id] = 0.0
55                         for line in purchase.order_line:
56                                 res[purchase.id] += line.price_subtotal
57                         cur = purchase.pricelist_id.currency_id
58                         res[purchase.id] = cur_obj.round(cr, uid, cur, res[purchase.id])
59
60                 return res
61
62         def _amount_tax(self, cr, uid, ids, field_name, arg, context):
63                 res = {}
64                 cur_obj=self.pool.get('res.currency')
65                 for order in self.browse(cr, uid, ids):
66                         val = 0.0
67                         cur=order.pricelist_id.currency_id
68                         for line in order.order_line:
69                                 for c in self.pool.get('account.tax').compute(cr, uid, line.taxes_id, line.price_unit, line.product_qty, order.partner_address_id.id, line.product_id, order.partner_id):
70                                         val+= cur_obj.round(cr, uid, cur, c['amount'])
71                         res[order.id]=cur_obj.round(cr, uid, cur, val)
72                 return res
73
74         def _amount_total(self, cr, uid, ids, field_name, arg, context):
75                 res = {}
76                 untax = self._amount_untaxed(cr, uid, ids, field_name, arg, context) 
77                 tax = self._amount_tax(cr, uid, ids, field_name, arg, context)
78                 cur_obj=self.pool.get('res.currency')
79                 for id in ids:
80                         order=self.browse(cr, uid, [id])[0]
81                         cur=order.pricelist_id.currency_id
82                         res[id] = cur_obj.round(cr, uid, cur, untax.get(id, 0.0) + tax.get(id, 0.0))
83                 return res
84
85         _columns = {
86                 'name': fields.char('Order Description', size=64, required=True, select=True),
87                 'origin': fields.char('Origin', size=64),
88                 'ref': fields.char('Order Reference', size=64),
89                 'partner_ref': fields.char('Partner Ref.', size=64),
90                 'date_order':fields.date('Date Ordered', required=True, states={'confirmed':[('readonly',True)], 'approved':[('readonly',True)]}),
91                 'date_approve':fields.date('Date Approved'),
92                 'partner_id':fields.many2one('res.partner', 'Partner', required=True, states={'confirmed':[('readonly',True)], 'approved':[('readonly',True)]}, change_default=True),
93                 'partner_address_id':fields.many2one('res.partner.address', 'Address', required=True, states={'posted':[('readonly',True)]}),
94
95                 'dest_address_id':fields.many2one('res.partner.address', 'Destination Address', states={'posted':[('readonly',True)]}),
96                 'warehouse_id': fields.many2one('stock.warehouse', 'Warehouse', states={'posted':[('readonly',True)]}),
97                 'location_id': fields.many2one('stock.location', 'Delivery destination', required=True),
98
99                 'pricelist_id':fields.many2one('product.pricelist', 'Pricelist', required=True, states={'confirmed':[('readonly',True)], 'approved':[('readonly',True)]}),
100
101                 'state': fields.selection([('draft', 'Request for Quotation'), ('wait', 'Waiting'), ('confirmed', 'Confirmed'), ('approved', 'Approved'),('except_picking', 'Shipping Exception'), ('except_invoice', 'Invoice Exception'), ('done', 'Done'), ('cancel', 'Cancelled')], 'Order State', readonly=True, help="The state of the purchase order or the quotation request. A quotation is a purchase order in a 'Draft' state. Then the order has to be confirmed by the user, the state switch to 'Confirmed'. Then the supplier must confirm the order to change the state to 'Approved'. When the purchase order is paid and received, the state becomes 'Done'. If a cancel action occurs in the invoice or in the reception of goods, the state becomes in exception.", select=True),
102                 'order_line': fields.one2many('purchase.order.line', 'order_id', 'Order State', states={'confirmed':[('readonly',True)], 'approved':[('readonly',True)]}),
103                 'validator' : fields.many2one('res.users', 'Validated by', readonly=True),
104                 'notes': fields.text('Notes'),
105                 'invoice_id': fields.many2one('account.invoice', 'Invoice', readonly=True),
106                 'picking_ids': fields.one2many('stock.picking', 'purchase_id', 'Picking List', readonly=True, help="This is the list of picking list that have been generated for this purchase"),
107                 'shipped':fields.boolean('Received', readonly=True, select=True),
108                 'invoiced':fields.boolean('Invoiced & Paid', readonly=True, select=True),
109                 'invoice_method': fields.selection([('manual','Manual'),('order','From order'),('picking','From picking')], 'Invoicing Control', required=True),
110
111                 'amount_untaxed': fields.function(_amount_untaxed, method=True, string='Untaxed Amount'),
112                 'amount_tax': fields.function(_amount_tax, method=True, string='Taxes'),
113                 'amount_total': fields.function(_amount_total, method=True, string='Total'),
114         }
115         _defaults = {
116                 'date_order': lambda *a: time.strftime('%Y-%m-%d'),
117                 'state': lambda *a: 'draft',
118                 'name': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'purchase.order'),
119                 'shipped': lambda *a: 0,
120                 'invoice_method': lambda *a: 'order',
121                 'invoiced': lambda *a: 0,
122                 'partner_address_id': lambda self, cr, uid, context: context.get('partner_id', False) and self.pool.get('res.partner').address_get(cr, uid, [context['partner_id']], ['default'])['default'],
123                 'pricelist_id': lambda self, cr, uid, context: context.get('partner_id', False) and self.pool.get('res.partner').browse(cr, uid, context['partner_id']).property_product_pricelist_purchase.id,
124         }
125         _name = "purchase.order"
126         _description = "Purchase order"
127         _order = "name desc"
128
129         def button_dummy(self, cr, uid, ids, context={}):
130                 return True
131
132         def onchange_dest_address_id(self, cr, uid, ids, adr_id):
133                 if not adr_id:
134                         return {}
135                 part_id = self.pool.get('res.partner.address').read(cr, uid, [adr_id], ['partner_id'])[0]['partner_id'][0]
136                 loc_id = self.pool.get('res.partner').browse(cr, uid, part_id).property_stock_customer.id
137                 return {'value':{'location_id': loc_id, 'warehouse_id': False}}
138
139         def onchange_warehouse_id(self, cr, uid, ids, warehouse_id):
140                 if not warehouse_id:
141                         return {}
142                 res = self.pool.get('stock.warehouse').read(cr, uid, [warehouse_id], ['lot_input_id'])[0]['lot_input_id'][0]
143                 return {'value':{'location_id': res, 'dest_address_id': False}}
144
145         def onchange_partner_id(self, cr, uid, ids, part):
146                 if not part:
147                         return {'value':{'partner_address_id': False}}
148                 addr = self.pool.get('res.partner').address_get(cr, uid, [part], ['default'])
149                 pricelist = self.pool.get('res.partner').browse(cr, uid, part).property_product_pricelist_purchase.id
150                 return {'value':{'partner_address_id': addr['default'], 'pricelist_id': pricelist}}
151
152         def wkf_approve_order(self, cr, uid, ids):
153                 self.write(cr, uid, ids, {'state': 'approved', 'date_approve': time.strftime('%Y-%m-%d')})
154                 return True
155
156         def wkf_confirm_order(self, cr, uid, ids, context={}):
157                 for po in self.browse(cr, uid, ids):
158                         if self.pool.get('res.partner.event.type').check(cr, uid, 'purchase_open'):
159                                 self.pool.get('res.partner.event').create(cr, uid, {'name':'Purchase Order: '+po.name, 'partner_id':po.partner_id.id, 'date':time.strftime('%Y-%m-%d %H:%M:%S'), 'user_id':uid, 'partner_type':'retailer', 'probability': 1.0, 'planned_cost':po.amount_untaxed})
160                 current_name = self.name_get(cr, uid, ids)[0][1]
161                 for id in ids:
162                         self.write(cr, uid, [id], {'state' : 'confirmed', 'validator' : uid})
163                 return True
164         
165         def wkf_warn_buyer(self, cr, uid, ids):
166                 self.write(cr, uid, ids, {'state' : 'wait', 'validator' : uid})
167                 request = pooler.get_pool(cr.dbname).get('res.request')
168                 for po in self.browse(cr, uid, ids):
169                         managers = []
170                         for oline in po.order_line:
171                                 manager = oline.product_id.product_manager
172                                 if manager and not (manager.id in managers):
173                                         managers.append(manager.id)
174                         for manager_id in managers:
175                                 request.create(cr, uid, 
176                                           {'name' : "Purchase amount over the limit",
177                                            'act_from' : uid,
178                                            'act_to' : manager_id,
179                                            'body': 'Somebody has just confirmed a purchase with an amount over the defined limit',
180                                            'ref_partner_id': po.partner_id.id,
181                                            'ref_doc1': 'purchase.order,%d' % (po.id,),
182                                            })
183         def inv_line_create(self,a,ol):
184                 return (0, False, {
185                                         'name': ol.name,
186                                         'account_id': a,
187                                         'price_unit': ol.price_unit or 0.0,
188                                         'quantity': ol.product_qty,
189                                         'product_id': ol.product_id.id or False,
190                                         'uos_id': ol.product_uom.id or False,
191                                         'invoice_line_tax_id': [(6, 0, [x.id for x in ol.taxes_id])],
192                                         'account_analytic_id': ol.account_analytic_id.id,
193                                 })
194
195         def action_invoice_create(self, cr, uid, ids, *args):
196                 res = False
197                 for o in self.browse(cr, uid, ids):
198                         il = []
199                         for ol in o.order_line:
200
201                                 if ol.product_id:
202                                         a = ol.product_id.product_tmpl_id.property_account_expense.id
203                                         if not a:
204                                                 a = ol.product_id.categ_id.property_account_expense_categ.id
205                                         if not a:
206                                                 raise osv.except_osv('Error !', 'There is no expense account defined for this product: "%s" (id:%d)' % (ol.product_id.name, ol.product_id.id,))
207                                 else:
208                                         a = self.pool.get('ir.property').get(cr, uid, 'property_account_expense_categ', 'product.category')
209                                 il.append(self.inv_line_create(a,ol))
210 #                               il.append((0, False, {
211 #                                       'name': ol.name,
212 #                                       'account_id': a,
213 #                                       'price_unit': ol.price_unit or 0.0,
214 #                                       'quantity': ol.product_qty,
215 #                                       'product_id': ol.product_id.id or False,
216 #                                       'uos_id': ol.product_uom.id or False,
217 #                                       'invoice_line_tax_id': [(6, 0, [x.id for x in ol.taxes_id])],
218 #                                       'account_analytic_id': ol.account_analytic_id.id,
219 #                               }))
220
221                         a = o.partner_id.property_account_payable.id
222                         inv = {
223                                 'name': o.partner_ref or o.name,
224                                 'reference': "P%dPO%d" % (o.partner_id.id, o.id),
225                                 'account_id': a,
226                                 'type': 'in_invoice',
227                                 'partner_id': o.partner_id.id,
228                                 'currency_id': o.pricelist_id.currency_id.id,
229                                 'address_invoice_id': o.partner_address_id.id,
230                                 'address_contact_id': o.partner_address_id.id,
231                                 'origin': o.name,
232                                 'invoice_line': il,
233                         }
234                         inv_id = self.pool.get('account.invoice').create(cr, uid, inv, {'type':'in_invoice'})
235                         self.pool.get('account.invoice').button_compute(cr, uid, [inv_id], {'type':'in_invoice'}, set_total=True)
236
237                         self.write(cr, uid, [o.id], {'invoice_id': inv_id})
238                         res = inv_id
239                 return res
240
241         def has_stockable_product(self,cr, uid, ids, *args):
242                 for order in self.browse(cr, uid, ids):
243                         for order_line in order.order_line:
244                                 if order_line.product_id and order_line.product_id.product_tmpl_id.type in ('product', 'consu'):
245                                         return True
246                 return False
247
248         def action_picking_create(self,cr, uid, ids, *args):
249                 picking_id = False
250                 for order in self.browse(cr, uid, ids):
251                         loc_id = order.partner_id.property_stock_supplier.id
252                         istate = 'none'
253                         if order.invoice_method=='picking':
254                                 istate = '2binvoiced'
255                         picking_id = self.pool.get('stock.picking').create(cr, uid, {
256                                 'origin': order.name+((order.origin and (':'+order.origin)) or ''),
257                                 'type': 'in',
258                                 'address_id': order.dest_address_id.id or order.partner_address_id.id,
259                                 'invoice_state': istate,
260                                 'purchase_id': order.id,
261                         })
262                         for order_line in order.order_line:
263                                 if not order_line.product_id:
264                                         continue
265                                 if order_line.product_id.product_tmpl_id.type in ('product', 'consu'):
266                                         dest = order.location_id.id
267                                         self.pool.get('stock.move').create(cr, uid, {
268                                                 'name': 'PO:'+order_line.name,
269                                                 'product_id': order_line.product_id.id,
270                                                 'product_qty': order_line.product_qty,
271                                                 'product_uos_qty': order_line.product_qty,
272                                                 'product_uom': order_line.product_uom.id,
273                                                 'product_uos': order_line.product_uom.id,
274                                                 'date_planned': order_line.date_planned,
275                                                 'location_id': loc_id,
276                                                 'location_dest_id': dest,
277                                                 'picking_id': picking_id,
278                                                 'move_dest_id': order_line.move_dest_id.id,
279                                                 'state': 'assigned',
280                                                 'purchase_line_id': order_line.id,
281                                         })
282                                         if order_line.move_dest_id:
283                                                 self.pool.get('stock.move').write(cr, uid, [order_line.move_dest_id.id], {'location_id':order.location_id.id})
284                         wf_service = netsvc.LocalService("workflow")
285                         wf_service.trg_validate(uid, 'stock.picking', picking_id, 'button_confirm', cr)
286                 return picking_id
287         def copy(self, cr, uid, id, default=None,context={}):
288                 if not default:
289                         default = {}
290                 default.update({
291                         'state':'draft',
292                         'shipped':False,
293                         'invoiced':False,
294                         'invoice_id':False,
295                         'picking_ids':[],
296                         'name': self.pool.get('ir.sequence').get(cr, uid, 'purchase.order'),
297                 })
298                 return super(purchase_order, self).copy(cr, uid, id, default, context)
299
300 purchase_order()
301
302 class purchase_order_line(osv.osv):
303         def _amount_line(self, cr, uid, ids, prop, unknow_none,unknow_dict):
304                 res = {}
305                 cur_obj=self.pool.get('res.currency')
306                 for line in self.browse(cr, uid, ids):
307                         cur = line.order_id.pricelist_id.currency_id
308                         res[line.id] = cur_obj.round(cr, uid, cur, line.price_unit * line.product_qty)
309                 return res
310         
311         _columns = {
312                 'name': fields.char('Description', size=64, required=True),
313                 'product_qty': fields.float('Quantity', required=True, digits=(16,2)),
314                 'date_planned': fields.date('Scheduled date', required=True),
315                 'taxes_id': fields.many2many('account.tax', 'purchase_order_taxe', 'ord_id', 'tax_id', 'Taxes'),
316                 'product_uom': fields.many2one('product.uom', 'Product UOM', required=True),
317                 'product_id': fields.many2one('product.product', 'Product', domain=[('purchase_ok','=',True)], change_default=True),
318                 'move_id': fields.many2one('stock.move', 'Reservation', ondelete='set null'),
319                 'move_dest_id': fields.many2one('stock.move', 'Reservation Destination', ondelete='set null'),
320                 'price_unit': fields.float('Unit Price', required=True, digits=(16, int(config['price_accuracy']))),
321                 'price_subtotal': fields.function(_amount_line, method=True, string='Subtotal'),
322                 'notes': fields.text('Notes'),
323                 'order_id': fields.many2one('purchase.order', 'Order Ref', select=True, required=True, ondelete='cascade'),
324                 'account_analytic_id':fields.many2one('account.analytic.account', 'Analytic Account',),
325         }
326         _defaults = {
327                 'product_qty': lambda *a: 1.0
328         }
329         _table = 'purchase_order_line'
330         _name = 'purchase.order.line'
331         _description = 'Purchase Order line'
332         def copy(self, cr, uid, id, default=None,context={}):
333                 if not default:
334                         default = {}
335                 default.update({'state':'draft', 'move_id':False})
336                 return super(purchase_order_line, self).copy(cr, uid, id, default, context)
337
338         def product_id_change(self, cr, uid, ids, pricelist, product, qty, uom,
339                         partner_id, date_order=False):
340                 if not pricelist:
341                         raise osv.except_osv('No Pricelist !', 'You have to select a pricelist in the purchase form !\n Please set one before choosing a product.')
342                 if not product:
343                         return {'value': {'price_unit': 0.0, 'name':'','notes':''}, 'domain':{'product_uom':[]}}
344                 lang=False
345                 if partner_id:
346                         lang=self.pool.get('res.partner').read(cr, uid, [partner_id])[0]['lang']
347                 context={'lang':lang}
348
349                 prod = self.pool.get('product.product').read(cr, uid, [product], ['supplier_taxes_id','name','seller_delay','uom_po_id','description_purchase'])[0]
350                 prod_uom_po = prod['uom_po_id'][0]
351                 if not uom:
352                         uom = prod_uom_po
353                 if not date_order:
354                         date_order = time.strftime('%Y-%m-%d')
355                 price = self.pool.get('product.pricelist').price_get(cr,uid,[pricelist],
356                                 product, qty or 1.0, partner_id, {
357                                         'uom': uom,
358                                         'date': date_order,
359                                         })[pricelist]
360                 dt = (DateTime.now() + DateTime.RelativeDateTime(days=prod['seller_delay'] or 0.0)).strftime('%Y-%m-%d')
361                 prod_name = self.pool.get('product.product').name_get(cr, uid, [product], context=context)[0][1]
362
363                 res = {'value': {'price_unit': price, 'name':prod_name, 'taxes_id':prod['supplier_taxes_id'], 'date_planned': dt,'notes':prod['description_purchase'], 'product_uom': uom}}
364                 domain = {}
365
366                 res2 = self.pool.get('product.uom').read(cr, uid, [uom], ['category_id'])
367                 res3 = self.pool.get('product.uom').read(cr, uid, [prod_uom_po], ['category_id'])
368                 domain = {'product_uom':[('category_id','=',res2[0]['category_id'][0])]}
369                 if res2[0]['category_id'] != res3[0]['category_id']:
370                         raise osv.except_osv('Wrong Product UOM !', 'You have to select a product UOM in the same category than the purchase UOM of the product')
371
372                 res['domain'] = domain
373                 return res
374
375         def product_uom_change(self, cr, uid, ids, pricelist, product, qty, uom,
376                         partner_id, date_order=False):
377                 res = self.product_id_change(cr, uid, ids, pricelist, product, qty, uom,
378                                 partner_id, date_order=date_order)
379                 if 'product_uom' in res['value']:
380                         del res['value']['product_uom']
381                 if not uom:
382                         res['value']['price_unit'] = 0.0
383                 return res
384 purchase_order_line()