<%= stylesheet_link_tag 'wk-time', :plugin => "redmine_wktime" %>
<%= javascript_include_tag 'bootstrap', :plugin => "redmine_wktime" %>
<% typeHash = { 'C'  => l(:label_txn_contra), 'P' =>  l(:label_txn_payment),  } %>
<style>
	.tooltip{
		position:absolute;z-index:1070;display:block;font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-style:normal;font-weight:400;line-height:1.42857143;line-break:auto;text-align:left;text-align:start;text-decoration:none;text-shadow:none;text-transform:none;letter-spacing:normal;word-break:normal;word-spacing:normal;word-wrap:normal;white-space:normal;font-size:12px;filter:alpha(opacity=0);opacity:0
	}
	.tooltip.in{filter:alpha(opacity=90);opacity:.9}
	.tooltip.bottom{padding:5px 0;margin-top:3px}
	.tooltip.bottom .tooltip-arrow{top:0;left:50%;margin-left:-5px;border-width:0 5px 5px;border-bottom-color:#000}
	.tooltip-inner{max-width:500px;padding:3px 8px;color:#fff;text-align:center;background-color:#000;border-radius:4px}
	.tooltip-arrow{position:absolute;width:0;height:0;border-color:transparent;border-style:solid}
</style>
<table class="list time-entries" style="width: <%= width %>; float:left;">
  <thead>
    <tr>
			<%= sort_header_tag('trans_type', :caption => l(:field_type), class: "lbl-txt-align", title: "") %>
			<%= sort_header_tag('trans_date', :caption => l(:label_date), class: "lbl-txt-align", title: "") %>
			<th class="lbl-txt-align"><%= l(:label_particulars) %></th>
			<th class="lbl-txt-align"><%= l(:label_debit)  %></th>
			<th class="lbl-txt-align"><%= l(:label_credit) %></th>
			<th></th>
    </tr>
  </thead>
   <tbody>
	<%
		crTotal = 0
		dbTotal =0
		openingBalance = 0
		openingBalHash = getOpeningBalHash
		@transEntries.each do |entry|
			getAmount(entry)
			@selectedLedgerEntries.each do |trans|
				 unless trans[1].blank?
					if trans[2] == 'c'
						crTotal = crTotal + trans[1]
					else
						dbTotal = dbTotal + trans[1]
					end
				end
			end
			%>
			<tr class="trlist" data-toggle="tooltip" data-container="body" data-placement="bottom" title="<%=entry.comment%>">
				<td class="lbl-txt-align"><%=h transTypeHash[entry.trans_type]  %></td>
				<td class="lbl-txt-align"><%=h entry.trans_date %></td>
				<td class="lbl-txt-align"><%=h @partLedgerName %></td>
				<td class="lbl-txt-align"><%=h  "%.2f" % @dbAmount unless @dbAmount.blank? %></td>
				<td class="lbl-txt-align"><%=h  "%.2f" % @crAmount unless @crAmount.blank? %></td>
				<td class="lbl-txt-align"><%= link_to image_tag('edit.png'), {:controller => controller.controller_name, :action => 'edit', :txn_id => entry.id, id: entry.id, :tab => controller.controller_name},   :title => l(:button_edit) %>
				<% if validateERPPermission("A_ACC_PRVLG") %>
					<%= link_to image_tag('delete.png'), {:controller => controller.controller_name, :action => 'destroy', :txn_id => entry.id, id: entry.id, :tab => controller.controller_name},  :data => {:confirm => l(:text_are_you_sure)}, :method => :delete, :title => l(:button_delete) %>
				<% end %>
				</td>
			</tr>
		<% end %>
		<% 	unless @selectedLedger.blank? || (incomeLedgerTypes.include? @selectedLedger.ledger_type) || (expenseLedgerTypes.include? @selectedLedger.ledger_type)
				openingBalance = openingBalHash[@selectedLedger.name] unless openingBalHash.blank? || openingBalHash[@selectedLedger.name].blank?
				isSubCr = isSubtractCr(@selectedLedger.ledger_type)
				if isSubCr
					currentBal = dbTotal - crTotal
					#closeBal = currentBal + openingBalance
				else
					currentBal = crTotal - dbTotal
				end
				closeBal = currentBal + openingBalance

		%>
			<tr>
				<th class="lbl-txt-align"><%= %></th>
				<th class="lbl-txt-align"><%=  %></th>
				<th class="lbl-txt-align"><%=h l(:label_opening_balance) %>:</th>
				<% if (isSubCr && openingBalance > 0) || (!isSubCr && openingBalance < 0) %>
					<th class="lbl-txt-align"><%=  "%.2f" % openingBalance.abs  %></th>
					<th class="lbl-txt-align"><%=  %></th>
				<% else %>
					<th class="lbl-txt-align"><%=   %></th>
					<th class="lbl-txt-align"><%=  "%.2f" % openingBalance.abs %></th>
				<% end %>
				<th></th>
			</tr>
			<tr>
				<th class="lbl-txt-align"><%= %></th>
				<th class="lbl-txt-align"><%=  %></th>
				<th class="lbl-txt-align"><%=h l(:label_current_total) %>:</th>
				<th class="lbl-txt-align"><%=  "%.2f" % dbTotal  %></th>
				<th class="lbl-txt-align"><%=  "%.2f" % crTotal %></th>
				<th></th>
			</tr>
			<tr>
				<th class="lbl-txt-align"><%= %></th>
				<th class="lbl-txt-align"><%=  %></th>
				<th class="lbl-txt-align"><%=h l(:label_closing_balance) %>:</th>
				<% if (isSubCr && closeBal > 0) || (!isSubCr && closeBal < 0) %>
					<th class="lbl-txt-align"><%=  "%.2f" % closeBal.abs  %></th>
					<th class="lbl-txt-align"><%=  %></th>
				<% else %>
					<th class="lbl-txt-align"><%=   %></th>
					<th class="lbl-txt-align"><%=  "%.2f" % closeBal.abs %></th>
				<% end %>
				<th></th>
			</tr>
		<% end %>
   </tbody>
</table>