mirror of
https://gitlab.com/hibou-io/hibou-odoo/suite.git
synced 2025-01-20 12:37:31 +02:00
IMP connector_opencart Support orders with 'coupons' key to build up a discount product line.
This commit is contained in:
@@ -5,6 +5,9 @@ import requests
|
|||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
from json import loads, dumps
|
from json import loads, dumps
|
||||||
|
|
||||||
|
import logging
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Opencart:
|
class Opencart:
|
||||||
|
|
||||||
@@ -37,11 +40,19 @@ class Opencart:
|
|||||||
if params:
|
if params:
|
||||||
encoded_url += '?%s' % urlencode(params)
|
encoded_url += '?%s' % urlencode(params)
|
||||||
headers = self.get_headers(encoded_url, method)
|
headers = self.get_headers(encoded_url, method)
|
||||||
|
_logger.debug('send_request method: %s url: %s headers: %s params: %s body: %s' % (
|
||||||
|
method,
|
||||||
|
url,
|
||||||
|
headers,
|
||||||
|
params,
|
||||||
|
body
|
||||||
|
))
|
||||||
if method == 'GET':
|
if method == 'GET':
|
||||||
return loads(self.session.get(url, params=params, headers=headers).text)
|
result_text = self.session.get(url, params=params, headers=headers).text
|
||||||
elif method == 'PUT' or method == 'POST':
|
elif method == 'PUT' or method == 'POST':
|
||||||
return loads(self.session.put(url, data=body, headers=headers).text)
|
result_text = self.session.put(url, data=body, headers=headers).text
|
||||||
|
_logger.debug('raw_text: ' + str(result_text))
|
||||||
|
return loads(result_text)
|
||||||
|
|
||||||
|
|
||||||
class Resource:
|
class Resource:
|
||||||
|
|||||||
@@ -62,6 +62,8 @@ class OpencartBackend(models.Model):
|
|||||||
"in Odoo.",
|
"in Odoo.",
|
||||||
)
|
)
|
||||||
# payment_mode_id = fields.Many2one(comodel_name='account.payment.mode', string="Payment Mode")
|
# payment_mode_id = fields.Many2one(comodel_name='account.payment.mode', string="Payment Mode")
|
||||||
|
coupon_product_id = fields.Many2one(comodel_name='product.product', string='Coupon Product',
|
||||||
|
help='Product to represent coupon discounts.')
|
||||||
|
|
||||||
# New Product fields.
|
# New Product fields.
|
||||||
product_categ_id = fields.Many2one(comodel_name='product.category', string='Product Category',
|
product_categ_id = fields.Many2one(comodel_name='product.category', string='Product Category',
|
||||||
|
|||||||
@@ -50,6 +50,8 @@ class OpencartStore(models.Model):
|
|||||||
"order 36071 in Opencart, will be named 'OC-36071' "
|
"order 36071 in Opencart, will be named 'OC-36071' "
|
||||||
"in Odoo. (overridden from backend)",
|
"in Odoo. (overridden from backend)",
|
||||||
)
|
)
|
||||||
|
coupon_product_id = fields.Many2one(comodel_name='product.product', string='Coupon Product',
|
||||||
|
help='Product to represent coupon discounts.')
|
||||||
|
|
||||||
|
|
||||||
class OpencartStoreAdapter(Component):
|
class OpencartStoreAdapter(Component):
|
||||||
|
|||||||
@@ -61,6 +61,34 @@ class SaleOrderImportMapper(Component):
|
|||||||
children = [('products', 'opencart_order_line_ids', 'opencart.sale.order.line'),
|
children = [('products', 'opencart_order_line_ids', 'opencart.sale.order.line'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def _add_coupon_lines(self, map_record, values):
|
||||||
|
# Data from API
|
||||||
|
# 'coupons': [{'amount': '7.68', 'code': '1111'}],
|
||||||
|
record = map_record.source
|
||||||
|
|
||||||
|
coupons = record.get('coupons')
|
||||||
|
if not coupons:
|
||||||
|
return values
|
||||||
|
|
||||||
|
coupon_product = self.options.store.coupon_product_id or self.backend_record.coupon_product_id
|
||||||
|
if not coupon_product:
|
||||||
|
coupon_product = self.env.ref('connector_ecommerce.product_product_discount', raise_if_not_found=False)
|
||||||
|
|
||||||
|
if not coupon_product:
|
||||||
|
raise ValueError('Coupon %s on order requires coupon product in configuration.' % (coupons, ))
|
||||||
|
for coupon in coupons:
|
||||||
|
line_builder = self.component(usage='order.line.builder')
|
||||||
|
line_builder.price_unit = -float(coupon.get('amount', 0.0))
|
||||||
|
line_builder.product = coupon_product
|
||||||
|
# `order.line.builder` does not allow naming.
|
||||||
|
values = line_builder.get_line()
|
||||||
|
code = coupon.get('code')
|
||||||
|
if code:
|
||||||
|
values['name'] = '%s Code: %s' % (coupon_product.name, code)
|
||||||
|
line = (0, 0, values)
|
||||||
|
values['order_line'].append(line)
|
||||||
|
return values
|
||||||
|
|
||||||
def _add_shipping_line(self, map_record, values):
|
def _add_shipping_line(self, map_record, values):
|
||||||
record = map_record.source
|
record = map_record.source
|
||||||
|
|
||||||
@@ -77,6 +105,7 @@ class SaleOrderImportMapper(Component):
|
|||||||
|
|
||||||
def finalize(self, map_record, values):
|
def finalize(self, map_record, values):
|
||||||
values.setdefault('order_line', [])
|
values.setdefault('order_line', [])
|
||||||
|
self._add_coupon_lines(map_record, values)
|
||||||
self._add_shipping_line(map_record, values)
|
self._add_shipping_line(map_record, values)
|
||||||
values.update({
|
values.update({
|
||||||
'partner_id': self.options.partner_id,
|
'partner_id': self.options.partner_id,
|
||||||
|
|||||||
@@ -37,6 +37,7 @@
|
|||||||
</group>
|
</group>
|
||||||
<group name="product_configuration" string="Product Defaults">
|
<group name="product_configuration" string="Product Defaults">
|
||||||
<field name="product_categ_id"/>
|
<field name="product_categ_id"/>
|
||||||
|
<field name="coupon_product_id"/>
|
||||||
</group>
|
</group>
|
||||||
</group>
|
</group>
|
||||||
<notebook name="import_config">
|
<notebook name="import_config">
|
||||||
@@ -131,6 +132,7 @@
|
|||||||
<field name="fiscal_position_id"/>
|
<field name="fiscal_position_id"/>
|
||||||
<field name="team_id"/>
|
<field name="team_id"/>
|
||||||
<field name="sale_prefix"/>
|
<field name="sale_prefix"/>
|
||||||
|
<field name="coupon_product_id"/>
|
||||||
</group>
|
</group>
|
||||||
</group>
|
</group>
|
||||||
</sheet>
|
</sheet>
|
||||||
|
|||||||
Reference in New Issue
Block a user