<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="/struts-jquery-tags" prefix="sj"%>

<style type="text/css">
.cb-page #quantity,
.cb-page #salesRate,
.cb-page #discountRupees,
.cb-page input#quantity,
.cb-page input#salesRate,
.cb-page input#discountRupees {
  width: 72px !important;
  max-width: 72px !important;
  min-width: 64px !important;
}
.cb-page #amount,
.cb-page #amountAfterDiscount,
.cb-page input#amount,
.cb-page input#amountAfterDiscount {
  width: 110px !important;
  max-width: 120px !important;
  min-width: 100px !important;
}
.cb-page #salesUnit,
.cb-page #taxId {
  width: 120px !important;
  max-width: 140px !important;
}
.cb-page #productName,
.cb-page input#productName,
.cb-page .textBoxProductName {
  width: 320px !important;
  max-width: 360px !important;
  min-width: 280px !important;
}
</style>

<div class="cb-page" align="center" style="margin-top: 70px;font-size: 12px;">
  <div class="ui-widget">
    <s:if test="hasActionErrors()">
      <div class="alert alert-danger" style="width: 1150px;">
        <strong>Error! </strong>
        <s:iterator var="error" value="%{errorMessages}">
          <s:property value="%{error}"/><br/>
        </s:iterator>
      </div>
    </s:if>
    <s:if test="hasActionMessages()">
      <div class="alert alert-success" style="width: 1150px;">
        <strong>Success! </strong>
        <s:iterator var="message" value="%{actionMessages}">
          <s:property value="%{message}"/><br/>
        </s:iterator>
      </div>
    </s:if>

    <s:form method="post" name="addProductToCompositeBill" action="addProductToCompositeBill" theme="simple" acceptcharset="UTF-8">
      <table class="table table-bordered" style="width: 1150px;">
        <tr style="font-weight: bold; background-color: rgb(234,234,234);">
          <td colspan="8" align="center" style="padding: 2px;">
            Add New Composite Bill
            <div style="float: right; font-size: 16px;">Last Bill Number :<s:property value="lastBillNumber"/></div>
          </td>
        </tr>
        <tr style="font-size: 14px; font-weight: bold;">
          <td style="padding: 2px;">Bill Date *</td>
          <td style="padding: 2px;">
            <sj:datepicker displayFormat="dd-mm-yy" name="billDateTime" id="billDateTime" maxDate="1"
                           changeMonth="true" changeYear="true" readonly="true" cssClass="textBoxDate"/>
          </td>
          <td style="padding: 2px;">Customer Name *</td>
          <td style="padding: 2px;">
            <s:url id="customerList" action="databaseCustomerJSON" />
            <sj:autocompleter href="%{customerList}" name="customerName" list="customerList"
                              id="customerName" loadMinimumCount="2" size="30" cssClass="textBoxx"/>
            <s:hidden id="customerId" name="customerId" value="%{customerId}"/>
            <s:hidden id="customerIdNameAll" name="customerIdNameAll"/>
          </td>
          <td style="padding: 2px;">Mobile *</td>
          <td style="padding: 2px;"><s:textfield name="customerMobileNumber" id="customerMobileNumber" cssClass="textBoxDate"/></td>
          <td style="padding: 2px;">Customer Type *</td>
          <td style="padding: 2px;"><s:select list="customerTypeList" cssClass="selectSmall" name="customerType" id="customerType"/></td>
        </tr>
        <tr style="font-size: 14px; font-weight: bold;">
          <td style="padding: 2px;">Payment Type *</td>
          <td colspan="2" style="padding: 2px;">
            <s:radio list="paymentTypeList" name="paymentType" id="paymentType" theme="simple" value="defaultValue"/>
          </td>
          <td style="padding: 2px;">Rate Type *</td>
          <td colspan="2" style="padding: 2px;">
            <s:radio list="rateTypeList" name="rateType" id="rateType" theme="simple" value="rateType"/>
          </td>
          <td style="padding: 2px;">Bill Type</td>
          <td style="padding: 2px;">
            <s:textfield value="%{'COMPOSITE-BILL'}" readonly="true" cssClass="textBoxDate"/>
            <s:hidden name="billType" value="%{'COMPOSITE-BILL'}"/>
          </td>
        </tr>
      </table>

      <table class="table table-bordered" style="width: 1150px; margin-top: -20px;">
        <tr style="font-size: 14px; font-weight: bold;">
          <td style="padding: 2px;">Main Product Name *</td>
          <td style="padding: 2px;">Quantity *</td>
          <td style="padding: 2px;">Sales Rate *</td>
          <td style="padding: 2px;">Amount</td>
          <td style="padding: 2px;">Discount</td>
          <td style="padding: 2px;">Amount After Discount *</td>
          <td style="padding: 2px;">Sales Unit *</td>
          <td style="padding: 2px;">Tax *</td>
        </tr>
        <tr>
         <td style="padding: 2px; ">
			<s:url id="productList" action="databaseProductJSON" />
			<sj:autocompleter 				href="%{productList}" 
											name="productName" list="productList" 
											id="productName" 
											loadMinimumCount="2"								
											placeholder="Product Name"
											onfocus="this.select();" onselect="this.select();" cssClass="textBoxProductName" />
		</td>
          <td style="padding: 2px;"><s:textfield name="quantity" id="quantity" cssClass="textBoxFigures textBoxVerySmall"/></td>
          <td style="padding: 2px;"><s:textfield name="salesRate" id="salesRate" cssClass="textBoxFigures textBoxVerySmall"/></td>
          <td style="padding: 2px;"><s:textfield name="amount" id="amount" readonly="true" cssClass="textBoxFigures textBoxVerySmall"/></td>
          <td style="padding: 2px;"><s:textfield name="discountRupees" id="discountRupees" value="0" cssClass="textBoxFigures textBoxVerySmall"/></td>
          <td style="padding: 2px;"><s:textfield name="amountAfterDiscount" id="amountAfterDiscount" cssClass="textBoxFigures textBoxVerySmall"/></td>
          <td style="padding: 2px;"><s:select list="unitList" name="salesUnit" id="salesUnit" headerKey="" headerValue="Select Unit" cssClass="selectSmall"/></td>
          <td style="padding: 2px;"><s:select list="taxList" name="taxId" id="taxId" headerKey="" headerValue="TAX Percentage" cssClass="selectSmall"/></td>
        </tr>
      </table>

      <s:hidden name="discountPercentage" id="discountPercentage" value="0"/>
      <s:hidden name="makingLabourAmount" id="makingLabourAmount" value="0"/>
      <s:hidden name="quantity2" id="quantity2" value="0"/>
      <s:hidden name="batchId" id="batchId" value="NA"/>
      <s:hidden name="expiryDate" id="expiryDate" value="NA"/>
      <s:hidden name="productMarathiName" id="productMarathiName"/>
      <s:hidden name="viewBill" id="viewBill"/>

      <div class="cb-row-actions" style="width:1150px; text-align:right; margin-top:-10px; margin-bottom:10px;">
        <button type="submit" class="btn btn-primary btn-sm" id="addMainProductBtn">+ Add Main Product</button>
        <s:url var="compositeSubProductsPopupUrl" action="compositeSubProductsPopupJsp"/>
        <button type="button" class="btn btn-info btn-sm"
                onclick="window.open('${compositeSubProductsPopupUrl}','CompositeSubProducts','width=900,height=600,scrollbars=yes');">
          Manage Sub Products
        </button>
      </div>
    </s:form>

    <s:if test="productInfoList.size()>0">
      <table class="table table-striped table-bordered" style="width:1150px;">
        <tr style="font-weight:bold;">
          <td>SR</td><td>Main Product</td><td>Rate</td><td>Qty</td><td>Amount</td><td>Amount After Discount</td><td>Delete/Edit</td>
        </tr>
        <s:iterator value="productInfoList">
          <tr>
            <td><s:property value="serialNumber"/></td>
            <td><s:property value="productName"/></td>
            <td><s:property value="salesRate"/></td>
            <td><s:property value="quantity"/></td>
            <td><s:property value="amount"/></td>
            <td><s:property value="amountAfterDiscount"/></td>
            <td align="center">
              <s:url var="deleteEditProductComposite" action="deleteEditProductFromCompositeList">
                <s:param name="productNameForDelete"><s:property value="productName"/></s:param>
                <s:param name="serialNumber"><s:property value="serialNumber"/></s:param>
                <s:param name="id"><s:property value="id"/></s:param>
              </s:url>
              <a href="${deleteEditProductComposite}">
                <button type="button" class="btn btn-danger btn-xs">DELETE/EDIT</button>
              </a>
            </td>
          </tr>
        </s:iterator>
      </table>

      <table class="table table-striped table-bordered" style="width:1150px; margin-top:-10px;">
        <tr style="font-weight:bold;">
          <td colspan="4">Sub Products Summary (Stock Only, Hidden From Customer Bill)</td>
        </tr>
        <tr style="font-weight:bold;">
          <td style="width:80px;">SR</td>
          <td>Sub Product</td>
          <td style="width:150px;">Qty</td>
          <td style="width:220px;">Status</td>
        </tr>
        <s:if test="#session.compositeSubProductList != null && #session.compositeSubProductList.size() > 0">
          <s:iterator value="#session.compositeSubProductList" status="st">
            <tr>
              <td><s:property value="#st.index + 1"/></td>
              <td><s:property value="productName"/></td>
              <td><s:property value="quantity"/></td>
              <td>Will reduce stock only</td>
            </tr>
          </s:iterator>
        </s:if>
        <s:else>
          <tr>
            <td colspan="4" style="color:#777;">No sub products added yet. Use "Manage Sub Products".</td>
          </tr>
        </s:else>
      </table>

      <s:form method="post" action="saveCompositeBill" theme="simple" acceptcharset="UTF-8">
        <table class="table table-bordered" style="width:1150px; margin-top:-20px;">
          <tr style="font-weight:bold;">
            <td>Account *</td>
            <td><s:select list="accountList" name="accountId" id="accountId" cssClass="select"/></td>
            <td>Mode Of Payment *</td>
            <td><s:select list="paymentTypeList2" id="modeOfPayment" name="modeOfPayment" cssClass="select"/></td>
            <td>Transport Charge</td>
            <td><s:textfield name="transportCharge" id="transportCharge" value="0" cssClass="textBoxFigures"/></td>
          </tr>
          <tr style="font-weight:bold;">
            <td>Direct Discount</td>
            <td><s:textfield name="directDiscountAmount" id="directDiscountAmount" value="0" cssClass="textBoxFigures"/></td>
            <td>Total Payable</td>
            <td><s:textfield name="totalPaybleAmount" id="totalPaybleAmount" value="%{totalAmouAfterDisount}" readonly="true" cssClass="textBoxFigures"/></td>
            <td>Amount Paid *</td>
            <td><s:textfield name="amountPaid" id="amountPaid" value="%{totalAmouAfterDisount}" cssClass="textBoxFigures"/></td>
          </tr>
          <tr style="font-weight:bold;">
            <td>Return Amount *</td>
            <td><s:textfield name="returnAmount" id="returnAmount" value="0" cssClass="textBoxFigures"/></td>
            <td>Cash</td>
            <td><s:textfield name="cashAmount" id="cashAmount" value="%{totalAmouAfterDisount}" cssClass="textBoxFigures"/></td>
            <td>Bank/UPI</td>
            <td><s:textfield name="bankRtgsUpiAmount" id="bankRtgsUpiAmount" value="0" cssClass="textBoxFigures"/></td>
          </tr>
          <tr style="font-weight:bold;">
            <td>Buyer Order No</td>
            <td><s:textfield name="buyerOrderNumber" id="buyerOrderNumber" cssClass="textBoxSmall"/></td>
            <td>Dispatch Doc No</td>
            <td><s:textfield name="dispatchDocNumber" id="dispatchDocNumber" cssClass="textBoxSmall"/></td>
            <td>Vehicle No</td>
            <td><s:textfield name="vehicleNumber" id="vehicleNumber" cssClass="textBoxSmall"/></td>
          </tr>
          <tr>
            <td>Comments</td>
            <td colspan="5"><s:textarea cols="90" rows="2" id="comments" name="comments"/></td>
          </tr>
          <tr>
            <td colspan="6" align="center">
              <s:checkbox name="sendDirectSMS" id="sendDirectSMS">Send SMS</s:checkbox>
              <s:if test="billSave!='true'">
                <button type="submit" class="btn btn-danger">Save Composite Bill</button>
              </s:if>
              <s:if test="billSave=='true'">
                <span class="label label-success" style="font-size:14px;">Composite Bill Saved</span>
              </s:if>
            </td>
          </tr>
        </table>
      </s:form>

      <s:if test="billSave=='true'">
        <table class="table table-bordered" style="width:1150px; margin-top:-20px;">
          <tr>
            <td align="center" style="padding: 8px;">
              <s:url var="billPrintOnly" action="billPrintOnly">
                <s:param name="billNumber"><s:property value="billNumber"/></s:param>
                <s:param name="billDateTime"><s:property value="billDateTime"/></s:param>
                <s:param name="typeOfBill"><s:property value="billType"/></s:param>
                <s:param name="language">ENGLISH</s:param>
              </s:url>
              <a href="${billPrintOnly}" target="_blank"><button type="button" class="btn btn-primary">Print</button></a>

              <s:url var="billPrintA4" action="billPrintOnly">
                <s:param name="billNumber"><s:property value="billNumber"/></s:param>
                <s:param name="billDateTime"><s:property value="billDateTime"/></s:param>
                <s:param name="typeOfBill"><s:property value="billType"/></s:param>
                <s:param name="billType">A4</s:param>
                <s:param name="language">ENGLISH</s:param>
              </s:url>
              <a href="${billPrintA4}" target="_blank"><button type="button" class="btn btn-primary">Print A4</button></a>

              <s:url var="billPrintA5" action="billPrintOnlyType2">
                <s:param name="billNumber"><s:property value="billNumber"/></s:param>
                <s:param name="billDateTime"><s:property value="billDateTime"/></s:param>
                <s:param name="typeOfBill"><s:property value="billType"/></s:param>
                <s:param name="billType">A5</s:param>
                <s:param name="language">ENGLISH</s:param>
              </s:url>
              <a href="${billPrintA5}" target="_blank"><button type="button" class="btn btn-primary">Print A5</button></a>

              <a href="addCompositeBillJsp"><button type="button" class="btn btn-success">Add New Composite Bill</button></a>
            </td>
          </tr>
        </table>
      </s:if>
    </s:if>
  </div>
</div>

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

$('#customerMobileNumber').focusout(function () {
	var custName = $('#customerName').val();
	var custArray = (custName || '').split('-');
	if (custArray.length > 2) {
		$('#customerIdNameAll').val(custName);
		$('#customerId').val(custArray[0]);
		$('#customerName').val(custArray[1]);
		$('#customerMobileNumber').val(custArray[2]);
		$('#customerType').val(custArray[3]);
	} else {
		var allName = '1-' + $('#customerName').val() + '-' + $('#customerMobileNumber').val() + '-' + $('#customerType').val();
		$('#customerIdNameAll').val(allName);
	}
});

$('#productName').focusout(function () {
	if ($('#productName').val().length > 0) {
		var custAll = $('#customerIdNameAll').val();
		if (!custAll || custAll.length === 0) {
			custAll = '1-' + $('#customerName').val() + '-' + $('#customerMobileNumber').val() + '-' + $('#customerType').val();
			$('#customerIdNameAll').val(custAll);
		}
		$.ajax({
			url: '${pageContext.request.contextPath}/getAliasFullName.action',
			data: 'customerName=' + custAll + '&aliasValue=' + $(this).val(),
			dataType: 'json',
			success: function (data) {
				$('#productName').val(data.fullName);
				$('#productMarathiName').val(data.fullNameMarathi);
				$('#salesUnit').val(data.salesUnit);
				$('#taxId').val(data.tax);
				$('#salesRate').val(data.salesRate);
			}
		});
	}
});

$('#salesRate, #quantity').focusout(function () {
	$.ajax({
		url: 'getAmount',
		data: 'salesRate=' + $('#salesRate').val() + '&quantity=' + $('#quantity').val() + '&productName=' + $('#productName').val(),
		dataType: 'json',
		success: function (data) {
			$('#amount').val(data.amount);
			$('#amountAfterDiscount').val(data.amount);
		}
	});
});

$('#discountRupees').focusout(function () {
	$.ajax({
		url: 'getAmountAfterRsDiscount',
		data: 'salesRate=' + $('#salesRate').val() + '&quantity=' + $('#quantity').val() + '&discountRupees=' + $('#discountRupees').val(),
		dataType: 'json',
		success: function (data) {
			$('#amountAfterDiscount').val(data.amount);
		}
	});
});

$('#amountPaid, #totalPaybleAmount').focusout(function () {
	var total = parseFloat($('#totalPaybleAmount').val() || 0);
	var paid = parseFloat($('#amountPaid').val() || 0);
	$('#returnAmount').val((paid - total).toFixed(2));
	$('#cashAmount').val(paid.toFixed(2));
	$('#bankRtgsUpiAmount').val('0');
});

})(jQuery);
</script>
