-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.py
104 lines (91 loc) · 3.39 KB
/
product.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
# 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 trytond.pool import Pool, PoolMeta
from trytond.model import ModelView, fields
from trytond.pyson import Eval
from trytond.tools import grouped_slice
from trytond.transaction import Transaction
from trytond.config import config
from .algevasa import _requests
class Product(metaclass=PoolMeta):
__name__ = 'product.product'
algevasa = fields.Boolean('Algevasa',
states={
'invisible': Eval('type') == 'service',
},
help="Work with Algevasa, a Logitiscs Operator")
algevasa_synched = fields.Boolean('Algevasa Synched', readonly=True,
states={
'invisible': ~Eval('algevasa'),
},)
algevasa_synch_message = fields.Text('Algevasa Synch Message', readonly=True,
states={
'invisible': ~Eval('algevasa'),
},)
@classmethod
def __setup__(cls):
super().__setup__()
cls._buttons.update({
'sync_with_algevasa': {
'invisible': ~Eval('algevasa', False)
},})
@property
def algevasa_owner(self):
return config.get('algevasa', 'owner') or ''
@property
def algevasa_key(self):
return config.get('algevasa', 'key') or ''
@staticmethod
def default_algevasa():
return False
@classmethod
def generate_algevasa_product_file(cls, products):
pool = Pool()
Location = pool.get('stock.location')
result = {}
warehouses = Location.search([
('type', '=', 'warehouse'),
('active', '=', True),
('algevasa', '=', True),
('algevasa_product_format', '!=', None),
])
products = [product for product in products if product.algevasa]
for warehouse in warehouses:
result.update(warehouse.algevasa_product_format.export_file(
products))
return result
@classmethod
def update_algevasa_product(cls, products):
prods = cls.generate_algevasa_product_file(products)
for prod, data in prods.items():
response = _requests('POST', data=data)
prod.algevasa_synch_message = response.get('message', '')
if (response.get('code') == 200
and response.get('answer') == 'TRUE'):
prod.algevasa_synched = True
prod.save()
@classmethod
@ModelView.button
def sync_with_algevasa(cls, products):
for sub_ids in grouped_slice(products):
sub_products = cls.search([
('id', 'in', list(sub_ids))
])
cls.update_algevasa_product(sub_products)
Transaction().commit()
@classmethod
def create(cls, vlist):
pool = Pool()
Configuration = pool.get('product.configuration')
config = Configuration(1)
products = super().create(vlist)
products2sync = []
if config.auto_algevasa:
for product in products:
if product.type == 'goods':
product.algevasa = True
products2sync.append(product)
if products2sync:
cls.save(products2sync)
cls.sync_with_algevasa(products2sync)
return products