<?php
// array for JSON response
  $response= array();

    // include db connect class
    require_once __DIR__ . '/../DBConnect.php';

    if (isset($_POST['CounteryClientID'])){
      $CounteryClientID = $_POST['CounteryClientID'];
      $MobileNo         = $_POST['MobileNo'];
      $CB_Date           = $_POST['CBDate'];
      $CBTime           = $_POST['CBTime'];
      $approval         = $_POST['approval']; 
      $AcNameID         = $_POST['AcNameID']; 
      $NameOfPerson     = $_POST['NameOfPerson']; 
      $CounteryUserID   = $_POST['CounteryUserID'];
      $CBRemarks        = $_POST['CBRemarks'];
      $Amount           = $_POST['Amount'];
      $NetCode          = $_POST['NetCode'];
      $SysCode          = $_POST['SysCode'];   
      
      $CBDate = date("Y-m-d", strtotime($CB_Date));
            
        if($approval != 0){
            echo "ssss";
             $parm=array();
            $query="select max(CashBookID) as maxid from CashBook where ClientID=$CounteryClientID";
            
            try {
                $stmt = $conn->prepare($query);
                $stmt->execute();
                $users = $stmt->fetchAll();
                $CashBookID = 0;
                foreach ($users as $user) {
                    $CashBookID = $user['maxid'] + 1;
                }
            }catch (PDOException $e) {
                echo $sql . "<br>" . $e->getMessage();
            }
            $UpdatedDate = date("Y-m-d H:i:s");
            $sql = "INSERT INTO CashBook 
            (CashBookID,CBDate,CBTime,
            DebitAccount, CreditAccount,
            CBRemarks, Amount,
            ClientID, ClientUserID,
            NetCode, SysCode,
            UpdatedDate, TableID,
            SerialNo, TableName)
            VALUES (
                '$CashBookID',
                '$CBDate',
                '$CBTime',
                '$AcNameID',
                '$CounteryUserID',
                '$CBRemarks',
                '$Amount',
                '$CounteryClientID',
                '$CounteryUserID',
                '$NetCode',
                '$SysCode',
                '$UpdatedDate',
                '0',  /**TableID */
                '0',  /**SerialNo*/
                '');";/**TableName */
    
            try {
                $result= $conn->exec($sql);		   
                    if( $result === false ) {
                    /////////////not inserted
                        $response["success"] = 0;  
                        $response["message"] = "User Not Register";
                    echo json_encode($response);
                    }else{
                        /////////////inserted
                    $response["success"] = 1;
                    $response["CashBookID"] = $CashBookID;
                    $response["UpdatedDate"] = $UpdatedDate;
                    $response["message"] = "User Register";
                    echo json_encode($response);
                    }
                }
            catch (PDOException $e) {
                echo $sql . "<br>" . $e->getMessage();
            }
        }else{
            $sql = "SELECT AcNameID,NameOfPerson from Account3Name where ClientID = $CounteryClientID and AcMobileNo = '$MobileNo'";
            $stmt = $conn->prepare($sql);
            $stmt->execute();
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
            if($result!=null &&  $result>0){
                $response["Account3Name"] = array();
                foreach($result as $row) {       
                    $Account3Name["AcNameID"]     = $row["AcNameID"];
                    $Account3Name["NameOfPerson"] = $row["NameOfPerson"];

                    // push single product into final response array
                    array_push($response["Account3Name"], $Account3Name);
                }
                // success
                    $response["success"] = 1;
                    $response["message"] = "This Mobile No is registerd";
                // echoing JSON response
                    echo json_encode($response);
            }else {
                $response["success"] = 0;
                $response["message"] = "This Mobile No is not registerd";
                // echoing JSON response
                echo json_encode($response);
            }
            ///////////////////////////////////////////
        }
        
    }else {
        $response["success"] = 0;
        $response["message"] = "Set ClientID First ";
        // echoing JSON response
        echo json_encode($response);
    }
?>
