Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
YujithIsura committed Jun 12, 2024
2 parents 9633951 + e3d3469 commit 0d27160
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 47 deletions.
6 changes: 3 additions & 3 deletions api/inventory_data.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
281 changes: 240 additions & 41 deletions api/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -4723,58 +4723,177 @@ lock {
stream<Inventory, error?> 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);
}
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions api/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0d27160

Please sign in to comment.