lightswitch html - 不计算的总金额

时间:2015-05-28 00:44:28

标签: javascript lightswitch-2013

我试图在lightswitch html中构建一个简单的p.o.s条目。而我的问题是总金额不在正确的事件中计算。我是所有这些javascript和lightswitch的新手,所以请原谅我的清白。

我在我的" updateTotal函数中插入了一个断点"我明确的问题是,一旦我点击addsales详细信息(当还没有数据时)它会调用updateTotal函数。需要做的是在选择产品后应该调用updateTotal 我做错了什么?

以下是我写的代码。有谁能够帮我?     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));

    }

1 个答案:

答案 0 :(得分:-1)

这将解决你的问题:我所做的就是将时间间隔添加到底部的代码中,因此它知道多次检查总数...后期渲染函数通常只调用一次所以内部函数不是实际上被叫了。

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);

以下代码解决了Amt折扣和净金额中的NaN:

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;
           }
       });
};