diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-06-23 11:20:52 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-06-23 11:20:52 +0700 |
| commit | 23014336a1fe1fe5ef54fad30cf6c3d9cc59b2d8 (patch) | |
| tree | 3b1ee79d1e73b6e02f943411c5a2715c7f16e2f3 | |
| parent | ae998f145e4f8b4a0939a97c02e70564ef758e73 (diff) | |
Update response price di product promotion API, Membuat fungsi calculate price promotion, Refactor promotion homepage, Refactor fungsi calculate product price
| -rw-r--r-- | indoteknik_api/controllers/api_v1/product_variant.py | 3 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/promotion.py | 9 | ||||
| -rw-r--r-- | indoteknik_api/models/product_product.py | 60 | ||||
| -rw-r--r-- | indoteknik_custom/models/promotion_program_line.py | 43 |
4 files changed, 59 insertions, 56 deletions
diff --git a/indoteknik_api/controllers/api_v1/product_variant.py b/indoteknik_api/controllers/api_v1/product_variant.py index cec54fda..06ce5d3c 100644 --- a/indoteknik_api/controllers/api_v1/product_variant.py +++ b/indoteknik_api/controllers/api_v1/product_variant.py @@ -26,9 +26,10 @@ class ProductVariant(controller.Controller): def get_product_variant_promotions(self, product_id): product_id = int(product_id) user_data = self.verify_user_token() + pricelist = self.user_pricelist() program_line = request.env['promotion.program.line'] program_lines = program_line.get_active_promotions(product_id) - program_lines = program_line.res_format(program_lines, user_data) + program_lines = program_lines.res_format(user=user_data, pricelist=pricelist) return self.response(program_lines) diff --git a/indoteknik_api/controllers/api_v1/promotion.py b/indoteknik_api/controllers/api_v1/promotion.py index 948681fd..a3dbf0ba 100644 --- a/indoteknik_api/controllers/api_v1/promotion.py +++ b/indoteknik_api/controllers/api_v1/promotion.py @@ -73,20 +73,13 @@ class Promotion(controller.Controller): 'image': product['parent']['image'], 'name': product['parent']['name'], 'variant_total': len(product_template.product_variant_ids), - 'lowest_price': product['price'], + 'lowest_price': line.calculate_price(product['price']), 'stock_total': product['stock'], 'icon': { 'top': request.env['ir.attachment'].api_image('promotion.program', 'icon_top', line.program_id.id), 'bottom': request.env['ir.attachment'].api_image('promotion.program', 'icon_bottom', line.program_id.id) } }) - price = product['lowest_price']['price'] - if line.discount_type == 'percentage': - product['lowest_price']['discount_percentage'] = line.discount_amount - product['lowest_price']['price_discount'] = price - (price * line.discount_amount / 100) - if line.discount_type == 'fixed_price': - product['lowest_price']['price_discount'] = line.discount_amount - product['lowest_price']['discount_percentage'] = round((price - line.discount_amount) / price * 100) product.pop('parent', None) product.pop('price', None) diff --git a/indoteknik_api/models/product_product.py b/indoteknik_api/models/product_product.py index 14fe68cb..9e218565 100644 --- a/indoteknik_api/models/product_product.py +++ b/indoteknik_api/models/product_product.py @@ -28,19 +28,38 @@ class ProductProduct(models.Model): return data def v2_api_single_response(self, product_product, pricelist=False): + product_template = product_product.product_tmpl_id + data = { + 'id': product_product.id, + 'parent': { + 'id': product_template.id, + 'name': product_template.name, + 'image': self.env['ir.attachment'].api_image('product.template', 'image_256', product_template.id), + }, + 'code': product_product.default_code or '', + 'name': product_product.display_name, + 'price': product_product.calculate_website_price(pricelist), + 'stock': product_product.qty_stock_vendor, + 'weight': product_product.weight, + 'attributes': [x.name for x in product_product.product_template_attribute_value_ids], + 'manufacture' : self.api_manufacture(product_product) + } + return data + + def calculate_website_price(self, pricelist): 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')) - discount_percentage = product_product._get_website_disc(0) - price_discount = product_product._get_website_price_after_disc_and_tax() + discount_percentage = self._get_website_disc(0) + price_discount = self._get_website_price_after_disc_and_tax() price_tier = False pricelists = { - 'tier1': product_product._get_pricelist_tier1, - 'tier2': product_product._get_pricelist_tier2, - 'tier3': product_product._get_pricelist_tier3, + 'tier1': self._get_pricelist_tier1, + 'tier2': self._get_pricelist_tier2, + 'tier3': self._get_pricelist_tier3, } pricelist_id = pricelist.id if pricelist else False if pricelist_id == product_pricelist_tier1: price_tier = 'tier1' @@ -54,34 +73,11 @@ class ProductProduct(models.Model): if price[discount_key] > 0: discount_percentage = price[discount_key] if price[price_key] > 0: price_discount = price[price_key] - flashsale = product_product._get_flashsale_price() - flashsale_price = flashsale['flashsale_price'] - flashsale_discount = flashsale['flashsale_discount'] - if flashsale_price > 0 and flashsale_price < price_discount: - price_discount = flashsale_price - discount_percentage = flashsale_discount - - product_template = product_product.product_tmpl_id - data = { - 'id': product_product.id, - 'parent': { - 'id': product_template.id, - 'name': product_template.name, - 'image': self.env['ir.attachment'].api_image('product.template', 'image_256', product_template.id), - }, - 'code': product_product.default_code or '', - 'name': product_product.display_name, - 'price': { - 'price': product_product._get_website_price_exclude_tax(), - 'discount_percentage': discount_percentage, - 'price_discount': price_discount - }, - 'stock': product_product.qty_stock_vendor, - 'weight': product_product.weight, - 'attributes': [x.name for x in product_product.product_template_attribute_value_ids], - 'manufacture' : self.api_manufacture(product_product) + return { + 'price': self._get_website_price_exclude_tax(), + 'discount_percentage': discount_percentage, + 'price_discount': price_discount } - return data def api_manufacture(self, product_template): if product_template.x_manufacture: diff --git a/indoteknik_custom/models/promotion_program_line.py b/indoteknik_custom/models/promotion_program_line.py index 3e419ef9..c888f01a 100644 --- a/indoteknik_custom/models/promotion_program_line.py +++ b/indoteknik_custom/models/promotion_program_line.py @@ -71,6 +71,16 @@ class PromotionProgramLine(models.Model): 'qty': 1, 'line_id': program_line.id} line_free_item.create(data) + def calculate_price(self, price): + initial_price = price['price'] + if self.discount_type == 'percentage': + price['discount_percentage'] = self.discount_amount + price['price_discount'] = initial_price - (initial_price * self.discount_amount / 100) + elif self.discount_type == 'fixed_price': + price['price_discount'] = self.discount_amount + price['discount_percentage'] = round((initial_price - self.discount_amount) / initial_price * 100) + return price + def get_active_promotions(self, product_id): current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') return self.search([ @@ -116,20 +126,23 @@ class PromotionProgramLine(models.Model): 'label': dict(self._fields['promotion_type'].selection).get(self.promotion_type) } - def res_format(self, lines, user): + def format(self, user = None, pricelist = False): ir_attachment = self.env['ir.attachment'] - data = [] - for line in lines: - data.append({ - 'id': line.id, - 'name': line.name, - 'image': ir_attachment.api_image('promotion.program.line', 'image', line.id), - 'minimum_purchase_qty': line.minimum_purchase_qty, - 'applies_multiply': line.applies_multiply, - 'remaining_time': line._get_remaining_time(), - 'type': line._res_promotion_type(), - 'limit_qty': line._res_limit_qty(), - 'remaining_qty': line._get_remaining_qty(user), - }) + product_price = self.product_id.calculate_website_price(pricelist=pricelist) + return { + 'id': self.id, + 'name': self.name, + 'image': ir_attachment.api_image('promotion.program.line', 'image', self.id), + 'minimum_purchase_qty': self.minimum_purchase_qty, + 'applies_multiply': self.applies_multiply, + 'remaining_time': self._get_remaining_time(), + 'type': self._res_promotion_type(), + 'limit_qty': self._res_limit_qty(), + 'remaining_qty': self._get_remaining_qty(user), + 'price': self.calculate_price(price=product_price) + } + + def res_format(self, user, pricelist): + data = [x.format(user, pricelist=pricelist) for x in self] return data - + |
