[FIX] code cleanup (addon purchase)
authorGery Debongnie <ged@openerp.com>
Wed, 7 May 2014 14:45:32 +0000 (16:45 +0200)
committerGery Debongnie <ged@openerp.com>
Wed, 7 May 2014 14:45:32 +0000 (16:45 +0200)
simplify a few _count methods by removing useless try/except/pass and by using search_count when appropriate.  It allows us to remove two one2many fields as well.

bzr revid: ged@openerp.com-20140507144532-dgm9mfgt9k5p10jr

addons/purchase/partner.py
addons/purchase/purchase.py

index b7757c2..36ad724 100644 (file)
@@ -25,22 +25,16 @@ class res_partner(osv.osv):
     _name = 'res.partner'
     _inherit = 'res.partner'
 
-    def _purchase_order_count(self, cr, uid, ids, field_name, arg, context=None):
-        res = dict(map(lambda x: (x,{'purchase_order_count': 0, 'supplier_invoice_count': 0}), ids))
-        invoice_ids = self.pool.get('account.invoice').search(cr,uid, [('type', '=', 'in_invoice'), ('partner_id', '=', ids[0])])
-        for partner in self.browse(cr, uid, ids, context=context):
-            res[partner.id] = {
-                    'purchase_order_count': len(partner.purchase_order_ids),
-                    'supplier_invoice_count': len(invoice_ids),
-                }
-        return res
-    def copy(self, cr, uid, id, default=None, context=None):
-        if default is None:
-            default = {}
-
-        default.update({'purchase_order_ids': []})
-
-        return super(res_partner, self).copy(cr, uid, id, default=default, context=context)
+    def _purchase_invoice_count(self, cr, uid, ids, field_name, arg, context=None):
+        PurchaseOrder = self.pool['purchase.order']
+        Invoice = self.pool['account.invoice']
+        return {
+            partner_id: {
+                'purchase_order_count': PurchaseOrder.search_count(cr,uid, [('partner_id', '=', partner_id)], context=context),
+                'supplier_invoice_count': Invoice.search_count(cr,uid, [('partner_id', '=', partner_id), ('type','=','in_invoice')], context=context)
+            }
+            for partner_id in ids
+        }
 
     def _commercial_fields(self, cr, uid, context=None):
         return super(res_partner, self)._commercial_fields(cr, uid, context=context) + ['property_product_pricelist_purchase']
@@ -52,13 +46,9 @@ class res_partner(osv.osv):
           domain=[('type','=','purchase')],
           string="Purchase Pricelist", 
           help="This pricelist will be used, instead of the default one, for purchases from the current partner"),
-        'purchase_order_count': fields.function(_purchase_order_count, string='# of Purchase Order', type='integer', multi="count"),
-        'purchase_order_ids': fields.one2many('purchase.order','partner_id','Purchase Order'),
-        'invoice_ids': fields.one2many('account.invoice','partner_id','Supplier Invoices'),
-        'supplier_invoice_count': fields.function(_purchase_order_count, string='# Supplier Invoices', type='integer', multi="count"),
+        'purchase_order_count': fields.function(_purchase_invoice_count, string='# of Purchase Order', type='integer', multi="count"),
+        'supplier_invoice_count': fields.function(_purchase_invoice_count, string='# Supplier Invoices', type='integer', multi="count"),
     }
 
-
-
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
index 4e98282..457523d 100644 (file)
@@ -148,15 +148,13 @@ class purchase_order(osv.osv):
         return res and res[0] or False  
 
     def _count_all(self, cr, uid, ids, field_name, arg, context=None):
-        res = dict(map(lambda x: (x,{'shipment_count': 0, 'invoice_count': 0,}), ids))
-        try:
-            for data in self.browse(cr, uid, ids, context=context):
-                res[data.id] = {'shipment_count': len(data.picking_ids),
-                'invoice_count': len(data.invoice_ids),
-                }
-        except:
-            pass
-        return res
+        return {
+            purchase.id: {
+                'shipment_count': len(purchase.picking_ids),
+                'invoice_count': len(purchase.invoice_ids),                
+            }
+            for purchase in self.browse(cr, uid, ids, context=context)
+        }
 
     STATE_SELECTION = [
         ('draft', 'Draft PO'),