Search code examples
javascriptlightswitch-2013

lightswitch html - total amount not calculating


I'm trying to build a simple p.o.s entry in lightswitch html. And my problem is the total amount is not calculating in the correct event. I'm new to all this javascript and lightswitch so please forgive my innocence.

I have inserted a breakpoint in my "updateTotal function" the problem I have clearly is that it calls the updateTotal function once I click the addsales detail(When there is no data yet). What needs to happen is that it should call the updateTotal after I selected a product.
What am I doing wrong?

Below is the code I wrote. Can anybody help me? myapp.AddEditSalesHeader.created = function (screen) {

 // Write code here.
    //Set the default values of the following fields.
    screen.SalesHeader.InvoiceNo = "(Auto Invoice #)";
    var vTime = new Date();
    screen.SalesHeader.InvoiceTime = vTime;
    screen.SalesHeader.ModePmt = "CASH";
    screen.SalesHeader.Shipment = "PICK-UP";
    screen.SalesHeader.PerDiscount = 0;
    screen.SalesHeader.TotalDiscount = 0;
    screen.SalesHeader.NetAmount = 0;
    screen.SalesHeader.AmountReturn = 0;
    screen.SalesHeader.CashReceived = 0;
    screen.SalesHeader.ChangeDue = 0;
    screen.findContentItem("InvoiceNo").isReadOnly = true;
    screen.findContentItem("InvoiceDate").isReadOnly = true;
    screen.findContentItem("TotalAmount").isReadOnly = true;
    screen.findContentItem("NetAmount").isReadOnly = true;   
};

myapp.AddEditSalesHeader.PerDiscount_postRender = function (element, contentItem) {

    //Compute TotalAmount & NetAmount when PerDiscount value is entered
    contentItem.dataBind("value",
           function (PerDiscount) {
               var vADisc = contentItem.screen.SalesHeader.TotalAmount * (PerDiscount / 100);
               contentItem.screen.SalesHeader.TotalDiscount = vADisc;
               contentItem.screen.SalesHeader.NetAmount = contentItem.screen.SalesHeader.TotalAmount - vADisc;
           });
};

myapp.AddEditSalesHeader.TotalAmount_postRender = function (element, contentItem) {

    function updateTotal() {
        var InvDetail = contentItem.screen.SalesDetails.data;
        var AmtDetail = 0;

        InvDetail.forEach(function (sales) {
            if (isNaN(sales.Amount)) {
                AmtDetail = 0;
           }
            else {
                //Update the TotalAmount
                AmtDetail = AmtDetail + sales.Amount;
            }
        })
        //Display the TotalAmount
        contentItem.screen.SalesHeader.TotalAmount = AmtDetail;
        contentItem.screen.SalesHeader.NetAmount = contentItem.screen.SalesHeader.TotalAmount
                 - contentItem.screen.SalesHeader.TotalDiscount;

    }
    //Set up a databind on screen.SaleDetails.count
    contentItem.dataBind("screen.SalesDetails.count", function () {
        updateTotal();}

 );
    }

    myapp.AddEditSalesHeader.TotalDiscount_postRender = function (element, contentItem) {
        // Write code here.
        //Compute PerDiscount & NetAmount when the TotalDiscount value is entered
        contentItem.dataBind("value",
               function (TotalDiscount) {
                   if (TotalDiscount > 0) {
                       contentItem.screen.SalesHeader.PerDiscount = (TotalDiscount / contentItem.screen.SalesHeader.TotalAmount) * 100;
                       contentItem.screen.SalesHeader.NetAmount = contentItem.screen.SalesHeader.TotalAmount - TotalDiscount;
                   }
                   else {
                       contentItem.screen.SalesHeader.PerDiscount = 0;
                   }
               });
    };

    myapp.AddEditSalesHeader.InvTime_postRender = function (element, contentItem) {
        // Write code here.
        this.setInterval(
                 function () {
                     var currentTime = new Date();

                     $(element).text(currentTime.toLocaleTimeString());
                 },
                 1000
                 );
    };


    //function CallGetTotAmt(operation) {
    //    $.ajax({
    //        type: 'post',
    //        data: {},
    //        url: '../Web/GetTotAmount.ashx',
    //        success: operation.code(function AjaxSuccess(AjaxResult) {
    //            operation.complete(AjaxResult);
    //        })
    //    });
    //}


    myapp.AddEditSalesHeader.Details_postRender = function (element, contentItem) {
        // Write code here.
        //Hide the save command button
        $("[data-ls-tap='tap:{data.shell.saveCommand.command}']").hide();
        //Hide the discard command button
        $("[data-ls-tap='tap:{data.shell.discardCommand.command}']").hide();
    };

    myapp.AddEditSalesHeader.Cancel_execute = function (screen) {
        // Write code here.    
        msls.showMessageBox("Close transaction? Closing will cause any unsaved changes to be discarded.", {
            title: "System Advisory",
            buttons: msls.MessageBoxButtons.yesNo
        })
            .then(function (result) {
                if (result === msls.MessageBoxResult.yes) {
                    myapp.cancelChanges();
                }
            });
    };


    myapp.AddEditSalesHeader.Save_execute = function (screen) {
        // Write code here.
        //Get the total data in SalesHeader
        myapp.activeDataWorkspace.ApplicationData.SalesHeaders.includeTotalCount().execute().then(function (result) {
            var varInvNo = AutoInvNo(result);
            //Set InvoiceNo before saving
            screen.SalesHeader.InvoiceNo = varInvNo;

            var InvDetail = screen.SalesDetails.data;
            var varStock = 0;
            InvDetail.forEach(function (entity) {
                //Update ProductFile StockOnHand that have products in SalesDetail
                varStock = entity.ProductFile.StockOnHand - entity.Quantity;
                entity.ProductFile.StockOnHand = varStock;
                entity.ProductFile.TranCode = "O";
                entity.InvoiceNo = varInvNo;
            })

            myapp.commitChanges().then(null, function fail(e) {
                msls.showMessageBox(e.message, { title: e.title }).then(function () {
                    screen.SalesHeader.InvoiceNo = "(Auto Invoice #)";
                    throw e;
                });
            });

        });
    };


    //Utility
    function AutoInvNo(InvNo) {
        var InvNumber = 1;
        var Entities = InvNo.results;
        //Count the total data in SalesHeader
        Entities.forEach(function (entity) {
            InvNumber = InvNumber + 1;
        });
        //Return InvNumber value
        return InvNumber;
    };


    myapp.AddEditSalesHeader.Print_execute = function (screen) {
        // Write code here.
        var LoadingDiv = $("<div></div>");
        var Loading = $("<h1>&nbsp;&nbsp;&nbsp;&nbsp;Loading Report...</h1>");
        Loading.appendTo(LoadingDiv);
        LoadingDiv.appendTo($(element));
        // Get Report
        var HTMLContent = $("<div></div>").html(
            "<object width='650px' height='650px' data='../Reports/BranchSalesReport.rdlc'/>"
            );
        // Make report appear on top of the Loading message
        HTMLContent.css({
            "margin-top": "-50px"
        });
        HTMLContent.appendTo($(element));

    }

Solution

  • this will solve you problem: alls I have done is add the time interval to the code at the bottom so it knows to check the total multiple times... the post render function is only called once usually so the function inside wasn't actually being called.

    function updateTotal() {
            var InvDetail = contentItem.screen.SalesDetails.data;
            var AmtDetail = 0;
            InvDetail.forEach(function (sales) {
                if (isNaN(sales.Amount)) {
                    AmtDetail = 0;
                }
                else {
                    //Update the TotalAmount
                    AmtDetail = AmtDetail + sales.Amount;
                }
            })
            //Display the TotalAmount
            contentItem.screen.SalesHeader.TotalAmount = AmtDetail;
            contentItem.screen.SalesHeader.NetAmount =      contentItem.screen.SalesHeader.TotalAmount
                     - contentItem.screen.SalesHeader.TotalDiscount;
    }
    
    
    this.setInterval(
        function () {
    //Set up a databind on screen.SaleDetails.count
    contentItem.dataBind("screen.SalesDetails.count", updateTotal);
      },1000);
    

    and the below code solves the NaN within the Amt Discount and Net Amount:

    myapp.AddEditSalesHeader.TotalDiscount_postRender = function (element, contentItem) {
    // Write code here.
    //Compute PerDiscount & NetAmount when the TotalDiscount value is entered
    contentItem.dataBind("value",
           function () {
               if (contentItem.value > 0) {
                   contentItem.screen.SalesHeader.PerDiscount = (contentItem.value / contentItem.screen.SalesHeader.TotalAmount) * 100;
                   contentItem.screen.SalesHeader.NetAmount = contentItem.screen.SalesHeader.TotalAmount - contentItem.value;
               }
               else {
                   contentItem.value = 0;
               }
           });
    };