[MERGE]
[odoo/odoo.git] / addons / account / account_cash_statement.py
index 454290c..0776732 100644 (file)
@@ -23,7 +23,6 @@
 import time
 
 from osv import osv, fields
-from decimal import Decimal
 from tools.translate import _
 import decimal_precision as dp
 
@@ -53,14 +52,14 @@ class account_cashbox_line(osv.osv):
         @param number:
         """
         sub = pieces * number
-        return {'value':{'subtotal': sub or 0.0}}
+        return {'value': {'subtotal': sub or 0.0}}
 
     _columns = {
         'pieces': fields.float('Values', digits_compute=dp.get_precision('Account')),
         'number': fields.integer('Number'),
         'subtotal': fields.function(_sub_total, method=True, string='Sub Total', type='float', digits_compute=dp.get_precision('Account')),
-        'starting_id': fields.many2one('account.bank.statement',ondelete='cascade'),
-        'ending_id': fields.many2one('account.bank.statement',ondelete='cascade'),
+        'starting_id': fields.many2one('account.bank.statement', ondelete='cascade'),
+        'ending_id': fields.many2one('account.bank.statement', ondelete='cascade'),
      }
 
 account_cashbox_line()
@@ -86,7 +85,7 @@ class account_cash_statement(osv.osv):
             for line in statement.starting_details_ids:
                 amount_total+= line.pieces * line.number
             res[statement.id] = {
-                'balance_start':amount_total
+                'balance_start': amount_total
             }
         return res
 
@@ -166,13 +165,13 @@ class account_cash_statement(osv.osv):
         curr = [1, 2, 5, 10, 20, 50, 100, 500]
         for rs in curr:
             dct = {
-                'pieces':rs,
-                'number':0
+                'pieces': rs,
+                'number': 0
             }
             res.append(dct)
-        journal_ids = self.pool.get('account.journal').search(cr, uid, [('type','=','cash')], context=context)
+        journal_ids = self.pool.get('account.journal').search(cr, uid, [('type', '=', 'cash')], context=context)
         if journal_ids:
-            results = self.search(cr, uid, [('journal_id','in',journal_ids),('state','=','confirm')],context=context)
+            results = self.search(cr, uid, [('journal_id', 'in', journal_ids),('state', '=', 'confirm')], context=context)
             if results:
                 cash_st = self.browse(cr, uid, results, context)[0]
                 for cash_line in cash_st.ending_details_ids:
@@ -186,8 +185,8 @@ class account_cash_statement(osv.osv):
         curr = [1, 2, 5, 10, 20, 50, 100, 500]
         for rs in curr:
             dct = {
-                'pieces':rs,
-                'number':0
+                'pieces': rs,
+                'number': 0
             }
             res.append(dct)
         return res
@@ -197,10 +196,10 @@ class account_cash_statement(osv.osv):
         curr = [1, 2, 5, 10, 20, 50, 100, 500]
         for rs in curr:
             dct = {
-                'pieces':rs,
-                'number':0
+                'pieces': rs,
+                'number': 0
             }
-            res.append((0,0,dct))
+            res.append((0, 0, dct))
         return res
 
     def _get_cash_open_close_box_lines(self, cr, uid, context={}):
@@ -210,41 +209,41 @@ class account_cash_statement(osv.osv):
         starting_details = self._get_cash_open_box_lines(cr, uid, context)
         ending_details = self._get_default_cash_close_box_lines(cr, uid, context)
         for start in starting_details:
-            start_l.append((0,0,start))
+            start_l.append((0, 0, start))
         for end in ending_details:
-            end_l.append((0,0,end))
+            end_l.append((0, 0, end))
         res['start'] = start_l
         res['end'] = end_l
         return res
 
     _columns = {
-        '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"),
+        '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"),
         'state': fields.selection(
             [('draft', 'Draft'),
             ('confirm', 'Closed'),
             ('open','Open')], 'State', required=True, states={'confirm': [('readonly', True)]}, readonly="1"),
-        'total_entry_encoding':fields.function(_get_sum_entry_encoding, method=True, store=True, string="Cash Transaction", help="Total cash transactions"),
-        'closing_date':fields.datetime("Closed On"),
+        'total_entry_encoding': fields.function(_get_sum_entry_encoding, method=True, store=True, string="Cash Transaction", help="Total cash transactions"),
+        'closing_date': fields.datetime("Closed On"),
         'balance_end': fields.function(_end_balance, method=True, store=True, string='Balance', help="Closing balance based on Starting Balance and Cash Transactions"),
         'balance_end_cash': fields.function(_balance_end_cash, method=True, store=True, string='Balance', help="Closing balance based on cashBox"),
         'starting_details_ids': fields.one2many('account.cashbox.line', 'starting_id', string='Opening Cashbox'),
         'ending_details_ids': fields.one2many('account.cashbox.line', 'ending_id', string='Closing Cashbox'),
         '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'),
-        'user_id':fields.many2one('res.users', 'Responsible', required=False),
+        'user_id': fields.many2one('res.users', 'Responsible', required=False),
     }
     _defaults = {
         'state': 'draft',
         'date': time.strftime("%Y-%m-%d %H:%M:%S"),
         'user_id': lambda self, cr, uid, context=None: uid,
-        'starting_details_ids':_get_cash_open_box_lines,
-        'ending_details_ids':_get_default_cash_close_box_lines
+        'starting_details_ids': _get_cash_open_box_lines,
+        'ending_details_ids': _get_default_cash_close_box_lines
      }
 
     def create(self, cr, uid, vals, context=None):
         if 'journal_id' not in vals:
             raise osv.except_osv('Error', _('You cannot create a bank or cash register without a journal!'))
         sql = [
-                ('journal_id', '=', vals['journal_id']),
+                ('journal_id', '=', vals.get('journal_id', False)),
                 ('state', '=', 'open')
         ]
         open_jrnl = self.search(cr, uid, sql)
@@ -253,20 +252,20 @@ class account_cash_statement(osv.osv):
 
         if self.pool.get('account.journal').browse(cr, uid, vals['journal_id']).type == 'cash':
             open_close = self._get_cash_open_close_box_lines(cr, uid, context)
-            if vals.get('starting_details_ids',False):
+            if vals.get('starting_details_ids', False):
                 for start in vals.get('starting_details_ids'):
                     dict_val = start[2]
                     for end in open_close['end']:
                        if end[2]['pieces'] == dict_val['pieces']:
                            end[2]['number'] += dict_val['number']
             vals.update({
-                'ending_details_ids':open_close['start'],
-                'starting_details_ids':open_close['end']
+#                'ending_details_ids': open_close['start'],
+                'starting_details_ids': open_close['end']
             })
         else:
             vals.update({
-                'ending_details_ids':False,
-                'starting_details_ids':False
+                'ending_details_ids': False,
+                'starting_details_ids': False
             })
         res_id = super(account_cash_statement, self).create(cr, uid, vals, context=context)
         self.write(cr, uid, [res_id], {})
@@ -298,8 +297,6 @@ class account_cash_statement(osv.osv):
         @param journal_id: Changed journal_id
         @return:  Dictionary of changed values
         """
-        cash_pool = self.pool.get('account.cashbox.line')
-        statement_pool = self.pool.get('account.bank.statement')
         res = {}
         balance_start = 0.0
 
@@ -319,34 +316,35 @@ class account_cash_statement(osv.osv):
         else:
             return True
 
-    def _user_allow(self, cr, uid, ids, statement, context={}):
+    def _user_allow(self, cr, uid, statement_id, context=None):
         return True
 
     def button_open(self, cr, uid, ids, context=None):
-
         """ Changes statement state to Running.
         @return: True
         """
-        cash_pool = self.pool.get('account.cashbox.line')
+        if context is None:
+            context = {}
         statement_pool = self.pool.get('account.bank.statement')
-        statement = statement_pool.browse(cr, uid, ids[0])
-        vals = {}
+        for statement in statement_pool.browse(cr, uid, ids, context=context):
+            vals = {}
 
-        if not self._user_allow(cr, uid, ids, statement, context={}):
-            raise osv.except_osv(_('Error !'), _('User %s does not have rights to access %s journal !' % (statement.user_id.name, statement.journal_id.name)))
+            if not self._user_allow(cr, uid, statement.id, context=context):
+                raise osv.except_osv(_('Error !'), _('User %s does not have rights to access %s journal !' % (statement.user_id.name, statement.journal_id.name)))
 
-        if statement.name and statement.name == '/':
-            number = self.pool.get('ir.sequence').get(cr, uid, 'account.cash.statement')
-            vals.update({
-                'name': number
-            })
+            if statement.name and statement.name == '/':
+                number = self.pool.get('ir.sequence').get(cr, uid, 'account.cash.statement')
+                vals.update({
+                    'name': number
+                })
 
-        vals.update({
-            'date':time.strftime("%Y-%m-%d %H:%M:%S"),
-            'state':'open',
+            vals.update({
+                'date': time.strftime("%Y-%m-%d %H:%M:%S"),
+                'state': 'open',
 
-        })
-        return self.write(cr, uid, ids, vals)
+            })
+            self.write(cr, uid, [statement.id], vals)
+        return True
 
     def balance_check(self, cr, uid, cash_id, journal_type='bank', context=None):
         if journal_type == 'bank':
@@ -371,14 +369,14 @@ class account_cash_statement(osv.osv):
 
     def button_confirm_cash(self, cr, uid, ids, context=None):
         super(account_cash_statement, self).button_confirm_bank(cr, uid, ids, context=context)
-        return self.write(cr, uid, ids, {'closing_date':time.strftime("%Y-%m-%d %H:%M:%S")}, context=context)
+        return self.write(cr, uid, ids, {'closing_date': time.strftime("%Y-%m-%d %H:%M:%S")}, context=context)
 
     def button_cancel(self, cr, uid, ids, context=None):
         cash_box_line_pool = self.pool.get('account.cashbox.line')
         super(account_cash_statement, self).button_cancel(cr, uid, ids, context=context)
         for st in self.browse(cr, uid, ids, context):
             for end in st.ending_details_ids:
-                cash_box_line_pool.write(cr, uid, [end.id], {'number':0})
+                cash_box_line_pool.write(cr, uid, [end.id], {'number': 0})
         return True
 
 account_cash_statement()