2013-07-16 09:36:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-01-30 23:39:51 +00:00
|
|
|
import random
|
|
|
|
|
2013-09-02 10:46:46 +00:00
|
|
|
from openerp import SUPERUSER_ID
|
2014-01-30 23:39:51 +00:00
|
|
|
from openerp.osv import osv, orm, fields
|
2014-01-07 13:59:33 +00:00
|
|
|
from openerp.addons.web.http import request
|
2015-05-18 08:20:49 +00:00
|
|
|
from openerp.tools.translate import _
|
2013-07-16 09:36:24 +00:00
|
|
|
|
2013-12-17 15:26:05 +00:00
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
class sale_order(osv.Model):
|
2013-08-12 16:31:23 +00:00
|
|
|
_inherit = "sale.order"
|
2013-09-24 12:19:44 +00:00
|
|
|
|
2014-02-21 17:28:16 +00:00
|
|
|
def _cart_qty(self, cr, uid, ids, field_name, arg, context=None):
|
2014-05-12 15:52:10 +00:00
|
|
|
res = dict()
|
2014-02-21 17:28:16 +00:00
|
|
|
for order in self.browse(cr, uid, ids, context=context):
|
|
|
|
res[order.id] = int(sum(l.product_uom_qty for l in (order.website_order_line or [])))
|
|
|
|
return res
|
|
|
|
|
2013-10-08 15:22:33 +00:00
|
|
|
_columns = {
|
2013-12-18 13:21:50 +00:00
|
|
|
'website_order_line': fields.one2many(
|
|
|
|
'sale.order.line', 'order_id',
|
|
|
|
string='Order Lines displayed on Website', readonly=True,
|
|
|
|
help='Order Lines to be displayed on the website. They should not be used for computation purpose.',
|
|
|
|
),
|
2014-05-12 15:52:10 +00:00
|
|
|
'cart_quantity': fields.function(_cart_qty, type='integer', string='Cart Quantity'),
|
2014-11-19 10:36:04 +00:00
|
|
|
'payment_acquirer_id': fields.many2one('payment.acquirer', 'Payment Acquirer', on_delete='set null', copy=False),
|
|
|
|
'payment_tx_id': fields.many2one('payment.transaction', 'Transaction', on_delete='set null', copy=False),
|
2013-10-08 15:22:33 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 14:45:20 +00:00
|
|
|
def _get_errors(self, cr, uid, order, context=None):
|
|
|
|
return []
|
|
|
|
|
2013-12-13 11:06:29 +00:00
|
|
|
def _get_website_data(self, cr, uid, order, context):
|
2013-12-17 15:26:05 +00:00
|
|
|
return {
|
|
|
|
'partner': order.partner_id.id,
|
|
|
|
'order': order
|
|
|
|
}
|
2013-12-13 11:06:29 +00:00
|
|
|
|
2014-06-25 09:31:39 +00:00
|
|
|
def _cart_find_product_line(self, cr, uid, ids, product_id=None, line_id=None, context=None, **kwargs):
|
2014-01-30 23:39:51 +00:00
|
|
|
for so in self.browse(cr, uid, ids, context=context):
|
2014-05-02 14:01:52 +00:00
|
|
|
domain = [('order_id', '=', so.id), ('product_id', '=', product_id)]
|
|
|
|
if line_id:
|
|
|
|
domain += [('id', '=', line_id)]
|
2014-06-24 15:37:13 +00:00
|
|
|
return self.pool.get('sale.order.line').search(cr, SUPERUSER_ID, domain, context=context)
|
2014-05-02 14:01:52 +00:00
|
|
|
|
2014-09-30 15:20:28 +00:00
|
|
|
def _website_product_id_change(self, cr, uid, ids, order_id, product_id, qty=0, line_id=None, context=None):
|
2014-05-02 14:01:52 +00:00
|
|
|
so = self.pool.get('sale.order').browse(cr, uid, order_id, context=context)
|
|
|
|
|
|
|
|
values = self.pool.get('sale.order.line').product_id_change(cr, SUPERUSER_ID, [],
|
|
|
|
pricelist=so.pricelist_id.id,
|
|
|
|
product=product_id,
|
|
|
|
partner_id=so.partner_id.id,
|
2014-10-10 19:02:30 +00:00
|
|
|
fiscal_position=so.fiscal_position.id,
|
2014-09-30 15:20:28 +00:00
|
|
|
qty=qty,
|
2014-05-02 14:01:52 +00:00
|
|
|
context=context
|
|
|
|
)['value']
|
|
|
|
|
|
|
|
if line_id:
|
|
|
|
line = self.pool.get('sale.order.line').browse(cr, SUPERUSER_ID, line_id, context=context)
|
|
|
|
values['name'] = line.name
|
|
|
|
else:
|
|
|
|
product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
|
2015-03-09 19:05:22 +00:00
|
|
|
values['name'] = product.description_sale and "%s\n%s" % (product.display_name, product.description_sale) or product.display_name
|
2014-05-02 14:01:52 +00:00
|
|
|
|
|
|
|
values['product_id'] = product_id
|
|
|
|
values['order_id'] = order_id
|
|
|
|
if values.get('tax_id') != None:
|
|
|
|
values['tax_id'] = [(6, 0, values['tax_id'])]
|
|
|
|
return values
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-06-25 09:31:39 +00:00
|
|
|
def _cart_update(self, cr, uid, ids, product_id=None, line_id=None, add_qty=0, set_qty=0, context=None, **kwargs):
|
2014-01-30 23:39:51 +00:00
|
|
|
""" Add or set product quantity, add_qty can be negative """
|
2014-04-25 13:34:03 +00:00
|
|
|
sol = self.pool.get('sale.order.line')
|
2014-01-30 23:39:51 +00:00
|
|
|
|
2014-04-28 10:28:57 +00:00
|
|
|
quantity = 0
|
2014-04-25 13:34:03 +00:00
|
|
|
for so in self.browse(cr, uid, ids, context=context):
|
2015-05-18 08:20:49 +00:00
|
|
|
if so.state != 'draft':
|
2015-06-09 09:13:17 +00:00
|
|
|
request.session['sale_order_id'] = None
|
2015-05-18 08:20:49 +00:00
|
|
|
raise osv.except_osv(_('Error!'), _('It is forbidden to modify a sale order which is not in draft status'))
|
2014-06-24 15:37:13 +00:00
|
|
|
if line_id != False:
|
2014-06-25 09:31:39 +00:00
|
|
|
line_ids = so._cart_find_product_line(product_id, line_id, context=context, **kwargs)
|
2014-06-24 15:37:13 +00:00
|
|
|
if line_ids:
|
|
|
|
line_id = line_ids[0]
|
2014-01-30 23:39:51 +00:00
|
|
|
|
|
|
|
# Create line if no line with product_id can be located
|
|
|
|
if not line_id:
|
2014-09-30 15:20:28 +00:00
|
|
|
values = self._website_product_id_change(cr, uid, ids, so.id, product_id, qty=1, context=context)
|
2014-02-21 17:28:16 +00:00
|
|
|
line_id = sol.create(cr, SUPERUSER_ID, values, context=context)
|
2014-05-02 15:08:42 +00:00
|
|
|
if add_qty:
|
|
|
|
add_qty -= 1
|
2014-01-30 23:39:51 +00:00
|
|
|
|
|
|
|
# compute new quantity
|
|
|
|
if set_qty:
|
|
|
|
quantity = set_qty
|
2014-04-28 09:36:09 +00:00
|
|
|
elif add_qty != None:
|
|
|
|
quantity = sol.browse(cr, SUPERUSER_ID, line_id, context=context).product_uom_qty + (add_qty or 0)
|
2014-01-30 23:39:51 +00:00
|
|
|
|
|
|
|
# Remove zero of negative lines
|
|
|
|
if quantity <= 0:
|
2014-06-24 13:11:53 +00:00
|
|
|
sol.unlink(cr, SUPERUSER_ID, [line_id], context=context)
|
2014-01-30 23:39:51 +00:00
|
|
|
else:
|
|
|
|
# update line
|
2014-09-30 15:20:28 +00:00
|
|
|
values = self._website_product_id_change(cr, uid, ids, so.id, product_id, qty=quantity, line_id=line_id, context=context)
|
2014-01-30 23:39:51 +00:00
|
|
|
values['product_uom_qty'] = quantity
|
|
|
|
sol.write(cr, SUPERUSER_ID, [line_id], values, context=context)
|
|
|
|
|
2014-06-25 09:31:39 +00:00
|
|
|
return {'line_id': line_id, 'quantity': quantity}
|
2014-01-30 23:39:51 +00:00
|
|
|
|
|
|
|
def _cart_accessories(self, cr, uid, ids, context=None):
|
|
|
|
for order in self.browse(cr, uid, ids, context=context):
|
2014-06-04 09:25:51 +00:00
|
|
|
s = set(j.id for l in (order.website_order_line or []) for j in (l.product_id.accessory_product_ids or []))
|
2014-05-02 15:51:24 +00:00
|
|
|
s -= set(l.product_id.id for l in order.order_line)
|
2014-01-30 23:39:51 +00:00
|
|
|
product_ids = random.sample(s, min(len(s),3))
|
2014-06-04 09:25:51 +00:00
|
|
|
return self.pool['product.product'].browse(cr, uid, product_ids, context=context)
|
2014-01-30 23:39:51 +00:00
|
|
|
|
|
|
|
class website(orm.Model):
|
|
|
|
_inherit = 'website'
|
|
|
|
|
|
|
|
_columns = {
|
|
|
|
'pricelist_id': fields.related('user_id','partner_id','property_product_pricelist',
|
2014-07-02 16:18:29 +00:00
|
|
|
type='many2one', relation='product.pricelist', string='Default Pricelist'),
|
2014-06-24 15:37:13 +00:00
|
|
|
'currency_id': fields.related('pricelist_id','currency_id',
|
2014-07-02 16:18:29 +00:00
|
|
|
type='many2one', relation='res.currency', string='Default Currency'),
|
2014-01-30 23:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def sale_product_domain(self, cr, uid, ids, context=None):
|
|
|
|
return [("sale_ok", "=", True)]
|
|
|
|
|
2014-06-29 21:31:16 +00:00
|
|
|
def sale_get_order(self, cr, uid, ids, force_create=False, code=None, update_pricelist=None, context=None):
|
2014-04-28 10:28:57 +00:00
|
|
|
sale_order_obj = self.pool['sale.order']
|
2014-02-25 12:57:36 +00:00
|
|
|
sale_order_id = request.session.get('sale_order_id')
|
2014-01-30 23:39:51 +00:00
|
|
|
sale_order = None
|
|
|
|
# create so if needed
|
|
|
|
if not sale_order_id and (force_create or code):
|
2014-04-28 10:28:57 +00:00
|
|
|
# TODO cache partner_id session
|
|
|
|
partner = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context).partner_id
|
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
for w in self.browse(cr, uid, ids):
|
|
|
|
values = {
|
|
|
|
'user_id': w.user_id.id,
|
|
|
|
'partner_id': partner.id,
|
|
|
|
'pricelist_id': partner.property_product_pricelist.id,
|
2014-05-13 10:09:24 +00:00
|
|
|
'section_id': self.pool.get('ir.model.data').get_object_reference(cr, uid, 'website', 'salesteam_website_sales')[1],
|
2014-01-30 23:39:51 +00:00
|
|
|
}
|
2014-04-28 10:28:57 +00:00
|
|
|
sale_order_id = sale_order_obj.create(cr, SUPERUSER_ID, values, context=context)
|
|
|
|
values = sale_order_obj.onchange_partner_id(cr, SUPERUSER_ID, [], partner.id, context=context)['value']
|
|
|
|
sale_order_obj.write(cr, SUPERUSER_ID, [sale_order_id], values, context=context)
|
2014-02-25 12:57:36 +00:00
|
|
|
request.session['sale_order_id'] = sale_order_id
|
2014-01-30 23:39:51 +00:00
|
|
|
if sale_order_id:
|
2014-04-28 10:28:57 +00:00
|
|
|
# TODO cache partner_id session
|
|
|
|
partner = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context).partner_id
|
|
|
|
|
|
|
|
sale_order = sale_order_obj.browse(cr, SUPERUSER_ID, sale_order_id, context=context)
|
2014-04-10 14:35:24 +00:00
|
|
|
if not sale_order.exists():
|
|
|
|
request.session['sale_order_id'] = None
|
|
|
|
return None
|
2014-04-28 10:28:57 +00:00
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
# check for change of pricelist with a coupon
|
|
|
|
if code and code != sale_order.pricelist_id.code:
|
|
|
|
pricelist_ids = self.pool['product.pricelist'].search(cr, SUPERUSER_ID, [('code', '=', code)], context=context)
|
|
|
|
if pricelist_ids:
|
|
|
|
pricelist_id = pricelist_ids[0]
|
2014-04-28 10:28:57 +00:00
|
|
|
request.session['sale_order_code_pricelist_id'] = pricelist_id
|
2014-06-29 21:31:16 +00:00
|
|
|
update_pricelist = True
|
2013-09-02 10:46:46 +00:00
|
|
|
|
2014-06-29 21:31:16 +00:00
|
|
|
pricelist_id = request.session.get('sale_order_code_pricelist_id') or partner.property_product_pricelist.id
|
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
# check for change of partner_id ie after signup
|
2014-05-05 12:47:02 +00:00
|
|
|
if sale_order.partner_id.id != partner.id and request.website.partner_id.id != partner.id:
|
2014-04-28 10:28:57 +00:00
|
|
|
flag_pricelist = False
|
|
|
|
if pricelist_id != sale_order.pricelist_id.id:
|
|
|
|
flag_pricelist = True
|
|
|
|
fiscal_position = sale_order.fiscal_position and sale_order.fiscal_position.id or False
|
|
|
|
|
|
|
|
values = sale_order_obj.onchange_partner_id(cr, SUPERUSER_ID, [sale_order_id], partner.id, context=context)['value']
|
2014-07-11 13:29:22 +00:00
|
|
|
if values.get('fiscal_position'):
|
|
|
|
order_lines = map(int,sale_order.order_line)
|
|
|
|
values.update(sale_order_obj.onchange_fiscal_position(cr, SUPERUSER_ID, [],
|
|
|
|
values['fiscal_position'], [[6, 0, order_lines]], context=context)['value'])
|
2014-05-05 09:23:00 +00:00
|
|
|
|
2014-04-28 10:28:57 +00:00
|
|
|
values['partner_id'] = partner.id
|
|
|
|
sale_order_obj.write(cr, SUPERUSER_ID, [sale_order_id], values, context=context)
|
|
|
|
|
2015-04-01 14:13:44 +00:00
|
|
|
if flag_pricelist or values.get('fiscal_position', False) != fiscal_position:
|
2014-06-29 21:31:16 +00:00
|
|
|
update_pricelist = True
|
|
|
|
|
|
|
|
# update the pricelist
|
|
|
|
if update_pricelist:
|
|
|
|
values = {'pricelist_id': pricelist_id}
|
|
|
|
values.update(sale_order.onchange_pricelist_id(pricelist_id, None)['value'])
|
|
|
|
sale_order.write(values)
|
|
|
|
for line in sale_order.order_line:
|
2015-03-04 14:34:13 +00:00
|
|
|
if line.exists():
|
|
|
|
sale_order._cart_update(product_id=line.product_id.id, line_id=line.id, add_qty=0)
|
2014-04-28 10:28:57 +00:00
|
|
|
|
2014-05-05 12:47:02 +00:00
|
|
|
# update browse record
|
|
|
|
if (code and code != sale_order.pricelist_id.code) or sale_order.partner_id.id != partner.id:
|
|
|
|
sale_order = sale_order_obj.browse(cr, SUPERUSER_ID, sale_order.id, context=context)
|
|
|
|
|
2014-01-30 23:39:51 +00:00
|
|
|
return sale_order
|
2013-09-24 12:19:44 +00:00
|
|
|
|
2014-02-25 12:57:36 +00:00
|
|
|
def sale_get_transaction(self, cr, uid, ids, context=None):
|
2014-01-30 23:39:51 +00:00
|
|
|
transaction_obj = self.pool.get('payment.transaction')
|
2014-02-25 12:57:36 +00:00
|
|
|
tx_id = request.session.get('sale_transaction_id')
|
2014-01-30 23:39:51 +00:00
|
|
|
if tx_id:
|
2014-09-17 10:58:08 +00:00
|
|
|
tx_ids = transaction_obj.search(cr, SUPERUSER_ID, [('id', '=', tx_id), ('state', 'not in', ['cancel'])], context=context)
|
2014-01-30 23:39:51 +00:00
|
|
|
if tx_ids:
|
2014-09-17 10:58:08 +00:00
|
|
|
return transaction_obj.browse(cr, SUPERUSER_ID, tx_ids[0], context=context)
|
2014-01-30 23:39:51 +00:00
|
|
|
else:
|
2014-02-25 12:57:36 +00:00
|
|
|
request.session['sale_transaction_id'] = False
|
2014-01-30 23:39:51 +00:00
|
|
|
return False
|
2013-09-24 12:19:44 +00:00
|
|
|
|
2014-02-25 12:57:36 +00:00
|
|
|
def sale_reset(self, cr, uid, ids, context=None):
|
|
|
|
request.session.update({
|
2014-01-30 23:39:51 +00:00
|
|
|
'sale_order_id': False,
|
|
|
|
'sale_transaction_id': False,
|
2014-04-28 10:28:57 +00:00
|
|
|
'sale_order_code_pricelist_id': False,
|
2014-01-30 23:39:51 +00:00
|
|
|
})
|
2014-01-07 13:59:33 +00:00
|
|
|
|
2014-06-18 15:43:14 +00:00
|
|
|
|