[REF] Stock : Removed unused method button_confirm
[odoo/odoo.git] / addons / account / partner.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from osv import fields, osv
23 import ir
24
25 class account_fiscal_position(osv.osv):
26     _name = 'account.fiscal.position'
27     _description = 'Fiscal Position'
28     _columns = {
29         'name': fields.char('Fiscal Position', size=64, translate=True, required=True),
30         'company_id': fields.many2one('res.company', 'Company'),
31         'account_ids': fields.one2many('account.fiscal.position.account', 'position_id', 'Account Mapping'),
32         'tax_ids': fields.one2many('account.fiscal.position.tax', 'position_id', 'Tax Mapping'),
33         'note': fields.text('Notes', translate=True),
34     }
35
36     def map_tax(self, cr, uid, fposition_id, taxes, context={}):
37         if not taxes:
38             return []
39         if not fposition_id:
40             return map(lambda x: x.id, taxes)
41         result = []
42         for t in taxes:
43             ok = False
44             for tax in fposition_id.tax_ids:
45                 if tax.tax_src_id.id == t.id:
46                     if tax.tax_dest_id:
47                         result.append(tax.tax_dest_id.id)
48                     ok=True
49             if not ok:
50                 result.append(t.id)
51         return result
52
53     def map_account(self, cr, uid, fposition_id, account_id, context={}):
54         if not fposition_id :
55             return account_id
56         for pos in fposition_id.account_ids:
57             if pos.account_src_id.id==account_id:
58                 account_id = pos.account_dest_id.id
59                 break
60         return account_id
61 account_fiscal_position()
62
63
64 class account_fiscal_position_tax(osv.osv):
65     _name = 'account.fiscal.position.tax'
66     _description = 'Fiscal Position Taxes Mapping'
67     _rec_name = 'position_id'
68     _columns = {
69         'position_id': fields.many2one('account.fiscal.position', 'Fiscal Position', required=True, ondelete='cascade'),
70         'tax_src_id': fields.many2one('account.tax', 'Tax Source', required=True),
71         'tax_dest_id': fields.many2one('account.tax', 'Replacement Tax')
72     }
73 account_fiscal_position_tax()
74
75
76 class account_fiscal_position_account(osv.osv):
77     _name = 'account.fiscal.position.account'
78     _description = 'Fiscal Position Accounts Mapping'
79     _rec_name = 'position_id'
80     _columns = {
81         'position_id': fields.many2one('account.fiscal.position', 'Fiscal Position', required=True, ondelete='cascade'),
82         'account_src_id': fields.many2one('account.account', 'Account Source', domain=[('type','<>','view')], required=True),
83         'account_dest_id': fields.many2one('account.account', 'Account Destination', domain=[('type','<>','view')], required=True)
84     }
85 account_fiscal_position_account()
86
87 class res_partner(osv.osv):
88     _name = 'res.partner'
89     _inherit = 'res.partner'
90     _description = 'Partner'
91     def _credit_debit_get(self, cr, uid, ids, field_names, arg, context):
92         query = self.pool.get('account.move.line')._query_get(cr, uid, context=context)
93         cr.execute("""select
94                 l.partner_id, a.type, sum(l.debit-l.credit)
95             from
96                 account_move_line l
97             left join
98                 account_account a on (l.account_id=a.id)
99             where
100                 a.type =ANY(%s) and
101                 l.partner_id =ANY(%s) and
102                 l.reconcile_id is null and
103                 """+query+"""
104             group by
105                 l.partner_id, a.type
106             """,(['receivable','payable'],ids,))
107         tinvert = {
108             'credit': 'receivable',
109             'debit': 'payable'
110         }
111         maps = {'receivable':'credit', 'payable':'debit' }
112         res = {}
113         for id in ids:
114             res[id] = {}.fromkeys(field_names, 0)
115         for pid,type,val in cr.fetchall():
116             if val is None: val=0
117             res[pid][maps[type]] = (type=='receivable') and val or -val
118         return res
119
120     def _credit_search(self, cr, uid, obj, name, args, context):
121         if not len(args):
122             return []
123         where = ' and '.join(map(lambda x: '(sum(debit-credit)'+x[1]+str(x[2])+')',args))
124         query = self.pool.get('account.move.line')._query_get(cr, uid, context={})
125         cr.execute(('select partner_id from account_move_line l where account_id in (select id from account_account where type=%s and active) and reconcile_id is null and '+query+' and partner_id is not null group by partner_id having '+where), ('receivable',) )
126         res = cr.fetchall()
127         if not len(res):
128             return [('id','=','0')]
129         return [('id','in',map(lambda x:x[0], res))]
130
131     def _debit_search(self, cr, uid, obj, name, args, context):
132         if not len(args):
133             return []
134         query = self.pool.get('account.move.line')._query_get(cr, uid, context={})
135         where = ' and '.join(map(lambda x: '(sum(debit-credit)'+x[1]+str(x[2])+')',args))
136         cr.execute(('select partner_id from account_move_line l where account_id in (select id from account_account where type=%s and active) and reconcile_id is null and '+query+' and partner_id is not null group by partner_id having '+where), ('payable',) )
137         res = cr.fetchall()
138         if not len(res):
139             return [('id','=','0')]
140         return [('id','in',map(lambda x:x[0], res))]
141
142     _columns = {
143         'credit': fields.function(_credit_debit_get,
144             fnct_search=_credit_search, method=True, string='Total Receivable', multi='dc', help="Total amount this customer owes you."),
145         'debit': fields.function(_credit_debit_get, fnct_search=_debit_search, method=True, string='Total Payable', multi='dc', help="Total amount you have to pay to this supplier."),
146         'debit_limit': fields.float('Payable Limit'),
147         'property_account_payable': fields.property(
148             'account.account',
149             type='many2one',
150             relation='account.account',
151             string="Account Payable",
152             method=True,
153             view_load=True,
154             domain="[('type', '=', 'payable')]",
155             help="This account will be used instead of the default one as the payable account for the current partner",
156             required=True),
157         'property_account_receivable': fields.property(
158             'account.account',
159             type='many2one',
160             relation='account.account',
161             string="Account Receivable",
162             method=True,
163             view_load=True,
164             domain="[('type', '=', 'receivable')]",
165             help="This account will be used instead of the default one as the receivable account for the current partner",
166             required=True),
167         'property_account_position': fields.property(
168             'account.fiscal.position',
169             type='many2one',
170             relation='account.fiscal.position',
171             string="Fiscal Position",
172             method=True,
173             view_load=True,
174             help="The fiscal position will determine taxes and the accounts used for the partner.",
175         ),
176         'property_payment_term': fields.property(
177             'account.payment.term',
178             type='many2one',
179             relation='account.payment.term',
180             string ='Payment Term',
181             method=True,
182             view_load=True,
183             help="This payment term will be used instead of the default one for the current partner"),
184         'ref_companies': fields.one2many('res.company', 'partner_id',
185             'Companies that refers to partner'),
186     }
187 res_partner()
188
189
190
191 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
192