diff --git a/api/inventory_data.bal b/api/inventory_data.bal index bae7064..537e76a 100644 --- a/api/inventory_data.bal +++ b/api/inventory_data.bal @@ -89,19 +89,19 @@ public isolated service class InventoryData{ return new PersonData((),id); } - isolated resource function get quantity() returns int?|error { + isolated resource function get quantity() returns decimal?|error { lock { return self.inventory.quantity; } } - isolated resource function get quantity_in() returns int?|error { + isolated resource function get quantity_in() returns decimal?|error { lock { return self.inventory.quantity_in; } } - isolated resource function get quantity_out() returns int?|error { + isolated resource function get quantity_out() returns decimal?|error { lock { return self.inventory.quantity_out; } diff --git a/api/main.bal b/api/main.bal index 8fae651..7768367 100644 --- a/api/main.bal +++ b/api/main.bal @@ -4723,58 +4723,177 @@ lock { stream inventory_data; // first check if inventory data for date are already have - 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, - C.name, C.description, C.manufacturer - FROM inventory I - INNER JOIN consumable C ON I.consumable_id = C.id - INNER JOIN resource_property RP ON C.id = RP.consumable_id - WHERE I.organization_id = ${organization_id} AND DATE(I.updated) = ${date};` - ); - + int|error? check_inventory_data_for_date = check db_client->queryRow( + `SELECT + CASE + WHEN ( + SELECT COUNT(DISTINCT I.consumable_id) + FROM inventory I + WHERE I.organization_id = ${organization_id} + AND DATE(I.updated) = ${date} + ) = ( + SELECT COUNT(*) + FROM consumable + ) THEN 1 + ELSE 0 + END AS all_consumables_present;`); + + + if (check_inventory_data_for_date == 0){ + + int|error? check_least_updated_inventory_data_for_date = check db_client->queryRow( + `SELECT + CASE + WHEN ( + SELECT COUNT(DISTINCT I.consumable_id) + FROM inventory I + 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 + ) = ( + SELECT COUNT(*) + FROM consumable + ) THEN 1 + ELSE 0 + END AS all_consumables_present;`); + + + if(check_least_updated_inventory_data_for_date == 0){ + + + inventory_data = db_client->query( + `SELECT + I.id, + I.avinya_type_id, + C.id AS consumable_id, + I.organization_id, + I.person_id, + COALESCE(I.quantity, 0.0) AS quantity, + COALESCE(I.quantity_in, 0.0) AS quantity_in, + COALESCE(I.quantity_out, 0.0) AS quantity_out, + RP.id AS resource_property_id, + RP.value AS resource_property_value, + C.name, + C.description, + C.manufacturer + FROM + consumable C + LEFT JOIN + (SELECT * FROM inventory WHERE organization_id = ${organization_id}) I + ON C.id = I.consumable_id AND I.updated = ( + SELECT MAX(I2.updated) + FROM inventory I2 + WHERE I2.consumable_id = C.id AND I2.organization_id = ${organization_id} + ) + LEFT JOIN + resource_property RP + ON C.id = RP.consumable_id;`); + }else{ - 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, - C.name,C.description,C.manufacturer - FROM inventory I - INNER JOIN consumable C ON I.consumable_id = C.id - INNER JOIN resource_property RP ON C.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; - ` - ); - if(inventory_data.next()== ()){ - - inventory_data = db_client->query( - `SELECT I.id,I.avinya_type_id,I.consumable_id,I.organization_id,I.person_id,COALESCE(I.quantity, 0) AS quantity, - COALESCE(I.quantity_in, 0) AS quantity_in,COALESCE(I.quantity_out, 0) AS quantity_out, - RP.id as resource_property_id,RP.value as resource_property_value, - C.name,C.description,C.manufacturer - FROM inventory I - RIGHT JOIN consumable C ON I.consumable_id = C.id - Left JOIN resource_property RP ON C.id = RP.consumable_id; - `); + 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, + C.name, + C.description, + C.manufacturer + FROM + consumable C + INNER JOIN + ( + SELECT + I1.* + FROM + inventory I1 + INNER JOIN + ( + SELECT + consumable_id, + MAX(updated) AS max_updated_at + FROM + inventory + WHERE + organization_id = ${organization_id} + GROUP BY + consumable_id + ) latest_inventory + ON I1.consumable_id = latest_inventory.consumable_id + AND I1.updated = latest_inventory.max_updated_at + ) I + ON C.id = I.consumable_id + LEFT JOIN + resource_property RP + ON C.id = RP.consumable_id;`); } - } + + }else{ - } + 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, + C.name, + C.description, + C.manufacturer + FROM + inventory I + INNER JOIN + consumable C ON I.consumable_id = C.id + INNER JOIN + resource_property RP ON C.id = RP.consumable_id + INNER JOIN ( + SELECT + consumable_id, + MAX(updated) AS latest_update + FROM + inventory + WHERE + organization_id = ${organization_id} + AND DATE(updated) = ${date} + GROUP BY + consumable_id + ) Latest + ON I.consumable_id = Latest.consumable_id + AND I.updated = Latest.latest_update + 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); } @@ -4783,6 +4902,86 @@ lock { check inventory_data.close(); return inventoryDatas; } + + remote function consumable_replenishment(Inventory[] inventories) returns InventoryData[]|error? { + + InventoryData[] newlyAddedInventoryDatas = []; + + foreach Inventory inventory in inventories { + sql:ExecutionResult response = check db_client->execute( + `INSERT INTO inventory ( + avinya_type_id, + consumable_id, + organization_id, + person_id, + quantity, + quantity_in, + created, + updated + ) VALUES ( + ${inventory.avinya_type_id}, + ${inventory.consumable_id}, + ${inventory.organization_id}, + ${inventory.person_id}, + ${inventory.quantity}, + ${inventory.quantity_in}, + ${inventory.updated}, + ${inventory.updated} + );` + ); + + int|string? insert_id = response.lastInsertId; + if !(insert_id is int) { + return error("Unable to insert inventories"); + } else { + InventoryData|error newlyAddedInventoryData = new InventoryData(insert_id); + if !(newlyAddedInventoryData is error) { + newlyAddedInventoryDatas.push(newlyAddedInventoryData); + } + } + } + return newlyAddedInventoryDatas; + } + + remote function consumable_depletion(Inventory[] inventories) returns InventoryData[]|error? { + + InventoryData[] newlyAddedInventoryDepletionDatas = []; + + foreach Inventory inventory in inventories { + sql:ExecutionResult response = check db_client->execute( + `INSERT INTO inventory ( + avinya_type_id, + consumable_id, + organization_id, + person_id, + quantity, + quantity_out, + created, + updated + ) VALUES ( + ${inventory.avinya_type_id}, + ${inventory.consumable_id}, + ${inventory.organization_id}, + ${inventory.person_id}, + ${inventory.quantity}, + ${inventory.quantity_out}, + ${inventory.updated}, + ${inventory.updated} + );` + ); + + int|string? insert_id = response.lastInsertId; + if !(insert_id is int) { + return error("Unable to insert inventory depletion data"); + } else { + InventoryData|error newlyAddedInventoryDepletionData = new InventoryData(insert_id); + if !(newlyAddedInventoryDepletionData is error) { + newlyAddedInventoryDepletionDatas.push(newlyAddedInventoryDepletionData); + } + } + } + return newlyAddedInventoryDepletionDatas; + } } isolated function calculateWeekdays(time:Utc toDate, time:Utc fromDate) returns int { diff --git a/api/types.bal b/api/types.bal index d6c3c39..3e1e5d6 100644 --- a/api/types.bal +++ b/api/types.bal @@ -613,9 +613,9 @@ public type Inventory record {| string? manufacturer; int? organization_id; int? person_id; - int? quantity; - int? quantity_in; - int? quantity_out; + decimal? quantity; + decimal? quantity_in; + decimal? quantity_out; int? resource_property_id; string? resource_property_value; string? created;