Skip to content

Commit

Permalink
Merge pull request #132 from lahirulakruwan/main
Browse files Browse the repository at this point in the history
Asset gdapi changes added
  • Loading branch information
YujithIsura authored May 22, 2024
2 parents cc49c86 + 90590e9 commit e48302b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
15 changes: 14 additions & 1 deletion api/inventory_data.bal
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down
81 changes: 81 additions & 0 deletions api/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -4718,6 +4718,87 @@ lock {
}
}

isolated resource function get inventory_data_by_organization(int? organization_id,string? date= null) returns InventoryData[]|error? {
stream<Inventory, error?> 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 {
Expand Down
2 changes: 2 additions & 0 deletions api/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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;
|};
Expand Down

0 comments on commit e48302b

Please sign in to comment.