# app_script_history

id: 66c430e3993294a6b34860b1
scriptid: 669dc6399932df22f60ce47f
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.20 17:16:53.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);

    if(urlArgs.action=='view'){
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;

        if(data.flow_instance_status.indexOf('结束')>0){
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    }else{

        if(data.flow_instance_status.indexOf('接收方签收确认')>0){
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;
            
            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        }else{
            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }
        
    }

}

//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    
    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }
    
    if (data.pzh2 < data.pzh1) {
        toastr.error( '终止凭证号要比起始凭证号大' );
        return false;
    }

    toastr.show("正在计算...");
        setTimeout(function () {
            back_add_pzh(data, function () {
                toastr.hide();
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            });
        }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');
    
    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }
    

    toastr.show("正在计算...");
        setTimeout(function () {
            back_import_pzh_data(data, function () {
                toastr.hide();
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            });
        }, 100);
}


function cb_buildsum_onClick() {
    $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
        toastr.show("正在计算...");
        setTimeout(function () {
            back_build_sum(function () {
                toastr.hide();
                toastr.info("导入完成");
                $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();

            });
        }, 100);

    });

}


/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
                chang_qsqx();
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
            chang_qsqx();
        }

    }


}

function chang_qsqx() {
    var count = $q_pz_yjd_pzh_ds.getRowCount();
    var qsqx = 3;
    for (var i = 0; i < count; i++) {
        var qsqx1 = $q_pz_yjd_pzh_ds.getValue(i, 'qsqx');
        if (qsqx < qsqx1) {
            qsqx = qsqx1;
        }
    }

    $pz_yjd_ds.setValue(0, 'qsqx', qsqx);
}


/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c54b8a993294a6b3486135
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 13:29:36.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

console.log(data.flow_instance_status)
        if (data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}


//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
                chang_qsqx();
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
            chang_qsqx();
        }

    }


}

function chang_qsqx() {
    var count = $q_pz_yjd_pzh_ds.getRowCount();
    var qsqx = 3;
    for (var i = 0; i < count; i++) {
        var qsqx1 = $q_pz_yjd_pzh_ds.getValue(i, 'qsqx');
        if (qsqx < qsqx1) {
            qsqx = qsqx1;
        }
    }

    $pz_yjd_ds.setValue(0, 'qsqx', qsqx);
}


/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c54b8a993294a6b3486139
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 13:30:50.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd(dsname, rowCount, withLastSelect) {
    if (dsname == 'pz_yjd') {
        var data = $pz_yjd_ds.getJSON(0);
        // console.log(data)

        if (urlArgs.action == 'view') {
            book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
            book.getWorkSheet('清单').getBrick('cb_import').visible = false;
            book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


            book.getWorkSheet('清单').cells('qsqx').visible = false;
            book.getWorkSheet('清单').cells('qsqx').clickable = false;

            book.getWorkSheet('清单').cells('state').visible = false;
            book.getWorkSheet('清单').cells('state').clickable = false;


            if (data.flow_instance_status.indexOf('结束') > 0) {
                book.getWorkSheet('清单').cells('qsqx').visible = true;
                book.getWorkSheet('清单').cells('state').visible = true;
            }

        } else {

            if (data.flow_instance_status.indexOf('接收方签收确认') > 0) {
                book.getWorkSheet('清单').cells('qsqx').editable = true;
                book.getWorkSheet('清单').cells('state').editable = true;

                book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
                book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
            } else {

                book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
                book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
            }

        }
    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue > '0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
                chang_qsqx();
            }, false, {
                    onContentReady: function () {
                        $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                        window.qsqx_old = 0;
                    }
                }, 10);
        } else {
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
            chang_qsqx();
        }

    }


}

function chang_qsqx() {
    var count = $q_pz_yjd_pzh_ds.getRowCount();
    var qsqx = 3;
    for (var i = 0; i < count; i++) {
        var qsqx1 = $q_pz_yjd_pzh_ds.getValue(i, 'qsqx');
        if (qsqx < qsqx1) {
            qsqx = qsqx1;
        }
    }

    $pz_yjd_ds.setValue(0, 'qsqx', qsqx);
}


/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c54b8a993294a6b348613d
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 13:31:24.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd(dsname, rowCount, withLastSelect) {
    if (dsname == 'pz_yjd') {
        var data = $pz_yjd_ds.getJSON(0);
        // console.log(data)

        if (urlArgs.action == 'view') {
            book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
            book.getWorkSheet('清单').getBrick('cb_import').visible = false;
            book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


            book.getWorkSheet('清单').cells('qsqx').visible = false;
            book.getWorkSheet('清单').cells('qsqx').clickable = false;

            book.getWorkSheet('清单').cells('state').visible = false;
            book.getWorkSheet('清单').cells('state').clickable = false;


            if (data.flow_instance_status.indexOf('结束') > 0) {
                book.getWorkSheet('清单').cells('qsqx').visible = true;
                book.getWorkSheet('清单').cells('state').visible = true;
            }

        } else {
console.log(data)
            if (data.flow_instance_status.indexOf('接收方签收确认') > 0) {
                book.getWorkSheet('清单').cells('qsqx').editable = true;
                book.getWorkSheet('清单').cells('state').editable = true;

                book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
                book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
            } else {

                book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
                book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
            }

        }
    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue > '0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
                chang_qsqx();
            }, false, {
                    onContentReady: function () {
                        $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                        window.qsqx_old = 0;
                    }
                }, 10);
        } else {
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
            chang_qsqx();
        }

    }


}

function chang_qsqx() {
    var count = $q_pz_yjd_pzh_ds.getRowCount();
    var qsqx = 3;
    for (var i = 0; i < count; i++) {
        var qsqx1 = $q_pz_yjd_pzh_ds.getValue(i, 'qsqx');
        if (qsqx < qsqx1) {
            qsqx = qsqx1;
        }
    }

    $pz_yjd_ds.setValue(0, 'qsqx', qsqx);
}


/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c54b8a993294a6b3486141
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 13:31:50.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status&&data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status&&data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd (dsname,rowCount, withLastSelect) {
    if(dsname=='pz_yjd'){

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
                chang_qsqx();
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
            chang_qsqx();
        }

    }


}

function chang_qsqx() {
    var count = $q_pz_yjd_pzh_ds.getRowCount();
    var qsqx = 3;
    for (var i = 0; i < count; i++) {
        var qsqx1 = $q_pz_yjd_pzh_ds.getValue(i, 'qsqx');
        if (qsqx < qsqx1) {
            qsqx = qsqx1;
        }
    }

    $pz_yjd_ds.setValue(0, 'qsqx', qsqx);
}


/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c54b8a993294a6b348614c
scriptid: 669dc60c9932df22f60cad57
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 13:37:37.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if(urlArgs.action=='view'){
        
        book.getWorkSheet('main').getBrick('cb_import_pz').visible = false;

        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;

        if(data.flow_instance_status&&data.flow_instance_status.indexOf('结束')>0){
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    }else{
        
        if(data.flow_instance_status&&data.flow_instance_status.indexOf('接收方签收确认')>0){
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;
            
            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else{
            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

//@comment (group=按钮,caption=凭证资料导入)
function cb_import_pz_onClick() {
    var p_gguid = currentGUID();

    back_get_pzdj_gguid(p_gguid, function(gguid){
        $.dialog({
            title: '凭证资料导入',
            boxWidth: '1000px',
            useBootstrap: false,
            content: `<iframe  style="width:100%;height:400px; border:0" src="billedit?id=pzdj${gguid?`&action=edit&gguid=${gguid}`:'&action=new'}&from_id=${p_gguid}"></iframe>`
        });
    })

    // let dlg= floatDialog( "凭证资料导入" ,"billlist" , "pzdj" , 1000,500, {} , "listToolbarVisible=true&editToolbarVisible=true&from_id="+currentGUID()  , function(val){alert(val);});
}


//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
        toastr.show("正在计算...");
        setTimeout(function () {
            back_build_sum(function () {
                toastr.hide();
                toastr.info("导入完成");
                $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();

            });
        }, 100);

    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
                chang_qsqx();
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
            chang_qsqx();
        }

    }


}

function chang_qsqx() {
    var count = $q_pz_yjd_pzh_ds.getRowCount();
    var qsqx = 3;
    for (var i = 0; i < count; i++) {
        var qsqx1 = $q_pz_yjd_pzh_ds.getValue(i, 'qsqx');
        if (qsqx < qsqx1) {
            qsqx = qsqx1;
        }
    }

    $pz_yjd_ds.setValue(0, 'qsqx', qsqx);
}


/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c54b8a993294a6b3486151
scriptid: 669dc6249932df22f60cc8eb
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 13:37:49.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if(urlArgs.action=='view'){
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;

        if(data.flow_instance_status&&data.flow_instance_status.indexOf('结束')>0){
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    }else{

        if(data.flow_instance_status&&data.flow_instance_status.indexOf('接收方签收确认')>0){
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;
            
            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        }else{
            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }


}

//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    
    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }
    
    if (data.pzh2 < data.pzh1) {
        toastr.error( '终止凭证号要比起始凭证号大' );
        return false;
    }

    toastr.show("正在计算...");
        setTimeout(function () {
            back_add_pzh(data, function () {
                toastr.hide();
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            });
        }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');
    
    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }
    

    toastr.show("正在计算...");
        setTimeout(function () {
            back_import_pzh_data(data, function () {
                toastr.hide();
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            });
        }, 100);
}


function cb_buildsum_onClick() {
    $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
        toastr.show("正在计算...");
        setTimeout(function () {
            back_build_sum(function () {
                toastr.hide();
                toastr.info("导入完成");
                $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();

            });
        }, 100);

    });

}


/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
                chang_qsqx();
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
            chang_qsqx();
        }

    }


}

function chang_qsqx() {
    var count = $q_pz_yjd_pzh_ds.getRowCount();
    var qsqx = 3;
    for (var i = 0; i < count; i++) {
        var qsqx1 = $q_pz_yjd_pzh_ds.getValue(i, 'qsqx');
        if (qsqx < qsqx1) {
            qsqx = qsqx1;
        }
    }

    $pz_yjd_ds.setValue(0, 'qsqx', qsqx);
}


/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c54b8a993294a6b3486158
scriptid: 669dc6399932df22f60ce47f
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 13:38:06.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);

    if(urlArgs.action=='view'){
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;

        if(data.flow_instance_status&&data.flow_instance_status.indexOf('结束')>0){
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    }else{

        if(data.flow_instance_status&&data.flow_instance_status.indexOf('接收方签收确认')>0){
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;
            
            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        }else{
            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }
        
    }

}

//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    
    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }
    
    if (data.pzh2 < data.pzh1) {
        toastr.error( '终止凭证号要比起始凭证号大' );
        return false;
    }

    toastr.show("正在计算...");
        setTimeout(function () {
            back_add_pzh(data, function () {
                toastr.hide();
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            });
        }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');
    
    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }
    

    toastr.show("正在计算...");
        setTimeout(function () {
            back_import_pzh_data(data, function () {
                toastr.hide();
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            });
        }, 100);
}


function cb_buildsum_onClick() {
    $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
        toastr.show("正在计算...");
        setTimeout(function () {
            back_build_sum(function () {
                toastr.hide();
                toastr.info("导入完成");
                $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();

            });
        }, 100);

    });

}


/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
                chang_qsqx();
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
            chang_qsqx();
        }

    }


}

function chang_qsqx() {
    var count = $q_pz_yjd_pzh_ds.getRowCount();
    var qsqx = 3;
    for (var i = 0; i < count; i++) {
        var qsqx1 = $q_pz_yjd_pzh_ds.getValue(i, 'qsqx');
        if (qsqx < qsqx1) {
            qsqx = qsqx1;
        }
    }

    $pz_yjd_ds.setValue(0, 'qsqx', qsqx);
}


/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c54b8a993294a6b3486168
scriptid: 66bb02cf993294a6b3485671
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 13:50:58.000
script:
/**
 * 脚本中心的脚本，以 main函数为执行入口，相关的参数放在arg中，它是一个JSON字符串化后的数据
 *
 */
function main(arg) {
    println(arg);
    var param = JSON.parse(arg);

    var dsn = "";
    var sql = `select a.*, b.flow_instance_starttime, b.qsqx as qsqx2 from pz_yjd_pzh a left join pz_yjd b on a.gguid=b.gguid where b.flow_instance_state = 0 and b.flow_instance_starttime is not null and a.state=0`;
    // var list = getListFromSQL2( dsn, sql);


    // var list = getListFromSQL2( '', `select * from  pz_yjd `) 
    // var list = getListFromSQL2( '', `select gguid, sum(case when ifnull(state,0)=0 then 1 else 0 end) as count from  pz_yjd_pzh  group by gguid`) 

    // return list

    var ds = newDataStore(dsn, sql);
    ds.retrieve();
    println("select : " + ds.getSelectLastRetrieved())
    var count = ds.getRowCount();
    println('count: ' + count)
    for (var i = 0; i < count; i++) {
        var item = JSON.parse(ds.getJSONRow(i));
        var dayArr = item.flow_instance_starttime.replace(/ /g, '-').replace(/:/g, '-').replace(/\./g, '-').split('-');
        // println( 'item.flow_instance_starttime  ' + JSON.stringify(item.flow_instance_starttime) ) 

        var confirmTime = createDate2(dayArr[0], dayArr[1], dayArr[2], dayArr[3], dayArr[4], dayArr[5]);
        var nowTime = currentDate();
        
        // 开始时间和结束时间相差多少天
        var day_count = daysBetween((nowTime), (confirmTime));
        // println("day_count: " + day_count)
        // println("item.qsqx: " + (item.qsqx/1 > '0' ? item.qsqx : item.qsqx2 )  )


        if ( (item.qsqx/1 > '0' ? ( (item.qsqx/1)+ (item.qsqx2 /1) ) : item.qsqx2 ) >= day_count) {
            // println('  item.id ' + item.id)
            runSQL(dsn, `update pz_yjd_pzh set state=1 where id='${item.id}'`);
        }
    }


    var list = getListFromSQL2('', `select * from  pz_yjd a left join (select gguid, sum(case when ifnull(state,0)=0 then 1 else 0 end) as count from  pz_yjd_pzh  group by gguid) b on a.gguid=b.gguid where  a.flow_instance_state = 0 and b.count=0 `)

    // return list

    list.forEach(item => {
        println("延长确认，自动确认");
        var gguid = item.gguid, workid = item.open_bill_with_url.split('&workspaceid=')[0].split('&id=')[1];

        // println("workid  " + workid)
        // println("gguid  " + gguid)

        //递交结束
        var loopCount = 0;
        while (loopCount < 10) {
            loopCount++;
            // println(gguid);
            var t = taskInfoForWork(workid, gguid);
            println(JSON.stringify(t));
            if (t.taskList.length == 0) break;
            //将任务递交下去，如果任务需要签收，则先签收     
            for (var i = 0; i < t.taskList.length; i++) {
                var task = t.taskList[i];

                //可以递交，那么递交下去
                if (task.canSubmit) println(submitWork(workid, gguid, task.taskId));
                //需要先签收 ，那么以第一个候选人签收它
                if (task.canClaim) claimWork(workid, gguid, task.taskId, task.candidateUserList[0].userId);

            }


        }

    })
    // return list

    return 'over';

}


id: 66c54b8a993294a6b3486171
scriptid: 669dc6399932df22f60ce47f
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 13:51:54.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);

    if(urlArgs.action=='view'){
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;

        if(data.flow_instance_status&&data.flow_instance_status.indexOf('结束')>0){
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    }else{

        if(data.flow_instance_status&&data.flow_instance_status.indexOf('接收方签收确认')>0){
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;
            
            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        }else{
            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }
        
    }

}

//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    
    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }
    
    if (data.pzh2 < data.pzh1) {
        toastr.error( '终止凭证号要比起始凭证号大' );
        return false;
    }

    toastr.show("正在计算...");
        setTimeout(function () {
            back_add_pzh(data, function () {
                toastr.hide();
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            });
        }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');
    
    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }
    

    toastr.show("正在计算...");
        setTimeout(function () {
            back_import_pzh_data(data, function () {
                toastr.hide();
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            });
        }, 100);
}


function cb_buildsum_onClick() {
    $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
        toastr.show("正在计算...");
        setTimeout(function () {
            back_build_sum(function () {
                toastr.hide();
                toastr.info("导入完成");
                $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();

            });
        }, 100);

    });

}


/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c54b8a993294a6b3486176
scriptid: 669dc6249932df22f60cc8eb
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 13:52:16.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if(urlArgs.action=='view'){
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;

        if(data.flow_instance_status&&data.flow_instance_status.indexOf('结束')>0){
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    }else{

        if(data.flow_instance_status&&data.flow_instance_status.indexOf('接收方签收确认')>0){
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;
            
            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        }else{
            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }


}

//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    
    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }
    
    if (data.pzh2 < data.pzh1) {
        toastr.error( '终止凭证号要比起始凭证号大' );
        return false;
    }

    toastr.show("正在计算...");
        setTimeout(function () {
            back_add_pzh(data, function () {
                toastr.hide();
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            });
        }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');
    
    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }
    

    toastr.show("正在计算...");
        setTimeout(function () {
            back_import_pzh_data(data, function () {
                toastr.hide();
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            });
        }, 100);
}


function cb_buildsum_onClick() {
    $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
        toastr.show("正在计算...");
        setTimeout(function () {
            back_build_sum(function () {
                toastr.hide();
                toastr.info("导入完成");
                $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();

            });
        }, 100);

    });

}


/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c54b8a993294a6b348617b
scriptid: 669dc60c9932df22f60cad57
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 13:52:35.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if(urlArgs.action=='view'){
        
        book.getWorkSheet('main').getBrick('cb_import_pz').visible = false;

        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;

        if(data.flow_instance_status&&data.flow_instance_status.indexOf('结束')>0){
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    }else{
        
        if(data.flow_instance_status&&data.flow_instance_status.indexOf('接收方签收确认')>0){
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;
            
            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else{
            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

//@comment (group=按钮,caption=凭证资料导入)
function cb_import_pz_onClick() {
    var p_gguid = currentGUID();

    back_get_pzdj_gguid(p_gguid, function(gguid){
        $.dialog({
            title: '凭证资料导入',
            boxWidth: '1000px',
            useBootstrap: false,
            content: `<iframe  style="width:100%;height:400px; border:0" src="billedit?id=pzdj${gguid?`&action=edit&gguid=${gguid}`:'&action=new'}&from_id=${p_gguid}"></iframe>`
        });
    })

    // let dlg= floatDialog( "凭证资料导入" ,"billlist" , "pzdj" , 1000,500, {} , "listToolbarVisible=true&editToolbarVisible=true&from_id="+currentGUID()  , function(val){alert(val);});
}


//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
        toastr.show("正在计算...");
        setTimeout(function () {
            back_build_sum(function () {
                toastr.hide();
                toastr.info("导入完成");
                $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();

            });
        }, 100);

    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c54b8a993294a6b3486180
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 13:52:49.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status&&data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status&&data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd (dsname,rowCount, withLastSelect) {
    if(dsname=='pz_yjd'){

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c5812d993294a6b3486187
scriptid: 66bb02cf993294a6b3485671
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 13:54:54.000
script:
/**
 * 脚本中心的脚本，以 main函数为执行入口，相关的参数放在arg中，它是一个JSON字符串化后的数据
 *
 */
function main(arg) {
    println(arg);
    var param = JSON.parse(arg);

    var dsn = "";
    var sql = `select a.*, b.flow_instance_starttime, b.qsqx as qsqx2 from pz_yjd_pzh a left join pz_yjd b on a.gguid=b.gguid where b.flow_instance_state = 0 and b.flow_instance_starttime is not null and a.state=0`;
    // var list = getListFromSQL2( dsn, sql);


    // var list = getListFromSQL2( '', `select * from  pz_yjd `) 
    // var list = getListFromSQL2( '', `select gguid, sum(case when ifnull(state,0)=0 then 1 else 0 end) as count from  pz_yjd_pzh  group by gguid`) 

    // return list

    var ds = newDataStore(dsn, sql);
    ds.retrieve();
    println("select : " + ds.getSelectLastRetrieved())
    var count = ds.getRowCount();
    println('count: ' + count)
    for (var i = 0; i < count; i++) {
        var item = JSON.parse(ds.getJSONRow(i));
        var dayArr = item.flow_instance_starttime.replace(/ /g, '-').replace(/:/g, '-').replace(/\./g, '-').split('-');
        // println( 'item.flow_instance_starttime  ' + JSON.stringify(item.flow_instance_starttime) ) 

        var confirmTime = createDate2(dayArr[0], dayArr[1], dayArr[2], dayArr[3], dayArr[4], dayArr[5]);
        var nowTime = currentDate();
        
        // 开始时间和结束时间相差多少天
        var day_count = daysBetween((nowTime), (confirmTime));
        // println("day_count: " + day_count)
        // println("item.qsqx: " + (item.qsqx/1 > '0' ? item.qsqx : item.qsqx2 )  )

        var confirm_day = (item.qsqx/1 > '0' ? ( (item.qsqx/1)+ (item.qsqx2 /1) ) : item.qsqx2 );


        if ( confirm_day <= day_count) {
            // println('  item.id ' + item.id)
            runSQL(dsn, `update pz_yjd_pzh set state=1 where id='${item.id}'`);
        }
    }


    var list = getListFromSQL2('', `select * from  pz_yjd a left join (select gguid, sum(case when ifnull(state,0)=0 then 1 else 0 end) as count from  pz_yjd_pzh  group by gguid) b on a.gguid=b.gguid where  a.flow_instance_state = 0 and b.count=0 `)

    // return list

    list.forEach(item => {
        println("延长确认，自动确认");
        var gguid = item.gguid, workid = item.open_bill_with_url.split('&workspaceid=')[0].split('&id=')[1];

        // println("workid  " + workid)
        // println("gguid  " + gguid)

        //递交结束
        var loopCount = 0;
        while (loopCount < 10) {
            loopCount++;
            // println(gguid);
            var t = taskInfoForWork(workid, gguid);
            println(JSON.stringify(t));
            if (t.taskList.length == 0) break;
            //将任务递交下去，如果任务需要签收，则先签收     
            for (var i = 0; i < t.taskList.length; i++) {
                var task = t.taskList[i];

                //可以递交，那么递交下去
                if (task.canSubmit) println(submitWork(workid, gguid, task.taskId));
                //需要先签收 ，那么以第一个候选人签收它
                if (task.canClaim) claimWork(workid, gguid, task.taskId, task.candidateUserList[0].userId);

            }


        }

    })
    // return list

    return 'over';

}


id: 66c5812d993294a6b348618d
scriptid: 66bb02cf993294a6b3485671
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 13:55:14.000
script:
/**
 * 脚本中心的脚本，以 main函数为执行入口，相关的参数放在arg中，它是一个JSON字符串化后的数据
 *
 */
function main(arg) {
    println(arg);
    var param = JSON.parse(arg);

    var dsn = "";
    var sql = `select a.*, b.flow_instance_starttime, b.qsqx as qsqx2 from pz_yjd_pzh a left join pz_yjd b on a.gguid=b.gguid where b.flow_instance_state = 0 and b.flow_instance_starttime is not null and a.state=0`;
    // var list = getListFromSQL2( dsn, sql);


    // var list = getListFromSQL2( '', `select * from  pz_yjd `) 
    // var list = getListFromSQL2( '', `select gguid, sum(case when ifnull(state,0)=0 then 1 else 0 end) as count from  pz_yjd_pzh  group by gguid`) 

    // return list

    var ds = newDataStore(dsn, sql);
    ds.retrieve();
    println("select : " + ds.getSelectLastRetrieved())
    var count = ds.getRowCount();
    println('count: ' + count)
    for (var i = 0; i < count; i++) {
        var item = JSON.parse(ds.getJSONRow(i));
        var dayArr = item.flow_instance_starttime.replace(/ /g, '-').replace(/:/g, '-').replace(/\./g, '-').split('-');
        // println( 'item.flow_instance_starttime  ' + JSON.stringify(item.flow_instance_starttime) ) 

        var confirmTime = createDate2(dayArr[0], dayArr[1], dayArr[2], dayArr[3], dayArr[4], dayArr[5]);
        var nowTime = currentDate();
        
        // 开始时间和结束时间相差多少天
        var day_count = daysBetween((nowTime), (confirmTime));
        // println("day_count: " + day_count)
        // println("item.qsqx: " + (item.qsqx/1 > '0' ? item.qsqx : item.qsqx2 )  )

        var confirm_day = (item.qsqx/1 > '0' ? ( (item.qsqx/1)+ (item.qsqx2 /1) ) : item.qsqx2 );
    println( "confirm_day  " + confirm_day ) 

        if ( confirm_day <= day_count) {
            // println('  item.id ' + item.id)
            runSQL(dsn, `update pz_yjd_pzh set state=1 where id='${item.id}'`);
        }
    }


    var list = getListFromSQL2('', `select * from  pz_yjd a left join (select gguid, sum(case when ifnull(state,0)=0 then 1 else 0 end) as count from  pz_yjd_pzh  group by gguid) b on a.gguid=b.gguid where  a.flow_instance_state = 0 and b.count=0 `)

    // return list

    list.forEach(item => {
        println("延长确认，自动确认");
        var gguid = item.gguid, workid = item.open_bill_with_url.split('&workspaceid=')[0].split('&id=')[1];

        // println("workid  " + workid)
        // println("gguid  " + gguid)

        //递交结束
        var loopCount = 0;
        while (loopCount < 10) {
            loopCount++;
            // println(gguid);
            var t = taskInfoForWork(workid, gguid);
            println(JSON.stringify(t));
            if (t.taskList.length == 0) break;
            //将任务递交下去，如果任务需要签收，则先签收     
            for (var i = 0; i < t.taskList.length; i++) {
                var task = t.taskList[i];

                //可以递交，那么递交下去
                if (task.canSubmit) println(submitWork(workid, gguid, task.taskId));
                //需要先签收 ，那么以第一个候选人签收它
                if (task.canClaim) claimWork(workid, gguid, task.taskId, task.candidateUserList[0].userId);

            }


        }

    })
    // return list

    return 'over';

}


id: 66c5812d993294a6b3486194
scriptid: 66bb02cf993294a6b3485671
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 14:00:00.000
script:
/**
 * 脚本中心的脚本，以 main函数为执行入口，相关的参数放在arg中，它是一个JSON字符串化后的数据
 *
 */
function main(arg) {
    println(arg);
    var param = JSON.parse(arg);

    var dsn = "";
    var sql = `select a.*, b.flow_instance_starttime, b.qsqx as qsqx2 from pz_yjd_pzh a left join pz_yjd b on a.gguid=b.gguid where b.flow_instance_state = 0 and b.flow_instance_starttime is not null and ifnull(a.state, 0)=0`;
    // var list = getListFromSQL2( dsn, sql);


    // var list = getListFromSQL2( '', `select * from  pz_yjd `) 
    // var list = getListFromSQL2( '', `select gguid, sum(case when ifnull(state,0)=0 then 1 else 0 end) as count from  pz_yjd_pzh  group by gguid`) 

    // return list

    var ds = newDataStore(dsn, sql);
    ds.retrieve();
    println("select : " + ds.getSelectLastRetrieved())
    var count = ds.getRowCount();
    println('count: ' + count)
    for (var i = 0; i < count; i++) {
        var item = JSON.parse(ds.getJSONRow(i));
        var dayArr = item.flow_instance_starttime.replace(/ /g, '-').replace(/:/g, '-').replace(/\./g, '-').split('-');
        // println( 'item.flow_instance_starttime  ' + JSON.stringify(item.flow_instance_starttime) ) 

        var confirmTime = createDate2(dayArr[0], dayArr[1], dayArr[2], dayArr[3], dayArr[4], dayArr[5]);
        var nowTime = currentDate();
        
        // 开始时间和结束时间相差多少天
        var day_count = daysBetween((nowTime), (confirmTime));
        // println("day_count: " + day_count)
        // println("item.qsqx: " + (item.qsqx/1 > '0' ? item.qsqx : item.qsqx2 )  )

        var confirm_day = (item.qsqx/1 > '0' ? ( (item.qsqx/1)+ (item.qsqx2 /1) ) : item.qsqx2 );
    println( "confirm_day  " + confirm_day ) 

        if ( confirm_day <= day_count) {
            // println('  item.id ' + item.id)
            runSQL(dsn, `update pz_yjd_pzh set state=1 where id='${item.id}'`);
        }
    }


    var list = getListFromSQL2('', `select * from  pz_yjd a left join (select gguid, sum(case when ifnull(state,0)=0 then 1 else 0 end) as count from  pz_yjd_pzh  group by gguid) b on a.gguid=b.gguid where  a.flow_instance_state = 0 and b.count=0 `)

    // return list

    list.forEach(item => {
        println("延长确认，自动确认");
        var gguid = item.gguid, workid = item.open_bill_with_url.split('&workspaceid=')[0].split('&id=')[1];

        // println("workid  " + workid)
        // println("gguid  " + gguid)

        //递交结束
        var loopCount = 0;
        while (loopCount < 10) {
            loopCount++;
            // println(gguid);
            var t = taskInfoForWork(workid, gguid);
            println(JSON.stringify(t));
            if (t.taskList.length == 0) break;
            //将任务递交下去，如果任务需要签收，则先签收     
            for (var i = 0; i < t.taskList.length; i++) {
                var task = t.taskList[i];

                //可以递交，那么递交下去
                if (task.canSubmit) println(submitWork(workid, gguid, task.taskId));
                //需要先签收 ，那么以第一个候选人签收它
                if (task.canClaim) claimWork(workid, gguid, task.taskId, task.candidateUserList[0].userId);

            }


        }

    })
    // return list

    return 'over';

}


id: 66c5812d993294a6b34861a8
scriptid: 66bb02cf993294a6b3485671
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 14:06:15.000
script:
/**
 * 脚本中心的脚本，以 main函数为执行入口，相关的参数放在arg中，它是一个JSON字符串化后的数据
 *
 */
function main(arg) {
    println(arg);
    var param = JSON.parse(arg);

    var dsn = "";
    var sql = `select a.*, b.flow_instance_starttime, b.qsqx as qsqx2 from pz_yjd_pzh a left join pz_yjd b on a.gguid=b.gguid where b.flow_instance_state = 0 and b.flow_instance_starttime is not null and ifnull(a.state, 0)=0`;
    // var list = getListFromSQL2( dsn, sql);


    // var list = getListFromSQL2( '', `select * from  pz_yjd `) 
    // var list = getListFromSQL2( '', `select gguid, sum(case when ifnull(state,0)=0 then 1 else 0 end) as count from  pz_yjd_pzh  group by gguid`) 

    // return list

    var ds = newDataStore(dsn, sql);
    ds.retrieve();
    println("select : " + ds.getSelectLastRetrieved())
    var count = ds.getRowCount();
    println('count: ' + count)
    for (var i = 0; i < count; i++) {
        var item = JSON.parse(ds.getJSONRow(i));
        var dayArr = item.flow_instance_starttime.replace(/ /g, '-').replace(/:/g, '-').replace(/\./g, '-').split('-');
        // println( 'item.flow_instance_starttime  ' + JSON.stringify(item.flow_instance_starttime) ) 

        var confirmTime = createDate2(dayArr[0], dayArr[1], dayArr[2], dayArr[3], dayArr[4], dayArr[5]);
        var nowTime = currentDate();
        println( 'confirmTime ' + confirmTime ) 
        println( 'nowTime ' + nowTime ) 
        
        // 开始时间和结束时间相差多少天
        var day_count = daysBetween((nowTime), (confirmTime));
        // println("day_count: " + day_count)
        // println("item.qsqx: " + (item.qsqx/1 > '0' ? item.qsqx : item.qsqx2 )  )

        var confirm_day = (item.qsqx/1 > '0' ? ( (item.qsqx/1)+ (item.qsqx2 /1) ) : item.qsqx2 );
    println( "confirm_day  " + confirm_day ) 

        if ( confirm_day <= day_count) {
            // println('  item.id ' + item.id)
            runSQL(dsn, `update pz_yjd_pzh set state=1 where id='${item.id}'`);
        }
    }


    var list = getListFromSQL2('', `select * from  pz_yjd a left join (select gguid, sum(case when ifnull(state,0)=0 then 1 else 0 end) as count from  pz_yjd_pzh  group by gguid) b on a.gguid=b.gguid where  a.flow_instance_state = 0 and b.count=0 `)

    // return list

    list.forEach(item => {
        println("延长确认，自动确认");
        var gguid = item.gguid, workid = item.open_bill_with_url.split('&workspaceid=')[0].split('&id=')[1];

        // println("workid  " + workid)
        // println("gguid  " + gguid)

        //递交结束
        var loopCount = 0;
        while (loopCount < 10) {
            loopCount++;
            // println(gguid);
            var t = taskInfoForWork(workid, gguid);
            println(JSON.stringify(t));
            if (t.taskList.length == 0) break;
            //将任务递交下去，如果任务需要签收，则先签收     
            for (var i = 0; i < t.taskList.length; i++) {
                var task = t.taskList[i];

                //可以递交，那么递交下去
                if (task.canSubmit) println(submitWork(workid, gguid, task.taskId));
                //需要先签收 ，那么以第一个候选人签收它
                if (task.canClaim) claimWork(workid, gguid, task.taskId, task.candidateUserList[0].userId);

            }


        }

    })
    // return list

    return 'over';

}


id: 66c5812d993294a6b34861d6
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:30:59.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked ( gguid ) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`  );
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2( "", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by `  ) 

        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }

    back_build_sum();
    return { success: true };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5812d993294a6b34861dc
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:31:12.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked ( gguid ) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`  );
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2( "", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`  ) 

        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }

    back_build_sum();
    return { success: true };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5812d993294a6b34861e2
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:31:16.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked ( gguid ) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`  );
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2( "", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);

        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }

    back_build_sum();
    return { success: true };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a616993294a6b34861eb
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:32:23.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked ( gguid ) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`  );
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2( "", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' and state='1' order by lastmodifieddate desc`);
        if(newItem.billtype)

        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }

    back_build_sum();
    return { success: true };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a616993294a6b34861f1
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:32:36.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked ( gguid ) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`  );
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2( "", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if(newItem.billtype)

        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }

    back_build_sum();
    return { success: true };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a616993294a6b348620f
scriptid: 650666139932de686bb62112
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:35:03.000
script:
/**
 * 脚本中心的脚本，以 main函数为执行入口，相关的参数放在arg中，它是一个JSON字符串化后的数据
 *
 */
function main(arg)
{

// runSQL('', `alter table pz_yjd_pzh alter column lastmodifieddate set default =DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s')`  ) 
    // return getListFromSQL2( '',  ` SELECT 
    // max(b.corpid) as corpid,  max(a.ny) as ny, max(a.pzh) as pzh,   CONCAT(a.ny, a.pzh) AS ny_pzh,
    // case 
    //     when a.billtype ='交办' and b.flow_instance_state='0' then '交办中'
    //     when a.billtype ='交办' and b.flow_instance_state='1' and a.billtype ='交回' and b.flow_instance_state='0' then '交办没有交回' 
    // else end as jbzt
    // from pz_yjd_pzh a left join pz_yjd b on a.gguid=b.gguid  where a.billtype='交办'
    // group by ny_pzh` ) 
    
    // return getListFromSQL2( '',  `  select  a.lastmodifieddate, a.corpid_ny_pzh, a.ny,  a.pzh, a.corpid, a.flow_instance_state,a.flow_instance_status
    // from (
    //     select a1.pzh, a1.ny, b1.lastmodifieddate, b1.corpid, b1.flow_instance_state,b1.flow_instance_status,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
    //     from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  
    //     ) a 
    //     inner  join  (
    //         select max(lastmodifieddate) as lastmodifieddate, corpid_ny_pzh from (select b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
    //     from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) aa group by corpid_ny_pzh  
    //     ) b on a.corpid_ny_pzh=b.corpid_ny_pzh and a.lastmodifieddate=b.lastmodifieddate      ` ) 


runSQL('', `delete from pz_yjd`  ) 
runSQL('', `delete from pz_yjd_pzh`  ) 
runSQL('', `delete from pz_yjdex`  ) 


    
    return getListFromSQL2( '',  ` select jb.* 
    , case when jb.flow_instance_state='0' and jb.flow_instance_status is not null then '交办申请中' 
      when jb.flow_instance_state='1' and jh.flow_instance_state is null then '交办没有交回' 
      when jb.flow_instance_state='1' and jh.flow_instance_state='0' and jh.flow_instance_status is not null then '交回申请中' 
      when jb.flow_instance_state='1' and jh.flow_instance_state='1' then '交办且已交回' 
     else '' end as jb_state

    , case when jy.flow_instance_state='0' and jy.flow_instance_status is not null then '借阅申请中' 
      when jy.flow_instance_state='1' and gh.flow_instance_state is null then '借阅没有归还' 
      when jy.flow_instance_state='1' and gh.flow_instance_state='0' and gh.flow_instance_status is not null then '归还申请中' 
      when jy.flow_instance_state='1' and gh.flow_instance_state='1' then '借阅且已归还' 
     else '' end as jy_state
    
     from (
        select  a.lastmodifieddate, a.corpid_ny_pzh, a.ny,  a.pzh, a.corpid, a.flow_instance_state,a.flow_instance_status
    from (
        select a1.pzh, a1.ny, b1.lastmodifieddate, b1.corpid, b1.flow_instance_state,b1.flow_instance_status,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  
        ) a 
        inner  join  (
            select max(lastmodifieddate) as lastmodifieddate, corpid_ny_pzh from (select b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) aa group by corpid_ny_pzh  
        ) b on a.corpid_ny_pzh=b.corpid_ny_pzh and a.lastmodifieddate=b.lastmodifieddate 
    ) jb 
    left join (
        select  a.lastmodifieddate, a.corpid_ny_pzh, a.ny,  a.pzh, a.corpid, a.flow_instance_state,a.flow_instance_status
    from (
        select a1.pzh, a1.ny, b1.lastmodifieddate, b1.corpid, b1.flow_instance_state, b1.flow_instance_status, CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='交回'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  
        ) a 
        inner  join  (
            select max(lastmodifieddate) as lastmodifieddate, corpid_ny_pzh from (select b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='交回'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) aa group by corpid_ny_pzh  
        ) b on a.corpid_ny_pzh=b.corpid_ny_pzh and a.lastmodifieddate=b.lastmodifieddate 
    ) jh on jb.corpid_ny_pzh=jh.corpid_ny_pzh
    left join (
        select  a.lastmodifieddate, a.corpid_ny_pzh, a.ny,  a.pzh, a.corpid, a.flow_instance_state,a.flow_instance_status
    from (
        select a1.pzh, a1.ny, b1.lastmodifieddate, b1.corpid, b1.flow_instance_state, b1.flow_instance_status, CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='借阅'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  
        ) a 
        inner  join  (
            select max(lastmodifieddate) as lastmodifieddate, corpid_ny_pzh from (select b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='借阅'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) aa group by corpid_ny_pzh  
        ) b on a.corpid_ny_pzh=b.corpid_ny_pzh and a.lastmodifieddate=b.lastmodifieddate 
    ) jy on jb.corpid_ny_pzh=jy.corpid_ny_pzh
    left join (
        select  a.lastmodifieddate, a.corpid_ny_pzh, a.ny,  a.pzh, a.corpid, a.flow_instance_state,a.flow_instance_status
    from (
        select a1.pzh, a1.ny, b1.lastmodifieddate, b1.corpid, b1.flow_instance_state, b1.flow_instance_status, CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='归还'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  
        ) a 
        inner  join  (
            select max(lastmodifieddate) as lastmodifieddate, corpid_ny_pzh from (select b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='归还'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) aa group by corpid_ny_pzh  
        ) b on a.corpid_ny_pzh=b.corpid_ny_pzh and a.lastmodifieddate=b.lastmodifieddate 
    ) gh  on jb.corpid_ny_pzh=gh.corpid_ny_pzh
    
    order by jb.lastmodifieddate desc` ) 
    
    // return getListFromSQL2( '',  ` select  max(a.lastmodifieddate) as lastmodifieddate, a.corpid_ny_pzh, max(a.ny) as ny,  max(a.pzh) as pzh, max(a.corpid) as corpid
    // from (select a1.pzh, a1.ny, b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
    //     from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) a 
    //     left join  (
    //         select max(lastmodifieddate) as lastmodifieddate, corpid_ny_pzh from (select b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
    //     from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) aa group by corpid_ny_pzh  
    //     ) b on a.corpid_ny_pzh=b.corpid_ny_pzh and a.lastmodifieddate=b.lastmodifieddate ` ) 
    
    
    // return getListFromSQL2( '',  ` select a1.pzh, a1.ny, b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
    //     from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ` ) 
    
    return getListFromSQL2( '',  ` select max(lastmodifieddate) as lastmodifieddate, corpid_ny_pzh from (select b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) a group by corpid_ny_pzh   ` ) 


    
    // return getListFromSQL2( '',  ` SELECT 
    // max(b.corpid) as corpid,  max(a.ny) as ny, max(a.pzh) as pzh,   CONCAT( CONCAT(b.corpid, a.ny), a.pzh) AS corpid_ny_pzh, max(b.flow_instance_state) as flow_instance_state
    // from (select * from pz_yjd_pzh where billtype='交办') a 
    // left join (
    //     select a.* from pz_yjd a 
    //     left join ( select max(b1.lastmodifieddate) as lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
    //     from (select * from pz_yjd_pzh where billtype='交办')  a1 left join pz_yjd b1 on a1.gguid=b1.gguid   group by corpid_ny_pzh ) b 
    //     on b.lastmodifieddate=a.lastmodifieddate and a.corpid =b.corpid
    // ) b on a.gguid=b.gguid   
    // group by corpid_ny_pzh` ) 
    
    

    return false
    
   var t='2023-02-23 ';
var s= t.match(  /20[0-9]{2}-[0-9]{2}-[0-9]{2}/g);
return s;   
    
    return OCR ( { src:'fileServer?bucketName=电子凭证&objectName=dzpz_imgpool/2023/09/17/650665939932db10f02cebaf.jpg&fileName=p4.jpg&serverIndex=2&action=view' ,
             left:'50%' , top:'20%' ,height:'20%' ,width:'50%'} );


}


id: 66c5a616993294a6b3486217
scriptid: 650666139932de686bb62112
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:35:47.000
script:
/**
 * 脚本中心的脚本，以 main函数为执行入口，相关的参数放在arg中，它是一个JSON字符串化后的数据
 *
 */
function main(arg)
{

// runSQL('', `alter table pz_yjd_pzh alter column lastmodifieddate set default =DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s')`  ) 
    // return getListFromSQL2( '',  ` SELECT 
    // max(b.corpid) as corpid,  max(a.ny) as ny, max(a.pzh) as pzh,   CONCAT(a.ny, a.pzh) AS ny_pzh,
    // case 
    //     when a.billtype ='交办' and b.flow_instance_state='0' then '交办中'
    //     when a.billtype ='交办' and b.flow_instance_state='1' and a.billtype ='交回' and b.flow_instance_state='0' then '交办没有交回' 
    // else end as jbzt
    // from pz_yjd_pzh a left join pz_yjd b on a.gguid=b.gguid  where a.billtype='交办'
    // group by ny_pzh` ) 
    
    // return getListFromSQL2( '',  `  select  a.lastmodifieddate, a.corpid_ny_pzh, a.ny,  a.pzh, a.corpid, a.flow_instance_state,a.flow_instance_status
    // from (
    //     select a1.pzh, a1.ny, b1.lastmodifieddate, b1.corpid, b1.flow_instance_state,b1.flow_instance_status,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
    //     from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  
    //     ) a 
    //     inner  join  (
    //         select max(lastmodifieddate) as lastmodifieddate, corpid_ny_pzh from (select b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
    //     from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) aa group by corpid_ny_pzh  
    //     ) b on a.corpid_ny_pzh=b.corpid_ny_pzh and a.lastmodifieddate=b.lastmodifieddate      ` ) 


// runSQL('', `delete from pz_yjd`  ) 
// runSQL('', `delete from pz_yjd_pzh`  ) 
// runSQL('', `delete from pz_yjdex`  ) 


    
    return getListFromSQL2( '',  ` select jb.* 
    , case when jb.flow_instance_state='0' and jb.flow_instance_status is not null then '交办申请中' 
      when jb.flow_instance_state='1' and jh.flow_instance_state is null then '交办没有交回' 
      when jb.flow_instance_state='1' and jh.flow_instance_state='0' and jh.flow_instance_status is not null then '交回申请中' 
      when jb.flow_instance_state='1' and jh.flow_instance_state='1' then '交办且已交回' 
     else '' end as jb_state

    , case when jy.flow_instance_state='0' and jy.flow_instance_status is not null then '借阅申请中' 
      when jy.flow_instance_state='1' and gh.flow_instance_state is null then '借阅没有归还' 
      when jy.flow_instance_state='1' and gh.flow_instance_state='0' and gh.flow_instance_status is not null then '归还申请中' 
      when jy.flow_instance_state='1' and gh.flow_instance_state='1' then '借阅且已归还' 
     else '' end as jy_state
    
     from (
        select  a.lastmodifieddate, a.corpid_ny_pzh, a.ny,  a.pzh, a.corpid, a.flow_instance_state,a.flow_instance_status
    from (
        select a1.pzh, a1.ny, b1.lastmodifieddate, b1.corpid, b1.flow_instance_state,b1.flow_instance_status,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  
        ) a 
        inner  join  (
            select max(lastmodifieddate) as lastmodifieddate, corpid_ny_pzh from (select b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) aa group by corpid_ny_pzh  
        ) b on a.corpid_ny_pzh=b.corpid_ny_pzh and a.lastmodifieddate=b.lastmodifieddate 
    ) jb 
    left join (
        select  a.lastmodifieddate, a.corpid_ny_pzh, a.ny,  a.pzh, a.corpid, a.flow_instance_state,a.flow_instance_status
    from (
        select a1.pzh, a1.ny, b1.lastmodifieddate, b1.corpid, b1.flow_instance_state, b1.flow_instance_status, CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='交回'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  
        ) a 
        inner  join  (
            select max(lastmodifieddate) as lastmodifieddate, corpid_ny_pzh from (select b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='交回'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) aa group by corpid_ny_pzh  
        ) b on a.corpid_ny_pzh=b.corpid_ny_pzh and a.lastmodifieddate=b.lastmodifieddate 
    ) jh on jb.corpid_ny_pzh=jh.corpid_ny_pzh
    left join (
        select  a.lastmodifieddate, a.corpid_ny_pzh, a.ny,  a.pzh, a.corpid, a.flow_instance_state,a.flow_instance_status
    from (
        select a1.pzh, a1.ny, b1.lastmodifieddate, b1.corpid, b1.flow_instance_state, b1.flow_instance_status, CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='借阅'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  
        ) a 
        inner  join  (
            select max(lastmodifieddate) as lastmodifieddate, corpid_ny_pzh from (select b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='借阅'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) aa group by corpid_ny_pzh  
        ) b on a.corpid_ny_pzh=b.corpid_ny_pzh and a.lastmodifieddate=b.lastmodifieddate 
    ) jy on jb.corpid_ny_pzh=jy.corpid_ny_pzh
    left join (
        select  a.lastmodifieddate, a.corpid_ny_pzh, a.ny,  a.pzh, a.corpid, a.flow_instance_state,a.flow_instance_status
    from (
        select a1.pzh, a1.ny, b1.lastmodifieddate, b1.corpid, b1.flow_instance_state, b1.flow_instance_status, CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='归还'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  
        ) a 
        inner  join  (
            select max(lastmodifieddate) as lastmodifieddate, corpid_ny_pzh from (select b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='归还'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) aa group by corpid_ny_pzh  
        ) b on a.corpid_ny_pzh=b.corpid_ny_pzh and a.lastmodifieddate=b.lastmodifieddate 
    ) gh  on jb.corpid_ny_pzh=gh.corpid_ny_pzh
    
    order by jb.lastmodifieddate desc` ) 
    
    // return getListFromSQL2( '',  ` select  max(a.lastmodifieddate) as lastmodifieddate, a.corpid_ny_pzh, max(a.ny) as ny,  max(a.pzh) as pzh, max(a.corpid) as corpid
    // from (select a1.pzh, a1.ny, b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
    //     from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) a 
    //     left join  (
    //         select max(lastmodifieddate) as lastmodifieddate, corpid_ny_pzh from (select b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
    //     from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) aa group by corpid_ny_pzh  
    //     ) b on a.corpid_ny_pzh=b.corpid_ny_pzh and a.lastmodifieddate=b.lastmodifieddate ` ) 
    
    
    // return getListFromSQL2( '',  ` select a1.pzh, a1.ny, b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
    //     from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ` ) 
    
    return getListFromSQL2( '',  ` select max(lastmodifieddate) as lastmodifieddate, corpid_ny_pzh from (select b1.lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
        from ( select * from pz_yjd_pzh where billtype='交办'  )  a1 left join pz_yjd b1 on a1.gguid=b1.gguid where  b1.corpid is not null  ) a group by corpid_ny_pzh   ` ) 


    
    // return getListFromSQL2( '',  ` SELECT 
    // max(b.corpid) as corpid,  max(a.ny) as ny, max(a.pzh) as pzh,   CONCAT( CONCAT(b.corpid, a.ny), a.pzh) AS corpid_ny_pzh, max(b.flow_instance_state) as flow_instance_state
    // from (select * from pz_yjd_pzh where billtype='交办') a 
    // left join (
    //     select a.* from pz_yjd a 
    //     left join ( select max(b1.lastmodifieddate) as lastmodifieddate, b1.corpid,  CONCAT( CONCAT(b1.corpid, a1.ny), a1.pzh) AS corpid_ny_pzh 
    //     from (select * from pz_yjd_pzh where billtype='交办')  a1 left join pz_yjd b1 on a1.gguid=b1.gguid   group by corpid_ny_pzh ) b 
    //     on b.lastmodifieddate=a.lastmodifieddate and a.corpid =b.corpid
    // ) b on a.gguid=b.gguid   
    // group by corpid_ny_pzh` ) 
    
    

    return false
    
   var t='2023-02-23 ';
var s= t.match(  /20[0-9]{2}-[0-9]{2}-[0-9]{2}/g);
return s;   
    
    return OCR ( { src:'fileServer?bucketName=电子凭证&objectName=dzpz_imgpool/2023/09/17/650665939932db10f02cebaf.jpg&fileName=p4.jpg&serverIndex=2&action=view' ,
             left:'50%' , top:'20%' ,height:'20%' ,width:'50%'} );


}


id: 66c5a616993294a6b348621d
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:40:42.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked ( gguid ) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`  );
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2( "", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if( ( (newItem.state != '1') || ( (newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1') ) && pzh_list.length ){
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state=='1'?`没有${newItem.billtype=='交办'?'交回':'归还'}`:'申请中'}`
            })
        }else{

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
        }

    }

    back_build_sum();
    return { success: true };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a616993294a6b3486223
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:40:45.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked ( gguid ) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`  );
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2( "", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if( ( (newItem.state != '1') || ( (newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1') ) && pzh_list.length ){
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state=='1'?`没有${newItem.billtype=='交办'?'交回':'归还'}`:'申请中'}`；
            })
        }else{

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
        }

    }

    back_build_sum();
    return { success: true };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a616993294a6b3486229
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:40:47.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked ( gguid ) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`  );
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2( "", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if( ( (newItem.state != '1') || ( (newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1') ) && pzh_list.length ){
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state=='1'?`没有${newItem.billtype=='交办'?'交回':'归还'}`:'申请中'}`;
            })
        }else{

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
        }

    }

    back_build_sum();
    return { success: true };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a616993294a6b348622f
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:40:51.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked ( gguid ) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`  );
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2( "", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if( ( (newItem.state != '1') || ( (newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1') ) && pzh_list.length ){
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state=='1'?`没有${newItem.billtype=='交办'?'交回':'归还'}`:'申请中'}`;
            })
        }else{

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a616993294a6b3486235
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:40:54.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked ( gguid ) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`  );
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2( "", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if( ( (newItem.state != '1') || ( (newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1') ) && pzh_list.length ){
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state=='1'?`没有${newItem.billtype=='交办'?'交回':'归还'}`:'申请中'}`;
            })
        }else{

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list:pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a616993294a6b348623b
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:40:59.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked ( gguid ) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`  );
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2( "", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if( ( (newItem.state != '1') || ( (newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1') ) && pzh_list.length ){
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state=='1'?`没有${newItem.billtype=='交办'?'交回':'归还'}`:'申请中'}`
            })
        }else{

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list:pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a616993294a6b3486241
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:41:02.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked ( gguid ) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`  );
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2( "", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if( ( (newItem.state != '1') || ( (newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1') ) && pzh_list.length ){
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state=='1'?`没有${newItem.billtype=='交办'?'交回':'归还'}`:'申请中'}`
            })
        }else{

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a616993294a6b3486247
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:41:11.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked ( gguid ) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`  );
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2( "", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if( ( (newItem.state != '1') || ( (newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1') ) && pzh_list.length ){
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state=='1'?`没有${newItem.billtype=='交办'?'交回':'归还'}`:'申请中'}`
            })
        }else{

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list:; };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a82a993294a6b3486253
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:41:22.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked ( gguid ) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`  );
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2( "", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if( ( (newItem.state != '1') || ( (newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1') ) && pzh_list.length ){
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state=='1'?`没有${newItem.billtype=='交办'?'交回':'归还'}`:'申请中'}`
            })
        }else{
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list:pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate( currentDate()  , "yyyy.MM.dd HH:mm:ss") }') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a82a993294a6b3486259
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:41:57.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getgetOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getgetOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a82a993294a6b348625f
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:43:35.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status&&data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status&&data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd (dsname,rowCount, withLastSelect) {
    if(dsname=='pz_yjd'){

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function (ret) {
            toastr.hide();
            console.log(ret)
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c5a82a993294a6b348626b
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:44:32.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a82a993294a6b348626f
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:45:16.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status&&data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status&&data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd (dsname,rowCount, withLastSelect) {
    if(dsname=='pz_yjd'){

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function () {
            toastr.hide();
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c5a82a993294a6b3486275
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:47:00.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${data.pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a82a993294a6b348627b
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:47:09.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a82a993294a6b348627f
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:48:04.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status&&data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status&&data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd (dsname,rowCount, withLastSelect) {
    if(dsname=='pz_yjd'){

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c5a82a993294a6b3486285
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:48:51.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        println(`select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc` ) 
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a82a993294a6b348628b
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:49:38.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        println(`select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc` ) 
        println(`newItem   ---  ${JSON.stringify(newItem)}` ) 
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a82a993294a6b3486291
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:50:53.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        println(`select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc` ) 
        println(`newItem   ---  ${JSON.stringify(newItem)}` ) 
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) ) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        }else if( pzh_list.length){

        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a82a993294a6b3486297
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:51:02.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        println(`select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`)
        println(`newItem   ---  ${JSON.stringify(newItem)}`)
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1'))) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else if (pzh_list.length) {

        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else if (pzh_list.length) {

        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5a82a993294a6b348629b
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:51:46.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status&&data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status&&data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd (dsname,rowCount, withLastSelect) {
    if(dsname=='pz_yjd'){

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            if(ret.pzh_list.length){

            }else{
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            }
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c5a82a993294a6b348629f
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:51:48.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status&&data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status&&data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd (dsname,rowCount, withLastSelect) {
    if(dsname=='pz_yjd'){

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            if(ret.pzh_list.length){
                
            }else{
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            }
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c5a82a993294a6b34862a3
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:54:31.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status&&data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status&&data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd (dsname,rowCount, withLastSelect) {
    if(dsname=='pz_yjd'){

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            if(ret.pzh_list.length){
                var str = [];
                

    inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
        
    }, false, {onContentReady:function(){   }}, 10);

            }else{
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            }
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue>'0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                onContentReady: function () {
                    $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                    window.qsqx_old = 0;
                }
            }, 10);
        }else{
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c5a82a993294a6b34862a9
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:56:09.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        println(`select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`)
        println(`newItem   ---  ${JSON.stringify(newItem)}`)
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1'))) {
            pzh_list.push({
                pzh: pzh,
                state: `${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`
            })
        } else if (pzh_list.length) {

        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push(`${pzh} ${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`);
        } else if (pzh_list.length) {

        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5abbb993294a6b34862af
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:56:28.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        println(`select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`)
        println(`newItem   ---  ${JSON.stringify(newItem)}`)
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1'))) {
            pzh_list.push(`${pzh} ${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`);
        } else if (pzh_list.length) {

        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push(`${pzh} ${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`);
        } else if (pzh_list.length) {

        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5abbb993294a6b34862b3
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:58:12.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status && data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status && data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd(dsname, rowCount, withLastSelect) {
    if (dsname == 'pz_yjd') {

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            if (ret.pzh_list.length) {

                inputBox("提示", "以下凭证号不符合条件", ret.pzh_list.join('\n'), "", function (val) {

                }, false, { onContentReady: function () { } }, 10);

            } else {
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            }
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue > '0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                    onContentReady: function () {
                        $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                        window.qsqx_old = 0;
                    }
                }, 10);
        } else {
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c5abbb993294a6b34862b7
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:58:13.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status && data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status && data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd(dsname, rowCount, withLastSelect) {
    if (dsname == 'pz_yjd') {

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 < data.pzh1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            if (ret.pzh_list.length) {

                inputBox("提示", "以下凭证号不符合条件", ret.pzh_list.join('\n'), "", function (val) {
                }, false, { onContentReady: function () { } }, 10);

            } else {
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            }
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue > '0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                    onContentReady: function () {
                        $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                        window.qsqx_old = 0;
                    }
                }, 10);
        } else {
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c5abbb993294a6b34862bb
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 16:59:23.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status && data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status && data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd(dsname, rowCount, withLastSelect) {
    if (dsname == 'pz_yjd') {

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2/1 < data.pzh1/1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            if (ret.pzh_list.length) {

                inputBox("提示", "以下凭证号不符合条件", ret.pzh_list.join('\n'), "", function (val) {
                }, false, { onContentReady: function () { } }, 10);

            } else {
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            }
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue > '0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                    onContentReady: function () {
                        $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                        window.qsqx_old = 0;
                    }
                }, 10);
        } else {
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c5abbb993294a6b34862c1
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 17:03:06.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        println(`select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`)
        println(`newItem   ---  ${JSON.stringify(newItem)}`)
        if (((newItem.state != '1') || (newItem.billtype != '交回' && newItem.state == '1'))) {
            pzh_list.push(`${pzh} ${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype != '借阅' ? '交回' : '归还'}` : '申请中'}`);
        } else if (pzh_list.length) {

        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || ((newItem.billtype == '借阅' || newItem.billtype == '交办') && newItem.state == '1')) && pzh_list.length) {
            pzh_list.push(`${pzh} ${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype == '交办' ? '交回' : '归还'}` : '申请中'}`);
        } else if (pzh_list.length) {

        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5abbb993294a6b34862c7
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 17:03:15.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        println(`select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`)
        println(`newItem   ---  ${JSON.stringify(newItem)}`)
        if (((newItem.state != '1') || (newItem.billtype != '交回' && newItem.state == '1'))) {
            pzh_list.push(`${pzh} ${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype != '借阅' ? '交回' : '归还'}` : '申请中'}`);
        } else if (pzh_list.length) {

        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || (newItem.billtype != '交回' && newItem.state == '1'))) {
            pzh_list.push(`${pzh} ${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype != '借阅' ? '交回' : '归还'}` : '申请中'}`);
        } else if (pzh_list.length) {

        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5abbb993294a6b34862cd
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 17:04:23.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        println(`select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`)
        println(`newItem   ---  ${JSON.stringify(newItem)}`)
        if ( newItem &&  (newItem.state != '1' || (newItem.billtype != '交回' && newItem.state == '1')) ) {
            pzh_list.push(`${pzh} ${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype != '借阅' ? '交回' : '归还'}` : '申请中'}`);
        } else if (pzh_list.length) {

        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        if (((newItem.state != '1') || (newItem.billtype != '交回' && newItem.state == '1'))) {
            pzh_list.push(`${pzh} ${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype != '借阅' ? '交回' : '归还'}` : '申请中'}`);
        } else if (pzh_list.length) {

        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5abbb993294a6b34862d3
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 17:04:37.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        println(`newItem   ---  ${JSON.stringify(newItem)}`)
        if ( newItem &&  (newItem.state != '1' || (newItem.billtype != '交回' && newItem.state == '1')) ) {
            pzh_list.push(`${pzh} ${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype != '借阅' ? '交回' : '归还'}` : '申请中'}`);
        } else if (pzh_list.length) {

        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        if ( newItem &&  (newItem.state != '1' || (newItem.billtype != '交回' && newItem.state == '1')) ) {
            pzh_list.push(`${pzh} ${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype != '借阅' ? '交回' : '归还'}` : '申请中'}`);
        } else if (pzh_list.length) {

        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5abbb993294a6b34862d9
scriptid: 662a47879932b5035f32efab
tablename: app_script
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 17:05:37.000
script:
function verifyGZL(gguid) {
    var count = getValueFromSQL2("", `select count(0) from pz_yjd_pzh where gguid='${gguid}' and state<>'1' and qsqx is not null and qsqx>0`);
    println('verifyGZL count  ' + count)

    return count > 0 ? '有凭证式延迟确认，不能提交结束工作流！' : ''
}


/**
 * 在已结束的流程被撤销结束后，触发本事件
 * 无需返回值
 * @param gguid 单据的gguid
 *
 */
//@comment (group=工作流事件,caption=x结束的流程被撤销结束后)
function afterFinishedWorkRollbacked(gguid) {
    runSQL("", ` update pz_yjd_pzh set state=0 where gguid='${gguid}' and  state=1`);
}



/**
 * 工作结束
 * @param flow 流程定义
 * @param node 开始节点
 * @param gguid 单据的gguid

 */
//@comment (group=工作流事件,caption=工作结束)
function onWorkFlowEnd(flow, node, gguid) {
    // println( flow.id) ; //流程定义的ID
    println(gguid + "工作结束了");

    runSQL("", ` update pz_yjd_pzh set state=1 where gguid='${gguid}' and  ifnull(state, 0)=0`);

    runSQL("", `UPDATE pzbmx a
            INNER JOIN  pz_yjdex b  inner join pz_yjd c on (b.gguid=c.gguid) 
             ON a.pzh  >= b.pzh1 and a.pzh<= b.pzh2 and b.gguid=${gguid}
            SET a.pos  = c.towhere , a.memo = c.summary , a.state_jb=0, a.state_jy=0   ` , true);
}


function back_add_pzh(data) {
    var count = (data.pzh2 / 1) - (data.pzh1 / 1);

    var pzh_list = [];
    var gguid = currentGUID();
    for (var i = 0; i <= count; i++) {
        var newid = newGuid();
        var pzh = (data.pzh1 / 1) + i;

        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        println(`newItem   ---  ${JSON.stringify(newItem)}`)
        if ( newItem.pzh/1 &&  (newItem.state != '1' || (newItem.billtype != '交回' && newItem.state == '1')) ) {
            pzh_list.push(`${pzh} ${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype != '借阅' ? '交回' : '归还'}` : '申请中'}`);
        } else if (pzh_list.length) {

        } else {
            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate) values ( ${newid} , ${gguid} , '${data.ny}', '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }

    }

    back_build_sum();
    return { success: true, pzh_list: pzh_list };

}

function back_import_pzh_data(data) {

    var t = data.pzh.split(/[\s]/);
    var pzh_list = [];
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        var newItem = getOneRowFromSQL2("", `select * from pz_yjd_pzh where ny='${data.ny}' and pzh='${pzh}' order by lastmodifieddate desc`);
        if ( newItem.pzh/1 &&  (newItem.state != '1' || (newItem.billtype != '交回' && newItem.state == '1')) ) {
            pzh_list.push(`${pzh} ${newItem.billtype}${newItem.state == '1' ? `没有${newItem.billtype != '借阅' ? '交回' : '归还'}` : '申请中'}`);
        } else if (pzh_list.length) {

        } else {

            runSQL("", `insert into  pz_yjd_pzh (id, gguid, ny, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${data.ny}' , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
        }
    }
    back_build_sum();
    return { success: true, pzh_list: pzh_list };
}

function back_import_pzh(s) {
    var t = s.split(/[\s]/);
    for (var i = 0; i < t.length; i++) {
        var pzh = t[i].trim();
        if (pzh == '') continue;
        var newid = newGuid();
        var gguid = currentGUID();
        runSQL("", `insert into  pz_yjd_pzh (id, gguid, pzh, billtype, lastmodifieddate ) values ( ${newid} , ${gguid} , '${pzh}', '交办', '${formatDate(currentDate(), "yyyy.MM.dd HH:mm:ss")}') `, true);
    }
    back_build_sum();
}

//  根据凭证号清单生成凭证号汇总信息 
function back_build_sum() {
    var gguid = currentGUID();
    $back_build_sum(gguid);
}


function $back_build_sum(gguid) {


    var sql = `
            
        SELECT
            MAX( ny ) as ny , MIN( pzh ) as pzh1 ,   MAX( pzh )  AS  pzh2,
        COUNT(*) AS c 
        FROM
        (
            SELECT
            ny,
            pzh,
            CONCAT(ny, pzh) as ny_pzh,
            @group_id := IF( pzh  = @prev_number + 1, @group_id, @group_id + 1) AS group_id,
            @prev_number := pzh 
            FROM
            pz_yjd_pzh,
            (SELECT @group_id := 0, @prev_number := NULL) AS vars 
                    where  gguid= ${gguid}
            ORDER BY
            ny_pzh 
        ) AS subquery
        GROUP BY
        group_id;
            
    ` ;

    println(sql);
    var tds = newDataStore("", sql);
    var n = tds.retrieve();

    runSQL("", `delete from  pz_yjdex where gguid= ${gguid} `, true);

    var pz_yjdex_ds = newDataStore("", "select * from pz_yjdex");
    pz_yjdex_ds.setUpdateProperty('pz_yjdex', 'id', '*', 'id');
    for (var i = 0; i < n; i++) {
        var row = pz_yjdex_ds.insertRow(9999);
        pz_yjdex_ds.setValue(row, {
            id: newGuid(), gguid: gguid, inner_xh: row + 1,
            billtype: '交办',
            ny: tds.getString(i, 'ny'),
            pzh1: tds.getString(i, "pzh1"),
            pzh2: tds.getString(i, "pzh2"),
            pzs: tds.getInt(i, "c")


        });
    }

    //
    pz_yjdex_ds.executeAfterUpdate(`update  pz_yjd  set pzs= (select count(*) from  pz_yjd_pzh where gguid= ${gguid} ) where   id= ${gguid}  `);
    pz_yjdex_ds.update(true);


}


/**
 * 单据点击保存，前端所有的操作被转译成3种操作:insert , delete , update ,
 *  并且可以在  beforeBillSaveOnServer 中返回一些SQL语句，可以在afterBillSaveOnServer中返回一些SQL语句
 *
 * 系统分别在需要的数据库连接中执行这些语句 ， 当所有语句执行成功后，事件被提交。一些细节如下 ：
 *
 * 1 如果单据中的表，属于不同的数据库连接，那么所有的SQL语句会以连接为单位，对语句进行批量执行
 * 2 这些语句的执行顺序是：对同一个数据库连接中的语句， 先执行 beforeBillSaveOnServer 中返回的SQL语句，再执行单据操作转译得到的SQL语句，
 *   再执行 afterBillSaveOnServer 中返回的SQL语句， 当多个（或一个）数据库连接执行完这些语句，且没有错误时，所有的数据库连接做提交，否则全部回滚
 * 3 不要在 beforeBillSaveOnServer 或 afterBillSaveOnServer 中直接执行数据库操作，而是返回SQL语句，让系统批量执行它们，并控制好事务
 * 4 当执行成功后，系统再执行事件函数  afterBillSaveSuccessfulOnServer 。 此事件中可以执行任意操作，它与单据保存事件结束后才被触发执行，
 *   与单据保存并不在一个事务中。
 *
 *
 * @param gguid  要删除记录的gguid ，等同于主表的id
 * @param session  一个JSON对象，用于在整个单据保存过程中的各个事件之间传递数据。本事件做为单据保存的最后一个事件，session对象在使用后将被丢弃
 *
 *
 * @return 本函数不需要返回值
 *
 */
//@comment (group=单据保存,caption=单据保存成功后)
function afterBillSaveSuccessfulOnServer(gguid, session) {

    $back_build_sum(gguid);
}


id: 66c5abbb993294a6b34862de
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 17:08:52.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status && data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status && data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd(dsname, rowCount, withLastSelect) {
    if (dsname == 'pz_yjd') {

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2/1 < data.pzh1/1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function (ret) {
            toastr.hide();
            if (ret.pzh_list.length) {

                inputBox("提示", "以下凭证号不符合条件", ret.pzh_list.join('\n'), "", function (val) {
                }, false, { onContentReady: function () { } }, 10);

            } else {
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            }
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue > '0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                    onContentReady: function () {
                        $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                        window.qsqx_old = 0;
                    }
                }, 10);
        } else {
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c5abbb993294a6b34862e2
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 17:08:54.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status && data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status && data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd(dsname, rowCount, withLastSelect) {
    if (dsname == 'pz_yjd') {

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2/1 < data.pzh1/1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function (ret) {
            toastr.hide();
            if (ret.pzh_list.length) {
                inputBox("提示", "以下凭证号不符合条件", ret.pzh_list.join('\n'), "", function (val) {
                }, false, { onContentReady: function () { } }, 10);

            } else {
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            }
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function (ret) {
            toastr.hide();
            console.log(ret, '============ret')
            toastr.info("导入完成");
            // $pz_yjd_ds.smartRetrieve();
            $pz_yjdex_ds.smartRetrieve();
            $pz_yjd_pzh_ds.smartRetrieve();
            $q_pz_yjd_pzh_ds.smartRetrieve();
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue > '0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                    onContentReady: function () {
                        $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                        window.qsqx_old = 0;
                    }
                }, 10);
        } else {
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c5abbb993294a6b34862e6
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 17:09:17.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status && data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status && data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd(dsname, rowCount, withLastSelect) {
    if (dsname == 'pz_yjd') {

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 / 1 < data.pzh1 / 1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function (ret) {
            toastr.hide();
            if (ret.pzh_list.length) {
                inputBox("提示", "以下凭证号不符合条件", ret.pzh_list.join('\n'), "", function (val) {
                }, false, { onContentReady: function () { } }, 10);
            } else {
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            }
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function (ret) {
            toastr.hide();
            if (ret.pzh_list.length) {
                inputBox("提示", "以下凭证号不符合条件", ret.pzh_list.join('\n'), "", function (val) {
                }, false, { onContentReady: function () { } }, 10);
            } else {
                console.log(ret, '============ret')
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            }
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue > '0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                    onContentReady: function () {
                        $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                        window.qsqx_old = 0;
                    }
                }, 10);
        } else {
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


id: 66c5abbb993294a6b34862ea
scriptid: 662a47879932b5035f32efb0
tablename: app_template
clientip: 125.119.204.23
username: sa
createdate: 2024.08.21 17:09:19.000
script:
/**
 * 一个单据打开，
 * 1 先对类型为“表”的结果集做数据异步检索
 * 2 全部表检索完成之后，再执行 formOpen 事件
 * 3 再根据  actio 执行 newOpen/viewOpen/editOpen 之一
 * 4 再执行  afterOpen
 * 5 对类型为“结果集”的且需要自动检索的结果集，做异步检索
 * 6 异步检索完成后，最后执行  formOpenOver 事件
 */
//@comment (group=表单事件,caption=表单初始化全部完成之后)
function formOpenOver() {
    var data = $pz_yjd_ds.getJSON(0);
    // console.log(data)

    if (urlArgs.action == 'view') {
        book.getWorkSheet('汇总').getBrick('cb_add_pz').visible = false;
        book.getWorkSheet('清单').getBrick('cb_import').visible = false;
        book.getWorkSheet('清单').getBrick('cb_buildsum').visible = false;

        book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
        book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;


        book.getWorkSheet('清单').cells('qsqx').visible = false;
        book.getWorkSheet('清单').cells('qsqx').clickable = false;

        book.getWorkSheet('清单').cells('state').visible = false;
        book.getWorkSheet('清单').cells('state').clickable = false;


        if (data.flow_instance_status && data.flow_instance_status.indexOf('结束') > 0) {
            book.getWorkSheet('清单').cells('qsqx').visible = true;
            book.getWorkSheet('清单').cells('state').visible = true;
        }

    } else {

        if (data.flow_instance_status && data.flow_instance_status.indexOf('接收方签收确认') > 0) {
            book.getWorkSheet('清单').cells('qsqx').editable = true;
            book.getWorkSheet('清单').cells('state').editable = true;

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = false;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = false;
        } else {

            book.getWorkSheet('清单').cells('c_db_deleterow').visible = true;
            book.getWorkSheet('清单').cells('c_db_deleterow').clickable = true;
        }

    }

}

/**
 * 结果集检索完成
 * @param dsname 结果集名称
 * @param rowCount  本次检索的数据条数
 * @param withLastSelect   boolean型  检索是否是使用上一次检索的SQL
 */
function retrieveEnd(dsname, rowCount, withLastSelect) {
    if (dsname == 'pz_yjd') {

    }
}




//@comment (group=按钮,caption=添加凭证号)
function cb_add_pz_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh1').setValue('');
    sheet.cells('pzh2').setValue('');

    sheet.float('fa fa-cog', "添加凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 900, height: 400 });

}

//@comment (group=添加凭证号按钮,caption=取消)
function cb_cancel_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();
}

//@comment (group=添加凭证号按钮,caption=确认)
function cb_comfirn_onClick() {
    var sheet = book.getWorkSheet('添加凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh1: sheet.cells('pzh1').getValue(),
        pzh2: sheet.cells('pzh2').getValue(),
    }

    if (data.pzh2 / 1 < data.pzh1 / 1) {
        toastr.error('终止凭证号要比起始凭证号大');
        return false;
    }

    toastr.show("正在计算...");
    setTimeout(function () {
        back_add_pzh(data, function (ret) {
            toastr.hide();
            if (ret.pzh_list.length) {
                inputBox("提示", "以下凭证号不符合条件", ret.pzh_list.join('\n'), "", function (val) {
                }, false, { onContentReady: function () { } }, 10);
            } else {
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            }
        });
    }, 100);
}

function cb_import_onClick() {

    var sheet = book.getWorkSheet('导入凭证号');

    sheet.cells('ny').setValue('');
    sheet.cells('pzh').setValue('');

    sheet.float('fa fa-cog', "导入凭证号", {
        my: 'center',
        at: 'center',
        autoposition: 'down',
        offsetX: -5,
        offsetY: 5
    }, { width: 500, height: 400 });


    // inputBox("提示", "请将凭证号粘贴在此", "", "", function (val) {
    //     toastr.show("正在计算...");
    //     setTimeout(function () {
    //         back_import_pzh(val, function () {
    //             toastr.hide();
    //             toastr.info("导入完成");
    //             $pz_yjd_ds.smartRetrieve();
    //             $pz_yjdex_ds.smartRetrieve();
    //             $pz_yjd_pzh_ds.smartRetrieve();
    //             $q_pz_yjd_pzh_ds.smartRetrieve();
    //         });
    //     }, 100);
    // }, false, {}, 10);
}


//@comment (group=导入凭证号按钮,caption=取消)
function cb_cancel2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();
}

//@comment (group=导入凭证号按钮,caption=确认)
function cb_comfirn2_onClick() {
    var sheet = book.getWorkSheet('导入凭证号');
    sheet.floatPanelClose();

    var data = {
        ny: sheet.cells('ny').getValue(),
        pzh: sheet.cells('pzh').getValue(),
    }


    toastr.show("正在计算...");
    setTimeout(function () {
        back_import_pzh_data(data, function (ret) {
            toastr.hide();
            if (ret.pzh_list.length) {
                inputBox("提示", "以下凭证号不符合条件", ret.pzh_list.join('\n'), "", function (val) {
                }, false, { onContentReady: function () { } }, 10);
            } else {
                toastr.info("导入完成");
                // $pz_yjd_ds.smartRetrieve();
                $pz_yjdex_ds.smartRetrieve();
                $pz_yjd_pzh_ds.smartRetrieve();
                $q_pz_yjd_pzh_ds.smartRetrieve();
            }
        });
    }, 100);
}


function cb_buildsum_onClick() {
    form.save(false, function () {
        $confirm("提示", "确信要根据凭证号清单生成凭证号汇总信息吗?", function () {
            toastr.show("正在计算...");
            setTimeout(function () {
                back_build_sum(function () {
                    toastr.hide();
                    toastr.info("导入完成");
                    $pz_yjd_ds.smartRetrieve();
                    $pz_yjdex_ds.smartRetrieve();
                    $pz_yjd_pzh_ds.smartRetrieve();
                    $q_pz_yjd_pzh_ds.smartRetrieve();

                });
            }, 100);

        });
    });

}

/**
 * 单元格因为被编辑而改变。当用户通过编辑单元的值而让单元格发生值改变时本事件被触发。
 * 注意，当使用脚本 sheet.cells(row,col).setValue ( value) 来改变单元格的值时，本事件并不触发
 * @param sheet
 * @param cell
 * @param newValue
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChangedByEdit(sheet, cell, newValue, innerRow) {

    //通过别名来判断
    if (cell.alias == 'XXX') {

    }

    //通过数据库绑定情况来判断
    let bind = cell.bind;
    if (bind != null && bind.dataSource == 'q_pz_yjd_pzh' && bind.DBCol == 'qsqx') {

        var qsqx = $pz_yjd_ds.getValue(0, 'qsqx');
        var ycly = $q_pz_yjd_pzh_ds.getValue(innerRow, 'ycly');
        if (ycly == 'null' || ycly == null) ycly = '';

        window.qsqx_change = true;

        if (newValue > '0') {
            inputBox("提示", "请输入延长理由", ycly, "", function (val) {
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', val);
                $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', newValue);
            }, false, {
                    onContentReady: function () {
                        $q_pz_yjd_pzh_ds.setValue(innerRow, 'qsqx', window.qsqx_old);
                        window.qsqx_old = 0;
                    }
                }, 10);
        } else {
            $q_pz_yjd_pzh_ds.setValue(innerRow, 'ycly', '');
        }

    }


}



/**
 * 数据集中的数据发生变化
 * @param dsname 数据集名称
 * @param row   数据行号
 * @param col    字段名
 * @param oldValue   原值
 * @param newValue  新值
 */
function itemChanged(dsname, row, col, oldValue, newValue) {

    if (dsname == 'q_pz_yjd_pzh' && col == 'qsqx' && window.qsqx_change) {
        window.qsqx_old = oldValue;
        window.qsqx_change = false;
    }

}


