fixed bug:308803
[odoo/odoo.git] / addons / purchase / purchase.py
index f71973b..514dbba 100644 (file)
@@ -63,7 +63,7 @@ class purchase_order(osv.osv):
             cur=order.pricelist_id.currency_id
             for line in order.order_line:
                 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):
-                    val+= cur_obj.round(cr, uid, cur, c['amount'])
+                    val+= c['amount']
             res[order.id]=cur_obj.round(cr, uid, cur, val)
         return res
 
@@ -86,7 +86,7 @@ class purchase_order(osv.osv):
             cr.execute("""update purchase_order_line set
                     date_planned=%s
                 where
-                    order_id=%d and
+                    order_id=%s and
                     (date_planned=%s or date_planned<%s)""", (value,po.id,po.minimum_planned_date,value))
         return True
 
@@ -207,8 +207,7 @@ class purchase_order(osv.osv):
                 unlink_ids.append(s['id'])
             else:
                 raise osv.except_osv(_('Invalid action !'), _('Cannot delete Purchase Order(s) which are in %s State!' % s['state']))
-        osv.osv.unlink(self, cr, uid, unlink_ids)
-        return True
+        return osv.osv.unlink(self, cr, uid, unlink_ids)        
     
     def button_dummy(self, cr, uid, ids, context={}):
         return True
@@ -339,19 +338,17 @@ class purchase_order(osv.osv):
                     raise osv.except_osv(
                         _('Could not cancel purchase order !'),
                         _('You must first cancel all packings attached to this purchase order.'))
-            for r in self.read(cr,uid,ids,['picking_ids']):
-                for pick in r['picking_ids']:
-                    wf_service = netsvc.LocalService("workflow")
-                    wf_service.trg_validate(uid, 'stock.picking', pick, 'button_cancel', cr)
-            for inv in purchase.invoice_ids:
-                if inv.state not in ('draft','cancel'):
-                    raise osv.except_osv(
-                        _('Could not cancel this purchase order !'),
-                        _('You must first cancel all invoices attached to this purchase order.'))
-            for r in self.read(cr,uid,ids,['invoice_ids']):
-                for inv in r['invoice_ids']:
-                    wf_service = netsvc.LocalService("workflow")
-                    wf_service.trg_validate(uid, 'account.invoice', inv, 'invoice_cancel', cr)
+            for pick in purchase.picking_ids:
+                wf_service = netsvc.LocalService("workflow")
+                wf_service.trg_validate(uid, 'stock.picking', pick.id, 'button_cancel', cr)
+            inv = purchase.invoice_id
+            if inv and inv.state not in ('cancel','draft'):
+                raise osv.except_osv(
+                    _('Could not cancel this purchase order !'),
+                    _('You must first cancel all invoices attached to this purchase order.'))
+            if inv:
+                wf_service = netsvc.LocalService("workflow")
+                wf_service.trg_validate(uid, 'account.invoice', inv.id, 'invoice_cancel', cr)
         self.write(cr,uid,ids,{'state':'cancel'})
         return True
 
@@ -456,9 +453,10 @@ class purchase_order_line(osv.osv):
         if partner_id:
             lang=self.pool.get('res.partner').read(cr, uid, partner_id)['lang']
         context={'lang':lang}
+        context['partner_id'] = partner_id
 
-        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]
+        prod = self.pool.get('product.product').browse(cr, uid, product, context=context)
+        prod_uom_po = prod.uom_po_id.id
         if not uom:
             uom = prod_uom_po
         if not date_order:
@@ -468,21 +466,32 @@ class purchase_order_line(osv.osv):
                     'uom': uom,
                     'date': date_order,
                     })[pricelist]
-        dt = (DateTime.now() + DateTime.RelativeDateTime(days=prod['seller_delay'] or 0.0)).strftime('%Y-%m-%d %H:%M:%S')
-        prod_name = self.pool.get('product.product').name_get(cr, uid, [product], context=context)[0][1]
 
-        res = {'value': {'price_unit': price, 'name':prod_name, 'taxes_id':prod['supplier_taxes_id'], 'date_planned': dt,'notes':prod['description_purchase'], 'product_uom': uom}}
+        qty = 1
+        seller_delay = 0
+        for s in prod.seller_ids:
+            seller_delay = s.delay
+            if s.name.id == partner_id:
+                seller_delay = s.delay
+                qty = s.qty
+        dt = (DateTime.now() + DateTime.RelativeDateTime(days=seller_delay or 0.0)).strftime('%Y-%m-%d %H:%M:%S')
+        prod_name = prod.partner_ref
+
+
+        res = {'value': {'price_unit': price, 'name':prod_name, 'taxes_id':map(lambda x: x.id, prod.supplier_taxes_id),
+            'date_planned': dt,'notes':prod.description_purchase,
+            'product_qty': qty,
+            'product_uom': uom}}
         domain = {}
 
         partner = self.pool.get('res.partner').browse(cr, uid, partner_id)
-        taxes = self.pool.get('account.tax').browse(cr, uid,prod['supplier_taxes_id'])
+        taxes = self.pool.get('account.tax').browse(cr, uid,map(lambda x: x.id, prod.supplier_taxes_id))
         res['value']['taxes_id'] = self.pool.get('account.fiscal.position').map_tax(cr, uid, partner, taxes)
-      #  res['value']['taxes_id'] = [x.id for x in taxes]
 
         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'])
+        res3 = prod.uom_id.category_id.id
         domain = {'product_uom':[('category_id','=',res2[0]['category_id'][0])]}
-        if res2[0]['category_id'] != res3[0]['category_id']:
+        if res2[0]['category_id'][0] != res3:
             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'))
 
         res['domain'] = domain