vatCommonService.js 24.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
webservices.factory('vatCommonService', ['$http', '$q', '$log', '$location', 'apiConfig', '$translate',
    'enums', 'SweetAlert', 'vatSessionService', 'projectService', 'vatWorkflowService', '$rootScope',
    function ($http, $q, $log, $location, apiConfig, $translate,
        enums, SweetAlert, vatSessionService, projectService, vatWorkflowService, $rootScope) {
    'use strict';
    return {
        sample: function () {
            return $http.get('url', apiConfig.createVat());
        },

        //设置项目状态 + 更新WorkflowTaskLog数据
        //dbName, periodId, status: 设置项目的状态
        //dicKey,isFinished: 设置Task是否完成
14
        setProjectStatus: function (projectId, periodId, status,dicKey,isFinished) {
15
            projectService.setProjectStatus(projectId, periodId, status).success(function (or) {
16 17 18
                if (or.result) {
                    $rootScope.$broadcast('statusChanged', { status: status });
                    //记录操作log
neo's avatar
neo committed
19 20 21
//                    if (!_.isNull(dicKey) && !_.isNull(isFinished)) {
//                        vatWorkflowService.setProcessLog(status,dbName, periodId, dicKey, isFinished);
//                    }
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
                    vatSessionService.project.projectStatusList[periodId] = status;            
                }
                else {
                    SweetAlert.error($translate.instant('SetProjectStatusError'));
                }
            });
        },


        //只更新设置项目状态
        //setProjectStatusSimple: function(dbName, periodId, status){
        //    projectService.setProjectStatus(dbName, periodId, status).success(function (or) {
        //        if (or.result) {
        //            $rootScope.$broadcast('statusChanged', { status: status });                 
        //            vatSessionService.project.projectStatusList[periodId] = status;
        //        }
        //        else {
        //            SweetAlert.error($translate.instant('SetProjectStatusError'));
        //        }
        //    });
        //},

        //导入操作设置项目状态
        //前提状态为未开始 -->  已导入
        //前提状态为已导入及后续状态 -->原状态 
        importSetProjectStatus: function (dbName, periodId, dicKey, isFinished) {
            $log.debug("vatCommonService importSetProjectStatus start.....");
            $log.debug("current status: " + vatSessionService.project.projectStatusList[periodId]);

            var projectStatus = -1;
            if (!_.isUndefined(vatSessionService.project.projectStatusList[periodId])) {
                if (_.isEqual(vatSessionService.project.projectStatusList[periodId], constant.ProjectStatusEnum.UnStarted)) {
                    projectStatus = constant.ProjectStatusEnum.Imported;
                }
                else {

                    projectStatus = vatSessionService.project.projectStatusList[periodId];
                }              
            }

            $log.debug("status before update db: " + projectStatus);
63
            projectService.setProjectStatus(project.id, periodId, projectStatus, dicKey, isFinished).success(function (or) {
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
                if (or.result) {             
                    $rootScope.$broadcast('statusChanged', { status: projectStatus });
                    //记录操作log
                    vatWorkflowService.setProcessLog(projectStatus, dbName, periodId, dicKey, isFinished);
                    vatSessionService.project.projectStatusList[periodId] = projectStatus;
                }
                else {
                    SweetAlert.error($translate.instant('SetProjectStatusError'));
                }
            });
        },
      
        //清空操作设置项目状态
        //有导入过任何数据时: 1): 状态>='已生成', 回退到'已生成'  2): < '已生成',回退到已导入; 
        //没有导入过任何数据时:保持原来的项目状态   
        clearProjectStatus: function (dbName, periodId, dicKey, isFinished) {
            projectService.isImportedAnyData(periodId, vatSessionService.project.id).success(function (or) {
                if (or.result) {
                    var projectStatus = -1;
                    if (or.data) {                        
                        projectStatus = vatSessionService.project.projectStatusList[periodId] >= constant.ProjectStatusEnum.Generated
                        ? vatSessionService.project.projectStatusList[periodId] : constant.ProjectStatusEnum.Imported;
                    }
                    else {
                        
                        projectStatus = vatSessionService.project.projectStatusList[periodId] <= constant.ProjectStatusEnum.AccountMapSubmitted
                        ? constant.ProjectStatusEnum.UnStarted : vatSessionService.project.projectStatusList[periodId];
                    }
                    
93
                    projectService.setProjectStatus(project.id, periodId, projectStatus,dicKey,isFinished).success(function (or) {
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
                        if (or.result) {
                            $rootScope.$broadcast('statusChanged', { status: projectStatus });
                            //记录操作log
                            vatWorkflowService.setProcessLog(constant.inValidNum,dbName, periodId, dicKey, isFinished);
                            vatSessionService.project.projectStatusList[periodId] = projectStatus;                        
                        }
                        else {
                            SweetAlert.error($translate.instant('SetProjectStatusError'));
                        }
                    });

                }
                else {
                    SweetAlert.error($translate.instant('SetProjectStatusError'));
                }
            });
        },

        //清空操作设施项目状态
        //清空所有当月导入数据:未开始;
        //否则: 1)前提状态为已导入-->已导入; 2) 前提状态为已对应、已生成--> 已对应
        clearDataSetProjectStatus: function (dbName, periodId, dicKey, isFinished) {
            projectService.isImportedAnyData(periodId, vatSessionService.project.id).success(function (or) {
                if (or.result) {
                    var projectStatus = -1;
                    if (or.data) {
                        if (!_.isUndefined(vatSessionService.project.projectStatusList[periodId])) {
                            if (_.isEqual(vatSessionService.project.projectStatusList[periodId], constant.ProjectStatusEnum.Imported)) {
                                projectStatus = constant.ProjectStatusEnum.Imported;
                            }

                            if (_.isEqual(vatSessionService.project.projectStatusList[periodId], constant.ProjectStatusEnum.AccountMapSubmitted)
                                  || _.isEqual(vatSessionService.project.projectStatusList[periodId], constant.ProjectStatusEnum.Generated)) {

                                projectStatus = constant.ProjectStatusEnum.AccountMapSubmitted;
                            }
                        }
                    }
                    else {
                        if (!_.isUndefined(vatSessionService.project.projectStatusList[periodId])) {
                            projectStatus = constant.ProjectStatusEnum.UnStarted;
                        }
                    }

138
                    projectService.setProjectStatus(project.id, periodId, projectStatus).success(function (or) {
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
                        if (or.result) {
                            $rootScope.$broadcast('statusChanged', { status: projectStatus });
                            //记录操作log
                            vatWorkflowService.setProcessLog(dbName, periodId, dicKey, isFinished);
                            vatSessionService.project.projectStatusList[periodId] = projectStatus;
                        }
                        else {
                            SweetAlert.error($translate.instant('SetProjectStatusError'));
                        }
                    });
                }
                else {
                    SweetAlert.error($translate.instant('SetProjectStatusError'));
                }
            });
        },


        //清空操作设施项目状态
        //清空所有当月导入数据时,状态变为:未开始; 否则:状态为已导入     
        clearDataSetProjectStatusSimple: function (dbName, periodId, dicKey, isFinished) {
            projectService.isImportedAnyData(periodId, vatSessionService.project.id).success(function (or) {
                if (or.result) {
                    var projectStatus = -1;
                    if (or.data) {
                        projectStatus = constant.ProjectStatusEnum.Imported;
                    }
                    else {
                        projectStatus = constant.ProjectStatusEnum.UnStarted;
                    }


171
                    projectService.setProjectStatus(project.id, periodId, projectStatus).success(function (or) {
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
                        if (or.result) {
                            $rootScope.$broadcast('statusChanged', { status: projectStatus });
                            //记录操作log
                            vatWorkflowService.setProcessLog(projectStatus, dbName, periodId, dicKey, isFinished);
                            vatSessionService.project.projectStatusList[periodId] = projectStatus;
                        }
                        else {
                            SweetAlert.error($translate.instant('SetProjectStatusError'));
                        }
                    });

                }
                else {
                    SweetAlert.error($translate.instant('SetProjectStatusError'));
                }
            });
        },

        //页面中导入、清除数据后,设置vatSessionService.project.importSubStatus子状态
        setImportSubStatus: function (statusId,isImported) {
            if (_.isEqual(enums.VatImportSubStatus.isAdjustImport, statusId)) {
                vatSessionService.project.importSubStatus.isAdjustImport = isImported;
            }
            
            if (_.isEqual(enums.VatImportSubStatus.isCustomInvoiceImport, statusId)) {
                vatSessionService.project.importSubStatus.isCustomInvoiceImport = isImported;
            }

            if (_.isEqual(enums.VatImportSubStatus.isEntryImport, statusId)) {
                vatSessionService.project.importSubStatus.isEntryImport = isImported;
            }

            if (_.isEqual(enums.VatImportSubStatus.isErpImport, statusId)) {
                vatSessionService.project.importSubStatus.isErpImport = isImported;
            }

            if (_.isEqual(enums.VatImportSubStatus.isInputInvoiceImport, statusId)) {
                vatSessionService.project.importSubStatus.isInputInvoiceImport = isImported;
            }

            if (_.isEqual(enums.VatImportSubStatus.isInvoiceMapImport, statusId)) {
                vatSessionService.project.importSubStatus.isInvoiceMapImport = isImported;
            }

            if (_.isEqual(enums.VatImportSubStatus.isOutputInvoiceImport, statusId)) {
                vatSessionService.project.importSubStatus.isOutputInvoiceImport = isImported;
            }

            if (_.isEqual(enums.VatImportSubStatus.isTbImport, statusId)) {
                vatSessionService.project.importSubStatus.isTbImport = isImported;
            }

            if (_.isEqual(enums.VatImportSubStatus.isVoucherMapImport, statusId)) {
                vatSessionService.project.importSubStatus.isVoucherMapImport = isImported;
            }
        },


        getProjectStautsEnumDesc: function (status) {
            switch (status) {
                case constant.ProjectStatusEnum.UnStarted:
                    return $translate.instant('ProjectStatusUnStarted');
                case constant.ProjectStatusEnum.Imported:
                    return $translate.instant('ProjectStatusImported');
                case constant.ProjectStatusEnum.AccountMapSubmitted:
                    return $translate.instant('ProjectStatusAcccountMapSubmitted');
                case constant.ProjectStatusEnum.Generated:
                    return $translate.instant('ProjectStatusGenerated');
                case constant.ProjectStatusEnum.ReportSubmitted:
                    return $translate.instant('ProjectStatusReportSubmitted');
                case constant.ProjectStatusEnum.ReportApproved:
                    return $translate.instant('ProjectStatusReportApproved');
                case constant.ProjectStatusEnum.ReportRejected:
                    return $translate.instant('ProjectStatusReportRejected');
                case constant.ProjectStatusEnum.Completed:
                    return $translate.instant('ProejctStatusCompleted');
                default:
                    return 'todo';
            }       
        },


        //获取当前登录用户在'报表审核'节点中有几级审批
        getLoginUserApproveReportLevels: function () {
            var wf = vatSessionService.project.workflow;        
            var approveLevel = []; //存放登录用户具有的审批层级
            var approveReprotNode = _.where(wf.workflowNodeList, { dictionaryKey: constant.DictionaryDictKey.ApproveReport })[0];
            if (!_.isNull(approveReprotNode) && !_.isUndefined(approveReprotNode)) {
                approveReprotNode.auditReportLevelList.forEach(function (item, index) {
                    var userList = _.where(item.userDetailList, { userId: vatSessionService.logUser.id });
                    if (userList.length > 0) {
                        approveLevel.push(item.orderIndex);
                    }
                });
            }
            return approveLevel;           
        },


        //获取当前登录用户在当前'报表审核'的审批
        getLoginUserCurrentApproveReportLevel: function () {
            var wf = vatSessionService.project.workflow;        
            var approveReprotNode = _.where(wf.workflowNodeList, { dictionaryKey: constant.DictionaryDictKey.ApproveReport })[0];
            if (!_.isNull(approveReprotNode) && !_.isUndefined(approveReprotNode)) {
                return approveReprotNode.currentApproveLevel;
            }                     
        },


        //判断报表审核中上个level是否审核通过
        //ctOrderIndex: 当前审批层级的OrderIndex
        isReportLastLevelApproved: function (ctOrderIndex) {
            var wf = vatSessionService.project.workflow;      
            var approveReprotNode = _.where(wf.workflowNodeList, { dictionaryKey: constant.DictionaryDictKey.ApproveReport })[0];
            if (ctOrderIndex === 0 || ctOrderIndex === 1) { //审批第一级时,总是返回true
                return true;
            }
            else {
                var lastLevel = _.where(approveReprotNode.auditReportLevelList, { orderIndex: ctOrderIndex - 1 })[0];
                if (!_.isNull(lastLevel) && !_.isUndefined(lastLevel)) {
                    return lastLevel.isDone;
                }
            }
                               
        },


        //判断报表审核的上个节点是否完成
        isPreviousNodeFinished: function (dicKey, wf) {
            var result = false;
            var crtNode = _.where(wf.workflowNodeList,{dictionaryKey: dicKey})[0];
            if(!_.isNull(crtNode) && !_.isUndefined(crtNode)){
                var previousNode = _.where(wf.workflowNodeList, { orderIndex: crtNode.orderIndex - 1 })[0];
                result = !_.isNull(previousNode) && !_.isUndefined(previousNode)
                                 ? previousNode.isDone : crtNode.isDone; //previousNode == null说明ViewReport是第一个节点
            }

            return result;
        },

        //数据导入,数据预览,数据整理layout: vat-import-layout.ctl.js
        isLayoutMenuShow: function (dbName, month, nodeDicKeyValue, linkShort, menus) {
            $log.debug("start to isLayoutMenuShow");
            $q.all([vatWorkflowService.getWorkflowDetailByDb(dbName, month)]).then(function (result) {
                var wfData = result[0].data;
                if (wfData.result) {
                    var wf = wfData.data;
                    if (_.isNull(wf) || _.isUndefined(wf)) {//当前项目没有配置workflow,就忽略workflow的配置,全部显示
                        $.each(menus, function (index, m) { m.show = true; });
                    }
                    else {
                        var wfNodesList = wf.workflowNodeList;          
                        var nodeDto = _.where(wfNodesList, { dictionaryKey: constant.DictionaryDictKey[nodeDicKeyValue] })[0];
                        if (!_.isNull(nodeDto) && !_.isUndefined(nodeDto)) {
                            if (nodeDto.isActive === false) { //Node不可用,全部隐藏
                                $.each(menus, function (index, m) { m.show = false; });
                            }
                            else {
                                var taskMenus = [];//存放已经处理过的menu name
                                $.each(nodeDto.workflowTaskList, function (index, item) {
                                    var menu = _.where(menus, { name: enums.dictionaryKeyMenusMap[item.dictionaryKey] })[0];
                                    if (!_.isNull(menu) && !_.isUndefined(menu)) {
                                        menu.show = item.isActive;
                                        taskMenus.push(menu.name);
                                    }
                                });

                                //没有配置过的Task对应的menu需要隐藏
                                $.each(menus, function (index, item) {
                                    var findResult = _.find(taskMenus, function (n) {
                                        return _.isEqual(n, item.name);
                                    });

                                    if (!findResult) {
                                        item.show = false;
                                    }
                                });
                            }

                            //上一个节点未完成的话,弹出提示框
                            var lastNodeDto = _.where(wfNodesList, { orderIndex: nodeDto.orderIndex - 1 })[0];
                            if (!_.isNull(lastNodeDto) && !_.isUndefined(lastNodeDto)) {
                                //debug: only for debug
                                //lastNodeDto.isDone = false;
                                if (!lastNodeDto.isDone) {
                                    SweetAlert.warning($translate.instant('LastNodeIsNotFinished'));
                                }
                            }
                        }
                        else {

                            $log.debug("nodeDto is undefined");
                        }
                    }
                }
            });
                   
            $log.debug("menus before return");
            $log.debug(menus);
            return menus;
        },

    
        isViewReportBtShow: function (wf, buttonStatus) {
            var viewReportNode = _.where(wf.workflowNodeList, { dictionaryKey: constant.DictionaryDictKey.ViewReport })[0];         
            if (!_.isNull(viewReportNode) && !_.isUndefined(viewReportNode)) {
                var reportPreviewNode = _.where(wf.workflowNodeList, { orderIndex: viewReportNode.orderIndex - 1 })[0];
                var isPreveiwNodeDone = !_.isNull(reportPreviewNode) ? reportPreviewNode.isDone : viewReportNode.isDone; //reportPreviewNode == null说明ViewReport是第一个节点         
                var submitTask = _.where(viewReportNode.workflowTaskList, { dictionaryKey: constant.DictionaryDictKey.WFSubmitReport })[0];
                if (!_.isNull(submitTask) && !_.isUndefined(submitTask)) {
                    buttonStatus.viewReportBtShow = isPreveiwNodeDone && submitTask.isActive && !submitTask.isDone;
                }
                else {//没有配置“提交审核”任务
                    buttonStatus.viewReportBtShow = false;
                }                             
            }
            else {//没有配置“查看报表”节点
                buttonStatus.viewReportBtShow = false;
            }

            return buttonStatus;
        },


        isApproveReportBtShow: function (wf, buttonStatus) {
            var viewReportNode = _.where(wf.workflowNodeList, { dictionaryKey: constant.DictionaryDictKey.ViewReport })[0];
            var approveReportNode = _.where(wf.workflowNodeList, { dictionaryKey: constant.DictionaryDictKey.ApproveReport })[0];      
            if (!_.isNull(approveReportNode) && !_.isUndefined(approveReportNode)) {
                var approveTask = _.where(approveReportNode.workflowTaskList, { dictionaryKey: constant.DictionaryDictKey.WFReportApproval })[0];
                var currentApproveLevel = approveReportNode.currentApproveLevel; //项目当前所在的审批层级
                var approveLevelUsers = _.where(approveReportNode.auditReportLevelList, { orderIndex: currentApproveLevel })[0].userDetailList; //当前审批层级的审核人
                var userHasApproveAuth = _.where(approveLevelUsers, { userId: vatSessionService.logUser.id })[0]; //登录用户是否在审核人之中
                var isUserHasApproveAuth = !_.isNull(userHasApproveAuth) && !_.isUndefined(userHasApproveAuth) ? true : false;
           
                if (!_.isNull(approveTask) && !_.isUndefined(approveTask)) {

                    if (!_.isNull(viewReportNode) && !_.isUndefined(viewReportNode)) { //配置了上一节点“查看报表”节点

                        buttonStatus.approveReportBtShow = viewReportNode.isDone && approveTask.isActive && !approveTask.isDone && isUserHasApproveAuth;
                    }
                    else { //没有配置上一节点“提交审核”任务
                        buttonStatus.approveReportBtShow = approveTask.isActive && !approveTask.isDone && isUserHasApproveAuth;
                    }
                }
                else {//没有配置“报表审核”Task
                    buttonStatus.approveReportBtShow = false;
                }

            }
            else { //没有配置“报表审核”节点
                buttonStatus.approveReportBtShow = false;
            }

            return buttonStatus;
        },


        isDeclarationComplete: function (wf, buttonStatus) {
            var approveReportNode = _.where(wf.workflowNodeList, { dictionaryKey: constant.DictionaryDictKey.ApproveReport })[0];
            var decCompleteNode = _.where(wf.workflowNodeList, { dictionaryKey: constant.DictionaryDictKey.DeclarationComplete })[0];

            if (!_.isNull(decCompleteNode) && !_.isUndefined(decCompleteNode)) {
                var decCompleteTask = _.where(decCompleteNode.workflowTaskList, { dictionaryKey: constant.DictionaryDictKey.WFDeclarationComplete })[0];
                var uploadCertifiTask = _.where(decCompleteNode.workflowTaskList, { dictionaryKey: constant.DictionaryDictKey.WFUploadCertificate })[0];

                //“完成申报”Task
                if (!_.isNull(decCompleteTask) && !_.isUndefined(decCompleteTask)) {

                    if (!_.isNull(approveReportNode) && !_.isUndefined(approveReportNode)) { //配置了上一节点“报表审核”

                        buttonStatus.decCompleteBtShow = approveReportNode.isDone && decCompleteTask.isActive && !decCompleteTask.isDone;
                    }
                    else {//没有配置上一节点“报表审核”
                        buttonStatus.decCompleteBtShow = decCompleteTask.isActive && !decCompleteTask.isDone;
                    }
                }
                else {//没有配置“完成申报”Task
                    buttonStatus.decCompleteBtShow = false;
                }


                //“上传凭证”Task
                if (!_.isNull(uploadCertifiTask) && !_.isUndefined(uploadCertifiTask)) {

                    if (!_.isNull(approveReportNode) && !_.isUndefined(approveReportNode)) { //配置了上一节点“报表审核”

                        buttonStatus.uploadCerficateBtShow = approveReportNode.isDone && uploadCertifiTask.isActive && !uploadCertifiTask.isDone;
                    }
                    else {//没有配置上一节点“报表审核”
                        buttonStatus.uploadCerficateBtShow = uploadCertifiTask.isActive && !uploadCertifiTask.isDone;
                    }

                }
                else {
                    buttonStatus.uploadCerficateBtShow = false;
                }


            }
            else { //没有配置“完成申报”节点
                buttonStatus.decCompleteBtShow = false;
                buttonStatus.uploadCerficateBtShow = false;
            }

            return buttonStatus;
        },



    };
}]);