From a5f6748f3298b357ecfbd75f7f1665a6124c8863 Mon Sep 17 00:00:00 2001 From: KolushovAlexandr Date: Fri, 13 Dec 2019 17:43:04 +0500 Subject: [PATCH] :zap: order splitting --- .../controllers/main.py | 35 ++++++----- .../models/sale_order.py | 46 +++++++------- .../models/website.py | 6 +- .../static/src/css/website_cart.css | 7 ++- .../static/src/js/website_cart.js | 7 ++- .../views/templates.xml | 63 ++++++++++++++++++- .../views/website_views.xml | 6 +- 7 files changed, 118 insertions(+), 52 deletions(-) diff --git a/website_multi_company_separate_orders/controllers/main.py b/website_multi_company_separate_orders/controllers/main.py index 964ee5130..14487b94e 100644 --- a/website_multi_company_separate_orders/controllers/main.py +++ b/website_multi_company_separate_orders/controllers/main.py @@ -24,9 +24,9 @@ def _check_and_update_child_order(self, sale_order, product_id, add_qty, set_qty product_company_id = product_id.company_id.id order_company = sale_order.company_id website_id = sale_order.website_id - if not website_id.order_duplicating or not \ + if not website_id.split_orders or not \ (product_company_id and product_company_id != order_company.id and - (product_company_id in website_id.order_duplicating_companies.ids or force or (not add_qty and set_qty == 0))): + (product_company_id in website_id.split_orders_companies.ids or force or (not add_qty and set_qty == 0))): return False sale_order_child = sale_order.order_child_ids.filtered(lambda so: so.company_id.id == product_company_id) if not sale_order_child: @@ -45,14 +45,14 @@ def _check_and_update_child_order(self, sale_order, product_id, add_qty, set_qty _logger.info("website_sale created new child order for company: %s for order: %s", product_company_id, sale_order.id) - if set_qty == 0 and not add_qty: - deleted_order_lines = sale_order_child.order_line.filtered(lambda ol: ol.product_id == product_id) - deleted_order_lines.unlink() - return sale_order_child + # if set_qty == 0 and not add_qty: + # deleted_order_lines = sale_order_child.order_line.filtered(lambda ol: ol.product_id == product_id) + # deleted_order_lines.unlink() + # return sale_order_child + product_custom_attribute_values = None if kw.get('product_custom_attribute_values'): product_custom_attribute_values = json.loads(kw.get('product_custom_attribute_values')) - no_variant_attribute_values = None if kw.get('no_variant_attribute_values'): no_variant_attribute_values = json.loads(kw.get('no_variant_attribute_values')) @@ -64,15 +64,17 @@ def _check_and_update_child_order(self, sale_order, product_id, add_qty, set_qty product_custom_attribute_values=product_custom_attribute_values, no_variant_attribute_values=no_variant_attribute_values ) + sale_order.order_line.filtered(lambda ol: ol.product_id == product_id).unlink() return sale_order_child @http.route() def cart_update(self, product_id, add_qty=1, set_qty=0, **kw): result = super(WebsiteSaleExtended, self).cart_update(product_id, add_qty, set_qty, **kw) - sale_order = request.website.sale_get_order() - prod_id = request.env['product.product'].browse(int(product_id)) - self._check_and_update_child_order(sale_order, prod_id, add_qty, set_qty, **kw) + if add_qty: + sale_order = request.website.sale_get_order() + prod_id = request.env['product.product'].browse(int(product_id)) + self._check_and_update_child_order(sale_order, prod_id, add_qty, set_qty, **kw) return result @@ -80,15 +82,16 @@ def cart_update(self, product_id, add_qty=1, set_qty=0, **kw): def cart_update_json(self, product_id, line_id=None, add_qty=None, set_qty=None, display=True): result = super(WebsiteSaleExtended, self).cart_update_json(product_id, line_id, add_qty, set_qty, display) - sale_order = request.website.sale_get_order() - prod_id = request.env['product.product'].browse(int(product_id)) - self._check_and_update_child_order(sale_order, prod_id, add_qty, set_qty) + if set_qty: + sale_order = request.website.sale_get_order() + prod_id = request.env['product.product'].browse(int(product_id)) + self._check_and_update_child_order(sale_order, prod_id, add_qty, set_qty) return result - @http.route(['/shop/duplicate_orders_for_daughter_companies/'], + @http.route(['/shop/split_order_for_daughter_companies/'], type='json', auth="public", methods=['POST'], website=True, csrf=False) - def duplicate_orders_for_daughter_companies(self, company_id=False): + def split_order_for_daughter_companies(self, company_id=False): order = request.website.sale_get_order() lines = order.order_line.filtered(lambda ol: ol.product_id.company_id.id == company_id) if not lines: @@ -96,7 +99,7 @@ def duplicate_orders_for_daughter_companies(self, company_id=False): result = [] for line in lines: prod = line.product_id - result.append(self._check_and_update_child_order(order, prod, False, line.product_uom_qty, 'Force')) + result.append(self._check_and_update_child_order(order, prod, line.product_uom_qty, False, 'Force')) return all(result) diff --git a/website_multi_company_separate_orders/models/sale_order.py b/website_multi_company_separate_orders/models/sale_order.py index 31cdb235f..544954517 100644 --- a/website_multi_company_separate_orders/models/sale_order.py +++ b/website_multi_company_separate_orders/models/sale_order.py @@ -48,26 +48,26 @@ def write(self, values): return result -class AccountInvoice(models.Model): - _inherit = 'account.invoice' - - @api.multi - def register_payment(self, payment_line, writeoff_acc_id=False, writeoff_journal_id=False): - result = super(AccountInvoice, self).register_payment(payment_line, writeoff_acc_id, writeoff_journal_id) - for record in self: - if record.state != 'paid': - return result - sale_line_ids = record.invoice_line_ids[0].sale_line_ids - if sale_line_ids: - order = sale_line_ids[0].order_id.sudo() - children = order.order_child_ids.filtered(lambda o: o.invoice_status not in ['cancel', 'invoiced']) - if children: - children.action_cancel() - parent = order.order_parent_id - if parent: - product_ids = order.order_line.mapped(lambda ol: ol.product_id.id) - order_line_ids = parent.order_line.filtered(lambda ol: ol.product_id.id in product_ids) - order_line_ids.write({ - 'product_uom_qty': 0, - }) - return result +# class AccountInvoice(models.Model): +# _inherit = 'account.invoice' +# +# @api.multi +# def register_payment(self, payment_line, writeoff_acc_id=False, writeoff_journal_id=False): +# result = super(AccountInvoice, self).register_payment(payment_line, writeoff_acc_id, writeoff_journal_id) +# for record in self: +# if record.state != 'paid': +# return result +# sale_line_ids = record.invoice_line_ids[0].sale_line_ids +# if sale_line_ids: +# order = sale_line_ids[0].order_id.sudo() +# children = order.order_child_ids.filtered(lambda o: o.invoice_status not in ['cancel', 'invoiced']) +# if children: +# children.action_cancel() +# parent = order.order_parent_id +# if parent: +# product_ids = order.order_line.mapped(lambda ol: ol.product_id.id) +# order_line_ids = parent.order_line.filtered(lambda ol: ol.product_id.id in product_ids) +# order_line_ids.write({ +# 'product_uom_qty': 0, +# }) +# return result diff --git a/website_multi_company_separate_orders/models/website.py b/website_multi_company_separate_orders/models/website.py index b2466c114..8590775c2 100644 --- a/website_multi_company_separate_orders/models/website.py +++ b/website_multi_company_separate_orders/models/website.py @@ -13,9 +13,9 @@ class Website(models.Model): _inherit = "website" - order_duplicating = fields.Boolean(string='Automatic Order Duplicating', - help='Duplicate orders for Daughter companies') - order_duplicating_companies = fields.Many2many('res.company', string='Order Duplicating Companies', + split_orders = fields.Boolean(string='Automatic Order Splitting', + help='Split orders for Daughter companies') + split_orders_companies = fields.Many2many('res.company', string='Order Duplicating Companies', help='Daughter companies where order is being automatically duplicated to') @api.model diff --git a/website_multi_company_separate_orders/static/src/css/website_cart.css b/website_multi_company_separate_orders/static/src/css/website_cart.css index 0a29ef0e0..f549bb1cd 100644 --- a/website_multi_company_separate_orders/static/src/css/website_cart.css +++ b/website_multi_company_separate_orders/static/src/css/website_cart.css @@ -1,6 +1,9 @@ .separate_order { - cursor:pointer; color: #00A09D; - text-decoration:underline; font-size: 13px; } + +.separate_order:not(.child_order) { + cursor:pointer; + text-decoration:underline; +} diff --git a/website_multi_company_separate_orders/static/src/js/website_cart.js b/website_multi_company_separate_orders/static/src/js/website_cart.js index d4b845b83..baa87b1f9 100644 --- a/website_multi_company_separate_orders/static/src/js/website_cart.js +++ b/website_multi_company_separate_orders/static/src/js/website_cart.js @@ -9,7 +9,7 @@ var sAnimations = require('website.content.snippets.animation'); sAnimations.registry.WebsiteSale.include({ read_events: _.extend(sAnimations.registry.WebsiteSale.prototype.read_events, { - 'click td.td-product_name .separate_order': '_duplicate_order_request', + 'click td.td-product_name .separate_order:not(.child_order)': '_duplicate_order_request', }), _duplicate_order_request: function(event) { @@ -17,10 +17,11 @@ sAnimations.registry.WebsiteSale.include({ if (!cid) { return; } - session.rpc("/shop/duplicate_orders_for_daughter_companies/" + cid, {}) + session.rpc("/shop/split_order_for_daughter_companies/" + cid, {}) .then(function(result) { if (result) { - alert('Order was duplicated'); + // alert('Order was duplicated'); + location.reload(); } }); }, diff --git a/website_multi_company_separate_orders/views/templates.xml b/website_multi_company_separate_orders/views/templates.xml index 3e956ffc2..7ca82ed6c 100644 --- a/website_multi_company_separate_orders/views/templates.xml +++ b/website_multi_company_separate_orders/views/templates.xml @@ -3,19 +3,78 @@ License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -->