forked from trytonus/trytond-sale-channel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel.py
1017 lines (869 loc) · 32.6 KB
/
channel.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
sale_channel.py
"""
from datetime import datetime
from trytond.pool import PoolMeta, Pool
from trytond.transaction import Transaction
from trytond.pyson import Eval, Bool
from trytond.model import ModelView, fields, ModelSQL, Unique
from dateutil.relativedelta import relativedelta
from trytond.modules.company.company import TIMEZONES
__metaclass__ = PoolMeta
__all__ = [
'SaleChannel', 'ReadUser', 'WriteUser', 'ChannelException',
'ChannelOrderState', 'TaxMapping', 'ChannelPaymentGateway'
]
STATES = {
'readonly': ~Eval('active', True),
}
DEPENDS = ['active']
PRODUCT_STATES = {
'invisible': ~(Eval('source') != 'manual'),
'required': Eval('source') != 'manual',
}
INVISIBLE_IF_MANUAL = {
'invisible': Eval('source') == 'manual',
}
class SaleChannel(ModelSQL, ModelView):
"""
Sale Channel model
"""
__name__ = 'sale.channel'
name = fields.Char(
'Name', required=True, select=True, states=STATES, depends=DEPENDS
)
code = fields.Char(
'Code', select=True, states={'readonly': Eval('code', True)},
depends=['code']
)
active = fields.Boolean('Active', select=True)
company = fields.Many2One(
'company.company', 'Company', required=True, select=True,
states=STATES, depends=DEPENDS
)
address = fields.Many2One(
'party.address', 'Address', domain=[
('party', '=', Eval('company_party')),
], depends=['company_party']
)
source = fields.Selection(
'get_source', 'Source', required=True, states=STATES, depends=DEPENDS
)
read_users = fields.Many2Many(
'sale.channel-read-res.user', 'channel', 'user', 'Read Users'
)
create_users = fields.Many2Many(
'sale.channel-write-res.user', 'channel', 'user', 'Create Users'
)
warehouse = fields.Many2One(
'stock.location', "Warehouse", required=True,
domain=[('type', '=', 'warehouse')]
)
invoice_method = fields.Selection(
[
('manual', 'Manual'),
('order', 'On Order Processed'),
('shipment', 'On Shipment Sent')
], 'Invoice Method', required=True
)
shipment_method = fields.Selection(
[
('manual', 'Manual'),
('order', 'On Order Processed'),
('invoice', 'On Invoice Paid'),
], 'Shipment Method', required=True
)
currency = fields.Many2One(
'currency.currency', 'Currency', required=True
)
price_list = fields.Many2One(
'product.price_list', 'Price List', required=True
)
payment_term = fields.Many2One(
'account.invoice.payment_term', 'Payment Term', required=True
)
company_party = fields.Function(
fields.Many2One('party.party', 'Company Party'),
'on_change_with_company_party'
)
taxes = fields.One2Many("sale.channel.tax", "channel", "Taxes")
# These fields would be needed at the time of product imports from
# external channel
default_uom = fields.Many2One(
'product.uom', 'Default Product UOM',
states=PRODUCT_STATES, depends=['source']
)
exceptions = fields.One2Many(
'channel.exception', 'channel', 'Exceptions'
)
order_states = fields.One2Many(
"sale.channel.order_state", "channel", "Order States"
)
shipping_carriers = fields.One2Many(
'sale.channel.carrier', 'channel', 'Shipping Carriers'
)
last_order_import_time = fields.DateTime(
'Last Order Import Time',
depends=['source', 'last_order_import_time_required'], states={
'invisible': Eval('source') == 'manual',
'required': Bool(Eval('last_order_import_time_required'))
}
)
last_order_export_time = fields.DateTime(
"Last Order Export Time", states=INVISIBLE_IF_MANUAL,
depends=['source']
)
last_shipment_export_time = fields.DateTime(
'Last shipment export time', states=INVISIBLE_IF_MANUAL,
depends=['source']
)
last_product_price_export_time = fields.DateTime(
'Last Product Price Export Time', states=INVISIBLE_IF_MANUAL,
depends=['source']
)
last_product_export_time = fields.DateTime(
'Last Product Export Time', states=INVISIBLE_IF_MANUAL,
depends=['source']
)
last_order_import_time_required = fields.Function(
fields.Boolean('Last Order Import Time Required'),
'get_last_order_import_time_required'
)
last_inventory_export_time = fields.DateTime(
'Last Inventory Export Time', states=INVISIBLE_IF_MANUAL,
depends=['source']
)
payment_gateways = fields.One2Many(
'sale.channel.payment_gateway', 'channel', 'Payment Gateways'
)
timezone = fields.Selection(
TIMEZONES, 'Timezone',
translate=False, required=True
)
# This field is to set according to sequence
sequence = fields.Integer('Sequence', select=True)
invoice_tax_account = fields.Many2One(
'account.account', 'Invoice Tax Account',
domain=[
('company', '=', Eval('company')),
('kind', 'not in', ['view', 'receivable', 'payable']),
], depends=['company'],
help="GL to book for taxes when new taxes are created from channel",
)
credit_note_tax_account = fields.Many2One(
'account.account', 'Credit Note Tax Account',
domain=[
('company', '=', Eval('company')),
('kind', 'not in', ['view', 'receivable', 'payable']),
], depends=['company'],
help="GL to book for taxes when new taxes are created from channel",
)
@staticmethod
def default_timezone():
return 'UTC'
@staticmethod
def default_sequence():
return 10
def get_last_order_import_time_required(self, name):
"""
Returns True or False if last_order_import_time field should be required
or not
"""
return False
@classmethod
def view_attributes(cls):
return super(SaleChannel, cls).view_attributes() + [
('//page[@id="configuration"]', 'states', {
'invisible': Eval('source') == 'manual',
}),
('//page[@id="last_import_export_time"]', 'states', {
'invisible': Eval('source') == 'manual',
}),
('//page[@id="product_defaults"]', 'states', {
'invisible': Eval('source') == 'manual',
}),
('//page[@id="order_states"]', 'states', {
'invisible': Eval('source') == 'manual',
}),
('//page[@id="import_export_buttons"]', 'states', {
'invisible': Eval('source') == 'manual',
})]
@classmethod
def __setup__(cls):
"""
Setup the class before adding to pool
"""
super(SaleChannel, cls).__setup__()
cls._buttons.update({
'import_data_button': {},
'export_data_button': {},
'import_order_states_button': {},
'import_shipping_carriers': {},
})
cls._error_messages.update({
"no_carriers_found":
"Shipping carrier is not configured for code: %s",
"no_tax_found":
"%s (tax) of rate %f was not found.",
"no_order_states_to_import":
"No importable order state found\n"
"HINT: Import order states from Order States tab in Channel"
})
cls._order.insert(0, ('sequence', 'ASC'))
@staticmethod
def default_default_uom():
Uom = Pool().get('product.uom')
unit = Uom.search([('name', '=', 'Unit')])
return unit and unit[0].id or None
@classmethod
def get_source(cls):
"""
Get the source
"""
return [('manual', 'Manual')]
@staticmethod
def default_last_order_import_time():
return datetime.utcnow() - relativedelta(months=1)
@staticmethod
def default_active():
return True
@staticmethod
def default_currency():
pool = Pool()
Company = pool.get('company.company')
Channel = pool.get('sale.channel')
company_id = Channel.default_company()
return company_id and Company(company_id).currency.id
@staticmethod
def default_company():
return Transaction().context.get('company')
@fields.depends('company')
def on_change_with_company_party(self, name=None):
Company = Pool().get('company.company')
company = self.company
if not company:
company = Company(SaleChannel.default_company()) # pragma: nocover
return company and company.party.id or None
@classmethod
def get_current_channel(cls):
"""Helper method to get the current current_channel.
"""
return cls(Transaction().context['current_channel'])
@classmethod
@ModelView.button
def import_shipping_carriers(cls, channels):
"""
Create shipping carriers by importing data from external channel.
Since external channels are implemented by downstream modules, it is
the responsibility of those channels to reuse this method or call super.
:param instances: Active record list of magento instances
"""
raise NotImplementedError(
"This feature has not been implemented."
)
def get_shipping_carrier(self, code, silent=False):
"""
Search for an existing carrier by matching code and channel.
If found, return its active record else raise_user_error.
"""
SaleCarrierChannel = Pool().get('sale.channel.carrier')
try:
carrier, = SaleCarrierChannel.search([
('code', '=', code),
('channel', '=', self.id),
])
except ValueError:
if silent:
return None
self.raise_user_error(
'no_carriers_found',
error_args=code
)
else:
return carrier.carrier
def get_shipping_carrier_service(self, code, silent=False):
"""
Search for an existing carrier service by matching code and channel.
If found, return its active record else raise_user_error.
"""
SaleCarrierChannel = Pool().get('sale.channel.carrier')
try:
carrier, = SaleCarrierChannel.search([
('code', '=', code),
('channel', '=', self.id),
])
except ValueError:
if silent:
return None
self.raise_user_error(
'no_carriers_found',
error_args=code
)
else:
return carrier.carrier_service
def get_order_states_to_import(self):
"""
Return list of `sale.channel.order_state` to import orders
"""
OrderState = Pool().get('sale.channel.order_state')
order_states_to_import = ['process_automatically', 'process_manually']
if Transaction().context.get('include_past_orders', False):
order_states_to_import.append('import_as_past')
order_states = OrderState.search([
('action', 'in', order_states_to_import),
('channel', '=', self.id),
])
if not order_states:
self.raise_user_error("no_order_states_to_import")
return order_states
def export_product_prices(self):
"""
Export product prices to channel
Since external channels are implemented by downstream modules, it is
the responsibility of those channels to reuse this method or call super.
:return: List of active records of products for which prices are
exported
"""
raise NotImplementedError(
"This feature has not been implemented for %s channel yet."
% self.source
)
def export_order_status(self):
"""
Export order status to external channel
Since external channels are implemented by downstream modules, it is
the responsibility of those channels to reuse this method or call super.
:return: List of active records of orders exported
"""
raise NotImplementedError(
"This feature has not been implemented for %s channel yet."
% self.source
)
@classmethod
def import_orders_using_cron(cls): # pragma: nocover
"""
Cron method to import orders from channels using cron
Downstream module need not to implement this method.
It will automatically call import_orders of the channel
Silently pass if import_orders is not implemented
"""
for channel in cls.search([]):
with Transaction().set_context(company=channel.company.id):
try:
channel.import_orders()
except NotImplementedError:
# Silently pass if method is not implemented
pass
@classmethod
def export_product_prices_using_cron(cls): # pragma: nocover
"""
Cron method to export product prices to external channel using cron
Downstream module need not to implement this method.
It will automatically call export_product_prices method of the channel.
Silently pass if export_product_prices is not implemented
"""
for channel in cls.search([]):
with Transaction().set_context(company=channel.company.id):
try:
channel.export_product_prices()
except NotImplementedError:
# Silently pass if method is not implemented
pass
@classmethod
def export_order_status_using_cron(cls):
"""
Export sales orders status to external channel using cron
"""
for channel in cls.search([]):
try:
channel.export_order_status()
except NotImplementedError:
pass
def get_listings_to_export_inventory(self):
"""
This method returns listing, which needs inventory update
Downstream module can override change its implementation
:return: List of AR of `product.product.channel_listing`
"""
ChannelListing = Pool().get('product.product.channel_listing')
cursor = Transaction().connection.cursor()
if not self.last_inventory_export_time:
# Return all active listings
return ChannelListing.search([
('channel', '=', self),
('state', '=', 'active')
])
else:
# Query to find listings
# in which product inventory is recently updated or
# listing it self got updated recently
cursor.execute("""
SELECT listing.id
FROM product_product_channel_listing AS listing
INNER JOIN stock_move ON stock_move.product = listing.product
WHERE listing.channel = %s AND listing.state = 'active' AND
(
COALESCE(stock_move.write_date, stock_move.create_date) > %s
OR
COALESCE(listing.write_date, listing.create_date) > %s
)
GROUP BY listing.id
""", (
self.id,
self.last_inventory_export_time,
self.last_inventory_export_time,
))
listing_ids = map(lambda r: r[0], cursor.fetchall())
return ChannelListing.browse(listing_ids)
def export_inventory(self):
"""
Export inventory to external channel
"""
Listing = Pool().get('product.product.channel_listing')
Channel = Pool().get('sale.channel')
last_inventory_export_time = datetime.utcnow()
channel_id = self.id
listings = self.get_listings_to_export_inventory()
# TODO: check if inventory export is allowed for this channel
Listing.export_bulk_inventory(listings)
# XXX: Exporting inventory to external channel is an expensive.
# To avoid lock on sale_channel table save record after
# exporting all inventory
with Transaction().new_transaction() as txn:
channel = Channel(channel_id)
channel.last_inventory_export_time = last_inventory_export_time
channel.save()
txn.commit()
@classmethod
def export_inventory_from_cron(cls): # pragma: nocover
"""
Cron method to export inventory to external channel
"""
for channel in cls.search([]):
with Transaction().set_context(company=channel.company.id):
try:
channel.export_inventory()
except NotImplementedError:
# Silently pass if method is not implemented
pass
def import_orders(self):
"""
Import orders from external channel.
Since external channels are implemented by downstream modules, it is
the responsibility of those channels to implement importing or call
super to delegate.
:return: List of active records of sale orders that are imported
"""
raise NotImplementedError(
"Import orders is not implemented for %s channels" % self.source
)
def import_order(self, order_info):
"""
Import specific order from external channel based on order_info.
Since external channels are implemented by downstream modules, it is
the responsibility of those channels to implement importing or call
super to delegate.
:param order_info: The type of order_info depends on the channel. It
could be an integer ID, a dictionary or anything.
:return: imported sale order active record
"""
raise NotImplementedError(
"Import order is not implemented for %s channels" % self.source
)
def import_products(self):
"""
Import Products from external channel.
Since external channels are implemented by downstream modules, it is
the responsibility of those channels to implement importing or call
super to delegate.
:return: List of active records of products that are imported
"""
raise NotImplementedError(
"Method import_products is not implemented for %s channel yet"
% self.source
) # pragma: nocover
def import_product(self, identifier, product_data=None):
"""
Import specific product from external channel based on product
identifier.
Since external channels are implemented by downstream modules, it is
the responsibility of those channels to implement importing or call
super to delegate.
:param identifier: product code or sku
:return: imported product active record
"""
raise NotImplementedError(
"Method import_product is not implemented for %s channel yet"
% self.source
) # pragma: nocover
def get_product(self, identifier, product_data=None):
"""
Given a SKU find the product or if it aint there create it and then
return the active record of the product. This cannot be done async
under any circumstances, because a product created on another
transaction will not be visible to the current transaction unless the
transaction is started over.
:param identifier: product identifier
"""
return self.import_product(identifier, product_data)
def import_product_images(self):
"""
Helper method for importing product images from external channel
after products are imported.
Since external channels are implemented by downstream modules, it is
the responsibility of those channels to implement importing or call
super to delegate.
"""
ProductListing = Pool().get('product.product.channel_listing')
product_listings = ProductListing.search([
('channel', '=', self.id),
('state', '=', 'active'),
])
if not product_listings:
self.raise_user_error(
'There are no active listings to import images for'
)
for listing in product_listings:
listing.import_product_image()
@classmethod
@ModelView.button_action('sale_channel.wizard_import_data')
def import_data_button(cls, channels):
pass # pragma: nocover
@classmethod
@ModelView.button_action('sale_channel.wizard_export_data')
def export_data_button(cls, channels):
pass # pragma: nocover
@classmethod
@ModelView.button_action('sale_channel.wizard_import_order_states')
def import_order_states_button(cls, channels):
"""
Import order states for current channel
:param channels: List of active records of channels
"""
pass
def import_order_states(self):
"""
Imports order states for current channel
Since external channels are implemented by downstream modules, it is
the responsibility of those channels to implement importing or call
super to delegate.
"""
raise NotImplementedError(
"This feature has not been implemented for %s channel yet"
% self.source
)
def get_default_tryton_action(self, code, name=None):
"""
Return default tryton_actions for this channel
"""
return {
'action': 'do_not_import',
'invoice_method': 'manual',
'shipment_method': 'manual',
}
def get_tryton_action(self, code):
"""
Get the tryton action corresponding to the channel state
as per the predefined logic.
Downstream modules need to inherit method and map states as per
convenience.
:param code: Code of channel state
:returns: A dictionary of tryton action and shipment and invoice methods
"""
ChannelOrderState = Pool().get('sale.channel.order_state')
try:
order_state, = ChannelOrderState.search([
('channel', '=', self.id),
('code', '=', code),
])
except ValueError:
return {
'action': 'do_not_import',
'invoice_method': 'manual',
'shipment_method': 'manual',
}
else:
return {
'action': order_state.action,
'invoice_method': order_state.invoice_method,
'shipment_method': order_state.shipment_method,
}
def create_order_state(self, code, name):
"""
This method creates order state for channel with given state code and
state name. If state already exist, return same.
:param code: State code used by external channel
:param name: State name used by external channel
:return: Active record of order state created or found
"""
OrderState = Pool().get('sale.channel.order_state')
order_states = OrderState.search([
('code', '=', code),
('channel', '=', self.id)
])
if order_states:
return order_states[0]
values = self.get_default_tryton_action(code, name)
values.update({
'name': name,
'code': code,
'channel': self.id,
})
return OrderState.create([values])[0]
def get_availability_context(self):
"""
Return the context in which the stock availability of any product must
be computed
"""
return {
'locations': [self.warehouse.id],
}
def get_availability(self, product):
"""
Return availability of the product within the context of this channel
Availability consists of three factors:
type: finite, bucket, infinite
quantity: (optional) quantity available
value: in_stock, limited, out_of_stock
If this looks like the value in the stripe relay API, do not be
confused, they are the same :)
"""
Listing = Pool().get('product.product.channel_listing')
Product = Pool().get('product.product')
listings = Listing.search([
('channel', '=', self.id),
('product', '=', product),
])
if listings:
# If there are listings, return the values from listing since
# they override channel defaults for a product and channel
return listings[0].get_availability()
with Transaction().set_context(**self.get_availability_context()):
rv = {'type': 'bucket'}
quantity = Product.get_quantity([product], 'quantity')[product.id]
if quantity > 0:
rv['value'] = 'in_stock'
else:
rv['value'] = 'out_of_stock'
return rv
@classmethod
def update_order_status_using_cron(cls): # pragma: nocover
"""
Cron method to update orders from channels using cron
Downstream module need not to implement this method.
It will automatically call update_order_status of the channel
Silently pass if update_order_status is not implemented
"""
for channel in cls.search([]):
with Transaction().set_context(company=channel.company.id):
try:
channel.update_order_status()
except NotImplementedError:
# Silently pass if method is not implemented
pass
def update_order_status(self):
"""This method is responsible for updating order status from external
channel.
"""
if self.source == 'manual':
return
raise NotImplementedError(
"This feature has not been implemented for %s channel yet."
% self.source
)
def get_tax(self, name, rate, silent=False):
"""
Search for an existing Tax record by matching name and rate.
If found return its active record else raise user error.
"""
TaxMapping = Pool().get('sale.channel.tax')
domain = [
('rate', '=', rate),
('channel', '=', self)
]
if name:
# Search with name when it is provided
# Name can be explicitly passed as None, when external
# channels like magento does not provide it.
domain.append(('name', '=', name))
try:
mapped_tax, = TaxMapping.search(domain)
except ValueError:
if silent:
return None
self.raise_user_error(
'no_tax_found', error_args=(name, rate)
)
else:
return mapped_tax.tax
class ReadUser(ModelSQL):
"""
Read Users for Sale Channel
"""
__name__ = 'sale.channel-read-res.user'
channel = fields.Many2One(
'sale.channel', 'Channel', ondelete='CASCADE', select=True,
required=True
)
user = fields.Many2One(
'res.user', 'User', ondelete='RESTRICT', required=True
)
class WriteUser(ModelSQL):
"""
Write Users for Sale Channel
"""
__name__ = 'sale.channel-write-res.user'
channel = fields.Many2One(
'sale.channel', 'Channel', ondelete='CASCADE', select=True,
required=True
)
user = fields.Many2One(
'res.user', 'User', ondelete='RESTRICT', required=True
)
class ChannelException(ModelSQL, ModelView):
"""
Channel Exception model
"""
__name__ = 'channel.exception'
origin = fields.Reference(
"Origin", selection='models_get', select=True, readonly=True
)
log = fields.Text('Exception Log', readonly=True)
channel = fields.Many2One(
"sale.channel", "Channel", required=True, readonly=True
)
is_resolved = fields.Boolean("Is Resolved ?", select=True)
@classmethod
def __setup__(cls):
"""
Setup the class before adding to pool
"""
super(ChannelException, cls).__setup__()
cls._buttons.update({
'resolve_exception_button': {
'readonly': Bool(Eval('is_resolved')),
},
})
@classmethod
@ModelView.button
def resolve_exception_button(cls, exceptions):
"""
Method called from a button to resolve exceptions
:param channels: List of active records of exceptions
"""
for exception in exceptions:
if exception.is_resolved:
continue
exception.is_resolved = True
exception.save()
@staticmethod
def default_is_resolved():
return False
@classmethod
def models_get(cls):
'''
Return valid models allowed for origin
'''
return [
('sale.sale', 'Sale'),
('sale.line', 'Sale Line'),
]
class ChannelOrderState(ModelSQL, ModelView):
"""
Sale Channel - Tryton Order State map
This model stores a map of order states between tryton and sale channel.
This allows the user to configure the states mapping according to his/her
convenience. This map is used to process orders in tryton when they are
imported. This is also used to map the order status back to channel when
sales are exported. This also allows the user to determine in which state
order need to be imported.
"""
__name__ = 'sale.channel.order_state'
name = fields.Char('Name', required=True, readonly=True)
code = fields.Char('Code', required=True, readonly=True)
action = fields.Selection([
('do_not_import', 'Do Not Import'),
('process_automatically', 'Process Automatically'),
('process_manually', 'Process Manually'),
('import_as_past', 'Import As Past Orders'),
], 'Action', required=True)
invoice_method = fields.Selection([
('manual', 'Manual'),
('order', 'On Order Processed'),
('shipment', 'On Shipment Sent'),
], 'Invoice Method', required=True)
shipment_method = fields.Selection([
('manual', 'Manual'),
('order', 'On Order Processed'),
('invoice', 'On Invoice Paid'),
], 'Shipment Method', required=True)
channel = fields.Many2One(
'sale.channel', 'Sale Channel', required=True,
ondelete="CASCADE", readonly=True
)
@staticmethod
def default_channel():
"Return default channel from context"
return Transaction().context.get('current_channel')
class TaxMapping(ModelSQL, ModelView):
'Sale Tax'
__name__ = 'sale.channel.tax'
name = fields.Char("Name", required=True)
rate = fields.Numeric('Rate', digits=(14, 10), required=True)
tax = fields.Many2One("account.tax", "Tax", required=True)
channel = fields.Many2One("sale.channel", "Channel", required=True)
@classmethod
def __setup__(cls):
super(TaxMapping, cls).__setup__()
table = cls.__table__()
cls._error_messages.update({
'unique_tax_rate_per_channel':
"Tax rate and name must be unique per channel"
})
cls._sql_constraints += [
('unique_tax_percent',
Unique(table, table.channel, table.name, table.rate),
'unique_tax_rate_per_channel')
]
class ChannelPaymentGateway(ModelSQL, ModelView):
"""
Sale Channel Payment Gateway
"""
__name__ = 'sale.channel.payment_gateway'
code = fields.Char("Code", required=True, select=True)
name = fields.Char('Name', required=True)
gateway = fields.Many2One(
'payment_gateway.gateway', 'Gateway', required=True,
ondelete='RESTRICT', select=True,
)
channel = fields.Many2One(
'sale.channel', 'Channel', readonly=True, select=True,
)
@classmethod
def __setup__(cls):
"""
Setup the class before adding to pool
"""
super(ChannelPaymentGateway, cls).__setup__()
cls._sql_constraints += [
(
'code_channel_unique', 'unique(code, channel)',
'Payment gateway already exists for this channel'
)