
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="/struts-dojo-tags" prefix="sx"%>  
<%@ taglib prefix="sj" uri="/struts-jquery-tags"  %>
<script type="text/javascript">
if (typeof window.easysContextPath === 'undefined') {
  window.easysContextPath = "${pageContext.request.contextPath}";
}
if (typeof window.easysStrutsActionUrl !== 'function') {
  window.easysStrutsActionUrl = function(actionName) {
    var c = (typeof window.easysContextPath === 'string') ? window.easysContextPath : '';
    if (!actionName) { return c || '/'; }
    return c + '/' + actionName + '.action';
  };
}
</script>






<s:if test="productInfoList.size()>0">
<script lang="javascript" type="text/javascript"> 

 window.onload = function() {
	  document.getElementById("amountPaid").focus();
	};

</script>

</s:if>

<s:else>
<script lang="javascript" type="text/javascript"> 

 window.onload = function() {
	  document.getElementById("billDateTime").focus();
	
	 	 
	};

</script>
</s:else>


<div align="center" style="margin-top: 70px;font-size: 12px;">
		<div class="ui-widget">
		<s:form method="post" name="addProductToQuotation" action="addProductToQuotation" theme="simple" enctype="multipart/form-data" acceptcharset="UTF-8" >


<table class="table table-bordered" style="height: 100px; width: 1100px;">


<tr  style="font-weight: bold; background-color: rgb(234,234,234);">

<td colspan="9" align="center" >

<s:property value="getText('header.addNewQuotation')" />

</td>
</tr>
<s:if test="hasActionErrors()" >


<tr style="height:50px;">

<td colspan="9" align="center" > 

 
     
    <div class="alert alert-danger">
 	 <strong><s:property value="getText('header.error')" />! </strong>
     
      <s:iterator var="error" value="%{errorMessages}">
        <s:property value="%{error}"/>
    </s:iterator>
	</div>
      


</td>
</tr>
</s:if>
<s:if test="hasActionMessages()" >
<tr>
<td colspan="9" style="font-size: medium; color: green;" align="center" >  
    
    <div class="alert alert-success">
  	<strong><s:property value="getText('header.success')" />! </strong>
   	<s:iterator var="message" value="%{actionMessages}">
        <s:property value="%{message}"/>
    </s:iterator>
    </div>
  
 


</td>
</tr>
</s:if>

<tr>

<td  align="left"><s:property value="getText('text.lable.quotationDate')" /> *</td>
<td  align="left"  >
<sj:datepicker displayFormat="dd-mm-yy" 
               timepicker="true" 
               name="quotationDateTime"
			   id="quotationDateTime"
			   maxDate="1"
			   changeMonth="true"
			   changeYear="true" 
			   readonly="true"
			   placeholder="Quot Date" cssClass="textBoxDate" tabindex="1"/>
 
</td>


<td><s:property value="getText('text.lable.quotationNo')" />.</td><td align="left"><s:textfield name="quotationNumber" id="quotationNumber" readonly="true" cssClass="textBoxSmallSmall"/> </td>




<s:if test="viewBill=='true' || productInfoList.size()>0" >
<td align="right"><s:property value="getText('text.lable.customerName')" /> *</td><td>
<s:textfield name="customerName" id="customerName"  readonly="true"/>

</td>
</s:if>

<s:else>

 
<td align="right"><s:property value="getText('text.lable.customerName')" />*</td><td>
<s:url id="customerList" action="databaseCustomerJSON" />
<sj:autocompleter 				href="%{customerList}" 
								name="customerName" list="customerList" 
								id="customerName" 
								loadMinimumCount="1"								
								placeholder="Customer Name"							 
								tabindex="5" 
								onfocus="this.select();"/>

</td>
 </s:else> 
<td >
Rate Type *
</td>
<td >
  <s:radio list="rateTypeList"  name="rateType" id="rateType" theme="simple" value="rateType" tabindex="5" />
</td>
</tr>

</table>

<table class="table table-bordered" style="height: 0px; width: 1100px; margin-top: -20px;">
<tr>

<td><s:property value="getText('text.lable.productName')" /> *</td>
<td ><s:property value="getText('text.lable.quantity')" /> *</td>
<td><s:property value="getText('text.lable.salesRate')" />*</td>
<td><s:property value="getText('text.lable.amount')" /> *</td>
<td ><s:property value="getText('text.lable.discountType')" /> *</td>
<td ><s:property value="getText('text.lable.disAmount')" /></td>
<td><s:property value="getText('text.lable.amt.aft.dis')" /> *</td>

<td rowspan="2" align="center" >
 <button  type='submit' class='btn btn-primary btn-sm' id="addProductToBill"  tabindex="20" style="margin-top: 40px;">+ <s:property value="getText('button.AddProductToQuotation')" /></button>
</td>

</tr>
<tr>



<td>
<s:url id="productList" action="databaseProductJSON" />
<sj:autocompleter 				href="%{productList}" 
								name="productName" list="productList" 
								id="productName" 
								loadMinimumCount="2"								
								placeholder="Product Name" cssClass="textBoxSmall" tabindex="13"  onfocus="this.select();"/>
</td>
<td >
<s:textfield name="quantity" id="quantity" cssClass="textBoxSmallSmall" tabindex="14"/>
</td>
<td>
<s:textfield name="salesRate" id="salesRate" cssClass="textBoxSmallSmall" tabindex="15"/>
</td>
<td>
<s:textfield name="amount" id="amount" readonly="true" cssClass="textBoxSmallSmall" />
</td>
<td >
<s:select list="discountTypeList" name= "discountType" id="discountType" headerKey="" cssClass="selectSmall" tabindex="16"/>
</td>
<td>
<s:textfield name="discountRupees"  id="discountRupees" value="0" tabindex="16" cssClass="textBoxSmallSmall"/>
</td>
<td>
<s:textfield name="amountAfterDiscount" id="amountAfterDiscount" readonly="true" cssClass="textBoxSmallSmall" />
</td>

</tr>

</table>
<table class="table table-bordered" style="height: 50px; width: 1100px; margin-top: -20px;">
<tr>
<td><s:property value="getText('text.lable.transportationCharges')" /></td>
<td>
<s:if test="viewBill=='true' || productInfoList.size()>0" >

<s:textfield name="transportCharge" id = "transportCharge" cssClass="textBoxSmall" 
tabindex="10" onfocus="this.select();" onclick="this.select();"/> 
</s:if>
<s:else>
<s:textfield name="transportCharge" id = "transportCharge" cssClass="textBoxSmall" tabindex="17"/> 
</s:else>
</td>
<td><s:property value="getText('text.lable.salesUnit')" /> *</td><td>
  <s:select list="unitList"  name="salesUnit" id="salesUnit" headerKey=""  headerValue="Select Sales Unit" cssClass="selectSmall" />
</td>
<td align="left"><s:property value="getText('text.lable.selectTax')" /> % *</td>

<td >
<s:select list="taxList" name= "taxPercentage" id="taxPercentage" headerKey="" headerValue="TAX Percentage" cssClass="selectSmall"/>
</td>

</tr>

 <s:hidden name="productMarathiName" id="productMarathiName"/>

</table>

<table class="table table-bordered" style="height: 50px; width: 1100px; margin-top: -20px;">
<tr>


<td><s:property value="getText('text.lable.comments')" /></td>
<td><s:textarea cols="20" rows="1" id="comments" name="comments" value="NA"  tabindex="19" onfocus="this.select();" onselect="this.select();"/></td>

</tr>
<tr>

<!-- RealLinkBillingPrintHandler -->
</tr>
</table>

<s:hidden id="discountPercentage" name="discountPercentage"/>
<!--
<table class="table table-bordered" style="height: 50px; width: 1100px; margin-top: -20px;">
<tr>
 
<td>
Discount %
</td>
<td>
<s:textfield name="discountPercentage"  id="discountPercentage" value="0" tabindex="10" cssClass="textBoxSmallSmall"/>
</td>


</tr>  
</table>
-->
</s:form>


<s:if test="productInfoList.size()>0">


<table class="table .table-striped" style="height: 50px; width: 1100px;">
<tr style="font-weight: bold; font-size: 14px;">


<td><s:property value="getText('text.lable.productName')" /></td>
<td><s:property value="getText('text.lable.quantity')" />.</td>
<td><s:property value="getText('text.lable.salesUnit')" /></td>
<td ><s:property value="getText('text.lable.discountType')" /> *</td>
<td><s:property value="getText('text.lable.rate')" /></td>
<td><s:property value="getText('text.lable.tax')" />%</td>
<td>SGST</td>
<td>CGST</td>
<td>IGST</td>
<td><s:property value="getText('text.lable.netAmount')" /></td>
<td><s:property value="getText('text.lable.grossAmount')" /></td>
<td align="center"><s:property value="getText('button.delete/edit')" /></td>
</tr>
<s:iterator  value="productInfoList" > 


<tr>

<td><s:property value="productName"/></td>
<td><s:property value="quantity"/></td>
<td><s:property value="salesUnit"/></td>
<td><s:property value="discountType"/></td>
<td><s:property value="salesRate"/></td>
<td><s:property value="taxPercentage"/></td>
<td><s:property value="sgst"/></td>
<td><s:property value="cgst"/></td>
<td><s:property value="igst"/></td>
<td><s:property value="grossAmount"/></td>
<td><s:property value="amountAfterDiscount"/></td>
<td align="center">
<s:url var="deleteEditProductFromQuotationList" action="deleteEditProductFromQuotationList">
  <s:param name="productNameForDelete"><s:property value="productName"/></s:param>
  <s:param name="viewBill"><s:property value="viewBill"/></s:param>
</s:url>
<a href="${deleteEditProductFromQuotationList}"><button type="button" class="btn btn-danger"><s:property value="getText('button.delete/edit')" /></button></a> 
</td>
</tr>
</s:iterator>
</table>

<s:form method="post" name="saveAndPrintQuotation" action="saveAndPrintQuotation" theme="simple" enctype="multipart/form-data" acceptcharset="UTF-8" >

<table class="table .table-striped" style="height: 50px; width: 1100px; font-size: 24px; font-weight: bold;">

<tr>

<td align="left">
<s:hidden name="totalAmount" id="totalAmount"/>
<s:property value="getText('text.lable.totalQuotationAmount')" /> : <s:property value="totalAmount"/>
</td>
<td>
<s:property value="getText('text.lable.totalPayableAmount')" />:
</td>
<td><s:textfield name="totalPaybleAmount" id="totalPaybleAmount" cssClass="textBoxSmall" value="%{totalAmouAfterDisount}" readonly="true"/> </td>
</tr>

</table>
<table class="table .table-striped" style="height: 50px; width: 1100px; font-size: 24px; font-weight: bold;">

<tr>

<td align="center">
<s:if test="billSaved">

</s:if>
<s:else>
<button type="submit" class="btn btn-primary" tabindex="22"><s:property value="getText('button.save')" /></button>
</s:else>
</td> 
<td align="center">

<s:if test="billSaved">
<s:url var="printQuotation" action="printQuotation">
<s:param name="quotationNumner"><s:property value="billNumber"/></s:param>
<s:param name="size">A4</s:param>
<s:param name="lang">eng</s:param>
</s:url>

<s:url var="printQuotationTax" action="printQuotationTax">
<s:param name="quotationNumner"><s:property value="billNumber"/></s:param>
<s:param name="size">A4</s:param>
<s:param name="lang">eng</s:param>
</s:url>


<s:url var="printQuotationa5" action="printQuotation">
<s:param name="quotationNumner"><s:property value="billNumber"/></s:param>
<s:param name="size">A5</s:param>
<s:param name="lang">eng</s:param>

</s:url>

<a href="${printQuotation}" target="_blank" ><button type='button' class='btn btn-primary' tabindex="24"><s:property value="getText('button.printA4English')" /></button></a>
<a href="${printQuotationTax}" target="_blank" ><button type='button' class='btn btn-primary' tabindex="24">Print Quotation Type 2</button></a>
<a href="${printQuotationa5}" target="_blank" ><button type='button' class='btn btn-primary' tabindex="25"><s:property value="getText('button.printA5English')" /></button></a>

</s:if>
<s:if test="viewBill=='true'">
<s:url var="printQuotation" action="printQuotation">
<s:param name="quotationNumner"><s:property value="billNumber"/></s:param>
<s:param name="size">A4</s:param>
<s:param name="lang">eng</s:param>
</s:url>

<s:url var="printQuotationa5" action="printQuotation">
<s:param name="quotationNumner"><s:property value="billNumber"/></s:param>
<s:param name="size">A5</s:param>
<s:param name="lang">eng</s:param>
</s:url>
<a href="${printQuotation}" target="_blank" ><button type='button' class='btn btn-primary' tabindex="24"><s:property value="getText('button.printA4English')" /></button></a>
<a href="${printQuotationa5}" target="_blank" ><button type='button' class='btn btn-primary' tabindex="25"><s:property value="getText('button.printA5English')" /></button></a>
</s:if>
</td>
</tr>
</table>
</s:form>
</s:if>

</div>
</div>

<script type="text/javascript">
/* EASYS_NOCONFLICT_WRAP */
jQuery.noConflict();
jQuery(function ($) {

function easysCustomerKey() {
	var cust = ($('#customerName').val() || '').trim();
	if (!cust) { return '1--9999999999-REGULER'; }
	if (cust.indexOf('-') >= 0) { return cust; }
	return '1-' + cust + '-9999999999-REGULER';
}
function easysSetSelectValue($select, value) {
	if (!$select || !$select.length) { return false; }
	var raw = (value == null) ? '' : String(value).trim();
	if (!raw) { return false; }
	$select.val(raw);
	if (String($select.val()) === raw) { return true; }
	var matched = false;
	var target = raw.toUpperCase();
	$select.find('option').each(function () {
		var optVal = String($(this).attr('value') || '').trim();
		var optText = String($(this).text() || '').trim();
		if (optVal.toUpperCase() === target || optText.toUpperCase() === target) {
			$select.val(optVal);
			matched = true;
			return false;
		}
	});
	return matched;
}

$('#productName').keypress(function (e) {
	if (e.which == 13) { e.preventDefault(); $('#quantity').focus(); }
});
$('#quantity').keypress(function (e) {
	if (e.which == 13) { e.preventDefault(); $('#salesRate').focus(); }
});
$('#salesRate').keypress(function (e) {
	if (e.which == 13) { e.preventDefault(); $('#discountType').focus(); }
});
$('#discountType').keypress(function (e) {
	if (e.which == 13) { e.preventDefault(); $('#discountRupees').focus(); }
});
$('#discountRupees').keypress(function (e) {
	if (e.which == 13) { e.preventDefault(); $('#transportCharge').focus(); }
});
$('#transportCharge').keypress(function (e) {
	if (e.which == 13) { e.preventDefault(); $('#comments').focus(); }
});
$('#comments').keypress(function (e) {
	if (e.which == 13) { e.preventDefault(); $('#addProductToBill').focus(); }
});

$('#productName').focusout(function () {
	var productValue = ($('#productName').val() || '').trim();
	if (productValue.length > 0) {
		$.ajax({
			url: "${pageContext.request.contextPath}/getAliasFullName.action",
			data: "customerName=" + encodeURIComponent(easysCustomerKey()) + "&aliasValue=" + encodeURIComponent(productValue),
			dataType: "json",
			success: function (data) {
				if (!data) { return; }
				if (data.fullName) { $('#productName').val(data.fullName); }
				if (data.fullNameMarathi != null) { $('#productMarathiName').val(data.fullNameMarathi); }
				easysSetSelectValue($('#salesUnit'), data.salesUnit);
				easysSetSelectValue($('#taxPercentage'), data.tax);
				if (data.salesRate != null && data.salesRate !== '') { $('#salesRate').val(data.salesRate); }
			}
		});
	}
});

$('#salesRate').focusout(function () {
	$.ajax({
		url: "${pageContext.request.contextPath}/getAmount.action",
		data: "salesRate=" + ($('#salesRate').val() || '') + "&quantity=" + ($('#quantity').val() || '') + "&productName=" + encodeURIComponent($('#productName').val() || ''),
		dataType: "json",
		success: function (data) {
			if (data && data.amount != null) { $('#amount').val(data.amount); }
		}
	});
});

$('#discountRupees').focusout(function () {
	var payload = {
		salesRate: $('#salesRate').val(),
		quantity: $('#quantity').val(),
		discountRupees: $('#discountRupees').val(),
		discountType: $('#discountType').val()
	};
	if ('<s:property value="#session.royaltyAllowed" />' == '1') {
		payload.royalty = $('#royalty').val() || 0;
	}
	$.ajax({
		url: "${pageContext.request.contextPath}/getAmountAfterRsDiscount.action",
		data: payload,
		dataType: "json",
		success: function (data) {
			if (data && data.amount != null) {
				$('#amountAfterDiscount').val(data.amount);
				$('#discountPercentage').val('0');
			}
		},
		error: function () { alert('Please provide correct values!'); }
	});
});

if ($('#amountPaid').length) {
	$('#amountPaid').focusout(function () {
		$.ajax({
			url: "${pageContext.request.contextPath}/getAmountReturn.action",
			data: "totalPaybleAmount=" + ($('#totalPaybleAmount').val() || '') + "&amountPaid=" + ($('#amountPaid').val() || ''),
			dataType: "json",
			success: function (data) {
				if (data && data.returnAmount != null) { $('#returnAmount').val(data.returnAmount); }
			}
		});
	});
}

});
</script>

