<?php
  $page_title = 'Draft Plan';
  require_once('includes/load.php');
  // Checkin What level user has permission to view this page
   page_require_level(2);
  $products = join_salesplan_table();
  //print_r($products);die();
  $insertadd=0;
  $trigbom= selectbomtocreate();

  //print_r($trigbom);die();
  if(!empty($trigbom)) {
    $PlanId=$trigbom[0]['PlanId'];
    $unitid=$trigbom[0]['UnitId'];
    $article=$trigbom[0]['Father'];

    $spdetails=salesplandetailsforarticle($PlanId,$unitid,$article);
    //print_r($spdetails[0]['CaseCount']);die();

      // Fetch plan name from plandetails table
  $plan = find_by_columnnew('plandetails', $PlanId, 'PlanId');
  $planName = isset($plan['PlanName']) ? $plan['PlanName'] : '';

  // Fetch unit name from unitdetails table
  $unit = find_by_columnnew('unitdetails', $unitid, 'UnitId');
  $unitname = isset($unit['UnitName']) ? $unit['UnitName'] : '';

    //$bomset=bomdetailscreation($PlanId,$unitid);

    $sql ="CALL SourcetableQtyCalculation({$PlanId},{$unitid})";
    //$bomset=$db->query($sql);
    //print_r($bomset);die();
    $bomset="yes";
    if(!empty($bomset)) {

    //echo $article;die();

        $articleraws=find_by_columnnall('bomdetails',$article,'ArticleSAPCode');
        //print_r($articleraws); die();
        //$processlimit=find_higest_process_order($article);
        //$Porder = $processlimit[0]['plimit'];

//---------------Getting BOM details and highest process order for loop------------------------//

        //for($k=$Porder;$k>0;$k--){
          $k=0;
          //$orderwisedetails=find_articledetails_by_process_order($article,$k);
          $orderwisedetails=findrawmaterials("sourcefek",$article);
          //print_r($orderwisedetails); die();

//---------------Getting BOM details based on process order for loop------------------------//

        for($j=0;$j<count($orderwisedetails);$j++){
            //print_r($orderwisedetails);die();
          $articleavlBom[$j]['bom']=$orderwisedetails[$j]['ChildSAPCode'];
          $articleavlBom[$j]['requiredstock']=$spdetails[0]['CaseCount'] * $orderwisedetails[$j]['Quantity'];
          $articleavlBom[$j]['order']=$k;
          $articleavlBom[$j]['avlstock']=0;
          $articleavlBom[$j]['alocatedstock']=0;
          $articleavlBom[$j]['Unitname']=$unitname;
          

          $sapdatafetchavl=find_materialdetails_by_articlecode($orderwisedetails[$j]['ChildSAPCode'],$unitname);
          //print_r($sapdatafetchavl);die();
          //---------------Getting BOM details based on sapdata available stock for loop------------------------//

          //if(!empty($sapdatafetchavl)&&!empty($sapdatafetchavl[0]['Unitname'])){

            //for($l=0;$j<count($sapdatafetchavl);$l++){
            //$articleavlBom[$j]['avlstock']=$sapdatafetchavl[$l]['avlstock'];
            //$articleavlBom[$j]['requiredstock']=$articleavlBom[$j]['requiredstock']-$articleavlBom[$j]['avlstock'];
            //$planwiseallocated=find_materialstock_in_plans($sapdatafetchavl[$l]['sap_code'],$sapdatafetchavl[$l]['Unitname']);
            //print_r($planwiseallocated);die();

            //if(!empty($planwiseallocated)&&!empty($planwiseallocated[0]['PlanId'])){
             // $articleavlBom[$j]['alocatedstock']=$planwiseallocated[0]['Quantity'];
              //$articleavlBom[$j]['requiredstock']=$articleavlBom[$j]['requiredstock']+$articleavlBom[$j]['alocatedstock'];
            //}
            //print_r($articleavlBom);die();
            //}
            //echo $k."--".$j."--";
            //print_r($sapdatafetchavl);die();
          //}
          $sapcode=$articleavlBom[$j]['bom'];
          $avlstock=$articleavlBom[$j]['avlstock'];
          $reqstock=$articleavlBom[$j]['requiredstock'];
          $alocstock=$articleavlBom[$j]['alocatedstock'];
          
          $query  = "INSERT INTO draftplandetails (";
          $query .=" PlanId,ArticleSAPCode,UnitId,SAPCode,AvlStock,AlocStock,ReqStock";
          $query .=") VALUES (";
          $query .=" {$PlanId}, '{$article}', {$unitid}, '{$sapcode}', {$avlstock}, {$alocstock}, {$reqstock}";
          $query .=")";
          $query .=" ON DUPLICATE KEY UPDATE AvlStock= AvlStock + {$avlstock},AlocStock= AlocStock + {$alocstock},ReqStock= ReqStock + {$reqstock}";
          if($db->query($query)){
            //$session->msg('s',"Product added ");
            //redirect('add_product.php', false);
            $insertadd=1;
          } else {
            //$session->msg('d',' Sorry failed to added!');
            //redirect('product.php', false);
            $insertadd=0;
          }
        
          
          
        }
        //echo "--".$k."--";
        //print_r($articleavlBom);die();
      //}
    }
    }

    if($insertadd>0) {
      updatefinishedboms($PlanId,$unitid);
    }

?>
