<?php
    include_once 'opendb.php';
   //  clear_old_records(52, $conn);
  $filter="";
    if (isset($_POST["filter"]))
    {
         $filter=" and " . $_POST["filter"];
    
    }
    if (isset($_POST["comp_id"]))
    {
        $CompID=$_POST["comp_id"]; 
         $filter.=" and comp_id=" . $CompID;
    }
    else {
        $response["message"] = "|No co id|";
        die( json_encode($response));
    }
    if (isset($_POST["counter_code"]))
    {
        $Countercode=$_POST["counter_code"]; 
         $filter.=" and counter_code='" . $Countercode."'";
    }
    else {
        $response["message"] = "|No counteroode|";
        die( json_encode($response));
    }
      
    $Qry="select m.entry_no as entry_no, entry_date,notes,amount,tax_amt,voucher_no,counter_code,sync_time,ref_no,edit_time,webentry_no,prod_code,sl_no,quantity,rate,unit,pack_qty,foc_qty,foc_unit,tax_rate from stock_issue m left  join  stock_issue_sub  s on m.entry_no=s.entry_no ";
    $Qry.=" where exported=0 and pc_edited=0 ".$filter . " order by m.Entry_No ";  
   // echo $Qry;
    $result = $conn->query($Qry) or die(mysqli_error($conn));
    if ($result->num_rows > 0)
    {
            $response = array();
            while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
            {
                // temp user array
                $data = array();
                $data["entry_no"] = $row["entry_no"];
                $data["webentry_no"] = $row["webentry_no"];
                $Edate=date_create($row["entry_date"]);
                $data["entry_date"] =date_format( $Edate,"Ymd");
                $data["notes"] = $row["notes"];
                $data["amount"] = $row["amount"];                
                $data["tax_amt"] = $row["tax_amt"];                
                $data["voucher_no"] = $row["voucher_no"];
                $data["counter_code"] = $row["counter_code"]; 
                $data["ref_no"] = $row["ref_no"]; 
                $data["edit_time"] = $row["edit_time"]; 
                $data["prod_code"] = $row["prod_code"];
                $data["sl_no"] = $row["sl_no"];
                $data["quantity"] = $row["quantity"];
                $data["rate"] = $row["rate"];
                $data["unit"] = $row["unit"];               
                $data["pack_qty"] = $row["pack_qty"];              
                $data["foc_qty"] = $row["foc_qty"];
                $data["foc_unit"] = $row["foc_unit"];
                $data["tax_rate"] = $row["tax_rate"];
               
                array_push($response, $data);
            }
            echo json_encode($response);
     } else
     {   
       $response["message"] = "|No Records|";
       echo json_encode($response);
  //     clear_old_records($CompID, $conn);
     }
 $conn->close();
 
 function clear_old_records($CompID, $conn){
    $Set_Name="STI_CLRDATE";
    $isClear=1;
    $sql = "SELECT * FROM appsettings WHERE set_category='GENERAL' and set_name='".$Set_Name."' and comp_id=" .$CompID;
    $result = $conn->query($sql);
    if ($result->num_rows == 0 ) {
           $sql = "INSERT INTO appsettings (set_category,set_name,set_value,val_type,comp_id) VALUES ('GENERAL','".$Set_Name."','','S',".$CompID.")";
           if ($conn->query($sql) === TRUE)
           {} else {
              die( "Error: Inerting settings " . $sql . "<br>" . $conn->error);
           } 
    } else if ($nrow=mysqli_fetch_assoc($result)) {
        if ($nrow['set_value']==date('Y-m-d')){
            $isClear=0;        
        }
    }
    
    if ($isClear==1){
        $cutDate=date('Y-m-d',strtotime('-30 day'));
        $sql="select entry_no from stock_issue where exported=1 and Entry_date < '$cutDate' and comp_id=$CompID";
        $mresult = $conn->query($sql);
        while ($row = mysqli_fetch_assoc($mresult)){
             $mENo=$row['entry_no'];
             $mqry="delete from stock_issue_sub where entry_no=$mENo";
             $conn->query($mqry);
             $mqry="delete from stock_issue where entry_no=$mENo";
             $conn->query($mqry);
        }
        mysqli_free_result($mresult);
        $sql = "update appsettings set set_value='". date('Y-m-d') ."'  WHERE set_category='GENERAL' and set_name='".$Set_Name."' and comp_id=" .$CompID;
        $conn->query($sql);
    }
    
        
}
 
 
 
?>