2013-11-16 22:43:06 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from openerp.osv import orm, fields
|
2014-01-17 15:46:48 +00:00
|
|
|
from openerp import SUPERUSER_ID
|
2014-01-23 17:42:08 +00:00
|
|
|
from openerp.addons import decimal_precision
|
2013-11-16 22:43:06 +00:00
|
|
|
|
2013-12-17 15:26:05 +00:00
|
|
|
|
2013-11-16 22:43:06 +00:00
|
|
|
class delivery_carrier(orm.Model):
|
|
|
|
_inherit = 'delivery.carrier'
|
|
|
|
_columns = {
|
|
|
|
'website_published': fields.boolean('Available in the website'),
|
|
|
|
'website_description': fields.text('Description for the website'),
|
|
|
|
}
|
|
|
|
_defaults = {
|
|
|
|
'website_published': True
|
|
|
|
}
|
|
|
|
|
2013-12-17 15:26:05 +00:00
|
|
|
|
|
|
|
class SaleOrder(orm.Model):
|
2013-11-16 22:43:06 +00:00
|
|
|
_inherit = 'sale.order'
|
2013-12-17 15:26:05 +00:00
|
|
|
|
2014-01-30 17:37:05 +00:00
|
|
|
def _amount_all_wrapper(self, cr, uid, ids, field_name, arg, context=None):
|
2014-01-23 17:42:08 +00:00
|
|
|
""" Wrapper because of direct method passing as parameter for function fields """
|
|
|
|
return self._amount_all(cr, uid, ids, field_name, arg, context=context)
|
|
|
|
|
|
|
|
def _amount_all(self, cr, uid, ids, field_name, arg, context=None):
|
|
|
|
res = super(SaleOrder, self)._amount_all(cr, uid, ids, field_name, arg, context=context)
|
2014-01-30 17:37:05 +00:00
|
|
|
currency_pool = self.pool.get('res.currency')
|
2014-01-23 17:42:08 +00:00
|
|
|
for order in self.browse(cr, uid, ids, context=context):
|
|
|
|
line_amount = sum([line.price_subtotal for line in order.order_line if line.is_delivery])
|
|
|
|
currency = order.pricelist_id.currency_id
|
2014-01-30 17:37:05 +00:00
|
|
|
res[order.id]['amount_delivery'] = currency_pool.round(cr, uid, currency, line_amount)
|
2014-01-23 17:42:08 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
def _get_order(self, cr, uid, ids, context=None):
|
|
|
|
result = {}
|
|
|
|
for line in self.pool.get('sale.order.line').browse(cr, uid, ids, context=context):
|
|
|
|
result[line.order_id.id] = True
|
|
|
|
return result.keys()
|
|
|
|
|
2013-12-18 13:21:50 +00:00
|
|
|
_columns = {
|
2014-01-23 17:42:08 +00:00
|
|
|
'amount_delivery': fields.function(
|
|
|
|
_amount_all_wrapper, type='float', digits_compute=decimal_precision.get_precision('Account'),
|
|
|
|
string='Delivery Amount',
|
|
|
|
store={
|
|
|
|
'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),
|
|
|
|
'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
|
|
|
|
},
|
|
|
|
multi='sums', help="The amount without tax.", track_visibility='always'
|
|
|
|
),
|
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,
|
|
|
|
domain=[('is_delivery', '=', False)],
|
|
|
|
help='Order Lines to be displayed on the website. They should not be used for computation purpose.',
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2014-04-02 14:45:20 +00:00
|
|
|
def _check_carrier_quotation(self, cr, uid, order, force_carrier_id=None, context=None):
|
|
|
|
carrier_obj = self.pool.get('delivery.carrier')
|
|
|
|
|
|
|
|
# check to add or remove carrier_id
|
2014-04-02 15:20:43 +00:00
|
|
|
if not order:
|
|
|
|
return False
|
2014-04-02 14:45:20 +00:00
|
|
|
if all(line.product_id.type == "service" for line in order.website_order_line):
|
|
|
|
order.write({'carrier_id': None}, context=context)
|
2014-04-04 10:25:12 +00:00
|
|
|
self.pool['sale.order']._delivery_unset(cr, SUPERUSER_ID, [order.id], context=context)
|
2014-04-02 14:45:20 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
carrier_id = force_carrier_id or order.carrier_id.id
|
2014-04-04 10:25:12 +00:00
|
|
|
carrier_ids = self._get_delivery_methods(cr, uid, order, context=context)
|
|
|
|
if carrier_id not in carrier_ids:
|
|
|
|
carrier_id = False
|
|
|
|
else:
|
|
|
|
carrier_ids.insert(0, carrier_ids.pop(carrier_id))
|
|
|
|
if force_carrier_id or not carrier_id or not carrier_id in carrier_ids:
|
2014-04-02 14:45:20 +00:00
|
|
|
for delivery_id in carrier_ids:
|
2014-04-02 15:28:14 +00:00
|
|
|
grid_id = carrier_obj.grid_get(cr, SUPERUSER_ID, [delivery_id], order.partner_shipping_id.id)
|
2014-04-02 14:45:20 +00:00
|
|
|
if grid_id:
|
|
|
|
carrier_id = delivery_id
|
|
|
|
break
|
|
|
|
order.write({'carrier_id': carrier_id}, context=context)
|
|
|
|
if carrier_id:
|
|
|
|
order.delivery_set(context=context)
|
2014-04-04 10:25:12 +00:00
|
|
|
else:
|
|
|
|
order._delivery_unset(context=context)
|
2014-04-02 14:45:20 +00:00
|
|
|
|
|
|
|
return bool(carrier_id)
|
|
|
|
|
|
|
|
def _get_delivery_methods(self, cr, uid, order, context=None):
|
|
|
|
carrier_obj = self.pool.get('delivery.carrier')
|
|
|
|
delivery_ids = carrier_obj.search(cr, uid, [('website_published','=',True)], context=context)
|
|
|
|
# Following loop is done to avoid displaying delivery methods who are not available for this order
|
|
|
|
# This can surely be done in a more efficient way, but at the moment, it mimics the way it's
|
|
|
|
# done in delivery_set method of sale.py, from delivery module
|
|
|
|
for delivery_id in list(delivery_ids):
|
2014-04-02 15:28:14 +00:00
|
|
|
grid_id = carrier_obj.grid_get(cr, SUPERUSER_ID, [delivery_id], order.partner_shipping_id.id)
|
2014-04-02 14:45:20 +00:00
|
|
|
if not grid_id:
|
|
|
|
delivery_ids.remove(delivery_id)
|
|
|
|
return delivery_ids
|
|
|
|
|
|
|
|
def _get_errors(self, cr, uid, order, context=None):
|
|
|
|
errors = super(SaleOrder, self)._get_errors(cr, uid, order, context=context)
|
|
|
|
if not self._get_delivery_methods(cr, uid, order, context=context):
|
|
|
|
errors.append(('No delivery method available', 'There is no available delivery method for your order'))
|
|
|
|
return errors
|
|
|
|
|
2013-11-16 22:43:06 +00:00
|
|
|
def _get_website_data(self, cr, uid, order, context=None):
|
2013-12-17 15:26:05 +00:00
|
|
|
""" Override to add delivery-related website data. """
|
|
|
|
values = super(SaleOrder, self)._get_website_data(cr, uid, order, context=context)
|
2013-11-16 22:43:06 +00:00
|
|
|
# We need a delivery only if we have stockable products
|
2013-12-18 13:21:50 +00:00
|
|
|
has_stockable_products = False
|
2013-11-16 22:43:06 +00:00
|
|
|
for line in order.order_line:
|
2013-12-17 15:26:05 +00:00
|
|
|
if line.product_id.type in ('consu', 'product'):
|
2013-12-18 13:21:50 +00:00
|
|
|
has_stockable_products = True
|
|
|
|
if not has_stockable_products:
|
2013-12-17 15:26:05 +00:00
|
|
|
return values
|
2013-11-16 22:43:06 +00:00
|
|
|
|
2013-12-18 13:21:50 +00:00
|
|
|
delivery_ctx = dict(context, order_id=order.id)
|
|
|
|
DeliveryCarrier = self.pool.get('delivery.carrier')
|
2014-04-02 14:45:20 +00:00
|
|
|
delivery_ids = self._get_delivery_methods(cr, uid, order, context=context)
|
|
|
|
|
2014-01-17 15:46:48 +00:00
|
|
|
values['deliveries'] = DeliveryCarrier.browse(cr, SUPERUSER_ID, delivery_ids, context=delivery_ctx)
|
2013-12-17 15:26:05 +00:00
|
|
|
return values
|