<%= javascript_include_tag 'wkpayroll', :plugin => "redmine_wktime" %>
<% settingCurreny = Setting.plugin_redmine_wktime['wktime_currency'] %>
<%
	userId = params[:user_id]
	isdisabled = true
	salEntryByTypeHash = {'a' => [], 'b'=> [], 'd'=> [], 'c' => []}
	actualSalValHash = Hash.new{|hsh,key| hsh[key] = {} }
	@userSalaryEntries.each do |entry|
		isOverride = entry.user_salary_component_id.blank? ? false : true
		if isOverride
			dependent_id = entry.usc_dependent_id.blank? ? 0 : entry.usc_dependent_id
			factor = entry.usc_factor.blank? ? 0 : (entry.usc_factor)
			salary_type = entry.usc_salary_type.present? ? entry.usc_salary_type : entry.sc_salary_type
		else
			key = entry.id.to_s + "_" + entry.user_id.to_s
			factor = @userSalCompHash[key].present? ? (@userSalCompHash[key]).factor : 0.00
			dependent_id = @userSalCompHash[key].present? ? (@userSalCompHash[key]).dependent_id : 0
			salary_type = @userSalCompHash[key].present? ? (@userSalCompHash[key]).sc_salary_type : null
		end
		actualSalValHash[entry.sc_component_type].store entry.sc_id, @userSalHash[entry.user_id][entry.sc_id] unless @userSalHash[entry.user_id].blank?
		salEntryByTypeHash[entry.sc_component_type] << [entry, isOverride, dependent_id, factor, salary_type] unless entry.sc_component_type.blank? || entry.sc_component_type == 'r'
	end
	grossVal = "%.2f" % (actualSalValHash['a'].values.sum + actualSalValHash['b'].values.sum)
%>

<div>
	<%= title "User Salary Settings" %>
	<div>
	<table>
		<tr>
			<th align="left"><%=l(:field_user)%></th>
			<td><%=h @userSalaryEntries[0].firstname + " " + @userSalaryEntries[0].lastname %></td>
		</tr>
	</table>
	<div><br/>

	<% if actualSalValHash.present?%>
		<%= form_tag({:controller => 'wkpayroll', :action => 'updateUserSalary'}, :method => :post, :id => 'query_form') do %>
			<%=h hidden_field_tag('user_id', userId) %>
			<%= hidden_field_tag('gross', grossVal) %>
			<fieldset style="width: 94%;margin:1px 34px 0px 1px;background-color:#F6F6F6;float:right"><legend><b><%=l(:label_basic)%></b></legend>
				<table "width:100%; border-collapse: collapse;">
					<thead>
						<tr>
							<th class="lbl-txt-align" style="padding:0 5px 0 5px;"><%= l(:label_override_common_setting) %></th>
							<th class="lbl-txt-align" style="padding:0 5px 0 5px;"><%= l(:label_salary_component) %></th>
							<th class="lbl-txt-align" style="padding:0 5px 0 5px;"><%= l(:label_salary_type) %></th>
							<th class="lbl-txt-align" style="padding:0 5px 0 5px;"><%= l(:label_factor) %></th>
							<th class="lbl-txt-align"><%= l(:label_actual) %></th>
						</tr>
					</thead>
					<%= render :partial => 'wkpayroll/user_sal_setting_comp_row', locals: { salCompTypeValues: salEntryByTypeHash['b'], actualSalVal: actualSalValHash['b']}%>
				</table>
			</fieldset>
				&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp

			<fieldset class="box tabular" style="display: inline-block; vertical-align: top; width:45%;"><legend><b><%=l(:label_allowances)%></b></legend>
				<table style="width:100%;border-collapse: collapse;" >
					<thead>
						<tr>
							<th class="lbl-txt-align"><%= l(:label_override_common_setting) %></th>
							<th class="lbl-txt-align"><%= l(:label_salary_component) %></th>
							<th class="lbl-txt-align"><%= l(:label_dependent_field) %></th>
							<th class="lbl-txt-align"><%= l(:label_factor) %></th>
							<th class="lbl-txt-align"><%= l(:label_actual) %></th>
						</tr>
					</thead>
					<%= render :partial => 'wkpayroll/user_sal_setting_comp_row', locals: { salCompTypeValues: salEntryByTypeHash['a'], actualSalVal: actualSalValHash['a']}%>
					<tr>
						<th></th>
						<th></th>
						<th></th>
						<th class="lbl-txt-align"><%= l(:label_gross) %></th>
						<th align='right' style="border-bottom: solid thin;border-top: solid thin;"><%= settingCurreny %><%= grossVal %></th>
					</tr>
				</table>
			</fieldset>
				&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp


			<fieldset class="box tabular" style="display: inline-block; vertical-align: top; width:45%; "><legend><b><%=l(:label_deduction)%></b></legend>
				<table style="width:100%;border-collapse: collapse;">
					<thead>
						<tr>
							<th class="lbl-txt-align"><%= l(:label_override_common_setting) %></th>
							<th class="lbl-txt-align"><%= l(:label_salary_component) %></th>
							<th class="lbl-txt-align"><%= l(:label_dependent_field) %></th>
							<th class="lbl-txt-align"><%= l(:label_factor) %></th>
							<th class="lbl-txt-align"><%= l(:label_actual) %></th>
						</tr>
					</thead>
					<%= render :partial => 'wkpayroll/user_sal_setting_comp_row', locals: { salCompTypeValues: salEntryByTypeHash['d'], actualSalVal: actualSalValHash['d']}%>
					<tr>
						<th></th>
						<th></th>
						<th></th>
						<th class="lbl-txt-align"><%= l(:label_deduction) %></th>
						<th align='right' style="border-bottom: solid thin;border-top: solid thin;"><%= settingCurreny %><%=  "%.2f" % actualSalValHash['d'].values.sum %></th>
					</tr>
				</table>
			</fieldset><br>
			<div style="padding-right:50px;">
				<table width="15%" align="right" style="border-collapse: collapse;border: 0px solid black;">
					<tr>
						<td style="float:left;"><b><%=l(:label_net)%></b>
						<td style="text-align:right; border-left: solid 0px black;"><b><%= settingCurreny %> <%=h "%.2f" % (actualSalValHash['b'].values.sum + actualSalValHash['a'].values.sum - actualSalValHash['d'].values.sum) %></b></td>
					</tr>
				</table>
			</div>
			<br>
			<% if getTaxSettings('tax_rule').present? %>
				<div style="width: 98%;margin:1px 34px 0px 1px;float:right">
					<fieldset class="collapsible">
						<legend onclick="toggleFieldset(this);" class="icon icon-collapsed"><h3><%= l(:label_income_tax_settings) %></h3></legend>
    					<div style="display: none;">
							<%= render partial: "wkrule/incometax/#{getTaxSettings('tax_rule').to_s}", locals: { action_type: "userSettings",
									userId: params[:user_id]} %>
    					</div>
					</fieldset>
				</div>
			<% end %>
			<% if validateERPPermission('A_PAYRL') %>
				<span style="margin-left:25.0em;">
					<%= submit_tag l(:button_save), :id => 'wk_user_sal_setting_save', :name => 'user_sal_save', :disabled => false %>
					<%= submit_tag l(:button_save_with_tax), :id => 'wk_user_sal_setting_save_with_tax', :name => 'user_sal_save_with_tax', :disabled => false if isCalculateTax %>
				</span>
			<% end %>
		<% end %>
	<% end %>
</div>