<?php
require_once('../auth/auth.php');

class balance
{
    // Db Property
    private $db;
    private $smsdb;
    private $coid;
    private $userid;
    private $datetime;
    
    // Db __construct Method
    public function __construct($db, $smsdb, $coid, $userid, $datetime)
    {
        $this->db       = $db;
        $this->smsdb    = $smsdb;
        $this->coid     = $coid;
        $this->userid   = $userid;
        $this->datetime = $datetime;
    }
    
    // Check Exist TNX ID Method
    public function checkExistTnx($tnx)
    {
        $tnxsql = mysqli_query($this->db, "SELECT adid FROM add_balance WHERE tnxid = '$tnx'");
        if (mysqli_num_rows($tnxsql) > 0) {
            return true;
        } else {
            return false;
        }
    }
    
    //// Select Company Balance,expiry_date ////
    public function companydata()
    {
        $cmsql = mysqli_query($this->db, "SELECT balance,expiry_date FROM company WHERE coid = '$this->coid'");
        return mysqli_fetch_assoc($cmsql);
    }
    
    // Auto Add Method //
    public function autocheck($method, $tnxid, $s_gsm, $s_amount)
    {
        if ($method == 'bKash') {
            $atchk = mysqli_query($this->smsdb, "SELECT ID FROM Message WHERE SUBSTRING(`message`, POSITION('TrxID ' IN `message`)+6,10)='$tnxid' AND SUBSTRING(`message`, POSITION('from ' IN `message`)+5,11)='$s_gsm' AND SUBSTRING(REPLACE(`message`,',',''), POSITION('received Tk ' IN REPLACE(`message`,',',''))+12,LENGTH('$s_amount'))='$s_amount' AND `number`='$method' AND `status`='Received' AND userID=5 AND deviceID=10 LIMIT 1");
        } else if ($method == 'NAGAD') {
            $atchk = mysqli_query($this->smsdb, "SELECT ID FROM Message WHERE SUBSTRING(`message`, POSITION('TxnID: ' IN `message`)+7,8)='$tnxid' AND SUBSTRING(`message`, POSITION('Sender: ' IN `message`)+8,11)='$s_gsm' AND SUBSTRING(`message`, POSITION('Amount: Tk ' IN `message`)+11,LENGTH('$s_amount'))='$s_amount' AND `number`='$method' AND `status`='Received' AND userID=5 AND deviceID=10 LIMIT 1");
        } else if ($method == '16216') {
            $atchk = mysqli_query($this->smsdb, "SELECT ID FROM Message WHERE SUBSTRING(`message`, POSITION('TxnId:' IN `message`)+6,10)='$tnxid' AND SUBSTRING(`message`, POSITION('from A/C:' IN `message`)+9,11)='$s_gsm' AND SUBSTRING(`message`, POSITION('Tk' IN `message`)+2,LENGTH('$s_amount'))='$s_amount' AND `number`='$method' AND `status`='Received' AND userID=5 AND deviceID=10 LIMIT 1");
        }
        if (mysqli_num_rows($atchk) > 0) {
            return true;
        } else {
            return false;
        }
    }
    // Insert Method
    public function insertrequest($data)
    {
        $pay_method = $data['pay_method'];
        $send_gsm   = $data['send_gsm'];
        $tnxid      = $data['tnxid'];
        $amount     = $data['amount'];
        $checktnx   = $this->checkExistTnx($tnxid);
        if ($checktnx == TRUE) {
            return 101;
        } else {
            $checksms = $this->autocheck($pay_method, $tnxid, $send_gsm, $amount);
            mysqli_query($this->db, "INSERT INTO `add_balance`(`coid`, `pay_method`, `send_gsm`, `tnxid`, `amount`, `plus`, `details`, `status`, `date`, `user`) VALUES ('$this->coid','$pay_method','$send_gsm','$tnxid','$amount','+','Balance Recharge','$checksms','$this->datetime','$this->userid')") or die(mysqli_error($this->db));
            
            if ($checksms == TRUE) {
                $cdrow                       = $this->companydata();
                $_SESSION['USER']['balance'] = $cdrow['balance'];
            }
        }
    }
    
    // Insert Method
    public function actionrequest($udata)
    {
        $adid   = $udata['adid'];
        $action = $udata['action'];
        if ($action == 1 || $action == 0) {
            mysqli_query($this->db, "UPDATE `add_balance` SET `status`='$action' WHERE `adid`='$adid'") or die(mysqli_error($this->db));
            $cdrow                       = $this->companydata();
            $_SESSION['USER']['balance'] = $cdrow['balance'];
            return 1;
        } else if ($action == 2) {
            mysqli_query($this->db, "DELETE FROM `add_balance` WHERE `adid`='$adid'") or die(mysqli_error($this->db));
            $cdrow                       = $this->companydata();
            $_SESSION['USER']['balance'] = $cdrow['balance'];
            return 2;
        }
    }
    
    // Insert Method
    public function packrenew($pdata)
    {
        $invoice      = $pdata['invoice'];
        $pack_id      = $pdata['pack_id'];
        $total_charge = $pdata['total_charge'];
        $package_duration = $pdata['package_duration'];
        $checkinv     = $this->checkExistTnx($invoice);
        if ($checkinv == TRUE && $_SESSION['USER']['balance'] < $total_charge) {
            return 101;
        } else {
            mysqli_query($this->db, "INSERT INTO `add_balance`(`coid`, `pay_method`, `tnxid`, `amount`, `plus`, `details`, `status`, `date`, `user`) VALUES ('$this->coid',(SELECT package_name FROM package WHERE pack_id='$pack_id'),'$invoice','$total_charge','-','Package Renew',1,'$this->datetime','$this->userid')") or die(mysqli_error($this->db));
            
            $cdrow                           = $this->companydata();
            $_SESSION['USER']['balance']     = $cdrow['balance'];
            $_SESSION['USER']['expiry_date'] = date("Y-m-d", strtotime("+$package_duration month", strtotime($cdrow['expiry_date'])));
            $new_expiry_date                 = $_SESSION['USER']['expiry_date'];

            mysqli_query($this->db, "UPDATE `company` SET `expiry_date`='$new_expiry_date' WHERE `coid`='$this->coid'") or die(mysqli_error($this->db));
        }
    }
    
}

$balance = new balance($conn, $smsdb, $coid, $uid, $datetime);

if (isset($_POST['tnxid'])) {
    echo $balance->insertrequest($_POST);
}

if (isset($_POST['adid'])) {
    echo $balance->actionrequest($_POST);
}

if (isset($_POST['invoice'])) {
    echo $balance->packrenew($_POST);
}
?>