forked from OCA/sale-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sale.py
210 lines (193 loc) · 8.79 KB
/
sale.py
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# -*- encoding: utf-8 -*-
##############################################################################
#
# Sale Start End Dates module for Odoo
# Copyright (C) 2014-2015 Akretion (http://www.akretion.com)
# @author Alexis de Lattre <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp import models, fields, api, _
from openerp.exceptions import ValidationError
from dateutil.relativedelta import relativedelta
class SaleOrder(models.Model):
_inherit = 'sale.order'
default_start_date = fields.Date(string='Default Start Date')
default_end_date = fields.Date(string='Default End Date')
@api.one
@api.constrains('default_start_date', 'default_end_date')
def _check_default_start_end_dates(self):
if (
self.default_start_date and
self.default_end_date and
self.default_start_date > self.default_end_date):
raise ValidationError(
_("Default Start Date should be before or be the "
"same as Default End Date for sale order %s")
% self.name)
@api.onchange('default_start_date')
def default_start_date_change(self):
if (
self.default_start_date and
self.default_end_date and
self.default_start_date > self.default_end_date):
self.default_end_date = self.default_start_date
@api.onchange('default_end_date')
def default_end_date_change(self):
if (
self.default_start_date and
self.default_end_date and
self.default_start_date > self.default_end_date):
self.default_start_date = self.default_end_date
class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'
@api.one
@api.depends('start_date', 'end_date')
def _compute_number_of_days(self):
if self.start_date and self.end_date:
self.number_of_days = (
fields.Date.from_string(self.end_date) -
fields.Date.from_string(self.start_date)).days + 1
else:
self.number_of_days = 0
start_date = fields.Date(
string='Start Date', readonly=True,
states={'draft': [('readonly', False)]})
end_date = fields.Date(
string='End Date', readonly=True,
states={'draft': [('readonly', False)]})
number_of_days = fields.Integer(string='Number of Days')
must_have_dates = fields.Boolean(
string='Must Have Start and End Dates', readonly=True,
states={'draft': [('readonly', False)]})
@api.one
@api.constrains('start_date', 'end_date', 'number_of_days')
def _check_start_end_dates(self):
if self.product_id and self.must_have_dates:
if not self.end_date:
raise ValidationError(
_("Missing End Date for sale order line with "
"Product '%s'.")
% (self.product_id.name))
if not self.start_date:
raise ValidationError(
_("Missing Start Date for sale order line with "
"Product '%s'.")
% (self.product_id.name))
if not self.number_of_days:
raise ValidationError(
_("Missing number of days for sale order line with "
"Product '%s'.")
% (self.product_id.name))
if self.start_date > self.end_date:
raise ValidationError(
_("Start Date should be before or be the same as "
"End Date for sale order line with Product '%s'.")
% (self.product_id.name))
if self.number_of_days < 0:
raise ValidationError(
_("On sale order line with Product '%s', the "
"number of days is negative ; this is not allowed.")
% (self.product_id.name))
days_delta = (
fields.Date.from_string(self.end_date) -
fields.Date.from_string(self.start_date)).days + 1
if self.number_of_days != days_delta:
raise ValidationError(
_("On the sale order line with Product '%s', "
"there are %d days between the Start Date (%s) and "
"the End Date (%s), but the number of days field "
"has a value of %d days.")
% (self.product_id.name, days_delta, self.start_date,
self.end_date, self.number_of_days))
@api.model
def _prepare_order_line_invoice_line(self, line, account_id=False):
res = super(SaleOrderLine, self)._prepare_order_line_invoice_line(
line, account_id=account_id)
if line.must_have_dates:
res.update({
'start_date': line.start_date,
'end_date': line.end_date,
})
return res
@api.onchange('end_date')
def end_date_change(self):
if self.end_date:
if self.start_date and self.start_date > self.end_date:
self.start_date = self.end_date
if self.start_date:
number_of_days = (
fields.Date.from_string(self.end_date) -
fields.Date.from_string(self.start_date)).days + 1
if self.number_of_days != number_of_days:
self.number_of_days = number_of_days
@api.onchange('start_date')
def start_date_change(self):
if self.start_date:
if self.end_date and self.start_date > self.end_date:
self.end_date = self.start_date
if self.end_date:
number_of_days = (
fields.Date.from_string(self.end_date) -
fields.Date.from_string(self.start_date)).days + 1
if self.number_of_days != number_of_days:
self.number_of_days = number_of_days
@api.onchange('number_of_days')
def number_of_days_change(self):
if self.number_of_days:
if self.start_date:
end_date_dt = fields.Date.from_string(self.start_date) +\
relativedelta(days=self.number_of_days - 1)
end_date = fields.Date.to_string(end_date_dt)
if self.end_date != end_date:
self.end_date = end_date
elif self.end_date:
self.start_date = fields.Date.from_string(self.end_date) -\
relativedelta(days=self.number_of_days - 1)
@api.multi
def product_id_change(
self, pricelist, product, qty=0,
uom=False, qty_uos=0, uos=False, name='', partner_id=False,
lang=False, update_tax=True, date_order=False, packaging=False,
fiscal_position=False, flag=False):
res = super(SaleOrderLine, self).product_id_change(
pricelist, product, qty=qty, uom=uom,
qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id,
lang=lang, update_tax=update_tax, date_order=date_order,
packaging=packaging, fiscal_position=fiscal_position,
flag=flag)
if not product:
res['value'].update({
'must_have_dates': False,
'start_date': False,
'end_date': False,
})
else:
product_o = self.env['product.product'].browse(product)
if product_o.must_have_dates:
res['value']['must_have_dates'] = True
if self.env.context.get('default_start_date'):
res['value']['start_date'] = self.env.context.get(
'default_start_date')
if self.env.context.get('default_end_date'):
res['value']['end_date'] = self.env.context.get(
'default_end_date')
else:
res['value'].update({
'must_have_dates': False,
'start_date': False,
'end_date': False,
})
return res