Skip to content

Commit

Permalink
⚡ order splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
KolushovAlexandr committed Dec 13, 2019
1 parent c3461f5 commit bfd925d
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 52 deletions.
35 changes: 19 additions & 16 deletions website_multi_company_separate_orders/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'))
Expand All @@ -64,39 +64,42 @@ 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

@http.route()
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/<int:company_id>'],
@http.route(['/shop/split_order_for_daughter_companies/<int:company_id>'],
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:
return 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)


Expand Down
46 changes: 23 additions & 23 deletions website_multi_company_separate_orders/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions website_multi_company_separate_orders/models/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ 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) {
var cid = event.target.attributes.pcid.value;
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();
}
});
},
Expand Down
63 changes: 61 additions & 2 deletions website_multi_company_separate_orders/views/templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,78 @@
License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -->
<odoo>
<template id="cart_lines" name="duplicate order assets" inherit_id="website_sale.cart_lines">

<!-- <xpath expr="//div[hasclass('oe_cart')]" position="before">-->
<!-- <div class="col-12">-->
<!-- Since the items in your shopping cart belong to different companies, please place an order separately-->
<!-- </div>-->
<!-- </xpath>-->

<xpath expr="//td[hasclass('td-product_name')]/div" position="after">
<t t-set="order_company" t-value="website_sale_order.company_id"/>
<t t-set="product_company_id" t-value="line.product_id.company_id.id"/>
<t t-set="website_id" t-value="website_sale_order.website_id"/>
<t t-if="website_id.order_duplicating and
<t t-if="website_id.split_orders and
order_company.id != product_company_id and
product_company_id in order_company.child_ids.ids and
product_company_id not in website_sale_order.website_id.order_duplicating_companies.ids">
product_company_id not in website_sale_order.website_id.split_orders_companies.ids">
<div class="separate_order" t-attf-pcid="{{product_company_id}}">
(duplicate order to <t t-esc="line.product_id.company_id.name"/>)
</div>
</t>
</xpath>

<xpath expr="//tbody" position="inside">
<t t-set="child_lines" t-value="website_sale_order.order_child_ids.mapped('website_order_line')"/>
<t t-foreach="child_lines" t-as="line">
<tr t-att-class="'optional_product info' if line.linked_line_id else None" style="opacity: 0.5;">
<td colspan="2" t-if="not line.product_id.product_tmpl_id" class='td-img'></td>
<td align="center" t-if="line.product_id.product_tmpl_id" class='td-img'>
<span t-field="line.product_id.image_small" t-options="{'widget': 'image', 'class': 'rounded'}" />
</td>
<td t-if="line.product_id.product_tmpl_id" class='td-product_name'>
<div>
<t t-call="website_sale.cart_line_product_link">
<strong t-field="line.name_short" />
</t>
</div>
<div>
<div class="separate_order child_order">
(<t t-esc="line.order_id.company_id.name"/>)
</div>
</div>
<t t-call="website_sale.cart_line_description_following_lines">
<t t-set="div_class" t-value="'d-none d-md-block'"/>
</t>
</td>
<td class="text-center td-qty">
<div t-if="not line.linked_line_id" class="css_quantity input-group mx-auto oe_website_spinner">
<input readonly="True" type="text" class="js_quantity form-control quantity" t-att-data-line-id="line.id" t-att-data-product-id="line.product_id.id" t-att-value="int(line.product_uom_qty) == line.product_uom_qty and int(line.product_uom_qty) or line.product_uom_qty" />
</div>
<t t-if="line.linked_line_id">
<span class="js_quantity text-muted" t-att-data-line-id="line.id" t-att-data-product-id="line.product_id.id" t-esc="int(line.product_uom_qty)"/>
</t>
</td>
<td class="text-center td-price" name="price">
<t t-set="combination" t-value="line.product_id.product_template_attribute_value_ids + line.product_no_variant_attribute_value_ids"/>
<t t-set="combination_info" t-value="line.product_id.product_tmpl_id._get_combination_info(combination)"/>

<t t-set="list_price_converted" t-value="website.currency_id._convert(combination_info['list_price'], website_sale_order.currency_id, website_sale_order.company_id, date)"/>
<t groups="account.group_show_line_subtotals_tax_excluded" t-if="(website_sale_order.pricelist_id.discount_policy == 'without_discount' and website_sale_order.currency_id.compare_amounts(list_price_converted, line.price_reduce_taxexcl) == 1) or website_sale_order.currency_id.compare_amounts(line.price_unit, line.price_reduce) == 1" name="order_line_discount">
<del t-attf-class="#{'text-danger mr8'}" style="white-space: nowrap;" t-esc="list_price_converted" t-options="{'widget': 'monetary', 'display_currency': website_sale_order.currency_id}" />
</t>
<span t-field="line.price_reduce_taxexcl" style="white-space: nowrap;" t-options="{'widget': 'monetary', 'display_currency': website_sale_order.currency_id}" groups="account.group_show_line_subtotals_tax_excluded" />
<t groups="account.group_show_line_subtotals_tax_included" t-if="(website_sale_order.pricelist_id.discount_policy == 'without_discount' and website_sale_order.currency_id.compare_amounts(list_price_converted, line.price_reduce_taxinc) == 1) or website_sale_order.currency_id.compare_amounts(line.price_unit, line.price_reduce) == 1" name="order_line_discount">
<del t-attf-class="#{'text-danger mr8'}" style="white-space: nowrap;" t-esc="list_price_converted" t-options="{'widget': 'monetary', 'display_currency': website_sale_order.currency_id}" />
</t>
<span t-field="line.price_reduce_taxinc" style="white-space: nowrap;" t-options="{'widget': 'monetary', 'display_currency': website_sale_order.currency_id}" groups="account.group_show_line_subtotals_tax_included" />
</td>
<td class="td-action">
</td>
</tr>
</t>
</xpath>

</template>

<template id="assets_frontend" inherit_id="website.assets_frontend" name="Shop Cart">
Expand Down
6 changes: 3 additions & 3 deletions website_multi_company_separate_orders/views/website_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<field name="inherit_id" ref="website.view_website_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='default_lang_id']" position="after">
<field name="order_duplicating"/>
<field name="order_duplicating_companies"
attrs="{'invisible': [('order_duplicating', '=', False)]}"
<field name="split_orders"/>
<field name="split_orders_companies"
attrs="{'invisible': [('split_orders', '=', False)]}"
widget="many2many_tags"/>
</xpath>
</field>
Expand Down

0 comments on commit bfd925d

Please sign in to comment.