<?php
require_once("../config/config.php");
////// final submit form ////
@extract($_POST);
if($_POST['Submit']=='Save'){
   ///// first of all we will check document string ///
   if(mysqli_num_rows(mysqli_query($link1,"SELECT id from document_counter where financial_year='".$prefixdocstr."' and doc_code='".$docstr."'"))==0){
   //// get state code from statemaster
   $res_state=mysqli_query($link1,"select code from state_master where state='".$locationstate."'")or die("ER1".mysqli_error($link1));
   $row_state=mysqli_fetch_array($res_state);
   $statecode=$row_state['code'];
   /// explode location type value
   $expld_loctyp=explode("~",$locationtype);
   ////// count max no. of location in selected state
   $query_code="select MAX(code_id) as qa from asc_master where state='".$locationstate."' and id_type='".$expld_loctyp[0]."'";
   $result_code=mysqli_query($link1,$query_code)or die("ER2".mysqli_error($link1));
   $arr_result2=mysqli_fetch_array($result_code);
   $code_id=$arr_result2[0];
   /// make 3 digit padding
   $pad=str_pad(++$code_id,3,"0",STR_PAD_LEFT);
   //// make logic of location code
   $newlocationcode=substr(strtoupper(BRANDNAME),0,2).$expld_loctyp[0].$statecode.$pad;
   ///// check generated id should not be in system
   if(mysqli_num_rows(mysqli_query($link1,"SELECT sno from asc_master where uid='".$newlocationcode."'"))==0){
   // insert all details of location //
  $sql="INSERT INTO asc_master set uid='".$newlocationcode."',pwd='".$newlocationcode."',name='".$locationname."',asc_code='".$newlocationcode."',code_id='".$pad."', user_level='".$expld_loctyp[1]."',group_id='".$map_gp_code."',shop_name='".$locationname."',id_type='".$expld_loctyp[0]."',contact_person='".$contact_person."',landline='".$landline."',email='".$email."',phone='".$phone."',addrs='".$comm_address."',disp_addrs='".$dd_address."',landmark='".$landmark."',city='".$locationcity."',state='".$locationstate."',pincode='".$pincode."',vat_no='".$tin_no."',pan_no='".$pan_no."',cst_no='".$cst_no."',st_no='".$st_no."',circle='".$circle."',status='Active',login_status='Active',start_date='".$today."',update_date='".$datetime."',remark='".$remark."',proprietor_type='".$proprietor."',tdsper='".$tdsper."',account_holder='".$accountholder."',account_no='".$accountno."',bank_name='".$bankname."',bank_city='".$bankcity."',ifsc_code='".$ifsccode."',gstin_no= '$_POST[gst_no]' , margin = '".$margin."',create_by='".$_SESSION['userid']."' ";

   mysqli_query($link1,$sql)or die("ER3".mysqli_error($link1));
   //insert into credit bal////////////////////////////
   mysqli_query($link1,"insert into current_cr_status set parent_code='".$parentid."', asc_code='".$newlocationcode."',cr_abl='0',cr_limit='0',total_cr_limit='0'")or die("ER4".mysqli_error($link1));
   ///insert into mapping table/////////////////////////
   mysqli_query($link1,"insert into mapped_master set uid='".$parentid."',mapped_code='".$newlocationcode."',status='Y',update_date='".$today."'")or die("ER5".mysqli_error($link1));
   ///insert into document string table/////////////////////////
   $invstr=$docstr."/".$prefixdocstr."/";
   $stnstr="STN/".$prefixdocstr."/".$docstr."/";
   $prnstr="PRN/".$prefixdocstr."/".$docstr."/";
   $srnstr="SRN/".$prefixdocstr."/".$docstr."/";
   $rcvpaystr="RECP/".$prefixdocstr."/".$docstr."/";
   mysqli_query($link1,"insert into document_counter set location_code='".$newlocationcode."',financial_year='".$prefixdocstr."',doc_code='".$docstr."',inv_str='".$invstr."',stn_str='".$stnstr."',prn_str='".$prnstr."',srn_str='".$srnstr."',rcvpay_str='".$rcvpaystr."',create_on='".$today."'")or die("ER6".mysqli_error($link1));
   //////////////////////////////////////////////////////////////
   //// create a user corresponding to this location
	$query_code="select MAX(uid) as qc from admin_users";
    $result_code=mysqli_query($link1,$query_code)or die("error2".mysql_error());
    $arr_result2=mysqli_fetch_array($result_code);
    $code_id=$arr_result2[0];
    $pad=str_pad(++$code_id,3,"0",STR_PAD_LEFT);
    $admiCode=substr(strtoupper(BRANDNAME),0,2)."USR".$expld_loctyp[0].$statecode.$pad;
	$pwd=$admiCode."@321";
	//// insert in user table
	$usr_add="INSERT INTO admin_users set username ='".$admiCode."',password ='".$pwd."',owner_code='".$newlocationcode."',user_level='".$expld_loctyp[1]."',name= '".$locationname."',utype='7',phone='".$phone."',emailid= '".$email."',create_by='".$_SESSION['userid']."' ,status='active',createdate='".date("Y-m-d H:i:s")."'";
    $res_add=mysqli_query($link1,$usr_add)or die("error3".mysql_error());
	//// give auto basic permission ////
	mysqli_query($link1,"insert into access_region set uid='".$admiCode."',region='".$circle."',status='Y'")or die(mysqli_error($link1));
	mysqli_query($link1,"insert into access_state set uid='".$admiCode."',state='".$locationstate."',status='Y'")or die(mysqli_error($link1));
	mysqli_query($link1,"insert into access_role set uid='".$admiCode."',role_id='".$expld_loctyp[0]."',status='Y'")or die(mysqli_error($link1));
	mysqli_query($link1,"insert into access_location set uid='".$admiCode."',location_id='".$newlocationcode."',status='Y'")or die(mysqli_error($link1));
	
   ////// insert in activity table////
	dailyActivity($_SESSION['userid'],$newlocationcode,"LOCATION","ADD",$ip,$link1,"");
	dailyActivity($_SESSION['userid'],$admiCode,"ADMIN USER","ADD",$_SERVER['REMOTE_ADDR'],$link1,"");
	////// return message
	$msg="You have successfully created a new location with ref. no. ".$newlocationcode." and location user id is ".$admiCode;
   }else{
	////// return message
	$msg="Something went wrong. Please try again.";
   }
   }else{
	////// return message
	$msg="Something went wrong like document code was already in DB. Please try again.";
   }
	///// move to parent page
    //header("Location:asp_details.php?msg=".$msg."".$pagenav);
	$pagenavi="&pid=22&hid=2";
	$urlvrbl=base64_encode("userid=".$admiCode."&userlevel=7&u_name=".$locationname."".$pagenavi);
	header("Location:urlfwd.php?msg=".base64_encode($msg)."&pagename=admin/update_permission&urlparameter=".$urlvrbl);
	exit;
}
?>

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title><?=siteTitle?></title>
 <script src="../js/jquery.min.js"></script>
 <link href="../css/font-awesome.min.css" rel="stylesheet">
 <link href="../css/abc.css" rel="stylesheet">
 <script src="../js/bootstrap.min.js"></script>
 <link href="../css/abc2.css" rel="stylesheet">
 <link rel="stylesheet" href="../css/bootstrap.min.css">
 <script>
	$(document).ready(function(){
        $("#frm1").validate();
    });
 </script>
 <script type="text/javascript" src="../js/jquery.validate.js"></script>
 <script type="text/javascript" src="../js/common_js.js"></script>
 <script language="javascript" type="text/javascript">
  /////////// function to get state on the basis of circle
  $(document).ready(function(){
	$('#circle').change(function(){
	  var name=$('#circle').val();
	  $.ajax({
	    type:'post',
		url:'../includes/getAzaxFields.php',
		data:{circle:name},
		success:function(data){
	    $('#statediv').html(data);
	    }
	  });
    });
/*	$('#frm1').on('submit',function(e){
		console.log('ffg');
		
	});*/
  });
 /////////// function to get city on the basis of state
 function get_citydiv(){
	  var name=$('#locationstate').val();
	  $.ajax({
	    type:'post',
		url:'../includes/getAzaxFields.php',
		data:{state:name},
		success:function(data){
	    $('#citydiv').html(data);
	    }
	  });
   
 }
 /////////// function to get Parent Location on the basis of location Type
 function getParentLocation(){
	  var name=$('#locationtype').val();
	  var splitval=name.split("~");
	  $.ajax({
	    type:'post',
		url:'../includes/getAzaxFields.php',
		data:{loctype:splitval[1]},
		success:function(data){
	    $('#parentdiv').html(data);
	    }
	  });
   
 }
 /////////// function to get city on the basis of state
 function checkDupliDoccode(val){
	  var fyr=$('#prefixdocstr').val();
	  $.ajax({
	    type:'post',
		url:'../includes/getAzaxFields.php',
		data:{fcyear:fyr,doccode:val},
		success:function(data){
	      //// if string found then alert
		  if(data>0){
			  alert("Duplicate document code");
			  $('#docstr').val('');
		  }
	    }
	  });
   
 }
  </script>
<body>
<div class="container-fluid">
  <div class="row content">
	<?php 
    include("../includes/leftnav2.php");
    ?>
    <div class="col-sm-9">
      <h2 align="center"><i class="fa fa-bank"></i> Add New Location</h2><br/><br/>
      <div class="form-group"  id="page-wrap" style="margin-left:10px;" >
          <form  name="frm1" id="frm1" class="form-horizontal" action="" method="post">
          <div class="form-group">
            <div class="col-md-6"><label class="col-md-6 control-label">Region/Circle <span class="red_small">*</span></label>
              <div class="col-md-6">
                 <select name="circle" id="circle" class="form-control required" required>
                  <option value="">--Please Select--</option>
                  <option value="EAST">EAST</option>
                  <option value="NORTH">NORTH</option>
                  <option value="SOUTH">SOUTH</option>
                  <option value="WEST">WEST</option>
                </select>
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label"> Mapped Group<span class="red_small">*</span></label>
              <div class="col-md-6">
               <select name="map_gp_code" id="map_gp_code"  required class="form-control required">
                  <option value="">--Please Select--</option>
                  <?php
                  $gp=mysqli_query($link1,"Select *  from group_master order by group_name");
				  while($row_gp=mysqli_fetch_array($gp)){
				  ?>
                  <option value="<?=$row_gp[group_id]?>">
                  <?=$row_gp[group_name]?>
                  </option>
                  <?php
				  }
                  ?>
                </select>
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-md-6"><label class="col-md-6 control-label">State <span class="red_small">*</span></label>
              <div class="col-md-6" id="statediv">
                 <select name="locationstate" id="locationstate" class="form-control required" required>
                  <option value=''>--Please Select--</option>
                
                </select>               
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label">City <span class="red_small">*</span></label>
              <div class="col-md-6" id="citydiv">
               <select name="locationcity" id="locationcity" class="form-control required" required>
               <option value=''>--Please Select-</option>
               </select>  
              </div>
            </div>
          </div>
          
          <div class="form-group">
            <div class="col-md-6"><label class="col-md-6 control-label">Location Type  <span class="red_small">*</span></label>
              <div class="col-md-6">
                <select name="locationtype" id="locationtype" class="form-control required" required onChange="return getParentLocation(this.value);">
                  <option value="">--Please Select--</option>
                  <?php
				///// check only one id of HO is in system  
				$checkhoid=mysqli_num_rows(mysqli_query($link1,"select sno from asc_master where id_type='HO' or user_level='1'"));
				if($checkhoid>0){$typelist=" and locationtype!='HO'";}else{$typelist="";}
				$type_query="SELECT * FROM location_type where status='A' $typelist and seq_id >  '".$_SESSION['user_level']."' order by seq_id";
				$check_type=mysqli_query($link1,$type_query);
				while($br_type = mysqli_fetch_array($check_type)){
				?>
                <option value="<?=$br_type['locationtype']."~".$br_type['seq_id']?>"<?php if($_REQUEST[locationtype]==$br_type[locationtype]."~".$br_type['seq_id']){ echo "selected";}?>><?php echo $br_type['locationname']?></option>
                <?php }?>
                </select>
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label">Organization Name <span class="red_small">*</span></label>
              <div class="col-md-6">
               <input type="text" name="locationname" id="locationname" required class="form-control required">
              </div>
            </div>
          </div>
		  <div class="form-group">
            <div class="col-md-6"><label class="col-md-6 control-label">Contact Person <span class="red_small">*</span></label>
              <div class="col-md-6">
                <input name="contact_person" type="text" class="form-control required" required id="contact_person">
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label">Contact Number<span class="red_small">*</span></label>
              <div class="col-md-6">
              <input name="phone" type="text" class="digits form-control"  id="phone" required maxlength="10" onKeyPress="return onlyNumbers(this.value);" onBlur="return phoneN();">
              </div>
            </div>
          </div>		 
		  <div class="form-group">
            <div class="col-md-6"><label class="col-md-6 control-label">Email <span class="red_small">*</span></label>
              <div class="col-md-6">
                <input name="email" type="email" class="email required form-control" id="email" required onBlur="return checkEmail(this.value,'email');">
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label">Landline Number <span class="red_small">*</span></label>
              <div class="col-md-6">
              <input name="landline" type="text" class="form-control required" id="landline" required maxlength="12" onKeyPress="return onlyNumbers(this.value);">
              </div>
            </div>
          </div>
		  <div class="form-group">
            <div class="col-md-6"><label class="col-md-6 control-label">Landmark </label>
              <div class="col-md-6">
                <input type="text" name="landmark" id="landmark" class="form-control">
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label">Pincode <span class="red_small">*</span></label>
              <div class="col-md-6">
                <input type="text" name="pincode" maxlength="6"  required class="digits form-control" onBlur="return pincodeV(this);" onKeyPress="return onlyNumbers(this.value);" id="pincode">
              </div>
            </div>
          </div>
		  <div class="form-group">
            <div class="col-md-6"><label class="col-md-6 control-label">Service Tax No. </label>
              <div class="col-md-6">
                <input name="st_no" type="text" class="form-control"  id="st_no">
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label">LST / CST Number</label>
              <div class="col-md-6">
              <input type="text" name="cst_no" class="form-control"  id="cst_no">
              </div>
            </div>
          </div>
		  <div class="form-group">
            <div class="col-md-6"><label class="col-md-6 control-label">PAN Number <span class="red_small">*</span></label>
              <div class="col-md-6">
                <input name="pan_no" type="text" class="form-control required" required id="pan_no">
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label">GST Number</label>
              <div class="col-md-6">
              <input type="text" name="gst_no" id="gst_no" class="form-control">
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-md-6"><label class="col-md-6 control-label">Proprietor Type <span class="red_small">*</span></label>
              <div class="col-md-6">
                <select name="proprietor" class="form-control required" required id="proprietor">
                   <option value="">--Please Select--</option>
                   <option value="OWNED">OWNED</option>
                   <option value="PARTNERSHIP">PARTNERSHIP</option>
                   <option value="NOPAN">NO PAN NUMBER</option>
                </select>
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label">TDS %</label>
              <div class="col-md-6">
              <select name="tdsper" class="form-control" id="tdsper">
                   <option value="">--Please Select--</option>
                   <option value="1.00">1%</option>
                   <option value="2.00">2%</option>
                   <option value="10.00">10%</option>
                </select>
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-md-6"><label class="col-md-6 control-label">Bank Account Holder Name <span class="red_small">*</span></label>
              <div class="col-md-6">
                <input name="accountholder" type="text" class="form-control required" required id="accountholder">
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label">Bank Account No. <span class="red_small">*</span></label>
              <div class="col-md-6">
              <input type="text" name="accountno" id="accountno" class="form-control required" required >
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-md-6"><label class="col-md-6 control-label">Bank Name <span class="red_small">*</span></label>
              <div class="col-md-6">
                <input name="bankname" type="text" class="form-control required" required id="bankname">
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label">Bank City <span class="red_small">*</span></label>
              <div class="col-md-6">
              <input type="text" name="bankcity" id="bankcity" class="form-control required" required>
              </div>
            </div>
          </div>
		  <!--<div class="form-group">
           <div class="col-md-6"><label class="col-md-6 control-label">CC Day </label>
              <div class="col-md-6">
                <input type="text" name="cc_day" id="cc_day" class="form-control"   value="0" onKeyPress="return onlyNumbers(this.value);">
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label">CC Limit</label>
              <div class="col-md-6">
              <input type="text" name="cc_limit" id="cc_limit" class="form-control" value="0.00" onKeyPress="return onlyFloat(this.value);">
              </div>
            </div>
          </div>-->
          <div class="form-group">
           <div class="col-md-6"><label class="col-md-6 control-label">Communication Address <span class="red_small">*</span></label>
              <div class="col-md-6">
                <textarea name="comm_address" id="comm_address" class="form-control required" required onkeypress = " return ( (event.keyCode ? event.keyCode : event.which ? event.which : event.charCode)!= 13);"  onContextMenu="return false" style="resize:vertical"></textarea>
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label">Delivery/Dispatch Address <span class="red_small">*</span></label>
              <div class="col-md-6">
               <textarea name="dd_address" id="dd_address" class="form-control required" required onkeypress = " return ( (event.keyCode ? event.keyCode : event.which ? event.which : event.charCode)!= 13);"  onContextMenu="return false" style="resize:vertical"></textarea>
              </div>
            </div>
          </div>
		  <div class="form-group">
            <div class="col-md-6"><label class="col-md-6 control-label">Unique Document Code <span class="red_small">*</span><br/><span style="font-size:11px;color:#FF0000">(Please enter a 3 character code)</span></label>
              <div class="col-md-3">
                <input type="text" name="prefixdocstr" id="prefixdocstr" class="form-control" style="width:120px;" value="<?=$fy?>" readonly>
              </div>
              <div class="col-md-3">
                <input type="text" name="docstr" id="docstr" class="required form-control" required style="width:95px;text-transform:uppercase" maxlength="3" minlength="3" onKeyUp="onlyCharcter(this.value,'docstr');" onBlur="checkDupliDoccode(this.value);">
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label">Parent Location <span class="red_small">*</span></label>
              <div class="col-md-6" id="parentdiv">
                 <select name="parentid" id="parentid" required class="form-control required">
                    <option value="">--Please Select--</option>
                    <option value="NONE">NONE</option>
                 </select>
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-md-6"><label class="col-md-6 control-label">Remark <span class="red_small">*</span></label>
              <div class="col-md-6">
                <textarea name="remark" id="remark" required class="form-control" onkeypress = " return ( (event.keyCode ? event.keyCode : event.which ? event.which : event.charCode)!= 13);"  onContextMenu="return false" style="resize:vertical"></textarea>
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label">IFSC Code </label>
               <div class="col-md-6">
               <input name="ifsccode" type="text" class="digits form-control required" id="ifsccode">
              </div>
            </div>
          </div>
		  <div class="form-group">
            <div class="col-md-6"><label class="col-md-6 control-label">Margin</label>
              <div class="col-md-6">
                 <input name="margin" type="text" class="form-control number" id="margin">
              </div>
            </div>
            <div class="col-md-6"><label class="col-md-6 control-label"></label>
               <div class="col-md-6">
            
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-md-12" align="center">
              <input type="submit" class="btn btn-primary" name="Submit" id="save" value="Save" title="" <?php if($_POST['Submit']=='Save'){?>disabled<?php }?>>
              <input title="Back" type="button" class="btn btn-primary" value="Back" onClick="window.location.href='asp_details.php?<?=$pagenav?>'">
            </div>
          </div>
    </form>
      </div><!--End form group-->
    </div><!--End col-sm-9-->
  </div><!--End row content-->
</div><!--End container fluid-->
<?php
include("../includes/footer.php");
include("../includes/connection_close.php");
?>
</body>
</html>