[FIX] account: go green test\!, also set the test to previous year
[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 operator import itemgetter
23 import time
24
25 from openerp.osv import fields, osv
26
27 class account_fiscal_position(osv.osv):
28     _name = 'account.fiscal.position'
29     _description = 'Fiscal Position'
30     _columns = {
31         'name': fields.char('Fiscal Position', size=64, required=True),
32         'active': fields.boolean('Active', help="By unchecking the active field, you may hide a fiscal position without deleting it."),
33         'company_id': fields.many2one('res.company', 'Company'),
34         'account_ids': fields.one2many('account.fiscal.position.account', 'position_id', 'Account Mapping'),
35         'tax_ids': fields.one2many('account.fiscal.position.tax', 'position_id', 'Tax Mapping'),
36         'note': fields.text('Notes', translate=True),
37     }
38
39     _defaults = {
40         'active': True,
41     }
42
43     def map_tax(self, cr, uid, fposition_id, taxes, context=None):
44         if not taxes:
45             return []
46         if not fposition_id:
47             return map(lambda x: x.id, taxes)
48         result = set()
49         for t in taxes:
50             ok = False
51             for tax in fposition_id.tax_ids:
52                 if tax.tax_src_id.id == t.id:
53                     if tax.tax_dest_id:
54                         result.add(tax.tax_dest_id.id)
55                     ok=True
56             if not ok:
57                 result.add(t.id)
58         return list(result)
59
60     def map_account(self, cr, uid, fposition_id, account_id, context=None):
61         if not fposition_id:
62             return account_id
63         for pos in fposition_id.account_ids:
64             if pos.account_src_id.id == account_id:
65                 account_id = pos.account_dest_id.id
66                 break
67         return account_id
68
69 account_fiscal_position()
70
71 class account_fiscal_position_tax(osv.osv):
72     _name = 'account.fiscal.position.tax'
73     _description = 'Taxes Fiscal Position'
74     _rec_name = 'position_id'
75     _columns = {
76         'position_id': fields.many2one('account.fiscal.position', 'Fiscal Position', required=True, ondelete='cascade'),
77         'tax_src_id': fields.many2one('account.tax', 'Tax Source', required=True),
78         'tax_dest_id': fields.many2one('account.tax', 'Replacement Tax')
79     }
80
81     _sql_constraints = [
82         ('tax_src_dest_uniq',
83          'unique (position_id,tax_src_id,tax_dest_id)',
84          'A tax fiscal position could be defined only once time on same taxes.')
85     ]
86
87 account_fiscal_position_tax()
88
89 class account_fiscal_position_account(osv.osv):
90     _name = 'account.fiscal.position.account'
91     _description = 'Accounts Fiscal Position'
92     _rec_name = 'position_id'
93     _columns = {
94         'position_id': fields.many2one('account.fiscal.position', 'Fiscal Position', required=True, ondelete='cascade'),
95         'account_src_id': fields.many2one('account.account', 'Account Source', domain=[('type','<>','view')], required=True),
96         'account_dest_id': fields.many2one('account.account', 'Account Destination', domain=[('type','<>','view')], required=True)
97     }
98
99     _sql_constraints = [
100         ('account_src_dest_uniq',
101          'unique (position_id,account_src_id,account_dest_id)',
102          'An account fiscal position could be defined only once time on same accounts.')
103     ]
104
105 account_fiscal_position_account()
106
107 class res_partner(osv.osv):
108     _name = 'res.partner'
109     _inherit = 'res.partner'
110     _description = 'Partner'
111
112     def _credit_debit_get(self, cr, uid, ids, field_names, arg, context=None):
113         query = self.pool.get('account.move.line')._query_get(cr, uid, context=context)
114         cr.execute("""SELECT l.partner_id, a.type, SUM(l.debit-l.credit)
115                       FROM account_move_line l
116                       LEFT JOIN account_account a ON (l.account_id=a.id)
117                       WHERE a.type IN ('receivable','payable')
118                       AND l.partner_id IN %s
119                       AND (l.reconcile_id IS NULL OR 
120                            reconcile_id in (SELECT id FROM account_move_reconcile WHERE opening_reconciliation is TRUE))
121                       AND """ + query + """
122                       GROUP BY l.partner_id, a.type
123                       """,
124                    (tuple(ids),))
125         maps = {'receivable':'credit', 'payable':'debit' }
126         res = {}
127         for id in ids:
128             res[id] = {}.fromkeys(field_names, 0)
129         for pid,type,val in cr.fetchall():
130             if val is None: val=0
131             res[pid][maps[type]] = (type=='receivable') and val or -val
132         return res
133
134     def _asset_difference_search(self, cr, uid, obj, name, type, args, context=None):
135         if not args:
136             return []
137         having_values = tuple(map(itemgetter(2), args))
138         where = ' AND '.join(
139             map(lambda x: '(SUM(bal2) %(operator)s %%s)' % {
140                                 'operator':x[1]},args))
141         query = self.pool.get('account.move.line')._query_get(cr, uid, context=context)
142         cr.execute(('SELECT pid AS partner_id, SUM(bal2) FROM ' \
143                     '(SELECT CASE WHEN bal IS NOT NULL THEN bal ' \
144                     'ELSE 0.0 END AS bal2, p.id as pid FROM ' \
145                     '(SELECT (debit-credit) AS bal, partner_id ' \
146                     'FROM account_move_line l ' \
147                     'WHERE account_id IN ' \
148                             '(SELECT id FROM account_account '\
149                             'WHERE type=%s AND active) ' \
150                     'AND reconcile_id IS NULL ' \
151                     'AND '+query+') AS l ' \
152                     'RIGHT JOIN res_partner p ' \
153                     'ON p.id = partner_id ) AS pl ' \
154                     'GROUP BY pid HAVING ' + where), 
155                     (type,) + having_values)
156         res = cr.fetchall()
157         if not res:
158             return [('id','=','0')]
159         return [('id','in',map(itemgetter(0), res))]
160
161     def _credit_search(self, cr, uid, obj, name, args, context=None):
162         return self._asset_difference_search(cr, uid, obj, name, 'receivable', args, context=context)
163
164     def _debit_search(self, cr, uid, obj, name, args, context=None):
165         return self._asset_difference_search(cr, uid, obj, name, 'payable', args, context=context)
166
167     def has_something_to_reconcile(self, cr, uid, partner_id, context=None):
168         '''
169         at least a debit, a credit and a line older than the last reconciliation date of the partner
170         '''
171         cr.execute('''
172             SELECT l.partner_id, SUM(l.debit) AS debit, SUM(l.credit) AS credit
173             FROM account_move_line l
174             RIGHT JOIN account_account a ON (a.id = l.account_id)
175             RIGHT JOIN res_partner p ON (l.partner_id = p.id)
176             WHERE a.reconcile IS TRUE
177             AND p.id = %s
178             AND l.reconcile_id IS NULL
179             AND (p.last_reconciliation_date IS NULL OR l.date > p.last_reconciliation_date)
180             AND l.state <> 'draft'
181             GROUP BY l.partner_id''', (partner_id,))
182         res = cr.dictfetchone()
183         if res:
184             return bool(res['debit'] and res['credit'])
185         return False
186
187     def mark_as_reconciled(self, cr, uid, ids, context=None):
188         return self.write(cr, uid, ids, {'last_reconciliation_date': time.strftime('%Y-%m-%d %H:%M:%S')}, context=context)
189
190     _columns = {
191         'credit': fields.function(_credit_debit_get,
192             fnct_search=_credit_search, string='Total Receivable', multi='dc', help="Total amount this customer owes you."),
193         'debit': fields.function(_credit_debit_get, fnct_search=_debit_search, string='Total Payable', multi='dc', help="Total amount you have to pay to this supplier."),
194         'debit_limit': fields.float('Payable Limit'),
195         'property_account_payable': fields.property(
196             'account.account',
197             type='many2one',
198             relation='account.account',
199             string="Account Payable",
200             view_load=True,
201             domain="[('type', '=', 'payable')]",
202             help="This account will be used instead of the default one as the payable account for the current partner",
203             required=True),
204         'property_account_receivable': fields.property(
205             'account.account',
206             type='many2one',
207             relation='account.account',
208             string="Account Receivable",
209             view_load=True,
210             domain="[('type', '=', 'receivable')]",
211             help="This account will be used instead of the default one as the receivable account for the current partner",
212             required=True),
213         'property_account_position': fields.property(
214             'account.fiscal.position',
215             type='many2one',
216             relation='account.fiscal.position',
217             string="Fiscal Position",
218             view_load=True,
219             help="The fiscal position will determine taxes and accounts used for the partner.",
220         ),
221         'property_payment_term': fields.property(
222             'account.payment.term',
223             type='many2one',
224             relation='account.payment.term',
225             string ='Customer Payment Term',
226             view_load=True,
227             help="This payment term will be used instead of the default one for sale orders and customer invoices"),
228         'property_supplier_payment_term': fields.property(
229             'account.payment.term',
230              type='many2one',
231              relation='account.payment.term',
232              string ='Supplier Payment Term',
233              view_load=True,
234              help="This payment term will be used instead of the default one for purchase orders and supplier invoices"),
235         'ref_companies': fields.one2many('res.company', 'partner_id',
236             'Companies that refers to partner'),
237         'last_reconciliation_date': fields.datetime('Latest Full Reconciliation Date', help='Date on which the partner accounting entries were fully reconciled last time. It differs from the last date where a reconciliation has been made for this partner, as here we depict the fact that nothing more was to be reconciled at this date. This can be achieved in 2 different ways: either the last unreconciled debit/credit entry of this partner was reconciled, either the user pressed the button "Nothing more to reconcile" during the manual reconciliation process.')
238     }
239
240     def _commercial_fields(self, cr, uid, context=None):
241         return super(res_partner, self)._commercial_fields(cr, uid, context=context) + \
242             ['debit_limit', 'property_account_payable', 'property_account_receivable', 'property_account_position',
243              'property_payment_term', 'property_supplier_payment_term', 'last_reconciliation_date']
244
245 res_partner()
246
247 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: