Skip to content

Commit

Permalink
chore(background tasks): Changed how submission of stock info happens.
Browse files Browse the repository at this point in the history
Changed the submission of stock information from submitting when records
are inserted to the Stock Ledger to a general background task, that
executes, by default, evey 4 mins (configurable)
  • Loading branch information
GichanaMayaka committed Aug 1, 2024
1 parent e3a7703 commit b43c03c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 53 deletions.
15 changes: 4 additions & 11 deletions kenya_compliance/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,6 @@
"kenya_compliance.kenya_compliance.overrides.server.shared_overrides.validate"
],
},
"Stock Ledger Entry": {
"on_update": [
"kenya_compliance.kenya_compliance.overrides.server.stock_ledger_entry.on_update",
"kenya_compliance.kenya_compliance.overrides.server.stock_ledger_entry.inventory_submit",
]
},
"Purchase Invoice": {
"on_submit": [
"kenya_compliance.kenya_compliance.overrides.server.purchase_invoice.on_submit"
Expand All @@ -282,18 +276,17 @@
# ---------------

scheduler_events = {
# "all": [
# "kenya_compliance.tasks.all"
# ],
"all": [
"kenya_compliance.kenya_compliance.background_tasks.tasks.send_stock_information",
"kenya_compliance.kenya_compliance.background_tasks.tasks.send_item_inventory_information",
],
# "daily": [
# "kenya_compliance.tasks.daily"
# ],
"hourly": [
"kenya_compliance.kenya_compliance.background_tasks.tasks.send_sales_invoices_information",
"kenya_compliance.kenya_compliance.background_tasks.tasks.send_pos_invoices_information",
"kenya_compliance.kenya_compliance.background_tasks.tasks.send_stock_information",
"kenya_compliance.kenya_compliance.background_tasks.tasks.send_purchase_information",
"kenya_compliance.kenya_compliance.background_tasks.tasks.send_item_inventory_information",
"kenya_compliance.kenya_compliance.background_tasks.tasks.refresh_notices",
],
# "weekly": [
Expand Down
4 changes: 3 additions & 1 deletion kenya_compliance/kenya_compliance/apis/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,9 @@ def submit_inventory(request_data: str) -> None:
endpoints_builder.headers = headers
endpoints_builder.url = url
endpoints_builder.payload = payload
endpoints_builder.success_callback = submit_inventory_on_success
endpoints_builder.success_callback = partial(
submit_inventory_on_success, document_name=data["name"]
)
endpoints_builder.error_callback = on_error

frappe.enqueue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ def imported_item_submission_on_success(response: dict, document_name: str) -> N
frappe.db.set_value("Item", document_name, {"custom_imported_item_submitted": 1})


def submit_inventory_on_success(response: dict) -> None:
return None
def submit_inventory_on_success(response: dict, document_name: str) -> None:
frappe.db.set_value(
"Stock Ledger Entry",
document_name,
{"custom_inventory_submitted_successfully": 1},
)


def sales_information_submission_on_success(
Expand Down
9 changes: 5 additions & 4 deletions kenya_compliance/kenya_compliance/background_tasks/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
TAXATION_TYPE_DOCTYPE_NAME,
UNIT_OF_QUANTITY_DOCTYPE_NAME,
)
from ..overrides.server.stock_ledger_entry import on_update
from ..utils import build_headers, get_route_path, get_server_url

endpoints_builder = EndpointsBuilder()
Expand Down Expand Up @@ -72,8 +73,6 @@ def send_pos_invoices_information() -> None:


def send_stock_information() -> None:
from ..overrides.server.stock_ledger_entry import on_update

all_stock_ledger_entries: list[Document] = frappe.get_all(
"Stock Ledger Entry",
{"docstatus": 1, "custom_submitted_successfully": 0},
Expand Down Expand Up @@ -119,9 +118,10 @@ def send_item_inventory_information() -> None:
from ..apis.apis import submit_inventory

query = """
SELECT sle.name,
SELECT sle.name as name,
sle.owner,
sle.custom_submitted_successfully,
sle.custom_inventory_submitted_successfully,
qty_after_transaction as residual_qty,
sle.warehouse,
w.custom_branch as branch_id,
Expand All @@ -130,7 +130,8 @@ def send_item_inventory_information() -> None:
FROM `tabStock Ledger Entry` sle
INNER JOIN tabItem i ON sle.item_code = i.item_code
INNER JOIN tabWarehouse w ON sle.warehouse = w.name
WHERE sle.custom_submitted_successfully = '0'
WHERE sle.custom_submitted_successfully = '1'
AND sle.custom_inventory_submitted_successfully = '0'
ORDER BY sle.creation DESC;
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ frappe.ui.form.on('Navari KRA eTims Settings', {
frappe.call({
method:
'kenya_compliance.kenya_compliance.background_tasks.tasks.get_item_classification_codes',
args: {
request_data: {
name: frm.doc.name,
company_name: companyName,
},
},
args: {},
callback: (response) => {},
error: (error) => {
// Error Handling is Defered to the Server
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
from functools import partial
from hashlib import sha256
from typing import Literal
Expand All @@ -8,7 +7,6 @@
from erpnext.controllers.taxes_and_totals import get_itemised_tax_breakup_data

from ...apis.api_builder import EndpointsBuilder
from ...apis.apis import submit_inventory
from ...apis.remote_response_status_handlers import (
on_error,
stock_mvt_submission_on_success,
Expand All @@ -23,32 +21,6 @@
endpoints_builder = EndpointsBuilder()


def inventory_submit(doc: Document, method: str | None = None) -> None:
residual_qty = frappe.db.sql(
f"""
SELECT item_code, warehouse, actual_qty
FROM tabBin
WHERE item_code = '{doc.item_code}'
AND warehouse = '{doc.warehouse}'
LIMIT 1;
""",
as_dict=True,
)

request_data = {
"company": frappe.defaults.get_user_default("Company"),
"name": doc.name,
"item_code": frappe.get_value(
"Item", {"item_code": doc.item_code}, ["custom_item_code_etims"]
),
"owner": doc.owner,
"branch_id": get_warehouse_branch_id(doc.warehouse),
"residual_qty": residual_qty[0].actual_qty,
}

submit_inventory(json.dumps(request_data))


def on_update(doc: Document, method: str | None = None) -> None:
company_name = doc.company
all_items = frappe.db.get_all(
Expand Down
3 changes: 2 additions & 1 deletion kenya_compliance/kenya_compliance/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ async def make_post_request(
"""
# TODO: Refactor to a more efficient handling of creation of the session object
# as described in documentation
async with aiohttp.ClientSession(timeout=ClientTimeout(1200)) as session:
async with aiohttp.ClientSession(timeout=ClientTimeout(1800)) as session:
# Timeout of 1800 or 30 mins, especially for fetching Item classification
async with session.post(url, json=data, headers=headers) as response:
return await response.json()

Expand Down

0 comments on commit b43c03c

Please sign in to comment.