-
Notifications
You must be signed in to change notification settings - Fork 0
/
sale.py
executable file
·251 lines (203 loc) · 11.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
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2015 RyePDX LLC
# Copyright (C) 2011 NovaPoint Group LLC (<http://www.novapointgroup.com>)
# Copyright (C) 2004-2010 OpenERP SA (<http://www.openerp.com>)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
#
##############################################################################
import datetime
from decimal import Decimal
import openerp.addons.decimal_precision as dp
from openerp.osv import fields, osv
class sale_order(osv.osv):
_inherit = "sale.order"
def _get_order(self, cr, uid, ids, context=None):
result = {}
for line in self.pool.get('sale.order.line').browse(cr, uid, ids, context=None):
result[line.order_id.id] = True
return result.keys()
def _prepare_invoice(self, cr, uid, order, lines, context=None):
res = super(sale_order, self)._prepare_invoice(cr, uid, order, lines, context=context)
res.update({
'shipcharge': order.shipcharge,
'ship_method_id': order.ship_method_id.id
})
if order.sale_account_id:
res['sale_account_id'] = order.sale_account_id.id
return res
def _make_invoice(self, cr, uid, order, lines, context=None):
inv_id = super(sale_order, self)._make_invoice(cr, uid, order, lines, context=None)
if inv_id and order.sale_account_id:
self.pool.get("account.invoice").button_reset_taxes(cr, uid, [inv_id], context=context)
return inv_id
def _amount_shipment_tax(self, cr, uid, shipment_taxes, shipment_charge):
val = 0.0
for c in self.pool.get('account.tax').compute_all(cr, uid, shipment_taxes, shipment_charge, 1)['taxes']:
val += c.get('amount', 0.0)
return val
def _amount_all(self, cr, uid, ids, field_name, arg, context=None):
cur_pool = self.pool.get('res.currency')
res = dict([(sale.id, {'amount_physical': cur_pool.round(cr, uid, sale.pricelist_id.currency_id, sum(
[line.price_subtotal for line in sale.order_line if line.product_id.type != "service"]
))}) for sale in self.browse(cr, uid, ids, context=context)])
if field_name == 'amount_physical':
return res
res2 = super(sale_order, self)._amount_all(cr, uid, ids, field_name, arg, context=context)
for sale_id in ids:
res[sale_id] = dict(res.get(sale_id, {}).items() + res2.get(sale_id, {}).items())
for order in self.browse(cr, uid, ids, context=context):
cur = order.pricelist_id.currency_id
tax_ids = order.ship_method_id and "shipment_tax_ids" in order.ship_method_id._columns \
and order.ship_method_id.shipment_tax_ids
if tax_ids:
val = self._amount_shipment_tax(cr, uid, tax_ids, order.shipcharge)
res[order.id]['amount_tax'] += cur_pool.round(cr, uid, cur, val)
ship_methods = None
if not order.ship_method_id:
ship_methods = self._get_ship_methods(
cr, uid, order.recipient_country_id.id,
res[order.id]['amount_untaxed'], res[order.id]['amount_physical']
)
if ship_methods:
order.shipcharge = self._get_ship_charge(cr, uid, ship_methods[0], order.id, context=context)
self.write(
cr, uid, order.id, {
"shipcharge": order.shipcharge,
"ship_method_id": ship_methods[0].id
}, context=context
)
res[order.id]['amount_total'] = res[order.id]['amount_untaxed'] + res[order.id]['amount_tax'] + order.shipcharge
return res
def _get_sale_ship_methods(self, cr, uid, ids, field_name, args, context=None):
methods = {}
todays_ratecards = self._get_todays_ratecards(cr, uid, context=context)
for sale_obj in self.browse(cr, uid, ids):
methods[sale_obj.id] = [m.id for m in self._get_ship_methods(
cr, uid, sale_obj.recipient_country_id.id, sale_obj.amount_total, sale_obj.amount_physical,
ratecards=todays_ratecards, context=context
)]
return methods
def _get_todays_ratecards(self, cr, uid, context=None):
table_pool = self.pool.get('shipping.rate.card')
today = datetime.date.today().strftime("%Y-%m-%d")
return table_pool.browse(cr, uid, table_pool.search(
cr, uid, ['|', ('from_date', '<=', today), ('from_date', '=', None),
'|', ('to_date', '>', today), ('to_date', '=', None)], context=context
), context=context)
def _get_ship_methods(self, cr, uid, country_id, amount_total, amount_physical, ratecards=None, context=None):
rate_pool = self.pool.get('shipping.rate')
if ratecards is None:
ratecards = self._get_todays_ratecards(cr, uid, context=context)
method_objs = rate_pool.browse(cr, uid, rate_pool.search(cr, uid, [
'|', ('country_id', '=', country_id), ('country_id', '=', None),
'|',
'&', '&', '|', '|', ('to_price', '>=', amount_total), ('to_price', '=', 0.0), ('to_price', '=', None),
('from_price', '<=', amount_total), ('physical_only', '=', False),
'&', '&', '|', '|', ('to_price', '>=', amount_physical), ('to_price', '=', 0.0),
('to_price', '=', None),
('from_price', '<=', amount_physical), ('physical_only', '=', True),
'|', ('id', 'in', [r.id for t in ratecards for r in t.rate_ids]), ('card_id', '=', None)
], order='country_id, charge, to_price', context=context), context=context)
methods = []
seen_methods = []
for method in method_objs:
if method.name not in seen_methods:
methods.append(method)
seen_methods.append(method.name)
return methods
_columns = {
'shipcharge': fields.float('Shipping Cost', required=True),
'ship_method_id': fields.many2one('shipping.rate', 'Shipping Method',
domain="[('id', 'in', valid_shipping_methods[0][2])]",
context="{'group_by':'name', 'order':'country_id, to_price'}"
),
'valid_shipping_methods': fields.function(_get_sale_ship_methods, type="many2many", relation="shipping.rate"),
'recipient_country_id': fields.related(
"partner_shipping_id", "country_id", type="many2one", relation="res.country", store=False
),
'sale_account_id': fields.many2one('account.account', 'Shipping Account',
help='This account represents the g/l account for booking shipping income.'),
'amount_untaxed': fields.function(_amount_all, method=True, digits_compute= dp.get_precision('Sale Price'), string='Untaxed Amount',
store = {
'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line', 'ship_method_id', 'shipcharge'], 10),
'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
},
multi='sums', help="The amount without tax."),
'amount_tax': fields.function(_amount_all, method=True, digits_compute=dp.get_precision('Sale Price'), string='Taxes',
store = {
'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line', 'ship_method_id', 'shipcharge'], 10),
'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
},
multi='sums', help="The tax amount."),
'amount_physical': fields.function(_amount_all, method=True, digits_compute= dp.get_precision('Sale Price'), string='Total',
store = {
'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line', 'ship_method_id', 'shipcharge'], 10),
'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
},
multi='sums', help="The total physical amount."),
'amount_total': fields.function(_amount_all, method=True, digits_compute= dp.get_precision('Sale Price'), string='Total',
store = {
'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line', 'ship_method_id', 'shipcharge'], 10),
'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
},
multi='sums', help="The total amount.")
}
defaults = {
'shipcharge': 0.0
}
def onchange_partner_id(self, cr, uid, ids, partner_id, context=None):
res = super(sale_order, self).onchange_partner_id(cr, uid, ids, partner_id, context=context) or {}
if "value" not in res:
res["value"] = {}
sale_total = 0.0
physical_total = 0.0
if ids:
sale = self.browse(cr, uid, ids[0], context=context)
sale_total = sale.amount_untaxed
physical_total = sale.amount_physical
partner = self.pool.get("res.partner").browse(cr, uid, partner_id, context=context)
methods = self._get_ship_methods(cr, uid, partner.country_id.id, sale_total, physical_total)
if not methods:
return res
res["value"]["valid_shipping_methods"] = [m.id for m in methods]
selected_ship_method = methods[0].id
international = partner.country_id.name not in ['United States', 'Canada']
for method in methods:
if (not international and method.name.lower() == 'pw shipping') \
or (international and method.name.lower() == 'pw int shipping'):
selected_ship_method = method.id
res["value"]["ship_method_id"] = selected_ship_method
return res
def onchange_ship_method(self, cr, uid, ids, ship_method_id, context=None):
if not ids or not ship_method_id:
return {"value": {"shipcharge": 0.0}}
rate_pool = self.pool.get("shipping.rate")
method = rate_pool.browse(cr, uid, ship_method_id, context=context)
return {"value": {"shipcharge": self._get_ship_charge(cr, uid, method, ids[0], context=context)}}
def onchange_update_total(self, cr, uid, ids, shipcharge, amount_untaxed, amount_tax):
return {"value": {"amount_total": shipcharge + amount_untaxed + amount_tax}}
def _get_ship_charge(self, cr, uid, method, order_id, context=None):
shipcharge = method.charge if method and method.charge else 0.0
if method and method.percentage:
order = self.browse(cr, uid, order_id, context=context)
shipcharge += float(Decimal(
Decimal(order.amount_untaxed) * (Decimal(method.percentage) / Decimal(100))
).quantize(Decimal("1.00")))
return shipcharge
sale_order()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: