Browse Source

Nov 30 : [FIX] Bug Fixed 'product_wise_shipping_method'

pull/295/head
AjmalCybro 1 year ago
parent
commit
de373b3adf
  1. 5
      product_wise_shipping_method/__manifest__.py
  2. 6
      product_wise_shipping_method/models/product_template.py
  3. 20
      product_wise_shipping_method/models/sale_order.py

5
product_wise_shipping_method/__manifest__.py

@ -24,11 +24,12 @@
'version': '16.0.1.0.0', 'version': '16.0.1.0.0',
'category': 'Sales/Sales', 'category': 'Sales/Sales',
'summary': """ Product Wise Shipping Method for sale order. """, 'summary': """ Product Wise Shipping Method for sale order. """,
'description': """ This module helps to provide shipping method easily via choosing products in order line. """, 'description': """ This module helps to provide shipping method easily via
choosing products in order line. """,
'author': 'Cybrosys Techno Solutions', 'author': 'Cybrosys Techno Solutions',
'company': 'Cybrosys Techno Solutions', 'company': 'Cybrosys Techno Solutions',
'maintainer': 'Cybrosys Techno Solutions', 'maintainer': 'Cybrosys Techno Solutions',
'depends': ['base', 'sale','delivery'], 'depends': ['base', 'sale', 'delivery', 'website_sale'],
'website': 'https://www.cybrosys.com', 'website': 'https://www.cybrosys.com',
'data': [ 'data': [
'views/product_template_views.xml', 'views/product_template_views.xml',

6
product_wise_shipping_method/models/product_template.py

@ -23,7 +23,9 @@ from odoo import fields, models
class ProductTemplate(models.Model): class ProductTemplate(models.Model):
""" Inherit product,so we are adding a field for defining shipping method. """ """Inherit product,so we are adding a field for defining shipping method."""
_inherit = 'product.template' _inherit = 'product.template'
shipping_method_id = fields.Many2one('delivery.carrier', string='Shipping Method',help="Choose shipping method") shipping_method_id = fields.Many2one('delivery.carrier',
string='Shipping Method',
help="Choose shipping method.")

20
product_wise_shipping_method/models/sale_order.py

@ -25,11 +25,15 @@ from odoo.exceptions import UserError
class SaleOrder(models.Model): class SaleOrder(models.Model):
""" Inherit sale,so we are getting shipping method corresponding to product""" """ Inherit sale,so we are getting shipping method corresponding to
product."""
_inherit = 'sale.order' _inherit = 'sale.order'
def action_confirm(self): def action_confirm(self):
"""Create delivery method value for sale order lines with a shipping method.""" """Create delivery method value for sale order lines with a
shipping method."""
if self.website_id:
return super().action_confirm()
order_lines_with_shipping = self.order_line.filtered(lambda line: line.product_template_id.shipping_method_id) order_lines_with_shipping = self.order_line.filtered(lambda line: line.product_template_id.shipping_method_id)
vals_list = [{ vals_list = [{
'product_id': line.product_template_id.shipping_method_id.product_id.id, 'product_id': line.product_template_id.shipping_method_id.product_id.id,
@ -43,25 +47,29 @@ class SaleOrder(models.Model):
def merge_similar_products(self): def merge_similar_products(self):
""" merge similar products on order line by adding their quantity.""" """ merge similar products on order line by adding their quantity."""
similar_products = defaultdict(lambda: {'qty': 0, 'lines_to_remove': []}) similar_products = defaultdict(
lambda: {'qty': 0, 'lines_to_remove': []})
order_line = self.env['sale.order.line'] order_line = self.env['sale.order.line']
for line in self.order_line.sorted(): for line in self.order_line.sorted():
key = (line.product_id.id, line.price_unit, line.tax_id) key = (line.product_id.id, line.price_unit, line.tax_id)
similar_products[key]['qty'] += line.product_uom_qty similar_products[key]['qty'] += line.product_uom_qty
similar_products[key]['lines_to_remove'].append(line.id) similar_products[key]['lines_to_remove'].append(line.id)
for key,product_info in similar_products.items(): for key,product_info in similar_products.items():
order_line.browse(product_info['lines_to_remove'][0]).write({'product_uom_qty': product_info['qty']}) order_line.browse(product_info['lines_to_remove'][0]).write(
{'product_uom_qty': product_info['qty']})
order_line.browse(product_info['lines_to_remove'][1:]).unlink() order_line.browse(product_info['lines_to_remove'][1:]).unlink()
@api.model @api.model
def create(self, vals): def create(self, vals):
""" When the products are newly selected in sale order line then they are Create on sale order """ """ When the products are newly selected in sale order line then they
are Create on sale order """
res = super(SaleOrder, self).create(vals) res = super(SaleOrder, self).create(vals)
res.merge_similar_products() res.merge_similar_products()
return res return res
def write(self, vals): def write(self, vals):
""" When the products are previously selected then Write similar products on sale order """ """ When the products are previously selected then Write similar
products on sale order """
res = super(SaleOrder, self).write(vals) res = super(SaleOrder, self).write(vals)
self.merge_similar_products() self.merge_similar_products()
return res return res

Loading…
Cancel
Save