-
Notifications
You must be signed in to change notification settings - Fork 10
/
sale.py
639 lines (548 loc) · 22.6 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
# -*- coding: utf-8 -*-
"""
sale.py
:copyright: (c) 2014 by Openlabs Technologies & Consulting (P) Limited
:license: BSD, see LICENSE for more details.
"""
from datetime import datetime, timedelta
from sql import Literal
from trytond.model import fields
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
from trytond.rpc import RPC
from trytond.model import ModelView
from trytond.pyson import Eval, Bool, And
from trytond import backend
from math import floor
from decimal import Decimal
__metaclass__ = PoolMeta
__all__ = ["Sale", "SaleChannel", "SaleLine"]
class SaleConfiguration:
'Sale Configuration'
__name__ = 'sale.configuration'
round_down_account = fields.Property(
fields.Many2One('account.account', 'Round Down Account', required=True)
)
class SaleChannel:
__name__ = 'sale.channel'
anonymous_customer = fields.Many2One(
'party.party', "Anonymous Customer", states={
'required': Eval('source') == 'pos',
'invisible': Eval('source') != 'pos',
}
)
# The warehouse from which backorders will be shipped.
#
# TODO: Default to channel's warehouse.
backorder_warehouse = fields.Many2One(
'stock.location', "Warehouse (Backorder)",
domain=[('type', '=', 'warehouse')], states={
'required': Eval('source') == 'pos',
'invisible': Eval('source') != 'pos',
}
)
delivery_mode = fields.Selection([
('pick_up', 'Pick Up'),
('ship', 'Ship'),
], 'Delivery Mode', required=True)
@classmethod
def get_source(cls):
"""
Override the get_source method to add 'pos' as a source in channel
"""
sources = super(SaleChannel, cls).get_source()
sources.append(('pos', 'POS'))
return sources
@staticmethod
def default_delivery_mode():
return 'ship'
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
cursor = Transaction().cursor
table = TableHandler(cursor, cls, module_name)
# Remove not null constraint from anonymous_customer
table.not_null_action('anonymous_customer', action='remove')
# Remove not null constraint from ship_to_warehouse
table.not_null_action('ship_to_warehouse', action='remove')
# Rename ship_to_warehouse to backorder_warehouse
table.column_rename('ship_to_warehouse', 'backorder_warehouse')
super(SaleChannel, cls).__register__(module_name)
class Sale:
__name__ = "sale.sale"
@staticmethod
def default_party():
User = Pool().get('res.user')
user = User(Transaction().user)
if (
'use_anonymous_customer' not in Transaction().context
): # pragma: no cover
return
if user.current_channel and user.current_channel.anonymous_customer:
return user.current_channel.anonymous_customer.id
@classmethod
def __setup__(cls):
super(Sale, cls).__setup__()
cls.__rpc__.update({
'pos_add_product': RPC(instantiate=0, readonly=False),
'pos_serialize': RPC(instantiate=0, readonly=True),
'get_recent_sales': RPC(readonly=True),
})
cls.lines.context = {
'current_channel': Eval('channel'),
}
cls._buttons.update({
'round_down_total': {
'invisible': ~Eval('state').in_(['draft', 'quotation']),
},
})
@classmethod
@ModelView.button
def round_down_total(cls, records):
'''
Round down total order price and add remaining amount as new sale line
'''
SaleLine = Pool().get('sale.line')
sale_lines = []
for record in records:
# Check if there's already a roundoff line, remove and create new
# if there is.
round_off_line = SaleLine.search([
('sale', '=', record.id),
('is_round_off', '=', True),
])
if round_off_line:
SaleLine.delete(round_off_line)
floored_total = floor(record.total_amount)
amount_diff = record.total_amount - Decimal(floored_total)
sale_lines.append({
'sale': record,
'is_round_off': True,
'type': 'line',
'quantity': -1,
'unit_price': amount_diff,
'description': 'Round Off'
})
SaleLine.create(
[line for line in sale_lines if line['unit_price']]
)
@classmethod
def get_recent_sales(cls):
"""
Return sales of current channel, which were made within last 5 days
and are in draft state. Sort by write_date or create_date of Sale and
sale lines.
"""
SaleLine = Pool().get('sale.line')
context = Transaction().context
date = (
datetime.now() - timedelta(days=5)
).strftime('%Y-%m-%d %H:%M:%S')
current_channel = context['current_channel']
SaleTable = cls.__table__()
SaleLineTable = SaleLine.__table__()
cursor = Transaction().cursor
query = SaleTable.join(
SaleLineTable,
condition=(SaleTable.id == SaleLineTable.sale)
).select(
SaleTable.id,
where=(
(SaleTable.channel == Literal(current_channel)) &
(SaleTable.state.in_([
'draft', 'quotation', 'confirmed', 'processing'
])) &
(
(SaleTable.write_date >= Literal(date)) |
(SaleTable.create_date >= Literal(date))
)
),
order_by=(
SaleLineTable.write_date.desc,
SaleLineTable.create_date.desc,
SaleTable.write_date.desc,
SaleTable.create_date.desc
)
)
cursor.execute(*query)
ids = [x[0] for x in cursor.fetchall()]
return [cls(id).serialize('recent_sales') for id in ids]
def pos_find_sale_line_domain(self):
"""
Return domain to find existing sale line for given product.
"""
domain = [
('sale', '=', self.id),
]
context = Transaction().context
if 'product' in context:
domain.append(('product', '=', context['product']))
if 'delivery_mode' in context:
domain.append(('delivery_mode', '=', context['delivery_mode']))
return domain
def pos_add_product(self, product_ids, quantity, unit_price=None):
"""
Add product to sale from POS.
This method is for POS, to add multiple products to cart in single call
"""
AccountTax = Pool().get('account.tax')
SaleLine = Pool().get('sale.line')
updated_lines = []
for product_id in product_ids:
Transaction().set_context(product=product_id)
try:
if 'sale_line' in Transaction().context:
sale_line = SaleLine(Transaction().context.get('sale_line'))
else:
sale_line, = SaleLine.search(
self.pos_find_sale_line_domain()
)
except ValueError:
sale_line = None
delivery_mode = Transaction().context.get(
'delivery_mode', 'pick_up'
)
if sale_line:
values = {
'product': sale_line.product.id,
'_parent_sale.currency': self.currency.id,
'_parent_sale.party': self.party.id,
'_parent_sale.price_list': (
self.price_list.id if self.price_list else None
),
'_parent_sale.sale_date': self.sale_date,
'_parent_sale.channel': self.channel,
'_parent_sale.shipment_address': self.shipment_address,
'warehouse': self.warehouse,
'_parent_sale.warehouse': self.warehouse,
'unit': sale_line.unit.id,
'quantity': quantity,
'type': 'line',
'delivery_mode': delivery_mode,
}
# Update the values by triggering an onchange which should
# fill missing vals
values.update(SaleLine(**values).on_change_quantity())
values.update(SaleLine(**values).on_change_delivery_mode())
values['unit_price'] = Decimal(unit_price) if unit_price else\
sale_line.unit_price
new_values = {}
for key, value in values.iteritems():
if '.' in key:
continue
if key == 'taxes':
# Difficult to reach here unless taxes change when
# quantities change.
continue # pragma: no cover
new_values[key] = value
SaleLine.write([sale_line], new_values)
else:
values = {
'product': product_id,
'_parent_sale.currency': self.currency.id,
'_parent_sale.party': self.party.id,
'_parent_sale.price_list': (
self.price_list.id if self.price_list else None
),
'_parent_sale.sale_date': self.sale_date,
'_parent_sale.channel': self.channel,
'_parent_sale.shipment_address': self.shipment_address,
'warehouse': self.warehouse,
'_parent_sale.warehouse': self.warehouse,
'sale': self.id,
'type': 'line',
'quantity': quantity,
'unit': None,
'description': None,
'delivery_mode': delivery_mode,
}
values.update(SaleLine(**values).on_change_product())
values.update(SaleLine(**values).on_change_quantity())
values.update(SaleLine(**values).on_change_delivery_mode())
new_values = {}
for key, value in values.iteritems():
if '.' in key:
continue
if key == 'taxes':
continue
new_values[key] = value
sale_line = SaleLine.create([new_values])[0]
updated_lines.append(sale_line.id)
if 'taxes' in values:
sale_line.taxes = AccountTax.browse(values['taxes'])
sale_line.save()
# Now that the sale line is built, return a serializable response
# which ensures that the client does not have to call again.
res = {
'sale': self.serialize('pos'),
'updated_lines': updated_lines,
}
return res
def pos_serialize(self):
"""
Serialize sale for pos
"""
return self.serialize('pos')
def serialize(self, purpose=None):
"""
Serialize with information needed for POS
"""
if purpose == 'pos':
invoice_address = self.invoice_address or \
self.party.address_get('invoice')
shipment_address = self.shipment_address or \
self.party.address_get('delivery')
return {
'party': self.party.id,
'total_amount': self.total_amount,
'untaxed_amount': self.untaxed_amount,
'tax_amount': self.tax_amount,
'comment': self.comment,
'state': self.state,
'invoice_address': invoice_address and
invoice_address.serialize(purpose),
'shipment_address': shipment_address and
shipment_address.serialize(purpose),
'lines': [line.serialize(purpose) for line in self.lines],
'reference': self.reference,
}
elif purpose == 'recent_sales':
return {
'id': self.id,
'party': {
'id': self.party.id,
'name': self.party.name,
},
'total_amount': self.total_amount,
'create_date': self.create_date,
'state': self.state,
'reference': self.reference,
}
elif hasattr(super(Sale, self), 'serialize'):
return super(SaleLine, self).serialize(purpose) # pragma: no cover
def _group_shipment_key(self, moves, move):
"""
This method returns a key based on which Tryton creates shipments
for a given sale order. By default Tryton uses the planned_date for the
delivery and warehouse to separate shipments.
We use the same functionality to split the shipments for items being
picked up and delivered. This is later used to auto proceed and finish
the shipping of the picked up products.
:param moves: A list of all moves
:param move: move is a tuple of line id and a move
"""
SaleLine = Pool().get('sale.line')
line = SaleLine(move[0])
rv = super(Sale, self)._group_shipment_key(moves, move)
return rv + (('delivery_mode', line.delivery_mode),)
def create_shipment(self, shipment_type):
"""
This method creates the shipments for the given sale order.
This implementation inspects the order lines to look for lines which
are expected to be picked up instantly and the shipment created for
pick_up is automatically processed all the way through.
"""
pool = Pool()
shipments = super(Sale, self).create_shipment(shipment_type)
if self.shipment_method == 'manual':
# shipments will be None but for future return the value
# returned by the super function
return shipments
if not shipments:
return shipments
picked_up_shipments = filter(
lambda s: s.delivery_mode == 'pick_up', shipments
)
if shipment_type == 'out':
Shipment = pool.get('stock.shipment.out')
with Transaction().set_user(0, set_context=True):
# If we are going to "process" a shipment, it is
# equivalent to sale being processed.
#
# Doing this here helps in an edge case where the
# sale total is 0. When a shipment is "Done" it
# tries to recprocess the order state, but
# usually this happens after sale is in
# processing state. Since we push things through in the
# next few lines, that call happens when the sale is in
# confirmed state and there is no transition from
# Confirmed to Done.
self.state = 'processing'
self.save()
# Assign and complete the shipments
if not Shipment.assign_try(picked_up_shipments):
draft_moves = filter(
lambda m: m.state == 'draft',
[m for s in picked_up_shipments for m in s.inventory_moves] # noqa
)
products_out_of_stock = [
m.product.rec_name for m in draft_moves
]
self.raise_user_error(
"Order cannot be processed as the following items are "
"out of stock:\n" + "\n".join(products_out_of_stock)
)
Shipment.pack(picked_up_shipments)
Shipment.done(picked_up_shipments)
elif shipment_type == 'return':
Shipment = pool.get('stock.shipment.out.return')
with Transaction().set_user(0, set_context=True):
Shipment.receive(picked_up_shipments)
Shipment.done(picked_up_shipments)
# Finally return the value the super function returned, but after
# reloading the active records.
return Shipment.browse(map(int, shipments))
def create_invoice(self, invoice_type):
"""
Sale creates draft invoices. But if the invoices are created from
shipments, then they should be automatically opened
"""
Invoice = Pool().get('account.invoice')
invoice = super(Sale, self).create_invoice(invoice_type)
if not invoice:
return invoice
if self.invoice_method == 'shipment' and invoice_type == 'out_invoice':
# Invoices created from shipment can be automatically opened
# for payment.
Invoice.post([invoice])
return invoice
class SaleLine:
__name__ = 'sale.line'
is_round_off = fields.Boolean('Round Off', readonly=True)
delivery_mode = fields.Selection([
(None, ''),
('pick_up', 'Pick Up'),
('ship', 'Ship'),
], 'Delivery Mode', states={
'invisible': Eval('type') != 'line',
'required': And(
Eval('type') == 'line',
Bool(Eval('product_type_is_goods'))
)
}, depends=['type', 'product_type_is_goods'])
product_type_is_goods = fields.Function(
fields.Boolean('Product Type is Goods?'), 'get_product_type_is_goods'
)
@classmethod
def __register__(cls, module_name):
super(SaleLine, cls).__register__(module_name)
TableHandler = backend.get('TableHandler')
cursor = Transaction().cursor
table = TableHandler(cursor, cls, module_name)
table.not_null_action('delivery_mode', action='remove')
@classmethod
def __setup__(cls):
super(SaleLine, cls).__setup__()
# Hide product and unit fields.
cls.product.states['invisible'] |= Bool(Eval('is_round_off'))
cls.unit.states['invisible'] |= Bool(Eval('is_round_off'))
cls.delivery_mode.states['invisible'] |= Bool(Eval('is_round_off'))
cls.product.depends.insert(0, 'is_round_off')
cls.unit.depends.insert(0, 'is_round_off')
@fields.depends(
'product', 'unit', 'quantity', '_parent_sale.party',
'_parent_sale.currency', '_parent_sale.sale_date', 'delivery_mode',
'_parent_sale.channel', '_parent_sale.shipment_address',
'warehouse', '_parent_sale.warehouse'
)
def on_change_delivery_mode(self):
"""
This method can be overridden by downstream modules to make changes
according to delivery mode. Like change taxes according to delivery
mode.
"""
return {}
@staticmethod
def default_is_round_off():
return False
def get_invoice_line(self, invoice_type):
SaleConfiguration = Pool().get('sale.configuration')
InvoiceLine = Pool().get('account.invoice.line')
if not self.is_round_off:
return super(SaleLine, self).get_invoice_line(invoice_type)
# The line is a round off line and apply the logic here.
# Check if the invoice line already exists for the sale line
# If yes, then no line needs to be created
# XXX: What if the invoice was cancelled ?
if self.invoice_lines:
return []
round_down_account = SaleConfiguration(1).round_down_account
if not round_down_account:
self.raise_user_error(
'''Set round down account from Sale Configuration to
add round off line'''
)
invoice_line = InvoiceLine()
invoice_line.origin = self
invoice_line.account = round_down_account
invoice_line.unit_price = self.unit_price
invoice_line.description = self.description
# For positive sales transactions (where the order is effectively
# a positive total), the round_down is applied on out_invoice
# and if overall order total is negative, then the round_down is
# tied to credit note.
if self.sale.total_amount >= Decimal('0'):
if invoice_type == 'out_credit_note':
# positive order looking for credit note
return []
invoice_line.quantity = self.quantity
else:
if invoice_type == 'out_invoice':
# negative order looking for invoice
return []
invoice_line.quantity = -self.quantity
return [invoice_line]
@staticmethod
def default_delivery_mode():
Channel = Pool().get('sale.channel')
User = Pool().get('res.user')
user = User(Transaction().user)
sale_channel = user.current_channel
if Transaction().context.get('current_channel'):
sale_channel = Channel(
Transaction().context.get('current_channel')
)
return sale_channel and sale_channel.delivery_mode
def get_warehouse(self, name):
"""
Return the warehouse from the channel for orders being picked up and the
backorder warehouse for orders with ship.
"""
if self.sale.channel.source == 'pos' and \
self.delivery_mode == 'ship' and \
self.sale.channel.backorder_warehouse:
return self.sale.channel.backorder_warehouse.id
return super(SaleLine, self).get_warehouse(name)
def serialize(self, purpose=None):
"""
Serialize for the purpose of POS
"""
if purpose == 'pos':
return {
'id': self.id,
'description': self.description,
'product': self.product and {
'id': self.product.id,
'code': self.product.code,
'rec_name': self.product.rec_name,
'default_image': self.product.default_image and
self.product.default_image.id,
},
'unit': self.unit and {
'id': self.unit.id,
'rec_name': self.unit.rec_name,
},
'unit_price': self.unit_price,
'quantity': self.quantity,
'amount': self.amount,
'delivery_mode': self.delivery_mode
}
elif hasattr(super(SaleLine, self), 'serialize'):
return super(SaleLine, self).serialize(purpose) # pragma: no cover
def get_product_type_is_goods(self, name):
"""
Return True if product is of type goods
"""
if self.product and self.product.type == 'goods':
return True
return False