Skip to content

Commit

Permalink
Merge pull request OCA#345 from TelmoSenseFly/BSSFL-470
Browse files Browse the repository at this point in the history
[BSSFL-470] Delivery method reset
  • Loading branch information
oleksandrpaziuk authored Feb 12, 2018
2 parents 36399c7 + 3d47a93 commit 96b9c6d
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 3 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ latest (unreleased)
* BSSFL-475: Separation between "Action to do" section and "resolution" in Repairs
* BSSFL-474: DN/Invoice swap invoice and delivery address
* BSSFL-473: Downpayment invoice number sequence
* BSSFL-470: Delivery method reset

**Bugfixes**

Expand All @@ -46,6 +47,7 @@ latest (unreleased)
* BSSFL-464: Payment order, payment reference
* BSSFL-467: MO Produce action set consumed products as Done
* BSSFL-468: Popup reserve stock
* BSSFL-464: Payment order, payment reference

**Bugfixes**

Expand Down
60 changes: 57 additions & 3 deletions odoo/local-src/sf_sale/models/sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,67 @@
class SaleOrder(models.Model):
_inherit = "sale.order"

def _reset_delivery_method(self, order_lines):
"""Check if reset delivery method is needed.
Delivery method field must be reseted when a change that
affects shipping calculation is applied on sale order lines"""

def is_service(product_id):
if product_id:
product = self.env['product.product'].browse(product_id)
if product.type == 'service':
return True
return False

# RMAs under warranty? Do not reset delivery method.
# SO of type RMA and RMA with decision Free or Commercial Gesture
if self.type_id.id == self.env.ref('sf_rma.rma_sale_type').id \
and self.rma_id.decision in ('free', 'to_offer'):
return False

# Check the changes on the order lines
to_reset = False
for line_vals in order_lines:
if len(line_vals) > 1 and line_vals[1]:
line = self.env['sale.order.line'].browse(line_vals[1])
line_product_type = line.product_id.type

# New record. Adding physical product
if line_vals[0] == 0 \
and not is_service(line_vals[2]['product_id']):
to_reset = True
break
# Update linked record.
# Changing qty of physical product
elif line_vals[0] == 1 \
and line_product_type != 'service' \
and 'product_uom_qty' in line_vals[2]:
to_reset = True
break
# Update linked record.
# Changing from a service to a physical product
elif line_vals[0] == 1 and line_product_type == 'service' \
and 'product_id' in line_vals[2] \
and not is_service(line_vals[2]['product_id']):
to_reset = True
break
# Remove and delete the linked record
elif line_vals[0] == 2 and line_product_type != 'service':
to_reset = True
break
return to_reset

@api.multi
def write(self, vals):
for sale_order in self:
# Reset delivery method (except for delivery method managers)
if sale_order.state in ('draft', 'sent') and 'order_line' in vals\
and not self.env.user.has_group(
'sf_stock.group_delivery_method_manager'):
delivery_method_man = self.env.user.has_group(
'sf_stock.group_delivery_method_manager'
)
if sale_order.state in ('draft', 'sent') \
and 'order_line' in vals \
and not delivery_method_man \
and self._reset_delivery_method(vals['order_line']):
sale_order.carrier_id = False

# Propagate carrier to stock.picking when confirming SO
Expand Down
1 change: 1 addition & 0 deletions odoo/local-src/sf_sale/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# -*- coding: utf-8 -*-
from . import test_to_invoice_status
from . import test_delivery_method_reset
112 changes: 112 additions & 0 deletions odoo/local-src/sf_sale/tests/test_delivery_method_reset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# -*- coding: utf-8 -*-
# Part of sensefly.


from datetime import datetime

import odoo.tests.common as test_common


class TestSale(test_common.TransactionCase):

def setUp(self):
super(TestSale, self).setUp()
Product = self.env['product.product']
SaleOrder = self.env['sale.order']
SaleOrderLine = self.env['sale.order.line']
partner = self.env.ref('base.res_partner_12')
self.product = Product.create({
'name': 'eWasp',
'type': 'product',
'uom_id': self.env.ref('product.product_uom_unit').id,
})
self.service = Product.create({
'name': 'service aws',
'type': 'service',
})

self.sale = SaleOrder.create({
'partner_id': partner.id,
'partner_invoice_id': partner.id,
'partner_shipping_id': partner.id,
'date_order': datetime.today(),
'pricelist_id': self.env.ref('product.list0').id,
'carrier_id': self.env.ref('delivery.free_delivery_carrier').id,
'ignore_exception': True
})

SaleOrderLine.create({
'order_id': self.sale.id,
'product_id': self.product.id,
'name': self.product.name,
'product_uom_qty': 2,
'product_uom': self.product.uom_id.id,
'price_unit': 123,
})

SaleOrderLine.create({
'order_id': self.sale.id,
'product_id': self.service.id,
'name': self.service.name,
'product_uom_qty': 1,
'price_unit': 100,
})

def test_create_sale_line_reset_delivery_method(self):
# Add service line
self.sale.order_line = [(0, 0, {
'product_id': self.service.id,
'name': self.service.name,
'product_uom_qty': 1,
'price_unit': 20,

})]
self.assertIsNotNone(self.sale.carrier_id)

# Add physical product line
self.sale.order_line = [(0, 0, {
'product_id': self.product.id,
'name': self.product.name,
'product_uom_qty': 1,
'price_unit': 30,

})]
self.assertFalse(self.sale.carrier_id)

def test_update_sale_line_reset_delivery_method(self):
# Update service line
service_line = self.sale.order_line.filtered(
lambda l: l.product_id.type == 'service'
)
self.sale.write(
{'order_line': [(1, service_line.id, {'product_uom_qty': 21})]}
)
self.assertIsNotNone(self.sale.carrier_id)

# Update physical product line
product_line = self.sale.order_line.filtered(
lambda l: l.product_id.type == 'product'
)
self.sale.write(
{'order_line': [(1, product_line.id, {'product_uom_qty': 21})]}
)
self.assertFalse(self.sale.carrier_id)

def test_delete_sale_line_reset_delivery_method(self):
# Delete service line
service_line = self.sale.order_line.filtered(
lambda l: l.product_id.type == 'service'
)
self.sale.write(
{'order_line': [[2, service_line.id, False]]}
)
self.assertIsNotNone(self.sale.carrier_id)

# Update physical product line
product_line = self.sale.order_line.filtered(
lambda l: l.product_id.type == 'product'
)
self.sale.write(
{'order_line': [(2, product_line.id, False)]}
)
self.assertFalse(self.sale.carrier_id)

0 comments on commit 96b9c6d

Please sign in to comment.