[IMP] sale purchase : fix usability
[odoo/odoo.git] / addons / account_voucher / account_voucher.py
index 0d0dfaf..a2dc425 100644 (file)
@@ -29,8 +29,9 @@ from tools.translate import _
 
 class account_move_line(osv.osv):
     _inherit = 'account.move.line'
+
     def _unreconciled(self, cr, uid, ids, prop, unknow_none, context):
-        res={}
+        res = {}
         for line in self.browse(cr, uid, ids, context=context):
             res[line.id] = line.debit - line.credit
             if line.reconcile_partial_id:
@@ -43,9 +44,11 @@ class account_move_line(osv.osv):
     _columns = {
         'amount_unreconciled': fields.function(_unreconciled, method=True, string='Unreconciled Amount'),
     }
+
 account_move_line()
 
 class account_voucher(osv.osv):
+
     def _get_type(self, cr, uid, ids, context={}):
         return context.get('type', False)
 
@@ -92,9 +95,9 @@ class account_voucher(osv.osv):
         journal_id = context.get('journal_id', False)
         if journal_id:
             journal = journal_pool.browse(cr, uid, journal_id)
-            currency_id = journal.company_id.currency_id.id
+#            currency_id = journal.company_id.currency_id.id
             if journal.currency:
-                currency_id = journal.currency.id
+                return journal.currency.id
         return False
 
     def _get_partner(self, cr, uid, context={}):
@@ -107,21 +110,20 @@ class account_voucher(osv.osv):
         return context.get('narration', False)
 
     def name_get(self, cr, uid, ids, context=None):
-        if not len(ids):
+        if not ids:
             return []
         return [(r['id'], (str("%.2f" % r['amount']) or '')) for r in self.read(cr, uid, ids, ['amount'], context, load='_classic_write')]
 
     def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
-        if not view_id and context.get('invoice_type',False):
+        if not view_id and context.get('invoice_type', False):
             mod_obj = self.pool.get('ir.model.data')
-            act_obj = self.pool.get('ir.actions.act_window')
-            if context.get('invoice_type') in ('out_invoice','out_refund'): 
+            if context.get('invoice_type') in ('out_invoice', 'out_refund'):
                 result = mod_obj._get_id(cr, uid, 'account_voucher', 'view_vendor_receipt_form')
             else:
                 result = mod_obj._get_id(cr, uid, 'account_voucher', 'view_vendor_payment_form')
             result = mod_obj.read(cr, uid, [result], ['res_id'], context=context)[0]['res_id']
             view_id = result
-        res = super(account_voucher,self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
+        res = super(account_voucher, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
         doc = etree.XML(res['arch'])
         nodes = doc.xpath("//field[@name='partner_id']")
         if context.get('type', 'sale') in ('purchase', 'payment'):
@@ -192,7 +194,7 @@ class account_voucher(osv.osv):
         'state': 'draft',
         'pay_now': 'pay_later',
         'name': '',
-        'date' : time.strftime('%Y-%m-%d'),
+        'date': time.strftime('%Y-%m-%d'),
         'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.voucher',context=c),
         'tax_id': _get_tax,
     }
@@ -224,17 +226,16 @@ class account_voucher(osv.osv):
             total_tax = 0.0
 
             if not tax[0].price_include:
-                for tax_line in tax_pool.compute_all(cr, uid, tax, voucher_amount, 1).get('taxes',[]):
-                    total_tax += tax_line.get('amount')
+                for tax_line in tax_pool.compute_all(cr, uid, tax, voucher_amount, 1).get('taxes', []):
+                    total_tax += tax_line.get('amount', 0.0)
                 total += total_tax
             else:
-                line_ids2 = []
                 for line in voucher.line_ids:
                     line_total = 0.0
                     line_tax = 0.0
 
-                    for tax_line in tax_pool.compute_all(cr, uid, tax, line.untax_amount or line.amount, 1).get('taxes',[]):
-                        line_tax += tax_line.get('amount')
+                    for tax_line in tax_pool.compute_all(cr, uid, tax, line.untax_amount or line.amount, 1).get('taxes', []):
+                        line_tax += tax_line.get('amount', 0.0)
                         line_total += tax_line.get('price_unit')
                     total_tax += line_tax
                     untax_amount = line.untax_amount or line.amount
@@ -247,21 +248,18 @@ class account_voucher(osv.osv):
         tax_pool = self.pool.get('account.tax')
         partner_pool = self.pool.get('res.partner')
         position_pool = self.pool.get('account.fiscal.position')
-        voucher_line_pool = self.pool.get('account.voucher.line')
         res = {
-            'tax_amount':False,
-            'amount':False,
+            'tax_amount': False,
+            'amount': False,
         }
-        voucher_total_tax = 0.0
         voucher_total = 0.0
         voucher_line_ids = []
 
         total = 0.0
         total_tax = 0.0
-
         for line in line_ids:
             line_amount = 0.0
-            line_amount = line[2].get('amount')
+            line_amount = line[2] and line[2].get('amount',0.0) or 0.0
             voucher_line_ids += [line[1]]
             voucher_total += line_amount
 
@@ -275,7 +273,7 @@ class account_voucher(osv.osv):
                 tax = tax_pool.browse(cr, uid, taxes)
 
             if not tax[0].price_include:
-                for tax_line in tax_pool.compute_all(cr, uid, tax, voucher_total, 1).get('taxes',[]):
+                for tax_line in tax_pool.compute_all(cr, uid, tax, voucher_total, 1).get('taxes', []):
                     total_tax += tax_line.get('amount')
                 total += total_tax
 
@@ -359,7 +357,6 @@ class account_voucher(osv.osv):
         if context is None:
             context = {}
         currency_pool = self.pool.get('res.currency')
-        move_pool = self.pool.get('account.move')
         line_pool = self.pool.get('account.voucher.line')
         move_line_pool = self.pool.get('account.move.line')
         partner_pool = self.pool.get('res.partner')
@@ -376,7 +373,7 @@ class account_voucher(osv.osv):
             return default
 
         if not partner_id and ids:
-            line_ids = line_pool.search(cr, uid, [('voucher_id','=',ids[0])])
+            line_ids = line_pool.search(cr, uid, [('voucher_id', '=', ids[0])])
             if line_ids:
                 line_pool.unlink(cr, uid, line_ids)
             return default
@@ -407,7 +404,7 @@ class account_voucher(osv.osv):
             account_type = 'receivable'
 
         if not context.get('move_line_ids', False):
-            ids = move_line_pool.search(cr, uid, [('account_id.type','=', account_type), ('reconcile_id','=', False), ('partner_id','=',partner_id)], context=context)
+            ids = move_line_pool.search(cr, uid, [('account_id.type', '=', account_type), ('reconcile_id', '=', False), ('partner_id', '=', partner_id)], context=context)
         else:
             ids = context['move_line_ids']
         ids.reverse()
@@ -463,6 +460,7 @@ class account_voucher(osv.osv):
                 default['value']['pre_line'] = 1
             elif ttype == 'receipt' and len(default['value']['line_dr_ids']) > 0:
                 default['value']['pre_line'] = 1
+
         return default
 
     def onchange_date(self, cr, user, ids, date, context={}):
@@ -473,7 +471,7 @@ class account_voucher(osv.osv):
         @return: Returns a dict which contains new values, and context
         """
         period_pool = self.pool.get('account.period')
-        pids = period_pool.search(cr, user, [('date_start','<=',date), ('date_stop','>=',date)])
+        pids = period_pool.search(cr, user, [('date_start', '<=', date), ('date_stop', '>=', date)])
         if not pids:
             return {}
         return {
@@ -503,6 +501,8 @@ class account_voucher(osv.osv):
     def proforma_voucher(self, cr, uid, ids, context=None):
         self.action_move_line_create(cr, uid, ids, context=context)
         return True
+        #return {'type' : 'ir.actions.act_window_close' }
+        
 
     def action_cancel_draft(self, cr, uid, ids, context={}):
         wf_service = netsvc.LocalService("workflow")
@@ -514,7 +514,6 @@ class account_voucher(osv.osv):
     def cancel_voucher(self, cr, uid, ids, context={}):
         reconcile_pool = self.pool.get('account.move.reconcile')
         move_pool = self.pool.get('account.move')
-        voucher_line_pool = self.pool.get('account.voucher.line')
 
         for voucher in self.browse(cr, uid, ids):
             recs = []
@@ -574,10 +573,8 @@ class account_voucher(osv.osv):
             context = {}
         move_pool = self.pool.get('account.move')
         move_line_pool = self.pool.get('account.move.line')
-        analytic_pool = self.pool.get('account.analytic.line')
         currency_pool = self.pool.get('res.currency')
-        invoice_pool = self.pool.get('account.invoice')
-        bank_st_line_obj = self.pool.get('account.bank.statement.line')
+        tax_obj = self.pool.get('account.tax')
         for inv in self.browse(cr, uid, ids):
             if inv.move_id:
                 continue
@@ -589,19 +586,14 @@ class account_voucher(osv.osv):
                 raise osv.except_osv(_('Error !'), _('Please define a sequence on the journal !'))
 
             move = {
-                'name' : name,
+                'name': name,
                 'journal_id': inv.journal_id.id,
-                'narration' : inv.narration,
+                'narration': inv.narration,
                 'date':inv.date,
                 'ref':inv.reference,
                 'period_id': inv.period_id and inv.period_id.id or False
             }
             move_id = move_pool.create(cr, uid, move)
-            line_bank_ids = bank_st_line_obj.search(cr, uid, [('voucher_id', '=', inv.id)], context=context)
-            if line_bank_ids:
-                bank_st_line_obj.write(cr, uid, line_bank_ids, {
-            'move_ids': [(4, move_id, False)]
-            })
 
             #create the first line manually
             company_currency = inv.journal_id.company_id.currency_id.id
@@ -621,18 +613,18 @@ class account_voucher(osv.osv):
                 credit = 0.0
 
             move_line = {
-                'name':inv.name or '/',
-                'debit':debit,
-                'credit':credit,
-                'account_id':inv.account_id.id,
-                'move_id':move_id ,
-                'journal_id':inv.journal_id.id,
-                'period_id':inv.period_id.id,
-                'partner_id':inv.partner_id.id,
-                'currency_id':inv.currency_id.id,
-                'amount_currency':inv.amount,
-                'date':inv.date,
-                'date_maturity':inv.date_due
+                'name': inv.name or '/',
+                'debit': debit,
+                'credit': credit,
+                'account_id': inv.account_id.id,
+                'move_id': move_id,
+                'journal_id': inv.journal_id.id,
+                'period_id': inv.period_id.id,
+                'partner_id': inv.partner_id.id,
+                'currency_id': inv.currency_id.id,
+                'amount_currency': inv.amount,
+                'date': inv.date,
+                'date_maturity': inv.date_due
             }
 
             if (debit == 0.0 or credit == 0.0 or debit+credit > 0) and (debit > 0.0 or credit > 0.0):
@@ -649,21 +641,20 @@ class account_voucher(osv.osv):
                 if not line.amount:
                     continue
                 amount = currency_pool.compute(cr, uid, inv.currency_id.id, company_currency, line.amount)
-
                 move_line = {
-                    'journal_id':inv.journal_id.id,
-                    'period_id':inv.period_id.id,
-                    'name':line.name and line.name or '/',
-                    'account_id':line.account_id.id,
-                    'move_id':move_id,
-                    'partner_id':inv.partner_id.id,
-                    'currency_id':inv.currency_id.id,
-                    'amount_currency':line.amount,
-                    'analytic_account_id':line.account_analytic_id and line.account_analytic_id.id or False,
-                    'quantity':1,
-                    'credit':0.0,
-                    'debit':0.0,
-                    'date':inv.date
+                    'journal_id': inv.journal_id.id,
+                    'period_id': inv.period_id.id,
+                    'name': line.name and line.name or '/',
+                    'account_id': line.account_id.id,
+                    'move_id': move_id,
+                    'partner_id': inv.partner_id.id,
+                    'currency_id': inv.currency_id.id,
+                    'amount_currency': line.amount,
+                    'analytic_account_id': line.account_analytic_id and line.account_analytic_id.id or False,
+                    'quantity': 1,
+                    'credit': 0.0,
+                    'debit': 0.0,
+                    'date': inv.date
                 }
                 if amount < 0:
                     amount = -amount
@@ -681,8 +672,12 @@ class account_voucher(osv.osv):
 
                 if inv.tax_id and inv.type in ('sale', 'purchase'):
                     move_line.update({
-                        'account_tax_id':inv.tax_id.id,
+                        'account_tax_id': inv.tax_id.id,
                     })
+                if move_line.get('account_tax_id', False):
+                    tax_data = tax_obj.browse(cr, uid, [move_line['account_tax_id']], context=context)[0]
+                    if not (tax_data.base_code_id and tax_data.tax_code_id):
+                        raise osv.except_osv(_('No Account Base Code and Account Tax Code!'),_("You have to configure account base code and account tax code on the '%s' tax!") % (tax_data.name))
                 master_line = move_line_pool.create(cr, uid, move_line)
                 if line.move_line_id.id:
                     rec_ids = [master_line, line.move_line_id.id]
@@ -691,13 +686,13 @@ class account_voucher(osv.osv):
             if not self.pool.get('res.currency').is_zero(cr, uid, inv.currency_id, line_total):
                 diff = line_total
                 move_line = {
-                    'name':name,
-                    'account_id':False,
-                    'move_id':move_id ,
-                    'partner_id':inv.partner_id.id,
-                    'date':inv.date,
-                    'credit':diff>0 and diff or 0.0,
-                    'debit':diff<0 and -diff or 0.0,
+                    'name': name,
+                    'account_id': False,
+                    'move_id': move_id,
+                    'partner_id': inv.partner_id.id,
+                    'date': inv.date,
+                    'credit': diff > 0 and diff or 0.0,
+                    'debit': diff < 0 and -diff or 0.0,
                 }
                 account_id = False
                 if inv.type in ('sale', 'receipt'):
@@ -706,7 +701,8 @@ class account_voucher(osv.osv):
                 else:
                     account_id = inv.partner_id.property_account_payable.id
                 move_line['account_id'] = account_id
-                move_line_id = move_line_pool.create(cr, uid, move_line)
+
+                move_line_pool.create(cr, uid, move_line)
 
             self.write(cr, uid, [inv.id], {
                 'move_id': move_id,
@@ -721,12 +717,12 @@ class account_voucher(osv.osv):
 
     def copy(self, cr, uid, id, default={}, context=None):
         default.update({
-            'state':'draft',
-            'number':False,
-            'move_id':False,
-            'line_cr_ids':False,
-            'line_dr_ids':False,
-            'reference':False
+            'state': 'draft',
+            'number': False,
+            'move_id': False,
+            'line_cr_ids': False,
+            'line_dr_ids': False,
+            'reference': False
         })
         if 'date' not in default:
             default['date'] = time.strftime('%Y-%m-%d')
@@ -779,7 +775,7 @@ class account_voucher_line(osv.osv):
         'company_id': fields.related('voucher_id','company_id', relation='res.company', string='Company', store=True),
     }
     _defaults = {
-        'name': lambda *a: ''
+        'name': ''
     }
 
     def onchange_move_line_id(self, cr, user, ids, move_line_id, context={}):
@@ -796,13 +792,10 @@ class account_voucher_line(osv.osv):
         move_line_pool = self.pool.get('account.move.line')
         if move_line_id:
             move_line = move_line_pool.browse(cr, user, move_line_id, context=context)
-            move_id = move_line.move_id.id
             if move_line.credit:
-                ttype='dr'
-                amount = move_line.credit
+                ttype = 'dr'
             else:
-                ttype='cr'
-                amount = move_line.debit
+                ttype = 'cr'
             account_id = move_line.account_id.id
             res.update({
                 'account_id':account_id,
@@ -857,24 +850,31 @@ class account_bank_statement(osv.osv):
     _inherit = 'account.bank.statement'
 
     def button_cancel(self, cr, uid, ids, context=None):
-        done = []
         for st in self.browse(cr, uid, ids, context):
             voucher_ids = []
             for line in st.line_ids:
                 if line.voucher_id:
                     voucher_ids.append(line.voucher_id.id)
             self.pool.get('account.voucher').cancel_voucher(cr, uid, voucher_ids, context)
-            self.pool.get('account.voucher').unlink(cr, uid, voucher_ids, context)
         return super(account_bank_statement, self).button_cancel(cr, uid, ids, context=context)
 
     def create_move_from_st_line(self, cr, uid, st_line_id, company_currency_id, next_number, context=None):
         voucher_obj = self.pool.get('account.voucher')
         wf_service = netsvc.LocalService("workflow")
-        st_line = self.pool.get('account.bank.statement.line').browse(cr, uid, st_line_id, context=context)
+        bank_st_line_obj = self.pool.get('account.bank.statement.line')
+        st_line = bank_st_line_obj.browse(cr, uid, st_line_id, context=context)
         if st_line.voucher_id:
             voucher_obj.write(cr, uid, [st_line.voucher_id.id], {'number': next_number}, context=context)
+            if st_line.voucher_id.state == 'cancel':
+                voucher_obj.action_cancel_draft(cr, uid, [st_line.voucher_id.id], context=context)
             wf_service.trg_validate(uid, 'account.voucher', st_line.voucher_id.id, 'proforma_voucher', cr)
-            return self.pool.get('account.move.line').write(cr, uid, [x.id for x in st_line.voucher_id.move_ids], {'statement_id': st_line.statement_id.id}, context=context)
+
+            v = voucher_obj.browse(cr, uid, st_line.voucher_id.id, context=context)
+            bank_st_line_obj.write(cr, uid, [st_line_id], {
+                'move_ids': [(4, v.move_id.id, False)]
+            })
+
+            return self.pool.get('account.move.line').write(cr, uid, [x.id for x in v.move_ids], {'statement_id': st_line.statement_id.id}, context=context)
         return super(account_bank_statement, self).create_move_from_st_line(cr, uid, st_line.id, company_currency_id, next_number, context=context)
 
 account_bank_statement()
@@ -886,9 +886,8 @@ class account_bank_statement_line(osv.osv):
         if not ids:
             return {}
 
-        res_currency_obj = self.pool.get('res.currency')
         res = {}
-        company_currency_id = False
+#        company_currency_id = False
         for line in self.browse(cursor, user, ids, context=context):
 #            if not company_currency_id:
 #                company_currency_id = line.company_id.id
@@ -918,3 +917,5 @@ class account_bank_statement_line(osv.osv):
         return super(account_bank_statement_line, self).unlink(cr, uid, ids, context=context)
 
 account_bank_statement_line()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:=======