GD-BD-invoice-detail.ctrl.js 11 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
invoiceModule.controller('invoiceDetailVehicleController', ['$log', '$scope', '$translate', '$document', '$uibModal', '$timeout',
    'vatOutputInvoiceManageService', 'SweetAlert', 'enums',
    function ($log, $scope, $translate, $document, $uibModal, $timeout, vatOutputInvoiceManageService, SweetAlert, enums) {
        var revertStatusList = _.invert(enums.GDBDStatusEnum);
        $scope.redLettered = enums.GDBDStatusEnum.redLettered;
        $scope.hasUpdate = false;
        $scope.detailDialog = {
            modalInstance: null,
            open: function () {
                var parentElem = $scope.parentElement ?
                            angular.element($document[0].querySelector('.' + $scope.parentElement)) : undefined;
                $scope.detailDialog.modalInstance = $uibModal.open({
                    animation: false,
                    backdrop: false,
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    templateUrl: 'myModalContent.html',
                    windowClass: 'GD-Vechicle-detail',
                    scope: $scope,
                    appendTo: parentElem
                });


            },
            cancel: function () {
                $scope.detailDialog.modalInstance.dismiss('cancel');
                if ($scope.hasUpdate&&_.isFunction($scope.parentRefresh)) {
                    $scope.parentRefresh();
                }
                $scope.hasUpdate = false;
            }
        };
        $scope.vehicleDetailOptions = {
            bindingOptions: {
                dataSource: 'invoiceDetail.details'
            },
            width: '99%',
            noDataText: $translate.instant('NoData'),
            columnAutoWidth: true,
            showBorders: true,
            showRowLines: true,
            columns: [
                { dataField: 'vin', caption: $translate.instant('VIN') },
                { dataField: 'vehicleModel', caption: $translate.instant('VehicleModel') },
                { dataField: 'quantity', caption: $translate.instant('Quantity'), alignment: 'left' },
                {
                    dataField: 'price', caption: $translate.instant('UnitPrice'),
                    alignment: 'right', format: { type: 'fixedPoint', precision: 2 }
                },
                {
                    dataField: 'amount', caption: $translate.instant('Amount'),
                    alignment: 'right', format: { type: 'fixedPoint', precision: 2 }
                },
                {
                    dataField: 'taxRate', caption: $translate.instant('TaxRate'),
                    alignment: 'right',
                    calculateDisplayValue: function (rowData) {
                        // return rowData.taxRate.formatRate(0);
                        var num = (rowData.taxRate * 100).formatAmount(0);
                        return num + "%";
                    }
                },
                {
                    dataField: 'taxAmount', caption: $translate.instant('TaxAmount'),
                    alignment: 'right', format: { type: 'fixedPoint', precision: 2 }
                },
                {
                    dataField: 'gdBdStatus', caption: $translate.instant('Status'),
                    calculateDisplayValue: function (rowData) {
                        //fix 12875
                        //if ($scope.invoiceStatus === enums.GDInvoiceStatusEnum.cancelled) {
                        //    return $translate.instant(revertStatusList[enums.GDBDStatusEnum.cancelled]);
                        //}
                        return $translate.instant(revertStatusList[rowData.status]);
                    }
                },
                {
                    caption: $translate.instant('Action'), width: '120px',
                    cellTemplate: 'redLetterTemplate'
                }

            ],
            onRowPrepared: function (e) {
                if (e.rowType === 'totalFooter') {
                    var instance = e.component;
                    var grandTotal = instance.getTotalSummaryValue('totalAmount') + instance.getTotalSummaryValue('totalTaxAmount');
                    if (_.isNumber(grandTotal)) {
                        grandTotal = grandTotal.formatAmount(2);
                    }
                    var grandStr = $translate.instant('GrandTotal');
                    var trStr = "<tr class='dx-row' role='row'>"
                        + '<td role="gridcell" aria-label="Column VIN, Value undefined" style="text-align: left;">' 
                        + '<div class="dx-datagrid-summary-item dx-datagrid-text-content" >'
                        + grandStr +'&nbsp;'+ grandTotal+'</div></td>'
                        + '<td role="gridcell" aria-label="Column Vehicle Model, Value undefined" style="text-align: left;"></td>'
                        + '<td role="gridcell" aria-label="Column 数量, Value undefined" style="text-align: left;"></td>'
                        + '<td role="gridcell" aria-label="Column Price, Value undefined" style="text-align: right;"></td>'
                        + '<td role="gridcell" aria-label="Column Amount, Value undefined" style="text-align: right;"></td>'
                        + '<td role="gridcell" aria-label="Column 税率, Value undefined" style="text-align: left;"></td>'
                        + '<td role="gridcell" aria-label="Column 税额, Value undefined" style="text-align: left;"></td>'
                        + '<td role="gridcell" aria-label="Column Status, Value undefined" style="text-align: left;"></td>'
                        + '<td role="gridcell" aria-label="Column , Value undefined" style="text-align: left;"></td>'
                       + "</tr>"
                    e.rowElement.after(trStr);
                }
            },
            summary: {
                totalItems: [
                     {
                         column: 'price',
                         customizeText: function (item) {
                             return $translate.instant('TotalPrice');
                         }
                     },
                    {
                        name: 'totalAmount',
                        column: 'amount',
                        summaryType: 'sum',
                        customizeText: function (data) {
                            return data.value.formatAmount(2);
                        }
                    },
                    {
                        name: 'totalTaxAmount',
                        column: 'taxAmount',
                        summaryType: 'sum',
                        customizeText: function (data) {
                            return data.value.formatAmount(2);
                        }
                    }
                ]
            }

        };


        $scope.eventService = {            
            LoadData: function () {
                if ($scope.detailKey) {
                    vatOutputInvoiceManageService.getGBInvoiceDetail($scope.detailKey).success(function (response) {
                        if (response.result) {
                            $scope.invoiceDetail = response.data;
                           
                            $scope.needHide = $scope.hideAction;
                          
                            //invoiceStatus
                            //GD发票是红冲或者取消状态,就不能操作
                            //或者车辆本身已经红冲过的就不能操作
                            if ($scope.invoiceStatus) {
                                //has value
                                $scope.needHide = $scope.needHide||$scope.invoiceDetail.isRedInvoice || $scope.invoiceStatus === enums.GDInvoiceStatusEnum.cancelled ||
                                    $scope.invoiceStatus === enums.GDInvoiceStatusEnum.redLettered;
                                
                            }
                            //红冲-除去当月往前退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);
                            var iDate = new Date($scope.invoiceDetail.gdIssuedDate);
                            $scope.needHide = $scope.needHide ||iDate < startDate|| iDate > endDate
                          
                            $scope.detailDialog.open();
                        } else {
                            SweetAlert.error('', $translate.instant(response.resultMsg));
                        }

                    }).error(function () {
                        SweetAlert.error('', $translate.instant('CommonFail'));
                    });
                }

            },
            confirmRedLetter: function (rowData) {
                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(rowData);
                    }
                });
            },
            toRedLetter: function (rowData) {
                var id = rowData.detailID;               
                //to connect baiwang interface and update the printed ID GB-BD status
                vatOutputInvoiceManageService.gdRedLetter(id).success(function (response) {
                    if (response&&response.result) {
                        //返回的是GD-BD发票的状态
                        $scope.invoiceStatus = response.resultMsg;
                        rowData.status = $scope.redLettered;
                        $scope.needHide = $scope.invoiceStatus === enums.GDInvoiceStatusEnum.cancelled ||
                                  $scope.invoiceStatus === enums.GDInvoiceStatusEnum.redLettered;
                        
                        $('#vehicleDetail').dxDataGrid('instance').refresh();
                        $scope.hasUpdate = true;

                    } else {
                        var msg = response ? response.resultMsg : "NotFindItem";
                        SweetAlert.error('', $translate.instant(msg));
                    }

                }).error(function () {
                    SweetAlert.error('', $translate.instant('CommonFail'));
                });
            },
            loadDataAfterReady: function () {
                $timeout(function () {
                    $scope.eventService.LoadData();
                }, 600);
            }

        };

    }
])