<?php
 
if (
    isset($_POST["orderID"]) && 
    isset($_POST["cus"]) && 
    isset($_POST["user"]) && 
    isset($_POST["tsprice"]) && 
    isset($_POST["sdate"]) && 
    isset($_POST["items"])
) {
    $bal   = 0;
    $ptd   = 0;
    $pst   = "NOT APPLICABLE";
    $user  = $_POST['user']; 
    $tsprice = $_POST['tsprice']; 
    $cus   = $_POST['cus'];
    $orderID = $_POST['orderID']; 
    $sdate = $_POST['sdate']; 

    require_once "labClass.php";
    $con = new labClass();

    // Decode items JSON
    $items = json_decode($_POST['items'], true);

    if (!is_array($items) || empty($items)) {
        echo "No items received";
        exit;
    }

    $sn = 0;
    foreach ($items as $item) {
        $sn++;
        $iname  = $item['iname'];
        $isprice = $item['rate'];
        $nQty   = $item['eQty'];
        $tprice = $item['tprice'];
        $itemID = $item['itemID'];

        $Q = $con->getThisItemBID($itemID, $iname);
        if ($Q->rowCount() > 0) {
            $n = $Q->fetch();
            $ipprice = $n["ipprice"];

            // For now we just use entered quantity directly
            $tQty = $nQty;

            // Insert into profItem
            $rQ = $con->profItem($orderID, $itemID, $iname, $tprice, $isprice, $nQty, $sdate, $user);

          
        }
    }
   
    $rQc = $con->regProf($orderID, $cus, $bal, $ptd, $tsprice, $pst, $sdate);
    $rQ = $con->clearProfTemp();
    echo "Data saved successfully!";
    require_once "proforma.php";
} else {
    echo "Missing required fields!";
}
 

 /*
if(  isset($_POST["n"]) && isset($_POST["orderID"]) && isset($_POST["itemID"]) && isset($_POST["iname"]) && isset($_POST["user"]) && isset($_POST["rate"]) && isset($_POST["tsprice"]) && isset($_POST["tprice"])  && isset($_POST["cus"]) && isset($_POST["eQty"]) &&   isset($_POST["sdate"]))
{ 
    $nr=0;
    $tsprice= $_POST['tsprice']; 
    $sn= $_POST['n'];
    $bal=0;
    $ptd=0;
    $pst="NOT APPLICABLE";
    $user= $_POST['user']; 
    $isprice= $_POST['rate'];
    $tprice= $_POST['tprice'];
    $user= $_POST['user'];
    $cus= $_POST['cus'];
    $iname= $_POST['iname'];
    $nQty= $_POST['eQty'];
    $itemID= $_POST['itemID'];
    $orderID= $_POST['orderID']; 
    $sdate= $_POST['sdate']; 
  
    require_once"labClass.php";
    $con=new labClass();

  $Q= $con->getThisItemBID($itemID,$iname);
  if($Q->rowCount()>0)
    {

   $n=$Q->fetch();
   //$tQty=$n["iqty"]-$nQty;

   $tQty=$nQty;
   $ipprice=$n["ipprice"];
   //$rQ= $con->itemSellEntry($itemID,$iname,$ipprice,$isprice,$nQty,$sdate,$user,$pOpt);
   //$Qr= $con->updateItemQ($iname,$tQty,$itemID);
   $rQ= $con->profItem($orderID,$itemID,$iname,$tprice,$isprice,$nQty,$sdate,$user);
  
   if($rQ->rowCount()>0)
      {
       //echo "PASS";
      

      $ql= $con->getThisProf($orderID);
      //print_r($Qc->ErrorInfo());
      
      $nr=$ql->rowCount();
       if($sn==1)
        {
          $rQc= $con->regProf($orderID,$cus,$bal,$ptd,$tsprice,$pst,$sdate);
             //if(($ptd>0) && ($ptd<$tsprice)) 
             //{ 
              //$rc= $con->debitPRec($orderID,$cus,$ptd,$sdate,$user);
             //}
       $rQ= $con->clearProfTemp();
       require_once "proforma.php";
         }
        }
        }
   }
   //echo "2.finish";*/
?>