-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rent fix #228
Conversation
WalkthroughThis update introduces comprehensive enhancements across the application, focusing on payment processing and order management. New functionalities include a method for full payment processing, updates to order views, and the integration of payment-related database procedures and triggers. These changes aim to streamline transactions and improve the user interface for payment handling. Changes
Poem
Recent Review DetailsConfiguration used: CodeRabbit UI Files ignored due to path filters (2)
Files selected for processing (13)
Files not reviewed due to errors (1)
Additional comments not posted (16)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 7
Out of diff range and nitpick comments (2)
database/wanderlust nir.sql (2)
6-6
: Ensure the generation time is updated dynamically if this script is auto-generated.
3946-3986
: The triggerAfterPaymentStatusCompleted
is well-implemented. Ensure that the cursor and loop operations are optimized for performance, especially if therent_pay
table grows significantly.
@@ -713,6 +740,7 @@ CREATE DEFINER=`root`@`%` PROCEDURE `ProcessCartToRentOrders` (IN `customerID` I | |||
) AND equipment.rentalservice_id = currentRentalServiceID; | |||
|
|||
|
|||
SET rentAmount = rentAmount * 0.20; -- 20% booking fee |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The calculation of the booking fee is hardcoded as 20%. Consider making this configurable through a parameter or a settings table to enhance flexibility.
public function fullPayRent(array $data) { | ||
|
||
$q = 'CALL CreatePaymentForRent(:rent_id)'; | ||
return $this->query($q, $data); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider implementing error handling and transaction management for the fullPayRent
method to ensure robustness in payment processing.
|
||
<tr> | ||
<td>Amount Due</td> | ||
<td> <b> <?= number_format($rent->total - $rent->paid_amount, 2) ?> </b></td> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the calculation of Amount Due
to the backend to maintain separation of concerns and ensure data integrity.
CREATE PROCEDURE `CreatePaymentForRent`( | ||
IN rent_id INT | ||
) | ||
BEGIN | ||
DECLARE lastPaymentID INT; | ||
DECLARE reference_number VARCHAR(10); | ||
DECLARE amount DECIMAL(10, 2); | ||
|
||
-- Fetching the outstanding amount for the given rent ID | ||
SELECT total - paid_amount INTO amount FROM rent WHERE id = rent_id; | ||
|
||
-- Insert a new payment entry | ||
INSERT INTO payment (amount, status) VALUES (amount, 'pending'); | ||
|
||
-- Get the ID of the last inserted payment | ||
SET lastPaymentID = LAST_INSERT_ID(); | ||
|
||
-- Create a reference number using the last payment ID | ||
SET reference_number = CONCAT('RNT', LPAD(lastPaymentID, 5, '0')); | ||
|
||
-- Update the payment with the reference number | ||
UPDATE payment SET reference_number = reference_number WHERE id = lastPaymentID; | ||
|
||
-- Insert into rent_pay table | ||
INSERT INTO rent_pay (rent_id, payment_id, amount) VALUES (rent_id, lastPaymentID, amount); | ||
|
||
-- Select the reference number and total amount for the output | ||
SELECT reference_number AS orderID, amount AS totalAmount; | ||
END $$ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure to start a transaction at the beginning of the procedure and commit it at the end to maintain data integrity across the inserts and updates.
public function fullPay(string $a = '', string $b = '', string $c = ''):void { | ||
AuthorizationMiddleware::authorize(['customer']); | ||
|
||
$rent = new RentModel; | ||
$data['rent'] = $rent->first(['id' => $a]); | ||
|
||
$this->view('customer/components/fullpay', $data); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add error handling for cases where rent data might not be found before rendering the view in the fullPay
method.
CREATE TRIGGER `AfterPaymentStatusCompleted` | ||
AFTER UPDATE ON `payment` | ||
FOR EACH ROW | ||
BEGIN | ||
-- Declare variables and cursor at the beginning of the block | ||
DECLARE total_paid DECIMAL(10,2); | ||
DECLARE done INT DEFAULT FALSE; | ||
DECLARE rent_id_var INT; | ||
DECLARE rent_cursor CURSOR FOR | ||
SELECT rent_id FROM rent_pay WHERE payment_id = NEW.id; | ||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; | ||
|
||
-- Check if the payment status has been updated to 'completed' | ||
IF NEW.status = 'completed' AND OLD.status <> 'completed' THEN | ||
|
||
-- Open the cursor | ||
OPEN rent_cursor; | ||
|
||
-- Loop through all rent IDs fetched by the cursor | ||
rent_loop: LOOP | ||
FETCH rent_cursor INTO rent_id_var; | ||
IF done THEN | ||
LEAVE rent_loop; | ||
END IF; | ||
|
||
-- Calculate the total paid amount for the specific rent | ||
SELECT SUM(rp.amount) INTO total_paid | ||
FROM rent_pay rp | ||
JOIN payment p ON rp.payment_id = p.id | ||
WHERE rp.rent_id = rent_id_var AND p.status = 'completed'; | ||
|
||
-- Update the paid_amount in the rent table for each affected rent | ||
UPDATE rent | ||
SET paid_amount = total_paid | ||
WHERE id = rent_id_var; | ||
END LOOP; | ||
|
||
-- Close the cursor | ||
CLOSE rent_cursor; | ||
END IF; | ||
END$$ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimize the SQL queries within the trigger to reduce the potential performance overhead from using a cursor and multiple updates.
public function fullPay(string $a = '', string $b = '', string $c = ''):void { | ||
|
||
|
||
AuthorizationMiddleware::authorize(['customer']); | ||
|
||
$response = new JSONResponse(); | ||
|
||
$rent = new RentModel; | ||
$payment = new PaymentModel; | ||
$data['rent'] = $rent->first(['id' => $a]); | ||
|
||
$data['payment'] = $payment ->fullPayRent(['rent_id' => $a])[0]; | ||
|
||
$merchant_id = MERCHANT_ID; | ||
$merchant_secret = MERCHANT_SECRET; | ||
// $amount = $data['rent']->total - $data['rent']->paid_amount; | ||
$amount = number_format($data['rent']->total - $data['rent']->paid_amount, 2, '.', ''); | ||
|
||
$hash = strtoupper( | ||
md5( | ||
$merchant_id . | ||
$data['payment']->orderID . | ||
// number_format($order->totalAmount, 2, '.', '') . | ||
$amount . | ||
'LKR' . | ||
strtoupper(md5($merchant_secret)) | ||
) | ||
); | ||
|
||
$data['hash'] = $hash; | ||
$data['merchant_id'] = $merchant_id; | ||
$data['orderId'] = $data['payment']->orderID; | ||
$data['amount'] = $amount; | ||
|
||
$response->data($data)->statusCode(200)->send(); | ||
|
||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clean up commented-out code and ensure all necessary error checks are in place in the fullPay
method to maintain code clarity and robustness.
Summary by CodeRabbit
New Features
Enhancements
Documentation