function refresh_dropdown()
{
	var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
	var token_value  = $('#my_csrf').val();
	var form_data    = new FormData();
	form_data.append(token_name,token_value);
	$.ajax({
	      url:base_url+'stock/refresh_categories_items',
	      type:'POST',
	      processData:false,
		  contentType: false,
	      data:form_data,
	      success:function(response)
	      {
	      	var res = $.parseJSON(response);
	      	$('#my_csrf').val(res[0]);
	      	$('#stk_category').html("");
	      	$('#stk_category').html(res[1]);
	      	$('#stk_ecategory').html(res[1]);
	      }
	});//End of Ajax Request
}
function chk_avl()
{
	$('#error-msg').html('');
	var aq = parseFloat($('#stk_trf_frm_qty').val());
	var b2 = parseFloat($('#stk_base_qty2').val());
	if(b2>aq)
	{
		$('#error-msg').html('Quantity Exceeded');
		$('#stk_base_qty2').val(aq);
	}	
}
function conversion()
{
	chk_avl();
	var b1 = parseFloat($('#stk_base_qty1').val());
	var c1 = parseFloat($('#stk_converted_qty1').val());
	var b2 = parseFloat($('#stk_base_qty2').val());
	if(isNaN(b1)){b1=0;}
	if(isNaN(c1)){c1=0;}
	if(isNaN(b2)){b2=0;}
	if(b1>c1)
	{
		$('#stk_converted_qty2').val((b2/b1)*c1);	
	}
	else
	{
		$('#stk_converted_qty2').val(b2*c1);
	}

}
$(document).ready(function(){
	$(window).on('load', function () {
	 	prevent_tabs_on_refresh();
	});
	
	$('.ifnotify2').trigger('click');
	//Number only
	$(document).on('keypress','.isnumber',function(e){
 	 return isNumber(e, this);
	});
	$("#stk_pdate").datepicker({dateFormat:'yy-mm-dd'});
	$("#e_stk_pdate").datepicker({dateFormat:'yy-mm-dd'});
	$('.genbarcode').on('click',function(e){
	  e.preventDefault();
	  var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
	  var token_value  = $('#my_csrf').val();
	  var form_data    = new FormData();
	  form_data.append(token_name,token_value);
	  $.ajax({
	      url:base_url+'stock/generate_barcode',
	      type:'POST',
	      processData:false,
		  contentType: false,
	      data:form_data,
	      success:function(response)
	      {
	      	var res = $.parseJSON(response);
	      	$('#my_csrf').val(res[0]);
	        $('#stk_barcode').val(res[1]);
	      },
	      error:function(jqXHR, exception)
	      {
	        alert(jqXHR.status);
	      }
	  });//End of Ajax Request
	});//End of generating barcode
	
	$('#e_genbarcode').on('click',function(e){
	  e.preventDefault();
	  var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
	  var token_value  = $('#my_csrf').val();
	  var form_data    = new FormData();
	  form_data.append(token_name,token_value);
	  $.ajax({
	      url:base_url+'stock/generate_barcode',
	      type:'POST',
	      processData:false,
		  contentType: false,
	      data:form_data,
	      success:function(response)
	      {
	      	var res = $.parseJSON(response);
	      	$('#my_csrf').val(res[0]);
	        $('#e_stk_barcode').val(res[1]);
	      },
	      error:function(jqXHR, exception)
	      {
	        alert(jqXHR.status);
	      }
	  });//End of Ajax Request
	});//End of generating barcode
	
	
	/*** Stock Type Section STARTS ***/
		$('#typeTable').DataTable({
	    	 "sAjaxSource": base_url+'stock/show_types',
	    	 "pageLength": 10,
	    	 "order": [],
	    	 "responsive":true
	    });
		
		$('#type_name').on('keypress',function(e){
			var key = e.which;
			if(key==13)
			{
				e.preventDefault();
				$('#btn-add-type').trigger('click');
			}
		});

		$('#btn-add-type').on('click',function(e){
			var type_name = $('#type_name').val();
			if(type_name=="")
			{
				$('#ifnotify-warning').text(" Provide Name for type");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
			var token_value  = $('#my_csrf').val();
			var form_data    = new FormData();
			form_data.append('type_name',type_name);
			form_data.append(token_name,token_value);
			$.ajax({
			      url:base_url+'stock/add_type',
			      type:'POST',
			      processData:false,
				  contentType: false,
			      data:form_data,
			      success:function(response)
			      {
			      	var res = $.parseJSON(response);
			      	$('#my_csrf').val(res[0]);
			      	if(res[1]==2)
			      	{
				      	$('#ifnotify-warning').text(" Already exists");
				      	$('#ifnotify-warning').trigger('click');
			      	}
			      	else if(res[1]==1)
			      	{
				      	$('#ifnotify-success').text("Added");
				      	$('#ifnotify-success').trigger('click');
			        	$('#type_name').val("");
			        	$('#typeTable').DataTable().ajax.reload();
			        }
			      	else
			      	{
				      	$('#ifnotify-error').text("Unexpected Try again");
				      	$('#ifnotify-error').trigger('click');
				    }
			      }
			});//End of Ajax Request
			
		});

		$('#type-edit-modal').on('show.bs.modal',function(e){
			$('#etype-msg').html("");
			var type_id = e.relatedTarget.id
			$('#htype-id').val(type_id);
			var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
			var token_value  = $('#my_csrf').val();
			var form_data    = new FormData();
			form_data.append('type_id',type_id);
			form_data.append(token_name,token_value);
			$.ajax({
				url:base_url+'stock/get_types',
				type:'POST',
		      	processData:false,
			  	contentType: false,
				data:form_data,
				success:function(response)
				{
			      	var res = $.parseJSON(response);
			      	$('#my_csrf').val(res[0]);
			      	$('#etype-name').val("");
					$('#etype-name').val(res[1]);
				}
			});
		});

		$('#btn-update-type').on('click',function(e){
			var type_id   = $('#htype-id').val();
			var type_name = $('#etype-name').val();
			var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
			var token_value  = $('#my_csrf').val();
			var form_data    = new FormData();
			form_data.append('type_id',type_id);
			form_data.append('type_name',type_name);
			form_data.append(token_name,token_value);
			if(type_name=="")
			{
				$('#ifnotify-warning').text(" Provide Name for type");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			$.ajax({
				url:base_url+'stock/update_type',
				type:'POST',
		      	processData:false,
			  	contentType: false,
				data:form_data,
				success:function(response)
				{
					var res = $.parseJSON(response);
			      	$('#my_csrf').val(res[0]);
			      	if(res[1]==2)
			      	{
				      	$('#ifnotify-warning').text(" Already exists");
				      	$('#ifnotify-warning').trigger('click');
			      	}
			      	else if(res[1]==3)
			      	{
				      	$('#ifnotify-warning').text(" Nothing to Update");
				      	$('#ifnotify-warning').trigger('click');
			      	}
			      	else if(res[1]==1)
			      	{
				      	$('#ifnotify-success').text("Updated");
				      	$('#ifnotify-success').trigger('click');
			        	$('#typeTable').DataTable().ajax.reload();
				      	refresh_dropdown();
				    }
			      	else
			      	{
				      	$('#ifnotify-error').text("Unexpected Try again");
				      	$('#ifnotify-error').trigger('click');
				    }
				}
			});
		});
		
		// Deleting
		$('#typeTable').on('click','.ifdel',function(e){
		  e.preventDefault();
		  var href = $(this).attr('href');
		  if(typeof(href) != "undefined" && href !== null)
		  {
			  swal({
			      title:"Are you sure?",
			      text: "You will not be able to recover this data!",
			      type: "warning",
			      showCancelButton: true,
			      confirmButtonClass: 'btn-danger btn-md waves-effect waves-light',
			      confirmButtonColor: "#DD6B55",
			      confirmButtonText: "Yes, delete it!",
			      cancelButtonText: "No, cancel it!",
			      closeOnConfirm: true,
			      closeOnCancel: true
			  }, function (isConfirm) {
			      if (isConfirm) 
			      {
			    	window.location=href;
			      } 
			      else 
			      {
			          return false;
			      }
			  });
		  }
		});
	/*** Stock Type Section ENDS ***/

	/*** Stock Brand Section STARTS ***/
		$('#brandTable').DataTable({
	    	 "sAjaxSource": base_url+'stock/show_brands',
	    	 "pageLength": 10,
	    	 "order": [],
	    	 "responsive":true
	    });
		
		$('#brand_name').on('keypress',function(e){
			var key = e.which;
			if(key==13)
			{
				e.preventDefault();
				$('#btn-add-brand').trigger('click');
			}
		});

		$('#btn-add-brand').on('click',function(e){
			var brand_name = $('#brand_name').val();
			if(brand_name=="")
			{
				$('#ifnotify-warning').text(" Provide Name for brand");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
			var token_value  = $('#my_csrf').val();
			var form_data    = new FormData();
			form_data.append('brand_name',brand_name);
			form_data.append(token_name,token_value);
			$.ajax({
			      url:base_url+'stock/add_brand',
			      type:'POST',
			      processData:false,
				  contentType: false,
			      data:form_data,
			      success:function(response)
			      {
			      	var res = $.parseJSON(response);
			      	$('#my_csrf').val(res[0]);
			      	if(res[1]==2)
			      	{
				      	$('#ifnotify-warning').text(" Already exists");
				      	$('#ifnotify-warning').trigger('click');
			      	}
			      	else if(res[1]==1)
			      	{
				      	$('#ifnotify-success').text("Added");
				      	$('#ifnotify-success').trigger('click');
			        	$('#brand_name').val("");
			        	$('#brandTable').DataTable().ajax.reload();
			        	refresh_dropdown();
				    }
			      	else
			      	{
				      	$('#ifnotify-error').text("Unexpected Try again");
				      	$('#ifnotify-error').trigger('click');
				    }
			      }
			});//End of Ajax Request
			
		});

		$('#brand-edit-modal').on('show.bs.modal',function(e){
			$('#ebrand-msg').html("");
			var brand_id = e.relatedTarget.id
			$('#hbrand-id').val(brand_id);
			var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
			var token_value  = $('#my_csrf').val();
			var form_data    = new FormData();
			form_data.append('brand_id',brand_id);
			form_data.append(token_name,token_value);
			$.ajax({
				url:base_url+'stock/get_brands',
				type:'POST',
		      	processData:false,
			  	contentType: false,
				data:form_data,
				success:function(response)
				{
			      	var res = $.parseJSON(response);
			      	$('#my_csrf').val(res[0]);
			      	$('#ebrand-name').val("");
					$('#ebrand-name').val(res[1]);
				}
			});
		});

		$('#btn-update-brand').on('click',function(e){
			var brand_id   = $('#hbrand-id').val();
			var brand_name = $('#ebrand-name').val();
			var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
			var token_value  = $('#my_csrf').val();
			var form_data    = new FormData();
			form_data.append('brand_id',brand_id);
			form_data.append('brand_name',brand_name);
			form_data.append(token_name,token_value);
			if(brand_name=="")
			{
				$('#ifnotify-warning').text(" Provide Name for brand");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			$.ajax({
				url:base_url+'stock/update_brand',
				type:'POST',
		      	processData:false,
			  	contentType: false,
				data:form_data,
				success:function(response)
				{
					var res = $.parseJSON(response);
			      	$('#my_csrf').val(res[0]);
			      	if(res[1]==2)
			      	{
				      	$('#ifnotify-warning').text(" Already exists");
				      	$('#ifnotify-warning').trigger('click');
			      	}
			      	else if(res[1]==3)
			      	{
				      	$('#ifnotify-warning').text(" Nothing to Update");
				      	$('#ifnotify-warning').trigger('click');
			      	}
			      	else if(res[1]==1)
			      	{
				      	$('#ifnotify-success').text("Updated");
				      	$('#ifnotify-success').trigger('click');
			        	$('#brandTable').DataTable().ajax.reload();
				    }
			      	else
			      	{
				      	$('#ifnotify-error').text("Unexpected Try again");
				      	$('#ifnotify-error').trigger('click');
				    }
				}
			});
		});
		
		// Deleting
		$('#brandTable').on('click','.ifdel',function(e){
		  e.preventDefault();
		  var href = $(this).attr('href');
		  if(typeof(href) != "undefined" && href !== null)
		  {
			  swal({
			      title:"Are you sure?",
			      text: "You will not be able to recover this data!",
			      type: "warning",
			      showCancelButton: true,
			      confirmButtonClass: 'btn-danger btn-md waves-effect waves-light',
			      confirmButtonColor: "#DD6B55",
			      confirmButtonText: "Yes, delete it!",
			      cancelButtonText: "No, cancel it!",
			      closeOnConfirm: true,
			      closeOnCancel: true
			  }, function (isConfirm) {
			      if (isConfirm) 
			      {
			    	window.location=href;
			      } 
			      else 
			      {
			          return false;
			      }
			  });
		  }
		});
	/*** Stock Brand Section ENDS ***/


	/*** Stock Item Section STARTS ***/
		$('#stkItemsTable').DataTable({
			 "ajax": base_url+'stock/show_stock_items',
			 "pageLength": 1000,
	    	 "order": [],
	    });

		$(document).on('click','.sbyc',function(e){
			e.preventDefault();
			var sterm = $(this).text();
			var table = $('#stkItemsTable').DataTable();
			table.search(sterm).draw();
		});
		
		$('#btn-add-stk-items').on('click',function(e){
			var stk_category = $('#stk_category').val();
			var stk_barcode = $('#stk_barcode').val();
			var stk_name = $('#stk_name').val();
			var stk_pprice = $('#stk_pprice').val();
			var stk_pdate  	  = $('#stk_pdate').val();
			var stk_provider  = $('#stk_provider').val();
			var stk_sprice_b1 = $('#stk_sprice_b1').val();
			var stk_sprice_b2 = $('#stk_sprice_b2').val();
			var stk_sprice_b3 = $('#stk_sprice_b3').val();
			var stk_qty = $('#stk_qty').val();
			var stk_notify = $('#stk_notify').val();
			var stk_notes = $('#stk_notes').val();
			var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
			var token_value  = $('#my_csrf').val();
			if(stk_category=="")
			{
				$('#ifnotify-warning').text(" Provide category");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_barcode=="")
			{
				$('#ifnotify-warning').text(" Please provide barcode");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_name=="")
			{
				$('#ifnotify-warning').text(" Please provide name");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_pprice=="")
			{
				$('#ifnotify-warning').text(" Please provide purchase price");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_pdate=="")
			{
				$('#ifnotify-warning').text(" Please provide purchase date");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_provider=="")
			{
				$('#ifnotify-warning').text(" Please add provider");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_sprice_b1=="")
			{
				$('#ifnotify-warning').text(" Please provide selling price for branch 1");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_sprice_b2=="")
			{
				$('#ifnotify-warning').text(" Please provide selling price for branch 2");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_sprice_b3=="")
			{
				$('#ifnotify-warning').text(" Please provide selling price for branch 3");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_qty=="")
			{
				$('#ifnotify-warning').text(" Please provide quantity");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_notify=="")
			{
				stk_notify=0;
			}
			var form_data    = new FormData();
			form_data.append('stk_category',stk_category);
			form_data.append('stk_barcode',stk_barcode);
			form_data.append('stk_name',stk_name);
			form_data.append('stk_pprice',stk_pprice);
			form_data.append('stk_pdate',stk_pdate);
			form_data.append('stk_provider',stk_provider);
			form_data.append('stk_sprice_b1',stk_sprice_b1);
			form_data.append('stk_sprice_b2',stk_sprice_b2);
			form_data.append('stk_sprice_b3',stk_sprice_b3);
			form_data.append('stk_qty',stk_qty);
			form_data.append('stk_notify',stk_notify);
			form_data.append('stk_notes',stk_notes);
			form_data.append(token_name,token_value);
			$.ajax({
				url:base_url+'stock/add_stock_items',
				type:'POST',
				processData:false,
				contentType: false,
			    data:form_data,
				success:function(response)
				{
					var res = $.parseJSON(response);
					$('#my_csrf').val(res[0]);
					if(res[1]==1)
					{
				      	$('#ifnotify-success').text("Added");
				      	$('#ifnotify-success').trigger('click');
						$('#stk_category').val("");
						$('#stk_barcode').val("");
						$('#stk_name').val("");
						$('#stk_pprice').val("");
						$('#stk_pdate').val("");
						$('#stk_provider').val("");
						$('#stk_sprice_b1').val("");
						$('#stk_sprice_b2').val("");
						$('#stk_sprice_b3').val("");
						$('#stk_qty').val("");
						$('#stk_notify').val("");
						$('#stk_notes').val("");
			        }
					else if(res[1]==2)
					{
						$('#ifnotify-warning').text("Barcode already exists");
				      	$('#ifnotify-warning').trigger('click');
					}
					else if(res[1]==3)
					{
						$('#ifnotify-warning').text("Name already exist");
						$('#ifnotify-warning').trigger('click');
					}
					else if(res[1]==0)
					{
						$('#ifnotify-error').text("Error try again");
						$('#ifnotify-error').trigger('click');
					}
					$('#stkItemsTable').DataTable().ajax.reload();
				}
			});
		});

		$('#stk-item-edit-modal').on('show.bs.modal',function(e){
			var stk_id = e.relatedTarget.id;
			$('#e_stk_uid').val(stk_id);
			var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
			var token_value  = $('#my_csrf').val();
			var form_data    = new FormData();
			form_data.append('stk_id',stk_id);
			form_data.append(token_name,token_value);
			$.ajax({
				url:base_url+'stock/get_stock_items',
				type:'POST',
				processData:false,
				contentType: false,
				data:form_data,
				success:function(msg)
				{
					var res = $.parseJSON(msg);
					$('#my_csrf').val(res[0]);
					$('#e_stk_category').val(res[1]);
					$('#e_stk_barcode').val(res[2]);
					$('#e_stk_name').val(res[3]);
					$('#e_stk_pprice').val(res[4]);
					$('#e_stk_pdate').val(res[5]);
					$('#e_stk_sprice_b1').val(res[6]);
					$('#e_stk_sprice_b2').val(res[7]);
					$('#e_stk_sprice_b3').val(res[8]);
					$('#e_stk_qty').val(res[9]);
					$('#e_stk_notify').val(res[10]);
					$('#e_stk_notes').val(res[11]);
					$('#e_stk_provider').val(res[12]);
				}
			});
		});

		$('#btn-update-stk-items').on('click',function(e){	
			var stk_uid 	  = $('#e_stk_uid').val();
			var stk_category = $('#e_stk_category').val();
			var stk_barcode = $('#e_stk_barcode').val();
			var stk_name = $('#e_stk_name').val();
			var stk_pprice = $('#e_stk_pprice').val();
			var stk_pdate = $('#e_stk_pdate').val();
			var stk_provider = $('#e_stk_provider').val();
			var stk_sprice_b1 = $('#e_stk_sprice_b1').val();
			var stk_sprice_b2 = $('#e_stk_sprice_b2').val();
			var stk_sprice_b3 = $('#e_stk_sprice_b3').val();
			var stk_qty = $('#e_stk_qty').val();
			var stk_notify = $('#e_stk_notify').val();
			var stk_notes = $('#e_stk_notes').val();
			var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
			var token_value  = $('#my_csrf').val();
			if(stk_category=="")
			{
				$('#ifnotify-warning').text(" Provide category");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			// if(stk_type=="")
			// {
			// 	$('#ifnotify-warning').text(" Provide type");
			// 	$('#ifnotify-warning').trigger('click');
			// 	return false;
			// }
			if(stk_name=="")
			{
				$('#ifnotify-warning').text(" Please provide name");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_pprice=="")
			{
				$('#ifnotify-warning').text(" Please provide purchase price");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_pdate=="")
			{
				$('#ifnotify-warning').text(" Please provide purchase date");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_provider=="")
			{
				$('#ifnotify-warning').text(" Please add a provider");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_sprice_b1=="")
			{
				$('#ifnotify-warning').text(" Please provide selling price for branch 1");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_sprice_b2=="")
			{
				$('#ifnotify-warning').text(" Please provide selling price for branch 2");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_sprice_b3=="")
			{
				$('#ifnotify-warning').text(" Please provide selling price for branch 3");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_qty=="")
			{
				$('#ifnotify-warning').text(" Please provide quantity");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_notify=="")
			{
				stk_notify=0;
			}
			var form_data    = new FormData();
			form_data.append('stk_id',stk_uid);
			form_data.append('stk_category',stk_category);
			form_data.append('stk_barcode',stk_barcode);
			form_data.append('stk_name',stk_name);
			form_data.append('stk_pprice',stk_pprice);
			form_data.append('stk_pdate',stk_pdate);
			form_data.append('stk_provider',stk_provider);
			form_data.append('stk_sprice_b1',stk_sprice_b1);
			form_data.append('stk_sprice_b2',stk_sprice_b2);
			form_data.append('stk_sprice_b3',stk_sprice_b3);
			form_data.append('stk_qty',stk_qty);
			form_data.append('stk_notify',stk_notify);
			form_data.append('stk_notes',stk_notes);
			form_data.append(token_name,token_value);
			$.ajax({
				url:base_url+'stock/update_stock_items',
				type:'POST',
				processData:false,
				contentType: false,
				data:form_data,
				success:function(response)
				{
					var res = $.parseJSON(response);
					$('#my_csrf').val(res[0]);

					if(res[1]==1)
					{
						$('#ifnotify-success').text("Updated");
						$('#ifnotify-success').trigger('click');
					}
					else if(res[1]==2)
					{	
						$('#ifnotify-warning').text("Nothing to update");
				      	$('#ifnotify-warning').trigger('click');
					}
					else if(res[1]==3)
					{
						$('#ifnotify-warning').text("Barcode already exists");
				      	$('#ifnotify-warning').trigger('click');
					}
					else if(res[1]==4)
					{
						$('#ifnotify-warning').text("Name already exists");
				      	$('#ifnotify-warning').trigger('click');
					}
					else if(res[1]==0)
					{
						$('#ifnotify-error').text("Error try again");
						$('#ifnotify-error').trigger('click');
					}
					$('#stkItemsTable').DataTable().ajax.reload();
				}
			});
		});
		// Deleting
		$('#stkItemsTable').on('click','.ifdel',function(e){
		  e.preventDefault();
		  var href = $(this).attr('href');
		  if(typeof(href) != "undefined" && href !== null)
		  {
			  swal({
			      title:"Are you sure?",
			      text: "You will not be able to recover this data!",
			      type: "warning",
			      showCancelButton: true,
			      confirmButtonClass: 'btn-danger btn-md waves-effect waves-light',
			      confirmButtonColor: "#DD6B55",
			      confirmButtonText: "Yes, delete it!",
			      cancelButtonText: "No, cancel it!",
			      closeOnConfirm: true,
			      closeOnCancel: true
			  }, function (isConfirm) {
			      if (isConfirm) 
			      {
			    	window.location=href;
			      } 
			      else 
			      {
			          return false;
			      }
			  });
		  }
		});

		// STOCK IN
		$('#stk-add-qty-modal').on('show.bs.modal',function(e){
			var stk_id = e.relatedTarget.id;
			$('#stk_add_id').val(stk_id);
		});

		$('#btn-stk-add-qty').on('click',function(){
			var stk_qty      = $('#stk_add_qty').val();
			var stk_date     = $('#stk_add_qty_date').val();
			var stk_price    = $('#stk_add_qty_price').val();
			var stk_notes    = $('#stk_add_qty_notes').val();
			var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
			var token_value  = $('#my_csrf').val();
			var stk_id       = $('#stk_add_id').val();
			if(stk_qty=="")
			{
				$('#ifnotify-warning').text(" Add a quantity");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			var form_data    = new FormData();
			form_data.append('stk_id',stk_id);
			form_data.append('stk_qty',stk_qty);
			form_data.append('stk_date',stk_date);
			form_data.append('stk_price',stk_price);
			form_data.append('stk_notes',stk_notes);
			form_data.append(token_name,token_value);
			$.ajax({
				url:base_url+'stock/add_stk_qty',
				type:'POST',
				processData:false,
				contentType: false,
				data:form_data,
				success:function(response)
				{
					var res = $.parseJSON(response);
					$('#my_csrf').val(res[0]);

					if(res[1]==1)
					{
						$('#ifnotify-success').text("Updated");
						$('#ifnotify-success').trigger('click');
						$('#stk_add_qty').val("");
					}
					else 
					{
						$('#ifnotify-error').text("Error try again");
						$('#ifnotify-error').trigger('click');
					}
					$('#stkItemsTable').DataTable().ajax.reload();
				}
			});
		});

		// STOCK OUT
		$('#stk-less-qty-modal').on('show.bs.modal',function(e){
			var stk_id = e.relatedTarget.id;
			$('#stk_less_id').val(stk_id);
		});

		$('#btn-stk-less-qty').on('click',function(){
			var stk_qty      = $('#stk_less_qty').val();
			var reason       = $('#stk_less_reason').val();
			var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
			var token_value  = $('#my_csrf').val();
			var stk_id       = $('#stk_less_id').val();
			if(stk_qty=="")
			{
				$('#ifnotify-warning').text(" Add a quantity");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(reason=="")
			{
				$('#ifnotify-warning').text(" Reason required");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			var form_data    = new FormData();
			form_data.append('stk_id',stk_id);
			form_data.append('stk_qty',stk_qty);
			form_data.append('reason',reason);
			form_data.append(token_name,token_value);
			$.ajax({
				url:base_url+'stock/less_stk_qty',
				type:'POST',
				processData:false,
				contentType: false,
				data:form_data,
				success:function(response)
				{
					var res = $.parseJSON(response);
					$('#my_csrf').val(res[0]);

					if(res[1]==1)
					{
						$('#ifnotify-success').text("Updated");
						$('#ifnotify-success').trigger('click');
						$('#stk_less_qty').val("");
						$('#stk_less_reason').val("");
					}
					else if(res[1]==3)
					{
						$('#ifnotify-warning').text("Quantity should not exceed the available quantity");
						$('#ifnotify-warning').trigger('click');	
					}
					else 
					{
						$('#ifnotify-error').text("Error try again");
						$('#ifnotify-error').trigger('click');
					}
					$('#stkItemsTable').DataTable().ajax.reload();
				}
			});
		});

		// STOCK TRANSFER
		$('#stk-transfer-modal').on('show.bs.modal',function(e){
			$('#qty-tt').hide();
			var stk_id 		 = e.relatedTarget.id;
			$('#stk_trf_frm_id').val(stk_id);
			var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
			var token_value  = $('#my_csrf').val();
			var form_data    = new FormData();
			form_data.append('stk_id',stk_id);
			form_data.append(token_name,token_value);
			$.ajax({
				url:base_url+'stock/transfer_qty',
				type:'POST',
				processData:false,
				contentType: false,
				data:form_data,
				success:function(response)
				{
					var res = $.parseJSON(response);
					$('#my_csrf').val(res[0]);
					$('#stk').html(res[1]);
					$('#stk_trf_frm_qty').val(res[2]);
					$('#stk_trf_to_id').html("");
					$('#stk_trf_to_id').html(res[3]);
				}
			});
			
		});
		$("#stk_trf_to_id").autocomplete({
	       source: base_url+'stock/search_prd',
	       minLength: 2,
	       select: function (event, ui) {
	       		var stk_trf_to_id = $('#stk_trf_to_id').val();
	       		var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
	       		var token_value  = $('#my_csrf').val();
	       		var form_data    = new FormData();
	       		form_data.append('stk_trf_to_id',stk_trf_to_id);
	       		form_data.append(token_name,token_value);
	       		$.ajax({
	       			url:base_url+'stock/get_transfer_info',
	       			type:'POST',
	       			processData:false,
	       			contentType: false,
	       			data:form_data,
	       			success:function(response)
	       			{
	       				var res = $.parseJSON(response);
	       				$('#my_csrf').val(res[0]);
	       				$('#qty-tt').html('Quantity : '+res[1]).fadeIn('slow');
	       			}
	       		});
	       }
	    });
		

		$('#stk_base_qty1').on('keyup',function(e){
			conversion();
		});
		$('#stk_converted_qty1').on('keyup',function(e){
			conversion();
		});

		$('#stk_base_qty2').on('keyup',function(e){
			conversion();
		});

		$('#btn-stk-transfer').on('click',function(){
			var stk_trf_frm_id = $('#stk_trf_frm_id').val();
			var stk_trf_to_id = $('#stk_trf_to_id').val();
			var stk_converted_qty1 = $('#stk_converted_qty1').val();
			var stk_base_qty2 = $('#stk_base_qty2').val();
			var stk_converted_qty2 = $('#stk_converted_qty2').val();
			var reason       = 'Transfer';
			var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
			var token_value  = $('#my_csrf').val();
			if(stk_trf_frm_id==stk_trf_to_id)
			{
				$('#ifnotify-warning').text(" Cannot Make Transfer to Self");
				$('#ifnotify-warning').trigger('click');
				return false;	
			}
			if(stk_trf_to_id=="")
			{
				$('#ifnotify-warning').text(" Select An Item");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_converted_qty1=="")
			{
				$('#ifnotify-warning').text(" Enter Conversion Number");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(stk_base_qty2=="")
			{
				$('#ifnotify-warning').text(" Enter Quantity To Tranfer");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			var form_data    = new FormData();
			form_data.append('stk_trf_frm_id',stk_trf_frm_id);
			form_data.append('stk_trf_to_id',stk_trf_to_id);
			form_data.append('stk_base_qty2',stk_base_qty2);
			form_data.append('stk_converted_qty2',stk_converted_qty2);
			form_data.append('reason',reason);
			form_data.append(token_name,token_value);
			$.ajax({
				url:base_url+'stock/process_transfer',
				type:'POST',
				processData:false,
				contentType: false,
				data:form_data,
				success:function(response)
				{
					var res = $.parseJSON(response);
					$('#my_csrf').val(res[0]);
					if(res[1]==1)
					{
						$('#ifnotify-success').text("TRANSFERRED");
						$('#ifnotify-success').trigger('click');
						$('#stk_trf_to_id').val('');
						$('#qty-tt').hide();
						$('#stk_base_qty1').val('');
						$('#stk_converted_qty1').val('');
						$('#stk_base_qty2').val('');
						$('#stk_converted_qty2').val('');
						$('#stk-transfer-modal').modal('toggle');
					}
					else 
					{
						$('#ifnotify-error').text("Error try again");
						$('#ifnotify-error').trigger('click');
					}
					$('#stkItemsTable').DataTable().ajax.reload();
				}
			});
		});
		// STOCK WS
		$('#stk-ws-qty-modal').on('show.bs.modal',function(e){
			var stk_id = e.relatedTarget.id;
			$('#stk_ws_id').val(stk_id);
		});

		$('#btn-stk-ws-qty').on('click',function(){
			var stk_qty      = $('#stk_ws_qty').val();
			var reason       = $('#stk_ws_reason').val();
			var token_name   = "<?php echo $this->security->get_csrf_token_name();?>";
			var token_value  = $('#my_csrf').val();
			var stk_id       = $('#stk_ws_id').val();
			if(stk_qty=="")
			{
				$('#ifnotify-warning').text(" Add a quantity");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			if(reason=="")
			{
				$('#ifnotify-warning').text(" Reason required");
				$('#ifnotify-warning').trigger('click');
				return false;
			}
			var form_data    = new FormData();
			form_data.append('stk_id',stk_id);
			form_data.append('stk_qty',stk_qty);
			form_data.append('reason',reason);
			form_data.append(token_name,token_value);
			$.ajax({
				url:base_url+'stock/ws_less_stk_qty',
				type:'POST',
				processData:false,
				contentType: false,
				data:form_data,
				success:function(response)
				{
					// alert(response);
					var res = $.parseJSON(response);
					$('#my_csrf').val(res[0]);

					if(res[1]==1)
					{
						$('#ifnotify-success').text("Updated");
						$('#ifnotify-success').trigger('click');
						$('#stk_less_qty').val("");
						$('#stk_less_reason').val("");
					}
					else if(res[1]==3)
					{
						$('#ifnotify-warning').text("Quantity should not exceed the available quantity");
						$('#ifnotify-warning').trigger('click');	
					}
					else 
					{
						$('#ifnotify-error').text("Error try again");
						$('#ifnotify-error').trigger('click');
					}
					$('#stkItemsTable').DataTable().ajax.reload();
				}
			});
		});

		// View All
		$('#modal-stock-in').on('show.bs.modal',function(e){
			var trn = e.relatedTarget.id;
			$.ajax({
				url:base_url+'stock/get_si_trn',
				type:'POST',
				data:{trn:trn},
				success:function(msg)
				{
					$('#stock-in-mbody').html("");
					$('#stock-in-mbody').html(msg);
				}
			});
		});

		$('#modal-stock-out').on('show.bs.modal',function(e){
			var trn = e.relatedTarget.id;
			$.ajax({
				url:base_url+'stock/get_so_trn',
				type:'POST',
				data:{trn:trn},
				success:function(msg)
				{
					$('#stock-out-mbody').html("");
					$('#stock-out-mbody').html(msg);
				}
			});
		});

		$('.rev-si').on('click',function(e){
			e.preventDefault();
			var trn = $(this).attr('id');
			swal({
			  title:"Are you sure?",
			    text: "This will reverse your transaction! \n This process is irrecoverable",
			    type: "warning",
			    showCancelButton: true,
			    confirmButtonClass: 'btn-danger btn-md waves-effect waves-light',
			    confirmButtonColor: "#DD6B55",
			    confirmButtonText: "Yes, do it!",
			    cancelButtonText: "No, cancel it!",
			    closeOnConfirm: true,
			    closeOnCancel: true
			}).then(function() {
			  	$.ajax({
			  		url:base_url+'stock/rev_si_trn',
			  		type:'POST',
			  		data:{trn:trn},
			  		success:function(msg)
			  		{
			  			location.reload();
			  		}
			  	});
			}, function(dismiss) {
			  return false;
			});
		});

		$('.rev-so').on('click',function(e){
			e.preventDefault();
			var trn = $(this).attr('id');
			swal({
			  title:"Are you sure?",
			    text: "This will reverse your transaction! \n This process is irrecoverable",
			    type: "warning",
			    showCancelButton: true,
			    confirmButtonClass: 'btn-danger btn-md waves-effect waves-light',
			    confirmButtonColor: "#DD6B55",
			    confirmButtonText: "Yes, do it!",
			    cancelButtonText: "No, cancel it!",
			    closeOnConfirm: true,
			    closeOnCancel: true
			}).then(function() {
			  	$.ajax({
			  		url:base_url+'stock/rev_so_trn',
			  		type:'POST',
			  		data:{trn:trn},
			  		success:function(msg)
			  		{
			  			location.reload();
			  		}
			  	});
			}, function(dismiss) {
			  return false;
			});
		});
	/*** Stock Item Section ENDS ***/

	

	
});// End Ready Function