-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsale.py
220 lines (186 loc) · 7.19 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
# -*- 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 functools import partial
from babel import numbers
from decimal import Decimal
from trytond.pool import Pool, PoolMeta
from trytond.model import fields
from trytond.transaction import Transaction
from nereid import current_user, url_for, request, redirect, flash, abort, \
current_locale
from nereid.contrib.locale import make_lazy_gettext
from nereid.ctx import has_request_context
_ = make_lazy_gettext('nereid_cart_b2c')
__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 channel's pricelist is chosen.
:param user: active record of the nereid user
"""
User = Pool().get('res.user')
user = User(Transaction().user)
channel_price_list = user.current_channel.price_list.id if \
user.current_channel else None
if not has_request_context():
# Not a nereid request
return channel_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 channel_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 channel_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')
Product = Pool().get('product.product')
order_line = self.find_existing_line(product_id)
product = Product(product_id)
old_price = Decimal('0.0')
if order_line:
old_price = order_line.unit_price
order_line.unit = order_line.unit.id
order_line.quantity = \
quantity if action == 'set' else quantity + order_line.quantity
else:
order_line = SaleLine(**{
'product': product_id,
'sale': self,
'type': 'line',
'sale': self.id,
'sequence': 10,
'quantity': quantity,
'unit': None,
'description': None,
'warehouse': self.warehouse
})
order_line.on_change_product()
order_line.on_change_quantity()
if old_price and old_price != order_line.unit_price:
vals = (
product.name, self.currency.symbol, old_price,
self.currency.symbol, order_line.unit_price
)
if old_price < order_line.unit_price:
message = _(
"The unit price of product %s increased from %s%d to "
"%s%d." % vals
)
else:
message = _(
"The unit price of product %s dropped from %s%d "
"to %s%d." % vals
)
flash(message)
return order_line
class SaleLine:
__name__ = 'sale.line'
def refresh_taxes(self):
"Refresh taxes of sale line"
SaleLine = Pool().get('sale.line')
updated_sale = SaleLine(self.id).on_change_product()
self.taxes = updated_sale.taxes
self.save()
def serialize(self, purpose=None):
"""
Serialize SaleLine data
"""
res = {}
if purpose == 'cart':
currency_format = partial(
numbers.format_currency, currency=self.sale.currency.code,
locale=current_locale.language.code
)
number_format = partial(
numbers.format_number, locale=current_locale.language.code
)
res.update({
'id': self.id,
'display_name': (
self.product and self.product.name or self.description
),
'url': self.product.get_absolute_url(_external=True),
'image': (
self.product.default_image.transform_command().thumbnail(
150, 150, 'a'
).url() if self.product.default_image else None
),
'product': self.product.serialize(purpose),
'quantity': number_format(self.quantity),
'unit': self.unit.symbol,
'unit_price': currency_format(self.unit_price),
'amount': currency_format(self.amount),
'remove_url': url_for(
'nereid.cart.delete_from_cart', line=self.id
),
})
elif hasattr(super(SaleLine, self), 'serialize'):
return super(SaleLine, self).serialize(purpose) # pragma: no cover
return res
def add_to(self, sale):
"""
Copy sale_line to new sale.
Downstream modules can override this method to add change this behaviour
of copying.
:param sale: Sale active record.
:return: Newly created sale_line
"""
return sale._add_or_update(self.product.id, self.quantity)
def validate_for_product_inventory(self):
"""
This method validates the sale line against the product's inventory
attributes. This method requires request context.
"""
if has_request_context() and not self.product.can_buy_from_eshop():
flash(_('This product is no longer available'))
abort(redirect(request.referrer))