1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2011 NovaPoint Group LLC (<http://www.novapointgroup.com>)
6
# Copyright (C) 2004-2010 OpenERP SA (<http://www.openerp.com>)
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>
21
##############################################################################
23
from osv import fields, osv
25
class res_partner(osv.osv):
26
_inherit = "res.partner"
28
#===========================================================================
29
# Overriding a function defined on account module.
30
# This is to include the payable and receivable from child partners if the
31
# account is a national account.
32
#===========================================================================
33
def _credit_debit_get(self, cr, uid, ids, field_names, arg, context=None):
35
calculating payable and receivable from account moves for the mentioned partners
37
query = self.pool.get('account.move.line')._query_get(cr, uid, context=context)
39
SELECT l.partner_id, a.type, SUM(l.debit-l.credit)
40
FROM account_move_line l
41
LEFT JOIN account_account a ON (l.account_id=a.id)
42
WHERE a.type IN ('receivable','payable')
43
AND l.partner_id IN %s
44
AND l.reconcile_id IS NULL
46
GROUP BY l.partner_id, a.type
48
maps = {'receivable': 'credit', 'payable': 'debit'}
51
res[id] = dict.fromkeys(field_names, 0)
52
for pid, type, val in cr.fetchall():
53
if val is None: val = 0
54
partner = self.browse(cr, uid, pid, context=context)
55
#Include the payable and receivable form child partner if the Partner is national account
56
if partner.nat_acc_parent:
57
res[pid][maps[type]] = (type == 'receivable') and val or -val
58
child_partner_ids = self.search(cr, uid, [('parent_id', 'child_of', [partner.id])], context=context)
60
child_partner_ids.remove(partner.id)
61
for val in self.read(cr, uid, child_partner_ids, ['credit', 'debit'], context=context):
62
res[pid][maps[type]] += val.get(maps[type], 0)
64
res[pid][maps[type]] = (type == 'receivable') and val or -val
68
def _credit_search(self, cr, uid, obj, name, args, context=None):
70
search function for the credit field
72
return self._asset_difference_search(cr, uid, obj, name, 'receivable', args, context=context)
74
def _debit_search(self, cr, uid, obj, name, args, context=None):
76
search function for the debit field
78
return self._asset_difference_search(cr, uid, obj, name, 'payable', args, context=context)
81
'credit': fields.function(_credit_debit_get, fnct_search=_credit_search, method=True, string='Total Receivable', multi='dc',
82
help="Total amount this customer owes you."),
83
'debit': fields.function(_credit_debit_get, fnct_search=_debit_search, method=True, string='Total Payable', multi='dc',
84
help="Total amount you have to pay to this supplier."),
85
'nat_acc_parent': fields.boolean('National Acct Parent', help='Designates this partner as the top level parent of a "National Account".'),
89
'nat_acc_parent': False,
94
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: