/**
 * Created by wangwei on 2017-2-14.
 */

function getEveryMonthReceiptAmount(contextPath){
    $.ajax({
        type: 'GET',
        url: contextPath + '/csh/write/off/getEveryMonthPaymentAndReceiptAmount/query',
        contentType: "application/json; charset=utf-8",
        success: function (datas) {

            var  yearReceiptAmount=0;
            var  monthReceiptAmount=0;
            var  yearPaymentAmount=0;
            var  monthPaymentAmount=0;
            //初始化应收
            if (datas.rows[0]) {
                tmpDate = new Date();
                month = tmpDate.getMonth() + 1 ;
                for(i=0;i<datas.rows.length;i++){
                    if(datas.rows[i].writeOffType=="RECEIPT") {
                        yearReceiptAmount = yearReceiptAmount + datas.rows[i].writeOffDueAmount;
                        if (month == datas.rows[i].writeOffMonth) {
                            monthReceiptAmount = datas.rows[i].writeOffDueAmount;
                        }
                    }
                    if(datas.rows[i].writeOffType=="PAYMENT") {
                        yearPaymentAmount = yearPaymentAmount + datas.rows[i].writeOffDueAmount;
                        if (month == datas.rows[i].writeOffMonth) {
                            monthPaymentAmount = datas.rows[i].writeOffDueAmount;

                        }
                    }
                }


            } else {
            }
            $("#year-receipt-amount").append(formatCurrency(yearReceiptAmount.toFixed(2)));
            $("#month-receipt-amount").append(formatCurrency(monthReceiptAmount.toFixed(2)));
            $("#month-payment-amount").append(formatCurrency(monthPaymentAmount.toFixed(2)));
            $("#year-payment-amount").append(formatCurrency(yearPaymentAmount.toFixed(2)));
            //初始化Receipt图表
            var everyReceiptMonthStr="";
            var monthReceiptTotalStr="";
            var monthReceiptTotal=0;
            for(i=1;i<=12;i++){
                var monthReceiptAmount=0;

                for(j=0;j<datas.rows.length;j++){
                    if(i==datas.rows[j].writeOffMonth&&datas.rows[j].writeOffType=="RECEIPT"){
                        monthReceiptAmount = parseFloat((datas.rows[j].writeOffDueAmount).toFixed(2));
                    }
                }
                monthReceiptTotal= monthReceiptTotal+ monthReceiptAmount;
                if(i==1){
                    everyReceiptMonthStr=everyReceiptMonthStr+ monthReceiptAmount;
                    monthReceiptTotalStr=monthReceiptTotalStr+parseFloat(monthReceiptTotal.toFixed(2));
                }else{
                    everyReceiptMonthStr=everyReceiptMonthStr+','+monthReceiptAmount;
                    monthReceiptTotalStr=monthReceiptTotalStr+','+ parseFloat(monthReceiptTotal.toFixed(2));
                }

            }
            $('#recepipt-span').attr("data-composite",everyReceiptMonthStr);
            $('#recepipt-span').text(monthReceiptTotalStr);
            //初始化Payment图表
            var everyPaymentMonthStr=0;
            var monthPaymentTotalStr=0;
            var monthPaymentTotal=0;
            for(i=1;i<=12;i++){
                var monthPaymentAmount=0;

                for(j=0;j<datas.rows.length;j++){
                    if(i==datas.rows[j].writeOffMonth&&datas.rows[j].writeOffType=="PAYMENT"){
                        monthPaymentAmount= parseFloat((datas.rows[j].writeOffDueAmount).toFixed(2));
                    }
                }
                monthPaymentTotal=monthPaymentTotal+monthPaymentAmount;
                if(i==1){
                    everyPaymentMonthStr=everyPaymentMonthStr+monthPaymentAmount;
                    monthPaymentTotalStr=monthPaymentTotalStr+parseFloat(monthPaymentTotal.toFixed(2));
                }else{
                    everyPaymentMonthStr=everyPaymentMonthStr+','+monthPaymentAmount;
                    monthPaymentTotalStr=monthPaymentTotalStr+','+parseFloat(monthPaymentTotal.toFixed(2));
                }

            }

            $('#payment-span').attr("data-composite",everyPaymentMonthStr);
            $('#payment-span').text(monthPaymentTotalStr);
            InitiateSparklineCharts.init();
        }

    });
}






