forked from openlabs/nereid-cart-b2c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sale.py
155 lines (131 loc) · 5.05 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
# -*- coding: UTF-8 -*-
'''
nereid_cart.sale
Sales modules changes to fit nereid
:copyright: (c) 2010-2014 by Openlabs Technologies & Consulting (P) Ltd.
:license: GPLv3, see LICENSE for more details
'''
from trytond.pool import Pool, PoolMeta
from trytond.model import fields
from trytond.transaction import Transaction
from nereid import current_user
from nereid.ctx import has_request_context
__all__ = ['Sale', 'SaleLine']
__metaclass__ = PoolMeta
class Sale:
'''Add a boolean to indicate if the order originated from a shopping cart.
'''
__name__ = 'sale.sale'
is_cart = fields.Boolean(
'Is Cart Order?', readonly=True, select=True
)
website = fields.Many2One(
'nereid.website', 'Website', readonly=True, select=True
)
nereid_user = fields.Many2One(
'nereid.user', 'Nereid User', select=True
)
@staticmethod
def default_is_cart():
"""Dont make this as a default as this would cause orders being placed
from backend to be placed under default.
"""
return False
@staticmethod
def default_price_list():
"""Get the pricelist of active user. In the
event that the logged in user does not have a pricelist set against
the user, the shop's pricelist is chosen.
:param user: active record of the nereid user
"""
User = Pool().get('res.user')
user = User(Transaction().user)
shop_price_list = user.shop.price_list.id if user.shop else None
if not has_request_context():
# Not a nereid request
return shop_price_list
# If control reaches here, then this is a nereid request. Lets try
# and personalise the pricelist of the user logged in.
if current_user.is_anonymous():
# Sorry anonymous users, you get the shop price
return shop_price_list
if current_user.party.sale_price_list:
# There is a sale pricelist for the specific user's party.
return current_user.party.sale_price_list.id
return shop_price_list
def refresh_taxes(self):
'''
Reload taxes of all sale lines
'''
for line in self.lines:
line.refresh_taxes()
def find_existing_line(self, product_id):
"""Return existing sale line for given product"""
SaleLine = Pool().get('sale.line')
lines = SaleLine.search([
('sale', '=', self.id),
('product', '=', product_id),
])
return lines[0] if lines else None
def _add_or_update(self, product_id, quantity, action='set'):
'''Add item as a line or if a line with item exists
update it for the quantity
:param product: ID of the product
:param quantity: Quantity
:param action: set - set the quantity to the given quantity
add - add quantity to existing quantity
'''
SaleLine = Pool().get('sale.line')
order_line = self.find_existing_line(product_id)
if order_line:
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
),
'unit': order_line.unit.id,
'quantity': quantity if action == 'set'
else quantity + order_line.quantity,
'type': 'line',
}
values.update(SaleLine(**values).on_change_quantity())
new_values = {}
for key, value in values.iteritems():
if '.' not in key:
new_values[key] = value
SaleLine.write([order_line], new_values)
return order_line
else:
values = {
'product': product_id,
'_parent_sale.currency': self.currency.id,
'_parent_sale.party': self.party.id,
'sale': self.id,
'type': 'line',
'sequence': 10,
'quantity': quantity,
'unit': None,
'description': None,
}
if self.price_list:
values['_parent_sale.price_list'] = self.price_list.id
values.update(SaleLine(**values).on_change_product())
values.update(SaleLine(**values).on_change_quantity())
new_values = {}
for key, value in values.iteritems():
if '.' not in key:
new_values[key] = value
if key == 'taxes' and value:
new_values[key] = [('add', value)]
return SaleLine.create([new_values])[0]
class SaleLine:
__name__ = 'sale.line'
def refresh_taxes(self):
"Refresh taxes of sale line"
SaleLine = Pool().get('sale.line')
values = SaleLine(self.id).on_change_product()
if 'taxes' in values:
self.taxes = values['taxes']
self.save()