41 lines
1.6 KiB
Python
41 lines
1.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from odoo import models, fields, api
|
|
|
|
class LoyaltyCard(models.Model):
|
|
_inherit = 'loyalty.card'
|
|
|
|
limit = fields.Boolean(string="Limit")
|
|
limit_count = fields.Integer(string="Limit Count", default=1)
|
|
limit_period = fields.Selection([
|
|
('day', 'Per Day'),
|
|
('month', 'Per Month'),
|
|
('year', 'Per Year')
|
|
], string="Limit Period", default='day')
|
|
|
|
backend_usage_count = fields.Integer(compute='_compute_backend_usage_count', string="Usage Count in Period")
|
|
|
|
def _compute_backend_usage_count(self):
|
|
for card in self:
|
|
if not card.limit:
|
|
card.backend_usage_count = 0
|
|
continue
|
|
|
|
domain = [('card_id', '=', card.id), ('used', '>', 0)]
|
|
today = fields.Datetime.now()
|
|
|
|
if card.limit_period == 'day':
|
|
domain.append(('create_date', '>=', today.replace(hour=0, minute=0, second=0, microsecond=0)))
|
|
elif card.limit_period == 'month':
|
|
domain.append(('create_date', '>=', today.replace(day=1, hour=0, minute=0, second=0, microsecond=0)))
|
|
elif card.limit_period == 'year':
|
|
domain.append(('create_date', '>=', today.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)))
|
|
|
|
card.backend_usage_count = self.env['loyalty.history'].search_count(domain)
|
|
|
|
@api.model
|
|
def _load_pos_data_fields(self, config):
|
|
fields_list = super()._load_pos_data_fields(config)
|
|
fields_list.extend(['limit', 'limit_count', 'limit_period', 'backend_usage_count'])
|
|
return fields_list
|