<?php
  

  require_once('D:\Workspace\htdocs\mmvkc\includes\load.php');

    $planids=9;
    $products = join_salesplan_table_plan($planids);
    //print_r($products); die();

//---------------Getting plan details and looping it------------------------//
$insertadd=0;
    for($i=0;$i<count($products);$i++) {
      $planname=$products[$i]['PlanName'];
      $PlanId=$products[$i]['PlanId'];
      $article=$products[$i]['SAPCode'];
      $unitname=$products[$i]['UnitName'];
      $unitid=$products[$i]['UnitId'];
        $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--){

          $orderwisedetails=find_articledetails_by_process_order($article,$k);
          //print_r($orderwisedetails); die();

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

        for($j=0;$j<count($orderwisedetails);$j++){

          $articleavlBom[$k][$j]['bom']=$orderwisedetails[$j]['ChildSAPCode'];
          $articleavlBom[$k][$j]['requiredstock']=$products[$i]['CaseCount'] * $orderwisedetails[$j]['Quantity'];
          $articleavlBom[$k][$j]['order']=$k;
          $articleavlBom[$k][$j]['avlstock']=0;
          $articleavlBom[$k][$j]['alocatedstock']=0;
          $articleavlBom[$k][$j]['Unitname']=$unitname;
          

          $sapdatafetchavl=find_materialdetails_by_articlecode($orderwisedetails[$j]['ChildSAPCode'],$unitname);

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

    if($insertadd>0){
      $session->msg('s'," Draft Plan Created Successfully! ");
      redirect('choosesalesplan.php', false);
      
    } else {
      $session->msg('d',' Draft Plan Creation Failed!');
      redirect('choosesalesplan.php', false);
      
    }




?>