File: service.py

package info (click to toggle)
tryton-modules-project-revenue 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 200 kB
  • ctags: 80
  • sloc: python: 415; xml: 143; makefile: 2
file content (161 lines) | stat: -rw-r--r-- 6,384 bytes parent folder | download
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#This file is part of Tryton.  The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
"Service"
from trytond.model import ModelView, ModelSQL, fields
from trytond.model.cacheable import Cacheable
from trytond.pyson import Eval


class Employee(ModelSQL, ModelView, Cacheable):
    _name = 'company.employee'

    cost_price = fields.Function(fields.Numeric('Cost Price',
        digits=(16, Eval('currency_digits', 2)), depends=['currency_digits'],
        help="Hourly cost price for this Employee"), 'get_cost_price')
    cost_prices = fields.One2Many('company.employee_cost_price', 'employee',
            'Cost Prices', help="List of hourly cost price over time")
    currency_digits = fields.Function(fields.Integer('Currency Digits',
        on_change_with=['company']), 'get_currency_digits')

    def get_cost_price(self, cursor, user, ids, name, context=None):
        '''
        Return the cost price at the date given in the context or the
        current date
        '''

        res = {}
        if context is None:
            context = {}
        ctx_date = context.get('date', None)

        for employee_id in ids:
            res[employee_id] = self.compute_cost_price(cursor, user,
                    employee_id, ctx_date, context=context)
        return res

    def compute_cost_price(self, cursor, user, employee_id, date=None,
            context=None):
        date_obj = self.pool.get('ir.date')
        cost_price_obj = self.pool.get('company.employee_cost_price')

        # Get from cache employee costs or fetch them from the db
        employee_costs = self.get(cursor, employee_id)
        if employee_costs is None:
            cost_price_ids = cost_price_obj.search(cursor, user, [
                    ('employee', '=', employee_id),
                    ], order=[('date', 'ASC')])
            cost_prices = cost_price_obj.browse(
                cursor, user, cost_price_ids, context=context)

            employee_costs = []
            for cost_price in cost_prices:
                employee_costs.append(
                    (cost_price.date, cost_price.cost_price))
            self.add(cursor, employee_id, employee_costs)

        if date is None:
            date = date_obj.today(cursor, user, context=context)
        # compute the cost price for the given date
        cost = 0
        if employee_costs and date >= employee_costs[0][0]:
            for edate, ecost in employee_costs:
                if date >= edate:
                    cost = ecost
                    break
        return cost

    def get_currency_digits(self, cursor, user, ids, name, context=None):
        res = {}
        for employee in self.browse(cursor, user, ids, context=context):
            res[employee.id] = employee.company.currency.digits
        return res

    def on_change_with_currency_digits(self, cursor, user, vals, context=None):
        company_obj = self.pool.get('company.company')
        if vals.get('company'):
            company = company_obj.browse(cursor, user, vals['company'],
                    context=context)
            return company.currency.digits
        return 2

    def default_currency_digits(self, cursor, user, context=None):
        company_obj = self.pool.get('company.company')
        if context is None:
            context = {}
        company = None
        if context.get('company'):
            company = company_obj.browse(cursor, user, context['company'],
                    context=context)
            return company.currency.digits
        return 2

Employee()


class EmployeeCostPrice(ModelSQL, ModelView):
    'Employee Cost Price'
    _name = 'company.employee_cost_price'
    _description = __doc__
    _rec_name = 'date'
    date = fields.Date('Date', required=True, select=1)
    cost_price = fields.Numeric('Cost Price',
            digits=(16, Eval('currency_digits', 2)),
            required=True, depends=['currency_digits'],
            help="Hourly cost price")
    employee = fields.Many2One('company.employee', 'Employee')
    currency_digits = fields.Function(fields.Integer('Currency Digits',
        on_change_with=['employee']), 'get_currency_digits')

    def __init__(self):
        super(EmployeeCostPrice, self).__init__()
        self._sql_constraints = [
            ('date_cost_price_uniq', 'UNIQUE(date, cost_price)',
                'A employee can only have one cost price by date!'),
        ]
        self._order.insert(0, ('date', 'DESC'))

    def default_date(self, cursor, user, context=None):
        date_obj = self.pool.get('ir.date')
        return date_obj.today(cursor, user, context=context)

    def delete(self, cursor, user, ids, context=None):
        self.pool.get('company.employee').clear(cursor)
        return super(EmployeeCostPrice , self).delete(cursor, user, ids,
                context=context)

    def create(self, cursor, user, vals, context=None):
        self.pool.get('company.employee').clear(cursor)
        return super(EmployeeCostPrice , self).create(cursor, user, vals,
                context=context)

    def write(self, cursor, user, ids, vals, context=None):
        self.pool.get('company.employee').clear(cursor)
        return super(EmployeeCostPrice , self).write(cursor, user, ids, vals,
                context=context)

    def get_currency_digits(self, cursor, user, ids, name, context=None):
        res = {}
        for costprice in self.browse(cursor, user, ids, context=context):
            res[costprice.id] = costprice.employee.company.currency.digits
        return res

    def on_change_with_currency_digits(self, cursor, user, vals, context=None):
        company_obj = self.pool.get('company.employee')
        if vals.get('employee'):
            employee = employee_obj.browse(cursor, user, vals['employee'],
                    context=context)
            return employee.company.currency.digits
        return 2

    def default_currency_digits(self, cursor, user, context=None):
        company_obj = self.pool.get('company.company')
        if context is None:
            context = {}
        company = None
        if context.get('company'):
            company = company_obj.browse(cursor, user, context['company'],
                    context=context)
            return company.currency.digits
        return 2

EmployeeCostPrice()