MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: /home/cafsindia/cloud_cafsinfotech_in/application/views/payroll_wizard/manage_bk.php
<?php 
	$this->load->view("partial/header"); 
	$access_data    = $this->session->userdata('access_data');
	$access_add     = (int)$access_data[$controller_name]['access_add'];
	$access_update  = (int)$access_data[$controller_name]['access_update'];
	$access_delete  = (int)$access_data[$controller_name]['access_delete'];
	$access_search  = (int)$access_data[$controller_name]['access_search']; 
	$access_export  = (int)$access_data[$controller_name]['access_export'];
	$access_import  = (int)$access_data[$controller_name]['access_import'];
	$page_name      = ucwords(str_replace("_"," ",$controller_name));

	$all_formula = [];
	$all_formula = array_merge($formula_master,$all_formula);
	$all_formula = array_merge($formula_earning,$all_formula);
	$all_formula = array_merge($formula_deduction,$all_formula);
?>
<style>
	body{
		overflow-x: hidden;
	}
	th{
	    position: sticky;
	    top: 0;
	    z-index: 1;
	    background-color: #f2f2f2;
	}
	.select2-selection--multiple{
		overflow-y: scroll !important;
		height: 60px;
		border-radius: 10px;
	}
	table#calculator {
		border-spacing: 10px 8px;
		border-collapse: inherit;
		width: 100%;
		text-align: center;
	}
	table#calculator > tbody>tr>td:hover{
		background-color: #f9f9f9;
	}
		/* //.btn:hover, .btn:focus, .btn.focus */
	table#calculator > tbody>tr>td{
		padding: 1px 15px;
		border: 1px solid #CCCCCC;
		font-size: 25px;
		background-color: #f2f2f2;
		border-radius: 4px;
		cursor: pointer !important;
		box-shadow: 0 2px 2px 0 rgb(0 0 0 / 14%), 0 1px 5px 0 rgb(0 0 0 / 12%) !important;
	}
	.popover-content {
		padding: 9px 14px;
		height: auto !important;
		word-break: break-all !important;
	}
</style>
<div class='row' style="margin: 0px;max-height:500px;">
	<div class='col-md-12 col-xs-12' style='margin-bottom: 15px;'>
		<h1 class='page_txt'><?php echo $page_name;?></h1>
	</div>
    <div class='col-md-12 col-xs-12' style='margin-bottom: 15px;'>
		<div class="row form-inline">
			<div class="form-group">
				<?php 
					echo form_label($this->lang->line('report_list'), 'report_list', array('class' => 'required'));
					echo form_dropdown(array( 'name' => 'report_list', 'id' => 'report_list', 'class' => 'form-control input-sm select2'), $report_list);
				?>
			</div>
			<div class="form-group" style="display:none;">
				<?php
					echo form_label($this->lang->line('category_for'), 'category_for', array('class' => 'required'));
                    echo form_dropdown(array( 'name' => 'category_for[]', 'multiple id' =>'category_for', 'class' => 'form-control input-sm select2'), $category_for);
                    echo "<label><input name='category_for_select' id='category_for_select' type='checkbox' style='cursor:pointer;'> Select All</label>";
				?>
			</div>
            <div class="form-group" style="display:none;">
				<?php
					echo form_label($this->lang->line('duration_type'), 'duration_type', array('class' => 'required'));
					$duration_type_array =  array(""=>"---- Duration Type ----",1=>"Single Month",2=>"Yearly");
					echo form_dropdown(array( 'name' => 'duration_type', 'id' => 'duration_type', 'class' => 'form-control input-sm select2'), $duration_type_array);
				?>
			</div>
            <div class="form-group" style="display:none;">
				<?php
					echo form_label($this->lang->line('report_type'), 'report_type', array('class' => 'required'));
					$report_type_array =  array(""=>"---- Report Type ----",1=>"Consolidate",2=>"Detail",3=>"Only Master");
					echo form_dropdown(array( 'name' => 'report_type', 'id' => 'report_type', 'class' => 'form-control input-sm select2'), $report_type_array);
				?>
			</div>
			<div class="form-group" style="display:none;">
				<?php
					echo form_label($this->lang->line('group_by'), 'group_by');
                    echo form_dropdown(array( 'name' => 'group_by[]', 'multiple id' =>'group_by', 'class' => 'form-control input-sm select2'), $group_by);
				?>
			</div>
			<div class="form-group" style="display:none;" >
				<label id='sub_total_div'>
					<input name='sub_total' id='sub_total' type="checkbox" checked> Sub Total
				</label>
			</div>
			<div class="form-group" style="display:none;">
				<?php
					echo form_label("Display Format", 'display_format');
					$display_format_array =  array(""=>"---- Display Format ----",1=>"Horizonal",2=>"Vertical");
                    echo form_dropdown(array( 'name' => 'display_format', 'id' =>'display_format', 'class' => 'form-control input-sm select2'), $display_format_array);
				?>
			</div>
            <div class="form-group" style="display:none;">
				<?php
					echo form_label("From Month", 'from_month', array('class' => 'required'));
					echo form_input(array("name"=>"from_month", "id"=>"from_month","value"=>$date,"placeholder"=>"From date","class"=>"form-control input-sm datepicker_month"));
				?>
			</div> 
            <div class="form-group" style="display:none;">
				<?php
					echo form_label("To Month", 'to_month', array('class' => 'required'));
					echo form_input(array("name"=>"to_month", "id"=>"to_month","value"=>$date,"placeholder"=>"From date","class"=>"form-control input-sm datepicker_month"));
				?>
			</div>
			<div class="form-group" style="display:none;">
                <?php
                    echo form_label($this->lang->line('report_title'), 'report_title', array('class' => 'required'));
                    echo form_input(array( 'name' => 'report_title', 'id' => 'report_title', 'class' => 'form-control input-sm', 'placeholder'=>$this->lang->line('report_title'),'value' => ''));
                ?>
            </div>
		</div>
    </div>
	<div class='col-md-12 col-xs-12' style='margin-bottom: 30px;display:none;' id="report_tab_holder">
		<div class="row">
			<div class="col-md-4">
				<ul class="nav nav-tabs" data-tabs="tabs" id="input_tab">
					<li class="active" role="presentation">
						<a data-toggle="tab" data_id="master_tab" href="#master_tab">Master</a>
					</li>
					<li role="presentation">
						<a data-toggle="tab" data_id="earning_tab" href="#earning_tab">Earning</a>
					</li>
					<li role="presentation">
						<a data-toggle="tab" data_id="deduction_tab" href="#deduction_tab">Deduction</a>
					</li>
					<li role="presentation">
						<a data-toggle="tab" data_id="formula_tab" href="#formula_tab">Formula</a>
					</li>
				</ul>
				<div class="tab-content">
					<div class="tab-pane fade in active" id="master_tab">
						<div style='padding:4px;background-color:#f2f2f2;color:#000000;'>
							<input id="master_search" type="text" placeholder="Search.." style="width:100%" class='form-control input-sm'>
						</div>
						<div style="max-height:350px;overflow:auto;">
							<table class='table table-hover table-striped'>
								<thead style="background-color:#f2f2f2;color:#000000;">
									<tr>
										<th>
											<input  type='checkbox' id='master_all' value='master_all' style='cursor:pointer;'>
										</th>
										<th>Name</th>
										<th>Type</th>
									</tr>
								</thead>
								<tbody id="master_body">
									<?php 
										foreach($master as $master_info){
											$prime_form_id     = $master_info['prime_form_id'];
											$label_name        = $master_info['label_name'];
											$view_name         = $master_info['view_name'];
											$transaction_type  = (int)$master_info['transaction_type'];
											$trans_name        = $master_info['trans_name'];
											$trans_color       ="";
											if($transaction_type === 2){
												$trans_color  = "#e3f7e4";
											}else
											if($transaction_type === 3){
												$trans_color  = "#f1dadc";
											}else
											if($transaction_type === 4){
												$trans_color  = "#f5e6d2";
											}

											
											if(($label_name !== "employee_code") && ($label_name !== "emp_name")){
												$master_id_info = "MASTER::$prime_form_id";
												echo "<tr style='background-color:$trans_color;cursor:pointer;'>
														<td>
															<input  type='checkbox' id='$master_id_info' value='$master_id_info' style='cursor:pointer;'>
														</td>
														<td>$view_name</td>
														<td>$trans_name</td>
													</tr>";
											}
										}
									?>
								</tbody>
							</table>
						</div>
					</div>
					<div class="tab-pane fade" id="earning_tab">
						<div style='padding:4px;background-color:#f2f2f2;color:#000000;'>
							<input id="earning_search" type="text" placeholder="Search.." style="width:100%" class='form-control input-sm'>
						</div>
						<div style="max-height:350px;overflow:auto;">
							<table class='table table-hover table-striped'>
								<thead style="background-color:#f2f2f2;color:#000000;">
									<tr>
										<th>
											<input  type='checkbox' id='earning_all' value='earning_all' style='cursor:pointer;'>
										</th>
										<th>Name</th>
										<th>Type</th>
									</tr>
								</thead>
								<tbody id="earning_body">
									<?php 
										foreach($earning as $earning_info){
											$prime_form_id     = $earning_info['prime_form_id'];
											$label_name        = $earning_info['label_name'];
											$view_name         = $earning_info['view_name'];
											$transaction_type  = (int)$earning_info['transaction_type'];
											$trans_name        = $earning_info['trans_name'];
											$gross_check       = (int)$earning_info['gross_check'];
											$earning_id_info = "EARNING::$prime_form_id";

											$trans_color       ="";
											if($gross_check === 1){
												$trans_name = "Include for Gross";
												$trans_color  = "#e3f7e4";
											}
											echo "<tr style='background-color:$trans_color;cursor:pointer;'>
													<td>
														<input  type='checkbox' id='$earning_id_info' value='$earning_id_info' style='cursor:pointer;'>
													</td>
													<td>$view_name</td>
													<td>$trans_name</td>
												</tr>";
										}
									?>
								</tbody>
							</table>
						</div>
					</div>
					<div class="tab-pane fade" id="deduction_tab">
						<div style='padding:4px;background-color:#f2f2f2;color:#000000;'>
							<input id="deduction_search" type="text" placeholder="Search.." style="width:100%" class='form-control input-sm'>
						</div>
						<div style="max-height:350px;overflow:auto;">
							<table class='table table-hover table-striped'>
								<thead style="background-color:#f2f2f2;color:#000000;">
									<tr>
										<th>
											<input  type='checkbox' id='deduction_all' value='deduction_all' style='cursor:pointer;'>
										</th>
										<th>Name</th>
										<th>Type</th>
									</tr>
								</thead>
								<tbody id="deduction_body">
									<?php 
										foreach($deduction as $deduction_info){
											$prime_form_id     = $deduction_info['prime_form_id'];
											$label_name        = $deduction_info['label_name'];
											$view_name         = $deduction_info['view_name'];
											$transaction_type  = (int)$deduction_info['transaction_type'];
											$trans_name        = $deduction_info['trans_name'];
											$deduction_check   = (int)$deduction_info['deduction_check'];
											$deduction_id_info = "DEDUCTION::$prime_form_id";

											$trans_color       ="";
											if($deduction_check === 1){
												$trans_name = "Include for Gross";
												$trans_color  = "#f1dadc";
											}
											echo "<tr style='background-color:$trans_color;cursor:pointer;'>
													<td>
														<input  type='checkbox' id='$deduction_id_info' value='$deduction_id_info' style='cursor:pointer;'>
													</td>
													<td>$view_name</td>
													<td>$trans_name</td>
												</tr>";
										}
									?>
								</tbody>
							</table>
						</div>
					</div>
					<div class="tab-pane fade" id="formula_tab">
						<div class="row" style="margin:0px;padding:10px;">
							<div class="form-group">
								<input type='hidden' id="formula_mode" name="formula_mode" value=""/>
								<input type='hidden' id="formula_id" name="formula_id" value=""/>
								<?php
									echo form_label("Formula Name", 'formula_name', array('class' => 'required'));
									echo form_input(array( 'name' => 'formula_name', 'id' => 'formula_name', 'class' => 'form-control input-sm', 'placeholder'=>"Formula Name",'value' => ''));
								?>
							</div>
							<div class="form-group">
								<?php
									echo form_label("Formula For", 'formula_for', array('class' => 'required'));
									$formula_for =  array(""=>"---- Formula For  ----",1=>"Master",2=>"Earning",3=>"Deduction"); //
									echo form_dropdown(array( 'name' => 'formula_for', 'id' =>'formula_for', 'class' => 'form-control input-sm select2'), $formula_for);
								?>
							</div>
							<div class="form-group">
								<?php
									echo form_label("Master", 'formula_master', array('class' => 'required'));
									echo form_dropdown(array( 'name' => 'formula_master', 'id' =>'formula_master', 'class' => 'form-control input-sm select2'), $formula_master);
								?>
							</div>
							<div class="form-group">
								<?php
									echo form_label("Earning", 'formula_earning', array('class' => 'required'));
									echo form_dropdown(array('name' => 'formula_earning', 'id' =>'formula_earning', 'class' => 'form-control input-sm select2'), $formula_earning);
								?>
							</div>
							<div class="form-group"> 
								<?php
									echo form_label("Deduction", 'formula_deduction', array('class' => 'required'));
									echo form_dropdown(array( 'name' => 'formula_deduction', 'id' =>'formula_deduction', 'class' => 'form-control input-sm select2'), $formula_deduction);
								?>
							</div>
							<div class="form-group">
								<textarea name='formula' id='formula' class='form-control' rows='4'  placeholder='Query Column'></textarea>
							</div>
							<div class="form-group">
								<div class="row" style="margin:0px;">
									<div class="col-md-6">
										<table id="calculator">
											<tr>
												<td id="+">+</td>
												<td id="-">-</td>
												<td id="*">*</td>
											</tr>
											<tr>
												<td id="/">/</td>
												<td id="(">(</td>
												<td id=")">)</td>
											</tr>
										</table>
									</div>
									<div class="col-md-6" style='text-align:right;'>
										<button class="btn btn-primary btn-sm" id="formula_submit">Add new field</button>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="col-md-8">
				<div class="row" style="max-height:450px;overflow:auto;" id="select_holder"> 
					<table class='table table-hover table-striped' id="select_table"> 
						<thead style="background-color:#f2f2f2;color:#000000;">
							<tr>
								<th>Sort Order</th>
								<th>Type</th>
								<th>Name</th>
								<th>View name </th>
								<th style='text-align:center;'>Total</th>
								<th style='text-align:center;'>Formula</th>
								<th style='text-align:center;'>Edit</th>
								<th style='text-align:center;'>Remove</th>
							</tr>
						</thead>
						<tbody id="selected_body" class='sortable default_table' >
						</tbody>
					</table>
				</div>
			</div>
		</div>
	</div>
	<div class='col-md-12 col-xs-12' style='margin-bottom: 15px;display:none;' id="report_submit_holder">
		<div class="row">
			<div class="col-md-6">
				<!-- <button class="btn btn-danger btn-sm" id="report_cancel">Cancel</button> -->
			</div>
			<div class="col-md-6" style='text-align:right;'>
				<button class="btn btn-primary btn-sm" id="report_submit">Add/Update</button>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
	$(document).ready(function (){
		const allowed_option = ["+","-","*","/","(",")"];
		const ignore_option  = [".+", ".-", ".*", "./", ".)", ".(", "+.", "-.", "*.", "/.", ").", "(.", "++", "+-", "+*", "+/", "+)", "--", "-+", "-*", "-/", "-)", "**", "*+", "*-", "*/", "*)", "//", "/+", "/-", "/*", "/)", "(+", "(-", "(/", "0(", "1(", "2(", "3(", "4(", "5(", "6(", "7(", "8(", "9(", "a(", "b(", "c(", "d(", "e(", "f(", "g(", "h(", "i(", "j(", "k(", "l(", "m(", "n(", "o(", "p(", "q(", "r(", "s(", "t(", "u(", "v(", "w(", "x(", "y(", "z(", "0(", ")1", ")2", ")3", ")4", ")5", ")6", ")7", ")8", ")9", ")a", ")b", ")c", ")d", ")e", ")f", ")g", ")h", ")i", ")j", ")k", ")l", ")m", ")n", ")o", ")p", ")q", ")r", ")s", ")t", ")u", ")v", ")w", ")x", ")y", ")z", "()",];
		var selected_id    = [];
		var view_name_info = [];
		var new_field_info = [];
 		var master         = <?php echo json_encode($master); ?>;
		var earning        = <?php echo json_encode($earning); ?>;
		var deduction      = <?php echo json_encode($deduction); ?>;
 		var all_formula    = <?php echo json_encode($all_formula); ?>;
		console.log(all_formula);

		call_select();
		$(function (){
			$(".datepicker").datetimepicker({
				format: 'DD-MM-YYYY',
			});
			$(".datepicker_month").datetimepicker({
				format: 'MM-YYYY',
			});
			$(".datepicker_year").datetimepicker({
				format: 'YYYY',
			});
		});
		$('body').on('click',function(e){
			$('[data-toggle=popover]').each(function () {
				if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
					$(this).popover('hide');
				}
			});
		});

		$("#category_for_select").click(function(){
			if($("#category_for_select").is(':checked') ){
				$("#category_for > option").prop("selected","selected");
				$("#category_for").trigger("change");
			}else{
				$("#category_for > option").removeAttr("selected");
				$("#category_for").trigger("change");
			}
			$('#category_for option').filter(function(){
				return !this.value || $.trim(this.value).length == 0;
			}).remove();
		});
		
		$('#report_title,#category_for,#duration_type,#report_type,#from_month,#to_month,#group_by,#sub_total_div,#display_format').parent().hide();
		$('#report_tab_holder,#report_submit_holder').hide();
		$("#report_list").change(function(){
			clear_error()
			$('#category_for,#duration_type').parent().show();
		});		
		$("#duration_type").change(function(){
			clear_error();
			$("#report_type").val("");
			$("#from_month").val("");
			$("#to_month").val("");
			$("#group_by").val("");
			$("#display_format").val("");
			$('#sub_total').prop('checked', false);

			$('#report_type,#from_month,#to_month,#group_by,#sub_total_div,#display_format').parent().hide();
			$('#report_tab_holder').hide();

			$('#report_type option[value="1"]').attr('disabled',false);
			$('#report_type option[value="3"]').attr('disabled',false);
			var duration_type = $("#duration_type").val();
			if(parseInt(duration_type) === 2){
				$('#report_type option[value="1"]').attr('disabled',true);
				$('#report_type option[value="3"]').attr('disabled',true);
			}
			$('#report_type').parent().show();
			call_select();
		});
		
		$("#report_type").change(function(){
			$("#group_by").val("");
			$("#display_format").val("");
			$('#sub_total').prop('checked', false);
			call_select();
			clear_error()
			var duration_type = $("#duration_type").val();
			var report_type   = $("#report_type").val();
			$('#from_month,#to_month,#group_by,#sub_total_div,#display_format').parent().hide();
			$('#report_tab_holder').hide();

			var report_list = $("#report_list").val();
			if(report_list === "NEW"){
				$('#report_title').parent().show();
			}

			$('#input_tab li > a').parent().removeClass('active')
			$('#master_tab,#earning_tab,#deduction_tab,#formula_tab').removeClass('active in');			
			$('#input_tab li > a[data_id=master_tab],#input_tab li > a[data_id=earning_tab],#input_tab li > a[data_id=deduction_tab],#input_tab li > a[data_id=formula_tab]').parent().addClass('hide');
			if((parseInt(duration_type) === 1) && (parseInt(report_type) === 1)){ // Single Month & Consolidate
				$('#report_tab_holder').show();
				$('#from_month,#to_month,#group_by').parent().show();
				$('#input_tab li > a[data_id=earning_tab],#input_tab li > a[data_id=deduction_tab],#input_tab li > a[data_id=formula_tab]').parent().removeClass('hide');
				$('#input_tab li > a[data_id=earning_tab]').parent().removeClass('hide').addClass('active');
				$('#earning_tab').addClass('active in')

				var myRows    = [];
				var $headers  = $("#select_table th");
				var $rows     = $("#select_table tbody tr").each(function(index) {
					var prime_form_id =  $(this).find("td:eq(0) input[name='prime_form_id']").val();
					var id_info = prime_form_id.split("::");
					var from    = id_info[0];
					if(from === "MASTER"){
						document.getElementById(prime_form_id).checked = false;
					}else{
						myRows.push(prime_form_id);
					}
				});
				document.getElementById("master_all").checked = false;
				selected_id = myRows
				var view_info = {master:master,earning:earning,deduction:deduction,new_field_info:new_field_info,selected_id:selected_id,view_name_info:view_name_info,call_from:"1"}
				view_selected(view_info);
			}else
			if((parseInt(duration_type) === 1) && (parseInt(report_type) === 2)){ // Single Month & Detail
				$('#report_tab_holder').show();
				$('#from_month,#group_by').parent().show();
				$('#input_tab li > a[data_id=master_tab],#input_tab li > a[data_id=earning_tab],#input_tab li > a[data_id=deduction_tab],#input_tab li > a[data_id=formula_tab]').parent().removeClass('hide');
				$('#input_tab li > a[data_id=master_tab]').parent().removeClass('hide').addClass('active');
				$('#master_tab').addClass('active in')

			}else
			if((parseInt(duration_type) === 1) && (parseInt(report_type) === 3)){ // Single Month & Only Mater
				$('#report_tab_holder').show();
				$('#input_tab li > a[data_id=master_tab]').parent().removeClass('hide');
				$('#input_tab li > a[data_id=master_tab]').parent().removeClass('hide').addClass('active');
				$('#master_tab').addClass('active in')

				var myRows    = [];
				var $headers  = $("#select_table th");
				var $rows     = $("#select_table tbody tr").each(function(index) {
					var prime_form_id =  $(this).find("td:eq(0) input[name='prime_form_id']").val();
					var id_info = prime_form_id.split("::");
					var from    = id_info[0];
					if(from === "MASTER"){
						myRows.push(prime_form_id);
					}else{
						document.getElementById(prime_form_id).checked = false;
					}
				});
				document.getElementById("master_all").checked    = false;
				document.getElementById("earning_all").checked   = false;
				document.getElementById("deduction_all").checked = false;
				//#master_all,#earning_all,#deduction_all
				selected_id = myRows
				var view_info = {master:master,earning:earning,deduction:deduction,new_field_info:new_field_info,selected_id:selected_id,view_name_info:view_name_info,call_from:"1"}
				view_selected(view_info);
			}else
			if((parseInt(duration_type) === 2) && (parseInt(report_type) === 2)){ // Yearly & Detail
				$('#report_tab_holder').show();
				$('#from_month,#to_month,#group_by,#display_format').parent().show();
				$('#input_tab li > a[data_id=earning_tab],#input_tab li > a[data_id=deduction_tab],#input_tab li > a[data_id=formula_tab]').parent().removeClass('hide');
				$('#input_tab li > a[data_id=earning_tab]').parent().removeClass('hide').addClass('active');
				$('#earning_tab').addClass('active in')

				var myRows    = [];
				var $headers  = $("#select_table th");
				var $rows     = $("#select_table tbody tr").each(function(index) {
					var prime_form_id =  $(this).find("td:eq(0) input[name='prime_form_id']").val();
					var id_info = prime_form_id.split("::");
					var from    = id_info[0];
					if(from === "MASTER"){
						document.getElementById(prime_form_id).checked = false;
					}else{
						myRows.push(prime_form_id);
					}
				});
				document.getElementById("master_all").checked = false;
				
				selected_id = myRows

				var view_info = {master:master,earning:earning,deduction:deduction,new_field_info:new_field_info,selected_id:selected_id,view_name_info:view_name_info,call_from:"1"}
				view_selected(view_info);
				/* UDY *******************
					SHOULD'T ENABLE MASTER TAB FOR // Yearly & Detail 
					 - if enable in excel Components same value repet for all month
				*/
			}
		});

		$("#group_by").change(function(){
			$("#display_format").val("");
			$('#sub_total').prop('checked', false);
			call_select();
			clear_error()
			var duration_type  = $("#duration_type").val();
			var report_type    = $("#report_type").val();
			var group_by_input = $("#group_by").val();
			if(group_by_input === null){
				$('#display_format').parent().hide();
			}else{
				group_by           = group_by_input.filter(function(v){return v!==''});
				let group_len      = group_by.length;
				if(((parseInt(duration_type) === 1) && (parseInt(report_type) === 1) && (group_len === 1 ) || ((parseInt(duration_type) === 2) && (parseInt(report_type) === 2)))){
					$('#display_format').parent().show();
				}else{
					$('#display_format').parent().hide();
				}
				if((parseInt(duration_type) === 1) && (parseInt(report_type) === 2) && (group_len > 0 )){
					$('#sub_total_div').parent().show();
				}else{
					$('#sub_total_div').parent().hide();
				}
			}
		});

		$("#master_search").on("keyup", function(){
			var value = $(this).val().toLowerCase();
			$("#master_body tr").filter(function() {
				$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
			});
		});

		$("#earning_search").on("keyup", function() {
			var value = $(this).val().toLowerCase();
			$("#earning_body tr").filter(function() {
				$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
			});
		});

		$("#deduction_search").on("keyup", function() {
			var value = $(this).val().toLowerCase();
			$("#deduction_body tr").filter(function() {
				$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
			});
		});
		
		$('#master_all,#earning_all,#deduction_all').click(function(event){
			var process_id   = event.target.id
			var process_list = false;
			var process_mode = "";
			if(process_id){
				if(process_id=== "master_all"){
					process_list = master;
					process_mode = "MASTER::";
				}else
				if(process_id=== "earning_all"){
					process_list = earning;
					process_mode = "EARNING::";
				}else
				if(process_id=== "deduction_all"){
					process_list = deduction;
					process_mode = "DEDUCTION::";
				}
			}

			var check_uncheck = false;
			if($(event.target).is(":checked")) {
				check_uncheck = true
			}

			if(process_list){
				$.map(process_list, function(val,i){
					var id_info = process_mode+val.prime_form_id;
					if((val.label_name !== "employee_code") && (val.label_name !== "emp_name")){
						document.getElementById(id_info).checked = check_uncheck;
						if(check_uncheck){
							var result = selected_id.filter(function(elem){ return elem === id_info;});
							if(result.length === 0){
								if(!($.inArray(id_info, selected_id) !== -1)){
									selected_id.push(id_info)
								}
							}
						}else{
							var sel_result = selected_id.filter(function(elem){ return elem != id_info;});
							selected_id = sel_result;
							delete view_name_info[id_info]
						}
					}
				})
			}
			var view_info = {master:master,earning:earning,deduction:deduction,new_field_info:new_field_info,selected_id:selected_id,view_name_info:view_name_info,call_from:"1"}
			view_selected(view_info);
		})

		$('#master_body tr,#earning_body tr,#deduction_body tr').click(function(event){
			var checkbox = $(this).find("input[type='checkbox']");
			var col_id   = event.target.id;
			if($(event.target).is(":checked")){
				if(!($.inArray(col_id, selected_id) !== -1)){
					selected_id.push(col_id);
				}
			}else{
				var result = selected_id.filter(function(elem){ return elem != col_id;});
				selected_id = result;
				delete view_name_info[col_id]
			}

			if(event.target.type !== 'checkbox'){
				$(':checkbox', this).trigger('click');
			}
			var view_info = {master:master,earning:earning,deduction:deduction,new_field_info:new_field_info,selected_id:selected_id,view_name_info:view_name_info,call_from:"1"}
			view_selected(view_info);
		});

		$('#report_submit').click(function(event){
			if(selected_id.length === 0){
				toastr.error("Please select field atleast one field");
			}else{
				const not_allowed = ["name","sort_order", "edit", "remove","formula"];
				var myRows    = [];
				var $headers  = $("#select_table th");
				var $rows     = $("#select_table tbody tr").each(function(index) {
					$cells        = $(this).find("td");
					myRows[index] = {};
					$cells.each(function(cellIndex) {
						var col_name = $($headers[cellIndex]).html().toLowerCase();
						if(col_name){
							col_name = col_name.trim().toLowerCase().replace(/ /g, "_")
							var td_data = $(this).html();
							if(col_name === "sort_order"){
								$(this).find("input[type='hidden']").each(function(){
									myRows[index][$(this).attr('name')] = $(this).val()
								});
							}else
							if(col_name === "view_name"){
								td_data = $(this).find("input[type='text']").val();
							}else
							if(col_name === "total"){
								if(td_data !== "-"){
									var isChecked = $(this).find("input:checkbox").is(':checked');
									td_data = false
									if(isChecked){
										td_data = true
									}
								}
							}
							if(!($.inArray(col_name, not_allowed) !== -1)){
								myRows[index][col_name] = td_data
							}
						}
					});
				});

				clear_error()
				var can_process    = true;
				var report_list    = $("#report_list").val();
				var category_for   = $("#category_for").val();
				var duration_type  = $("#duration_type").val();
				var report_type    = $("#report_type").val();
				var group_by_input = $("#group_by").val();
				group_by           = group_by_input.filter(function(v){return v!==''});
				var display_format = $("#display_format").val();
				var from_month     = $("#from_month").val();
				var to_month       = $("#to_month").val();
				var report_title   = $("#report_title").val();
				var sub_total      = false;
				if($('#sub_total').is(":checked")){
					sub_total = true
				}

				if(report_list === ""){
					can_process = false
					$('#report_list').parent().append("<div class='val_error' style='color:red;'>Please select Report name or new</div>");
				}

				if(category_for === null){
					can_process = false
					$('#category_for').parent().append("<div class='val_error' style='color:red;'>Please select atleast one category</div>");
				}else{
					if(category_for.length === 0){
						can_process = false
						$('#category_for').parent().append("<div class='val_error' style='color:red;'>Please select atleast one category</div>");
					}
				}

				if(display_format === ""){
					var group_len = 0;
					if(group_by){
						group_len = group_by.length;
					}
					
					if(((parseInt(duration_type) === 1) && (parseInt(report_type) === 1) && (group_len === 1 ) || ((parseInt(duration_type) === 2) && (parseInt(report_type) === 2)))){
						can_process = false
						$('#display_format').parent().append("<div class='val_error' style='color:red;'>Please select display formate</div>");
					}
					
					if(parseInt(duration_type) === 1){
						display_format = 1
					}else
					if(parseInt(duration_type) === 2){
						display_format = 2
					}
				}

				if(duration_type === ""){
					can_process = false
					$('#duration_type').parent().append("<div class='val_error' style='color:red;'>Please select duration type</div>");
				}else{
					if(report_type === ""){
						can_process = false
						$('#duration_type').parent().append("<div class='val_error' style='color:red;'>Please select duration type</div>");
					}else{
						var duration_type = parseInt(duration_type);
						var report_type   = parseInt(report_type);
						if((duration_type === 1) && (report_type === 1)){
							if(from_month == ""){
								can_process = false
								$('#from_month').parent().append("<div class='val_error' style='color:red;'>Please select duration type</div>");
							}
							if(to_month == ""){
								can_process = false
								$('#to_month').parent().append("<div class='val_error' style='color:red;'>Please select duration type</div>");
							}
							if(report_title == ""){
								can_process = false
								$('#report_title').parent().append("<div class='val_error' style='color:red;'>Please select duration type</div>");
							}
						}else
						if((duration_type === 1) && (report_type === 2)){
							to_month = "";
							if(from_month == ""){
								can_process = false
								$('#from_month').parent().append("<div class='val_error' style='color:red;'>Please select duration type</div>");
							}
							if(report_title == ""){
								can_process = false
								$('#report_title').parent().append("<div class='val_error' style='color:red;'>Please select duration type</div>");
							}
						}else
						if((duration_type === 2) && (report_type === 2)){
							if(from_month == ""){
								can_process = false
								$('#from_month').parent().append("<div class='val_error' style='color:red;'>Please select duration type</div>");
							}
							if(to_month == ""){
								can_process = false
								$('#to_month').parent().append("<div class='val_error' style='color:red;'>Please select duration type</div>");
							}
							if(report_title == ""){
								can_process = false
								$('#report_title').parent().append("<div class='val_error' style='color:red;'>Please select duration type</div>");
							}
						}
					}
				}
				
				// if((duration_type === 1) && (report_type === 1)){
				// 	let group_len     = group_by.length -1;
				// 	var total_col = group_len +myRows.length
				// 	if(total_col >= 220){
				// 		can_process = false
				// 		toastr.warning('only 220 column allowed');
				// 	}
				// 	var from_month_info = from_month.split("-");

				// 	var pro_from_month = from_month_info[1]+"-"+from_month_info[0]+"-01";
				// 	pro_from_month = new Date(pro_from_month);

				// 	var to_month_info = to_month.split("-");
				// 	var pro_to_month = to_month_info[1]+"-"+to_month_info[0]+"-01";
				// 	pro_to_month = new Date(pro_to_month);
				// 	var total_month = diff_months(from_month, to_month);
				// 	can_process = false
				// 	// var from_month     = $("#from_month").val();
				// 	// var to_month       = $("#to_month").val();
					
				// }

				if(!can_process){
					$("html, body").animate({ scrollTop: 0 }, "slow");
					toastr.warning('Please provide valid input');
				}else{
					var form_data = {
						report_list    : report_list,
						category_for   : category_for,
						duration_type  : duration_type,
						report_type    : report_type,
						group_by       : group_by,
						sub_total      : sub_total,
						display_format : display_format,
						from_month     : from_month,
						to_month       : to_month,
						report_title   : report_title,
						select_info    : myRows,
					}
					form_data = JSON.stringify(form_data);
					$.ajax({
						url: '<?php echo site_url("$controller_name/process_report"); ?>',
						data: {form_data},	
						type: 'POST',
						success:function(data){
							var rslt = JSON.parse(data);
							if(!rslt.success){
								toastr.error(rslt.message);
							}else{
								var report_data = rslt.rslt
								var file = new Blob(report_data, {type:'application/vnd.Test.xlsx'});
								var a = document.createElement("a");
						        a.href = URL.createObjectURL(file);
								var file_name = "RW_"+moment().format('YYYYMMDDHHMMSS')+".xls";
						        a.download = file_name;
								a.click();
							}
						},
					});
				}
			}
		});

		$('#formula_master,#formula_earning,#formula_deduction').parent().hide();
		$("#formula_for").change(function(){
			clear_error()
			var formula_name = $("#formula_name").val();
			var formula_for  = $("#formula_for").val();
			$('#formula_master,#formula_earning,#formula_deduction').parent().hide();
			$("#formula_master,#formula_earning,#formula_deduction").val("");
			call_select(); 
			if(formula_name === ""){
				toastr.error("Please new field enter name");
				$('#formula_name').parent().append("<div class='val_error' style='color:red;'>Please new field enter name</div>");
				$("#formula_for").val("");
				call_select(); 
			}else{
				if(parseInt(formula_for) === 1){
					$('#formula_master').parent().show();
				}else
				if(parseInt(formula_for) === 2){
					$('#formula_earning').parent().show();
				}else
				if(parseInt(formula_for) === 3){
					$('#formula_deduction').parent().show();
				}
			}
		});

		$("#formula_master").change(function(){
			formula_onchange(allowed_option,ignore_option,"#formula_master");
		});
		$("#formula_earning").change(function(){
			formula_onchange(allowed_option,ignore_option,"#formula_earning");
		});
		$("#formula_deduction").change(function(){
			formula_onchange(allowed_option,ignore_option,"#formula_deduction");
		});

		$('#calculator tbody tr td').click(function(event){
			var formula_info = $("#formula").val();
			formula_info     = formula_info.trim().toLowerCase().replace(/ /g,"");
			var operator     = event.target.id
			if(formula_info){
				var check_str = formula_info.substr(-1);
				var check_forumal = check_str+operator;
				if($.inArray(check_forumal, ignore_option) !== -1){
					operator = "";
				}
			}
			var formula = formula_info+operator;
			$('#formula').val(formula);
			
		});

		$(".default_table" ).sortable({
			update: function( event, ui ){
				var table_idsInOrder = [];
				$('#selected_body tr').each(function() {
					if($(this).attr('id')){
						table_idsInOrder.push($(this).attr('id'));
					}
				});
				selected_id = table_idsInOrder;
				var view_info = {master:master,earning:earning,deduction:deduction,new_field_info:new_field_info,selected_id:selected_id,view_name_info:view_name_info,call_from:"3"}
				view_selected(view_info);
			},connectWith: '.default_table'
		});

		$('#formula_submit').click(function(event){
			clear_error()
			var myRows    = [];
			var $headers  = $("#select_table th");
			var $rows     = $("#select_table tbody tr").each(function(index) {
				var prime_form_id =  $(this).find("td:eq(0) input[name='prime_form_id']").val();
				if(prime_form_id){
					myRows.push(prime_form_id);
				}
			});
			selected_id = myRows

			var formula_mode  = $("#formula_mode").val();
			var formula_id    = $("#formula_id").val();
			var formula_name  = $("#formula_name").val();
			var formula_info  = $("#formula").val();
			if((formula_name !== "") && (formula_info !== "")){
				formula_info         = formula_info.trim().toLowerCase().replace(/ /g,"");
				var chk_sel_col      = formula_info.replace(/[^a-zA-Z0-9._]/g, ",");
				var open_bracket     = formula_info.split("(").length - 1;
				var close_bracket    = formula_info.split(")").length - 1;
				var start_char       = formula_info.slice(0, 1);
				var end_char         = formula_info.substr(-1);
				var allowed_charater = /^[A-Za-z0-9_.\/+*()-]+$/;

				const start_end_ignore = ["+","-","*","/","_"];
				var isExist = []
				$.map(ignore_option, function(val,i){
					if(formula_info.includes(val)){
						isExist.push(val);
					}
				});

				var chk_sel_col_info = chk_sel_col.split(",");
				var inValid_col      = []
				$.map(chk_sel_col_info, function(val,i){
					if(!all_formula[val]){
						inValid_col.push(val);
					}
				});

				if($.inArray(start_char, start_end_ignore) !== -1){
					$('#formula').parent().append("<div class='val_error' style='color:red;'>Please check formula may contain incorrect operator at start</div>");
				}else
				if($.inArray(end_char, start_end_ignore) !== -1){
					$('#formula').parent().append("<div class='val_error' style='color:red;'>Please check formula may contain incorrect operator at end</div>");
				}else
				if(isExist.length > 0){
					$('#formula').parent().append("<div class='val_error' style='color:red;'>Please check formula may contain incorrect operator position</div>");
				}else
				if(inValid_col.length > 0){
					$('#formula').parent().append("<div class='val_error' style='color:red;'>Please remove invalid column name in formula</div>");
				}else
				if(allowed_charater.test(formula_info) == false) {
					$('#formula').parent().append("<div class='val_error' style='color:red;'>Allow charater are +,-,*,/,(,),aplha,number</div>");
				}else
				if(open_bracket !== close_bracket){
					$('#formula').parent().append("<div class='val_error' style='color:red;'>Please check open and close brackets</div>");
				}else{
					var label_name    = formula_name.trim().toLowerCase().replace(/ /g, "_");
					var form_id   = new_field_info.length;
					if(formula_mode === "UPD"){
						form_id = formula_id
					}
					var new_field = {"prime_form_id":form_id,"label_name":label_name,"view_name":formula_name,"transaction_type":0,"trans_name":"NEW",field_type:0,formula_info:formula_info}; 
					new_field_info[form_id] = new_field;
					if(formula_mode !== "UPD"){
						var new_id = "NEW::"+form_id
						selected_id.push(new_id);
					}
					$("#formula_mode,#formula_id,#formula_name,#formula_for,#formula_master,#formula_earning,#formula_deduction,#formula").val("");
					call_select();
					$('#formula_master,#formula_earning,#formula_deduction').parent().hide();
					var view_info = {master:master,earning:earning,deduction:deduction,new_field_info:new_field_info,selected_id:selected_id,view_name_info:view_name_info,call_from:"1"}
					view_selected(view_info);
				}
			}else{
				if(formula_name === ""){
					$('#formula_name').parent().append("<div class='val_error' style='color:red;'>Please enter formula name</div>");
				}
				if(formula_info === ""){
					$('#formula').parent().append("<div class='val_error' style='color:red;'>Please enter formula</div>");
				}
			}
		});
	});

	// function diff_months(dt2, dt1){
	// 	var diff =(dt2.getTime() - dt1.getTime()) / 1000;
	// 	diff /= (60 * 60 * 24 * 7 * 4);
	// 	return Math.abs(Math.round(diff));
	// }

	function call_select(){
		$(function(){
			$('.select2').select2({
				placeholder: '---- Select ----',
				allowClear: true,
				tags: true,
			});
			$('.select2-tags').select2({
				tags: true,
				allowClear: true,
				tokenSeparators: [',']
			});
		});
		$('#group_by').on("select2:select", function (evt) {
			var element = evt.params.data.element;
			var $element = $(element); 			  
			$element.detach();
			$(this).append($element);
			$(this).trigger("change");
		});
	}

	function clear_error(){
		var error_msg = $(".val_error").is(":visible");
		if(error_msg){ $(".val_error").remove(); }
	}

	function formula_onchange(allowed_option,ignore_option,change_id){
		if(change_id){
			clear_error();
			var formula_info   = $("#formula").val();
			formula_info        = formula_info.trim().toLowerCase().replace(/ /g,"");
			var formula_master = $(change_id).val();
			if(formula_info !== ""){
				var check_str = formula_info.substr(-1);
				if(!($.inArray(check_str, allowed_option) !== -1)){
					$(change_id).val("");
					formula_master = "";
					$('#formula').parent().append("<div class='val_error' style='color:red;'>Please check formula or arithmetic operator</div>");
				}

				if((check_str === ")") && (formula_master !== "")){
					var check_forumal = check_str+formula_master.substr(-1);
					if($.inArray(check_forumal, ignore_option) !== -1){
						$(change_id).val("");
						formula_master = "";
						$('#formula').parent().append("<div class='val_error' style='color:red;'>Please check formula or arithmetic operator</div>");
					}
				}

				var check_str2 = formula_info.substr(-2);
				if($.inArray(check_str2, ignore_option) !== -1){
					$(change_id).val("");
					formula_master = "";
					$('#formula').parent().append("<div class='val_error' style='color:red;'>Please check formula or arithmetic operator</div>");
				}
			}
			var formula = formula_info+formula_master;
			$('#formula').val(formula);
		}
	}

	function view_selected(view_info){
		var master         = view_info.master;
		var earning        = view_info.earning;
		var deduction      = view_info.deduction;
		var call_from      = view_info.call_from;

		selected_id        = view_info.selected_id;
		new_field_info     = view_info.new_field_info;	
		view_name_info     = view_info.view_name_info;
		$('#report_submit_holder').hide();
		if(selected_id.length === 0){
			$('#selected_body').html("");
		}else{
			$('#report_submit_holder').show();
			var tr_info = "";
			$.map(selected_id, function(val,i){
				const id_info   = val.split("::");
				var from        = id_info[0];
				var from_index  = id_info[1];
				var can_process = false
				var row_info    = [];
				var type        = "";
				var edit_td     = "<td style='padding:4px;text-align:center;'>-</td>";
				if((from === "MASTER") && (master[from_index])){
					can_process = true;
					type        = "Master";
					row_info    = master[from_index];
				}else
				if((from === "EARNING") && (earning[from_index])){
					can_process = true;
					type        = "Earning";
					row_info    = earning[from_index];
				}else
				if((from === "DEDUCTION") && (deduction[from_index])){
					can_process = true;
					type        = "Deduction";
					row_info    = deduction[from_index];
				}else
				if((from === "NEW") && (new_field_info[from_index])){
					can_process = true;
					type        = "New";
					row_info    = new_field_info[from_index];
					edit_td = "<td style='padding:4px;text-align:center;'><button class='btn btn-xs btn-edit edit' id="+val+">Edit</button></td>";
				}

				if(can_process){
					var sort_order        = i+1
					var prime_form_id     = row_info.prime_form_id;
					var label_name        = row_info.label_name;
					var name              = row_info.view_name;
					var view_name         = row_info.view_name;
					var transaction_type  = row_info.transaction_type;
					var trans_name        = row_info.trans_name;
					var field_type        = row_info.field_type;
					var formula_info      = "";
					if(from === "NEW"){
						formula_info  = row_info.formula_info
						name          = "-"
					}

					if((view_name_info)&&(view_name_info[val])){
						view_name = view_name_info[val];
					}

					var total_td   = "<td style='padding:4px;text-align:center;'>-</td>";
					var formula_td = "<td style='padding:4px;text-align:center;'>-</td>";
					if(field_type === 2){
						total_td = "<td style='padding:4px;text-align:center;'><input  type='checkbox' id="+val+" checked style='cursor:pointer;' /></td>";
					}
					if(from === "NEW"){
						total_td   = "<td style='padding:4px;text-align:center;'><input  type='checkbox' id="+val+" checked style='cursor:pointer;'/></td>";
						formula_td = "<td style='padding:4px;text-align:center;'> <span style='cursor:pointer;' class='btn btn-xs btn-default' data-container='body' data-toggle='popover' data-placement='bottom' data-content='"+formula_info+"'><i class='fa fa-info-circle' aria-hidden='true'></i></span></td>";
					}

					tr_info += "<tr style='cursor:pointer;' id="+val+"><td style='padding:4px;'>"+sort_order+"<input type='hidden' value='"+sort_order+"' name='sort_name'><input type='hidden' value='"+val+"' name='prime_form_id'><input type='hidden' value='"+label_name+"' name='label_name'><input type='hidden' value='"+field_type+"' name='field_type'><input type='hidden' value='"+formula_info+"' name='formula_info'></td><td style='padding:4px;'>"+type+"</td><td style='padding:4px;'>"+name+"</td><td style='padding:4px;'><input type='text' style='width:100%' class='form-control input-sm edit_input' id="+val+" value='"+view_name+"' maxlength='100'/></td>"+total_td + formula_td+edit_td+"<td style='padding:4px;text-align:center;' class='remove' ><button class='btn btn-xs btn-edit remove' id="+val+">Delete</button></td></tr>";
				}
				if(parseInt(call_from) === 1){
					var scrollBottom = Math.max($('#select_table').height() - $('#select_holder').height(), 0);
					$('#select_holder').scrollTop(scrollBottom);
				}
			});
			$('#selected_body').html(tr_info);
		}

		$('[data-toggle="popover"]').popover()

		$('#selected_body tr td input.edit_input').on('keyup', function (event) {
			var edit_id    = event.target.id;
			var edit_value = event.target.value;
			if(edit_id){
				var view_name_length  = edit_value.length;
				if(view_name_length > 100){
					toastr.error("Max char length is 100");
				}else{
					view_name_info[edit_id] = edit_value;
					const id_info  = edit_id.split("::");
					var from       = id_info[0];
					var from_index = id_info[1];
					if(from === "NEW"){
						var edit_info  = new_field_info[from_index];
						edit_info.view_name  = edit_value
						edit_info.label_name = edit_value
						new_field_info[from_index] = edit_info
					}
				}
			}
		});
		
		$('#selected_body tr td button.edit').click(function(event){
			var edit_id    = event.target.id
			if(edit_id){
				const id_info  = edit_id.split("::");
				var from       = id_info[0];
				var from_index = id_info[1];
				var edit_info  = new_field_info[from_index];
				$("#formula_mode").val("UPD");
				$("#formula_id").val(from_index);
				$("#formula_name").val(edit_info.view_name);
				$("#formula").val(edit_info.formula_info);

				$('#input_tab li > a').parent().removeClass('active')
				$('#master_tab,#earning_tab,#deduction_tab,#formula_tab').removeClass('active in');
				$('#input_tab li > a[data_id=formula_tab]').parent().removeClass('hide').addClass('active');
				$('#formula_tab').addClass('active in')
			}
		});

		$('#selected_body tr td button.remove').click(function(event){
			var remove_id = event.target.id
			if(remove_id){
				document.getElementById(remove_id).checked = false;
				var result = selected_id.filter(function(elem){ return elem != remove_id; });
				selected_id = result;
				delete view_name_info[remove_id]
				console.log(selected_id);
				var view_info = {master:master,earning:earning,deduction:deduction,new_field_info:new_field_info,selected_id:selected_id,view_name_info:view_name_info,call_from:"2"}
				view_selected(view_info);
			}
		});
	}
</script>
<?php $this->load->view("partial/footer"); ?>