gd-view-by-vin.ctrl.js 20.8 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 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 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
invoiceModule.controller('gdViewByVINController', ['$log', '$timeout', '$scope', '$translate', 'vatOutputInvoiceManageService',
    'SweetAlert', 'enums', 'modalAdapterService',
    function ($log, $timeout, $scope, $translate, vatOutputInvoiceManageService, SweetAlert, enums,
        modalAdapterService) {
        var dateFormat = 'yyyy-MM-dd';
        var revertStatusList = _.invert(enums.GDBDStatusEnum);
        var revertGDStatusList = _.invert(enums.GDInvoiceStatusEnum);
        $scope.pleaseSelect = $translate.instant("ChoosePlaceholder");
        $scope.pleaseInput = $translate.instant('InputPlaceholder');
        $scope.noDataText = $translate.instant('NoDataText');
        $scope.masterDetailHeader = {
            invoiceNo: $translate.instant('InvoiceNo'),
            invoiceCode: $translate.instant('InvoiceCode'),
            invoiceCaseNum: $translate.instant('InvoiceCase'),
            issuedDate: $translate.instant('IssuedDate'),
            buyer: $translate.instant('Buyer'),
            amount: $translate.instant('Amount'),
            taxAmount: $translate.instant('TaxAmount'),
            totalAmount: $translate.instant('TotalAmount'),
            gdStatus: $translate.instant('GDInvoiceStatus'),
            status: $translate.instant('VehicleStatus')
        };
        $scope.pagingOptions = {
            pageIndex: 1,  //当前页码
            totalItems: 0,  //总数据
            pageSize: constant.page.pageSizeArrary[1],  //每页多少条数据 
        };
        $scope.gdVinDetailVariables = {
            selectedTableRows: [],
            pageName: $translate.instant('GDVinView'),
            selectedGDInvoice: null

        };
        $scope.gdVinSearchEntity = {
            invoiceNumberVaule: null,
            invoiceCodeVaule: null,
            vinValue: null,
            caseNumberValue: null,
            vehicleModelValue:null

        };
        $scope.gdVinInvoice = {
            invoiceNumberOptions: {
                bindingOptions: {
                    'value': 'gdVinSearchEntity.invoiceNumberVaule'
                },
                showClearButton: true,
                placeholder: $scope.pleaseInput
            },
            invoiceCodeOptions: {
                bindingOptions: {
                    'value': 'gdVinSearchEntity.invoiceCodeVaule'
                },
                showClearButton: true,
                placeholder: $scope.pleaseInput
            },
            vinOptions: {
                bindingOptions: {
                    'value': 'gdVinSearchEntity.vinValue'
                },
                showClearButton: true,
                placeholder: $scope.pleaseInput
            },
            caseNumberOptions: {
                bindingOptions: {
                    'value': 'gdVinSearchEntity.caseNumberValue'
                },
                showClearButton: true,
                placeholder: $scope.pleaseInput
            },
            vehicleModelColonOptions: {
                bindingOptions: {
                    'value': 'gdVinSearchEntity.vehicleModelValue',
                    'dataSource': 'vehicleModelSource'
                },
                showClearButton: true,
                displayExpr: 'name',
                valueExpr: 'id',
                placeholder: $scope.pleaseSelect
            },
            gdVinGridOptions: {
                bindingOptions: {
                    dataSource: 'vinList',
                    'height': 'height'
                },
                keyExpr: 'vin',
                noDataText: $scope.noDataText,
                columnAutoWidth: true,
                showRowLines: true,
                showBorders: true,
                columnChooser: {
                    enabled: true
                },
                paging: {
                    enabled: false
                },
                loadPanel: {
                    enabled: false,
                },
                //selection: {
                //    mode: 'multiple',
                //    showCheckBoxesMode: 'always'
                //},
                //onSelectionChanged: function (e) {
                //    var selectedVin = e.selectedRowsData; 
                //    var mainTable = $('#gdVinGrid').dxDataGrid('instance');
                //    _.each(selectedVin,function(item){
                //        $scope.gdVinDetailVariables.selectedTableRows.concat(item.gdInvoices);
                //        if (mainTable.isAdaptiveDetailRowExpanded(item.vin)) {
                //            $("#vin-" + key).dxDataGrid("instance").selectAll();
                //        }
                //    });

                //},
                //onRowExpanded:function(e){
                //    var key = e.key;
                //    var mainTable = $('#gdVinGrid').dxDataGrid('instance');
                //    if (mainTable.isRowSelected(key)) {
                //        $("#vin-" + key).dxDataGrid("instance").selectAll();
                //    }
                //    //deselectRows(keys)
                //},
                columns: [
                    { dataField: 'vin', caption: $translate.instant('VIN'), allowHeaderFiltering: false },
                    { dataField: 'vehicleModel', caption: $translate.instant('VehicleModel'), allowHeaderFiltering: false },
                    { dataField: 'count', caption: $translate.instant('InvoiceCount'), allowHeaderFiltering: false },
                    {
                        dataField: 'totalTaxAmount', caption: $translate.instant('TotalTaxAmount'),
                        format: { type: 'fixedPoint', precision: 2 }, allowHeaderFiltering: false
                    },
                    {
                        dataField: 'totalEffectiveAmount', caption: $translate.instant('TotalEffectiveAmount'),
                        format: { type: 'fixedPoint', precision: 2 }, allowHeaderFiltering: false
                    },
                ],
                masterDetail: {
                    enabled: true,
                    template: "gdInvoiceTemplate"
                },
                gdView: function (gdInvoices) {
                    return {
                        dataSource: gdInvoices,
                        noDataText: $scope.noDataText,
                        columnAutoWidth: true,
                        //showRowLines: true,
                        selection: {
                            mode: 'multiple',
                            showCheckBoxesMode: 'always'
                        },
                        onSelectionChanged: function (e) {
                            var selectedVin = e.currentSelectedRowKeys;
                            var deSelectArray = e.currentDeselectedRowKeys;
                            // var select
                            if (deSelectArray.length > 0) {
                                $scope.gdVinDetailVariables.selectedTableRows = _.difference($scope.gdVinDetailVariables.selectedTableRows, deSelectArray);
                            }
                            if (selectedVin.length > 0) {
                                $scope.gdVinDetailVariables.selectedTableRows = $scope.gdVinDetailVariables.selectedTableRows.concat(selectedVin);
                            }

                        },
                        showBorders: true,
                        columns: [
                          { dataField: 'gdInvoiceNumber', caption: $scope.masterDetailHeader.invoiceNo, allowHeaderFiltering: false },
                          { dataField: 'gdInvoiceCode', caption: $scope.masterDetailHeader.invoiceCode, allowHeaderFiltering: false },
                            {
                                dataField: 'gdStatus', caption: $scope.masterDetailHeader.gdStatus,
                                calculateDisplayValue: function (rowData) {
                                    return $translate.instant(revertGDStatusList[rowData.gdStatus]);
                                }
                            },
                          { dataField: 'invoiceCase', caption: $scope.masterDetailHeader.invoiceCaseNum, allowHeaderFiltering: false },
                          {
                              dataField: 'gdIssuedDate', caption: $scope.masterDetailHeader.issuedDate,
                              dataType: 'date', format: dateFormat, allowHeaderFiltering: false
                          },
                          { dataField: 'buyerOrganizationName', caption: $scope.masterDetailHeader.buyer, allowHeaderFiltering: false },
                           {
                               dataField: 'amount', caption: $scope.masterDetailHeader.amount,
                               format: { type: 'fixedPoint', precision: 2 }, allowHeaderFiltering: false
                           },
                          {
                              dataField: 'taxAmount', caption: $scope.masterDetailHeader.taxAmount,
                              format: { type: 'fixedPoint', precision: 2 }, allowHeaderFiltering: false
                          },
                           {
                               dataField: 'totalAmount', caption: $scope.masterDetailHeader.totalAmount,
                               format: { type: 'fixedPoint', precision: 2 }, allowHeaderFiltering: false,
                               calculateDisplayValue: function (rowData) {
                                   var r = 0;
                                   if (_.isNumber(rowData.amount)) {
                                       r += rowData.amount;
                                   }
                                   if (_.isNumber(rowData.taxAmount)) {
                                       r += rowData.taxAmount;
                                   }
                                   return r;
                               }
                           },
                            {
                                dataField: 'gdBdStatus', caption: $scope.masterDetailHeader.status,
                                calculateDisplayValue: function (rowData) {
                                    //fix 12875
                                    //if ($scope.invoiceStatus === enums.GDInvoiceStatusEnum.cancelled) {
                                    //    return $translate.instant(revertStatusList[enums.GDBDStatusEnum.cancelled]);
                                    //}
                                    return $translate.instant(revertStatusList[rowData.gdBdStatus]);
                                }
                            },
                            {
                                caption: $translate.instant('Details'),
                                cellTemplate: 'detailTemplate', allowHeaderFiltering: false
                            }

                        ]

                    };

                }
            }
        };

        $scope.eventService = {
            //隐藏或者展示搜索框
            showOrHideSearchBox: function () {
                $scope.hasShowMoreSearchBox = !$scope.hasShowMoreSearchBox;
                // $scope.eventService.refreshInvoiceDataGrid();
                $timeout(function () {
                    $scope.setInvoiceGridHeight();
                }, 500);
            },
            refreshInvoiceDataGrid: function () {
                var dataDto = {
                    parameters: $scope.gdVinSearchEntity,
                    pagingInfo: $scope.pagingOptions
                }
                vatOutputInvoiceManageService.getGDDetailByVin(dataDto).success(function (response) {
                    $scope.vinList = response.list;
                    $scope.pagingOptions.totalItems = response.pageInfo.totalCount;
                }).error(function () {
                    SweetAlert.error('', $translate.instant('CommonFail'));
                });
            },
            checkBeforCancel: function () {
                var list = $scope.gdVinDetailVariables.selectedTableRows;
                //过月的不能cancel
                var currentMonth = new Date().getMonth();//1-12
                var notValidMonthCount = 0;
                var notValidStatus = true;
                var isRed = false;
                _.each(list, function (item) {
                    var m = new Date(item.gdIssuedDate).getMonth();
                    notValidMonthCount = m === currentMonth ? notValidMonthCount : ++notValidMonthCount;
                    if (item.gdStatus !== enums.GDInvoiceStatusEnum.issued) {
                        notValidStatus = false;
                    }
                    if (item.isRedInvoice) {
                        //红票不能cancel
                        isRed = true;
                    }

                });
                var result = {
                    pass: notValidMonthCount === 0 && notValidStatus && !isRed,
                    notMatchMonth: notValidMonthCount,
                    notValidStatus: notValidStatus,
                    isRedInvoice: isRed
                }
                return result;
            },
            cancelInvoiceCore: function () {
                //只用了ID
                var gdIDs = _.map($scope.gdVinDetailVariables.selectedTableRows, function (item) {
                    return { id: item.gdInvoiceID };
                });
                var dto = {
                    items: gdIDs,
                    reprint: $scope.isReprint,
                    userName: null,
                    userID: null
                }
                //need call baiwang jie kou
                vatOutputInvoiceManageService.cancelGDInvoice(dto).success(function (response) {
                    if (response.result) {
                        //_.each($scope.gdVinDetailVariables.selectedTableRows, function (item) {
                        //    item.status = enums.GDInvoiceStatusEnum.cancelled;
                        //});
                        modalAdapterService.close(constant.OutputInvoiceCancalDialog, 'gd-vin-view');
                        $scope.eventService.refreshInvoiceDataGrid();
                    }
                }).error(function () {
                    SweetAlert.error('', $translate.instant('CommonFail'));
                });
            },
            cancelFapiao: function (reprint) {
                if (!$scope.gdVinDetailVariables.selectedTableRows || $scope.gdVinDetailVariables.selectedTableRows.length < 1) {
                    SweetAlert.warning($translate.instant('SelectBdInvoiceCheck'));
                    return;
                }
                var result = $scope.eventService.checkBeforCancel();
                if (result.pass) {
                    $scope.title = reprint ? $translate.instant("CancelReprintInvoiceGDTitle") : $translate.instant("CancelInvoiceGDTitle")
                    $scope.isReprint = reprint ? reprint : false;
                    modalAdapterService.open(constant.OutputInvoiceCancalDialog, 'gd-vin-view');
                } else {
                    var message = result.notMatchMonth === 0 ? "" : $translate.instant('HasNotMatchMonth') + result.notMatchMonth + $translate.instant('Separator');
                    message = result.notValidStatus ? message : message + $translate.instant('HasNotMatchStatus');
                    message = result.isRedInvoice ? message + $translate.instant('RedInvoiceWarrning') : message;
                    SweetAlert.warning('', message);
                }

            },
            cancelandPrintFapiao: function () {
                $scope.eventService.cancelFapiao(true);
            },
            showDetail: function (rowData) {
                $scope.gdVinDetailVariables.selectedGDInvoice = rowData;
                $scope.$broadcast("openGDDetailDialog");
            },
            redLetterCheck: function () {
                var pass = true;
                var message = "";
                //已经red letter的不能操作
                var limitedRecords = _.where($scope.gdVinDetailVariables.selectedTableRows, { gdBdStatus: enums.GDBDStatusEnum.redLettered });
                if (limitedRecords && limitedRecords.length > 0) {
                    pass = false;
                    message += $translate.instant('HasRedVehicleWarrning');
                }
                limitedRecords = _.where($scope.gdVinDetailVariables.selectedTableRows, { isRedInvoice: true });
                if (limitedRecords && limitedRecords.length > 0) {
                    pass = false;
                    message += $translate.instant('RedInvoiceWarrning');
                }
                limitedRecords = _.where($scope.gdVinDetailVariables.selectedTableRows, { gdStatus: enums.GDInvoiceStatusEnum.redLettered });
                if (limitedRecords && limitedRecords.length > 0) {
                    pass = false;
                    message += $translate.instant('HasRedInvoiceWarrning');
                }
                limitedRecords = _.where($scope.gdVinDetailVariables.selectedTableRows, { gdStatus: enums.GDInvoiceStatusEnum.cancelled });
                if (limitedRecords && limitedRecords.length > 0) {
                    pass = false;
                    message += $translate.instant('HasCancelInvoiceWarrning');
                }

                //红冲-除去当月往前退180,如果开票日期在这里面的就可以红冲
                var currentDate = new Date();
                var currentMonth = currentDate.getMonth();
                var endDate = new Date(currentDate.getFullYear(), currentMonth, 1);
                var one_day = 1000 * 60 * 60 * 24;
                var startDate = new Date(endDate.getTime() - one_day * 180);
               
                limitedRecords = _.filter($scope.gdVinDetailVariables.selectedTableRows, function (item) {
                    var iDate = new Date(item.gdIssuedDate);
                    return iDate < startDate || iDate > endDate
                });
                if (limitedRecords && limitedRecords.length > 0) {
                    pass = false;
                    message += $translate.instant('HasUnmatchRedTimeLimit');
                }
                return {
                    pass: pass,
                    msg: message
                }
            },
            confirmRedLetter: function () {
                if (!$scope.gdVinDetailVariables.selectedTableRows || $scope.gdVinDetailVariables.selectedTableRows.length < 1) {
                    SweetAlert.warning($translate.instant('SelectBdInvoiceCheck'));
                    return;
                }
                var result = $scope.eventService.redLetterCheck();
                if (result.pass)
                {
                    SweetAlert.swal({
                        title: $translate.instant('RedLetterConfirmTitle'),
                        text: $translate.instant('RedLetterConfirmText'),
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: $translate.instant('Confirm'),
                        cancelButtonText: $translate.instant('ENCancel'),
                        closeOnConfirm: true,
                        closeOnCancel: true
                    },
                    function (isConfirm) {
                        if (isConfirm) {
                            $scope.eventService.toRedLetter();
                        }
                    });
                }
                else
                {
                    SweetAlert.warning('', result.msg);
                }

            },
            toRedLetter: function () {
                //只用了detail id
                var detailIDs = _.pluck($scope.gdVinDetailVariables.selectedTableRows, 'detialID');
                //to connect baiwang interface and update the printed ID GB-BD status
                vatOutputInvoiceManageService.gdMultipleRedLetter(detailIDs).success(function (response) {
                    if (response.result) {
                        //refresh table
                        $scope.eventService.refreshInvoiceDataGrid();
                    } else {
                        SweetAlert.error('', $translate.instant(response.resultMsg));
                    }

                }).error(function () {
                    SweetAlert.error('', $translate.instant('CommonFail'));
                });
            },
            getQueryBasic: function () {
                vatOutputInvoiceManageService.getModelSeries().success(function (response) {
                    if (response && response.length > 0) {
                        $scope.vehicleModelSource = _.map(response, function (item) {
                            return { id: item, name: item };
                        });
                    }
                }).error(function (err) {
                    SweetAlert.error('', $translate.instant('CommonFail'));
                });
            }
        };

        (function () {
            $scope.eventService.getQueryBasic();
            $scope.eventService.refreshInvoiceDataGrid();
            $scope.hasShowMoreSearchBox = false;
            //设置表格高度
            $timeout(function () {
                $scope.setInvoiceGridHeight();
            }, 500);
           
        })();


    }]);