invoice-manage-main-upload-electronic-invoice.ctrl.js 15.2 KB
Newer Older
frank.xa.zhang's avatar
frank.xa.zhang committed
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 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 171 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
invoiceModule.controller('invoiceManageMainUploadElectronicInvoiceController', ['$scope', '$log', '$document', '$uibModal', 'SweetAlert', '$translate', '$q', 'apiInterceptor', '$interval', 'region', '$timeout', 'InvoiceManageService', 'Upload',
function ($scope, $log, $document, $uibModal, SweetAlert, $translate, $q, apiInterceptor, $interval, region, $timeout, InvoiceManageService,Upload) {
            'use strict';
        
            //分页的设置
            $scope.pagingOptions = {
                pageIndex: 1,  //当前页码
                totalItems: 100,  //总数据
                totalPages: 10,//总页数
                maxSize: 10, //分页数字的限制。
                pageSize: constant.page.pageSizeArrary[1],  //每页多少条数据 
                pageSizeString: constant.page.pageSizeArrary[1].toString(),
                firstPage: $translate.instant('PagingFirstPage'),
                previousPage: $translate.instant('PagingPreviousPage'),
                nextPage: $translate.instant('PagingNextPage'),
                lastPage: $translate.instant('PagingLastPage'),
            };

            $scope.searchFilterOptions = [];
            $scope.searchEntity = [];

            $scope.translated = {
                NoData: $translate.instant('NoDataText'),
                pleaseSelect: $translate.instant("ChoosePlaceholder"),
                pleaseInput: $translate.instant('InputPlaceholder'),
            };



            var fileUploadInfo = {
                fileList: [],
                path: "C:\\Users\\admin\\Desktop\\Scan\\Temp",
                strEx: '.jpg',
                userId: '90CEF6C1C1C0762000D2'
            };
            var nResult = -1;

            var chunkSize = 100000;
            var resumable = true;
            var uploadInvoiceUrl = apiInterceptor.webApiHostUrl + '/invoiceManage/UploadInvoiceFile';

            //发票上传
            $scope.invoiceUpload = {
                //加载核心
                loadRecogKenal: function () {
                    nResult = objBillOcr.InitRecogForm(fileUploadInfo.userId);
                    if (nResult != 0) {
                        SweetAlert.warning("核心加载失败,返回值:" + nResult);
                        return false;
                    }
                    return true;
                },
                //释放核心
                freeRecogKenal: function () {
                    objBillOcr.UninitRecogForm();
                },
                //扫描上传
                scanUpload: function () {
                    if (!invoiceUpload.loadRecogKenal())
                        return;
                    //设置要识别的票据类型 
                    objBillOcr.SetFormTypes(document.IDScanRecog.BillType.value, 3);
                    objBillOcr.SetDeviceAdvancedParams(6, 0);
                    objBillOcr.SetDeviceAdvancedParams(10, 10);
                    var bHavePaper = 1;
                    //扫描
                    while (bHavePaper) {
                        var fileItem = {};
                        var dateNow = new Date().toDateString('yyyyMMddHHmmss');
                        var imagePath = fileUploadInfo.path + "\\" + dateNow + fileUploadInfo.strEx;
                        fileItem.filePath = imagePath;
                        fileItem.fileName = dateNow;
                        nResult = objBillOcr.AcquireNewImage(imagePath);
                        if (nResult != 0) {
                            fileItem.message = "扫描失败,返回值:" + nResult;
                            fileItem.status = false;
                        }
                        fileUploadInfo.fileList.push(fileItem);
                        bHavePaper = objBillOcr.IsHavePaper();
                    }
                    //to do
                    invoiceUpload.initFiles();

                    //遍历识别
                    fileUploadInfo.fileList.forEach(function (item) {
                        //对扫描成功的进行识别
                        if (item.status) {
                            nResult = objBillOcr.LoadImages(item.filePath);
                            if (nResult != 0) {
                                item.message = "加载图片不成功,返回值:" + nResult;
                                item.status = false;
                            }
                            nResult = objBillOcr.RecognizeForm();
                            if (nResult != 0) {
                                item.message = "识别不成功,返回值:" + nResult;
                                item.status = false;
                            }
                            else
                                item.status = true;
                            var nFieldNum = objBillOcr.GetRecogCellCount();
                            var itemData = {};
                            if (nFieldNum > 0) {
                                for (var i = 0; i < nFieldNum; ++i) {
                                    var strFieldName = objBillOcr.GetFieldName(i);
                                    strResult += strFieldName;
                                    strResult += ":";
                                    var strResultName = objBillOcr.GetRecognizeResult(i);
                                    if (strFieldName == "票据类型") {
                                        if (strResultName == "1") {
                                            strResultName = "普票";
                                        }
                                        else {
                                            strResultName = "专票";
                                        }

                                    }
                                    itemData[strFieldName] = strResultName;
                                }
                            }
                            item.itemData = itemData;
                            //to do
                            invoiceUpload.updateFileStatus(item);
                        }
                    });
                    invoiceUpload.freeRecogKenal();
                },

                initFiles: function () {
                    //to do
                    //根据文件List更新UI
                },
                updateFileStatus: function (fileItem) {
                    //to do 
                    //根据文件识别情况更新UI
                },
                //上传文件
                uploadInvoiceFile: function (files, inputInvoice) {
                    if (files && files.length) {
                        //allFilesCount = files.length;
                        //evidenceCount = 0;
                        for (var i = 0; i < files.length; i++) {
                            var file = files[i];
                            if (!file.$error) {
                                var tempFileName = PWC.newGuid() + '.dat';
                                var token = $('input[name="__RequestVerificationToken"]').val();
                                Upload.upload({
                                    url: uploadInvoiceUrl,
                                    data: {
                                        cancel: false,
                                        filename: file.name,
                                        tempFileName: tempFileName,
                                        file: file,
                                        inputInvoice: inputInvoice
                                    },
                                    resumeChunkSize: resumable ? chunkSize : null,
                                    headers: {
                                        'Access-Control-Allow-Origin': '*',
                                        Authorization: apiInterceptor.tokenType + ' ' + apiInterceptor.apiToken(),
                                        from: 'taxAdmin@cn.pwc.com'
                                    },
                                    __RequestVerificationToken: token,
                                    withCredentials: true
                                }).then(uploadInvoiceSuccess);
                            }
                        }
                    }
                },

                //上传成功
                uploadInvoiceSuccess: function (resp) {
                    //to do
                }
            };

             //内部处理
            var memoryService = {
                //发票来源列表构造
                populateInvoiceSourceTypList: function () {
                    $scope.searchFilterOptions.invoiceSourceList = memoryService.translateEnumValue(constant.inputInvoice.invoiceSourceTypeArray);
                },

                repaintInvoiceGrid: function () {
                    $scope.setInvoiceGridHeight();
                },
                //翻译查询的数据源
                translateEnumValue: function (data) {
                    if (!data || data.length == 0) return null;

                    data.forEach(function (item) {
                        item.value = $translate.instant(item.value);
                    });
                    return data;
                },
                //分页下拉
                populatePagingSelection: function () {
                    var pagingSelection = [];
                    var pageArray = constant.page.pageSizeArrary;
                    for (var i = 0 ; i < pageArray.length; i++) {
                        var selection = { id: pageArray[i], value: pageArray[i] };
                        pagingSelection.push(selection);
                    }
                    $scope.pagingOptions.pagingSelection = pagingSelection;
                },
            };



            //搜索框初始化
            var initControlsOptions = function () {

                var valueExpr = "id";
                var displayExpr = "value";

                var isShowClearButton = false;

                $scope.searchEntityOptions = {
                    selectinvoiceSourceTypeOptions: {
                        displayExpr: displayExpr,
                        valueExpr: valueExpr,
                        bindingOptions: {
                            value: 'searchEntity.invoiceSourceType',
                            dataSource: 'searchFilterOptions.invoiceSourceList'
                        },
                        onValueChanged: function (data) {
                            $scope.showPoOptions = false;
                            $scope.showStaffOptions = false;
                            if (data.value == constant.inputInvoice.invoiceSourceType.InvoiceNonRawMaterialPurchase.id) {
                                $scope.showPoOptions = true;
                            }
                            else if (data.value == constant.inputInvoice.invoiceSourceType.InvoiceClaim.id) {
                                $scope.showStaffOptions = true;
                            }
                        },
                        placeholder: $scope.translated.pleaseSelect,
                        showClearButton: isShowClearButton,
                        noDataText: $scope.translated.NoData,
                    },
                    //全选
                    checkAllOptions: {
                        onValueChanged: function (e) {
                            var row = e.model.template;
                            if (row) {
                                row.isCheckAll = e.value;
                                row.currentInvoiceList.forEach(function (item) {
                                    item.isCheckAll = e.value;
                                });
                            }
                            console.log(e.element);
                        }
                    },
                    //员工编号
                    staffIdOPtions: {
                        bindingOptions: {
                            value: 'searchEntity.staffID',
                        },
                        placeholder: $scope.pleaseInput,
                        showClearButton: isShowClearButton,
                    },
                    //po number
                    PoNumberOptions: {
                        bindingOptions: {
                            value: 'searchEntity.poNumber',
                        },
                        placeholder: $scope.pleaseInput,
                        showClearButton: isShowClearButton,
                    }
                };
            };

            //完成上传
            $scope.uploadToServer = function () {
                $scope.uploadInvoiceList;
            };

            var initInvoiceList = function () {
                $scope.uploadInvoiceList = [];

                for (var i = 0; i < 3; i++) {

                    var currentInvoiceList = [{
                        id: 0,
                        value: '58913988777',
                        status: constant.uploadStatus.success,
                        time: '2017/08/20 12:30'
                    }, {
                        id: 0,
                        value: '58913988777',
                        status: constant.uploadStatus.success,
                        time: '2017/08/20 12:30'
                    }, {
                        id: 0,
                        value: '58913988777',
                        status: constant.uploadStatus.success,
                        time: '2017/08/20 12:30'
                    }, {
                        id: 0,
                        value: '58913988777',
                        status: constant.uploadStatus.success,
                        time: '2017/08/20 12:30'
                    }, {
                        id: 0,
                        value: '58913988777',
                        status: constant.uploadStatus.success,
                        time: '2017/08/20 12:30'
                    }, {
                        id: 0,
                        value: '58913988777',
                        status: constant.uploadStatus.success,
                        time: '2017/08/20 12:30'
                    }, {
                        id: 1,
                        value: '58913988007',
                        status: constant.uploadStatus.failure,
                        time: '2017/08/20 12:30'
                    }, {
                        id: 2,
                        value: '58913988807',
                        status: constant.uploadStatus.failure,
                        time: '2017/08/20 12:30'
                    }, {
                        id: 3,
                        value: '58213988807',
                        status: constant.uploadStatus.success,
                        time: '2017/08/20 12:30'
                    }, {
                        id: 4,
                        value: '68913988807',
                        status: constant.uploadStatus.duplicate,
                        time: '2017/08/20 12:30'
                    }];

                    var invoice = {
                        id: i,
                        value: i + ' invoice',
                        isCheckAll: false,
                        currentInvoiceList: currentInvoiceList
                    }
                    $scope.uploadInvoiceList.push(invoice);

                }
            };


            (function initialize() {
                $log.debug('invoiceManageMainUploadElectronicInvoiceController.ctor()...');
                initInvoiceList();
                initControlsOptions();
                memoryService.populateInvoiceSourceTypList();
            })();
        }
]);