This repository has been archived by the owner on Dec 31, 2022. It is now read-only.
/
sale.py
425 lines (368 loc) · 15.3 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from decimal import Decimal
from trytond import backend
from trytond.i18n import gettext
from trytond.model import ModelView, Unique, Workflow, fields
from trytond.modules.product import price_digits, round_price
from trytond.modules.sale.exceptions import SaleConfirmError
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval, If
from trytond.transaction import Transaction
sale_shipment_cost_method = fields.Selection(
'get_sale_shipment_cost_methods', "Sale Shipment Cost Method")
def get_sale_methods(field_name):
@classmethod
def func(cls):
pool = Pool()
Sale = pool.get('sale.sale')
return Sale.fields_get([field_name])[field_name]['selection']
return func
class Configuration(metaclass=PoolMeta):
__name__ = 'sale.configuration'
sale_shipment_cost_method = fields.MultiValue(sale_shipment_cost_method)
get_sale_shipment_cost_methods = get_sale_methods('shipment_cost_method')
@classmethod
def multivalue_model(cls, field):
pool = Pool()
if field == 'sale_shipment_cost_method':
return pool.get('sale.configuration.sale_method')
return super(Configuration, cls).multivalue_model(field)
@classmethod
def default_sale_shipment_cost_method(cls, **pattern):
return cls.multivalue_model(
'sale_shipment_cost_method').default_sale_shipment_cost_method()
class ConfigurationSaleMethod(metaclass=PoolMeta):
__name__ = 'sale.configuration.sale_method'
sale_shipment_cost_method = sale_shipment_cost_method
get_sale_shipment_cost_methods = get_sale_methods('shipment_cost_method')
@classmethod
def __register__(cls, module_name):
exist = backend.TableHandler.table_exist(cls._table)
if exist:
table = cls.__table_handler__(module_name)
exist &= table.column_exist('sale_shipment_cost_method')
super(ConfigurationSaleMethod, cls).__register__(module_name)
if not exist:
cls._migrate_property([], [], [])
@classmethod
def _migrate_property(cls, field_names, value_names, fields):
field_names.append('sale_shipment_cost_method')
value_names.append('sale_shipment_cost_method')
super(ConfigurationSaleMethod, cls)._migrate_property(
field_names, value_names, fields)
@classmethod
def default_sale_shipment_cost_method(cls):
return 'order'
class Sale(metaclass=PoolMeta):
__name__ = 'sale.sale'
carrier = fields.Many2One('carrier', 'Carrier',
domain=[
If(Eval('state') == 'draft', [
('carrier_product.salable', '=', True),
('id', 'in', Eval('available_carriers', [])),
],
[]),
],
states={
'readonly': Eval('state') != 'draft',
},
context={
'company': Eval('company', -1),
},
depends={'company'})
available_carriers = fields.Function(
fields.Many2Many('carrier', None, None, 'Available Carriers'),
'on_change_with_available_carriers')
shipment_cost_method = fields.Selection([
(None, "None"),
('order', 'On Order'),
('shipment', 'On Shipment'),
], 'Shipment Cost Method', states={
'readonly': Eval('state') != 'draft',
})
@classmethod
def __register__(cls, module):
super().__register__(module)
table_h = cls.__table_handler__(module)
# Migration from 5.8: remove required on shipment_cost_method
table_h.not_null_action('shipment_cost_method', 'remove')
@classmethod
def default_shipment_cost_method(cls, **pattern):
Config = Pool().get('sale.configuration')
config = Config(1)
return config.get_multivalue(
'sale_shipment_cost_method', **pattern)
@fields.depends('company')
def on_change_company(self):
super().on_change_company()
self.shipment_cost_method = self.default_shipment_cost_method(
company=self.company.id if self.company else None)
@fields.depends('warehouse', 'shipment_address')
def _get_carrier_selection_pattern(self):
pattern = {}
if (self.warehouse
and self.warehouse.address
and self.warehouse.address.country):
pattern['from_country'] = self.warehouse.address.country.id
else:
pattern['from_country'] = None
if self.shipment_address and self.shipment_address.country:
pattern['to_country'] = self.shipment_address.country.id
else:
pattern['to_country'] = None
return pattern
@fields.depends(methods=['_get_carrier_selection_pattern'])
def on_change_with_available_carriers(self, name=None):
pool = Pool()
CarrierSelection = pool.get('carrier.selection')
pattern = self._get_carrier_selection_pattern()
carriers = CarrierSelection.get_carriers(pattern)
return [c.id for c in carriers]
@fields.depends('carrier', methods=['on_change_with_available_carriers'])
def on_change_party(self):
super(Sale, self).on_change_party()
self.available_carriers = self.on_change_with_available_carriers()
if self.available_carriers and (not self.carrier
or self.carrier not in self.available_carriers):
self.carrier = self.available_carriers[0]
elif not self.available_carriers:
self.carrier = None
if self.party and self.party.sale_shipment_cost_method:
self.shipment_cost_method = self.party.sale_shipment_cost_method
else:
self.shipment_cost_method = self.default_shipment_cost_method()
@fields.depends('carrier', methods=['on_change_with_available_carriers'])
def on_change_shipment_party(self):
super(Sale, self).on_change_shipment_party()
self.available_carriers = self.on_change_with_available_carriers()
if self.available_carriers and (not self.carrier
or self.carrier not in self.available_carriers):
self.carrier = self.available_carriers[0]
elif not self.available_carriers:
self.carrier = None
@fields.depends('carrier', methods=['on_change_with_available_carriers'])
def on_change_shipment_address(self):
try:
super_on_change = super(Sale, self).on_change_shipment_address
except AttributeError:
pass
else:
super_on_change()
self.available_carriers = self.on_change_with_available_carriers()
if self.available_carriers and (not self.carrier
or self.carrier not in self.available_carriers):
self.carrier = self.available_carriers[0]
elif not self.available_carriers:
self.carrier = None
@classmethod
@ModelView.button
@Workflow.transition('quotation')
def quote(cls, sales):
pool = Pool()
Line = pool.get('sale.line')
removed = []
for sale in sales:
removed.extend(sale.set_shipment_cost())
Line.delete(removed)
cls.save(sales)
super(Sale, cls).quote(sales)
@classmethod
@ModelView.button
@Workflow.transition('confirmed')
def confirm(cls, sales):
for sale in sales:
if sale.carrier and sale.carrier not in sale.available_carriers:
raise SaleConfirmError(
gettext('sale_shipment_cost.msg_sale_invalid_carrier',
sale=sale.rec_name,
carrier=sale.carrier.rec_name))
super(Sale, cls).confirm(sales)
@classmethod
@ModelView.button
def process(cls, sales):
with Transaction().set_context(_shipment_cost_invoiced=list()):
super().process(sales)
def _get_carrier_context(self):
return {}
def compute_shipment_cost(self):
pool = Pool()
Date = pool.get('ir.date')
Currency = pool.get('currency.currency')
Line = pool.get('sale.line')
stockable = any(
line.quantity >= 0 for line in self.lines
if line.type == 'line'
and line.product
and line.product.type in Line.get_move_product_types())
if self.carrier and stockable:
with Transaction().set_context(self._get_carrier_context()):
cost, currency_id = self.carrier.get_sale_price()
if cost is not None:
with Transaction().set_context(company=self.company.id):
today = Date.today()
date = self.sale_date or today
with Transaction().set_context(date=date):
return Currency.compute(
Currency(currency_id),
cost, self.currency, round=False)
def set_shipment_cost(self):
cost = None
if self.shipment_cost_method:
cost = self.compute_shipment_cost()
removed = []
unit_price = None
lines = list(self.lines or [])
for line in self.lines:
if line.type == 'line' and line.shipment_cost is not None:
if line.shipment_cost == cost:
unit_price = line.unit_price * Decimal(str(line.quantity))
lines.remove(line)
removed.append(line)
if cost is not None:
lines.append(self.get_shipment_cost_line(
cost, unit_price=unit_price))
self.lines = lines
return removed
def get_shipment_cost_line(self, cost, unit_price=None):
pool = Pool()
SaleLine = pool.get('sale.line')
product = self.carrier.carrier_product
sequence = None
if self.lines:
last_line = self.lines[-1]
if last_line.sequence is not None:
sequence = last_line.sequence + 1
shipment_cost = round_price(cost)
cost_line = SaleLine(
sale=self,
sequence=sequence,
type='line',
product=product,
quantity=1, # XXX
unit=product.sale_uom,
shipment_cost=shipment_cost,
)
cost_line.on_change_product()
if unit_price is not None:
cost_line.unit_price = round_price(unit_price)
else:
cost_line.unit_price = round_price(cost)
if not cost_line.unit_price:
cost_line.quantity = 0
cost_line.amount = cost_line.on_change_with_amount()
return cost_line
@property
def _shipment_grouping_fields(self):
return super()._shipment_grouping_fields + ('cost_method', 'carrier')
@property
def shipment_cost_amount(self):
cost = Decimal(0)
for line in self.lines:
if line.type == 'line' and line.shipment_cost is not None:
cost += line.amount
return cost
def _get_shipment_sale(self, Shipment, key):
pool = Pool()
ShipmentOut = pool.get('stock.shipment.out')
shipment = super()._get_shipment_sale(Shipment, key)
if isinstance(shipment, ShipmentOut):
shipment.cost_method = self.shipment_cost_method
shipment.carrier = self.carrier
return shipment
class Line(metaclass=PoolMeta):
__name__ = 'sale.line'
shipment_cost = fields.Numeric('Shipment Cost', digits=price_digits)
@classmethod
def __setup__(cls):
super().__setup__()
t = cls.__table__()
cls._sql_constraints += [
('sale_shipment_cost_unique',
Unique(t, t.sale, t.shipment_cost),
'sale_shipment_cost.msg_sale_shipment_cost_unique'),
]
# shipment_cost is needed to compute the unit_price
cls.unit_price.depends.add('shipment_cost')
@fields.depends('shipment_cost', 'unit_price')
def compute_unit_price(self):
unit_price = super().compute_unit_price()
if self.shipment_cost is not None:
unit_price = self.unit_price
return unit_price
def get_invoice_line(self):
context = Transaction().context
shipment_cost_invoiced = context.get('_shipment_cost_invoiced')
lines = super().get_invoice_line()
if (self.shipment_cost
and shipment_cost_invoiced is not None):
for shipment in self.sale.shipments:
if (shipment.state == 'done'
and shipment.id not in shipment_cost_invoiced):
invoice_line = shipment.get_cost_invoice_line(
self.sale._get_invoice_sale())
if invoice_line:
invoice_line.origin = self
lines.append(invoice_line)
shipment_cost_invoiced.append(shipment.id)
return lines
def _get_invoice_line_quantity(self):
quantity = super()._get_invoice_line_quantity()
if self.shipment_cost:
if self.sale.shipment_cost_method == 'shipment':
quantity = 0
elif (self.sale.shipment_cost_method == 'order'
and self.sale.invoice_method == 'shipment'):
shipments = self.sale.shipments
if (not shipments
or all(s.state != 'done' for s in shipments)):
quantity = 0
return quantity
def _get_invoiced_quantity(self):
quantity = super()._get_invoiced_quantity()
if self.shipment_cost:
if self.sale.shipment_cost_method == 'shipment':
quantity = 0
return quantity
class ReturnSale(metaclass=PoolMeta):
__name__ = 'sale.return_sale'
def do_return_(self, action):
pool = Pool()
Sale = pool.get('sale.sale')
SaleLine = pool.get('sale.line')
action, data = super(ReturnSale, self).do_return_(action)
return_sales = Sale.browse(data['res_id'])
lines = []
for sale in return_sales:
for line in sale.lines:
# Do not consider return shipment cost as a shipment cost
if line.shipment_cost:
line.shipment_cost = None
lines.append(line)
SaleLine.save(lines)
return action, data
class Promotion(metaclass=PoolMeta):
__name__ = 'sale.promotion'
amount_shipment_cost_included = fields.Boolean(
"Amount with Shipment Cost Included",
states={
'invisible': ~Eval('amount'),
})
@classmethod
def default_amount_shipment_cost_included(cls):
return False
def get_context_formula(self, sale_line):
context = super(Promotion, self).get_context_formula(sale_line)
if sale_line and sale_line.shipment_cost:
context['names']['unit_price'] = sale_line.shipment_cost
return context
def get_sale_amount(self, sale):
amount = super().get_sale_amount(sale)
if not self.amount_shipment_cost_included:
amount -= sum(l.amount for l in sale.lines if l.shipment_cost)
if not self.untaxed_amount:
amount -= sum(
v['amount']
for l in sale.lines if l.shipment_cost
for v in l._get_taxes().values())
return amount