[MERGE] OPW 18123
[odoo/odoo.git] / addons / account / account_cash_statement.py
1 # encoding: utf-8
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2008 PC Solutions (<http://pcsol.be>). All Rights Reserved
6 #    $Id$
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22
23 import time
24
25 from osv import osv, fields
26 from tools.translate import _
27 import decimal_precision as dp
28
29 class account_cashbox_line(osv.osv):
30
31     """ Cash Box Details """
32
33     _name = 'account.cashbox.line'
34     _description = 'CashBox Line'
35     _rec_name = 'number'
36
37     def _sub_total(self, cr, uid, ids, name, arg, context=None):
38
39         """ Calculates Sub total
40         @param name: Names of fields.
41         @param arg: User defined arguments
42         @return: Dictionary of values.
43         """
44         res = {}
45         for obj in self.browse(cr, uid, ids, context=context):
46             res[obj.id] = obj.pieces * obj.number
47         return res
48
49     def on_change_sub(self, cr, uid, ids, pieces, number, *a):
50
51         """ Calculates Sub total on change of number
52         @param pieces: Names of fields.
53         @param number:
54         """
55         sub = pieces * number
56         return {'value': {'subtotal': sub or 0.0}}
57
58     _columns = {
59         'pieces': fields.float('Values', digits_compute=dp.get_precision('Account')),
60         'number': fields.integer('Number'),
61         'subtotal': fields.function(_sub_total, method=True, string='Sub Total', type='float', digits_compute=dp.get_precision('Account')),
62         'starting_id': fields.many2one('account.bank.statement', ondelete='cascade'),
63         'ending_id': fields.many2one('account.bank.statement', ondelete='cascade'),
64      }
65
66 account_cashbox_line()
67
68 class account_cash_statement(osv.osv):
69
70     _inherit = 'account.bank.statement'
71
72     def _get_starting_balance(self, cr, uid, ids, context=None):
73
74         """ Find starting balance
75         @param name: Names of fields.
76         @param arg: User defined arguments
77         @return: Dictionary of values.
78         """
79         res = {}
80         for statement in self.browse(cr, uid, ids, context=context):
81             amount_total = 0.0
82
83             if statement.journal_id.type not in('cash'):
84                 continue
85
86             for line in statement.starting_details_ids:
87                 amount_total+= line.pieces * line.number
88             res[statement.id] = {
89                 'balance_start': amount_total
90             }
91         return res
92
93     def _balance_end_cash(self, cr, uid, ids, name, arg, context=None):
94         """ Find ending balance  "
95         @param name: Names of fields.
96         @param arg: User defined arguments
97         @return: Dictionary of values.
98         """
99         res = {}
100         for statement in self.browse(cr, uid, ids, context=context):
101             amount_total = 0.0
102             for line in statement.ending_details_ids:
103                 amount_total += line.pieces * line.number
104             res[statement.id] = amount_total
105         return res
106
107     def _get_sum_entry_encoding(self, cr, uid, ids, name, arg, context=None):
108
109         """ Find encoding total of statements "
110         @param name: Names of fields.
111         @param arg: User defined arguments
112         @return: Dictionary of values.
113         """
114         res2 = {}
115         for statement in self.browse(cr, uid, ids, context=context):
116             encoding_total=0.0
117             for line in statement.line_ids:
118                encoding_total += line.amount
119             res2[statement.id] = encoding_total
120         return res2
121
122     def _end_balance(self, cursor, user, ids, name, attr, context=None):
123         res_currency_obj = self.pool.get('res.currency')
124         res_users_obj = self.pool.get('res.users')
125         res = {}
126
127         company_currency_id = res_users_obj.browse(cursor, user, user,
128                 context=context).company_id.currency_id.id
129
130         statements = self.browse(cursor, user, ids, context=context)
131         for statement in statements:
132             res[statement.id] = statement.balance_start
133             currency_id = statement.currency.id
134             for line in statement.move_line_ids:
135                 if line.debit > 0:
136                     if line.account_id.id == \
137                             statement.journal_id.default_debit_account_id.id:
138                         res[statement.id] += res_currency_obj.compute(cursor,
139                                 user, company_currency_id, currency_id,
140                                 line.debit, context=context)
141                 else:
142                     if line.account_id.id == \
143                             statement.journal_id.default_credit_account_id.id:
144                         res[statement.id] -= res_currency_obj.compute(cursor,
145                                 user, company_currency_id, currency_id,
146                                 line.credit, context=context)
147
148             if statement.state in ('draft', 'open'):
149                 for line in statement.line_ids:
150                     res[statement.id] += line.amount
151         for r in res:
152             res[r] = round(res[r], 2)
153         return res
154
155     def _get_company(self, cr, uid, context=None):
156         user_pool = self.pool.get('res.users')
157         company_pool = self.pool.get('res.company')
158         user = user_pool.browse(cr, uid, uid, context=context)
159         company_id = user.company_id
160         if not company_id:
161             company_id = company_pool.search(cr, uid, [])
162         return company_id and company_id[0] or False
163
164     def _get_cash_open_box_lines(self, cr, uid, context=None):
165         res = []
166         curr = [1, 2, 5, 10, 20, 50, 100, 500]
167         for rs in curr:
168             dct = {
169                 'pieces': rs,
170                 'number': 0
171             }
172             res.append(dct)
173         journal_ids = self.pool.get('account.journal').search(cr, uid, [('type', '=', 'cash')], context=context)
174         if journal_ids:
175             results = self.search(cr, uid, [('journal_id', 'in', journal_ids),('state', '=', 'confirm')], context=context)
176             if results:
177                 cash_st = self.browse(cr, uid, results, context=context)[0]
178                 for cash_line in cash_st.ending_details_ids:
179                     for r in res:
180                         if cash_line.pieces == r['pieces']:
181                             r['number'] = cash_line.number
182         return res
183
184     def _get_default_cash_close_box_lines(self, cr, uid, context=None):
185         res = []
186         curr = [1, 2, 5, 10, 20, 50, 100, 500]
187         for rs in curr:
188             dct = {
189                 'pieces': rs,
190                 'number': 0
191             }
192             res.append(dct)
193         return res
194
195     def _get_cash_close_box_lines(self, cr, uid, context=None):
196         res = []
197         curr = [1, 2, 5, 10, 20, 50, 100, 500]
198         for rs in curr:
199             dct = {
200                 'pieces': rs,
201                 'number': 0
202             }
203             res.append((0, 0, dct))
204         return res
205
206     def _get_cash_open_close_box_lines(self, cr, uid, context=None):
207         res = {}
208         start_l = []
209         end_l = []
210         starting_details = self._get_cash_open_box_lines(cr, uid, context=context)
211         ending_details = self._get_default_cash_close_box_lines(cr, uid, context)
212         for start in starting_details:
213             start_l.append((0, 0, start))
214         for end in ending_details:
215             end_l.append((0, 0, end))
216         res['start'] = start_l
217         res['end'] = end_l
218         return res
219
220     _columns = {
221         'balance_end_real': fields.float('Closing Balance', digits_compute=dp.get_precision('Account'), states={'confirm': [('readonly', True)]}, help="closing balance entered by the cashbox verifier"),
222         'state': fields.selection(
223             [('draft', 'Draft'),
224             ('confirm', 'Closed'),
225             ('open','Open')], 'State', required=True, states={'confirm': [('readonly', True)]}, readonly="1"),
226         'total_entry_encoding': fields.function(_get_sum_entry_encoding, method=True, store=True, string="Cash Transaction", help="Total cash transactions"),
227         'closing_date': fields.datetime("Closed On"),
228         'balance_end': fields.function(_end_balance, method=True, store=True, string='Balance', help="Closing balance based on Starting Balance and Cash Transactions"),
229         'balance_end_cash': fields.function(_balance_end_cash, method=True, store=True, string='Balance', help="Closing balance based on cashBox"),
230         'starting_details_ids': fields.one2many('account.cashbox.line', 'starting_id', string='Opening Cashbox'),
231         'ending_details_ids': fields.one2many('account.cashbox.line', 'ending_id', string='Closing Cashbox'),
232         'name': fields.char('Name', size=64, required=True, states={'draft': [('readonly', False)]}, readonly=True, help='if you give the Name other then /, its created Accounting Entries Move will be with same name as statement name. This allows the statement entries to have the same references than the statement itself'),
233         'user_id': fields.many2one('res.users', 'Responsible', required=False),
234     }
235     _defaults = {
236         'state': 'draft',
237         'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"),
238         'user_id': lambda self, cr, uid, context=None: uid,
239         'starting_details_ids': _get_cash_open_box_lines,
240         'ending_details_ids': _get_default_cash_close_box_lines
241      }
242
243     def create(self, cr, uid, vals, context=None):
244         sql = [
245                 ('journal_id', '=', vals.get('journal_id', False)),
246                 ('state', '=', 'open')
247         ]
248         open_jrnl = self.search(cr, uid, sql)
249         if open_jrnl:
250             raise osv.except_osv(_('Error'), _('You can not have two open register for the same journal'))
251
252         if self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context=context).type == 'cash':
253             open_close = self._get_cash_open_close_box_lines(cr, uid, context)
254             if vals.get('starting_details_ids', False):
255                 for start in vals.get('starting_details_ids'):
256                     dict_val = start[2]
257                     for end in open_close['end']:
258                        if end[2]['pieces'] == dict_val['pieces']:
259                            end[2]['number'] += dict_val['number']
260             vals.update({
261 #                'ending_details_ids': open_close['start'],
262                 'starting_details_ids': open_close['end']
263             })
264         else:
265             vals.update({
266                 'ending_details_ids': False,
267                 'starting_details_ids': False
268             })
269         res_id = super(account_cash_statement, self).create(cr, uid, vals, context=context)
270         self.write(cr, uid, [res_id], {})
271         return res_id
272
273     def write(self, cr, uid, ids, vals, context=None):
274         """
275         Update redord(s) comes in {ids}, with new value comes as {vals}
276         return True on success, False otherwise
277
278         @param cr: cursor to database
279         @param user: id of current user
280         @param ids: list of record ids to be update
281         @param vals: dict of new values to be set
282         @param context: context arguments, like lang, time zone
283
284         @return: True on success, False otherwise
285         """
286
287         super(account_cash_statement, self).write(cr, uid, ids, vals, context=context)
288         res = self._get_starting_balance(cr, uid, ids)
289         for rs in res:
290             super(account_cash_statement, self).write(cr, uid, [rs], res.get(rs))
291         return True
292
293     def onchange_journal_id(self, cr, uid, statement_id, journal_id, context=None):
294         """ Changes balance start and starting details if journal_id changes"
295         @param statement_id: Changed statement_id
296         @param journal_id: Changed journal_id
297         @return:  Dictionary of changed values
298         """
299         res = {}
300         balance_start = 0.0
301         if not journal_id:
302             res.update({
303                 'balance_start': balance_start
304             })
305             return res
306         return super(account_cash_statement, self).onchange_journal_id(cr, uid, statement_id, journal_id, context=context)
307
308     def _equal_balance(self, cr, uid, cash_id, context=None):
309         statement = self.browse(cr, uid, cash_id, context=context)
310         self.write(cr, uid, [cash_id], {'balance_end_real': statement.balance_end})
311         statement.balance_end_real = statement.balance_end
312         if statement.balance_end != statement.balance_end_cash:
313             return False
314         return True
315
316     def _user_allow(self, cr, uid, statement_id, context=None):
317         return True
318
319     def button_open(self, cr, uid, ids, context=None):
320         """ Changes statement state to Running.
321         @return: True
322         """
323         if context is None:
324             context = {}
325         statement_pool = self.pool.get('account.bank.statement')
326         for statement in statement_pool.browse(cr, uid, ids, context=context):
327             vals = {}
328             if not self._user_allow(cr, uid, statement.id, context=context):
329                 raise osv.except_osv(_('Error !'), (_('User %s does not have rights to access %s journal !') % (statement.user_id.name, statement.journal_id.name)))
330
331             if statement.name and statement.name == '/':
332                 number = self.pool.get('ir.sequence').get(cr, uid, 'account.cash.statement')
333                 vals.update({
334                     'name': number
335                 })
336
337             vals.update({
338                 'date': time.strftime("%Y-%m-%d %H:%M:%S"),
339                 'state': 'open',
340
341             })
342             self.write(cr, uid, [statement.id], vals, context=context)
343         return True
344
345     def balance_check(self, cr, uid, cash_id, journal_type='bank', context=None):
346         if journal_type == 'bank':
347             return super(account_cash_statement, self).balance_check(cr, uid, cash_id, journal_type, context)
348         if not self._equal_balance(cr, uid, cash_id, context):
349             raise osv.except_osv(_('Error !'), _('CashBox Balance is not matching with Calculated Balance !'))
350         return True
351
352     def statement_close(self, cr, uid, ids, journal_type='bank', context=None):
353         if journal_type == 'bank':
354             return super(account_cash_statement, self).statement_close(cr, uid, ids, journal_type, context)
355         vals = {
356             'state':'confirm',
357             'closing_date': time.strftime("%Y-%m-%d %H:%M:%S")
358         }
359         return self.write(cr, uid, ids, vals, context=context)
360
361     def check_status_condition(self, cr, uid, state, journal_type='bank'):
362         if journal_type == 'bank':
363             return super(account_cash_statement, self).check_status_condition(cr, uid, state, journal_type)
364         return state=='open'
365
366     def button_confirm_cash(self, cr, uid, ids, context=None):
367         super(account_cash_statement, self).button_confirm_bank(cr, uid, ids, context=context)
368         return self.write(cr, uid, ids, {'closing_date': time.strftime("%Y-%m-%d %H:%M:%S")}, context=context)
369
370     def button_cancel(self, cr, uid, ids, context=None):
371         cash_box_line_pool = self.pool.get('account.cashbox.line')
372         super(account_cash_statement, self).button_cancel(cr, uid, ids, context=context)
373         for st in self.browse(cr, uid, ids, context):
374             for end in st.ending_details_ids:
375                 cash_box_line_pool.write(cr, uid, [end.id], {'number': 0})
376         return True
377
378 account_cash_statement()
379
380 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: