diff --git a/db/procedures/make_payment.sql b/db/procedures/make_payment.sql index bc7d037..54030c8 100644 --- a/db/procedures/make_payment.sql +++ b/db/procedures/make_payment.sql @@ -1,3 +1,5 @@ +-- Used by tenants to make an outstanding payment + CREATE OR REPLACE PROCEDURE make_payment ( amen_id IN amenity_payment.amenity_id%type, lease_id IN lease_payment.lease_id%type, @@ -10,12 +12,12 @@ CREATE OR REPLACE PROCEDURE make_payment ( IS BEGIN IF amen_id <> -1 THEN - INSERT INTO amenity_payment(date_paid, pay_method_id, pay_amt, payer, memo) - VALUES(DEFAULT, pay_method, pay_amt, payer, memo); + INSERT INTO amenity_payment(amenity_id, date_paid, pay_method_id, pay_amt, payer, memo) + VALUES(amen_id, CURRENT_TIMESTAMP, pay_method, pay_amt, payer, memo); ret := 0; ELSIF lease_id <> -1 THEN - INSERT INTO lease_payment(date_paid, pay_method_id, pay_amt, payer, memo) - VALUES(DEFAULT, pay_method, pay_amt, payer, memo); + INSERT INTO lease_payment(lease_id, date_paid, pay_method_id, pay_amt, payer, memo) + VALUES(lease_id, CURRENT_TIMESTAMP, pay_method, pay_amt, payer, memo); ret := 0; ELSE ret := 1; diff --git a/db/procedures/sign_lease.sql b/db/procedures/sign_lease.sql index 66badff..33bd988 100644 --- a/db/procedures/sign_lease.sql +++ b/db/procedures/sign_lease.sql @@ -1,32 +1,32 @@ CREATE OR REPLACE PROCEDURE sign_lease ( - personId IN payment_method.person_id%type, - propId IN lease.prop_id%type, - apt IN lease.apt%type, - termLength IN lease.term_length%type, - amt OUT number, - valid OUT number, - success OUT number - ) IS - rent_amount lease.rent_amount%type; - lease_id lease.id%type; - BEGIN - -- Ensures that the lease does not exist the same time as another - SELECT COUNT(*) into amt from lease where prop_id = propId and apt=apt and CURRENT_TIMESTAMP <= ADD_MONTHS(start_date, term_length); - -- Ensures that the apartment is a valid apartment - SELECT COUNT(*) into valid from apartment where prop_id = propId and apt = apt; - - IF amt <> 0 THEN - success := -1; - return; - ELSIF valid <> 0 THEN - success := -2; - return; - END IF; - - SELECT RENT into rent_amount from apartment where prop_id = propId and apt = apt; - INSERT INTO lease(prop_id, apt, start_date, term_length, rent_amount) VALUES(propId, apt, CURRENT_TIMESTAMP, termLength, rent_amount); - SELECT MAX(id) into lease_id from lease; - INSERT INTO person_on_lease VALUES(lease_id, personId); - success := 0; - END; + personId IN payment_method.person_id%type, + propId IN lease.prop_id%type, + apt IN lease.apt%type, + termLength IN lease.term_length%type, + amt OUT number, + valid OUT number, + success OUT number +) IS + rent_amount lease.rent_amount%type; + lease_id lease.id%type; +BEGIN + -- Ensures that the lease does not exist the same time as another + SELECT COUNT(*) into amt from lease where prop_id = propId and apt=apt and CURRENT_TIMESTAMP <= ADD_MONTHS(start_date, term_length); + -- Ensures that the apartment is a valid apartment + SELECT COUNT(*) into valid from apartment where prop_id = propId and apt = apt; + + IF amt <> 0 THEN + success := -1; + return; + ELSIF valid <> 0 THEN + success := -2; + return; + END IF; + + SELECT RENT into rent_amount from apartment where prop_id = propId and apt = apt; + INSERT INTO lease(prop_id, apt, start_date, term_length, rent_amount) VALUES(propId, apt, CURRENT_TIMESTAMP, termLength, rent_amount); + SELECT MAX(id) into lease_id from lease; + INSERT INTO person_on_lease VALUES(lease_id, personId); + success := 0; +END; diff --git a/db/queries/check_resident.sql b/db/queries/check_resident.sql index fb8eaac..ff4542e 100644 --- a/db/queries/check_resident.sql +++ b/db/queries/check_resident.sql @@ -1 +1,3 @@ +-- Check if a person is a resident or not, used for login + SELECT COUNT(*) FROM renter_info where person_id = ?; \ No newline at end of file diff --git a/db/queries/get_all_apt.sql b/db/queries/get_all_apt.sql new file mode 100644 index 0000000..d3f7683 --- /dev/null +++ b/db/queries/get_all_apt.sql @@ -0,0 +1,5 @@ +-- Used by NUMA to get more details about a certain apartment + +SELECT * +FROM apartment +WHERE prop_id = ?; \ No newline at end of file diff --git a/db/queries/get_all_prop.sql b/db/queries/get_all_prop.sql new file mode 100644 index 0000000..cf87eb5 --- /dev/null +++ b/db/queries/get_all_prop.sql @@ -0,0 +1,4 @@ +-- Used by NUMA to see all properties + +SELECT * +FROM property; \ No newline at end of file diff --git a/db/queries/numa_overview.sql b/db/queries/numa_overview.sql new file mode 100644 index 0000000..4ebc64c --- /dev/null +++ b/db/queries/numa_overview.sql @@ -0,0 +1,9 @@ +-- Used to get aggregate data about the leases +-- Group by prop_id (Avg term length and count of each property) +-- Group by term_length (Total # of tenants with the same term length) +-- Group by prop_id, term_length (Total # of tenants w/ same term length in the same property) +-- Group by nothing (Average term and total tenants) + +SELECT prop_id, avg(term_length), count(person_id) +FROM lease join person_on_lease on lease.id = person_on_lease.lease_id +GROUP BY cube(prop_id, term_length); \ No newline at end of file diff --git a/db/queries/view_lease_to_pay.sql b/db/queries/view_lease_to_pay.sql index 6e94edc..3ddce3e 100644 --- a/db/queries/view_lease_to_pay.sql +++ b/db/queries/view_lease_to_pay.sql @@ -1,8 +1,9 @@ -- Select leases that are still active +-- End date of the lease is past today's date +-- Payment has already been made for the month or first payment is due + SELECT id, prop_id, apt, start_date, term_length, rent_amount FROM lease left outer join lease_payment on lease.id = lease_payment.lease_id WHERE id in (SELECT lease_id from person_on_lease where person_id = 8) and TO_CHAR(ADD_MONTHS(start_date, term_length), 'YYYYMM') >= TO_CHAR(CURRENT_TIMESTAMP, 'YYYYMM') and - (TO_CHAR(date_paid, 'YYYYMM') = TO_CHAR(CURRENT_TIMESTAMP, 'YYYYMM') or TO_CHAR(date_paid, 'YYYYMM') is null); --- End date of the lease is past today's date --- Payment has already been made for the month or first payment is due \ No newline at end of file + (TO_CHAR(date_paid, 'YYYYMM') = TO_CHAR(CURRENT_TIMESTAMP, 'YYYYMM') or TO_CHAR(date_paid, 'YYYYMM') is null); \ No newline at end of file diff --git a/db/updates/add_person.sql b/db/updates/add_person.sql new file mode 100644 index 0000000..263f93b --- /dev/null +++ b/db/updates/add_person.sql @@ -0,0 +1,5 @@ +-- Used to register a new person into the NUMA system +-- For the purpose of a new leasee or a new visitor of one of NUMA's properties + +INSERT into person(first_name, last_name, age, phone_number, email, credit_score) +VALUES(?, ?, ?, ?, ?, ?); \ No newline at end of file diff --git a/db/updates/change_preferred_payment.sql b/db/updates/change_preferred_payment.sql new file mode 100644 index 0000000..d08a727 --- /dev/null +++ b/db/updates/change_preferred_payment.sql @@ -0,0 +1,5 @@ +-- Used to update a renter's preferred payment method + +UPDATE renter_info +SET preferred_payment = ? +WHERE person_id = ?; \ No newline at end of file diff --git a/db/updates/modify_lease_length.sql b/db/updates/modify_lease_length.sql new file mode 100644 index 0000000..92e453f --- /dev/null +++ b/db/updates/modify_lease_length.sql @@ -0,0 +1,5 @@ +-- Used by NUMA to end prematurely or extend a person's lease + +UPDATE lease +SET term_length = ? +WHERE id = ?; \ No newline at end of file diff --git a/db/updates/register_visit.sql b/db/updates/register_visit.sql new file mode 100644 index 0000000..d5eefc9 --- /dev/null +++ b/db/updates/register_visit.sql @@ -0,0 +1,4 @@ +-- Used by NUMA to register a new visit to a property + +INSERT into visited(person_id, date_visited, prop_id, apt) +VALUES(?, ?, ?, ?); \ No newline at end of file