From d6fe36f1f741fe701e092b4b6f9ad3c95d2bce9b Mon Sep 17 00:00:00 2001 From: Amit Vora Date: Tue, 13 Jan 2015 17:34:05 +0530 Subject: [PATCH] [IMP] sale: put stat button with invoices on sale order. invoice_exists field has been removed, because invoice_count can be used when the old field was used. invoiced_rate field has been removed, because it was never used except in a test. Also added a stat button with deliveries on sale order. --- addons/purchase/purchase.py | 16 +++-- addons/sale/sale.py | 63 +++++-------------- addons/sale/sale_view.xml | 24 ++++--- addons/sale/test/manual_order_policy.yml | 2 +- addons/sale_stock/sale_stock.py | 11 +++- addons/sale_stock/sale_stock_view.xml | 14 +++-- .../sale_stock/test/picking_order_policy.yml | 1 - 7 files changed, 61 insertions(+), 70 deletions(-) diff --git a/addons/purchase/purchase.py b/addons/purchase/purchase.py index 6e58e3893b4..8c83164e04c 100644 --- a/addons/purchase/purchase.py +++ b/addons/purchase/purchase.py @@ -416,15 +416,21 @@ class purchase_order(osv.osv): mod_obj = self.pool.get('ir.model.data') act_obj = self.pool.get('ir.actions.act_window') - result = mod_obj.get_object_reference(cr, uid, 'account', 'action_invoice_tree2') - id = result and result[1] or False - result = act_obj.read(cr, uid, [id], context=context)[0] + action_id = mod_obj.xmlid_to_res_id(cr, uid, 'account.action_invoice_tree2') + result = act_obj.read(cr, uid, action_id, context=context) inv_ids = [] for po in self.browse(cr, uid, ids, context=context): - inv_ids+= [invoice.id for invoice in po.invoice_ids] + inv_ids += [invoice.id for invoice in po.invoice_ids] if not inv_ids: raise UserError(_('Please create Invoices.')) - result['domain'] = [('id', 'in', inv_ids)] + + if len(inv_ids) > 1: + result['domain'] = [('id', 'in', inv_ids)] + else: + res = mod_obj.xmlid_to_res_id(cr, uid, 'account.invoice_supplier_form') + result['views'] = [(res, 'form')] + result['res_id'] = inv_ids and inv_ids[0] or False + return result def view_invoice(self, cr, uid, ids, context=None): ''' diff --git a/addons/sale/sale.py b/addons/sale/sale.py index 256529f9768..f549f4d8637 100644 --- a/addons/sale/sale.py +++ b/addons/sale/sale.py @@ -68,47 +68,7 @@ class sale_order(osv.osv): res[order.id]['amount_total'] = res[order.id]['amount_untaxed'] + res[order.id]['amount_tax'] return res - - def _invoiced_rate(self, cursor, user, ids, name, arg, context=None): - res = {} - for sale in self.browse(cursor, user, ids, context=context): - if sale.invoiced: - res[sale.id] = 100.0 - continue - tot = 0.0 - for invoice in sale.invoice_ids: - if invoice.state not in ('draft', 'cancel'): - tot += invoice.amount_untaxed - if tot: - res[sale.id] = min(100.0, tot * 100.0 / (sale.amount_untaxed or 1.00)) - else: - res[sale.id] = 0.0 - return res - - def _invoice_exists(self, cursor, user, ids, name, arg, context=None): - res = {} - for sale in self.browse(cursor, user, ids, context=context): - res[sale.id] = False - if sale.invoice_ids: - res[sale.id] = True - return res - - def _invoiced(self, cursor, user, ids, name, arg, context=None): - res = {} - for sale in self.browse(cursor, user, ids, context=context): - res[sale.id] = True - invoice_existence = False - for invoice in sale.invoice_ids: - if invoice.state!='cancel': - invoice_existence = True - if invoice.state != 'paid': - res[sale.id] = False - break - if not invoice_existence or sale.state == 'manual': - res[sale.id] = False - return res - - def _invoiced_search(self, cursor, user, obj, name, args, context=None): + def _search_invoiced(self, cursor, user, obj, name, args, context=None): if not len(args): return [] clause = '' @@ -169,6 +129,17 @@ class sale_order(osv.osv): return int(team_ids[0][0]) return None + def _get_invoiced(self, cr, uid, ids, field_name, arg, context=None): + res = {} + for order in self.browse(cr, uid, ids, context=context): + res[order.id] = { + 'invoice_count': len(order.invoice_ids), + 'invoiced': False, + } + if order.state != 'manual' and any(invoice.state == 'paid' for invoice in order.invoice_ids): + res[order.id]['invoiced'] = True + return res + _columns = { 'name': fields.char('Order Reference', required=True, copy=False, readonly=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, select=True), @@ -206,13 +177,9 @@ class sale_order(osv.osv): 'order_line': fields.one2many('sale.order.line', 'order_id', 'Order Lines', readonly=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, copy=True), 'invoice_ids': fields.many2many('account.invoice', 'sale_order_invoice_rel', 'order_id', 'invoice_id', 'Invoices', readonly=True, copy=False, help="This is the list of invoices that have been generated for this sales order. The same sales order may have been invoiced in several times (by line for example)."), - 'invoiced_rate': fields.function(_invoiced_rate, string='Invoiced Ratio', type='float'), - 'invoiced': fields.function(_invoiced, string='Paid', - fnct_search=_invoiced_search, type='boolean', help="It indicates that an invoice has been paid."), - 'invoice_exists': fields.function(_invoice_exists, string='Invoiced', - fnct_search=_invoiced_search, type='boolean', help="It indicates that sales order has at least one invoice."), + 'invoice_count': fields.function(_get_invoiced, type='integer', string='Invoices', multi="counts"), + 'invoiced': fields.function(_get_invoiced, fnct_search=_search_invoiced, type='boolean', string='Paid', multi="counts"), 'note': fields.text('Terms and conditions'), - 'amount_untaxed': fields.function(_amount_all_wrapper, digits_compute=dp.get_precision('Account'), string='Untaxed Amount', store={ 'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10), @@ -682,7 +649,7 @@ class sale_order(osv.osv): def action_ignore_delivery_exception(self, cr, uid, ids, context=None): for sale_order in self.browse(cr, uid, ids, context=context): - self.write(cr, uid, ids, {'state': 'progress' if sale_order.invoice_exists else 'manual'}, context=context) + self.write(cr, uid, ids, {'state': 'progress' if sale_order.invoice_count else 'manual'}, context=context) return True def action_ship_create(self, cr, uid, ids, context=None): diff --git a/addons/sale/sale_view.xml b/addons/sale/sale_view.xml index 1baa2448c8c..d02f6e58272 100644 --- a/addons/sale/sale_view.xml +++ b/addons/sale/sale_view.xml @@ -99,8 +99,6 @@ + @@ -221,7 +230,6 @@ - diff --git a/addons/sale/test/manual_order_policy.yml b/addons/sale/test/manual_order_policy.yml index d2a1ff0195f..ee62fe57e4b 100644 --- a/addons/sale/test/manual_order_policy.yml +++ b/addons/sale/test/manual_order_policy.yml @@ -64,6 +64,6 @@ !python {model: sale.order}: | sale_order = self.browse(cr, uid, ref("sale_order_2")) assert sale_order.invoice_ids, "Invoice should be created." - assert sale_order.invoice_exists, "Order is not invoiced." + assert sale_order.invoice_count, "Order is not invoiced." assert sale_order.invoiced, "Order is not paid." assert sale_order.state == 'done', 'Order should be Done.' diff --git a/addons/sale_stock/sale_stock.py b/addons/sale_stock/sale_stock.py index dc6a2541b13..0b417938baf 100644 --- a/addons/sale_stock/sale_stock.py +++ b/addons/sale_stock/sale_stock.py @@ -89,6 +89,12 @@ class sale_order(osv.osv): invoice_vals['incoterms_id'] = order.incoterm.id or False return invoice_vals + def _get_delivery_count(self, cr, uid, ids, field_name, arg, context=None): + res = {} + for order in self.browse(cr, uid, ids, context=context): + res[order.id] = len(picking for picking in order.picking_ids if picking.picking_type_id.code == 'outgoing') + return res + _columns = { 'incoterm': fields.many2one('stock.incoterms', 'Incoterms', help="International Commercial Terms are a series of predefined commercial terms used in international transactions."), 'picking_policy': fields.selection([('direct', 'Deliver each product when available'), ('one', 'Deliver all products at once')], @@ -105,6 +111,7 @@ class sale_order(osv.osv): }), 'warehouse_id': fields.many2one('stock.warehouse', 'Warehouse', required=True, readonly=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}), 'picking_ids': fields.function(_get_picking_ids, method=True, type='one2many', relation='stock.picking', string='Picking associated to this sale'), + 'delivery_count': fields.function(_get_delivery_count, type='integer', string='Delivery Orders'), } _defaults = { 'warehouse_id': _get_default_warehouse, @@ -136,8 +143,8 @@ class sale_order(osv.osv): #compute the number of delivery orders to display pick_ids = [] for so in self.browse(cr, uid, ids, context=context): - pick_ids += [picking.id for picking in so.picking_ids] - + pick_ids += [picking.id for picking in so.picking_ids if picking.picking_type_id.code == 'outgoing'] + #choose the view_mode accordingly if len(pick_ids) > 1: result['domain'] = "[('id','in',[" + ','.join(map(str, pick_ids)) + "])]" diff --git a/addons/sale_stock/sale_stock_view.xml b/addons/sale_stock/sale_stock_view.xml index bb3ddda3e88..a582cd70bf5 100644 --- a/addons/sale_stock/sale_stock_view.xml +++ b/addons/sale_stock/sale_stock_view.xml @@ -12,11 +12,16 @@ +