rule-engine-operation.svc.js 5.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
systemConfigurationModule.factory('ruleEngineOp', ['ruleEngineService', function (ruleEngineService) {
    var operations = {};
    operations.taxRuleSettingOperations = [];
    operations.taxPayerReportRuleOperations = [];

    function add(obj, prop, op) {
        var addOperation = {};
        addOperation.action = 'Add';
        addOperation[prop] = op;

        operations[obj].push(addOperation);
        console.log(operations[obj]);
    }

    function update(obj, prop,op) {
        var ret = operations[obj].filter(function (operation) {
            return operation[prop].editKey === op.editKey;
        });
        if (ret.length !== 0) {
            return;
        }
        updateOperation = {};
        updateOperation.action = 'Update';
        updateOperation[prop] = op;
        operations[obj].push(updateOperation);
        console.log(operations[obj]);
    }

    function del(obj, prop, op) {
        var isAlreadyInList = operations[obj].filter(function (operation) {
            return operation[prop].editKey === op.editKey;
        }).length !== 0;

        if (isAlreadyInList === true) {
            operations[obj] = operations[obj].filter(function (operation) {
                return operation[prop].editKey !== op.editKey;
            });
        }

        if (op.isNotPersistent !== true) {
            var deleteOperation = {};
            deleteOperation.action = 'Delete';
            deleteOperation[prop] = op;
            operations[obj].push(deleteOperation);
        }
        console.log(operations[obj]);
    };

    function validationBeforCommit() {
        //start to validation
        //first find the blank row
        //second check validation count
       // console.log(operations.taxRuleSettingOperations);
        //console.log(operations.taxPayerReportRuleOperations);
        var isValid = true;
        operations.taxRuleSettingOperations.every(function (taxRuleSettingOperation) {
            if (taxRuleSettingOperation.action === 'Add' || taxRuleSettingOperation.action === 'Update') {
                var taxRuleSetting = taxRuleSettingOperation.taxRuleSetting;
                if (!(angular.isArray(taxRuleSetting.orgs) && taxRuleSetting.orgs.length > 0) && taxRuleSetting.isDefault === 0 ) {
                    isValid = false;
                } else if (!(angular.isString(taxRuleSetting.taxBase)  || taxRuleSetting.taxBase.trim() === '')) {
                    isValid = false;
                } else if (!angular.isNumber(taxRuleSetting.taxRate)) {
                    isValid = false;
                }

                if (isValid === false) {
                    return false;
                }
                return true;
            }
        });

        operations.taxPayerReportRuleOperations.every(function (taxPayerReportRuleOperation) {
            if (taxPayerReportRuleOperation.action === 'Add' || taxPayerReportRuleOperation.action === 'Update') {
                var taxPayerReportRule = taxPayerReportRuleOperation.taxPayerReportRule;
                if (!(angular.isArray(taxPayerReportRule.orgs) && taxPayerReportRule.orgs.length > 0) && taxPayerReportRule.isDefault === 0) {
                    isValid = false;
                } else if (!angular.isNumber(taxPayerReportRule.taxPayerType)) {
                    isValid = false;
                } else if (!angular.isString(taxPayerReportRule.templateGroupID)) {
                    isValid = false;
                }

                if (isValid === false) {
                    return false;
                }
                return true;
            }
        });

        return isValid;
    };


    return {
        addTaxRuleSettingOperation: function (taxRuleSetting) {
            add('taxRuleSettingOperations', 'taxRuleSetting', taxRuleSetting);
        },
        updateTaxRuleSettingOperation: function (taxRuleSetting) {
            update('taxRuleSettingOperations', 'taxRuleSetting', taxRuleSetting);
        },
        deleteTaxRuleSettingOperation: function (taxRuleSetting) {
            del('taxRuleSettingOperations', 'taxRuleSetting', taxRuleSetting);
        },
        addTaxPayerReportRuleOperation: function (taxPayerReportRule) {
            add('taxPayerReportRuleOperations', 'taxPayerReportRule', taxPayerReportRule);
        },
        updateTaxPayerReportRuleOperation: function (taxPayerReportRule) {
            update('taxPayerReportRuleOperations', 'taxPayerReportRule', taxPayerReportRule);
        },
        deleteTaxPayerReportRuleOperation: function (taxPayerReportRule) {
            del('taxPayerReportRuleOperations', 'taxPayerReportRule', taxPayerReportRule);
        },
        clearOperations:function() {
            operations.taxRuleSettingOperations = [];
            operations.taxPayerReportRuleOperations = [];
        },
        commit: function (successCallback) {
            if (validationBeforCommit()) {
                var batchUpdateTaxRuleDto = {
                    taxRuleSettingOperations: operations.taxRuleSettingOperations,
                    taxPayerReportRuleOperations: operations.taxPayerReportRuleOperations
                };

                //console.log(batchUpdateTaxRuleDto);
                ruleEngineService.saveTaxRuleSettings(batchUpdateTaxRuleDto).success(function () {
                    operations.taxRuleSettingOperations = [];
                    operations.taxPayerReportRuleOperations = [];
                    successCallback();
                });
            } else
            {
                //sweet alert
                swal('有未填写项');
            }
           
        }
    }
}]);