<?php
ob_start();

/***************************************************************************************************
 PROGRAM DESCRIPTION :  PROGRAM TO VIEW FORPERIOD REPORTS  PROCESS PAGE
***************************************************************************************************/

include_once '../includes/constants.php';
include_once '../includes/functions.php';
include_once '../includes/session.php';
include_once "paymentsforsel.php";
include_once '../includes/allstripslashes.php';

$partners = new partners;
$mysqli = new mysqli($host, $user, $pass, $db);
$partners->connection($host, $user, $pass, $db);

/********************* Variables *****************************/
$Merchant   = trim($_POST['Mname'] ?? '');
$Affiliate  = trim($_POST['Aname'] ?? '');
$cfrom      = trim($_POST['txtfrom'] ?? '');
$cto        = trim($_POST['txtto'] ?? '');
$sub        = trim($_POST['sub'] ?? '');

/********************* Date Validation ***********************/
if (!$partners->is_date($cfrom) || !$partners->is_date($cto)) {
    $Err = "Please Enter Valid Date";
    header("Location: index.php?Act=forperiod&err=$Err");
    exit;
}

$From = $partners->date2mysql($cfrom);
$To   = $partners->date2mysql($cto);

/********************* SQL Logic Based on Filters ************/
$impSql = "";
$rawClick = 0;
$rawImp   = 0;

switch ($Merchant) {
    case 'All':
        switch ($Affiliate) {
            case 'All':
                $sql = "SELECT * FROM partners_joinpgm";
                $impSql = "SELECT SUM(imp_count) AS impr_count FROM partners_impression_daily WHERE 1";
                $rawClick = GetRawTrans('click', 0, 0, 0, 0, $From, $To, 0);
                $rawImp = GetRawTrans('impression', 0, 0, 0, 0, $From, $To, 0);
                break;
            default:
                $sql = "SELECT * FROM partners_joinpgm WHERE joinpgm_affiliateid = '$Affiliate'";
                $impSql = "SELECT SUM(imp_count) AS impr_count FROM partners_impression_daily WHERE imp_affiliateid = '$Affiliate'";
                $rawClick = GetRawTrans('click', 0, $Affiliate, 0, 0, $From, $To, 0);
                $rawImp = GetRawTrans('impression', 0, $Affiliate, 0, 0, $From, $To, 0);
                break;
        }
        break;

    default:
        switch ($Affiliate) {
            case 'All':
                $sql = "SELECT * FROM partners_joinpgm j
                        JOIN partners_program p ON j.joinpgm_programid = p.program_id
                        WHERE program_merchantid = '$Merchant'";
                $impSql = "SELECT SUM(imp_count) AS impr_count FROM partners_impression_daily WHERE imp_merchantid = '$Merchant'";
                $rawClick = GetRawTrans('click', $Merchant, 0, 0, 0, $From, $To, 0);
                $rawImp = GetRawTrans('impression', $Merchant, 0, 0, 0, $From, $To, 0);
                break;

            default:
                $sql = "SELECT * FROM partners_joinpgm j
                        JOIN partners_program p ON j.joinpgm_programid = p.program_id
                        WHERE program_merchantid = '$Merchant' AND joinpgm_affiliateid = '$Affiliate'";
                $impSql = "SELECT SUM(imp_count) AS impr_count FROM partners_impression_daily 
                           WHERE imp_merchantid = '$Merchant' AND imp_affiliateid = '$Affiliate'";
                $rawClick = GetRawTrans('click', $Merchant, $Affiliate, 0, 0, $From, $To, 0);
                $rawImp = GetRawTrans('impression', $Merchant, $Affiliate, 0, 0, $From, $To, 0);
                break;
        }
}

$impSql .= " AND imp_date BETWEEN '$From' AND '$To'";

$impRes = $mysqli->query($impSql);
$numRec = 0;
if ($impRes && $row_impr = $impRes->fetch_object()) {
    $numRec = $row_impr->impr_count ?? 0;
}

/********************* Get Main Result Set *******************/
$result1 = $mysqli->query($sql);
$total = GetPaymentDetails($sql, $To, $From);

/********************* Initialize Counters *******************/
$click      = 0;
$lead       = 0;
$sale       = 0;
$nclick     = 0;
$nlead      = 0;
$nsale      = 0;
$impression = 0;

if ($result1) {
    while ($rows = $result1->fetch_object()) {
        $joinid = $rows->joinpgm_id;

        // Clicks
        $sql_click = "SELECT * FROM partners_transaction 
                      WHERE transaction_dateoftransaction BETWEEN '$From' AND '$To'
                      AND transaction_type = 'click' AND transaction_joinpgmid = '$joinid'";
        $res_click = $mysqli->query($sql_click);
        $nclick += $res_click->num_rows;

        while ($row = $res_click->fetch_object()) {
            $click += $row->transaction_amttobepaid + $row->transaction_admin_amount;
        }

        // Leads
        $sql_lead = "SELECT * FROM partners_transaction 
                     WHERE transaction_dateoftransaction BETWEEN '$From' AND '$To'
                     AND transaction_type = 'lead' AND transaction_joinpgmid = '$joinid'";
        $res_lead = $mysqli->query($sql_lead);
        $nlead += $res_lead->num_rows;

        while ($row = $res_lead->fetch_object()) {
            $lead += $row->transaction_amttobepaid + $row->transaction_admin_amount;
        }

        // Sales
        $sql_sale = "SELECT * FROM partners_transaction 
                     WHERE transaction_dateoftransaction BETWEEN '$From' AND '$To'
                     AND transaction_type = 'sale' AND transaction_joinpgmid = '$joinid'";
        $res_sale = $mysqli->query($sql_sale);
        $nsale += $res_sale->num_rows;

        while ($row = $res_sale->fetch_object()) {
            $transactionId = $row->transaction_id;
            $recur = $row->transaction_recur;

            if ($recur == '1') {
                $sql_Recur = "SELECT * FROM partners_recur WHERE recur_transactionid = '$transactionId'";
                $res_Recur = $mysqli->query($sql_Recur);

                if ($res_Recur && $res_Recur->num_rows > 0) {
                    $row_recur = $res_Recur->fetch_object();
                    $recurId = $row_recur->recur_id;

                    $sql_recurpay = "SELECT * FROM partners_recurpayments WHERE recurpayments_recurid = '$recurId'";
                    $res_recurpay = $mysqli->query($sql_recurpay);

                    if ($res_recurpay && $res_recurpay->num_rows > 0) {
                        $row_recurpay = $res_recurpay->fetch_object();
                        $sale += $row_recurpay->recurpayments_amount;
                    }
                }
            } else {
                $sale += $row->transaction_amttobepaid;
            }

            $sale += $row->transaction_admin_amount;
        }

        // Impressions
        $sql_imp = "SELECT * FROM partners_transaction 
                    WHERE transaction_dateoftransaction BETWEEN '$From' AND '$To'
                    AND transaction_type = 'impression' AND transaction_joinpgmid = '$joinid'";
        $res_imp = $mysqli->query($sql_imp);

        while ($row = $res_imp->fetch_object()) {
            $impression += $row->transaction_amttobepaid + $row->transaction_admin_amount;
        }
    }
}

/********************* Final Output ***************************/
$sale_string = "$sale~$subsale~$nsubsale~$numRec~$rawClick~$rawImp";

header("Location: index.php?Act=forperiod"
    . "&click=$click"
    . "&nclick=$nclick"
    . "&lead=$lead"
    . "&nlead=$nlead"
    . "&sale=$sale_string"
    . "&nsale=$nsale"
    . "&merchant=$Merchant"
    . "&affiliate=$Affiliate"
    . "&from=$cfrom"
    . "&to=$cto"
    . "&sub=$sub"
    . "&total=$total"
    . "&impression=$impression"
);
exit;
?>
