~openbig/bigconsulting/milestone_v3

« back to all changes in this revision

Viewing changes to account_invoice_cash_discount/account_invoice_cash_discount.py

  • Committer: husen
  • Date: 2010-07-14 14:51:36 UTC
  • mfrom: (46.1.2 bigconsulting)
  • Revision ID: husen@husen-laptop-20100714145136-1yms0ix8by07o7tx
Improved payment wizard for computing cash discount based on invoice line ratio

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
        'credit_account_id': fields.many2one('account.account', 'Credit Account'),
46
46
        'debit_account_id': fields.many2one('account.account', 'Debit Account'),
47
47
        'payment_term_ids': fields.one2many('account.payment.term.line', 'cash_account_discount_id', 'Payment Term Lines'),
 
48
        'day_tolerance': fields.integer('Days Tolerance', digits=(16,6),required=True),
 
49
        'sup_discount_account_id': fields.many2one('account.account', 'Supplier Discount Account',required=True),
48
50
    }
49
51
account_cash_discount()
50
52
 
61
63
    _inherit = "account.invoice"
62
64
    
63
65
    def _get_amount(self, cr, uid, ids, resudial_amonut, payment_term, context=None):
64
 
        
65
66
        """
66
67
        This function return the Amount to paid  according to the payment term cash discount payment term lines
67
68
        """
68
 
        
69
69
        if context is None:
70
70
           context = {}
71
 
           
72
71
        tax_obj = self.pool.get('account.tax')
73
72
        invoice = self.browse(cr, uid, ids[0], context=context)
74
73
        
203
202
                    dis = 0.0
204
203
                    for discount_line in payment_term_lines.cash_discount_ids:
205
204
                        if diff_day >= dis and  diff_day <= discount_line.delay:
206
 
                            account_id = discount_line.discount_account_id.id
 
205
                            if invoice.type in ('in_invoice', 'in_refund'):
 
206
                                account_id = discount_line.sup_discount_account_id.id
 
207
                            else:    
 
208
                                account_id = discount_line.discount_account_id.id
207
209
                        dis = discount_line.delay    
208
210
                    return account_id 
209
211
            
210
212
    def pay_and_reconcile(self, cr, uid, ids, pay_amount, pay_account_id, period_id, pay_journal_id, writeoff_acc_id, writeoff_period_id, writeoff_journal_id, context=None, name=''):
211
 
        
212
213
        if context is None:
213
214
            context = {}
214
215
        #TODO check if we can use different period for payment and the writeoff line
220
221
        move_line_obj = self.pool.get('account.move.line')
221
222
        move_obj = self.pool.get('account.move')
222
223
        
223
 
        if 'amount_currency' in context and context['amount_currency']:
224
 
            amount_currency = context['amount_currency']
225
 
        else:
226
 
            amount_currency = invoice.residual
227
224
        amount_discount = 0.0    
228
225
        if invoice.payment_term:
229
226
            # Return the discount on for the payment term
230
 
            amount_discount = self._get_payment(cr, uid, ids, amount_currency, invoice.payment_term.id, context=context)
231
 
            
 
227
            amount_discount = self._get_payment(cr, uid, ids, pay_amount, invoice.payment_term.id, context=context)
 
228
 
 
229
        
232
230
        src_account_id = invoice.account_id.id
233
231
        # Take the seq as name for move
234
232
        types = {'out_invoice': -1, 'in_invoice': 1, 'out_refund': 1, 'in_refund': -1}
257
255
        
258
256
        if 'cash_amount' in context and context['cash_amount']:
259
257
            discount_amount = context['cash_amount']
260
 
                
 
258
            
261
259
        # Pay attention to the sign for both debit/credit AND amount_currency
262
260
        l1 = {
263
261
            'debit': direction * (pay_amount)>0 and direction * (pay_amount),
270
268
            'amount_currency':amount_currency and direction * amount_currency or 0.0,
271
269
        }
272
270
        
273
 
        ### if we change the amount paid and in the disocunt  then it used the changes discount 
 
271
        ### if we change the amount paid and in the disocunt  then it used the changes discount
 
272
            
274
273
        if amount_discount != discount_amount and discount_amount>0.0:
275
274
            amount_discount = discount_amount
276
275
            
291
290
        l1['name'] = name
292
291
        l2['name'] = name
293
292
        lines = [(0, 0, l1), (0, 0, l2)]
294
 
        
 
293
 
 
294
        ### When we make payment by pay invoice wizard and if found the taxes move then directly take the entry from the wizard  
295
295
        if 'tax_move_ids' in context and context['tax_move_ids']:
296
296
            move_line = context['tax_move_ids']
297
297
            for move_line_id in move_line:
337
337
                            }
338
338
                            l3['name'] = name
339
339
                            lines.append((0, 0, l3))
340
 
                    
 
340
                            
 
341
        ### When we make payment by pay invoice wizard and if found the discount moves in wizard discount move then directly take the entry from the wizard
341
342
        if 'discount_move_ids' in context and context['discount_move_ids']: 
342
 
            dis_move_id = context['discount_move_ids'][0]
343
 
            move_id = move_line_obj.search(cr, uid,[('move_id','=',dis_move_id)])
344
 
            move_line_data = move_line_obj.browse(cr, uid, move_id[0])
345
 
            l4 = {
346
 
                            'debit': move_line_data.debit,
347
 
                            'credit':move_line_data.credit,
348
 
                            'account_id': move_line_data.account_id.id,
349
 
                            'partner_id': move_line_data.partner_id.id,
350
 
                            'ref':move_line_data.ref,
351
 
                            'date': move_line_data.date,
352
 
                            'currency_id':currency_id,
353
 
                            'amount_currency':amount_currency and direction * amount_currency or 0.0,
354
 
                            'name':move_line_data.name,
355
 
                        }
356
 
            lines.append((0, 0, l4))
357
 
            
358
 
            move_obj.unlink(cr, uid, context['discount_move_ids'])
 
343
            move_line = context['discount_move_ids']
 
344
            for move_line_id in move_line:
 
345
                move_line_data = move_line_obj.browse(cr, uid,move_line_id)
 
346
                l4 = {
 
347
                        'debit': move_line_data.debit,
 
348
                        'credit':move_line_data.credit,
 
349
                        'account_id': move_line_data.account_id.id,
 
350
                        'partner_id': move_line_data.partner_id.id,
 
351
                        'ref':move_line_data.ref,
 
352
                        'date': move_line_data.date,
 
353
                        'currency_id':currency_id,
 
354
                        'amount_currency':amount_currency and direction * amount_currency or 0.0,
 
355
                        'name':move_line_data.name,
 
356
                        'tax_code_id':move_line_data.tax_code_id.id,
 
357
                        'tax_amount':move_line_data.tax_amount,
 
358
                      }
 
359
                lines.append((0, 0, l4))
 
360
                move_line_obj.unlink(cr, uid,[move_line_id])
 
361
                move_obj.unlink(cr, uid,[move_line_data.move_id.id])
359
362
        else:
360
363
            if amount_discount>0:
361
364
                if 'account_id' in context and context['account_id']:
376
379
                }
377
380
                l4['name'] = name
378
381
                lines.append((0, 0, l4))
379
 
 
 
382
        
380
383
        move = {'ref': ref, 'line_id': lines, 'journal_id': pay_journal_id, 'period_id': period_id, 'date': date}
381
384
        move_id = move_obj.create(cr, uid, move, context=context)
382
385
        
420
423
          type='many2one', relation='account.invoice', fnct_search=_invoice_search),
421
424
    }
422
425
account_move_line()
 
426
 
423
427
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
424
428