summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/voucher.py
blob: 4ca13bd358f39a1d414226c8b1bd35996418351b (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
from odoo import models, fields
from datetime import datetime


class Voucher(models.Model):
    _name = 'voucher'

    name = fields.Char(string='Name')
    image = fields.Binary(string='Image')
    code = fields.Char(string='Code', help='Kode voucher yang akan berlaku untuk pengguna')
    description = fields.Text(string='Description')
    discount_amount = fields.Integer(string='Discount Amount')
    discount_type = fields.Selection(
        selection=[
            ('percentage', 'Percentage'),
            ('fixed_price', 'Fixed Price'),
        ], 
        string='Discount Type', 
        help='Select the type of discount:\n'
            '- Percentage: Persentage dari total harga.\n'
            '- Fixed Price: Jumlah tetap yang dikurangi dari harga total.'
    )
    visibility = fields.Selection(
        selection=[
            ('public', 'Public'),
            ('private', 'Private')
        ], 
        string='Visibility', 
        help='Select the visibility:\n'
            '- Public: Ditampilkan kepada seluruh pengguna.\n'
            '- Private: Tidak ditampilkan kepada seluruh pengguna.'
    )
    start_time = fields.Datetime(string='Start Time')
    end_time = fields.Datetime(string='End Time')
    min_purchase_amount = fields.Integer(string='Min. Purchase Amount', help='Nominal minimum untuk dapat menggunakan voucher. Isi 0 jika tidak ada minimum purchase amount')
    max_discount_amount = fields.Integer(string='Max. Discount Amount', help='Max nominal terhadap persentase diskon')
    order_ids = fields.One2many('sale.order', 'voucher_id', string='Order')

    def res_format(self):
        datas = [voucher.format() for voucher in self]
        return datas
    
    def format(self):
        data = {
            'name': self.name,
            'code': self.code,
            'description': self.description,
            'discount_amount': self.discount_amount,
            'discount_type': self.discount_type,
            'remaining_time': self._get_remaining_time(),
            'min_purchase_amount': self.min_purchase_amount
        }
        return data
    
    def _get_remaining_time(self):
        calculate_time = self.end_time - datetime.now()
        return round(calculate_time.total_seconds())
    
    def get_active_voucher(self, parameter):
        current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        parameter += [
            ('start_time', '<=', current_time),
            ('end_time', '>=', current_time),
        ]
        vouchers =  self.search(parameter)
        return vouchers