Jump to content

Automated Invoice Management System: Dynamic Updates and Status Handling for Payments and Overpayments


Go to solution Solved by Olumide,

Recommended Posts

I have an invoice table whereby if payment is made or recorded, it should update the table by setting the tendered_amount, if the tendered_amount is greater than the total_amount, the result/value should be store in change_balance (e.g. 500 - 300 = 200) and if the tendered_amount is lower than the total_amount, that is debts because the amount expected is not equal to the tendered amount, but if the tendered amount is equal to the total amount, the user has fully paid and the status should change to 'paid', also, if the tendered amount is greater than the total amount, the status will also change to paid because he overpaid. 

 

 

// Calculate change_balance, debts, and update status
            $change_balance = max(0, $amount_paid - $invoice_data['total_amount']);
            $debts = max(0, $invoice_data['total_amount'] - $amount_paid);
            $status = ($debts == 0) ? 'paid' : 'unpaid';

            // Update the tendered_amount, change_balance, debts, and status columns
            $update_query = "UPDATE `invoices` 
                             SET `tendered_amount` = '{$amount_paid}', 
                                 `change_balance` = '{$change_balance}', 
                                 `debts` = '{$debts}', 
                                 `status` = '{$status}'
                             WHERE `invoice_code` = '{$invoice_code}'";

 

you would not UPDATE amounts in a table to track this data, since that doesn't provide an accounting trail should a programming mistake, duplicate entry, or nafarious activity alter a value. these are derived amounts that you would calculate when needed. you would have an invoice_accounting (or similar named) table, that gets a row inserted for every amount that affects an invoice amount. you would query when needed (a UNION between the invoice(s) table and the invoice accounting table) to determine the current total +/-/0 for any invoice.

Edited by mac_gyver
  • Great Answer 1
  • Solution
On 12/19/2023 at 10:54 PM, mac_gyver said:

you would not UPDATE amounts in a table to track this data, since that doesn't provide an accounting trail should a programming mistake, duplicate entry, or nafarious activity alter a value. these are derived amounts that you would calculate when needed. you would have an invoice_accounting (or similar named) table, that gets a row inserted for every amount that affects an invoice amount. you would query when needed (a UNION between the invoice(s) table and the invoice accounting table) to determine the current total +/-/0 for any invoice.

 

Thank you. I will work on that and update you.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.