Help

Welcome!

This community is for professionals and enthusiasts of our products and services.
Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.

2

Calculate sum of customer payments

Avatar
Davronbek

in Odoo 8.0, I am trying to calculate the sum of payments made by a customer according to the customer's id. I wrote the following code but this hasn't helped at all. If any suggestions or help, you are more than welcome

def _get_sum_invoices(self, cr, uid, ids, field_name, arg, context=None):
res = dict(map(lambda x: (x, 0), ids))
# The current user may not have access rights for sale orders
try:
for partner in self.browse(cr, uid, ids, context):
setOfInvoices = self.env['account.invoice'].search([('id', "in", partner.invoice_ids)])
total = 0
for invoice in setOfInvoices:
total += invoice.amount
res[partner.id] = total
except:
pass
return res

_columns = {
'sum_payments': fields.function(_get_sum_pay, string='# SUM', type='float'),
'total': fields.one2many('account.invoice', 'partner_id','Sum Of Payments')
}
Avatar
Discard
1 Answer
0
Avatar
Leo Tran
Best Answer

Dear Davronbek,

The following code to get sum of payments made by a customer

from openerp import fields, model, api

class res_partner(models.Model):
    _inherit = 'res.partner'
    
    @api.one
    def _get_sum_payment(self):
        payment = self.env['account.voucher'].search([('partner_id','=', self.id),('type','=','payment')])        
        self.sum_payments = sum(item.amount for item in payment)
        
    sum_payments = fields.Float(string="# Sum", compute='_get_sum_payment')


Good luck!
Avatar
Discard

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!