<?php

//Daily Auto Task Each 07:00:00 AM
// /usr/local/bin/php /home/evosyspos/public_html/app.evosyspos.com/models/AutoTaskUpdateCustomerCashBackStatus.php
// curl -s https://app.evosyspos.com/models/AutoTaskUpdateCustomerCashBackStatus.php

require_once "Operations.php";

date_default_timezone_set('America/New_York');

// GET CURRENT DATE
$currentDate = date('Y-m-d');

$posData = getAllPosWithCashBack();

foreach ($posData as $key => $posValue) {

    $availability_days = $posValue['pos_cash_back_availability_days'];
    $expiration_days = $posValue['pos_cash_back_expiration_days'];

    // UPDATE CASH BACk TO AVAILABLE
    $sqlSetCashBackAvailable = "UPDATE tb_payments_details SET pay_det_cashback_status = 'Available' WHERE pay_det_cashback_status = 'Pending' AND DATE_FORMAT(DATE_ADD(pay_det_date, INTERVAL {$availability_days} DAY), '%Y-%m-%d') <= '{$currentDate}'";
    executeQueryPDO($sqlSetCashBackAvailable, "Success", "Error");

    // UPDATE CASH BACk TO EXPIRED
    $sqlSetCashBackExpired = "UPDATE tb_payments_details SET pay_det_cashback_status = 'Expired' WHERE pay_det_cashback_status IN ('Available','Pending') AND DATE_FORMAT(DATE_ADD(pay_det_date, INTERVAL {$expiration_days} DAY), '%Y-%m-%d') <= '{$currentDate}'";
    executeQueryPDO($sqlSetCashBackExpired, "Success", "Error");
}
