Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
add more sql statements
Browse files Browse the repository at this point in the history
  • Loading branch information
L23de committed Apr 20, 2022
1 parent f7a89a5 commit de9a153
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 37 deletions.
10 changes: 6 additions & 4 deletions db/procedures/make_payment.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand Down
60 changes: 30 additions & 30 deletions db/procedures/sign_lease.sql
Original file line number Diff line number Diff line change
@@ -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;

2 changes: 2 additions & 0 deletions db/queries/check_resident.sql
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
-- Check if a person is a resident or not, used for login

SELECT COUNT(*) FROM renter_info where person_id = ?;
5 changes: 5 additions & 0 deletions db/queries/get_all_apt.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Used by NUMA to get more details about a certain apartment

SELECT *
FROM apartment
WHERE prop_id = ?;
4 changes: 4 additions & 0 deletions db/queries/get_all_prop.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Used by NUMA to see all properties

SELECT *
FROM property;
9 changes: 9 additions & 0 deletions db/queries/numa_overview.sql
Original file line number Diff line number Diff line change
@@ -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);
7 changes: 4 additions & 3 deletions db/queries/view_lease_to_pay.sql
Original file line number Diff line number Diff line change
@@ -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
(TO_CHAR(date_paid, 'YYYYMM') = TO_CHAR(CURRENT_TIMESTAMP, 'YYYYMM') or TO_CHAR(date_paid, 'YYYYMM') is null);
5 changes: 5 additions & 0 deletions db/updates/add_person.sql
Original file line number Diff line number Diff line change
@@ -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(?, ?, ?, ?, ?, ?);
5 changes: 5 additions & 0 deletions db/updates/change_preferred_payment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Used to update a renter's preferred payment method

UPDATE renter_info
SET preferred_payment = ?
WHERE person_id = ?;
5 changes: 5 additions & 0 deletions db/updates/modify_lease_length.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Used by NUMA to end prematurely or extend a person's lease

UPDATE lease
SET term_length = ?
WHERE id = ?;
4 changes: 4 additions & 0 deletions db/updates/register_visit.sql
Original file line number Diff line number Diff line change
@@ -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(?, ?, ?, ?);

0 comments on commit de9a153

Please sign in to comment.