weicd
2023-09-28 d0c51ac702c57f2cae84a8d879a5b605f2dab630
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
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-select v-model="query.contractId" filterable clearable placeholder="所属合同" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery">
          <el-option
            v-for="item in contractList"
            :key="item.contractId"
            :label="item.contractNumber + ' - ' + item.contractName"
            :value="item.contractId"
          />
        </el-select>
        <el-select v-model="query.invoiceType" filterable clearable placeholder="发票类型" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery">
          <el-option
            v-for="item in dict[invoiceType]"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮,可以使用插槽方式, slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="630px">
        <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="所属合同" prop="contractId">
            <el-select v-model="form.contractId" filterable clearable placeholder="请选择" style="width: 495px;" @change="contractIdChange">
              <el-option
                v-for="item in contractList"
                :key="item.contractId"
                :label="item.contractNumber + ' - ' + item.contractName"
                :value="item.contractId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="开票时间" prop="billingTime">
            <el-date-picker v-model="form.billingTime" type="datetime" style="width: 200px;" />
          </el-form-item>
          <el-form-item label="发票类型" prop="invoiceType">
            <el-select v-model="form.invoiceType" filterable placeholder="请选择" style="width: 200px;">
              <el-option
                v-for="item in dict[invoiceType]"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="税前金额" prop="beforeTaxAmount">
            <money-input :key="random" v-model="form.beforeTaxAmount" :width="200" @change="amountOrRateChange" />
          </el-form-item>
          <el-form-item label="税率" prop="taxRate">
            <el-input v-model="form.taxRate" style="width: 200px;" @change="amountOrRateChange">
              <template slot="append">%</template>
            </el-input>
          </el-form-item>
          <el-form-item label="税额" prop="taxAmount">
            <money-input :key="randomAR" v-model="form.taxAmount" :width="200" />
          </el-form-item>
          <el-form-item label="税后金额" prop="afterTaxAmount">
            <money-input :key="randomAR" v-model="form.afterTaxAmount" :width="200" />
          </el-form-item>
          <el-form-item label="相关文件" prop="relatedDocuments">
            <file-upload :key="random" ref="fileUpload" :file-url="form.relatedDocuments == null || form.relatedDocuments === '' ? null : form.relatedDocuments" :limit="5" @fileInfo="getFileInfo" />
          </el-form-item>
          <el-form-item label="相关付款">
            <el-select v-model="form.contractPaymentIds" filterable multiple placeholder="请选择" style="width: 495px;">
              <el-option
                v-for="item in contractPaymentList"
                :key="item.paymentId"
                :label="item.paymentAmount"
                :value="item.paymentId"
              >
                <span style="float: left">付款金额:{{ item.paymentAmount }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">付款时间:{{ item.paymentTime }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注信息" prop="remarks">
            <el-input v-model="form.remarks" :rows="3" type="textarea" style="width: 495px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column :show-overflow-tooltip="true" prop="contractName" label="所属合同" />
        <el-table-column :show-overflow-tooltip="true" prop="billingTime" label="开票时间" />
        <el-table-column :show-overflow-tooltip="true" prop="invoiceType" label="发票类型">
          <template slot-scope="scope">
            {{ dict.label[invoiceType][scope.row.invoiceType] }}
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="beforeTaxAmount" label="税前金额">
          <template slot-scope="scope">
            {{ crud.toMoney(scope.row.beforeTaxAmount) }}
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="taxRate" label="税率">
          <template slot-scope="scope">
            {{ scope.row.taxRate }}%
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="taxAmount" label="税额">
          <template slot-scope="scope">
            {{ crud.toMoney(scope.row.taxAmount) }}
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="afterTaxAmount" label="税后金额">
          <template slot-scope="scope">
            {{ crud.toMoney(scope.row.afterTaxAmount) }}
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="remarks" label="备注信息" />
        <el-table-column v-if="checkPer(['admin','contractInvoice:edit','contractInvoice:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>
 
<script>
import crudContractInvoice from '@/api/contract/contractInvoice'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import moneyInput from '../../assembly/moneyInput/index.vue'
import fileUpload from '../../assembly/fileUpload/index.vue'
import constant from '../../../utils/constant'
import crudContract from '@/api/contract/contract'
import crudContractPayment from '@/api/contract/contractPayment'
 
const defaultForm = { invoiceId: null, contractId: null, billingTime: null, invoiceType: null, beforeTaxAmount: null, taxRate: null, taxAmount: null, afterTaxAmount: null, relatedDocuments: null, remarks: null, contractPaymentIds: [], createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'ContractInvoice',
  components: { pagination, crudOperation, rrOperation, udOperation, moneyInput, fileUpload },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: [constant.INVOICE_TYPE],
  cruds() {
    return CRUD({ title: '合同发票', url: 'api/contractInvoice', idField: 'invoiceId', sort: 'invoiceId,desc', crudMethod: { ...crudContractInvoice }})
  },
  data() {
    // 税前金额校验规则
    var validateBeforeTaxAmount = (rule, value, callback) => {
      if (value !== null) {
        if (isNaN(Number(value))) {
          callback(new Error('请输入数字值'))
        } else {
          if (value < 0 || value > 1000000000) {
            callback(new Error('税前金额不能超过10亿'))
          } else {
            callback()
          }
        }
      } else {
        callback(new Error('税前金额不能为空'))
      }
    }
    // 税率校验规则
    var validateTaxRate = (rule, value, callback) => {
      if (value !== null) {
        if (isNaN(Number(value))) {
          callback(new Error('请输入数字值'))
        } else {
          if (value < 0 || value > 100) {
            callback(new Error('税率不能超过100%'))
          } else {
            callback()
          }
        }
      } else {
        callback()
      }
    }
    return {
      permission: {
        add: ['admin', 'contractInvoice:add'],
        edit: ['admin', 'contractInvoice:edit'],
        del: ['admin', 'contractInvoice:del'],
        download: ['admin', 'contractInvoice:download']
      },
      rules: {
        contractId: [
          { required: true, message: '请选择所属合同', trigger: 'change' }
        ],
        billingTime: [
          { required: true, message: '请选择开票时间', trigger: 'change' }
        ],
        invoiceType: [
          { required: true, message: '请选择发票类型', trigger: 'change' }
        ],
        beforeTaxAmount: [
          { required: true, validator: validateBeforeTaxAmount, trigger: 'blur' }
        ],
        taxRate: [
          { validator: validateTaxRate, trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'contractId', display_name: '所属合同' },
        { key: 'invoiceType', display_name: '发票类型' }
      ],
      // 字典:发票类型
      invoiceType: constant.INVOICE_TYPE,
      // 随机数
      random: 0,
      randomAR: 0,
      // 合同数据
      contractList: [],
      // 付款数据
      contractPaymentList: []
    }
  },
  methods: {
    // 钩子:在获取表格数据之前执行,false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      const that = this
      // 获取合同数据
      that.getContractList()
      // 检索条件赋值
      if (that.$route.query.contractId != null && that.crud.query.contractId === undefined) {
        that.$set(that.crud.query, 'contractId', Number(that.$route.query.contractId))
      }
      return true
    },
    // 获取合同数据
    getContractList() {
      const that = this
      // 获取合同数据
      crudContract.getAll().then(res => {
        // 获取合同数据
        that.contractList = res
      })
    },
    // 钩子:开始 "新建/编辑" - 之前
    [CRUD.HOOK.beforeToCU]() {
      const that = this
      // 随机数
      that.random = Math.random()
      that.randomAR = Math.random()
    },
    // 钩子:新建 - 之前
    [CRUD.HOOK.beforeToAdd]() {
      const that = this
      // 判断检索条件
      if (that.crud.query.contractId !== undefined) {
        // 表单赋值
        that.$set(that.crud.form, 'contractId', Number(that.crud.query.contractId))
        // 所属合同变更
        that.contractIdChange()
      }
    },
    // 钩子:编辑 - 之前
    [CRUD.HOOK.beforeToEdit]() {
      const that = this
      // 获取付款数据
      that.getContractPayment()
    },
    // 所属合同变更
    contractIdChange() {
      const that = this
      // 相关付款
      that.crud.form.contractPaymentIds = []
      // 获取付款数据
      that.contractPaymentList = []
      // 判断所属合同是否为空
      if (that.crud.form.contractId != null && that.crud.form.contractId !== '') {
        // 获取付款数据
        that.getContractPayment()
      }
    },
    // 获取付款数据
    getContractPayment() {
      const that = this
      // 获取付款数据
      crudContractPayment.getAll({ contractId: that.crud.form.contractId }).then(res => {
        // 获取付款数据
        that.contractPaymentList = res
      })
    },
    // 税前金额或税率变更
    amountOrRateChange() {
      const that = this
      // 判断税前金额和税率是否为空
      if (that.crud.form.beforeTaxAmount !== null && that.crud.form.beforeTaxAmount !== '' && that.crud.form.taxRate !== null && that.crud.form.taxRate !== '') {
        // 税额
        that.crud.form.taxAmount = that.crud.form.beforeTaxAmount * (that.crud.form.taxRate / 100)
        // 税后金额
        that.crud.form.afterTaxAmount = that.crud.form.beforeTaxAmount + that.crud.form.beforeTaxAmount * (that.crud.form.taxRate / 100)
        // 随机数
        that.randomAR = Math.random()
      }
    },
    // 表单提交
    submitCU() {
      const that = this
      // 通过“that.$refs.fileUpload”找到子页面,“submit”是子页面的方法。
      that.$refs.fileUpload.submit()
    },
    // 获取文件信息
    getFileInfo(info) {
      const that = this
      // 判断税率是否为空
      if (that.crud.form.taxRate == null || that.crud.form.taxRate === '') {
        // 税率
        that.crud.form.taxRate = 0
      }
      // 判断税额是否为空
      if (that.crud.form.taxAmount == null || that.crud.form.taxAmount === '') {
        // 税额
        that.crud.form.taxAmount = 0
      }
      // 判断税后金额是否为空
      if (that.crud.form.afterTaxAmount == null || that.crud.form.afterTaxAmount === '') {
        // 税后金额
        that.crud.form.afterTaxAmount = that.crud.form.beforeTaxAmount
      }
      // 判断返回数据
      if (info.length === 0) {
        // 表单文件路径赋值
        that.crud.form.relatedDocuments = null
        // 表单提交
        that.crud.submitCU()
        // 终止
        return
      }
      // 循环返回数据
      for (let i = 0; i < info.length; i++) {
        // 判断循环下标
        if (i === 0) {
          // 表单文件路径赋值
          that.crud.form.relatedDocuments = info[i].response == null || info[i].response === '' ? info[i].url.substring(info[i].url.indexOf('/file/'), info[i].url.length) : '/file/' + info[i].response.type + '/' + info[i].response.realName
        } else {
          // 表单文件路径赋值
          that.crud.form.relatedDocuments += info[i].response == null || info[i].response === '' ? ',' + info[i].url.substring(info[i].url.indexOf('/file/'), info[i].url.length) : ',/file/' + info[i].response.type + '/' + info[i].response.realName
        }
        // 判断循环是否结束
        if (i === info.length - 1) {
          // 表单提交
          that.crud.submitCU()
        }
      }
    }
  }
}
</script>
 
<style scoped>
 
</style>