 
 <?php
if (
    isset($_POST["tim"]) &&
    isset($_POST["pOpt"]) &&
    isset($_POST["orderID"]) &&
    isset($_POST["tsprice"]) &&
    isset($_POST["cash"]) &&
    isset($_POST["cus"]) &&
    isset($_POST["pst"]) &&
    isset($_POST["sdate"]) &&
    isset($_POST["items"]) // JSON array of items
) {
    $tsprice = $_POST['tsprice'];
    $cash    = $_POST['cash'];
    $pst     = $_POST['pst'];
    $tim     = $_POST['tim'];
    $user    = $_POST['user'];
    $pOpt    = $_POST['pOpt'];
    $cus     = $_POST['cus'];
    $orderID = $_POST['orderID'];
    $sdate   = $_POST['sdate'];

    // Payment status
    if ($cash == $tsprice) {
        $pst = "PAID";
    }
    $ptd = $cash;
    $bal = $tsprice - $cash;

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

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

      $ql= $con->getThisOrder($orderID);
      //print_r($Qc->ErrorInfo());
      
      $nr=$ql->rowCount();
       if($nr<=0)
        {
    // Loop through each item
    foreach ($items as $item) {
        $itemID = $item['itemID'];
        $iname  = $item['iname'];
        $isprice= $item['rate']; // selling price
        $nQty   = $item['eQty'];
        $tprice = $item['tprice'];

        if (empty($itemID) || empty($iname) || empty($isprice) || empty($nQty) || empty($tprice)) {
            continue; // skip incomplete
        }

        // Validate item in DB
        $Q = $con->getThisItemBID($itemID, $iname);
        if ($Q->rowCount() > 0) {
            $n = $Q->fetch();
            $tQty    = $n["iqty"] - $nQty;
            $ipprice = $n["ipprice"];

            // Record sale
            $con->itemSellEntry($itemID, $tim, $iname, $ipprice, $isprice, $nQty, $sdate, $user, $pOpt);

            // Update stock
            $con->updateItemQ($iname, $tQty, $itemID);

            // Save order item
            $con->orderItem($orderID, $tim, $itemID, $iname, $tprice, $isprice, $nQty, $sdate, $user);
        }
    }

    // Register order once (header)
    $con->regOrder($orderID, $cus, $bal, $ptd, $tsprice, $pst, $sdate);

    // Record partial payment if needed
    if (($ptd > 0) && ($ptd < $tsprice)) {
        $con->debitPRec($orderID, $cus, $ptd, $sdate, $user);
    }

    // Clear temp records
    $con->clearOrderTemp();
}
    // Load invoice view
    require_once "pharmSinvoice.php";

} else {
    echo "Missing required POST data!";
}
 

 
 
 
 
 
 
/*if(isset($_POST["tim"]) && isset($_POST["pOpt"]) && 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["cash"]) && isset($_POST["cus"]) && isset($_POST["eQty"]) && isset($_POST["pst"]) && isset($_POST["sdate"]))
{ 
    $nr=0;
    $tsprice= $_POST['tsprice'];
    $cash= $_POST['cash'];
    $pst= $_POST['pst'];
    $sn= $_POST['n'];
    if($cash==$tsprice)
    {
      $pst="PAID";
    }
    $ptd=$cash;
    $bal=$tsprice-$cash;
    $tim= $_POST['tim'];
    $user= $_POST['user'];
    $pOpt= $_POST['pOpt'];
    $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;
   $ipprice=$n["ipprice"];
   $rQ= $con->itemSellEntry($itemID,$tim,$iname,$ipprice,$isprice,$nQty,$sdate,$user,$pOpt);;
   $Qr= $con->updateItemQ($iname,$tQty,$itemID);
   $rQ= $con->orderItem($orderID,$tim,$itemID,$iname,$tprice,$isprice,$nQty,$sdate,$user);
  
   if($rQ->rowCount()>0)
      {
       //echo "PASS";
      

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