diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2024-09-02 16:14:26 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2024-09-02 16:14:26 +0700 |
| commit | 9808cae54a32a65a41d0a13a141e62d88abedfef (patch) | |
| tree | c2689eb6e158f687589d7e20a91e9c231d206aaa | |
| parent | aa60fbca555f1ed654909ed11e87add53676f625 (diff) | |
cr api get user cart
| -rw-r--r-- | indoteknik_api/controllers/api_v1/cart.py | 3 | ||||
| -rw-r--r-- | indoteknik_custom/models/website_user_cart.py | 110 |
2 files changed, 83 insertions, 30 deletions
diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py index f472a9b0..aa47b247 100644 --- a/indoteknik_api/controllers/api_v1/cart.py +++ b/indoteknik_api/controllers/api_v1/cart.py @@ -19,7 +19,8 @@ class Cart(controller.Controller): carts.write({'source': 'add_to_cart'}) data = { 'product_total': user_cart.search_count(query), - 'products': carts.with_context(price_for="web").get_products() + 'products': carts.with_context(price_for="web").get_products(), + 'product_inactive': carts.with_context(price_for="web").get_products_inactive() } return self.response(data) diff --git a/indoteknik_custom/models/website_user_cart.py b/indoteknik_custom/models/website_user_cart.py index 581ed4cd..0fd8df98 100644 --- a/indoteknik_custom/models/website_user_cart.py +++ b/indoteknik_custom/models/website_user_cart.py @@ -23,36 +23,46 @@ class WebsiteUserCart(models.Model): record.user_other_carts = others def get_product(self): - res = { - 'cart_id': self.id, - 'quantity': self.qty, - 'selected': self.is_selected, - 'can_buy': True - } - if self.product_id: - res['cart_type'] = 'product' - res['active'] = True if self.product_id.active else False - product = self.product_id.v2_api_single_response(self.product_id) - res.update(product) - - # Check if the product's inventory location is in ID 57 or 83 - target_locations = [57, 83] - stock_quant = self.env['stock.quant'].search([ - ('product_id', '=', self.product_id.id), - ('location_id', 'in', target_locations) - ]) - - if stock_quant: - res['is_in_bu'] = True - res['on_hand_qty'] = sum(stock_quant.mapped('quantity')) - else: - res['is_in_bu'] = False - res['on_hand_qty'] = 0 + base_price = self.product_id._v2_get_website_price_include_tax() + active = self.product_id.active + if base_price > 0 and active: + res = { + 'cart_id': self.id, + 'quantity': self.qty, + 'selected': self.is_selected, + 'can_buy': True + } + res['cart_type'] = 'product' + res['active'] = True if self.product_id.active else False + product = self.product_id.v2_api_single_response(self.product_id) + res.update(product) + + # Check if the product's inventory location is in ID 57 or 83 + target_locations = [57, 83] + stock_quant = self.env['stock.quant'].search([ + ('product_id', '=', self.product_id.id), + ('location_id', 'in', target_locations) + ]) + + if stock_quant: + res['is_in_bu'] = True + res['on_hand_qty'] = sum(stock_quant.mapped('quantity')) + else: + res['is_in_bu'] = False + res['on_hand_qty'] = 0 - flashsales = self.product_id._get_active_flash_sale() - res['has_flashsale'] = True if len(flashsales) > 0 else False + flashsales = self.product_id._get_active_flash_sale() + res['has_flashsale'] = True if len(flashsales) > 0 else False + res['subtotal'] = self.qty * res['price']['price_discount'] + return res elif self.program_line_id: + res = { + 'cart_id': self.id, + 'quantity': self.qty, + 'selected': self.is_selected, + 'can_buy': True + } res['cart_type'] = 'promotion' userdata = { 'partner_id': self.user_id.partner_id.id, @@ -63,14 +73,56 @@ class WebsiteUserCart(models.Model): if program['remaining_qty']['transaction'] < self.qty: res['can_buy'] = False - res['subtotal'] = self.qty * res['price']['price_discount'] + res['subtotal'] = self.qty * res['price']['price_discount'] - return res + return res def get_products(self): products = [x.get_product() for x in self] return products + + def get_product_inactive(self): + if self.product_id: + base_price = self.product_id._v2_get_website_price_include_tax() + active = self.product_id.active + if base_price == 0 or not active: + res = { + 'cart_id': self.id, + 'quantity': self.qty, + 'selected': self.is_selected, + 'can_buy': True + } + res['cart_type'] = 'product' + res['active'] = True if self.product_id.active else False + product = self.product_id.v2_api_single_response(self.product_id) + res.update(product) + + # Check if the product's inventory location is in ID 57 or 83 + target_locations = [57, 83] + stock_quant = self.env['stock.quant'].search([ + ('product_id', '=', self.product_id.id), + ('location_id', 'in', target_locations) + ]) + + if stock_quant: + res['is_in_bu'] = True + res['on_hand_qty'] = sum(stock_quant.mapped('quantity')) + else: + res['is_in_bu'] = False + res['on_hand_qty'] = 0 + + flashsales = self.product_id._get_active_flash_sale() + res['has_flashsale'] = True if len(flashsales) > 0 else False + + res['subtotal'] = self.qty * res['price']['price_discount'] + + return res + + def get_products_inactive(self): + products = [x.get_product_inactive() for x in self] + + return products def get_product_by_user(self, user_id, selected=False, source=False): user_id = int(user_id) |
