Skip to content

Commit

Permalink
feat: get price package from jubilee
Browse files Browse the repository at this point in the history
  • Loading branch information
av-dev2 committed Jun 24, 2024
1 parent 446df5c commit 25ec74d
Show file tree
Hide file tree
Showing 19 changed files with 666 additions and 10 deletions.
68 changes: 63 additions & 5 deletions hms_tz/jubilee/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
from frappe import _
from time import sleep
from erpnext import get_default_company
from frappe.utils.background_jobs import enqueue
from hms_tz.jubilee.api.token import get_jubilee_service_token
from hms_tz.jubilee.doctype.jubilee_response_log.jubilee_response_log import add_jubilee_log
from hms_tz.jubilee.api.price_package import sync_jubilee_price_package
from hms_tz.jubilee.doctype.jubilee_response_log.jubilee_response_log import (
add_jubilee_log,
)


@frappe.whitelist()
Expand All @@ -19,7 +23,9 @@ def get_member_card_detials(card_no, insurance_provider):

if not company:
company = frappe.get_list(
"Company Insurance Setting", fields=["company"], filters={"enable": 1, "api_provider": insurance_provider}
"Company Insurance Setting",
fields=["company"],
filters={"enable": 1, "api_provider": insurance_provider},
)[0].company
if not company:
frappe.throw(_("No companies found to connect to Jubilee"))
Expand All @@ -32,9 +38,7 @@ def get_member_card_detials(card_no, insurance_provider):
"service_url",
)
headers = {"Authorization": "Bearer " + token}
url = (
str(service_url) + f"/jubileeapi/Getcarddetails?MemberNo={str(card_no)}"
)
url = str(service_url) + f"/jubileeapi/Getcarddetails?MemberNo={str(card_no)}"
for i in range(3):
try:
r = requests.get(url, headers=headers, timeout=5)
Expand Down Expand Up @@ -77,3 +81,57 @@ def get_member_card_detials(card_no, insurance_provider):
continue
else:
raise e


@frappe.whitelist()
def enqueue_get_jubilee_price_packages(company):
enqueue(
method=get_jubilee_price_packages,
job_name="get_jubilee_price_packages",
queue="default",
timeout=None,
is_async=True,
company=company,
)


@frappe.whitelist()
def get_jubilee_price_packages(company, insurance_provider="Jubilee"):
if not company:
frappe.throw(_("No companies found to connect to Jubilee"))

token = get_jubilee_service_token(company, insurance_provider)

service_url = frappe.get_cached_value(
"Company Insurance Setting",
{"company": company, "api_provider": insurance_provider},
"service_url",
)
headers = {"Authorization": "Bearer " + token}
url = str(service_url) + "/jubileeapi/GetPriceList"
r = requests.get(url, headers=headers, timeout=300)
if r.status_code != 200:
add_jubilee_log(
request_type="GetPricePackage",
request_url=url,
request_header=headers,
response_data=r.text,
status_code=r.status_code,
ref_doctype="Jubilee Price Package",
company=company,
)
frappe.throw(json.loads(r.text))
else:
if json.loads(r.text):
log_name = add_jubilee_log(
request_type="GetPricePackage",
request_url=url,
request_header=headers,
response_data=r.text,
status_code=r.status_code,
ref_doctype="Jubilee Price Package",
company=company,
)

packages = json.loads(r.text)["Description"]
sync_jubilee_price_package(packages, company, log_name)
153 changes: 153 additions & 0 deletions hms_tz/jubilee/api/price_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import json
import frappe
from time import sleep
from frappe.utils import now_datetime
from frappe.query_builder import DocType


def sync_jubilee_price_package(
packages, company, log_name, insurance_provider="Jubilee"
):
if len(packages) == 0:
return

delete_price_package(company)

sleep(30)
create_price_package(packages, company, log_name)

sleep(30)
set_package_diff(company)


def delete_price_package(company):
jpp = DocType("Jubilee Price Package")
frappe.qb.from_(jpp).delete().where(jpp.company == company).run()


def create_price_package(packages, company, log_name):
fields = [
"name",
"company",
"itemcode",
"itemname",
"cleanname",
"itemprice",
"providerid",
"log_name",
"time_stamp",
]

data = []
time_stamp = now_datetime()
for row in packages:
jpp_name = frappe.generate_hash(length=10)

data.append(
(
jpp_name,
company,
row.get("ItemCode"),
row.get("ItemName"),
row.get("CleanName"),
row.get("ItemPrice"),
row.get("ProviderID"),
log_name,
time_stamp,
)
)
frappe.db.bulk_insert(
"Jubilee Price Package", fields=fields, values=data, chunk_size=1000
)


def set_package_diff(company):
logs = frappe.get_all(
"Jubilee Response Log",
filters={
"request_type": "GetPricePackage",
"response_data": ["not in", ["", None]],
"company": company,
},
fields=["name", "response_data"],
order_by="creation desc",
page_length=2,
)
if len(logs) < 2:
return

current_rec = json.loads(logs[0]["response_data"])
previous_rec = json.loads(logs[1]["response_data"])
current_price_packages = current_rec.get("Description")
previousـprice_packages = previous_rec.get("Description")

diff_price_packages_from_current = [
i for i in current_price_packages if i not in previousـprice_packages
]
diff_price_packages_from_previous = [
i for i in previousـprice_packages if i not in current_price_packages
]

changed_price_packages = []
new_price_packages = []

for e in diff_price_packages_from_current:
exist_rec = next(
(
item
for item in diff_price_packages_from_previous
if item.get("ItemCode") == e.get("ItemCode")
),
None,
)
if exist_rec:
changed_price_packages.append(exist_rec)
else:
new_price_packages.append(e)

deleted_price_packages = []

for z in diff_price_packages_from_previous:
exist_rec = next(
(
item
for item in diff_price_packages_from_current
if item.get("ItemCode") == z.get("ItemCode")
),
None,
)
if not exist_rec:
deleted_price_packages.append(z)

if (
len(changed_price_packages) > 0
or len(new_price_packages) > 0
or len(deleted_price_packages) > 0
):
doc = frappe.new_doc("Jubilee Update")

add_price_packages_records(doc, changed_price_packages, "Changed")
add_price_packages_records(doc, new_price_packages, "New")
add_price_packages_records(doc, deleted_price_packages, "Deleted")

if (doc.get("price_package") and len(doc.price_package)) or ():
doc.company = company
doc.current_log = logs[0].name
doc.previous_log = logs[1].name
doc.save(ignore_permissions=True)


def add_price_packages_records(doc, rec, type):
if not len(rec) > 0:
return

for e in rec:
price_row = doc.append("price_package", {})
price_row.itemcode = e.get("ItemCode")
price_row.type = type
price_row.olditemcode = e.get("OldItemCode")
price_row.itemname = e.get("ItemName")
price_row.strength = e.get("Strength")
price_row.dosage = e.get("Dosage")
price_row.unitprice = e.get("UnitPrice")
price_row.record = json.dumps(e)
2 changes: 2 additions & 0 deletions hms_tz/jubilee/api/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def make_jubilee_token_request(doc, url, headers, payload, fields):
request_body=payload,
response_data=data,
status_code=r.status_code,
company=doc.company,
)

if (
Expand All @@ -46,6 +47,7 @@ def make_jubilee_token_request(doc, url, headers, payload, fields):
request_body=payload,
response_data=data,
status_code=r.status_code,
company=doc.company,
)
frappe.throw(data)

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2024, Aakvatech and contributors
// For license information, please see license.txt

frappe.ui.form.on('Jubilee Price Package', {
// refresh: function(frm) {

// }
});
Loading

0 comments on commit 25ec74d

Please sign in to comment.