<?php
require_once("../includes/config.php");

$get_opr_rgts = getOprRights($_SESSION['userid'],$_REQUEST['pid'],$link1);

/////get status//
$arrstatus = getFullStatus("master",$link1);
@extract($_POST);
////// case 1. if we want to update details
if ($_REQUEST['op']=='Edit'){
	$sel_usr="select * from partcode_master where partid='".$_REQUEST['refid']."' ";
	$sel_res12=mysqli_query($link1,$sel_usr)or die("error1".mysqli_error($link1));
	$sel_result=mysqli_fetch_assoc($sel_res12);
        $pro_dimen = explode('x', $sel_result['pro_dimension']);      
}
////// case 2. if we want to Add new user
if($_POST){
	if ($_POST['addButton']=='ADD'){
		if(mysqli_num_rows(mysqli_query($link1,"select partid from partcode_master where part_name='".$partname."'"))==0){
			//////// upload file for travel //////
			if($_FILES['part_image']["name"]!=''){	
			   //// upload doc into folder ////
				$file_name =$_FILES['part_image']['name'];
				$file_tmp =$_FILES['part_image']['tmp_name'];
				$file_n =$datetime.$file_name;
				$file_p ="../doc_attach/part_image/$file_n";
				move_uploaded_file($file_tmp,$file_p);	
			}else{
				$file_n = "";
				$file_p = "";
			} // end of file upload	
		
    		$usr_add="INSERT INTO partcode_master set brandid='".$brandname."',itemtypeid='".$parttype."',vendor_code='".$vendpartcode."',hsn_code='".$hsn_code."', productid='".$prod_cat."',psubcatid ='".$product_sub_cat."',part_name='".ucwords($partname)."',pro_dimension='".$l."x".$w."x".$h."',bom_unit ='".$bomunit."',purchase_unit ='".$purchaseunit."',vendor_part_name='".$vendor_part_name."',material  ='".$material."',goods_type ='".$goodstype."',serialized ='".$serialize."',status='".$status."',createdate='".$datetime."', createby='".$_SESSION['userid']."', file_name = '".$file_n."', file_path = '".$file_p."', part_group = '".$part_group."' ";
			
		$res_add=mysqli_query($link1,$usr_add)or die("error3".mysqli_error($link1));
		$insid = mysqli_insert_id($link1);
		/// make 5 digit padding
		$pad=str_pad($insid,5,"0",STR_PAD_LEFT);
		//// make logic of partcode code
		$newpartcode="P".$pad; 
		//////// update system genrated code in model
		$req_res = mysqli_query($link1,"UPDATE partcode_master set partcode='".$newpartcode."' where partid='".$insid."'");
		////// insert in activity table////
		dailyActivity($_SESSION['userid'],$newpartcode,"PARTCODE","ADD",$_SERVER['REMOTE_ADDR'],$link1,"");
		////// return message
		$msg="You have successfully created a part like ".ucwords($partname);
		$cflag="success";
		$cmsg = "Success";
		}else{
			$msg = ucwords($partname)." is already exist";
			$cflag="warning";
			$cmsg = "Warning";
		}   
	}   
   	else if ($_POST['updButton']=='Update'){
	
		//////// upload file for travel //////
		if($_FILES['part_image']["name"]!=''){	
		   //// upload doc into folder ////
			$file_name =$_FILES['part_image']['name'];
			$file_tmp =$_FILES['part_image']['tmp_name'];
			$file_n =$datetime.$file_name;
			$file_p ="../doc_attach/part_image/$file_n";
     $file_name=", file_name = '".$file_n."'";
     $file_path=", file_path = '".$file_p."'";

			if($sel_result['file_path']!=""){
				unlink($sel_result['file_path']);
			}
			move_uploaded_file($file_tmp,$file_p);	
		}else{
			$file_name = "";
			$file_path = "";
		} // end of file upload	
	
    	$usr_upd="update partcode_master set brandid='".$brandname."',itemtypeid='".$parttype."',vendor_code='".$vendpartcode."',hsn_code='".$hsn_code."', productid='".$prod_cat."',psubcatid ='".$product_sub_cat."',part_name='".ucwords($partname)."',bom_unit ='".$bomunit."',purchase_unit ='".$purchaseunit."',vendor_part_name='".$vendor_part_name."',pro_dimension='".$l."x".$w."x".$h."',material  ='".$material."',goods_type ='".$goodstype."',serialized ='".$serialize."',status='".$status."',updatedate='".$datetime."',updateby='".$_SESSION['userid']."' ,part_group = '".$part_group."' $file_name $file_path where partid ='".$refid."' ";
	
    $res_upd=mysqli_query($link1,$usr_upd)or die("error4".mysqli_error($link1));
	////// insert in activity table////
	dailyActivity($_SESSION['userid'],$postpartcode,"PARTCODE","UPDATE",$ip,$link1,"");
	////// return message
	$msg="You have successfully updated part details for ".ucwords($partname);
	$cflag="success";
	$cmsg = "Success";
   }
   ///// move to parent page
    header("location:partcode_master.php?msg=".$msg."&chkflag=".$cflag."&chkmsg=".$cmsg."".$pagenav);
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title><?=siteTitle?></title>
 <link rel="shortcut icon" href="../images/titleimg.png" type="image/png">
 <script src="../js/jquery.js"></script>
 <?php 
 include("../includes/fontawesome.html");
 include("../includes/main_css.html"); 
 include("../includes/bootstrap.html");
  ?>
 <script>
	$(document).ready(function(){
		var spinner = $('#loader');
        $("#frm1").validate({
		  submitHandler: function (form) {
				if(!this.wasSent){
					this.wasSent = true;
					$(':submit', form).val('Please wait...')
									  .attr('disabled', 'disabled')
									  .addClass('disabled');
					spinner.show();				  
					form.submit();
				} else {
					return false;
				}
          }
		});
                $('.number').on('input', function() {
		match = (/(\d{0,6})[^.]*((?:\.\d{0,6})?)/g).exec(this.value.replace(/[^\d.]/g, ''));
		this.value = match[1] + match[2];
  	});
    });
/////////// function to get product sub category on the basis of product category
  $(document).ready(function(){
	$('#prod_cat').change(function(){
	  var prod_catid=$('#prod_cat').val();
	  $.ajax({
	    type:'post',
		url:'../includes/getAzaxFields.php',
		data:{grt_sub_pro_cat:prod_catid},
		success:function(data){
	    $('#pscdiv').html(data);
		$('#product_sub_cat').addClass('required');
	    }
	  });
    });
  });
</script>
<script src="../js/jquery.validate.js"></script>
<link href="../css/loader.css" rel="stylesheet"/>
<link href="../css/abc2.css" rel="stylesheet"/>	
</head>
<body>
<div class="container-fluid">
  <div class="row content">
	<?php 
    include("../includes/leftnav_admin.php");
    ?>
    <div class="<?=$screenwidth?>">
      <h2 align="center"><i class="fa <?=$fa_icon?>"></i> <?=$_REQUEST['op']?> Part Code</h2><br/>
      <div class="form-group"  id="page-wrap" style="margin-left:10px;">
          <form id="frm1" name="frm1" class="form-horizontal" action="" method="post" enctype="multipart/form-data">
          <div class="form-group">
              <div class="col-md-6">
                <label class="col-md-6 control-label">Brand <span class="red_small">*</span></label>
                <div class="col-md-6">
                  <select name="brandname" id="brandname" class="form-control required custom-select">
              			<option value="">Please Select</option>
						<?php 
						$sql ="select brand,brandid from brand_master where status='1' order by brand";
                        $qry = mysqli_query($link1,$sql) ;
                        while ($row=mysqli_fetch_array($qry)){?>    
              			<option value="<?php echo $row['brandid'];?>"<?php if(isset($sel_result['brandid'])){if($sel_result['brandid']==$row['brandid']){echo "selected";}}?>><?php echo $row['brand'];?></option>
              			<?php } ?>
              		</select>
                </div>
              </div>
              <div class="col-md-6">
                <label class="col-md-6 control-label">Product Type <span class="red_small">*</span></label>
                <div class="col-md-6">
                  <select name="parttype" id="parttype" class="form-control required custom-select" >
              			<option value="">Please Select</option>
              			<?php 
						$sql ="select typeid,parttype from part_type_master where status='1' order by parttype";
			  			$qry = mysqli_query($link1,$sql) ;
			  			while ($row=mysqli_fetch_array($qry)){?>
              			<option value="<?php echo $row['typeid'];?>"<?php if(isset($sel_result['itemtypeid'])){ if($sel_result['itemtypeid']==$row['typeid']){echo"selected";}}?>><?php echo $row['parttype']?></option>
              			<?php } ?>
              		</select>
                </div>
              </div>
            </div>
            <div class="form-group">
              <div class="col-md-6">
                <label class="col-md-6 control-label">Product <span class="red_small">*</span></label>
                <div class="col-md-6">
                  <select name="prod_cat" id="prod_cat" class="form-control required custom-select">
              		<option value="">Please Select</option>
              		<?php 
					$sql ="select productid,product_name from product_master where status='1' order by product_name";
			  		$qry = mysqli_query($link1,$sql)or die (mysqli_error($link1));
			  		while ($row=mysqli_fetch_array($qry)){?>
              		<option value="<?php echo $row['productid'];?>"<?php if(isset($sel_result['productid'])){ if($sel_result['productid']==$row['productid']){echo "selected";}}?>><?php echo $row['product_name']?></option>
              		<?php } ?>
              	</select>
                </div>
              </div>
              <div class="col-md-6">
                <label class="col-md-6 control-label">Product Sub Category <span class="red_small">*</span></label>
                <div class="col-md-6" id="pscdiv">
                  <?php if($_REQUEST['op']=='Add'){ ?>
                  <select name="product_sub_cat" id="product_sub_cat" class="form-control required custom-select">
              		<option value="">Please Select</option>
              	  </select>
                  <?php }else{?>
                  <select name="product_sub_cat" class="form-control required custom-select" id="product_sub_cat">
                  	<option value=''>All</option>
					<?php
                    $sql_psc = "select psubcatid,prod_sub_cat  from product_sub_category where productid='".$sel_result['productid']."' order by prod_sub_cat";
					$res_psc = mysqli_query($link1,$sql_psc);
					while($row_psc = mysqli_fetch_array($res_psc)){
					?>	
					<option value="<?=$row_psc[0]?>"<?php if(isset($sel_result['psubcatid'])){ if($sel_result['psubcatid']==$row_psc[0]){ echo "selected";}}?>><?php echo $row_psc[1]?></option>
					<?php } ?>
				  </select>
                  <?php }?>
                </div>
              </div>
            </div>
            <div class="form-group">
              <div class="col-md-6">
                <label class="col-md-6 control-label">HSN Code <span class="red_small">*</span></label>
                <div class="col-md-6">
                  <select name="hsn_code" id="hsn_code" class="form-control required custom-select">
              		<option value="">Please Select</option>
              		<?php 
					$sql ="select hsn_code,sgst,igst,cgst from tax_hsn_master where status='1'";
			  		$qry = mysqli_query($link1,$sql)or die (mysqli_error($link1));
			  		while ($row=mysqli_fetch_array($qry)){?>
              		<option value="<?php echo $row['hsn_code'];?>"<?php if(isset($sel_result['hsn_code'])){ if($sel_result['hsn_code']==$row['hsn_code']){echo "selected";}}?>><?php echo $row['hsn_code']." (SGST- ".$row['sgst']."%&nbsp;&nbsp;CGST- ".$row['cgst']."%&nbsp;&nbsp;CGST- ".$row['igst']."%)";?></option>
              		<?php } ?>
              	</select>
                </div>
              </div>
                <div class="col-md-6">
                <label class="col-md-6 control-label">Serialize/Non-Serialize <span style="color:red">*</span></label>
                <div class="col-md-6">
                <select name="serialize" id="serialize" class="form-control required custom-select">
               		<option value="">Please Select</option>
              		<option value="Y"<?php if(isset($sel_result['serialized'])){ if(($sel_result['serialized']=="Y")){echo "selected";}}?>>Yes</option>
              		<option value="N"<?php if(isset($sel_result['serialized'])){ if(($sel_result['serialized']=="N")){echo "selected";}}?>>No</option>  
              	</select>
                </div>
              </div>            
            </div>
              <div class="form-group">
                <div class="col-md-6">
                <label class="col-md-6 control-label">Model</label>
                <div class="col-md-6">
                	<input type="text" name="material" id="material" value="<?php if(isset($sel_result['material'])){ echo $sel_result['material'];}?>" class="form-control enter"/>
                </div>
              </div>
              <div class="col-md-6">
                <label class="col-md-6 control-label">Material/Part Name <span class="red_small">*</span></label>
                <div class="col-md-6">
                    <input type="text" name="partname" id="partname"  value="<?php if(isset($sel_result['part_name'])){ echo $sel_result['part_name'];}?>" class="form-control enter"/>
                    
                  <!--<textarea name="partname" id="partname" class="form-control required mastername cp" style="background-color:#FFFFCC; resize:vertical"><?php if(isset($sel_result['part_name'])){ echo $sel_result['part_name'];}?></textarea>-->
                </div>
              </div>              
            </div>
              <div class="form-group">
              <div class="col-md-6">
                <label class="col-md-6 control-label">Vendor Partcode</label>
                <div class="col-md-6">
                	<input type="text" name="vendpartcode" id="vendpartcode" class="form-control alphanume" value="<?php if(isset($sel_result['vendor_code'])){ echo $sel_result['vendor_code'];}?>"/>
                </div>
              </div>
              <div class="col-md-6">
                <label class="col-md-6 control-label">Vendor Part Name</label>
                <div class="col-md-6">
                	<input type="text" name="vendor_part_name" id="vendor_part_name" value="<?php if(isset($sel_result['vendor_part_name'])){ echo $sel_result['vendor_part_name'];}?>" class="form-control enterna"/>
                </div>
              </div>
            </div>            
            <div class="form-group">
              <div class="col-md-6">
                <label class="col-md-6 control-label">Bom Unit <span class="red_small">*</span></label>
                <div class="col-md-6">
                  <select name="bomunit" id="bomunit" class="form-control required custom-select" >
              		<option value="">Please Select</option>
              			<?php 
						$sql ="select unitname from unit_master where status='1' order by unitname";
			  			$qry = mysqli_query($link1,$sql) ;
			  			while ($row=mysqli_fetch_array($qry)){?>
              			<option value="<?php echo $row['unitname'];?>"<?php if(isset($sel_result['bom_unit'])){ if($sel_result['bom_unit']==$row['unitname']){echo "selected";}}?>><?php echo $row['unitname']?></option>
              		<?php } ?>
              	</select>
                </div>
              </div>
              <div class="col-md-6">
                <label class="col-md-6 control-label">Purchase Unit</label>
                <div class="col-md-6">
                <select name="purchaseunit" id="purchaseunit" class="form-control required custom-select">
              		<option value="">Please Select</option>
              		<?php 
					$sql ="select unitname from unit_master where status='1' order by unitname";
			  		$qry = mysqli_query($link1,$sql) ;
			  		while ($row=mysqli_fetch_array($qry)){?>
              		<option value="<?php echo $row['unitname'];?>"<?php if(isset($sel_result['purchase_unit'])){ if($sel_result['purchase_unit']==$row['unitname']){echo "selected";}}?>><?php echo $row['unitname']?></option>
              		<?php } ?>
              	</select>
                </div>
              </div>
            </div>
            <div class="form-group">
              <div class="col-md-6">
                <label class="col-md-6 control-label">Goods Type <span class="red_small">*</span></label>
                <div class="col-md-6">
                  <select name="goodstype" id="goodstype" class="form-control required custom-select">
              		<option value="">Please Select</option>
              		<option value="Trading"<?php if(isset($sel_result['goods_type'])){ if($sel_result['goods_type']=="Trading"){echo "selected";}}?>>Trading</option>
              		<option value="Manufacturing"<?php if(isset($sel_result['goods_type'])){ if($sel_result['goods_type']=="Manufacturing"){echo "selected";}}?>>Manufacturing</option>
              	</select>
                </div>
              </div>
                <div class="col-md-6">
                <label class="col-md-6 control-label">Status <span class="red_small">*</span></label>
                <div class="col-md-6">
                 <select name="status" id="status" class="form-control custom-select">
                    <?php foreach($arrstatus as $key => $value){
						if($value!="Pending For Approval"){
						?>
                    	<option value="<?=$key?>" <?php if(isset($sel_result['status'])){ if($sel_result['status'] == $key) { echo 'selected'; }}?>><?=$value?></option>
                    <?php }} ?>
                 </select>
                </div>
              </div>              
            </div>
             <div class="form-group">
              <div class="col-md-6">
                <label class="col-md-6 control-label">Product Dimension(In Centimetre)</label>
                 <div class="col-md-2">Length
                	<input type="text" name="l" id="l" value="<?php if(isset($pro_dimen[0])){ echo $pro_dimen[0];}?>" class="form-control number"/>
                </div>
                <div class="col-md-2">Width
                	<input type="text" name="w" id="w" value="<?php if(isset($pro_dimen[1])){ echo $pro_dimen[1];}?>" class="form-control number"/>
                </div>
                <div class="col-md-2">Height
                	<input type="text" name="h" id="h" value="<?php if(isset($pro_dimen[2])){ echo $pro_dimen[2];}?>" class="form-control number"/>
                </div>
              </div>
              <div class="col-md-6">
                <label class="col-md-6 control-label">Part Image</label>
                <div class="col-md-6">
                	<input type="file" name="part_image" id="part_image" class="form-control"/>
                </div>
              </div>                
            </div>
			<div class="form-group">
              <div class="col-md-6">
                <label class="col-md-6 control-label">Part Group</label>
                 <div class="col-md-6">
                	<input type="text" name="part_group" id="part_group" value="<?php if(isset($sel_result['part_group'])){ echo $sel_result['part_group'];}?>" class="form-control"/>
                </div>  
			  </div>
			  <div class="col-md-6">
                
			  </div>	           
            </div>
			
          	<div class="form-group">
            	<div class="col-md-12" align="center">
              	<?php if($_REQUEST['op']=='Add'){ ?>
                <button class='btn<?=$btncolor?>' id="add" type="submit" name="addButton" value="ADD"><i class="fa fa-save fa-lg"></i>&nbsp;&nbsp;Add</button>
              	<?php }else{
					if($get_opr_rgts['edit']=="Y"){              	
              	?>
                <button class='btn<?=$btncolor?>' id="upd" type="submit" name="updButton" value="Update"><i class="fa fa-retweet fa-lg"></i>&nbsp;&nbsp;Update</button>
              	<?php } }?>
              	<input type="hidden" name="refid"  id="refid" value="<?=$sel_result['partid']?>" />
                <input type="hidden" name="postpartcode"  id="postpartcode" value="<?=$sel_result['partcode']?>" />
                <button title="Back" type="button" class="btn<?=$btncolor?>" onClick="window.location.href='partcode_master.php?status=<?php if(isset($_REQUEST['status'])){echo $_REQUEST['status'];}?>&prod_cat=<?php if(isset($_REQUEST['prod_cat'])){echo $_REQUEST['prod_cat'];}?>&product_sub_cat=<?php if(isset($_REQUEST['product_sub_cat'])){echo $_REQUEST['product_sub_cat'];}?>&brandname=<?php if(isset($_REQUEST['brandname'])){echo $_REQUEST['brandname'];}?><?=$pagenav?>'"><i class="fa fa-reply fa-lg"></i>&nbsp;&nbsp;Back</button>
            	</div>
          	</div>
    	</form>
      	</div>
    </div>
  </div>
</div>
<div id="loader"></div>
<?php
include("../includes/footer.php");
include("../includes/connection_close.php");
?>
</body>
</html>