summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/ged.py
blob: 832fb023adcd7549c2aef21b0a377c64ecc39d1a (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from odoo import models, fields
from datetime import datetime, timedelta
import requests
import urllib.parse
from odoo.exceptions import UserError
import json

#dev
# url_token = 'https://stagging-api.ged.co.id/api/GetToken'
# url_tracking = 'https://stagging-api.ged.co.id/api/TrackingAwb'
#production
url_token = 'https://gedapi.ged.co.id/api/GetToken'
url_tracking = 'https://gedapi.ged.co.id/api/TrackingAwb'
username = 'stephan@indoteknik.co.id'
password = 'faCE2HFFKdzhvPW'
x_ged_key = 'Indoteknikdotcom'
x_ged_password = 'Ind0t3kn1kdotc0m4p1'

class GedTracking(models.Model):
    _name = 'ged.tracking'

    status = fields.Char(string='Status')
    messages = fields.Char(string='Messages')
    awb_no = fields.Char(string='AWB No')
    reff_no = fields.Char(string='Reff No')
    consignee_name = fields.Char(string='Consignee Name')
    consignee_contact = fields.Char(string='Consignee Contact')
    consignee_phone = fields.Char(string='Consignee Phone')
    consignee_address = fields.Char(string='Consignee Address')
    moda = fields.Char(string='Moda')
    service = fields.Char(string='Service')
    package = fields.Char(string='Package')
    height = fields.Char(string='Height')
    width = fields.Char(string='Width')
    length = fields.Char(string='Length')
    coli = fields.Char(string='Coli')
    kg = fields.Char(string='KG')
    origin = fields.Char(string='Origin')
    destination = fields.Char(string='Destination')
    last_user_input = fields.Char(string='Last User Input')
    last_created_at = fields.Char(string='Last Created At')
    last_location = fields.Char(string='Last Location')
    last_description = fields.Char(string='Last Description')
    last_status_detail = fields.Char(string='Last Status Detail')
    last_status = fields.Char(string='Last Status')


class GedTrackingLine(models.Model):
    _name = 'ged.tracking.line'

    ged_tracking_id = fields.Many2one('ged.tracking', string='Ged Tracking ID', required=True, ondelete='cascade',
                                      index=True, copy=False)
    user_input = fields.Char(string='User Input')
    created_at = fields.Char(string='Created At')
    location = fields.Char(string='Location')
    description = fields.Char(string='Description')
    status_detail = fields.Char(string='Status Detail')
    status = fields.Char(string='Status')
    receiver_name = fields.Char(string='Receiver Name')
    receiver_relationship = fields.Char(string='Receiver Relationship')
    photo_pod = fields.Char(string='Photo POD')
    photo_signature = fields.Char(string='Photo Signature')
    photo_location = fields.Char(string='Photo Location')
    photo_receiver = fields.Char(string='Photo Receiver')
    gps = fields.Char(string='GPS')


class GedApi(models.Model):
    _name = 'ged.api'

    def get_token(self):
        #TODO check if have old active token or not
        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
        }
        json_data = {
            'data': {
                'email': username,
                'password': password,
            },
        }
        response = requests.post(url_token, headers=headers, json=json_data)

        status = response.json()['status']
        messages = response.json()['messages']
        token = response.json()['token']
        expired = response.json()['expired']
        date_expired = datetime.strptime(expired, '%y-%m-%d %H:%M:%S')
        date_expired = date_expired.strftime('%Y-%m-%d %H:%M:%S')

        param = {
            "status": status,
            "messages": messages,
            "access_token": token,
            "expired_date": date_expired,
            "app_id": 1,
            "source": "GED"
        }
        self.env['token.storage'].create([param])
        return token

    def get_tracking_awb(self):
        token = self.get_token()
        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'X-Ged-Key': x_ged_key,
            'X-Ged-Password': x_ged_password,
            'Authorization': 'Bearer '+token
        }
        json_data = {
            'data': {
                'awb': 19138131
            }
        }
        response = requests.post(url_tracking, headers=headers, json=json_data)
        info_awb = response.json()['infoawb']
        last_status = response.json()['laststatus']

        status = response.json()['status']
        messages = response.json()['messages']
        awb_no = info_awb['awb_no']
        reff_no = info_awb['reff_no']
        consignee_name = info_awb['consignee_name']
        consignee_contact = info_awb['consignee_contact']
        consignee_phone = info_awb['consignee_phone']
        consignee_address = info_awb['consignee_address']
        moda = info_awb['moda']
        service = info_awb['service']
        package = info_awb['package']
        height = info_awb['height']
        width = info_awb['width']
        length = info_awb['length']
        coli = info_awb['coli']
        kg = info_awb['kg']
        origin = info_awb['origin']
        destination = info_awb['destination']
        last_user_input = last_status['user_input']
        last_created_at = last_status['created_at']
        last_location = last_status['location']
        last_description = last_status['description']
        last_status_detail = last_status['status_detail']
        last_status = last_status['status']

        param_header = {
            'status': status,
            'messages': messages,
            'awb_no': awb_no,
            'reff_no': reff_no,
            'consignee_name': consignee_name,
            'consignee_contact': consignee_contact,
            'consignee_phone': consignee_phone,
            'consignee_address': consignee_address,
            'moda': moda,
            'service': service,
            'package': package,
            'height': height,
            'width': width,
            'length': length,
            'coli': coli,
            'kg': kg,
            'origin': origin,
            'destination': destination,
            'last_user_input': last_user_input,
            'last_created_at': last_created_at,
            'last_location': last_location,
            'last_description': last_description,
            'last_status_detail': last_status_detail,
            'last_status': last_status
        }

        ged_tracking = self.env['ged.tracking'].create(param_header)

        for line in response.json()['data']:
            ged_tracking_id = ged_tracking.id
            user_input = line['user_input']
            created_at = line['created_at']
            location = line['location']
            description = line['description']
            status_detail = line['status_detail']
            status = line['status']

            param_line = {
                'ged_tracking_id': ged_tracking_id,
                'user_input': user_input,
                'created_at': created_at,
                'location': location,
                'description': description,
                'status_detail': status_detail,
                'status': status
            }
            self.env['ged.tracking.line'].create(param_line)