diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2023-10-06 14:22:12 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2023-10-06 14:22:12 +0700 |
| commit | 5f9c6e2837c8201ca8132f3d3d71627bf799fb6d (patch) | |
| tree | df2b9d4d0b52a6cf8d45d153dff977e04785abb2 | |
| parent | bb3c3c6cac4382220ae8a521e8c2162a5fe3d3e9 (diff) | |
| parent | 7a23a18a45fbd38fabc33f4e1a74d9f31e86cd60 (diff) | |
Merge branch 'production' of bitbucket.org:altafixco/indoteknik-addons into production
| -rw-r--r-- | indoteknik_api/controllers/api_v1/cart.py | 2 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/sale_order.py | 19 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/voucher.py | 2 | ||||
| -rw-r--r-- | indoteknik_api/controllers/controller.py | 12 | ||||
| -rw-r--r-- | indoteknik_api/models/product_product.py | 54 | ||||
| -rw-r--r-- | indoteknik_api/models/res_users.py | 9 | ||||
| -rw-r--r-- | indoteknik_custom/models/product_pricelist.py | 26 | ||||
| -rwxr-xr-x | indoteknik_custom/models/sale_order.py | 10 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/product_product.py | 4 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/product_template.py | 4 | ||||
| -rw-r--r-- | indoteknik_custom/models/voucher.py | 6 | ||||
| -rwxr-xr-x | indoteknik_custom/views/voucher.xml | 2 |
12 files changed, 85 insertions, 65 deletions
diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py index cb0f5a99..88fa9f88 100644 --- a/indoteknik_api/controllers/api_v1/cart.py +++ b/indoteknik_api/controllers/api_v1/cart.py @@ -18,7 +18,7 @@ class Cart(controller.Controller): carts = user_cart.search(query, limit=limit, offset=offset, order='create_date desc') data = { 'product_total': user_cart.search_count(query), - 'products': carts.get_products() + 'products': carts.with_context(price_for="web").get_products() } return self.response(data) diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index ef4c2688..8209c751 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -236,14 +236,15 @@ class SaleOrder(controller.Controller): voucher_code = kw.get('voucher') source = kw.get('source') voucher = request.env['voucher'].search([('code', '=', voucher_code)], limit=1) - result = cart.get_user_checkout(user_id, voucher, source) + result = cart.with_context(price_for="web").get_user_checkout(user_id, voucher, source) return self.response(result) @http.route(PREFIX_PARTNER + 'sale_order/checkout', auth='public', method=['POST', 'OPTIONS'], csrf=False) @controller.Controller.must_authorized(private=True, private_key='partner_id') def create_partner_sale_order(self, **kw): config = request.env['ir.config_parameter'] - product_pricelist_default_discount_id = int(config.get_param('product.pricelist.default_discount_id')) + product_pricelist_default_discount_id = int(config.get_param('product.pricelist.tier1_v2')) + user_pricelist = request.env.context.get('user_pricelist').id or False params = self.get_request_params(kw, { 'user_id': ['number'], @@ -268,8 +269,8 @@ class SaleOrder(controller.Controller): parameters = { 'warehouse_id': 8, 'carrier_id': 1, - 'sales_tax_id': 20, - 'pricelist_id': product_pricelist_default_discount_id, + 'sales_tax_id': 23, + 'pricelist_id': user_pricelist or product_pricelist_default_discount_id, 'payment_term_id': 26, 'team_id': 2, 'company_id': 1, @@ -301,18 +302,19 @@ class SaleOrder(controller.Controller): source = params['value']['source'] carts = user_cart.get_product_by_user(user_id=user_id, selected=True, source=source) - order_lines = [] promotions = [] for cart in carts: if cart['cart_type'] == 'product': - order_lines.append({ + order_line = request.env['sale.order.line'].create({ 'company_id': 1, 'order_id': sale_order.id, - 'price_unit': cart['price']['price'], - 'discount': cart['price']['discount_percentage'], 'product_id': cart['id'], 'product_uom_qty': cart['quantity'] }) + order_line.product_id_change() + order_line.onchange_vendor_id() + order_line.price_unit = cart['price']['price'] + order_line.discount = cart['price']['discount_percentage'] elif cart['cart_type'] == 'promotion': promotions.append({ 'order_id': sale_order.id, @@ -320,7 +322,6 @@ class SaleOrder(controller.Controller): 'quantity': cart['quantity'] }) - request.env['sale.order.line'].create(order_lines) request.env['sale.order.promotion'].create(promotions) if len(promotions) > 0: diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index dfe9ceba..3c056ecd 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -20,7 +20,7 @@ class Voucher(controller.Controller): if code: visibility.append('private') parameter += [('code', '=', code)] - user_pricelist = request.env.user_pricelist + user_pricelist = request.env.context.get('user_pricelist') if user_pricelist: parameter += [('excl_pricelist_ids', 'not in', [user_pricelist.id])] diff --git a/indoteknik_api/controllers/controller.py b/indoteknik_api/controllers/controller.py index 08b3084e..12305d92 100644 --- a/indoteknik_api/controllers/controller.py +++ b/indoteknik_api/controllers/controller.py @@ -21,9 +21,12 @@ class Controller(http.Controller): def inner_wrapper(*args, **kwargs): self = args[0] auth = self.authenticate() - request.env.user_pricelist = self.user_pricelist() + if not auth: return self.unauthorized_response() + + self.set_user_pricelist_ctx() + if private: auth_key = int(auth[private_key]) param_key = int(kwargs.get(private_key, -1)) @@ -56,13 +59,16 @@ class Controller(http.Controller): result = user_token return result - def user_pricelist(self): + def set_user_pricelist_ctx(self): user_token = self.authenticate() pricelist = request.env['product.pricelist'].new() if isinstance(user_token, dict): partner = request.env['res.partner'].browse(user_token['partner_id']) if partner: pricelist = partner.property_product_pricelist - return pricelist + + context = request.env.context.copy() + context.update({'user_pricelist': pricelist}) + request.env.context = context def get_request_params(self, kw, queries): result = { diff --git a/indoteknik_api/models/product_product.py b/indoteknik_api/models/product_product.py index fdc90533..aa9e0ad7 100644 --- a/indoteknik_api/models/product_product.py +++ b/indoteknik_api/models/product_product.py @@ -58,36 +58,38 @@ class ProductProduct(models.Model): return True if len(product_promotions) > 0 else False def calculate_website_price(self, pricelist=False): - pricelist = pricelist or self.env.user_pricelist + price_for = self.env.context.get('price_for', 'odoo') + pricelist = pricelist or self.env.context.get('user_pricelist') + default_price_tier = '1_v2' - discount_percentage = self._get_website_disc(0) - price_discount = self._get_website_price_after_disc_and_tax() + price_tier = pricelist.get_tier_level() + price_tier = price_tier if price_tier else default_price_tier - pricelists = { - 'tier1': self._get_pricelist_tier1, - 'tier2': self._get_pricelist_tier2, - 'tier3': self._get_pricelist_tier3, - } + pricelist = self._get_pricelist_tier(price_tier) + + discount_key = f'discount_tier{price_tier}' + price_key = f'price_tier{price_tier}' - price_tier = pricelist.get_tier_name() - if price_tier: - price = pricelists[price_tier]() - discount_key = 'discount_%s' % price_tier - price_key = 'price_%s' % price_tier - if price[discount_key] > 0: discount_percentage = price[discount_key] - if price[price_key] > 0: price_discount = price[price_key] + discount_percentage = pricelist.get(discount_key, 0) + price_discount = pricelist.get(price_key, 0) + if price_for == 'web': + discount_percentage = 0 + price = price_discount + else: + price = self._v2_get_website_price_include_tax() + flashsale = self._get_flashsale_price() flashsale_price = flashsale.get('flashsale_price', 0) flashsale_discount = flashsale.get('flashsale_discount', 0) if flashsale_price > 0 and flashsale_price < price_discount: price_discount = flashsale_price discount_percentage = flashsale_discount - + return { - 'price': self._get_website_price_exclude_tax(), + 'price': price, 'discount_percentage': discount_percentage, - 'price_discount': price_discount + 'price_discount': price_discount if price_discount > 0 else price } def api_manufacture(self, product_template): @@ -249,6 +251,7 @@ class ProductProduct(models.Model): return data def _get_flashsale_price(self): + price_for = self.env.context.get('price_for', 'odoo') result = {} current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') @@ -262,7 +265,16 @@ class ProductProduct(models.Model): if not item: return result - base_price = self._get_website_price_exclude_tax() + tier1_id = self.env['ir.config_parameter'].get_param('product.pricelist.tier1_v2', 0) + base_pricelist = self.env['product.pricelist.item'].search([ + ('pricelist_id', '=', int(tier1_id)), + ('product_id', '=', self.id) + ], limit=1) + + base_price = 0 + if base_pricelist: + base_price = base_pricelist.computed_price / 1.11 + discount = 0 price_flashsale = 0 if item.price_discount > 0: @@ -272,6 +284,10 @@ class ProductProduct(models.Model): price_flashsale = item.fixed_price # ask darren for include or exclude discount = (base_price - price_flashsale) // base_price * 100 + if price_for == 'odoo': + base_price = self._v2_get_website_price_exclude_tax() + discount = (base_price - price_flashsale) / base_price * 100 + jkt_tz = pytz.timezone('Asia/Jakarta') result.update({ 'flashsale_id': item.pricelist_id.id, diff --git a/indoteknik_api/models/res_users.py b/indoteknik_api/models/res_users.py index 0fe9008b..f331321f 100644 --- a/indoteknik_api/models/res_users.py +++ b/indoteknik_api/models/res_users.py @@ -6,16 +6,9 @@ class ResUsers(models.Model): def api_single_response(self, res_user, with_detail=''): config = self.env['ir.config_parameter'] - product_pricelist_tier1 = int(config.get_param('product.pricelist.tier1')) - product_pricelist_tier2 = int(config.get_param('product.pricelist.tier2')) - product_pricelist_tier3 = int(config.get_param('product.pricelist.tier3')) user_pricelist = res_user.property_product_pricelist - user_pricelist_id = user_pricelist.id if user_pricelist else False - pricelist_tier = False - if user_pricelist_id == product_pricelist_tier1: pricelist_tier = 'tier1' - if user_pricelist_id == product_pricelist_tier2: pricelist_tier = 'tier2' - if user_pricelist_id == product_pricelist_tier3: pricelist_tier = 'tier3' + pricelist_tier = user_pricelist.sudo().get_tier_name() data = { 'id': res_user.id, diff --git a/indoteknik_custom/models/product_pricelist.py b/indoteknik_custom/models/product_pricelist.py index 026977f8..b7a6d77e 100644 --- a/indoteknik_custom/models/product_pricelist.py +++ b/indoteknik_custom/models/product_pricelist.py @@ -24,21 +24,25 @@ class ProductPricelist(models.Model): remaining_time = round(remaining_time) return max(remaining_time, 0) - def get_tier_name(self): + def get_tier_level(self): config = self.env['ir.config_parameter'] - product_pricelist_tier1 = int(config.get_param('product.pricelist.tier1', 0)) - product_pricelist_tier2 = int(config.get_param('product.pricelist.tier2', 0)) - product_pricelist_tier3 = int(config.get_param('product.pricelist.tier3', 0)) + tier_keys = ['1', '2', '3', '1_v2', '2_v2', '3_v2', '4_v2', '5_v2'] + + for tier in tier_keys: + tier_id = config.get_param('product.pricelist.tier' + tier, 0) + if self.id == int(tier_id): + return tier - price_tier = None - if self.id == product_pricelist_tier1: price_tier = 'tier1' - if self.id == product_pricelist_tier2: price_tier = 'tier2' - if self.id == product_pricelist_tier3: price_tier = 'tier3' - return price_tier + return None + + def get_tier_name(self): + tier_level = self.get_tier_level() + tier_name = f'tier{tier_level}' if tier_level else None + + return tier_name class ProductPricelistItem(models.Model): _inherit = 'product.pricelist.item' manufacture_id = fields.Many2one('x_manufactures', string='Manufacture') - -
\ No newline at end of file + computed_price = fields.Float(string='Computed Price')
\ No newline at end of file diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 93c63d56..9324930e 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -423,8 +423,8 @@ class SaleOrder(models.Model): order.grand_total = order.amount_total def action_apply_voucher(self): - for order in self.order_line: - if order.program_line_id: + for line in self.order_line: + if line.order_promotion_id: raise UserError('Voucher tidak dapat digabung dengan promotion program') voucher = self.voucher_id @@ -474,8 +474,10 @@ class SaleOrder(models.Model): line_contribution = line.price_subtotal / used_total line_voucher = used_discount * line_contribution line_voucher_item = line_voucher / line.product_uom_qty - line_discount_item = line.price_unit * line.discount / 100 + line_voucher_item - line_voucher_item = line_discount_item / line.price_unit * 100 + + line_price_unit = line.price_unit / 1.11 if any(tax.id == 23 for tax in line.tax_id) else line.price_unit + line_discount_item = line_price_unit * line.discount / 100 + line_voucher_item + line_voucher_item = line_discount_item / line_price_unit * 100 line.amount_voucher_disc = line_voucher line.discount = line_voucher_item diff --git a/indoteknik_custom/models/solr/product_product.py b/indoteknik_custom/models/solr/product_product.py index 16135e5e..31a0026d 100644 --- a/indoteknik_custom/models/solr/product_product.py +++ b/indoteknik_custom/models/solr/product_product.py @@ -84,7 +84,7 @@ class ProductProduct(models.Model): price_excl_after_disc = variant._get_website_price_after_disc_and_tax() tax = variant._get_website_tax() discount = variant._get_website_disc(0) - flashsale_data = variant._get_flashsale_price() + flashsale_data = variant.with_context(price_for="web")._get_flashsale_price() price_excl_v2 = variant._v2_get_website_price_exclude_tax() price_excl_after_disc_v2 = variant._v2_get_website_price_after_disc_and_tax() @@ -134,7 +134,7 @@ class ProductProduct(models.Model): def solr_results(self): solr_model = self.env['apache.solr'] - pricelist = self.env.user_pricelist + pricelist = self.env.context.get('user_pricelist') price_tier = pricelist.get_tier_name() results = [] diff --git a/indoteknik_custom/models/solr/product_template.py b/indoteknik_custom/models/solr/product_template.py index e39530a7..d7439bcb 100644 --- a/indoteknik_custom/models/solr/product_template.py +++ b/indoteknik_custom/models/solr/product_template.py @@ -103,7 +103,7 @@ class ProductTemplate(models.Model): tier_data = {} for variant in template.product_variant_ids: - variant_flashsale = variant._get_flashsale_price() + variant_flashsale = variant.with_context(price_for="web")._get_flashsale_price() variant_flashsale_price = variant_flashsale.get('flashsale_price', 0) flashsale_data_price = flashsale_data.get('flashsale_price', 0) @@ -157,7 +157,7 @@ class ProductTemplate(models.Model): def solr_results(self, detail=False): solr_model = self.env['apache.solr'] - pricelist = self.env.user_pricelist + pricelist = self.env.context.get('user_pricelist') price_tier = pricelist.get_tier_name() results = [] diff --git a/indoteknik_custom/models/voucher.py b/indoteknik_custom/models/voucher.py index 8486fca0..a40f8c42 100644 --- a/indoteknik_custom/models/voucher.py +++ b/indoteknik_custom/models/voucher.py @@ -1,7 +1,6 @@ from odoo import models, fields, api from datetime import datetime, timedelta from odoo.exceptions import ValidationError -import locale class Voucher(models.Model): @@ -229,9 +228,8 @@ class Voucher(models.Model): def generate_detail_tnc(self): def format_currency(amount): - locale.setlocale(locale.LC_ALL, 'id_ID') - formatted_amount = locale.format("%d", amount, grouping=True) - return f'Rp{formatted_amount}' + formatted_number = '{:,.0f}'.format(amount).replace(',', '.') + return f'Rp{formatted_number}' tnc = [] if self.apply_type == 'all': diff --git a/indoteknik_custom/views/voucher.xml b/indoteknik_custom/views/voucher.xml index a2f5557a..b8489942 100755 --- a/indoteknik_custom/views/voucher.xml +++ b/indoteknik_custom/views/voucher.xml @@ -35,7 +35,7 @@ <field name="limit" required="1"/> <field name="limit_user" required="1"/> <field name="apply_type" required="1" /> - <field name="excl_pricelist_ids" widget="many2many_tags" domain="[('id', 'in', [4, 15037, 15038, 15039])]"/> + <field name="excl_pricelist_ids" widget="many2many_tags" domain="[('id', 'in', [4, 15037, 15038, 15039, 17023, 17024, 17025, 17026,17027])]"/> </group> <group string="Discount Settings" attrs="{'invisible': [('apply_type', '!=', 'all')]}"> <field name="min_purchase_amount" widget="monetary" required="1" /> |
