如何在PHP中显示每月帐单

问题描述 投票:-1回答:1

我没有从stackoverflow上找到任何东西,所以我自己去尝试了,这就是我想出的,使用cookie来测试数据库是否已付款。

php sql payment
1个回答
0
投票
<?php
    $payment_date = date("Y-m-01 00:00:00");

    function endKey($array){
        end($array);
        return key($array);
    }

    $payments_due = array(
                 "2019-11-01 00:00:00",
                 "2019-12-01 00:00:00",
                 "2020-01-01 00:00:00",
                 "2020-02-01 00:00:00",
                 "2020-03-01 00:00:00",
                 "2020-04-01 00:00:00",
                 "2020-05-01 00:00:00",
                 "2020-06-01 00:00:00",
                 "2020-07-01 00:00:00",
                 "2020-08-01 00:00:00",
                 "2020-09-01 00:00:00",
                 "2020-10-01 00:00:00",
                 "2020-11-01 00:00:00",
                 "2020-12-01 00:00:00");     

    // If payment_date and not paid, unset cookie            
    for ($j = 0; $j < sizeof($payments_due); $j++) {

        if ($j < endKey($payments_due)) {
            $k = $j + 1;
        }

        // If payment_date is between current month and next month's payment
        if ((strtotime($payment_date) >= strtotime($payments_due[$j])) && (strtotime($payment_date) < strtotime($payments_due[$k]))) {

            // echo "Payment date: " . $payment_date . "<br />is >=<br />";
            // echo "Payment due: " . $payments_due[$j] . "<br />is <<br />";
            // echo "Payment due: " . $payments_due[$k] . "<br />";         

        // Check if payment due already in DB
            $query = "SELECT *
                      FROM payments
                      WHERE farm_id = " . sanitise($_SESSION['farm_id']) . "
                      AND payment_due = '{$payment_date}'";

            $data_set = mysqli_query($connection, $query);
            confirm_query2($data_set, $query);  


        // If payment due not in DB, insert it
            if (mysqli_num_rows($data_set) == 0) {              
                $query = "INSERT INTO payments (
                                farm_id, bill, payment_due
                            ) VALUES ( 
                                " .  sanitise($_SESSION['farm_id']) . ", {$monthly_bill}, '{$payment_date}'
                            )";

                $data_set = mysqli_query($connection, $query);
                confirm_query2($data_set, $query);

                $first_name = $_SESSION['first_name'];

                mail($email, "BuyRawMilk - Monthly Bill Due", 
                             "Hello {$first_name},
                             <br /><br />Your monthly bill totalling " . sprintf("%0.2f", $monthly_bill) . " is due.
                             <br /><br />Please click here https://buyrawmilk.co.uk/orders.php to pay your bill.",
                             "[email protected]", "-f [email protected]");
            }


        // Get payments due
            $query = "SELECT p.paid
                      FROM users u
                      LEFT JOIN settings s ON s.user_id = u.id
                      LEFT JOIN farms f ON s.farm_id = f.id
                      LEFT JOIN payments p ON s.farm_id = f.id
                      WHERE farmer = 1
                      AND payment_due = '{$payment_date}'
                      AND p.paid IS NULL
                      AND f.id = " . sanitise($_SESSION['farm_id']) . "
                      AND bill != 0
                      LIMIT 1";

            $data_set = mysqli_query($connection, $query);
            confirm_query2($data_set, $query);

            $row = mysqli_fetch_array($data_set);
            $paid = $row['paid'];

            if(mysqli_num_rows($data_set) > 0) {

            // If not paid bill
                if ($paid == NULL) {
                // Unset cookie
                    setcookie("payments", "payment_paid", 1);
            // If paid bill
                } else if ($paid != NULL) {             
                    if (!isset($_COOKIE['payments'])) {     
                    // Set cookie
                        setcookie("payments", "payment_paid", time() + (60*60*24)*3650);
                    }
                }
            } else {
            // No payments due; unset cookie
                setcookie("payments", "payment_paid", time() + (60*60*24)*3650);
            }
        }
    }
?>
<?php if (!isset($_COOKIE['payments'])) { ?>
<br /><strong>Monthly bill due</strong>
<br />

<?php
    $query = "SELECT p.bill, p.payment_due, p.paid
              FROM users u
              LEFT JOIN settings s ON s.user_id = u.id
              LEFT JOIN farms f ON s.farm_id = f.id
              LEFT JOIN payments p ON s.farm_id = f.id
              WHERE farmer = 1
              AND p.paid IS NULL
              AND f.id = " . sanitise($_SESSION['farm_id']) . "
              AND bill != 0";

    $data_set = mysqli_query($connection, $query);
    confirm_query2($data_set, $query);
?>

<table style='border:1px solid black;background-color:white;margin-top:10px;'>
    <!-- *** Table: payments *** -->
    <th style='text-align:center;background-color:silver;padding:5px;'>Payment due</th>
    <th style='text-align:center;background-color:silver;padding:5px;'>Bill</th>
    <th style='text-align:center;background-color:silver;padding:5px;'>Pay</th>
    <?php while ($row = mysqli_fetch_array($data_set)) { ?>
    <tr>
        <!-- Payment due -->
        <td style='padding:5px;background-color:#EEEEEE;'>
            <?php $payment_due = $row['payment_due']; ?>
            <?php if (date('Y-m-d', strtotime($payment_due)) == date('Y-m-d')) { echo "Today<br />"; } else { echo date('d/m/y', strtotime($payment_due)); } echo "</span>"; ?>
        </td>
        <!-- Bill -->
        <td style='padding:5px;background-color:#EEEEEE;'>
            <?php echo "&pound;" . sprintf("%0.2f", $row['bill']); ?>
        </td>
        <!-- Pay -->
        <td style='padding:5px;background-color:#EEEEEE;'>
            <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
                <input type="hidden" name="cmd" value="_xclick">
                <input type="hidden" name="business" value="[email protected]">
                <input type="hidden" name="lc" value="GB">
                <input type="hidden" name="item_name" value="Payments">
                <input type="hidden" name="amount" value="<?php echo $row['bill']; ?>">
                <input type="hidden" name="currency_code" value="GBP">
                <input type="hidden" name="button_subtype" value="services">
                <input type="hidden" name="no_note" value="0">
                <input type="hidden" name="rm" value="1">
                <input type="hidden" name="return" value="../../../localhost/BuyRawMilk/check_paid.php">
                <input type="hidden" name="cancel_return" value="http://buyrawmilk.co.uk/orders.php">
                <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
                <input type="image" src="https://www.paypalobjects.com/en_GB/i/btn/btn_paynow_SM.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online!">
                <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
            </form>
        </td>               
    </tr>           
    <?php } ?>  
    <?php if(mysqli_num_rows($data_set) == 0) { ?>
    <tr>
        <td colspan="5" style='text-align:center;padding:5px;background-color:#EEEEEE;'>
            No items to display.
        </td>       
    </tr>                   
    <?php } ?>
</table>

<?php } ?>
© www.soinside.com 2019 - 2024. All rights reserved.