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' # username = 'mandiri@gmail.co.id' # password = 'nPcWh8Up75ai5CW' # x_ged_key = 'Mandiriind' # x_ged_password = 'M4ndir1ind0n3si4ap1' #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' _order = 'awb_no desc' _rec_name = 'awb_no' 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') receiver_name = fields.Char(string='Receiver Name') receiver_relationship = fields.Char(string='Receiver Relationship') delivered_date = fields.Char(string='Delivered Date') 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') 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') tracking_line = fields.One2many('ged.tracking.line', 'ged_tracking_id', string='Tracking Lines', auto_join=True) dunning_id = fields.Many2one('dunning.run', string='Dunning Run') 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') dunning_id = fields.Many2one('dunning.run', string='Dunning Run') class GedTrackingLog(models.Model): _name = 'ged.tracking.log' responses = fields.Char(string='Responses') body = fields.Char(string='Body') class GedApi(models.Model): _name = 'ged.api' def _get_token(self): 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_log(self, dunning_run): 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': dunning_run.resi_tukar_faktur } } response = requests.post(url_tracking, headers=headers, json=json_data) log = { 'body': str(json_data), 'responses': str(response.json()) } self.env['ged.tracking.log'].create(log) def get_tracking_awb(self, dunning_run): # current_time = datetime.now() # current_time = current_time.strftime('%Y-%m-%d %H:%M:%S') # query = [('app_id', '=', 1), ('expired_date', '>', current_time)] # token_data = self.env['token.storage'].search(query, order='expired_date desc', limit=1) # if not token_data: # token_data = self._get_token() # token = token_data # else: # token = token_data.access_token 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': dunning_run.resi_tukar_faktur } } response = requests.post(url_tracking, headers=headers, json=json_data) log = { 'body': str(json_data), 'responses': str(response.json()) } self.env['ged.tracking.log'].create(log) 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'] receiver_name = info_awb['receiver_name'] receiver_relationship = info_awb['receiver_relationship'] delivered_date = info_awb['delivered_date'] photo_pod = info_awb['photo_pod'] photo_signature = info_awb['photo_signature'] photo_location = info_awb['photo_location'] photo_receiver = info_awb['photo_receiver'] 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'] header_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, 'receiver_name': receiver_name, 'receiver_relationship': receiver_relationship, 'delivered_date': delivered_date, 'photo_pod': photo_pod, 'photo_signature': photo_signature, 'photo_location': photo_location, 'photo_receiver': photo_receiver, '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': header_last_status, 'dunning_id': dunning_run.id } 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'] if status == 'Received At Warehouse' and not dunning_run.date_kirim_tukar_faktur: dunning_run.date_kirim_tukar_faktur = created_at dunning_run.copy_date_faktur() 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, 'dunning_id': dunning_run.id } self.env['ged.tracking.line'].create(param_line) return ged_tracking class DunningRunGed(models.Model): _inherit = 'dunning.run' last_status_awb = fields.Char(string='Last Status AWB') ged_tracking = fields.One2many('ged.tracking', 'dunning_id', string='GED Tracking', auto_join=True) ged_tracking_line = fields.One2many('ged.tracking.line', 'dunning_id', string='GED Tracking Line', auto_join=True) def _get_tracking_history(self, awb_number='all_data'): if awb_number == 'all_data': query = [ ('last_status_awb', '!=', 'POD Return'), ('resi_tukar_faktur', '!=', False), ('shipper_faktur_id', '=', 123), ('date_terima_tukar_faktur', '=', False) ] else: query = [ ('last_status_awb', '!=', 'POD Return'), ('resi_tukar_faktur', '=', awb_number), ('shipper_faktur_id', '=', 123) ] dunnings = self.env['dunning.run'].search(query, limit=100) for dunning in dunnings: current_tracking = self.env['ged.tracking'].search([('awb_no', '=', dunning.resi_tukar_faktur)], limit=1) if current_tracking: current_tracking.unlink() # else: ged_api = self.env['ged.api'] ged_tracking = ged_api.get_tracking_awb(dunning) if ged_tracking: dunning.last_status_awb = ged_tracking.last_status if ged_tracking.last_status == 'POD Return': if not dunning.date_terima_tukar_faktur: dunning.date_terima_tukar_faktur = ged_tracking.delivered_date dunning.copy_date_faktur() return def _get_tracking_history_log(self, awb_number='all_data'): query = [ ('last_status_awb', '!=', 'POD Return'), ('resi_tukar_faktur', '=', awb_number), ('shipper_faktur_id', '=', 123) ] dunnings = self.env['dunning.run'].search(query, limit=1) for dunning in dunnings: ged_api = self.env['ged.api'] ged_api.get_tracking_awb_log(dunning) return def get_tracking_history_by_awb(self): self._get_tracking_history(self.resi_tukar_faktur) def get_tracking_history_by_awb_log(self): self._get_tracking_history_log(self.resi_tukar_faktur)