Skip to content

Commit

Permalink
Merge pull request #176 from lahirulakruwan/main
Browse files Browse the repository at this point in the history
Monthly payment report apis changes added
  • Loading branch information
YujithIsura authored Oct 24, 2024
2 parents 1670a7e + d3ab2e5 commit d1fb636
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 18 deletions.
15 changes: 14 additions & 1 deletion api/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies = [
{org = "ballerina", name = "graphql"},
{org = "ballerina", name = "io"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "regex"},
{org = "ballerina", name = "sql"},
{org = "ballerina", name = "test"},
{org = "ballerina", name = "time"},
Expand Down Expand Up @@ -112,7 +113,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.16"
version = "2.10.17"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "cache"},
Expand Down Expand Up @@ -313,6 +314,18 @@ dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "regex"
version = "1.4.3"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.string"}
]
modules = [
{org = "ballerina", packageName = "regex", moduleName = "regex"}
]

[[package]]
org = "ballerina"
name = "sql"
Expand Down
51 changes: 51 additions & 0 deletions api/calendar_meta_data.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
public isolated service class CalendarMetaData {

private CalendarMetadata calendar_metadata;

isolated function init(int? id=0, CalendarMetadata? calendarMetadata = null) returns error? {

if (calendarMetadata != null) {
self.calendar_metadata = calendarMetadata.cloneReadOnly();
return;
}

lock {

CalendarMetadata calendar_metadata_raw;

if (id>0) {

calendar_metadata_raw = check db_client->queryRow(
`SELECT *
FROM calendar_metadata
WHERE id = ${id};`);

} else {
return error("No id provided");
}

self.calendar_metadata = calendar_metadata_raw.cloneReadOnly();

}

}

isolated resource function get id() returns int?|error {
lock {
return self.calendar_metadata.id;
}
}

isolated resource function get organization_id() returns int?|error {
lock {
return self.calendar_metadata.organization_id;
}
}

isolated resource function get monthly_payment_amount() returns decimal?|error {
lock {
return self.calendar_metadata.monthly_payment_amount;
}
}

}
163 changes: 146 additions & 17 deletions api/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -5879,30 +5879,30 @@ AND p.organization_id IN (
isolated resource function get cities(int? district_id) returns CityData[]|error? {
stream<City, error?> cities_data;

if(district_id !=null && district_id != 0 && district_id > 0){
if (district_id != null && district_id != 0 && district_id > 0) {

lock {
cities_data = db_client->query(
lock {
cities_data = db_client->query(
`SELECT *
from city
where district_id=${district_id};`);
}
}

CityData[] cityDatas = [];
CityData[] cityDatas = [];

check from City city_data_record in cities_data
do {
CityData|error cityData = new CityData(null, 0,city_data_record);
if !(cityData is error) {
cityDatas.push(cityData);
}
};
check from City city_data_record in cities_data
do {
CityData|error cityData = new CityData(null, 0, city_data_record);
if !(cityData is error) {
cityDatas.push(cityData);
}
};

check cities_data.close();
return cityDatas;
}else{
return error("Provide valid value for district_id.");
}
check cities_data.close();
return cityDatas;
} else {
return error("Provide valid value for district_id.");
}
}

isolated resource function get all_organizations() returns OrganizationData[]|error? {
Expand All @@ -5928,6 +5928,135 @@ AND p.organization_id IN (
return organizationDatas;
}

remote function add_monthly_leave_dates(MonthlyLeaveDates monthly_leave_dates) returns MonthlyLeaveDatesData|error? {

string leaveDatesString = "";
int totalLeaveDates = 0;
decimal dailyAmount = 0.0;

MonthlyLeaveDates|error? monthlyLeaveDatesRaw = db_client->queryRow(
`SELECT *
FROM monthly_leave_dates
WHERE year = ${monthly_leave_dates.year} and month = ${monthly_leave_dates.month} and
organization_id = ${monthly_leave_dates.organization_id};`
);

if ( monthlyLeaveDatesRaw is MonthlyLeaveDates) {
return error("A record for this organization id already exists for the same year and month.");
}


int[] leaveDates = monthly_leave_dates.leave_dates_list;

foreach int date in leaveDates {
leaveDatesString = leaveDatesString + date.toString() + ",";
totalLeaveDates = totalLeaveDates+1;
}

if(leaveDatesString.length() > 0){
leaveDatesString = leaveDatesString.substring(0,leaveDatesString.length()-1);
}


int? totalSchoolDays = monthly_leave_dates.total_days_in_month - totalLeaveDates;


CalendarMetadata|error? monthlyPaymentAmount = check db_client->queryRow(
`SELECT *
FROM calendar_metadata
WHERE organization_id = ${monthly_leave_dates.organization_id};`
);

if (monthlyPaymentAmount is CalendarMetadata) {
dailyAmount = monthlyPaymentAmount.monthly_payment_amount/totalSchoolDays ?: 0.0;
}


sql:ExecutionResult res = check db_client->execute(
`INSERT INTO monthly_leave_dates (
year,
month,
organization_id,
leave_dates,
daily_amount
) VALUES (
${monthly_leave_dates.year},
${monthly_leave_dates.month},
${monthly_leave_dates.organization_id},
${leaveDatesString},
${dailyAmount}
);`
);

int|string? insert_id = res.lastInsertId;
if !(insert_id is int) {
return error("Unable to insert Monthly Leave Dates record");
}

return new (insert_id);
}

remote function update_monthly_leave_dates(MonthlyLeaveDates monthly_leave_dates) returns MonthlyLeaveDatesData|error? {

int id = monthly_leave_dates.id ?: 0;
if (id == 0) {
return error("Unable to update Monthly Leave Dates record");
}

string leaveDatesString = "";
int totalLeaveDates = 0;
decimal dailyAmount = 0.0;

int[] leaveDates = monthly_leave_dates.leave_dates_list;

foreach int date in leaveDates {
leaveDatesString = leaveDatesString + date.toString() + ",";
totalLeaveDates = totalLeaveDates+1;
}

if(leaveDatesString.length() > 0){
leaveDatesString = leaveDatesString.substring(0,leaveDatesString.length()-1);
}


int? totalSchoolDays = monthly_leave_dates.total_days_in_month - totalLeaveDates;


CalendarMetadata|error? monthlyPaymentAmount = check db_client->queryRow(
`SELECT *
FROM calendar_metadata
WHERE organization_id = ${monthly_leave_dates.organization_id};`
);

if (monthlyPaymentAmount is CalendarMetadata) {
dailyAmount = monthlyPaymentAmount.monthly_payment_amount/totalSchoolDays ?: 0.0;
}

sql:ExecutionResult res = check db_client->execute(
`UPDATE monthly_leave_dates SET
year = ${monthly_leave_dates.year},
month = ${monthly_leave_dates.month},
organization_id = ${monthly_leave_dates.organization_id},
leave_dates = ${leaveDatesString},
daily_amount = ${dailyAmount}
WHERE id = ${id};`
);

if (res.affectedRowCount == sql:EXECUTION_FAILED) {
return error("Unable to update Monthly Leave Dates record");
}

return new (id);
}

isolated resource function get monthly_leave_dates_record_by_id(int? id) returns MonthlyLeaveDatesData|error? {
if (id != null) {
return new (id);
} else {
return error("Provide non-null value for id.");
}
}

}

isolated function calculateWeekdays(time:Utc toDate, time:Utc fromDate) returns int {
Expand Down
100 changes: 100 additions & 0 deletions api/monthly_leave_dates.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import ballerina/io;
import ballerina/regex;

public isolated service class MonthlyLeaveDatesData {

private MonthlyLeaveDates monthly_leave_dates;

isolated function init(int? id = 0, MonthlyLeaveDates? monthlyLeaveDates = null) returns error? {

if (monthlyLeaveDates != null) {
self.monthly_leave_dates = monthlyLeaveDates.cloneReadOnly();
return;
}

lock {

MonthlyLeaveDates monthly_leave_dates_raw;

if (id > 0) {

monthly_leave_dates_raw = check db_client->queryRow(
`SELECT *
FROM monthly_leave_dates
WHERE id = ${id};`);

} else {
return error("No id provided");
}

self.monthly_leave_dates = monthly_leave_dates_raw.cloneReadOnly();

}

}

isolated resource function get id() returns int?|error {
lock {
return self.monthly_leave_dates.id;
}
}

isolated resource function get year() returns int?|error {
lock {
return self.monthly_leave_dates.year;
}
}

isolated resource function get month() returns int?|error {
lock {
return self.monthly_leave_dates.month;
}
}

isolated resource function get organization_id() returns int?|error {
lock {
return self.monthly_leave_dates.organization_id;
}
}

isolated resource function get leave_dates_list() returns int[]?|error {
string[] strArray;

lock{
string? str = self.monthly_leave_dates.leave_dates;

// Split the string by commas using string:split
strArray = regex:split(str ?: "",",");
}
// Convert the string array to an integer array

int[] intArray = from var s in strArray
where s.trim() != ""
select check int:fromString(s);

// Output the integer array
io:println(intArray);

return intArray;

}

isolated resource function get daily_amount() returns decimal?|error {
lock {
return self.monthly_leave_dates.daily_amount;
}
}

isolated resource function get created() returns string?|error {
lock {
return self.monthly_leave_dates.created;
}
}

isolated resource function get updated() returns string?|error {
lock {
return self.monthly_leave_dates.updated;
}
}

}
21 changes: 21 additions & 0 deletions api/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -689,3 +689,24 @@ public type VehicleFuelConsumption record{|
string? created;
string? updated;
|};

public type CalendarMetadata record {|
readonly string? record_type = "calendar_metadata";
int id?;
int? organization_id;
decimal? monthly_payment_amount;
|};

public type MonthlyLeaveDates record {|
readonly string? record_type = "monthly_leave_dates";
int id?;
int? year;
int? month;
int? total_days_in_month;
int? organization_id;
int[] leave_dates_list;
string? leave_dates;
decimal? daily_amount;
string? created;
string? updated;
|};

0 comments on commit d1fb636

Please sign in to comment.