~openbig/bigconsulting/addons

« back to all changes in this revision

Viewing changes to account_invoice_cash_discount/wizard/account_pay_invoice.py

  • Committer: husen
  • Date: 2010-06-29 13:37:29 UTC
  • mfrom: (29.2.2 bigconsulting)
  • Revision ID: husen@husen-laptop-20100629133729-vwboo2buswebscrx
Committed changes for milestone1 (without test fully) and few developments of milestone2

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
        'amount': fields.float('Amount paid', required=True),
59
59
        'name': fields.char('Entry Name', size=64, required=True),
60
60
        'date': fields.date('Date payment', required=True),
 
61
        'cash_residual_amount': fields.float('Residual Amount', readonly=True),
61
62
        'journal_id': fields.many2one('account.journal', 'Journal/Payment Mode', required=True),
62
63
        'period_id': fields.many2one('account.period', 'Period', required=True),
63
64
        'cash_amount':fields.float('Cash Discount Amount',),
64
65
        'account_id': fields.many2one('account.account', 'Account For Discount',),
 
66
        'cal_method_selection': fields.selection([('method_gross_methodology','Calculate Discount with Net Gross Methodology'),
 
67
                                            ('method_net_methodology','Calculate Discount with Net Methodology'),],
 
68
                                              'Method To Calculation'),
 
69
        'discount_move_ids': fields.many2many('account.move', 'account_discount_move_rel', 'discount_account_id', 'discount_move_id', 'Account Discount Moves'),
 
70
        'tax_move_ids': fields.many2many('account.move.line', 'account_tax_move_rel', 'tax_account_id', 'tax_move_id', 'Account Taxes Moves'),
65
71
        }
66
72
 
67
 
    def view_init(self, cr, uid, ids, context=None):
68
 
        invoice = self.pool.get('account.invoice').browse(cr, uid, context['active_id'], context=context)
69
 
        if invoice.state in ['draft', 'proforma2', 'cancel']:
70
 
            raise osv.except_osv(_('Error !'), _('Can not pay draft/proforma/cancel invoice.'))
71
 
        pass
72
 
 
73
73
    def _get_period(self, cr, uid, context=None):
74
74
        ids = self.pool.get('account.period').find(cr, uid, context=context)
75
75
        period_id = False
78
78
        return period_id
79
79
 
80
80
    def _get_amount(self, cr, uid, context=None):
81
 
        residual_amount = self.pool.get('account.invoice').browse(cr, uid, context['id'], context=context).residual
 
81
        obj_inv = self.pool.get('account.invoice')
 
82
        invoice =  obj_inv.browse(cr, uid, context['id'], context=context)
 
83
        discount = obj_inv._get_payment(cr, uid, [context['id']] ,invoice.residual, invoice.payment_term.id, context=context)
 
84
        residual_amount = invoice.residual - discount
82
85
        return residual_amount
83
86
    
84
87
    def on_change_ammount(self, cr, uid, ids, amount, context=None):
88
91
        res = {}
89
92
        obj_inv = self.pool.get('account.invoice')
90
93
        invoice =  obj_inv.browse(cr, uid, context['id'], context=context)
91
 
        discount = obj_inv._get_payment(cr, uid, [context['id']] , amount, invoice.payment_term.id, context=context)
92
 
        return {'value' : {'cash_amount':discount}}
 
94
        discount = obj_inv._get_payment(cr, uid, [context['id']] , invoice.residual, invoice.payment_term.id, context=context)
 
95
        diff_amount = round(invoice.residual -  (amount + discount),2)
 
96
        return {'value' : {'cash_residual_amount':diff_amount}}
 
97
    
 
98
    def on_change_cash_discount_amount(self, cr, uid, ids, discount_amount, amount, context=None):
 
99
        ###  Return discount amount
 
100
        res = {}
 
101
        obj_inv = self.pool.get('account.invoice')
 
102
        invoice =  obj_inv.browse(cr, uid, context['id'], context=context)
 
103
        diff_amount = 0.0
 
104
        diff_amount = round(invoice.residual -  (amount + discount_amount),2)
 
105
        return {'value' : {'cash_amount':discount_amount,'cash_residual_amount':diff_amount}}
93
106
    
94
107
    def _get_discount(self, cr, uid, context=None):
95
108
        obj_inv = self.pool.get('account.invoice')
116
129
          raise osv.except_osv(_('Error !'), _('Can not pay draft/proforma/cancel invoice.'))
117
130
      return res
118
131
  
 
132
    def _calculation(self, cr, uid, ids, context=None):
 
133
        invoice_obj = self.pool.get('account.invoice')
 
134
        data =  self.read(cr, uid, ids,context=context)[0]
 
135
        invoice = self.pool.get('account.invoice').browse(cr, uid, context['id'], context=context)
 
136
        tax_obj = self.pool.get('account.tax')
 
137
        invoice_tax_obj = self.pool.get("account.invoice.tax")
 
138
 
 
139
        ######## to get ref
 
140
        if invoice.type in ('in_invoice', 'in_refund'):
 
141
            ref = invoice.reference
 
142
        else:
 
143
            ref = invoice_obj._convert_ref(cr, uid, invoice.number)        
 
144
        
 
145
        #### to get direction
 
146
        types = {'out_invoice': -1, 'in_invoice': 1, 'out_refund': 1, 'in_refund': -1}
 
147
        direction = types[invoice.type]
 
148
        date=time.strftime('%Y-%m-%d')
 
149
        
 
150
        #### to get name
 
151
        name = invoice.invoice_line and invoice.invoice_line[0].name or invoice.number
 
152
        
 
153
        ##### Entry in the discount account moves and entry in the tax account moves
 
154
        
 
155
        ### Entry for taxes in the line of wizard tax
 
156
        if data.get('cash_amount',0.0)>0.0:
 
157
            lines3 = []
 
158
            tax_total_amount = 0.0    
 
159
            line_ids = []     
 
160
            for line in invoice.invoice_line:
 
161
                if line.invoice_line_tax_id:
 
162
                    for tax in tax_obj.compute(cr, uid, line.invoice_line_tax_id, data['cash_amount'], line.quantity, invoice.address_invoice_id.id, line.product_id, invoice.partner_id):
 
163
                        tax_amount = tax['amount']
 
164
                        tax_total_amount += tax['amount']
 
165
                        tax_invoice_id = invoice_tax_obj.search(cr,uid,[('invoice_id','=',invoice.id),('name','=',tax['name'])])
 
166
                        for tax_id in tax_invoice_id:
 
167
                            tax_value = invoice_tax_obj.browse(cr, uid, tax_id)
 
168
                            tax_account_id = tax_value.account_id.id
 
169
                        l3 = {
 
170
                            'debit': direction * tax_amount<0 and - direction * tax_amount,
 
171
                            'credit': direction * tax_amount>0 and  direction * tax_amount,
 
172
                            'account_id': tax_account_id,
 
173
                            'partner_id': invoice.partner_id.id,
 
174
                            'ref':ref,
 
175
                            'date': date,
 
176
                            'journal_id':data.get('journal_id',False) ,
 
177
                            'period_id':data.get('period_id',False),
 
178
                            'name':name,
 
179
                            'tax_code_id':tax_value.tax_code_id.id,
 
180
                            'tax_amount':tax_amount,
 
181
                        }
 
182
                        line_id = self.pool.get('account.move.line').create(cr,uid,l3,context=context)
 
183
                        line_ids.append(line_id)
 
184
            self.write(cr, uid, ids, {'tax_move_ids':[(6,0,line_ids)]}, context)
 
185
 
 
186
            ### Entry for discount in the line of wizard discount
 
187
            lines4 = []
 
188
            if tax_total_amount>0:
 
189
                data['cash_amount'] = (data['cash_amount'] - tax_total_amount)
 
190
                    
 
191
            l4 = {
 
192
                'debit': direction * data.get('cash_amount',0.0)<0 and - direction * data.get('cash_amount',0.0),
 
193
                'credit': direction * data.get('cash_amount',0.0)>0 and  direction * data.get('cash_amount',0.0),
 
194
                'account_id': data['account_id'],
 
195
                'partner_id': invoice.partner_id.id,
 
196
                'ref':ref,
 
197
                'date': date,
 
198
                'name':name
 
199
                }
 
200
            
 
201
            lines4.append((0, 0, l4))    
 
202
            move = {'ref': ref, 'line_id': lines4, 'journal_id': data['journal_id'], 'period_id': data['period_id'], }
 
203
            move_id = self.pool.get('account.move').create(cr, uid, move, context=context)
 
204
            self.write(cr, uid, ids, {'discount_move_ids':[(6,0,[move_id])]}, context)
 
205
        return True
 
206
    
119
207
    _defaults = {
120
208
        'date': lambda *a: time.strftime('%Y-%m-%d'),
121
209
        'period_id': _get_period,
122
210
        'amount': _get_amount,
123
211
        'cash_amount':_get_discount,
124
 
        'account_id':_get_account
 
212
        'account_id':_get_account,
 
213
        'cash_residual_amount': lambda *a:0.0,
 
214
        'cal_method_selection':lambda *a :'method_gross_methodology',
125
215
        }
126
216
 
127
217
    def wo_check(self, cr, uid, ids, context=None):
186
276
        writeoff_account_id = False
187
277
        writeoff_journal_id = False
188
278
        comment = False
189
 
 
 
279
        
190
280
        if 'write_off' in context and context['write_off'] :
191
281
            writeoff_account_id = context['write_off']['writeoff_acc_id']
192
282
            writeoff_journal_id = context['write_off']['writeoff_journal_id']
193
283
            comment = context['write_off']['comment']
194
284
 
195
 
        amount = data['amount'] 
 
285
        amount = data['amount'] + data['cash_amount']
196
286
        
197
287
        invoice = self.pool.get('account.invoice').browse(cr, uid, context['id'], context=context)
198
288
        journal = self.pool.get('account.journal').browse(cr, uid, data['journal_id'], context=context)
203
293
            amount = cur_obj.compute(cr, uid, journal.currency.id, invoice.company_id.currency_id.id, amount, context=ctx)
204
294
            currency_id = journal.currency.id
205
295
            # Put the paid amount in currency, and the currency, in the context if currency is different from company's currency
206
 
            context.update({'amount_currency':data['amount'],'currency_id':currency_id})
 
296
            context.update({'amount_currency':data['amount'] + data['cash_amount'],'currency_id':currency_id})
207
297
 
208
298
        if invoice.company_id.currency_id.id<>invoice.currency_id.id:
209
299
            ctx = {'date':data['date']}
210
300
            amount = cur_obj.compute(cr, uid, invoice.currency_id.id, invoice.company_id.currency_id.id, amount, context=ctx)
211
301
            currency_id = invoice.currency_id.id
212
302
            # Put the paid amount in currency, and the currency, in the context if currency is different from company's currency
213
 
            context.update({'amount_currency':data['amount'],'currency_id':currency_id})
 
303
            context.update({'amount_currency':data['amount'] + data['cash_amount'],'currency_id':currency_id})
214
304
 
215
305
        # Take the choosen date
216
306
        if comment:
218
308
        else:
219
309
            context.update({'date_p':data['date'],'comment':False})
220
310
 
221
 
        context.update({'account_id':data['account_id'],'cash_amount':data['cash_amount'],'amount_currency':data['amount']})        
222
 
        
 
311
        context.update({'account_id':data['account_id'],'cash_amount':data['cash_amount'],'amount_currency':data['amount'] + data['cash_amount']})        
 
312
        
 
313
        if data['discount_move_ids']:
 
314
            context.update({'discount_move_ids':data['discount_move_ids']})
 
315
        
 
316
        if data['tax_move_ids']:
 
317
            context.update({'tax_move_ids':data['tax_move_ids']})
 
318
        
 
319
        if data['cash_amount']:
 
320
            context.update({'cash_amount':data['cash_amount']})
 
321
                
223
322
        acc_id = journal.default_credit_account_id and journal.default_credit_account_id.id
224
323
        if not acc_id:
225
324
            raise osv.except_osv(_('Error !'), _('Your journal must have a default credit and debit account.'))