SALE,PURCHASE: fix default value when domain defined from relate action
[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                 id_set = ",".join(map(str, ids))
52                 cr.execute("SELECT s.id,COALESCE(SUM(l.price_unit*l.product_qty),0) AS amount FROM purchase_order s LEFT OUTER JOIN purchase_order_line l ON (s.id=l.order_id) WHERE s.id IN ("+id_set+") GROUP BY s.id ")
53                 res = dict(cr.fetchall())
54                 cur_obj=self.pool.get('res.currency')
55                 for id in res.keys():
56                         order=self.browse(cr, uid, [id])[0]
57                         cur=order.pricelist_id.currency_id
58                         res[id]=cur_obj.round(cr, uid, cur, res[id])
59                 return res
60
61         def _amount_tax(self, cr, uid, ids, field_name, arg, context):
62                 res = {}
63                 cur_obj=self.pool.get('res.currency')
64                 for order in self.browse(cr, uid, ids):
65                         val = 0.0
66                         cur=order.pricelist_id.currency_id
67                         for line in order.order_line:
68                                 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):
69                                         val+= cur_obj.round(cr, uid, cur, c['amount'])
70                         res[order.id]=cur_obj.round(cr, uid, cur, val)
71                 return res
72
73         def _amount_total(self, cr, uid, ids, field_name, arg, context):
74                 res = {}
75                 untax = self._amount_untaxed(cr, uid, ids, field_name, arg, context) 
76                 tax = self._amount_tax(cr, uid, ids, field_name, arg, context)
77                 cur_obj=self.pool.get('res.currency')
78                 for id in ids:
79                         order=self.browse(cr, uid, [id])[0]
80                         cur=order.pricelist_id.currency_id
81                         res[id] = cur_obj.round(cr, uid, cur, untax.get(id, 0.0) + tax.get(id, 0.0))
82                 return res
83
84         _columns = {
85                 'name': fields.char('Order Description', size=64, required=True, select=True),
86                 'origin': fields.char('Origin', size=64),
87                 'ref': fields.char('Order Reference', size=64),
88                 'partner_ref': fields.char('Partner Reference', size=64),
89                 'date_order':fields.date('Date Ordered', required=True, states={'confirmed':[('readonly',True)], 'approved':[('readonly',True)]}),
90                 'date_approve':fields.date('Date Approved'),
91                 'partner_id':fields.many2one('res.partner', 'Partner', required=True, states={'confirmed':[('readonly',True)], 'approved':[('readonly',True)]}, change_default=True),
92                 'partner_address_id':fields.many2one('res.partner.address', 'Address', required=True, states={'posted':[('readonly',True)]}),
93
94                 'dest_address_id':fields.many2one('res.partner.address', 'Destination Address', states={'posted':[('readonly',True)]}),
95                 'warehouse_id': fields.many2one('stock.warehouse', 'Warehouse', states={'posted':[('readonly',True)]}),
96                 'location_id': fields.many2one('stock.location', 'Delivery destination', required=True),
97
98                 'pricelist_id':fields.many2one('product.pricelist', 'Pricelist', required=True, states={'confirmed':[('readonly',True)], 'approved':[('readonly',True)]}),
99
100                 '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),
101                 'order_line': fields.one2many('purchase.order.line', 'order_id', 'Order State', states={'confirmed':[('readonly',True)], 'approved':[('readonly',True)]}),
102                 'validator' : fields.many2one('res.users', 'Validated by', readonly=True),
103                 'notes': fields.text('Notes'),
104                 'invoice_id': fields.many2one('account.invoice', 'Invoice', readonly=True),
105                 '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"),
106                 'shipped':fields.boolean('Received', readonly=True, select=True),
107                 'invoiced':fields.boolean('Invoiced & Paid', readonly=True, select=True),
108                 'invoice_method': fields.selection([('manual','Manual'),('order','From order'),('picking','From picking')], 'Invoicing method', required=True),
109
110                 'amount_untaxed': fields.function(_amount_untaxed, method=True, string='Untaxed Amount'),
111                 'amount_tax': fields.function(_amount_tax, method=True, string='Taxes'),
112                 'amount_total': fields.function(_amount_total, method=True, string='Total'),
113         }
114         _defaults = {
115                 'date_order': lambda *a: time.strftime('%Y-%m-%d'),
116                 'state': lambda *a: 'draft',
117                 'name': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'purchase.order'),
118                 'shipped': lambda *a: 0,
119                 'invoice_method': lambda *a: 'order',
120                 'invoiced': lambda *a: 0,
121                 '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'],
122                 '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[0],
123         }
124         _name = "purchase.order"
125         _description = "Purchase order"
126         _order = "name desc"
127
128         def button_dummy(self, cr, uid, ids, context={}):
129                 return True
130
131         def onchange_dest_address_id(self, cr, uid, ids, adr_id):
132                 if not adr_id:
133                         return {}
134                 part_id = self.pool.get('res.partner.address').read(cr, uid, [adr_id], ['partner_id'])[0]['partner_id'][0]
135                 loc_id = self.pool.get('res.partner').browse(cr, uid, part_id).property_stock_customer[0]
136                 return {'value':{'location_id': loc_id, 'warehouse_id': False}}
137
138         def onchange_warehouse_id(self, cr, uid, ids, warehouse_id):
139                 if not warehouse_id:
140                         return {}
141                 res = self.pool.get('stock.warehouse').read(cr, uid, [warehouse_id], ['lot_input_id'])[0]['lot_input_id'][0]
142                 return {'value':{'location_id': res, 'dest_address_id': False}}
143
144         def onchange_partner_id(self, cr, uid, ids, part):
145                 if not part:
146                         return {'value':{'partner_address_id': False}}
147                 addr = self.pool.get('res.partner').address_get(cr, uid, [part], ['default'])
148                 pricelist = self.pool.get('res.partner').browse(cr, uid, part).property_product_pricelist_purchase[0]
149                 return {'value':{'partner_address_id': addr['default'], 'pricelist_id': pricelist}}
150
151         def wkf_approve_order(self, cr, uid, ids):
152                 self.write(cr, uid, ids, {'state': 'approved', 'date_approve': time.strftime('%Y-%m-%d')})
153                 return True
154
155         def wkf_confirm_order(self, cr, uid, ids, context={}):
156                 for po in self.browse(cr, uid, ids):
157                         if self.pool.get('res.partner.event.type').check(cr, uid, 'purchase_open'):
158                                 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})
159                 current_name = self.name_get(cr, uid, ids)[0][1]
160                 for id in ids:
161                         self.write(cr, uid, [id], {'state' : 'confirmed', 'validator' : uid})
162                 return True
163         
164         def wkf_warn_buyer(self, cr, uid, ids):
165                 self.write(cr, uid, ids, {'state' : 'wait', 'validator' : uid})
166                 request = pooler.get_pool(cr.dbname).get('res.request')
167                 for po in self.browse(cr, uid, ids):
168                         managers = []
169                         for oline in po.order_line:
170                                 manager = oline.product_id.product_manager
171                                 if manager and not (manager.id in managers):
172                                         managers.append(manager.id)
173                         for manager_id in managers:
174                                 request.create(cr, uid, 
175                                           {'name' : "Purchase amount over the limit",
176                                            'act_from' : uid,
177                                            'act_to' : manager_id,
178                                            'body': 'Somebody has just confirmed a purchase with an amount over the defined limit',
179                                            'ref_partner_id': po.partner_id.id,
180                                            'ref_doc1': 'purchase.order,%d' % (po.id,),
181                                            })
182
183         def action_invoice_create(self, cr, uid, ids, *args):
184                 res = False
185                 for o in self.browse(cr, uid, ids):
186                         il = []
187                         for ol in o.order_line:
188
189                                 if ol.product_id:
190                                         a = ol.product_id.product_tmpl_id.property_account_expense
191                                         if not a:
192                                                 a = ol.product_id.categ_id.property_account_expense_categ
193                                         if not a:
194                                                 raise osv.except_osv('Error !', 'There is no income account defined for this product: "%s" (id:%d)' % (line.product_id.name, line.product_id.id,))
195                                         a = a[0]
196                                 else:
197                                         a = self.pool.get('ir.property').get(cr, uid, 'property_account_expense_categ', 'product.category')
198                                 il.append((0, False, {
199                                         'name': ol.name,
200                                         'account_id': a,
201                                         'price_unit': ol.price_unit or 0.0,
202                                         'quantity': ol.product_qty,
203                                         'product_id': ol.product_id.id or False,
204                                         'uos_id': ol.product_uom.id or False,
205                                         'invoice_line_tax_id': [(6, 0, [x.id for x in ol.taxes_id])],
206                                         'account_analytic_id': ol.account_analytic_id.id,
207                                 }))
208
209                         a = o.partner_id.property_account_payable[0]
210                         inv = {
211                                 'name': o.name,
212                                 'reference': "P%dPO%d" % (o.partner_id.id, o.id),
213                                 'account_id': a,
214                                 'type': 'in_invoice',
215                                 'partner_id': o.partner_id.id,
216                                 'currency_id': o.pricelist_id.currency_id.id,
217                                 'address_invoice_id': o.partner_address_id.id,
218                                 'address_contact_id': o.partner_address_id.id,
219                                 'origin': o.name,
220                                 'invoice_line': il,
221                         }
222                         inv_id = self.pool.get('account.invoice').create(cr, uid, inv, {'type':'in_invoice'})
223
224                         self.write(cr, uid, [o.id], {'invoice_id': inv_id})
225                         res = inv_id
226                 return res
227
228         def has_stockable_product(self,cr, uid, ids, *args):
229                 for order in self.browse(cr, uid, ids):
230                         for order_line in order.order_line:
231                                 if order_line.product_id and order_line.product_id.product_tmpl_id.type in ('product', 'consu'):
232                                         return True
233                 return False
234
235         def action_picking_create(self,cr, uid, ids, *args):
236                 picking_id = False
237                 for order in self.browse(cr, uid, ids):
238                         loc_id = order.partner_id.property_stock_supplier[0]
239                         istate = 'none'
240                         if order.invoice_method=='picking':
241                                 istate = '2binvoiced'
242                         picking_id = self.pool.get('stock.picking').create(cr, uid, {
243                                 'origin': order.name+((order.origin and (':'+order.origin)) or ''),
244                                 'type': 'in',
245                                 'address_id': order.dest_address_id.id or order.partner_address_id.id,
246                                 'invoice_state': istate,
247                                 'purchase_id': order.id,
248                         })
249                         for order_line in order.order_line:
250                                 if not order_line.product_id:
251                                         continue
252                                 if order_line.product_id.product_tmpl_id.type in ('product', 'consu'):
253                                         dest = order.location_id.id
254                                         self.pool.get('stock.move').create(cr, uid, {
255                                                 'name': 'PO:'+order_line.name,
256                                                 'product_id': order_line.product_id.id,
257                                                 'product_qty': order_line.product_qty,
258                                                 'product_uos_qty': order_line.product_qty,
259                                                 'product_uom': order_line.product_uom.id,
260                                                 'product_uos': order_line.product_uom.id,
261                                                 'date_planned': order_line.date_planned,
262                                                 'location_id': loc_id,
263                                                 'location_dest_id': dest,
264                                                 'picking_id': picking_id,
265                                                 'move_dest_id': order_line.move_dest_id.id,
266                                                 'state': 'assigned',
267                                                 'purchase_line_id': order_line.id,
268                                         })
269                                         if order_line.move_dest_id:
270                                                 self.pool.get('stock.move').write(cr, uid, [order_line.move_dest_id.id], {'location_id':order.location_id.id})
271                         wf_service = netsvc.LocalService("workflow")
272                         wf_service.trg_validate(uid, 'stock.picking', picking_id, 'button_confirm', cr)
273                 return picking_id
274         def copy(self, cr, uid, id, default=None,context={}):
275                 if not default:
276                         default = {}
277                 default.update({
278                         'state':'draft',
279                         'shipped':False,
280                         'invoiced':False,
281                         'invoice_id':False,
282                         'picking_ids':[],
283                         'name': self.pool.get('ir.sequence').get(cr, uid, 'purchase.order'),
284                 })
285                 return super(purchase_order, self).copy(cr, uid, id, default, context)
286
287 purchase_order()
288
289 class purchase_order_line(osv.osv):
290         def _amount_line(self, cr, uid, ids, prop, unknow_none,unknow_dict):
291                 res = {}
292                 cur_obj=self.pool.get('res.currency')
293                 for line in self.browse(cr, uid, ids):
294                         cur = line.order_id.pricelist_id.currency_id
295                         res[line.id] = cur_obj.round(cr, uid, cur, line.price_unit * line.product_qty)
296                 return res
297         
298         _columns = {
299                 'name': fields.char('Description', size=64, required=True),
300                 'product_qty': fields.float('Quantity', required=True, digits=(16,2)),
301                 'date_planned': fields.date('Date Promised', required=True),
302                 'taxes_id': fields.many2many('account.tax', 'purchase_order_taxe', 'ord_id', 'tax_id', 'Taxes'),
303                 'product_uom': fields.many2one('product.uom', 'Product UOM', required=True),
304                 'product_id': fields.many2one('product.product', 'Product', domain=[('purchase_ok','=',True)], change_default=True),
305                 'move_id': fields.many2one('stock.move', 'Reservation', ondelete='set null'),
306                 'move_dest_id': fields.many2one('stock.move', 'Reservation Destination', ondelete='set null'),
307                 'price_unit': fields.float('Unit Price', required=True, digits=(16, int(config['price_accuracy']))),
308                 'price_subtotal': fields.function(_amount_line, method=True, string='Subtotal'),
309                 'notes': fields.text('Notes'),
310                 'order_id': fields.many2one('purchase.order', 'Order Ref', select=True, required=True, ondelete='cascade'),
311                 'account_analytic_id':fields.many2one('account.analytic.account', 'Analytic Account',),
312         }
313         _defaults = {
314                 'product_qty': lambda *a: 1.0
315         }
316         _table = 'purchase_order_line'
317         _name = 'purchase.order.line'
318         _description = 'Purchase Order line'
319         def copy(self, cr, uid, id, default=None,context={}):
320                 if not default:
321                         default = {}
322                 default.update({'state':'draft', 'move_id':False})
323                 return super(purchase_order_line, self).copy(cr, uid, id, default, context)
324
325         def product_id_change(self, cr, uid, ids, pricelist, product, qty, uom, partner_id):
326                 if not pricelist:
327                         raise osv.except_osv('No Pricelist !', 'You have to select a pricelist in the sale form !\n Please set one before choosing a product.')
328                 if not product:
329                         return {'value': {'price_unit': 0.0, 'name':'','notes':''}, 'domain':{'product_uom':[]}}
330                 lang=False
331                 if partner_id:
332                         lang=self.pool.get('res.partner').read(cr, uid, [partner_id])[0]['lang']
333                 context={'lang':lang}
334
335                 prod = self.pool.get('product.product').read(cr, uid, [product], ['supplier_taxes_id','name','seller_delay','uom_po_id','description_purchase'])[0]
336                 prod_uom_po = prod['uom_po_id'][0]
337                 if not uom:
338                         uom = prod_uom_po
339                 price = self.pool.get('product.pricelist').price_get(cr,uid,[pricelist], product, qty or 1.0, partner_id, {'uom': uom})[pricelist]
340                 dt = (DateTime.now() + DateTime.RelativeDateTime(days=prod['seller_delay'] or 0.0)).strftime('%Y-%m-%d')
341                 prod_name = self.pool.get('product.product').name_get(cr, uid, [product], context=context)[0][1]
342
343                 res = {'value': {'price_unit': price, 'name':prod_name, 'taxes_id':prod['supplier_taxes_id'], 'date_planned': dt,'notes':prod['description_purchase'], 'product_uom': uom}}
344                 domain = {}
345
346                 res2 = self.pool.get('product.uom').read(cr, uid, [uom], ['category_id'])
347                 res3 = self.pool.get('product.uom').read(cr, uid, [prod_uom_po], ['category_id'])
348                 domain = {'product_uom':[('category_id','=',res2[0]['category_id'][0])]}
349                 if res2[0]['category_id'] != res3[0]['category_id']:
350                         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')
351
352                 res['domain'] = domain
353                 return res
354
355         def product_uom_change(self, cr, uid, ids, pricelist, product, qty, uom, partner_id):
356                 res = self.product_id_change(cr, uid, ids, pricelist, product, qty, uom, partner_id)
357                 if 'product_uom' in res['value']:
358                         del res['value']['product_uom']
359                 if not uom:
360                         res['value']['price_unit'] = 0.0
361                 return res
362 purchase_order_line()