~ierpncd/ncdierp/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Tb25 shine period vvsgelt
# enehvv PERIOD n sanhvvgiin moduli-s engiin ajilchdiig salgaj awch bga ym
# ooroor helwel ajliin turshlaga deer ali neg kompanid tuhailsan mochlog songoh n 
# tsaashid tuhain hereglegch olon kompani ashiglah bolomjgvi boloh ayultai yum

import logging
from datetime import datetime
from dateutil.relativedelta import relativedelta
from operator import itemgetter
import time

import openerp
from openerp import SUPERUSER_ID, api
from openerp import tools
from openerp.osv import fields, osv, expression
from openerp.tools.translate import _
from openerp.tools.float_utils import float_round as round

import openerp.addons.decimal_precision as dp

_logger = logging.getLogger(__name__)


class hr_fiscalyear(osv.osv):
    _name = "hr.fiscalyear"
    _description = "HR Fiscal Year"
    _columns = {
        'name': fields.char('HR Fiscal Year', required=True),
        'code': fields.char('Code', size=25, required=True),
        'date_start': fields.date('Start Date', required=True),
        'date_stop': fields.date('End Date', required=True),
        'period_ids': fields.one2many('hr.period', 'fiscalyear_id', 'Periods'),
        # 'state': fields.selection([('draft','Open'), ('done','Closed')], 'Status', readonly=True, copy=False),
        
    }
    # _defaults = {
        # 'state': 'draft',
    # }
    _order = "date_start, id"

    def _check_duration(self, cr, uid, ids, context=None):
        obj_fy = self.browse(cr, uid, ids[0], context=context)
        if obj_fy.date_stop < obj_fy.date_start:
            return False
        return True

    _constraints = [
        (_check_duration, 'Error!\nThe start date of a fiscal year must precede its end date.', ['date_start', 'date_stop'])
    ]

    def create_period3(self, cr, uid, ids, context=None):
        return self.create_period(cr, uid, ids, context, 3)

    def create_period(self, cr, uid, ids, context=None, interval=1):
        period_obj = self.pool.get('hr.period')
        for fy in self.browse(cr, uid, ids, context=context):
            ds = datetime.strptime(fy.date_start, '%Y-%m-%d')
            #===================================================================
            # period_obj.create(cr, uid, {
            #         'name':  "%s %s" % (_('Opening Period'), ds.strftime('%Y')),
            #         'code': ds.strftime('00/%Y'),
            #         'date_start': ds,
            #         'date_stop': ds,
            #         #'special': True,
            #         'fiscalyear_id': fy.id,
            #     })
            #===================================================================
            while ds.strftime('%Y-%m-%d') < fy.date_stop:
                de = ds + relativedelta(months=interval, days=-1)

                if de.strftime('%Y-%m-%d') > fy.date_stop:
                    de = datetime.strptime(fy.date_stop, '%Y-%m-%d')

                period_obj.create(cr, uid, {
                    'name': ds.strftime('%m/%Y'),
                    'code': ds.strftime('%m/%Y'),
                    'date_start': ds.strftime('%Y-%m-%d'),
                    'date_stop': de.strftime('%Y-%m-%d'),
                    'fiscalyear_id': fy.id,
                })
                ds = ds + relativedelta(months=interval)
        return True

    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
        if args is None:
            args = []
        if operator in expression.NEGATIVE_TERM_OPERATORS:
            domain = [('code', operator, name), ('name', operator, name)]
        else:
            domain = ['|', ('code', operator, name), ('name', operator, name)]
        ids = self.search(cr, user, expression.AND([domain, args]), limit=limit, context=context)
        return self.name_get(cr, user, ids, context=context)


class hr_period(osv.osv):
    _name = "hr.period"
    _description = "HR Account period"
    _columns = {
        'name': fields.char('Period Name', required=True),
        'code': fields.char('Code', size=25),
        'date_start': fields.date('Start of Period', required=True),
        'date_stop': fields.date('End of Period', required=True),
        'fiscalyear_id': fields.many2one('hr.fiscalyear', 'HR Fiscal Year', required=True, select=True),
    }
    # _defaults = {
    #    'state': 'draft',
    # }
    _order = "date_start"
    _sql_constraints = [
        ('name_code_uniq', 'unique(name, code)', 'The name and code of the period must be unique!'),
    ]

    def _check_duration(self, cr, uid, ids, context=None):
        obj_period = self.browse(cr, uid, ids[0], context=context)
        if obj_period.date_stop < obj_period.date_start:
            return False
        return True

    _constraints = [
        (_check_duration, 'Error!\nThe duration of the Period(s) is/are invalid.', ['date_stop'])
    ]

    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
        if args is None:
            args = []
        if operator in expression.NEGATIVE_TERM_OPERATORS:
            domain = [('code', operator, name), ('name', operator, name)]
        else:
            domain = ['|', ('code', operator, name), ('name', operator, name)]
        ids = self.search(cr, user, expression.AND([domain, args]), limit=limit, context=context)
        return self.name_get(cr, user, ids, context=context)

    #===========================================================================
    # def write(self, cr, uid, ids, vals, context=None):
    #     if 'company_id' in vals:
    #         move_lines = self.pool.get('account.move.line').search(cr, uid, [('period_id', 'in', ids)])
    #         if move_lines:
    #             raise osv.except_osv(_('Warning!'), _('This journal already contains items for this period, therefore you cannot modify its company field.'))
    #     return super(account_period, self).write(cr, uid, ids, vals, context=context)
    #===========================================================================