/*--------------------------文档信息-------------------------------------------
///
///功能：    采购管理
///创建人：  王思波
///创建时间：2011年05月31日
///修改人：  --------------
///修改时间：--------------
///详细说明：--------------
///
-----------------------------------------------------------------------------*/

//页面全局变量的定义
var strTableP0101 = "Tp0101";       //表格ID
var strPageP0101 = "divPageP0101";  //分页控件ID
var strSelRowIDP0101 = "";          //选择行的ID
var objSelRowP0101 = {};            //选择行的数据
var objPageParamP0101 = { sortfield: "", orderby: "", where: "", flag: 0 }; //分页控件使用


/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**保存和关闭 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//保存按钮调用方法
function saveMyData(strDialog, strType, strTable) {
    try {
        if (strTable == "Tp010101") {
            afterSaveClick(strDialog, strType);     //采购单保存
        }
        else if (strTable == "Tp010102") {
            saveMyDataTp010102(strDialog, strType); //退货单保存
        }
        else if (strTable == "Tp010103") {
            saveMyDataTp010103();                   //付款单保存
        }
        else if (strTable == ciho.layal.table) {
            ciho.saveType();                        //添加分类
        }
    } catch (e) { }
}

//窗口关闭按钮调用方法
function closeMyDlg(strDialog, strType, strTable) {
    if ($("#" + strDialog + "_divform").data("change")) {
        //提示用户保存信息
        window.parent.cihoAlert(msg.other.haveChange, msgBox.dlgBtn.YesNoCancel, function (result) {
            //是
            if (result == msgBox.dlgRst.Yes) {
                saveMyData(strDialog, strType, strTable);
            }
            //否
            else if (result == msgBox.dlgRst.No) {
                //$("#" + strDialog).dialog("close");
                cihoDlgClose(strDialog);
                freeMemory_P010102();
                freeMemory_P010103();
            }
        });
    }
    else {
        //关闭窗口
        //$("#" + strDialog).dialog("close");
        cihoDlgClose(strDialog);
        freeMemory_P010102();
        freeMemory_P010103();
    }
}

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**保存和关闭 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/


/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**事件方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//页面加载事件
function pageLoad_P0101() {
    try {
        //选择日期范围
        loadDateExtent_P0101();
        //加载供应商信息
        loadProviderData_P0101();
        //药品下拉框信息的加载
        loadDrugData_P0101();
        //单据类型下拉框的设置
        loadBillsType_P0101();

        //获得jqGrid的列名和属性
        var objConfig = ParseData(window.external.DoOptions(strTableP0101, "GetTableConfig", "{}"));
        //表格格式化方法的设置
        setColFormat_P0101(objConfig.colModel);
        //创建表格
        createGrid(strTableP0101, objConfig.colNames, objConfig.colModel, [], null, null, true, dblClickRow_P0101);
        //表格事件的设置
        setGridEvent_P0101();
        //调整表格的大小
        //resizeTbl(true, strTableP0101, true, true);
        resizeGrid(strTableP0101, true, true);

        //创建分页
        createPage(strPageP0101, strTableP0101);

        //加载表格数据
        searchDataClick_P0101();

        //调整表格宽度
        //resizeTbl(true, strTableP0101, true, true);
        //resizeGrid(strTableP0101);
        //释放内存
        objConfig = null;
    } catch (e) { }
}

//查询按钮的事件
function searchDataClick_P0101() {
    try {
        //参数的更新
        objPageParamP0101.sortfield = "djdate"; //排序字段
        objPageParamP0101.orderby = "desc";     //排序顺序
        objPageParamP0101.flag = 1;             //表示点击查询按钮
        loadGridData_P0101();                   //加载数据
        objPageParamP0101.flag = 0;             //使用完后，设置为零  

        //清空选择行信息
        strSelRowIDP0101 = "";
        objSelRowP0101 = {};

        //有滚动条时改变表格宽度
        //resizeGrid(strTableP0101);
    } catch (e) { }
}

//删除按钮事件
function deleteData_P0101() {
    try {
        window.parent.cihoAlert(msg.del.delConfirm, msgBox.dlgBtn.YesNo, function (result) {
            if (result == msgBox.dlgRst.Yes) {
                //删除数据库信息
                var objResult = ParseData(window.external.DoOptions(strTableP0101, 'Delete', "{djbh:\"" + objSelRowP0101.djbh + "\",djdate:\"" + objSelRowP0101.djdate + "\"}"));

                //数据删除后的操作
                if (objResult) {
                    //删除成功后，重新加载数据
                    if (objResult.ReturnCode == 0) {
                        //清空选择行信息
                        strSelRowIDP0101 = "";
                        objSelRowP0101 = {};

                        //重新加载数据
                        objPageParamP0101.flag = 2;
                        loadGridData_P0101();
                        objPageParamP0101.flag = 0;
                    }
                    else {
                        window.parent.cihoAlert(objResult.MSG);
                    }
                }

                //释放内存
                objResult = null;
            }
        });
    } catch (e) { }
}

//日期范围变更事件
function dateTimeChange_P0101() {
    if ($("#cmbDateExtentP0101").combobox("getText") != "自定义") {
        $("#cmbDateExtentP0101").combobox("setValue", "");
        $("#cmbDateExtentP0101").combobox("setText", "自定义");
    }
}

//双击行事件
function dblClickRow_P0101() {
    //采购单
    if (objSelRowP0101.djtypebh == "101") {
        openBuy(2);
    }
    //采购退货单
    else if (objSelRowP0101.djtypebh == "102") {
        showDialog_P010102('2');
    }
    //期初单据
    else if (objSelRowP0101.djtypebh == "003") {
        window.parent.cihoAlert(msg.cg.viewQcdj);
    }
}

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**事件方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/


/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓**自定义方法 开始**↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/

//日期范围的选择方法
function loadDateExtent_P0101() {
    //设置默认日期为本月
    dateExtentCalculation("本月", "dateStartTimeP0101", "dateEndTimeP0101");

    //设置属性
    $("#cmbDateExtentP0101").combobox({
        width: _iWidth100,
        listWidth: _iWidth100,
        panelHeight: 102,
        valueField: "dm",
        textField: "mc",
        editable: false,
        //选择事件
        onSelect: function (option) {
            dateExtentCalculation(option.dm, "dateStartTimeP0101", "dateEndTimeP0101");
        },
        //回车事件
        enterDown: function () {
            dateExtentCalculation($(this).combobox("getValue"), "dateStartTimeP0101", "dateEndTimeP0101");
        }
    });

    //加载数据
    $("#cmbDateExtentP0101").combobox("loadData", [{ dm: "本月", mc: "本月", selected: true },
                                                   { dm: "上一月", mc: "上一月" },
                                                   { dm: "本周", mc: "本周" },
                                                   { dm: "过去三个月", mc: "过去三个月" },
                                                   { dm: "本年", mc: "本年"}]);
}

//供应商信息的加载
function loadProviderData_P0101() {
    //属性的设置
    $("#cmbProviderP0101").combobox({
        width: _iWidth150,
        listWidth: _iWidth150,
        valueField: "dm",
        textField: "mc",
        //获得焦点事件
        onfocus: function () {
            if ($("#cmbProviderP0101").combobox("getText") == "全部供应商") {
                $("#cmbProviderP0101").combobox("setValue", undefined);
                $("#cmbProviderP0101").combobox("setText", "");
            }
        },
        //失去焦点事件
        onblur: function () {
            if ($("#cmbProviderP0101").combobox("getText") == "") {
                $("#cmbProviderP0101").combobox("setValue", "");
            }
        }
    });

    //加载数据
    var objProvider = getComboboxData("Tgysxx2");
    objProvider.unshift({ "dm": "", "mc": "全部供应商", "selected": true });
    $("#cmbProviderP0101").combobox("loadData", objProvider);

    //释放内存
    objProvider = null;
}

//药品信息的加载
function loadDrugData_P0101() {
    //属性的设置
    $("#cmbDrugP0101").combobox({
        width: _iWidth150,
        listWidth: _iWidth150,
        valueField: "dm",
        textField: "mc",
        //获得焦点事件
        onfocus: function () {
            if ($("#cmbDrugP0101").combobox("getText") == "全部药品") {
                $("#cmbDrugP0101").combobox("setValue", undefined);
                $("#cmbDrugP0101").combobox("setText", "");
            }
        },
        //失去焦点事件
        onblur: function () {
            if ($("#cmbDrugP0101").combobox("getText") == "") {
                $("#cmbDrugP0101").combobox("setValue", "");
            }
        }
    });

    //加载数据
    var objMedicine = getComboboxData("Tspxx2");
    objMedicine.unshift({ "dm": "", "mc": "全部药品", "selected": true });
    $("#cmbDrugP0101").combobox("loadData", objMedicine);

    //释放内存
    objMedicine = null;
}

//单据类型信息的加载
function loadBillsType_P0101() {
    //属性的设置
    $("#cmbBillsTypeP0101").combobox({
        width: _iWidth120,
        listWidth: _iWidth120,
        panelHeight: 82,
        valueField: "dm",
        textField: "mc",
        editable: false,
        //获得焦点事件
        onfocus: function () {
            if ($("#cmbBillsTypeP0101").combobox("getText") == "全部单据类型") {
                $("#cmbBillsTypeP0101").combobox("setValue", undefined);
                $("#cmbBillsTypeP0101").combobox("setText", "");
            }
        },
        //失去焦点事件
        onblur: function () {
            if ($("#cmbBillsTypeP0101").combobox("getText") == "") {
                $("#cmbBillsTypeP0101").combobox("setValue", "");
            }
        }
    });

    //加载数据
    $("#cmbBillsTypeP0101").combobox("loadData", [{ dm: "", mc: "全部单据类型", selected: true },
                                                  { dm: "101", mc: "采购单" },
                                                  { dm: "102", mc: "退货单" },
                                                  { dm: "003", mc: "期初应付"}]);
}

//加载数据
function loadGridData_P0101(strFlag) {
    var objDataSet = {}; //数据的取得
    //供应商
    if ($("#cmbProviderP0101").combobox("getValue") == undefined) {
        $("#cmbProviderP0101").combobox("setValue", "");
    }
    //药品
    if ($("#cmbDrugP0101").combobox("getValue") == undefined) {
        $("#cmbDrugP0101").combobox("setValue", "");
    }
    if (!strFlag) {
        //更新查询条件
        objPageParamP0101.where = getWhereJson_P0101();
    }
    //数据的取得
    objDataSet = loadGridData_Page(objPageParamP0101);
    //合计的计算
    if (objDataSet) {
        if (objDataSet.Records.length > 0) {
            $("#" + strTableP0101).footerData("set", { rn: "合计",
                a2: parseFloat(objDataSet.a2).toFixed(window.parent.strCountPlace),              //数量
                a4: parseFloat(objDataSet.a4).toFixed(window.parent.strMoneyPlace),              //金额
                youhui: parseFloat(objDataSet.youhui).toFixed(window.parent.strMoneyPlace),      //优惠
                yifukuan: parseFloat(objDataSet.yifukuan).toFixed(window.parent.strMoneyPlace),  //已付款
                nopay: parseFloat(objDataSet.nopay).toFixed(window.parent.strMoneyPlace),        //未付款
                a8: parseFloat(objDataSet.a8).toFixed(window.parent.strMoneyPlace)               //应收发票金额
            }, false);
        }
        else {
            $("#" + strTableP0101).footerData("set", { rn: "合计", a2: "&nbsp;", a4: "&nbsp;", youhui: "&nbsp;", yifukuan: "&nbsp;", nopay: "&nbsp;", a8: "&nbsp;" }, false);
        }
    }

    //释放内存
    objDataSet = null;
}

//表格格式化方法的设置
function setColFormat_P0101(objColModel) {
    for (var intI = 0; intI < objColModel.length; intI++) {
        switch (objColModel[intI].name) {
            case "a2": //数量
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue != "") {
                        return parseFloat(cellvalue).toFixed(window.parent.strCountPlace);
                    }
                    else {
                        return "0";
                    }
                }
                break;
            case "price": //单价
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue != "") {
                        return parseFloat(cellvalue).toFixed(window.parent.strPricePlace);
                    }
                    else {
                        return "0";
                    }
                }
                break;
            case "youhui": //优惠
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (rowObject.djtypebh != "102") {
                        if (cellvalue != "") {
                            return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
                        }
                        else {
                            return "0";
                        }
                    }
                    else {
                        return "&nbsp;";
                    }
                }
                break;
            case "a4":          //金额
            case "yifukuan":    //已付款
            case "nopay":       //未付款
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue != "") {
                        return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
                    }
                    else {
                        return "0";
                    }
                }
                break;
            case "a8":          //应收发票金额
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (rowObject.a7 == "True") {
                        if (cellvalue != "") {
                            return parseFloat(cellvalue).toFixed(window.parent.strMoneyPlace);
                        }
                        else {
                            return "0";
                        }
                    }
                    else {
                        return "&nbsp;";
                    }
                }
                break;
            case "a7":          //是否开票
                objColModel[intI].formatter = function (cellvalue, options, rowObject) {
                    if (cellvalue == "False") {
                        return "否";
                    }
                    else if (cellvalue == "True") {
                        return "是";
                    }
                }
                break;
        }
    }
}

//表格事件的设置
function setGridEvent_P0101() {
    $("#" + strTableP0101).setGridParam({
        //选择行事件
        onSelectRow: function (rowid) {
            //记录选中行的信息
            strSelRowIDP0101 = rowid;
            objSelRowP0101 = $(this).getRowData(rowid);

            //浮动按钮的设置
            setFloatButton_P0101();
        },
        //排序事件
        onSortCol: function (index, colindex, sortorder) {
            $("div.operate").hide();                //隐藏浮动按钮
            objPageParamP0101.sortfield = index;    //排序字段
            objPageParamP0101.orderby = sortorder;  //排序顺序
            loadGridData_P0101(true);                   //加载数据
            return "stop";
        },
        //表格加载完成事件
        gridComplete: function () {
            var arrDataRowID = $(this).getDataIDs();
            for (var intI = 0; intI < arrDataRowID.length; intI++) {
                //设置退货单的字体颜色
                if ($(this).getCell(arrDataRowID[intI], "djtypebh") == "102") {
                    $("#" + arrDataRowID[intI]).css("color", "#996600");
                }
            }
        }
    });
}

//浮动按钮的设置
function setFloatButton_P0101() {
    var strButtonHtml = "";
    //浮动按钮内容
    if (objSelRowP0101.djtypebh == "101") {
        strButtonHtml = "<a onclick=\"showDialog_P010103()\">付款</a>-<a onclick=\"openBuy(2)\">修改</a>-<a onclick=\"deleteData_P0101()\">删除</a>"; //<a onclick=\"showDialog_P010102('3')\">退货</a>-
    }
    else if (objSelRowP0101.djtypebh == "102") {
        strButtonHtml = "<a onclick=\"showDialog_P010102('2')\">修改</a>-<a onclick=\"deleteData_P0101()\">删除</a>";
    }
    else if (objSelRowP0101.djtypebh == "003") {
        strButtonHtml = "<a onclick=\"showDialog_P010103()\">付款</a>"; //-<a onclick=\"showDialog_P010104()\">查看</a>
    }
    createFloatButton.call($("#" + strTableP0101), strSelRowIDP0101, strButtonHtml);

    //释放内存
    strButtonHtml = null;
}

//查询条件json字符串的取得
function getWhereJson_P0101() {
    var strWhereJson = "";
    strWhereJson += "\"sdate\":\"" + $("#dateStartTimeP0101").val() + "\",";
    strWhereJson += "\"edate\":\"" + $("#dateEndTimeP0101").val() + "\",";
    strWhereJson += "\"gysdm\":\"" + $("#cmbProviderP0101").combobox("getValue") + "\",";
    strWhereJson += "\"spdm\":\"" + $("#cmbDrugP0101").combobox("getValue") + "\",";
    strWhereJson += "\"djtypebh\":\"" + $("#cmbBillsTypeP0101").combobox("getValue") + "\"";
    return strWhereJson;
}

/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑**自定义方法 结束**↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/