summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/product_template.py
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2023-06-27 17:05:47 +0700
committerstephanchrst <stephanchrst@gmail.com>2023-06-27 17:05:47 +0700
commit577fe5a01f3b782a2a441ad55f3cc721ba2e212c (patch)
tree359fdcaadc4a9af6a0105c549f5bec40abaa615a /indoteknik_custom/models/product_template.py
parent57c571c40de1acfdcf23132782ec8c7fcbb8c6bd (diff)
add token storage for altama stock api
Diffstat (limited to 'indoteknik_custom/models/product_template.py')
-rwxr-xr-xindoteknik_custom/models/product_template.py66
1 files changed, 60 insertions, 6 deletions
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"