~nicolariolini/openobject-italia/save_fiscalcode_data

« back to all changes in this revision

Viewing changes to account_invoice_entry_date/invoice.py

  • Committer: sergiocorato at gmail
  • Date: 2013-04-08 21:40:26 UTC
  • mto: This revision was merged to the branch mainline in revision 211.
  • Revision ID: sergiocorato@gmail.com-20130408214026-zprsmmigrdkj7pbg
[ADD] account_invoice_entry_date

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2010 ISA srl (<http://www.isa.it>).
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
import time
 
23
from osv import fields, osv, orm
 
24
from tools.translate import _
 
25
from datetime import datetime
 
26
 
 
27
class account_invoice(osv.osv):
 
28
    
 
29
    _inherit = 'account.invoice'
 
30
    _columns = {
 
31
        'registration_date':fields.date('Registration Date', states={'paid':[('readonly',True)], 'open':[('readonly',True)], 'close':[('readonly',True)]}, select=True, help="Keep empty to use the current date"),
 
32
    }
 
33
        
 
34
    def action_move_create(self, cr, uid, ids, *args):
 
35
        
 
36
        super(account_invoice,self).action_move_create(cr, uid, ids,*args)
 
37
        
 
38
        for inv in self.browse(cr, uid, ids):
 
39
            
 
40
            date_invoice=inv.date_invoice
 
41
                
 
42
            reg_date = inv.registration_date
 
43
            if not inv.registration_date :
 
44
                if not inv.date_invoice:
 
45
                    reg_date = time.strftime('%Y-%m-%d')
 
46
                else : reg_date = inv.date_invoice
 
47
 
 
48
            if date_invoice and reg_date :
 
49
                if (date_invoice > reg_date) :
 
50
                    raise osv.except_osv(_('Error date !'), _('The invoice date cannot be later than the date of registration!'))
 
51
 
 
52
            #periodo
 
53
            date_start = inv.registration_date or inv.date_invoice or time.strftime('%Y-%m-%d')
 
54
            date_stop = inv.registration_date or inv.date_invoice or time.strftime('%Y-%m-%d')
 
55
            
 
56
            period_ids = self.pool.get('account.period').search(cr, uid, [('date_start','<=',date_start),('date_stop','>=',date_stop), ('company_id', '=', inv.company_id.id)])
 
57
            if period_ids:
 
58
                period_id = period_ids[0]
 
59
            
 
60
            self.write(cr, uid, [inv.id], {'registration_date':reg_date,'period_id':period_id,})
 
61
            
 
62
            mov_date = reg_date or inv.date_invoice or time.strftime('%Y-%m-%d')           
 
63
 
 
64
            self.pool.get('account.move').write(cr, uid, [inv.move_id.id], {'state':'draft'})
 
65
 
 
66
            sql="update account_move_line set period_id="+str(period_id)+",date='"+mov_date+"' where move_id = "+str(inv.move_id.id)
 
67
                      
 
68
            cr.execute(sql)
 
69
            
 
70
            self.pool.get('account.move').write(cr, uid, [inv.move_id.id], {'period_id':period_id,'date':mov_date})           
 
71
            
 
72
            self.pool.get('account.move').write(cr, uid, [inv.move_id.id], {'state':'posted'})
 
73
            
 
74
        self._log_event(cr, uid, ids)
 
75
        return True
 
76
                
 
77
account_invoice()