summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/website_user_cart.py
blob: 29bf42913208bc8b6fca12111dfe3546fc606bb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from odoo import fields, models


class WebsiteUserCart(models.Model):
    _name = 'website.user.cart'
    _rec_name = 'user_id'
    
    user_id = fields.Many2one('res.users', string='User')
    product_id = fields.Many2one('product.product', string='Product')
    program_line_id = fields.Many2one('promotion.program.line', string='Program', help="Apply program")
    qty = fields.Float(string='Quantity', digits='Product Unit of Measure')
    is_selected = fields.Boolean(string='Selected?', digits='Is selected to process checkout')

    def get_product(self):
        user_data = {
            'partner_id': self.user_id.partner_id.id,
            'user_id': self.user_id.id
        }
        product = self.product_id.v2_api_single_response(self.product_id)
        product['cart_id'] = self.id
        product['quantity'] = self.qty
        product['subtotal'] = self.qty * product['price']['price_discount']
        product['selected'] = self.is_selected
        product['program'] = None
        product['can_buy'] = True
        if self.program_line_id:
            product['program'] = self.program_line_id.res_format_cart(user=user_data, quantity=self.qty)
            
        if product['program']:
            if self.qty < product['program']['minimum_purchase_qty'] or self.qty > product['program']['remaining_qty']['transaction']:
                product['can_buy'] = False
            product['price'] = product['program']['price']
            
        return product
    
    def get_products(self):
        return [x.get_product() for x in self]

    def get_product_by_user(self, user_id, selected = False):
        user_id = int(user_id)
        parameters = [('user_id', '=', user_id)]
        if selected:
            parameters.append(('is_selected', '=', True))
        carts = self.search(parameters)
        products = carts.get_products()
        return products
    
    def get_user_checkout(self, user_id, voucher=False):
        products = self.get_product_by_user(user_id=user_id, selected=True)
        total_purchase = sum(x['price']['price'] * x['quantity'] for x in products)
        total_discount = sum((x['price']['price'] - x['price']['price_discount']) * x['quantity'] for x in products)
        subtotal = total_purchase - total_discount
        discount_voucher = 0
        if voucher:
            discount_voucher = voucher.calculate_discount(subtotal)
            subtotal -= discount_voucher
        tax = round(subtotal * 0.11)
        grand_total = subtotal + tax
        total_weight = sum(x['weight'] * x['quantity'] for x in products)
        result = {
            'total_purchase': total_purchase,
            'total_discount': total_discount,
            'discount_voucher': discount_voucher,
            'subtotal': subtotal,
            'tax': tax,
            'grand_total': round(grand_total),
            'total_weight': {
                'kg': total_weight,
                'g': total_weight * 1000
            },
            'has_product_without_weight': any(not product.get('weight') or product.get('weight') == 0 for product in products),
            'products': products
        }
        return result