summaryrefslogtreecommitdiff
path: root/fixco_custom/models/detail_order.py
blob: c93bc72c8e64cefd073ca13272819770e0a774dd (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
from odoo import api, fields, models, _
from odoo.exceptions import UserError
import time
import requests
import json
import hmac
import base64
from hashlib import sha256

class DetailOrder(models.Model):
    _name = "detail.order"
    _inherit = ['mail.thread']

    detail_order = fields.Text()
    execute_status = fields.Selection([
        ('success', 'Success'),
        ('failed', 'Failed'),
        ('done', 'Done'),
        ('not_found', 'Record not found')
    ], 'Execute Status')

    def get_order_id(self):
        try:
            if self.json_ginee: 
                json_data = json.loads(self.json_ginee)
                order_id = json_data.get('data', {}).get('orderId')
                order_status = json_data.get('data', {}).get('orderStatus')
                print_info = json_data.get('data', {}).get('printInfo', {}).get('labelPrintStatus')
                if not order_id:
                    raise UserError(_("Order ID not found in JSON data"))
                return order_id 
            raise UserError(_("No JSON data available"))
        except json.JSONDecodeError:
            raise UserError(_("Invalid JSON format in json_ginee field"))
        except Exception as e:
            raise UserError(_("Error extracting order ID: %s") % str(e))
        
    def process_queue_item(self, limit=100, max_exec_time=30):
        domain = [('execute_status', '=', 'success')]
        records = self.search(domain, order='create_date asc', limit=limit)
        start_time = time.time()
        for rec in records:
            end_time = time.time()
            elapsed_time = end_time - start_time
            if elapsed_time > max_exec_time:
                break
            rec.execute_queue()
    
    def execute_queue(self):
        try:
            order_id, order_status, print_info = self.get_order_id()
            
        except Exception as e:
            self.write({
                'execute_status': 'failed',
                'json_ginee': json.dumps({
                    'error': str(e)
                })
            })