2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2008 PC Solutions (<http://pcsol.be>). All Rights Reserved
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 ##############################################################################
25 from osv import osv, fields
26 from tools.translate import _
27 import decimal_precision as dp
29 class account_cashbox_line(osv.osv):
31 """ Cash Box Details """
33 _name = 'account.cashbox.line'
34 _description = 'CashBox Line'
37 def _sub_total(self, cr, uid, ids, name, arg, context=None):
39 """ Calculates Sub total
40 @param name: Names of fields.
41 @param arg: User defined arguments
42 @return: Dictionary of values.
45 for obj in self.browse(cr, uid, ids, context=context):
46 res[obj.id] = obj.pieces * obj.number
49 def on_change_sub(self, cr, uid, ids, pieces, number, *a):
51 """ Calculates Sub total on change of number
52 @param pieces: Names of fields.
56 return {'value': {'subtotal': sub or 0.0}}
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'),
66 account_cashbox_line()
68 class account_cash_statement(osv.osv):
70 _inherit = 'account.bank.statement'
72 def _get_starting_balance(self, cr, uid, ids, context=None):
74 """ Find starting balance
75 @param name: Names of fields.
76 @param arg: User defined arguments
77 @return: Dictionary of values.
80 for statement in self.browse(cr, uid, ids, context=context):
83 if statement.journal_id.type not in('cash'):
86 for line in statement.starting_details_ids:
87 amount_total+= line.pieces * line.number
89 'balance_start': amount_total
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.
100 for statement in self.browse(cr, uid, ids, context=context):
102 for line in statement.ending_details_ids:
103 amount_total += line.pieces * line.number
104 res[statement.id] = amount_total
107 def _get_sum_entry_encoding(self, cr, uid, ids, name, arg, context=None):
109 """ Find encoding total of statements "
110 @param name: Names of fields.
111 @param arg: User defined arguments
112 @return: Dictionary of values.
115 for statement in self.browse(cr, uid, ids, context=context):
117 for line in statement.line_ids:
118 encoding_total += line.amount
119 res2[statement.id] = encoding_total
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')
127 company_currency_id = res_users_obj.browse(cursor, user, user,
128 context=context).company_id.currency_id.id
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:
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)
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)
148 if statement.state in ('draft', 'open'):
149 for line in statement.line_ids:
150 res[statement.id] += line.amount
152 res[r] = round(res[r], 2)
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
161 company_id = company_pool.search(cr, uid, [])
162 return company_id and company_id[0] or False
164 def _get_cash_open_box_lines(self, cr, uid, context=None):
166 curr = [1, 2, 5, 10, 20, 50, 100, 500]
173 journal_ids = self.pool.get('account.journal').search(cr, uid, [('type', '=', 'cash')], context=context)
175 results = self.search(cr, uid, [('journal_id', 'in', journal_ids),('state', '=', 'confirm')], context=context)
177 cash_st = self.browse(cr, uid, results, context=context)[0]
178 for cash_line in cash_st.ending_details_ids:
180 if cash_line.pieces == r['pieces']:
181 r['number'] = cash_line.number
184 def _get_default_cash_close_box_lines(self, cr, uid, context=None):
186 curr = [1, 2, 5, 10, 20, 50, 100, 500]
195 def _get_cash_close_box_lines(self, cr, uid, context=None):
197 curr = [1, 2, 5, 10, 20, 50, 100, 500]
203 res.append((0, 0, dct))
206 def _get_cash_open_close_box_lines(self, cr, uid, context=None):
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
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(
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),
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
243 def create(self, cr, uid, vals, context=None):
245 ('journal_id', '=', vals.get('journal_id', False)),
246 ('state', '=', 'open')
248 open_jrnl = self.search(cr, uid, sql)
250 raise osv.except_osv(_('Error'), _('You can not have two open register for the same journal'))
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'):
257 for end in open_close['end']:
258 if end[2]['pieces'] == dict_val['pieces']:
259 end[2]['number'] += dict_val['number']
261 # 'ending_details_ids': open_close['start'],
262 'starting_details_ids': open_close['end']
266 'ending_details_ids': False,
267 'starting_details_ids': False
269 res_id = super(account_cash_statement, self).create(cr, uid, vals, context=context)
270 self.write(cr, uid, [res_id], {})
273 def write(self, cr, uid, ids, vals, context=None):
275 Update redord(s) comes in {ids}, with new value comes as {vals}
276 return True on success, False otherwise
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
284 @return: True on success, False otherwise
287 super(account_cash_statement, self).write(cr, uid, ids, vals, context=context)
288 res = self._get_starting_balance(cr, uid, ids)
290 super(account_cash_statement, self).write(cr, uid, [rs], res.get(rs))
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
303 'balance_start': balance_start
306 return super(account_cash_statement, self).onchange_journal_id(cr, uid, statement_id, journal_id, context=context)
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:
316 def _user_allow(self, cr, uid, statement_id, context=None):
319 def button_open(self, cr, uid, ids, context=None):
320 """ Changes statement state to Running.
325 statement_pool = self.pool.get('account.bank.statement')
326 for statement in statement_pool.browse(cr, uid, ids, context=context):
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)))
331 if statement.name and statement.name == '/':
332 number = self.pool.get('ir.sequence').get(cr, uid, 'account.cash.statement')
338 'date': time.strftime("%Y-%m-%d %H:%M:%S"),
342 self.write(cr, uid, [statement.id], vals, context=context)
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 !'))
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)
357 'closing_date': time.strftime("%Y-%m-%d %H:%M:%S")
359 return self.write(cr, uid, ids, vals, context=context)
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)
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)
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})
378 account_cash_statement()
380 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: