<?php
    include_once 'opendb.php';
     $mess="";
   // die(isset($_POST["Category"]) );
    
    if (isset($_POST["AccessCode"]))
    {
    
       $Access=$_POST["AccessCode"]=="Manacc Delete All";
       $CompID=$_POST["comp_id"]; 
       if($Access && isset($_POST["Category"]) )
       {
       
          if($_POST["CutofDate"])
          {
             
             $CutofDate=$_POST["CutofDate"];
            
            if($_POST["Category"]=="Sale Order")
             {
            
            
                      $sql = "delete from sale_order where Entry_date<'".$CutofDate."' and IS_SYNCHED=1  and comp_id=" .$CompID;
                           if ($conn->query($sql) === TRUE)
                           {
                              $mess="Order Master cleared,";
                           } else
                           {
                              $mess= "Error: " . $sql . "<br>" . $conn->error;
                           } 
                            $sql = "delete from sale_order_sub where  compid=" .$CompID." and entry_no not in (select Entry_no from sale_order where comp_id=" .$CompID.")";
                           if ($conn->query($sql) === TRUE)
                           {
                              $mess.=" and details too";
                           } else
                           {
                              $mess= "Error: " . $sql . "<br>" . $conn->error;
                           } 
             }
             else if($_POST["Category"]=="Sales")
             {
            
            
                      $sql = "delete from sales where Entry_date<'".$CutofDate."' and IS_SYNCHED=1  and comp_id=" .$CompID;
                           if ($conn->query($sql) === TRUE)
                           {
                              $mess="Sales Master cleared";
                           } else
                           {
                              $mess= "Error: " . $sql . "<br>" . $conn->error;
                           } 
                            $sql = "delete from sales_sub where  compid=" .$CompID." and entry_no not in (select Entry_no from sales where comp_id=" .$CompID.  ")";
                           if ($conn->query($sql) === TRUE)
                           {
                              $mess.=" and details too";
                           } else
                           {
                              $mess= "Error: " . $sql . "<br>" . $conn->error;
                           } 
             }
             else if($_POST["Category"]=="Requests")
             {
                   $sql = "delete from client_requests where request_time<'".$CutofDate."' and req_status>0  and comp_id=" .$CompID;
                           if ($conn->query($sql) === TRUE)
                           {
                              $mess="Requests cleared";
                           } else
                           {
                              $mess= "Error: " . $sql . "<br>" . $conn->error;
                           } 
             }
           else if($_POST["Category"]=="Receipts")
             {
                   $sql = "delete from receipts where trans_date <'".$CutofDate."' and IS_SYNCHED=1  and comp_id=" .$CompID;
                           if ($conn->query($sql) === TRUE)
                           {
                              $mess="Receipts cleared";
                           } else
                           {
                              $mess= "Error: " . $sql . "<br>" . $conn->error;
                           } 
             }
             
          }
      }  
      else {
              $mess= "Error : IAC";
      }
        
    }
    else
    {
        $mess= "Error : No parameters!";
    }
    echo $mess;
    $conn->close();
?>