[MERGE] merge with latest stable
[odoo/odoo.git] / addons / purchase / purchase.py
index 773bb07..0767e20 100644 (file)
@@ -348,7 +348,8 @@ class purchase_order(osv.osv):
 
     def action_invoice_create(self, cr, uid, ids, *args):
         res = False
-
+        property_obj = self.pool.get('ir.property')
+        fp_obj =  self.pool.get('account.fiscal.position')
         journal_obj = self.pool.get('account.journal')
         for o in self.browse(cr, uid, ids):
             il = []
@@ -356,22 +357,22 @@ class purchase_order(osv.osv):
             for ol in o.order_line:
                 todo.append(ol.id)
                 if ol.product_id:
-                    a = ol.product_id.product_tmpl_id.property_account_expense.id
-                    if not a:
-                        a = ol.product_id.categ_id.property_account_expense_categ.id
-                    if not a:
+                    acc_id = ol.product_id.product_tmpl_id.property_account_expense.id
+                    if not acc_id:
+                        acc_id = ol.product_id.categ_id.property_account_expense_categ.id
+                    if not acc_id:
                         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,))
                 else:
-                    acc_id = self.pool.get('ir.property').get(cr, uid, 'property_account_expense_categ', 'product.category')
-                    a = acc_id and acc_id.id or False
+                    prop = property_obj.get(cr, uid, 'property_account_expense_categ', 'product.category')
+                    acc_id = prop and prop.id or False
                 fpos = o.fiscal_position or False
-                a = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, a)
-                if not a:
+                acc_id = fp_obj.map_account(cr, uid, fpos, acc_id)
+                if not acc_id:
                     raise osv.except_osv(_('Error !'),
                         _('There is no expense account defined in default Properties for Product Category or Fiscal Position is not defined !'))
-                il.append(self.inv_line_create(cr, uid, a, ol))
+                il.append(self.inv_line_create(cr, uid, acc_id, ol))
 
-            a = o.partner_id.property_account_payable.id
+            acc_id = o.partner_id.property_account_payable.id
             journal_ids = journal_obj.search(cr, uid, [('type', '=','purchase'),('company_id', '=', o.company_id.id)], limit=1)
             if not journal_ids:
                 raise osv.except_osv(_('Error !'),
@@ -379,7 +380,7 @@ class purchase_order(osv.osv):
             inv = {
                 'name': o.partner_ref or o.name,
                 'reference': o.partner_ref or o.name,
-                'account_id': a,
+                'account_id': acc_id,
                 'type': 'in_invoice',
                 'partner_id': o.partner_id.id,
                 'currency_id': o.pricelist_id.currency_id.id,
@@ -392,7 +393,7 @@ class purchase_order(osv.osv):
                 'payment_term': o.partner_id.property_payment_term and o.partner_id.property_payment_term.id or False,
                 'company_id': o.company_id.id,
             }
-            inv_id = self.pool.get('account.invoice').create(cr, uid, inv, {'type':'in_invoice'})
+            inv_id = self.pool.get('account.invoice').create(cr, uid, inv, {'type':'in_invoice', 'journal_type': 'purchase'})
             self.pool.get('account.invoice').button_compute(cr, uid, [inv_id], {'type':'in_invoice'}, set_total=True)
             self.pool.get('purchase.order.line').write(cr, uid, todo, {'invoiced':True})
             self.write(cr, uid, [o.id], {'invoice_ids': [(4, inv_id)]})
@@ -433,6 +434,16 @@ class purchase_order(osv.osv):
     def action_picking_create(self,cr, uid, ids, *args):
         picking_id = False
         for order in self.browse(cr, uid, ids):
+            reception_address_id = False
+            if order.dest_address_id:
+                reception_address_id = order.dest_address_id.id
+            elif order.warehouse_id and order.warehouse_id.partner_address_id:
+                reception_address_id = order.warehouse_id.partner_address_id.id
+            else:
+                if order.company_id.partner_id.address:
+                    addresses_default = [address.id for address in order.company_id.partner_id.address if address.type == 'default']
+                    addresses_delivery = [address.id for address in order.company_id.partner_id.address if address.type == 'delivery']
+                    reception_address_id = (addresses_delivery and addresses_delivery[0]) or (addresses_default and addresses_default[0]) or False
             loc_id = order.partner_id.property_stock_supplier.id
             istate = 'none'
             if order.invoice_method=='picking':
@@ -442,7 +453,7 @@ class purchase_order(osv.osv):
                 'name': pick_name,
                 'origin': order.name+((order.origin and (':'+order.origin)) or ''),
                 'type': 'in',
-                'address_id': order.dest_address_id.id or order.partner_address_id.id,
+                'address_id': reception_address_id,
                 'invoice_state': istate,
                 'purchase_id': order.id,
                 'company_id': order.company_id.id,
@@ -655,7 +666,7 @@ class purchase_order_line(osv.osv):
     def copy_data(self, cr, uid, id, default=None, context=None):
         if not default:
             default = {}
-        default.update({'state':'draft', 'move_ids':[],'invoiced':0,'invoice_lines':[]})
+        default.update({'state':'draft', 'move_ids':[], 'move_dest_id':False, 'invoiced':0,'invoice_lines':[]})
         return super(purchase_order_line, self).copy_data(cr, uid, id, default, context)
 
     def product_id_change(self, cr, uid, ids, pricelist, product, qty, uom,
@@ -675,9 +686,8 @@ class purchase_order_line(osv.osv):
         lang=False
         if partner_id:
             lang=self.pool.get('res.partner').read(cr, uid, partner_id, ['lang'])['lang']
-        context={'lang':lang}
-        context['partner_id'] = partner_id
-
+        context = self.pool.get('res.users').context_get(cr, uid)
+        context_partner = {'lang':lang, 'partner_id': partner_id}
         prod = self.pool.get('product.product').browse(cr, uid, product, context=context)
         prod_uom_po = prod.uom_po_id.id
         if not uom:
@@ -687,7 +697,7 @@ class purchase_order_line(osv.osv):
         qty = qty or 1.0
         seller_delay = 0
 
-        prod_name = self.pool.get('product.product').name_get(cr, uid, [prod.id], context=context)[0][1]
+        prod_name = self.pool.get('product.product').name_get(cr, uid, [prod.id], context=context_partner)[0][1]
         res = {}
         for s in prod.seller_ids:
             if s.name.id == partner_id:
@@ -705,6 +715,14 @@ class purchase_order_line(osv.osv):
                         'uom': uom,
                         'date': date_order,
                         })[pricelist]
+        if price is False:
+            warning = {
+                'title': 'No valid pricelist line found !',
+                'message':
+                    "Couldn't find a pricelist line matching this product and quantity.\n"
+                    "You have to change either the product, the quantity or the pricelist."
+                }
+            res.update({'warning': warning})
         dt = (datetime.now() + relativedelta(days=int(seller_delay) or 0.0)).strftime('%Y-%m-%d %H:%M:%S')
 
 
@@ -777,6 +795,7 @@ class procurement_order(osv.osv):
         prod_obj = self.pool.get('product.product')
         acc_pos_obj = self.pool.get('account.fiscal.position')
         po_obj = self.pool.get('purchase.order')
+        po_line_obj = self.pool.get('purchase.order.line')
         for procurement in self.browse(cr, uid, ids, context=context):
             res_id = procurement.move_id.id
             partner = procurement.product_id.seller_id # Taken Main Supplier of Product of Procurement.
@@ -785,6 +804,7 @@ class procurement_order(osv.osv):
             partner_id = partner.id
             address_id = partner_obj.address_get(cr, uid, [partner_id], ['delivery'])['delivery']
             pricelist_id = partner.property_product_pricelist_purchase.id
+            fiscal_position = partner.property_account_position and partner.property_account_position.id or False
 
             uom_id = procurement.product_id.uom_po_id.id
 
@@ -797,6 +817,10 @@ class procurement_order(osv.osv):
             newdate = datetime.strptime(procurement.date_planned, '%Y-%m-%d %H:%M:%S')
             newdate = (newdate - relativedelta(days=company.po_lead)) - relativedelta(days=seller_delay)
 
+            res_onchange = po_line_obj.product_id_change(cr, uid, ids, pricelist_id, procurement.product_id.id, qty, uom_id,
+                partner_id, time.strftime('%Y-%m-%d'), fiscal_position=fiscal_position, date_planned=datetime.now() + relativedelta(days=seller_delay or 0.0),
+            name=procurement.name, price_unit=procurement.product_id.list_price, notes=procurement.product_id.description_purchase)
+
             #Passing partner_id to context for purchase order line integrity of Line name
             context.update({'lang': partner.lang, 'partner_id': partner_id})
 
@@ -804,10 +828,10 @@ class procurement_order(osv.osv):
 
             line = {
                 'name': product.partner_ref,
-                'product_qty': qty,
+                'product_qty': res_onchange['value']['product_qty'],
                 'product_id': procurement.product_id.id,
-                'product_uom': uom_id,
-                'price_unit': price,
+                'product_uom': res_onchange['value']['product_uom'],
+                'price_unit': res_onchange['value']['price_unit'],
                 'date_planned': newdate.strftime('%Y-%m-%d %H:%M:%S'),
                 'move_dest_id': res_id,
                 'notes': product.description_purchase,