<?php
 

if (
    isset($_POST["add"]) &&
    isset($_POST["pOpt"]) &&
    isset($_POST["orderID"]) &&
    isset($_POST["user"]) &&
    isset($_POST["tsprice"]) &&
    isset($_POST["cash"]) &&
    isset($_POST["cus"]) &&
    isset($_POST["pst"]) &&
    isset($_POST["sdate"]) &&
    isset($_POST["items"])
) {
    $add = $_POST['add'];
    $tsprice = $_POST['tsprice'];
    $cash = $_POST['cash'];
    $pst = $_POST['pst'];

    // If fully paid, set as PAID
    if ($cash == $tsprice) {
        $pst = "PAID";
    }

    $ptd = $cash;
    $bal = $tsprice - $cash;
    $user = $_POST['user'];
    $pOpt = $_POST['pOpt'];
    $cus = $_POST['cus'];
    $orderID = $_POST['orderID'];
    $sdate = $_POST['sdate'];

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

    if (!is_array($items) || count($items) == 0) {
        echo "No items received.";
        exit;
    }

    require_once "labClass.php";
    $con = new labClass();
$Qc = $con->getThisPOrder($orderID);
  if ($Qc->rowCount() > 0) {
     $orderID= $Qc->rowCount()+1;
  }
  $Qc = $con->getThisPOrder($orderID);
  if ($Qc->rowCount() <= 0) {
    $allOk = true;
    foreach ($items as $index => $item) {
        $itemID = $item['itemID'];
        $iname = $item['iname'];
        $ipprice = $item['rate'];
        $nQty = $item['eQty'];
        $tprice = $item['tprice'];

        // Check if item exists
        $Q = $con->getThisItemBID($itemID, $iname);
        if ($Q->rowCount() > 0) {
            $n = $Q->fetch();
            $isprice = $n["isprice"];

            // Record the order item
            $rQ = $con->porderItem(
                $orderID,
                $itemID,
                $iname,
                $tprice,
                $ipprice,
                $nQty,
                $sdate,
                $user,
                $pOpt
            );

            if ($rQ->rowCount() <= 0) {
                $allOk = false;
                echo "Failed to save item: " . htmlspecialchars($iname);
            }
        } else {
            $allOk = false;
            echo "Item not found: " . htmlspecialchars($iname);
        }
    }

    // Register purchase order only once if all items succeeded
    if ($allOk) {
        
            $rQc = $con->regPOrder($orderID, $cus, $bal, $ptd, $tsprice, $pst, $sdate, $add);

            // Optional: if partially paid, handle debit
            if ($ptd > 0 && $ptd < $tsprice) {
                // $rc = $con->debitPRec($orderID, $cus, $ptd, $sdate, $user);
            }

            // Clear temp data
             $rQ= $con->clearPOrderTemp();
            //$rQ = $con->clearPCreateOrderTemp();

            // Load the order summary page
            require_once "pharmPurchaseOrderLPO.php";
       
    } else {
        echo "One or more items could not be saved.";
    }
    } else {
            echo "Purchases order exists.";
        }
} else {
    echo "Incomplete data received.";
}
 

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

      $Qc= $con->getThisPOrder($orderID);
      //print_r($Qc->ErrorInfo());
       if($sn==1)
        {
          $rQc= $con->regPOrder($orderID,$cus,$bal,$ptd,$tsprice,$pst,$sdate,$add);
             if(($ptd>0) && ($ptd<$tsprice)) 
             { 
              //$rc= $con->debitPRec($orderID,$cus,$ptd,$sdate,$user);
             }
       $rQ= $con->clearPOrderTemp();
       require_once "pharmPurchaseOrderLPO.php";
         }
        }
        }
   }*/
   //echo "2.finish";
?>