var party
function ValidateEmail() {
debugger;
var userEmail = $('#Email').val();
var re = /[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}/igm;
if (userEmail!="" && !re.test(userEmail)) {
$('#spnEmail').show();
return false;
}
else {
$('#spnEmail').hide();
return true;
}
};
///To fill customers in customer texbox as autocomplete
var $j = jQuery.noConflict();
$(document).ready(function () {
$('#example1').DataTable({
"aoColumnDefs": [{
"bSortable": false,
"aTargets": [0]
}],
"order": [[1, "desc"]],
"scrollX": true,
dom: 'Bfrtip',
buttons: [
{
extend: 'collection',
text: '<i class="fa fa-upload"></i> Export',
buttons: [
{
extend: 'excel',
text: '<i class="fa fa-file-excel-o"></i> Export to Excel',
titleAttr: 'Excel',
exportOptions: {
columns: [1, 2, 3, 4, 5, 6, 7, 8, 9]
}
},
{
extend: 'csv',
text: '<i class="fa fa-file-text-o"></i> Export to CSV',
titleAttr: 'CSV',
exportOptions: {
columns: [1, 2, 3, 4, 5, 6, 7, 8, 9]
}
}
]
},
{
extend: 'print',
text: '<i class="fa fa-print"></i> Print',
titleAttr: 'Print',
exportOptions: {
columns: [1, 2, 3, 4, 5, 6, 7, 8, 9]
}
}
],
"bSort": true,
});
//To Adjust the Export Buttons
$(".dt-buttons").insertAfter("#hdExport");
//autofill on sales entry form
$j("#custmer").autocomplete({
source: function (request, response) {
jQuery.ajax({
url: RootUrl + '/Sales/AutoCompleteCustomers',
type: "POST",
dataType: "json",
data: { term: request.term },
success: function (data) {
response(data);
}
})
},
messages: {
noResults: "", results: ""
}
});
var discount = $('#Discount').val();
var subTotal = $('#SubTotal').val();
var totalAmount = $('#TotalAmt').val();
var taxRate = $('#Tax').val();
var shipping = $('#Shipping').val();
if (discount == 0) { $('#Discount').val(''); }
if (subTotal == 0) { $('#SubTotal').val(''); }
if (totalAmount == 0) { $('#TotalAmt').val(''); }
if (taxRate == 0) { $('#Tax').val(''); }
if (shipping == 0) { $('#Shipping').val(''); }
var id = $('#hdnSalesId').val();
if (id > 0) {
SalesDetailsData(id);
}
debugger;
//For Default Date overriden prevention on Edit
var date = new Date();
var newFromDate = "";
if (navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Safari") != -1) {
newFromDate = date.toLocaleDateString("en-US");
}
else {
newFromDate = dateFormat(date, "mm-dd-yyyy");
}
var salesId = $('#hdnSalesId').val();
$("#datepicker").datepicker({
autoclose: true,
useCurrent: false,
todayHighlight: true,
Default: true,
defaultDate: new Date(newFromDate),
endDate: new Date(newFromDate)
});
if (salesId <= 0) {
$("#datepicker").datepicker("setDate", new Date(newFromDate));
};
});
//To validate sales entry form
function SalesValidate() {
var Cust = $('#custmer').val();
var x = true;
if (Cust == '') {
$('#spnCustomer').show();
x = false;
}
else {
$('#spnCustomer').hide();
}
if (party == "Party") {
var ddlParty = $('#party').val();
if (ddlParty == '') {
$('#spnParty').show();
x = false;
}
else {
$('#spnParty').hide();
}
}
var pMethod = $('#pMethod').val();
if (pMethod == '') {
$('#spnPMethod').show();
x = false;
}
else {
$('#spnPMethod').hide();
}
var salesType = $('#salesType').val();
if (salesType == '') {
$('#spnSalesType').show();
x = false;
}
else {
$('#spnSalesType').hide();
}
$('#tblSales > #tBodyH1 tr').each(function () {
var row = $(this);
var pId = row.find('td').eq(1).find('select').val();
var desc = row.find('td').eq(2).find('input').val();
var qty = row.find('td').eq(3).find('input').val();
if (pId == "") {
row.find('td').eq(1).find('span').show();
x = false;
}
else {
row.find('td').eq(1).find('span').hide();
}
if (qty == "") {
row.find('td').eq(3).find('span').show();
x = false;
}
else {
row.find('td').eq(3).find('span').hide();
}
});
if (x == false) {
return false;
}
else {
return true;
}
};
//to get Expense Detail Data for id provided
function SalesDetails(salesId) {
window.location.href = RootUrl + '/Sales/SalesEntry/' + salesId;
};
function SalesDetailsData(id) {
var editid = id;
$.ajax({
url: RootUrl + '/Sales/EditSalesEntry',
type: "POST",
dataType: "JSON",
data: { salesId: editid },
success: function (data) {
party = data.SalesTypeId;
if (party == "3") {
$("#divParty").show();
$("#PartyID").val(data.PartyID);
}
else { $("#divParty").hide(); }
$('#pMethod').val(data.PaymentMethodId)
$(data.ListSalesDetailData).each(function (index, item) {
var row = $('#tblSales #tBodyH1 tr:first').html();
var rowData = '<tr>' + row + '</tr>'
$('#tblSales > #tBodyH1:last').append(rowData);
});
$('#tblSales #tBodyH1 tr:last').remove();
$('#tblSales > #tBodyH1 tr').each(function (index, itemr) {
var listdata = data.ListSalesDetailData;
var row = $(this);
var DropDown = row.find('td').eq(1).find('select');
var pId = listdata[index].ProductID;
DropDown.val(pId);
row.find('td').eq(0).find('input').val(listdata[index].Size);
row.find('td').eq(2).find('input').val(listdata[index].Description);
row.find('td').eq(3).find('input').val(listdata[index].Quantity.toFixed(2));
row.find('td').eq(4).find('input').val(listdata[index].Amount.toFixed(2));
row.find('td').eq(5).find('input').val(listdata[index].ExtendedAmount.toFixed(2));
});
$("#TaxRate").val(data.TaxRate.toFixed(2));
$("#Tax").val(data.Tax.toFixed(2));
$("#Shipping").val(data.Shipping.toFixed(2));
$("#Discount").val(data.Discount.toFixed(2));
$("#SubTotal").val(data.SubTotal.toFixed(2));
$("#TotalAmt").val(data.TotalAmount.toFixed(2));
var taxableAmount = (data.SubTotal - data.Discount) + data.Shipping;
$("#txtTaxableAmt").val(parseFloat(taxableAmount).toFixed(2));
},
error: function (err) {
alert(err);
}
});
};
//To calculate discount and tax
function CalculateTaxDiscount() {
var subTotal = $("#SubTotal").val();
var discount = $("#Discount").val();
var tax = $("#TaxRate").val();
var shipping = $("#Shipping").val();
var totalAmount = subTotal;
if (discount != "") {
totalAmount = parseFloat(totalAmount) - parseFloat(discount)
}
if (shipping != "") {
totalAmount = parseFloat(totalAmount) + parseFloat(shipping)
}
if (isNaN(totalAmount)) {
$("#txtTaxableAmt").val('0.00');
}
else {
$("#txtTaxableAmt").val(parseFloat(totalAmount).toFixed(2));
}
var totalTax = 0;
if (tax != "") {
totalTax = (parseFloat(totalAmount)) * tax / 100;
}
var amountWithTax = parseFloat(totalAmount) + parseFloat(totalTax);
if (isNaN(totalTax)) {
$("#Tax").val('0.00');
}
else {
$("#Tax").val(parseFloat(totalTax).toFixed(2))
}
if (isNaN(amountWithTax)) {
$("#TotalAmt").val('0.00');
}
else {
$("#TotalAmt").val(parseFloat(amountWithTax).toFixed(2));
}
};
//clicked of product dropdown and fill data
function calculateSum(data) {
var sum = 0;
//iterate through each textboxes and add the values
$(".txt").each(function () {
//add only if the value is number
if (!isNaN(this.value) && this.value.length != 0) {
sum += parseFloat(this.value);
}
if (isNaN(sum)) {
$("#SubTotal").val('0.00');
}
else {
$("#SubTotal").val(sum.toFixed(2));
}
});
};
var pQty;
var isRow = 1;
$(document).on("change", "#ddlProduct", function () {
var pid = $(this).val();
var txtsize = $(this).closest('tr').find('td').eq(0).find('input');
var txtdesc = $(this).closest('tr').find('td').eq(2).find('input');
var txtqty = $(this).closest('tr').find('td').eq(3).find('input');
var txtamt = $(this).closest('tr').find('td').eq(4).find('input');
var txtextamnt = $(this).closest('tr').find('td').eq(5).find('input');
var lstPid = [];
$('#tblSales > #tBodyH1 tr').each(function () {
var pId = $(this).closest('tr').find('td').eq(1).find('select').val()
lstPid.push(pId);
});
var hasDups = !lstPid.every(function (v, i) {
return lstPid.indexOf(v) == i;
});
if (hasDups) {
$("#myAlert").modal({ show: true, backdrop: 'static', keyboard: false });
$("#myAlert .modal-header").addClass("delete");
$("#myAlert div#AlertMsg").empty();
$("#myAlert div#AlertMsg").append('This product alredy selected');
//$('#salesModal1').modal('show');
$(this).prop('selectedIndex', 0);
}
else {
if (pid == 0) {
txtsize.val('');
txtqty.val('');
txtamt.val('');
$("#Discount").val('0.00');
$("#SubTotal").val('0.00');
$("#TaxRate").val('0.00');
$("#Shipping").val('0.00');
$("#Tax").val('0.00');
}
else {
GetItemDetails(pid, txtqty, txtamt, txtextamnt, txtsize, txtdesc);
calculateSum(txtextamnt);
//CalculateTaxDiscount();
}
}
//SalesValidate();
});
$(document).on("change", "#salesType", function () {
party = $('#salesType option:selected').text();
if (party == "Party") {
$("#divParty").show();
}
else { $("#divParty").hide(); }
});
$(document).on("keyup", "#Quanity", function (e) {
var row = $(this);
var amount = $(this).closest('tr').find('td').eq(4).find('input').val();
var quantity = row.val();
var extendAmt = $(this).closest('tr').find('td').eq(5).find('input');
var qty = $(this).closest('tr').find('td').eq(3).find('input');
var result = ValidateQuantity(qty);
if (result == true) {
if (amount != '') {
sumCall(amount, quantity, extendAmt);
}
}
else {
sumCall(amount, pQty, extendAmt);
}
CalculateTaxDiscount();
});
$(document).on("blur", "#Quanity", function (e) {
var row = $(this);
var quantity = row.val();
if (quantity == "") {
row.val('');
}
else {
row.val(parseFloat(quantity).toFixed(2));
}
AddNewRow();
calculateSum();
});
$(document).on("keyup", "#Discount", function () {
var discount = parseFloat($('#Discount').val()).toFixed(2);
var total = $('#SubTotal').val();
if (parseFloat(discount) > parseFloat(total)) {
$("#myAlert").modal({ show: true, backdrop: 'static', keyboard: false });
$("#myAlert .modal-header").addClass("delete");
$("#myAlert div#AlertMsg").empty();
$("#myAlert div#AlertMsg").append('Discount should not greater than Subtotal');
$('#Discount').val('');
}
CalculateTaxDiscount();
});
$(document).on("blur", "#Discount", function () {
var discount = parseFloat($('#Discount').val()).toFixed(2);
if (isNaN(discount)) {
$("#Discount").val('0.00');
}
else {
$("#Discount").val(parseFloat(discount).toFixed(2))
}
});
$(document).on("keyup", "#TaxRate", function () {
CalculateTaxDiscount();
});
$(document).on("keyup", "#Shipping", function (e) {
CalculateTaxDiscount();
});
$(document).on("blur", "#Shipping", function (e) {
var shipping = parseFloat($('#Shipping').val()).toFixed(2);
if (isNaN(shipping)) {
$("#Shipping").val('0.00');
}
else {
$('#Shipping').val(parseFloat(shipping).toFixed(2));
}
});
$(document).on("blur", "#TaxRate", function () {
var taxrate = parseFloat($('#TaxRate').val()).toFixed(2);
$('#TaxRate').val(parseFloat(taxrate).toFixed(2));
});
$(document).on("click", "#btnSaveClose", function () {
RemoveRow();
if (SalesValidate()) {
var btnValue = $(this).val();
CreateSalesModel(btnValue)
}
});
$(document).on("click", "#btnSaveAndNew", function () {
RemoveRow();
if (SalesValidate()) {
var btnValue = $(this).val();
CreateSalesModel(btnValue)
}
});
$(document).on("click", "#btnUpdate", function () {
RemoveRow();
if (SalesValidate()) {
var btnValue = $(this).val();
CreateSalesModel(btnValue)
}
});
//create sales model and sales detail model
function CreateSalesModel(btnValue) {
debugger;
var lstSales = [];
$('#tblSales > #tBodyH1 tr').each(function () {
var row = $(this);
var pId = row.find('td').eq(1).find('select').val();
var desc = row.find('td').eq(2).find('input').val();
var size = row.find('td').eq(0).find('input').val();
var qty = row.find('td').eq(3).find('input').val();
var amt = row.find('td').eq(4).find('input').val();
var extAmt = row.find('td').eq(5).find('input').val();
if (pId > 0 && qty > 0 && amt > 0 && extAmt > 0) {
var SalesDetailModel =
{
"ProductId": pId,
"Description": desc,
"Size": size,
"Quantity": qty,
"Amount": amt,
"ExtendedAmount": extAmt
};
lstSales.push(SalesDetailModel);
}
});
var SalesModel =
{
"SalesDate": $("#date").val(),
"SalesID": $("#salesId").val(),
"Customer": $("#custmer").val(),
"Email": $("#Email").val(),
"PaymentMethodId": $("#pMethod").val(),
"SalesTypeID": $("#salesType").val(),
"PartyID": $("#party").val(),
"SubTotal": $("#SubTotal").val(),
"Discount": $("#Discount").val(),
"Shipping": $("#Shipping").val(),
"TaxRate": $("#TaxRate").val(),
"Tax": $("#Tax").val(),
"TotalAmount": $("#TotalAmt").val() - $("#Tax").val()
};
InsertSalesDetails(SalesModel, lstSales, btnValue);
};
//to calculate extended amount
function sumCall(amount, quantity, extendAmt) {
var result = parseFloat(amount) * parseFloat(quantity);
if (isNaN(result)) {
extendAmt.val('0.00');
}
else {
//extendAmt.val(result);
extendAmt.val(parseFloat(result).toFixed(2));
}
calculateSum();
};
//to add new row in the sales table
function AddNewRow() {
var row = $('#tblSales #tBodyH1 tr:first').html();
var rowData = '<tr>' + row + '</tr>'
$('#tblSales > #tBodyH1 tr:last').each(function () {
var pId = $(this).closest('tr').find('td').eq(1).find('select').val()
if (pId > 0) {
$('#tblSales > #tBodyH1:last').append(rowData);
}
});
};
$(document).on("click", "#btnDeleteRow", function () {
//$(this).closest('tr').remove()
//$(this).parent().parent().remove()
if ($("#tblSales >#tBodyH1 tr").length > 1) {
$(this).parent().parent("tr").remove();
calculateSum();
CalculateTaxDiscount();
}
});
//validate product quantity i.e. user can not enter more than product quantity.
function ValidateQuantity(qty) {
var result = true;
if (qty.val() > pQty) {
$("#myAlert").modal({ show: true, backdrop: 'static', keyboard: false });
$("#myAlert .modal-header").addClass("delete");
$("#myAlert div#AlertMsg").empty();
$("#myAlert div#AlertMsg").append('Quantity must be less than equal to Inventory');
isRow = 0;
qty.val(pQty);
result = false;
}
else { result = true; }
return result;
};
//to get product details
function GetItemDetails(pid, txtqty, txtamt, txtextamnt, txtsize, txtdesc) {
$.ajax({
url: RootUrl + '/Sales/GetProductDetails',
type: "GET",
dataType: "JSON",
data: { ProductId: pid },
success: function (data) {
if ($("#tblSales >#tBodyH1 tr").length == 1) {
$("#SubTotal").val(data.ExtendedAmount);
$("#TotalAmt").val(data.ExtendedAmount);
}
if (data.Quantity > 0) {
txtqty.val(data.Quantity.toFixed(2));
pQty = data.Quantity;
txtamt.val(data.Amount.toFixed(2));
txtextamnt.val(data.ExtendedAmount.toFixed(2));
txtsize.val(data.Size);
txtqty.removeAttr("disabled");
txtdesc.removeAttr("disabled");
calculateSum(txtextamnt);
CalculateTaxDiscount();
}
else {
txtdesc.val('');
txtqty.val('');
txtamt.val('');
txtextamnt.val('');
txtqty.attr("disabled", "disabled");
txtdesc.attr("disabled", "disabled");
$('#btnAddInventory').val(pid);
$('#salesModal3').modal('show');
}
}
});
};
//insert sales detail data
function InsertSalesDetails(salesmodel, salesdata, btnValue) {
$.ajax({
url: RootUrl + '/Sales/SalesEntry',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ SalesModel: salesmodel, SalesData: salesdata, SubmitButton: btnValue }),
type: 'POST',
success: function (salesId) {
if (salesId > 0) {
if (btnValue == "Save and New") {
window.location.href = RootUrl + "/Sales/SalesEntry";
}
else if (btnValue == "Update") {
window.location.href = RootUrl + "/Sales/SalesSummary";
}
else if (btnValue == "Save and Close") {
window.location.href = RootUrl + '/Sales/SalesSummary';
}
if (btnValue == "Update") {
$(".alert-success").removeAttr('style');
$("label[for='Success']").text('Sales Updated Successfully..');
}
else if (btnValue == "Save and New" || btnValue == "Save and Close") {
$(".alert-success").removeAttr('style');
$("label[for='Success']").text('Sales Submitted Successfully');
}
}
},
error: function (err) {
$("#myAlert").modal({ show: true, backdrop: 'static', keyboard: false });
$("#myAlert .modal-header").addClass("delete");
$("#myAlert div#AlertMsg").empty();
$("#myAlert div#AlertMsg").append(err);
}
});
};
//clear values
function ClearValues() {
$("#custmer").val('');
$('#pMethod').prop('selectedIndex', 0);
$('#salesType').prop('selectedIndex', 0);
$('#ddlProduct').prop('selectedIndex', 0);
$('#party').prop('selectedIndex', 0);
$("#Discount").val('0.00');
$("#SubTotal").val('0.00');
$("#Shipping").val('0.00');
$("#Tax").val('0.00');
$("#TotalAmt").val('0.00');
$('#tblSales >#tBodyH1 tr:gt(0)').remove();
$('#Desc').val('');
$('#Quanity').val('');
$('#Amount').val('');
$('#ExtendedAmount').val('');
}
//To remove blank row
function RemoveRow() {
if ($("#tblSales >#tBodyH1 tr").length > 1) {
$('#tblSales > #tBodyH1 tr:last').each(function () {
var pId = $(this).closest('tr').find('td').eq(1).find('select').val();
if (pId == null || pId == "" || pId == 'undefined' || pId < 0) {
$(this).remove();
}
});
}
}
////////////////Start of Sales Summary////////////////////////////////
function myfunction(sId) {
if (sId > 0) {
GetSalesDetail(sId);
}
};
function GetSalesDetail(salesId) {
$.ajax({
url: RootUrl + '/Sales/SalesDetail',
type: "GET",
dataType: "JSON",
data: { salesId: salesId },
success: function (data) {
if (result != '') {
}
}
});
};
$(document).on("click", "#checkAllItems", function () {
$(".checkBox").prop('checked',
$(this).prop('checked'));
});
$(document).on("click", "#btnDeleteReturn", function () {
var selectedIDs = ValidateDeleteData();
if (selectedIDs.length > 0)
{ $('#deletePopup').modal("show"); }
else {
$("#myAlert").modal({ show: true, backdrop: 'static', keyboard: false });
$("#myAlert .modal-header").addClass("delete");
$("#myAlert div#AlertMsg").empty();
$("#myAlert div#AlertMsg").append('Please select an item');
}
});
//To validate Sales item for delete
function ValidateDeleteData() {
var selectedItems = new Array();
$('input:checkbox.checkBox').each(function () {
if ($(this).prop('checked')) {
selectedItems.push($(this).val());
}
});
return selectedItems;
};
//For Delete Action
$(document).on("click", "#btnDelete", function () {
var selectedIds = new Array();
$('input:checkbox.checkBox').each(function () {
if ($(this).prop('checked')) {
selectedIds.push($(this).val());
}
});
var options = {};
options.url = RootUrl + "/Sales/DeleteReturn";
options.type = "POST";
options.data = JSON.stringify(selectedIds);
options.contentType = "application/json";
options.dataType = "json";
options.success = function (msg) {
window.location.href = RootUrl + "/Sales/SalesReturnSummary";
};
options.error = function (error) {
$("label[for='Error']").text(error.msg);
};
$.ajax(options);
});
/////////////////////End of Sales Summary//////////////////////////
function ValidateEmail() {
debugger;
var userEmail = $('#Email').val();
var re = /[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}/igm;
if (userEmail!="" && !re.test(userEmail)) {
$('#spnEmail').show();
return false;
}
else {
$('#spnEmail').hide();
return true;
}
};
///To fill customers in customer texbox as autocomplete
var $j = jQuery.noConflict();
$(document).ready(function () {
$('#example1').DataTable({
"aoColumnDefs": [{
"bSortable": false,
"aTargets": [0]
}],
"order": [[1, "desc"]],
"scrollX": true,
dom: 'Bfrtip',
buttons: [
{
extend: 'collection',
text: '<i class="fa fa-upload"></i> Export',
buttons: [
{
extend: 'excel',
text: '<i class="fa fa-file-excel-o"></i> Export to Excel',
titleAttr: 'Excel',
exportOptions: {
columns: [1, 2, 3, 4, 5, 6, 7, 8, 9]
}
},
{
extend: 'csv',
text: '<i class="fa fa-file-text-o"></i> Export to CSV',
titleAttr: 'CSV',
exportOptions: {
columns: [1, 2, 3, 4, 5, 6, 7, 8, 9]
}
}
]
},
{
extend: 'print',
text: '<i class="fa fa-print"></i> Print',
titleAttr: 'Print',
exportOptions: {
columns: [1, 2, 3, 4, 5, 6, 7, 8, 9]
}
}
],
"bSort": true,
});
//To Adjust the Export Buttons
$(".dt-buttons").insertAfter("#hdExport");
//autofill on sales entry form
$j("#custmer").autocomplete({
source: function (request, response) {
jQuery.ajax({
url: RootUrl + '/Sales/AutoCompleteCustomers',
type: "POST",
dataType: "json",
data: { term: request.term },
success: function (data) {
response(data);
}
})
},
messages: {
noResults: "", results: ""
}
});
var discount = $('#Discount').val();
var subTotal = $('#SubTotal').val();
var totalAmount = $('#TotalAmt').val();
var taxRate = $('#Tax').val();
var shipping = $('#Shipping').val();
if (discount == 0) { $('#Discount').val(''); }
if (subTotal == 0) { $('#SubTotal').val(''); }
if (totalAmount == 0) { $('#TotalAmt').val(''); }
if (taxRate == 0) { $('#Tax').val(''); }
if (shipping == 0) { $('#Shipping').val(''); }
var id = $('#hdnSalesId').val();
if (id > 0) {
SalesDetailsData(id);
}
debugger;
//For Default Date overriden prevention on Edit
var date = new Date();
var newFromDate = "";
if (navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Safari") != -1) {
newFromDate = date.toLocaleDateString("en-US");
}
else {
newFromDate = dateFormat(date, "mm-dd-yyyy");
}
var salesId = $('#hdnSalesId').val();
$("#datepicker").datepicker({
autoclose: true,
useCurrent: false,
todayHighlight: true,
Default: true,
defaultDate: new Date(newFromDate),
endDate: new Date(newFromDate)
});
if (salesId <= 0) {
$("#datepicker").datepicker("setDate", new Date(newFromDate));
};
});
//To validate sales entry form
function SalesValidate() {
var Cust = $('#custmer').val();
var x = true;
if (Cust == '') {
$('#spnCustomer').show();
x = false;
}
else {
$('#spnCustomer').hide();
}
if (party == "Party") {
var ddlParty = $('#party').val();
if (ddlParty == '') {
$('#spnParty').show();
x = false;
}
else {
$('#spnParty').hide();
}
}
var pMethod = $('#pMethod').val();
if (pMethod == '') {
$('#spnPMethod').show();
x = false;
}
else {
$('#spnPMethod').hide();
}
var salesType = $('#salesType').val();
if (salesType == '') {
$('#spnSalesType').show();
x = false;
}
else {
$('#spnSalesType').hide();
}
$('#tblSales > #tBodyH1 tr').each(function () {
var row = $(this);
var pId = row.find('td').eq(1).find('select').val();
var desc = row.find('td').eq(2).find('input').val();
var qty = row.find('td').eq(3).find('input').val();
if (pId == "") {
row.find('td').eq(1).find('span').show();
x = false;
}
else {
row.find('td').eq(1).find('span').hide();
}
if (qty == "") {
row.find('td').eq(3).find('span').show();
x = false;
}
else {
row.find('td').eq(3).find('span').hide();
}
});
if (x == false) {
return false;
}
else {
return true;
}
};
//to get Expense Detail Data for id provided
function SalesDetails(salesId) {
window.location.href = RootUrl + '/Sales/SalesEntry/' + salesId;
};
function SalesDetailsData(id) {
var editid = id;
$.ajax({
url: RootUrl + '/Sales/EditSalesEntry',
type: "POST",
dataType: "JSON",
data: { salesId: editid },
success: function (data) {
party = data.SalesTypeId;
if (party == "3") {
$("#divParty").show();
$("#PartyID").val(data.PartyID);
}
else { $("#divParty").hide(); }
$('#pMethod').val(data.PaymentMethodId)
$(data.ListSalesDetailData).each(function (index, item) {
var row = $('#tblSales #tBodyH1 tr:first').html();
var rowData = '<tr>' + row + '</tr>'
$('#tblSales > #tBodyH1:last').append(rowData);
});
$('#tblSales #tBodyH1 tr:last').remove();
$('#tblSales > #tBodyH1 tr').each(function (index, itemr) {
var listdata = data.ListSalesDetailData;
var row = $(this);
var DropDown = row.find('td').eq(1).find('select');
var pId = listdata[index].ProductID;
DropDown.val(pId);
row.find('td').eq(0).find('input').val(listdata[index].Size);
row.find('td').eq(2).find('input').val(listdata[index].Description);
row.find('td').eq(3).find('input').val(listdata[index].Quantity.toFixed(2));
row.find('td').eq(4).find('input').val(listdata[index].Amount.toFixed(2));
row.find('td').eq(5).find('input').val(listdata[index].ExtendedAmount.toFixed(2));
});
$("#TaxRate").val(data.TaxRate.toFixed(2));
$("#Tax").val(data.Tax.toFixed(2));
$("#Shipping").val(data.Shipping.toFixed(2));
$("#Discount").val(data.Discount.toFixed(2));
$("#SubTotal").val(data.SubTotal.toFixed(2));
$("#TotalAmt").val(data.TotalAmount.toFixed(2));
var taxableAmount = (data.SubTotal - data.Discount) + data.Shipping;
$("#txtTaxableAmt").val(parseFloat(taxableAmount).toFixed(2));
},
error: function (err) {
alert(err);
}
});
};
//To calculate discount and tax
function CalculateTaxDiscount() {
var subTotal = $("#SubTotal").val();
var discount = $("#Discount").val();
var tax = $("#TaxRate").val();
var shipping = $("#Shipping").val();
var totalAmount = subTotal;
if (discount != "") {
totalAmount = parseFloat(totalAmount) - parseFloat(discount)
}
if (shipping != "") {
totalAmount = parseFloat(totalAmount) + parseFloat(shipping)
}
if (isNaN(totalAmount)) {
$("#txtTaxableAmt").val('0.00');
}
else {
$("#txtTaxableAmt").val(parseFloat(totalAmount).toFixed(2));
}
var totalTax = 0;
if (tax != "") {
totalTax = (parseFloat(totalAmount)) * tax / 100;
}
var amountWithTax = parseFloat(totalAmount) + parseFloat(totalTax);
if (isNaN(totalTax)) {
$("#Tax").val('0.00');
}
else {
$("#Tax").val(parseFloat(totalTax).toFixed(2))
}
if (isNaN(amountWithTax)) {
$("#TotalAmt").val('0.00');
}
else {
$("#TotalAmt").val(parseFloat(amountWithTax).toFixed(2));
}
};
//clicked of product dropdown and fill data
function calculateSum(data) {
var sum = 0;
//iterate through each textboxes and add the values
$(".txt").each(function () {
//add only if the value is number
if (!isNaN(this.value) && this.value.length != 0) {
sum += parseFloat(this.value);
}
if (isNaN(sum)) {
$("#SubTotal").val('0.00');
}
else {
$("#SubTotal").val(sum.toFixed(2));
}
});
};
var pQty;
var isRow = 1;
$(document).on("change", "#ddlProduct", function () {
var pid = $(this).val();
var txtsize = $(this).closest('tr').find('td').eq(0).find('input');
var txtdesc = $(this).closest('tr').find('td').eq(2).find('input');
var txtqty = $(this).closest('tr').find('td').eq(3).find('input');
var txtamt = $(this).closest('tr').find('td').eq(4).find('input');
var txtextamnt = $(this).closest('tr').find('td').eq(5).find('input');
var lstPid = [];
$('#tblSales > #tBodyH1 tr').each(function () {
var pId = $(this).closest('tr').find('td').eq(1).find('select').val()
lstPid.push(pId);
});
var hasDups = !lstPid.every(function (v, i) {
return lstPid.indexOf(v) == i;
});
if (hasDups) {
$("#myAlert").modal({ show: true, backdrop: 'static', keyboard: false });
$("#myAlert .modal-header").addClass("delete");
$("#myAlert div#AlertMsg").empty();
$("#myAlert div#AlertMsg").append('This product alredy selected');
//$('#salesModal1').modal('show');
$(this).prop('selectedIndex', 0);
}
else {
if (pid == 0) {
txtsize.val('');
txtqty.val('');
txtamt.val('');
$("#Discount").val('0.00');
$("#SubTotal").val('0.00');
$("#TaxRate").val('0.00');
$("#Shipping").val('0.00');
$("#Tax").val('0.00');
}
else {
GetItemDetails(pid, txtqty, txtamt, txtextamnt, txtsize, txtdesc);
calculateSum(txtextamnt);
//CalculateTaxDiscount();
}
}
//SalesValidate();
});
$(document).on("change", "#salesType", function () {
party = $('#salesType option:selected').text();
if (party == "Party") {
$("#divParty").show();
}
else { $("#divParty").hide(); }
});
$(document).on("keyup", "#Quanity", function (e) {
var row = $(this);
var amount = $(this).closest('tr').find('td').eq(4).find('input').val();
var quantity = row.val();
var extendAmt = $(this).closest('tr').find('td').eq(5).find('input');
var qty = $(this).closest('tr').find('td').eq(3).find('input');
var result = ValidateQuantity(qty);
if (result == true) {
if (amount != '') {
sumCall(amount, quantity, extendAmt);
}
}
else {
sumCall(amount, pQty, extendAmt);
}
CalculateTaxDiscount();
});
$(document).on("blur", "#Quanity", function (e) {
var row = $(this);
var quantity = row.val();
if (quantity == "") {
row.val('');
}
else {
row.val(parseFloat(quantity).toFixed(2));
}
AddNewRow();
calculateSum();
});
$(document).on("keyup", "#Discount", function () {
var discount = parseFloat($('#Discount').val()).toFixed(2);
var total = $('#SubTotal').val();
if (parseFloat(discount) > parseFloat(total)) {
$("#myAlert").modal({ show: true, backdrop: 'static', keyboard: false });
$("#myAlert .modal-header").addClass("delete");
$("#myAlert div#AlertMsg").empty();
$("#myAlert div#AlertMsg").append('Discount should not greater than Subtotal');
$('#Discount').val('');
}
CalculateTaxDiscount();
});
$(document).on("blur", "#Discount", function () {
var discount = parseFloat($('#Discount').val()).toFixed(2);
if (isNaN(discount)) {
$("#Discount").val('0.00');
}
else {
$("#Discount").val(parseFloat(discount).toFixed(2))
}
});
$(document).on("keyup", "#TaxRate", function () {
CalculateTaxDiscount();
});
$(document).on("keyup", "#Shipping", function (e) {
CalculateTaxDiscount();
});
$(document).on("blur", "#Shipping", function (e) {
var shipping = parseFloat($('#Shipping').val()).toFixed(2);
if (isNaN(shipping)) {
$("#Shipping").val('0.00');
}
else {
$('#Shipping').val(parseFloat(shipping).toFixed(2));
}
});
$(document).on("blur", "#TaxRate", function () {
var taxrate = parseFloat($('#TaxRate').val()).toFixed(2);
$('#TaxRate').val(parseFloat(taxrate).toFixed(2));
});
$(document).on("click", "#btnSaveClose", function () {
RemoveRow();
if (SalesValidate()) {
var btnValue = $(this).val();
CreateSalesModel(btnValue)
}
});
$(document).on("click", "#btnSaveAndNew", function () {
RemoveRow();
if (SalesValidate()) {
var btnValue = $(this).val();
CreateSalesModel(btnValue)
}
});
$(document).on("click", "#btnUpdate", function () {
RemoveRow();
if (SalesValidate()) {
var btnValue = $(this).val();
CreateSalesModel(btnValue)
}
});
//create sales model and sales detail model
function CreateSalesModel(btnValue) {
debugger;
var lstSales = [];
$('#tblSales > #tBodyH1 tr').each(function () {
var row = $(this);
var pId = row.find('td').eq(1).find('select').val();
var desc = row.find('td').eq(2).find('input').val();
var size = row.find('td').eq(0).find('input').val();
var qty = row.find('td').eq(3).find('input').val();
var amt = row.find('td').eq(4).find('input').val();
var extAmt = row.find('td').eq(5).find('input').val();
if (pId > 0 && qty > 0 && amt > 0 && extAmt > 0) {
var SalesDetailModel =
{
"ProductId": pId,
"Description": desc,
"Size": size,
"Quantity": qty,
"Amount": amt,
"ExtendedAmount": extAmt
};
lstSales.push(SalesDetailModel);
}
});
var SalesModel =
{
"SalesDate": $("#date").val(),
"SalesID": $("#salesId").val(),
"Customer": $("#custmer").val(),
"Email": $("#Email").val(),
"PaymentMethodId": $("#pMethod").val(),
"SalesTypeID": $("#salesType").val(),
"PartyID": $("#party").val(),
"SubTotal": $("#SubTotal").val(),
"Discount": $("#Discount").val(),
"Shipping": $("#Shipping").val(),
"TaxRate": $("#TaxRate").val(),
"Tax": $("#Tax").val(),
"TotalAmount": $("#TotalAmt").val() - $("#Tax").val()
};
InsertSalesDetails(SalesModel, lstSales, btnValue);
};
//to calculate extended amount
function sumCall(amount, quantity, extendAmt) {
var result = parseFloat(amount) * parseFloat(quantity);
if (isNaN(result)) {
extendAmt.val('0.00');
}
else {
//extendAmt.val(result);
extendAmt.val(parseFloat(result).toFixed(2));
}
calculateSum();
};
//to add new row in the sales table
function AddNewRow() {
var row = $('#tblSales #tBodyH1 tr:first').html();
var rowData = '<tr>' + row + '</tr>'
$('#tblSales > #tBodyH1 tr:last').each(function () {
var pId = $(this).closest('tr').find('td').eq(1).find('select').val()
if (pId > 0) {
$('#tblSales > #tBodyH1:last').append(rowData);
}
});
};
$(document).on("click", "#btnDeleteRow", function () {
//$(this).closest('tr').remove()
//$(this).parent().parent().remove()
if ($("#tblSales >#tBodyH1 tr").length > 1) {
$(this).parent().parent("tr").remove();
calculateSum();
CalculateTaxDiscount();
}
});
//validate product quantity i.e. user can not enter more than product quantity.
function ValidateQuantity(qty) {
var result = true;
if (qty.val() > pQty) {
$("#myAlert").modal({ show: true, backdrop: 'static', keyboard: false });
$("#myAlert .modal-header").addClass("delete");
$("#myAlert div#AlertMsg").empty();
$("#myAlert div#AlertMsg").append('Quantity must be less than equal to Inventory');
isRow = 0;
qty.val(pQty);
result = false;
}
else { result = true; }
return result;
};
//to get product details
function GetItemDetails(pid, txtqty, txtamt, txtextamnt, txtsize, txtdesc) {
$.ajax({
url: RootUrl + '/Sales/GetProductDetails',
type: "GET",
dataType: "JSON",
data: { ProductId: pid },
success: function (data) {
if ($("#tblSales >#tBodyH1 tr").length == 1) {
$("#SubTotal").val(data.ExtendedAmount);
$("#TotalAmt").val(data.ExtendedAmount);
}
if (data.Quantity > 0) {
txtqty.val(data.Quantity.toFixed(2));
pQty = data.Quantity;
txtamt.val(data.Amount.toFixed(2));
txtextamnt.val(data.ExtendedAmount.toFixed(2));
txtsize.val(data.Size);
txtqty.removeAttr("disabled");
txtdesc.removeAttr("disabled");
calculateSum(txtextamnt);
CalculateTaxDiscount();
}
else {
txtdesc.val('');
txtqty.val('');
txtamt.val('');
txtextamnt.val('');
txtqty.attr("disabled", "disabled");
txtdesc.attr("disabled", "disabled");
$('#btnAddInventory').val(pid);
$('#salesModal3').modal('show');
}
}
});
};
//insert sales detail data
function InsertSalesDetails(salesmodel, salesdata, btnValue) {
$.ajax({
url: RootUrl + '/Sales/SalesEntry',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ SalesModel: salesmodel, SalesData: salesdata, SubmitButton: btnValue }),
type: 'POST',
success: function (salesId) {
if (salesId > 0) {
if (btnValue == "Save and New") {
window.location.href = RootUrl + "/Sales/SalesEntry";
}
else if (btnValue == "Update") {
window.location.href = RootUrl + "/Sales/SalesSummary";
}
else if (btnValue == "Save and Close") {
window.location.href = RootUrl + '/Sales/SalesSummary';
}
if (btnValue == "Update") {
$(".alert-success").removeAttr('style');
$("label[for='Success']").text('Sales Updated Successfully..');
}
else if (btnValue == "Save and New" || btnValue == "Save and Close") {
$(".alert-success").removeAttr('style');
$("label[for='Success']").text('Sales Submitted Successfully');
}
}
},
error: function (err) {
$("#myAlert").modal({ show: true, backdrop: 'static', keyboard: false });
$("#myAlert .modal-header").addClass("delete");
$("#myAlert div#AlertMsg").empty();
$("#myAlert div#AlertMsg").append(err);
}
});
};
//clear values
function ClearValues() {
$("#custmer").val('');
$('#pMethod').prop('selectedIndex', 0);
$('#salesType').prop('selectedIndex', 0);
$('#ddlProduct').prop('selectedIndex', 0);
$('#party').prop('selectedIndex', 0);
$("#Discount").val('0.00');
$("#SubTotal").val('0.00');
$("#Shipping").val('0.00');
$("#Tax").val('0.00');
$("#TotalAmt").val('0.00');
$('#tblSales >#tBodyH1 tr:gt(0)').remove();
$('#Desc').val('');
$('#Quanity').val('');
$('#Amount').val('');
$('#ExtendedAmount').val('');
}
//To remove blank row
function RemoveRow() {
if ($("#tblSales >#tBodyH1 tr").length > 1) {
$('#tblSales > #tBodyH1 tr:last').each(function () {
var pId = $(this).closest('tr').find('td').eq(1).find('select').val();
if (pId == null || pId == "" || pId == 'undefined' || pId < 0) {
$(this).remove();
}
});
}
}
////////////////Start of Sales Summary////////////////////////////////
function myfunction(sId) {
if (sId > 0) {
GetSalesDetail(sId);
}
};
function GetSalesDetail(salesId) {
$.ajax({
url: RootUrl + '/Sales/SalesDetail',
type: "GET",
dataType: "JSON",
data: { salesId: salesId },
success: function (data) {
if (result != '') {
}
}
});
};
$(document).on("click", "#checkAllItems", function () {
$(".checkBox").prop('checked',
$(this).prop('checked'));
});
$(document).on("click", "#btnDeleteReturn", function () {
var selectedIDs = ValidateDeleteData();
if (selectedIDs.length > 0)
{ $('#deletePopup').modal("show"); }
else {
$("#myAlert").modal({ show: true, backdrop: 'static', keyboard: false });
$("#myAlert .modal-header").addClass("delete");
$("#myAlert div#AlertMsg").empty();
$("#myAlert div#AlertMsg").append('Please select an item');
}
});
//To validate Sales item for delete
function ValidateDeleteData() {
var selectedItems = new Array();
$('input:checkbox.checkBox').each(function () {
if ($(this).prop('checked')) {
selectedItems.push($(this).val());
}
});
return selectedItems;
};
//For Delete Action
$(document).on("click", "#btnDelete", function () {
var selectedIds = new Array();
$('input:checkbox.checkBox').each(function () {
if ($(this).prop('checked')) {
selectedIds.push($(this).val());
}
});
var options = {};
options.url = RootUrl + "/Sales/DeleteReturn";
options.type = "POST";
options.data = JSON.stringify(selectedIds);
options.contentType = "application/json";
options.dataType = "json";
options.success = function (msg) {
window.location.href = RootUrl + "/Sales/SalesReturnSummary";
};
options.error = function (error) {
$("label[for='Error']").text(error.msg);
};
$.ajax(options);
});
/////////////////////End of Sales Summary//////////////////////////
No comments:
Post a Comment