<?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');
  //print_r($plan);die();
  $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'] : '';

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

    //$query="INSERT INTO draftplandetails (PlanId,ArticleSAPCode,UnitId,SAPCode,AvlStock,AlocStock,ReqStock) VALUES (13, '2-FB-3058-BK-G1', 5, '5-CO07-0002', 0, 0, 134400), (13, '2-FB-3058-BK-G1', 5, '5-NL01-1279', 0, 0, 18066.719999999983), (13, '2-FB-3058-BK-G1', 5, '5-NL06-0636', 0, 0, 72877.22400000083), (13, '2-FB-3058-BK-G1', 5, '5-PO01-0001', 0, 0, 3064.7819999999997), (13, '2-FB-3058-BK-G1', 5, '5-PO01-0002', 0, 0, 5369.1539999999995), (13, '2-FB-3058-BK-G1', 5, '5-PO01-0004', 0, 0, 22636.622400000007), (13, '2-FB-3058-BK-G1', 5, '5-PO01-0006', 0, 0, 2628.9900000000002), (13, '2-FB-3058-BK-G1', 5, '5-PO01-0008', 0, 0, 26784.769200000017), (13, '2-FB-3058-BK-G1', 5, '5-PO01-0017', 0, 0, 6272.112), (13, '2-FB-3058-BK-G1', 5, '5-PO01-0050', 0, 0, 3777.5220000000004), (13, '2-FB-3058-BK-G1', 5, '5-PO01-0053', 0, 0, 3421.1520000000005), (13, '2-FB-3058-BK-G1', 5, '5-PO01-0059', 0, 0, 3492.426000000001), (13, '2-FB-3058-BK-G1', 5, '5-PO01-0063', 0, 0, 3492.426000000001), (13, '2-FB-3058-BK-G1', 5, '6-ADH-0029', 0, 0, 5.039999999999999), (13, '2-FB-3058-BK-G1', 5, '6-ADH-0032', 0, 0, 2408.4480000000026), (13, '2-FB-3058-BK-G1', 5, '6-ADH-0076', 0, 0, 3.5), (13, '2-FB-3058-BK-G1', 5, '6-CHM-0010', 0, 0, 16.8), (13, '2-FB-3058-BK-G1', 5, '6-CHM-0012', 0, 0, 712.7400000000007), (13, '2-FB-3058-BK-G1', 5, '6-CHM-0018', 0, 0, 142.54800000000003), (13, '2-FB-3058-BK-G1', 5, '6-CHM-0029', 0, 0, 399.1344), (13, '2-FB-3058-BK-G1', 5, '6-CHM-0082', 0, 0, 285.09600000000006), (13, '2-FB-3058-BK-G1', 5, '6-CHM-0112', 0, 0, 71.27400000000002), (13, '2-FB-3058-BK-G1', 5, '6-CHM-0126', 0, 0, 20.159999999999997), (13, '2-FB-3058-BK-G1', 5, '6-CHM-0207', 0, 0, 142.54800000000003), (13, '2-FB-3058-BK-G1', 5, '6-OTH-0056', 0, 0, 67200), (13, '2-FB-3058-BK-G1', 5, '7-AT-0015', 0, 0, 616), (13, '2-FB-3058-BK-G1', 5, '7-BT-0001', 0, 0, 1505), (13, '2-FB-3058-BK-G1', 5, '7-MC-0656', 0, 0, 350), (13, '2-FB-3058-BK-G1', 5, '7-PS-0019', 0, 0, 16800), (13, '2-FB-3058-BK-G1', 5, '7-PS-0051', 0, 0, 350), (13, '2-FB-3058-BK-G1', 5, '7-PS-0119', 0, 0, 350), (13, '2-FB-3058-BK-G1', 5, '7-SC-0466', 0, 0, 16800), (13, '2-FB-3058-BK-G1', 5, '7-TG-0032', 0, 0, 16800), (13, '2-FB-3058-BK-G1', 5, 'CLICK-OH', 0, 0, 445200), (13, '2-FB-3058-BK-G1', 5, 'FGMC-OH', 0, 0, 8400), (13, '2-FB-3058-BK-G1', 5, 'FGSC-OH', 0, 0, 16800), (13, '2-FB-3058-BK-G1', 5, 'MPU-OH', 0, 0, 33600), (13, '2-FB-3058-BK-G1', 5, 'PRINTING-CHARGES', 0, 0, 151200), (13, '2-FB-3058-BK-G1', 5, 'SLITT-OH', 0, 0, 1881600), (13, '2-FB-3058-BK-G1', 5, 'STITCH-OH', 0, 0, 67200), (13, '2-FB-3058-BK-G1', 5, 'STITCHING-CHARGES', 0, 0, 67200)";
    //$db->query($query);die();


    $sql ="CALL rowmaterialcalculation('{$article}',{$spdetails[0]['CaseCount']},'{$unitname}')";

    $bomrow=$db->query($sql);
    //print_r($bomrow[0]);die();
    //$bomrow="yes";
    //$query  = "INSERT INTO draftplandetails (PlanId,ArticleSAPCode,UnitId,SAPCode,AvlStock,AlocStock,ReqStock) VALUES ";
    
    if(!empty($bomrow)) {

      foreach($bomrow as $bomr) {
         
          //$query .="({$PlanId}, '{$article}', {$unitid}, '{$bomr['child']}', 0, 0, {$bomr['ReqQty']}),";
          //$query .=" ON DUPLICATE KEY UPDATE AvlStock= AvlStock + 0,AlocStock= AlocStock + 0,ReqStock= ReqStock + {$bomr['ReqQty']}";
          $sql="CALL InsertDraftPlanDetails({$PlanId},{$article},{$unitid},'{$bomr['child']}',{$bomr['ReqQty']})";
          $db->query($sql);
        }

        //$query1=trim($query,", ");

         
           
        
        die();
          //---------------Getting BOM details based on process order for loop------------------------//

        foreach($bomrow as $bomr) {


            //print_r($orderwisedetails);die();
          $articleavlBom['requiredstock']=$bomr['ReqQty'];
          $articleavlBom['avlstock']=0;
          $articleavlBom['alocatedstock']=0;
          
          

          //$sapdatafetchavl=find_materialdetails_by_articlecode($orderwisedetails['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['avlstock']=$sapdatafetchavl[$l]['avlstock'];
            //$articleavlBom['requiredstock']=$articleavlBom['requiredstock']-$articleavlBom['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['alocatedstock']=$planwiseallocated[0]['Quantity'];
              //$articleavlBom['requiredstock']=$articleavlBom['requiredstock']+$articleavlBom['alocatedstock'];
            //}
            //print_r($articleavlBom);die();
            //}
            //echo $k."--".$j."--";
            //print_r($sapdatafetchavl);die();
          //}
          $sapcode=$bomr['child'];
          $avlstock=$articleavlBom['avlstock'];
          $reqstock=$articleavlBom['requiredstock'];
          $alocstock=$articleavlBom['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);
    }

?>
