diff --git a/api/inventory_data.bal b/api/inventory_data.bal index 14aea67..8e40c1a 100644 --- a/api/inventory_data.bal +++ b/api/inventory_data.bal @@ -1,7 +1,7 @@ public isolated service class InventoryData{ private Inventory inventory; - isolated function init(int id,Inventory? inventory=null) returns error?{ + isolated function init(int? id = 0, Inventory? inventory = null) returns error? { if(inventory != null){ self.inventory = inventory.cloneReadOnly(); return; @@ -107,6 +107,19 @@ public isolated service class InventoryData{ } } + isolated resource function get resource_property() returns ResourcePropertyData|error? { + int id = 0; + lock { + id = self.inventory.resource_property_id ?: 0; + if( id == 0) { + return null; // no point in querying if address id is null + } + } + return new ResourcePropertyData(id); + } + + + isolated resource function get created() returns string?|error { lock { return self.inventory.created; diff --git a/api/main.bal b/api/main.bal index 334e183..58a4c36 100644 --- a/api/main.bal +++ b/api/main.bal @@ -4718,6 +4718,87 @@ lock { } } + isolated resource function get inventory_data_by_organization(int? organization_id,string? date= null) returns InventoryData[]|error? { + stream inventory_data; + + // first check if inventory data for date are already have + Inventory|error dateInventoryData = db_client->queryRow( + `SELECT I.id,I.avinya_type_id,I.consumable_id, + I.organization_id,I.person_id,I.quantity,I.quantity_in,I.quantity_out,RP.id as resource_property_id,RP.value as resource_property_value + FROM inventory I + INNER JOIN resource_property RP ON I.consumable_id = RP.consumable_id + WHERE I.organization_id = ${organization_id} AND DATE(I.updated) = ${date};` + ); + + // lock { + // inventory_data = db_client->query( + // `SELECT I.id,I.avinya_type_id,I.consumable_id, + // I.organization_id,I.person_id,I.quantity,I.quantity_in,I.quantity_out,RP.id as resource_property_id,RP.value as resource_property_value + // FROM inventory I + // INNER JOIN resource_property RP ON I.consumable_id = RP.consumable_id + // WHERE I.organization_id = ${organization_id} AND DATE(I.updated) = ${date}; + // ` + // ); + // } + + if !(dateInventoryData is Inventory) { + //if(inventory_data.next() == ()){ + + lock { + inventory_data = db_client->query( + `SELECT I.id,I.avinya_type_id,I.consumable_id, + I.organization_id,I.person_id,I.quantity,I.quantity_in,I.quantity_out,RP.id as resource_property_id,RP.value as resource_property_value + FROM inventory I + INNER JOIN resource_property RP ON I.consumable_id = RP.consumable_id + INNER JOIN ( + SELECT consumable_id, MAX(updated) as max_updated_at + FROM inventory + WHERE organization_id = ${organization_id} + GROUP BY consumable_id + ) max_updated ON I.consumable_id = max_updated.consumable_id AND I.updated = max_updated.max_updated_at; + ` + ); + } + + }else{ + + lock { + inventory_data = db_client->query( + `SELECT I.id,I.avinya_type_id,I.consumable_id, + I.organization_id,I.person_id,I.quantity,I.quantity_in,I.quantity_out,RP.id as resource_property_id,RP.value as resource_property_value + FROM inventory I + INNER JOIN resource_property RP ON I.consumable_id = RP.consumable_id + WHERE I.organization_id = ${organization_id} AND DATE(I.updated) = ${date}; + ` + ); + } + + } + + + InventoryData[] inventoryDatas = []; + + check from Inventory inventory in inventory_data + do { + InventoryData|error inventoryData = new InventoryData(0,inventory); + if !(inventoryData is error) { + inventoryDatas.push(inventoryData); + } + }; + + if(inventoryDatas.length() == 0){ + Inventory inventory = {id:(),avinya_type_id: (),consumable_id: (),quantity: 0, + quantity_in: 0 , quantity_out:0,asset_id: 0, + organization_id:0,person_id: 0,created: (), + resource_property_id: () ,resource_property_value: (), + updated: () }; + InventoryData|error inventoryData = new InventoryData(0, inventory); + inventoryDatas.push(check inventoryData); + } + + check inventory_data.close(); + return inventoryDatas; + } } isolated function calculateWeekdays(time:Utc toDate, time:Utc fromDate) returns int { diff --git a/api/types.bal b/api/types.bal index cf4aa1a..9fc90ba 100644 --- a/api/types.bal +++ b/api/types.bal @@ -613,6 +613,8 @@ public type Inventory record {| int? quantity; int? quantity_in; int? quantity_out; + int? resource_property_id; + string? resource_property_value; string? created; string? updated; |};