~openobject-italia-core-devs/openobject-italia/italian-addons-6.1

« back to all changes in this revision

Viewing changes to account_central_journal/wizard/central_journal_report.py

[MERGE] lp:~l-turchetti/openobject-italia/add_account_central_journal

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) 2012 ISA s.r.l. (<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 datetime import datetime, date, timedelta
 
24
from osv import osv, fields
 
25
from tools.translate import _
 
26
 
 
27
class central_journal_report(osv.osv_memory):
 
28
    
 
29
    _name = 'wizard.central.journal.report'
 
30
    _description = 'Printing parameters of the Center Journal'
 
31
 
 
32
    def _get_fiscal_years(self, cr, uid, context=None):
 
33
        fiscalyear_obj = self.pool.get('account.fiscalyear')
 
34
        fiscalyear_ids = fiscalyear_obj.search(cr, uid, [], order="id desc")
 
35
        fiscalyears = []
 
36
        for account_fiscalyear in fiscalyear_obj.browse(cr,uid,fiscalyear_ids) :
 
37
            fiscalyears.append((account_fiscalyear.id, account_fiscalyear.name))
 
38
        return fiscalyears
 
39
 
 
40
    def _get_account_fiscalyear_data(self, cr, uid, ids, fiscalyear_id):
 
41
        fiscalyear_obj = self.pool.get('account.fiscalyear')
 
42
        fiscalyear_ids=fiscalyear_obj.search(cr,uid,[('id','=',fiscalyear_id),])
 
43
        fiscalyear_data=fiscalyear_obj.browse(cr,uid,fiscalyear_ids)[0]
 
44
        return fiscalyear_data
 
45
 
 
46
    def _dates_control(self, str_date_start, str_date_end):
 
47
        today_date = date.today()
 
48
        date_start = datetime.strptime(str_date_start,"%Y-%m-%d").date() 
 
49
        date_stop = datetime.strptime(str_date_end,"%Y-%m-%d").date() 
 
50
        if date_start > date_stop:
 
51
            raise osv.except_osv(_('Wrong dates !'), _("The end date must be greater than the initial date."))
 
52
            return False
 
53
        if date_stop > today_date:
 
54
            raise osv.except_osv(_('Wrong dates !'), _("The end date can not be greater than today's date."))
 
55
            return False
 
56
        return True
 
57
 
 
58
    def _get_report_datas(self, cr, uid, ids, context={}):
 
59
        wizard_form_datas = self.read(cr, uid, ids)[0]
 
60
        datas = {
 
61
            'ids': [],
 
62
            'model': 'account.move.line',
 
63
            'form': wizard_form_datas,
 
64
        }
 
65
        return datas
 
66
 
 
67
    _columns = {
 
68
        'date_move_line_from': fields.date('From date', required=True,),
 
69
        'date_move_line_from_view': fields.date('From date'),
 
70
        'date_move_line_to': fields.date('to date', required=True),
 
71
        'fiscalyear': fields.selection(_get_fiscal_years, 'Fiscal Year', required=True),
 
72
        'print_state': fields.selection([('draft','Draft'),('print','Ready for printing'),('printed','Printed')],'State',readonly=True),
 
73
    }
 
74
        
 
75
    def onchange_fiscalyear(self, cr, uid, ids, fiscalyear_id=False, context=None):
 
76
        print_state = 'draft'
 
77
        date_move_line_from = date_move_line_from_view = False
 
78
        date_move_line_to = False
 
79
        if fiscalyear_id:
 
80
            print_state = 'print'
 
81
            fiscalyear_data = self._get_account_fiscalyear_data(cr, uid, ids, fiscalyear_id)
 
82
            #set values
 
83
            today_date = date.today()
 
84
            date_start = datetime.strptime(fiscalyear_data.date_start,"%Y-%m-%d").date() 
 
85
            date_stop = datetime.strptime(fiscalyear_data.date_stop,"%Y-%m-%d").date() 
 
86
            #set date_move_line_from
 
87
            if fiscalyear_data.date_last_print:
 
88
                date_last_print = datetime.strptime(fiscalyear_data.date_last_print,"%Y-%m-%d").date()
 
89
                date_move_line_from = date_move_line_from_view = (date_last_print+timedelta(days=1)).__str__()
 
90
                if date_last_print == date_stop:
 
91
                    date_move_line_from = date_move_line_from_view = date_start.__str__()
 
92
                    print_state = 'printed'
 
93
            else:
 
94
                date_move_line_from = date_move_line_from_view = date_start.__str__()
 
95
            #set date_move_line_to
 
96
            if today_date > date_stop:
 
97
                date_move_line_to = date_stop.__str__()
 
98
            else:
 
99
                date_move_line_to = (today_date-timedelta(days=1)).__str__()
 
100
 
 
101
        return {'value': {
 
102
                    'date_move_line_from': date_move_line_from,
 
103
                    'date_move_line_from_view': date_move_line_from_view,
 
104
                    'date_move_line_to': date_move_line_to,
 
105
                    'print_state': print_state,
 
106
                    }
 
107
                }
 
108
        
 
109
    def print_report(self, cr, uid, ids, context={}):
 
110
        datas = self._get_report_datas(cr, uid, ids, context)
 
111
        if self._dates_control(datas['form']['date_move_line_from'],datas['form']['date_move_line_to']) == False:
 
112
            return False
 
113
        datas['print_final'] = False
 
114
        return {
 
115
            'type': 'ir.actions.report.xml',
 
116
            'report_name': 'central_journal_report',
 
117
            'datas': datas,
 
118
        }
 
119
 
 
120
    def print_report_final(self, cr, uid, ids, context={}):
 
121
        datas = self._get_report_datas(cr, uid, ids, context)
 
122
        if self._dates_control(datas['form']['date_move_line_from'],datas['form']['date_move_line_to']) == False:
 
123
            return False
 
124
        datas['print_final'] = True
 
125
        return {
 
126
            'type': 'ir.actions.report.xml',
 
127
            'report_name': 'central_journal_report',
 
128
            'datas': datas,
 
129
        }
 
130
        
 
131
    _defaults = {
 
132
        'print_state': 'draft',
 
133
    }
 
134
 
 
135
central_journal_report()