1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
24 from osv import osv, fields
25 from tools.translate import _
27 class account_automatic_reconcile(osv.osv_memory):
28 _name = 'account.automatic.reconcile'
29 _description = 'Automatic Reconcile'
32 'account_ids': fields.many2many('account.account', 'reconcile_account_rel', 'reconcile_id', 'account_id', 'Accounts to Reconcile', domain = [('reconcile','=',1)],),
33 'writeoff_acc_id': fields.many2one('account.account', 'Account'),
34 'journal_id': fields.many2one('account.journal', 'Journal'),
35 'period_id': fields.many2one('account.period', 'Period'),
36 'max_amount': fields.float('Maximum write-off amount'),
37 'power': fields.selection([(p, str(p)) for p in range(2, 5)], 'Power', required=True, help='Number of partial amounts that can be combined to find a balance point can be chosen as the power of the automatic reconciliation'),
38 'reconciled': fields.integer('Reconciled transactions', readonly=True),
39 'unreconciled': fields.integer('Not reconciled transactions', readonly=True),
40 'allow_write_off': fields.boolean('Allow write off')
43 def _get_reconciled(self, cr, uid, context=None):
46 return context.get('reconciled', 0)
48 def _get_unreconciled(self, cr, uid, context=None):
51 return context.get('unreconciled', 0)
54 'reconciled': _get_reconciled,
55 'unreconciled': _get_unreconciled,
59 #TODO: cleanup and comment this code... For now, it is awfulllll
60 # (way too complex, and really slow)...
61 def do_reconcile(self, cr, uid, credits, debits, max_amount, power, writeoff_acc_id, period_id, journal_id, context=None):
62 # for one value of a credit, check all debits, and combination of them
63 # depending on the power. It starts with a power of one and goes up
64 # to the max power allowed
65 move_line_obj = self.pool.get('account.move.line')
68 def check2(value, move_list, power):
69 def check(value, move_list, power):
70 for i in range(len(move_list)):
73 if abs(value - move[1]) <= max_amount + 0.00001:
77 res = check(value - move[1], move_list, power-1)
78 move_list[i:i] = [move]
84 for p in range(1, power+1):
85 res = check(value, move_list, p)
90 # for a list of credit and debit and a given power, check if there
91 # are matching tuples of credit and debits, check all debits, and combination of them
92 # depending on the power. It starts with a power of one and goes up
93 # to the max power allowed
94 def check4(list1, list2, power):
95 def check3(value, list1, list2, list1power, power):
96 for i in range(len(list1)):
99 res = check2(value + move[1], list2, power - 1)
101 return ([move[0]], res)
104 res = check3(value + move[1], list1, list2, list1power-1, power-1)
112 for p in range(1, power):
113 res = check3(0, list1, list2, p, power)
118 def check5(list1, list2, max_power):
119 for p in range(2, max_power+1):
120 res = check4(list1, list2, p)
126 while credits and debits and ok:
127 res = check5(credits, debits, power)
129 move_line_obj.reconcile(cr, uid, res[0] + res[1], 'auto', writeoff_acc_id, period_id, journal_id, context)
130 reconciled += len(res[0]) + len(res[1])
131 credits = [(id, credit) for (id, credit) in credits if id not in res[0]]
132 debits = [(id, debit) for (id, debit) in debits if id not in res[1]]
135 return (reconciled, len(credits)+len(debits))
137 def reconcile(self, cr, uid, ids, context=None):
138 move_line_obj = self.pool.get('account.move.line')
139 obj_model = self.pool.get('ir.model.data')
142 form = self.browse(cr, uid, ids, context=context)[0]
143 max_amount = form.max_amount or 0.0
145 allow_write_off = form.allow_write_off
146 reconciled = unreconciled = 0
147 if not form.account_ids:
148 raise osv.except_osv(_('User Error!'), _('You must select accounts to reconcile.'))
149 for account_id in form.account_ids:
150 params = (account_id.id,)
151 if not allow_write_off:
152 query = """SELECT partner_id FROM account_move_line WHERE account_id=%s AND reconcile_id IS NULL
153 AND state <> 'draft' GROUP BY partner_id
154 HAVING ABS(SUM(debit-credit)) = 0.0 AND count(*)>0"""
156 query = """SELECT partner_id FROM account_move_line WHERE account_id=%s AND reconcile_id IS NULL
157 AND state <> 'draft' GROUP BY partner_id
158 HAVING ABS(SUM(debit-credit)) < %s AND count(*)>0"""
159 params += (max_amount,)
160 # reconcile automatically all transactions from partners whose balance is 0
161 cr.execute(query, params)
162 partner_ids = [id for (id,) in cr.fetchall()]
163 for partner_id in partner_ids:
166 "FROM account_move_line " \
167 "WHERE account_id=%s " \
168 "AND partner_id=%s " \
169 "AND state <> 'draft' " \
170 "AND reconcile_id IS NULL",
171 (account_id.id, partner_id))
172 line_ids = [id for (id,) in cr.fetchall()]
174 reconciled += len(line_ids)
176 move_line_obj.reconcile(cr, uid, line_ids, 'auto', form.writeoff_acc_id.id, form.period_id.id, form.journal_id.id, context)
178 move_line_obj.reconcile_partial(cr, uid, line_ids, 'manual', context=context)
180 # get the list of partners who have more than one unreconciled transaction
182 "SELECT partner_id " \
183 "FROM account_move_line " \
184 "WHERE account_id=%s " \
185 "AND reconcile_id IS NULL " \
186 "AND state <> 'draft' " \
187 "AND partner_id IS NOT NULL " \
188 "GROUP BY partner_id " \
191 partner_ids = [id for (id,) in cr.fetchall()]
193 for partner_id in partner_ids:
194 # get the list of unreconciled 'debit transactions' for this partner
196 "SELECT id, debit " \
197 "FROM account_move_line " \
198 "WHERE account_id=%s " \
199 "AND partner_id=%s " \
200 "AND reconcile_id IS NULL " \
201 "AND state <> 'draft' " \
203 "ORDER BY date_maturity",
204 (account_id.id, partner_id))
205 debits = cr.fetchall()
207 # get the list of unreconciled 'credit transactions' for this partner
209 "SELECT id, credit " \
210 "FROM account_move_line " \
211 "WHERE account_id=%s " \
212 "AND partner_id=%s " \
213 "AND reconcile_id IS NULL " \
214 "AND state <> 'draft' " \
216 "ORDER BY date_maturity",
217 (account_id.id, partner_id))
218 credits = cr.fetchall()
220 (rec, unrec) = self.do_reconcile(cr, uid, credits, debits, max_amount, power, form.writeoff_acc_id.id, form.period_id.id, form.journal_id.id, context)
222 unreconciled += unrec
224 # add the number of transactions for partners who have only one
225 # unreconciled transactions to the unreconciled count
226 partner_filter = partner_ids and 'AND partner_id not in (%s)' % ','.join(map(str, filter(None, partner_ids))) or ''
229 "FROM account_move_line " \
230 "WHERE account_id=%s " \
231 "AND reconcile_id IS NULL " \
232 "AND state <> 'draft' " + partner_filter,
234 additional_unrec = cr.fetchone()[0]
235 unreconciled = unreconciled + additional_unrec
236 context.update({'reconciled': reconciled, 'unreconciled': unreconciled})
237 model_data_ids = obj_model.search(cr,uid,[('model','=','ir.ui.view'),('name','=','account_automatic_reconcile_view1')])
238 resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'])[0]['res_id']
242 'res_model': 'account.automatic.reconcile',
243 'views': [(resource_id,'form')],
244 'type': 'ir.actions.act_window',
249 account_automatic_reconcile()
251 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: