diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2023-06-27 17:05:47 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2023-06-27 17:05:47 +0700 |
| commit | 577fe5a01f3b782a2a441ad55f3cc721ba2e212c (patch) | |
| tree | 359fdcaadc4a9af6a0105c549f5bec40abaa615a | |
| parent | 57c571c40de1acfdcf23132782ec8c7fcbb8c6bd (diff) | |
add token storage for altama stock api
| -rwxr-xr-x | indoteknik_custom/models/__init__.py | 1 | ||||
| -rwxr-xr-x | indoteknik_custom/models/product_template.py | 66 | ||||
| -rw-r--r-- | indoteknik_custom/models/token_storage.py | 9 | ||||
| -rwxr-xr-x | indoteknik_custom/security/ir.model.access.csv | 3 |
4 files changed, 72 insertions, 7 deletions
diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index 3c6ce46c..4cc2433f 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -63,3 +63,4 @@ from . import procurement_monitoring_detail from . import brand_vendor from . import manufacturing from . import requisition +from . import token_storage diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py index 018d1b89..5fbac2e3 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -2,6 +2,8 @@ from odoo import fields, models, api from datetime import datetime, timedelta from odoo.exceptions import AccessError, UserError, ValidationError import logging +import requests +import json _logger = logging.getLogger(__name__) @@ -191,15 +193,67 @@ class ProductTemplate(models.Model): product.last_calculate_rating = current_time def _get_stock_website(self): - print(1) + qty = self._get_stock_altama() + print(qty) def _get_stock_altama(self): - print(1) - altama_stock_url = "https://erpapi.altama.co.id/erp/api/stock/buffer/btob" + current_time = datetime.now() + current_time = current_time.strftime('%Y-%m-%d %H:%M:%S') + query = [('source', '=', 'altama'), ('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_new_token_altama() + token = token_data['access_token'] + else: + token = token_data.access_token + + url = "https://erpapi.altama.co.id/erp/api/stock/buffer/btob" + auth = "Bearer "+token + headers = { + 'Content-Type': 'application/json', + 'Authorization': auth, + } + json_data = { + 'type_search': 'Item_code', + 'search_key':['RSG100-3'], + } + response = requests.post(url, headers=headers, json=json_data) + datas = json.loads(response.text)['data'] + for data in datas: + qty = data['availability'] + return qty or 0 + + def _get_new_token_altama(self): + url = "https://kc.altama.co.id/realms/altama/protocol/openid-connect/token" + auth = 'Basic SW5kb3Rla25pa19DbGllbnQ6Vm1iZExER1ZUS3RuVlRQdkU1MXRvRzdiTW51TE1WRVI=' + headers = { + 'Content-Type': 'application/x-www-form-urlencoded', + 'Authorization': auth, + } + data = { + 'grant_type': 'client_credentials', + } + + response = requests.post(url, headers=headers, data=data).json() + lookup_json = json.dumps(response, indent=4, sort_keys=True) + token = json.loads(lookup_json)['access_token'] + expires_in = json.loads(lookup_json)['expires_in'] + + current_time = datetime.now() + delta_time = current_time + timedelta(seconds=int(expires_in)) + + current_time = current_time.strftime('%Y-%m-%d %H:%M:%S') + delta_time = delta_time.strftime('%Y-%m-%d %H:%M:%S') + + values = { + 'source': 'altama', + 'access_token': token, + 'expires_in': expires_in, + 'expired_date': delta_time, + } + self.env['token.storage'].create([values]) + return values - def _get_token_altama(self): - print(1) - altama_token_url = "https://kc.altama.co.id/realms/altama/protocol/openid-connect/token" class ProductProduct(models.Model): _inherit = "product.product" diff --git a/indoteknik_custom/models/token_storage.py b/indoteknik_custom/models/token_storage.py new file mode 100644 index 00000000..0eef7f20 --- /dev/null +++ b/indoteknik_custom/models/token_storage.py @@ -0,0 +1,9 @@ +from odoo import fields, models + +class TokenStorage(models.Model): + + _name = 'token.storage' + source = fields.Char(string='Source') + access_token = fields.Char(string='Access Token') + expires_in = fields.Integer(string='Expires In') + expired_date = fields.Datetime(string='Expired Date') diff --git a/indoteknik_custom/security/ir.model.access.csv b/indoteknik_custom/security/ir.model.access.csv index 26e852f1..fb8f48f1 100755 --- a/indoteknik_custom/security/ir.model.access.csv +++ b/indoteknik_custom/security/ir.model.access.csv @@ -51,4 +51,5 @@ access_rajaongkir_kurir,access.rajaongkir.kurir,model_rajaongkir_kurir,,1,1,1,1 access_brand_vendor,access.brand.vendor,model_brand_vendor,,1,1,1,1 access_requisition,access.requisition,model_requisition,,1,1,1,1 access_requisition_line,access.requisition.line,model_requisition_line,,1,1,1,1 -access_requisition_purchase_match,access.requisition.purchase.match,model_requisition_purchase_match,,1,1,1,1
\ No newline at end of file +access_requisition_purchase_match,access.requisition.purchase.match,model_requisition_purchase_match,,1,1,1,1 +access_token_storage,access.token_storage,model_token_storage,,1,1,1,1
\ No newline at end of file |
