bugfix
[odoo/odoo.git] / addons / purchase / purchase.py
index 35a1363..170bf2d 100644 (file)
@@ -227,7 +227,8 @@ class purchase_order(osv.osv):
         if not part:
             return {'value':{'partner_address_id': False}}
         addr = self.pool.get('res.partner').address_get(cr, uid, [part], ['default'])
-        pricelist = self.pool.get('res.partner').browse(cr, uid, part).property_product_pricelist_purchase.id
+        part = self.pool.get('res.partner').browse(cr, uid, part)
+        pricelist = part.property_product_pricelist_purchase.id
         return {'value':{'partner_address_id': addr['default'], 'pricelist_id': pricelist}}
 
     def wkf_approve_order(self, cr, uid, ids):
@@ -263,15 +264,15 @@ class purchase_order(osv.osv):
                        })
     def inv_line_create(self,a,ol):
         return (0, False, {
-                    'name': ol.name,
-                    'account_id': a,
-                    'price_unit': ol.price_unit or 0.0,
-                    'quantity': ol.product_qty,
-                    'product_id': ol.product_id.id or False,
-                    'uos_id': ol.product_uom.id or False,
-                    'invoice_line_tax_id': [(6, 0, [x.id for x in ol.taxes_id])],
-                    'account_analytic_id': ol.account_analytic_id.id,
-                })
+            'name': ol.name,
+            'account_id': a,
+            'price_unit': ol.price_unit or 0.0,
+            'quantity': ol.product_qty,
+            'product_id': ol.product_id.id or False,
+            'uos_id': ol.product_uom.id or False,
+            'invoice_line_tax_id': [(6, 0, [x.id for x in ol.taxes_id])],
+            'account_analytic_id': ol.account_analytic_id.id,
+        })
 
     def action_invoice_create(self, cr, uid, ids, *args):
         res = False
@@ -287,17 +288,8 @@ class purchase_order(osv.osv):
                         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:
                     a = self.pool.get('ir.property').get(cr, uid, 'property_account_expense_categ', 'product.category')
+                a = self.pool.get('account.fiscal.position').map_account(cr, uid, o.partner_id, a)
                 il.append(self.inv_line_create(a,ol))
-#               il.append((0, False, {
-#                   'name': ol.name,
-#                   'account_id': a,
-#                   'price_unit': ol.price_unit or 0.0,
-#                   'quantity': ol.product_qty,
-#                   'product_id': ol.product_id.id or False,
-#                   'uos_id': ol.product_uom.id or False,
-#                   'invoice_line_tax_id': [(6, 0, [x.id for x in ol.taxes_id])],
-#                   'account_analytic_id': ol.account_analytic_id.id,
-#               }))
 
             a = o.partner_id.property_account_payable.id
             inv = {
@@ -418,17 +410,17 @@ class purchase_order_line(osv.osv):
 
     def product_id_change(self, cr, uid, ids, pricelist, product, qty, uom,
             partner_id, date_order=False):
-        prod= self.pool.get('product.product').browse(cr, uid,product)
         if not pricelist:
             raise osv.except_osv(_('No Pricelist !'), _('You have to select a pricelist in the purchase form !\nPlease set one before choosing a product.'))
         if not product:
             return {'value': {'price_unit': 0.0, 'name':'','notes':'', 'product_uom' : False}, 'domain':{'product_uom':[]}}
+        prod= self.pool.get('product.product').browse(cr, uid,product)
         lang=False
         if partner_id:
-            lang=self.pool.get('res.partner').read(cr, uid, [partner_id])[0]['lang']
+            lang=self.pool.get('res.partner').read(cr, uid, partner_id)['lang']
         context={'lang':lang}
 
-        prod = self.pool.get('product.product').read(cr, uid, [product], ['supplier_taxes_id','name','seller_delay','uom_po_id','description_purchase'])[0]
+        prod = self.pool.get('product.product').read(cr, uid, product, ['supplier_taxes_id','name','seller_delay','uom_po_id','description_purchase'],context=context)
         prod_uom_po = prod['uom_po_id'][0]
         if not uom:
             uom = prod_uom_po
@@ -445,21 +437,20 @@ class purchase_order_line(osv.osv):
         res = {'value': {'price_unit': price, 'name':prod_name, 'taxes_id':prod['supplier_taxes_id'], 'date_planned': dt,'notes':prod['description_purchase'], 'product_uom': uom}}
         domain = {}
 
-        if res['value']['taxes_id']:
-            taxes = self.pool.get('account.tax').browse(cr, uid,
-                    [x.id for x in prod.supplier_taxes_id])
-            taxep = None
-            if partner_id:
-                taxep = self.pool.get('res.partner').browse(cr, uid,
-                        partner_id).property_account_supplier_tax
-            if not taxep or not taxep.id:
-                res['value']['taxes_id'] = [x.id for x in prod.taxes_id]
-            else:
-                res5 = [taxep.id]
-                for t in taxes:
-                    if not t.tax_group==taxep.tax_group:
-                        res5.append(t.id)
-                res['value']['taxes_id'] = res5
+        
+        taxes = self.pool.get('account.tax').browse(cr, uid,prod['supplier_taxes_id'])
+        taxep = None
+        if partner_id:
+            partner = self.pool.get('res.partner').browse(cr, uid, partner_id)
+            taxep = partner.property_account_position and partner.property_account_position.account_supplier_tax
+        if not taxep or not taxep.id:
+            res['value']['taxes_id'] = prod['supplier_taxes_id']
+        else:
+            res5 = [taxep.id]
+            for t in taxes:
+                if not t.tax_group==taxep.tax_group:
+                    res5.append(t.id)
+            res['value']['taxes_id'] = res5
 
         res2 = self.pool.get('product.uom').read(cr, uid, [uom], ['category_id'])
         res3 = self.pool.get('product.uom').read(cr, uid, [prod_uom_po], ['category_id'])