gaoluyang
15 小时以前 85b3cc4cb996040632aa656c79b5047bd8b2597c
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
<template>
    <view class="account-detail">
        <!-- 使用通用页面头部组件 -->
        <PageHeader title="编辑来票台账" @back="goBack" />
 
        <van-form @submit="submitForm" ref="formRef" label-width="120px" input-align="right" error-message-align="right" scroll-to-error scroll-to-error-position="center">
            <van-cell-group title="基本信息" inset>
                <van-field v-model="form.purchaseContractNumber" label="采购合同号" readonly />
                <van-field v-model="form.salesContractNo" label="销售合同号" readonly />
                <van-field v-model="form.taxInclusiveUnitPrice" label="含税单价(元)" readonly />
                <van-field v-model="form.createdAt" label="创建时间" readonly />
                <van-field v-model="form.invoiceNumber" label="发票号" placeholder="请输入" readonly />
                <van-field v-model="form.ticketsNum" label="来票数" type="number" placeholder="请输入" required :rules="[{ required: true, message: '请输入来票数' }]" @change="inputTicketsNum"/>
                <van-field v-model="form.ticketsAmount" label="本次来票金额(元)" type="number" placeholder="请输入" required :rules="[{ required: true, message: '请输入本次来票金额' }]" @change="inputTicketsAmount"/>
                <view class="tip-text">未来票数:{{ formatAmount(form.futureTickets) }} 元</view>
<!--                <van-field v-model="form.invoicePerson" label="未来票数" readonly />-->
            </van-cell-group>
 
<!--            <van-cell-group title="附件材料(仅支持 pdf)" inset>-->
<!--                <van-uploader-->
<!--                    accept=".pdf"-->
<!--                    multiple-->
<!--                    :after-read="afterReadUpload"-->
<!--                    :before-read="beforeReadPdf"-->
<!--                >-->
<!--                    <van-button class="upload-btn" icon="plus" type="primary" block>上传文件</van-button>-->
<!--                </van-uploader>-->
<!--                <view class="uploaded-list" v-if="fileList.length">-->
<!--                    <view class="uploaded-item" v-for="(f, idx) in fileList" :key="idx">-->
<!--                        <text class="file-name">{{ f.name || getFileNameFromUrl(f.url) }}</text>-->
<!--                        <van-button size="mini" type="danger" plain @click="removeUploaded(idx)">移除</van-button>-->
<!--                    </view>-->
<!--                </view>-->
<!--            </van-cell-group>-->
 
            <view class="footer-btns">
                <van-button class="cancel-btn" @click="goBack">取消</van-button>
                <van-button class="save-btn" native-type="submit" form-type="submit">保存</van-button>
            </view>
        </van-form>
    </view>
</template>
 
<script setup>
import { ref, onMounted } from 'vue'
import { showToast, showLoadingToast, closeToast } from 'vant'
import dayjs from 'dayjs'
import useUserStore from '@/store/modules/user'
import { getToken } from '@/utils/auth'
import { invoiceLedgerSaveOrUpdate } from '@/api/salesManagement/invoiceLedger.js'
import config from '@/config.js'
import {getProductRecordById, updateRegistration} from "@/api/procurementManagement/procurementInvoiceLedger";
 
const userStore = useUserStore()
 
const formRef = ref()
let form = ref({
    salesLedgerId: '',
    customerId: '',
    invoiceNo: '',
    invoiceTotal: '',
    taxRate: '',
    invoicePerson: '',
    invoiceDate: '',
    customerName: '',
    fileList: [],
    createTime: '',
    taxInclusiveTotalPrice: '',
    taxInclusiveUnitPrice: ''
})
const fileList = ref([])
const currentId = ref('')
const temFutureTickets = ref(0)
 
// 日期选择
const currentInvoiceDate = ref([new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate()])
 
const goBack = () => {
    uni.removeStorageSync('invoiceLedgerEditRow');
    uni.navigateBack()
}
const inputTicketsNum = (val) => {
    // 确保含税单价存在且不为零
    if (!form.value.taxInclusiveUnitPrice || Number(form.value.taxInclusiveUnitPrice) === 0) {
        showToast("含税单价不能为零或未定义");
        return;
    }
    if (Number(form.value.ticketsNum) > Number(temFutureTickets.value)) {
        showToast("来票数不得大于未来票数");
        form.value.ticketsNum = temFutureTickets.value
    }
    
    // 确保所有数值都转换为数字类型进行计算
    const ticketsAmount = Number(form.value.ticketsNum) * Number(form.value.taxInclusiveUnitPrice);
    const futureTickets = Number(temFutureTickets.value) - Number(form.value.ticketsNum);
    form.value.futureTickets = Number(futureTickets.toFixed(2));
    form.value.ticketsAmount = Number(ticketsAmount.toFixed(2));
};
const inputTicketsAmount = (val) => {
    // 确保含税单价存在且不为零
    if (!form.value.taxInclusiveUnitPrice || Number(form.value.taxInclusiveUnitPrice) === 0) {
        showToast("含税单价不能为零或未定义");
        return;
    }
    
    if (Number(val) > Number(form.value.futureTickets*form.value.taxInclusiveUnitPrice)) {
        showToast("本次来票金额不得大于总金额");
        form.value.ticketsAmount = (form.value.futureTickets*form.value.taxInclusiveUnitPrice).toFixed(2)
        const ticketsNum = Number(form.value.ticketsAmount) / Number(form.value.taxInclusiveUnitPrice);
        form.value.ticketsNum = Number(ticketsNum.toFixed(2))
        return;
    }
    
    // 确保所有数值都转换为数字类型进行计算
    const ticketsNum = Number(val) / Number(form.value.taxInclusiveUnitPrice);
    form.value.ticketsNum = Number(ticketsNum.toFixed(2));
};
const formatAmount = (val) => {
    if (val === undefined || val === null || val === '') return '0.00'
    const num = Number(val)
    if (Number.isNaN(num)) return '0.00'
    return num.toFixed(2)
}
 
// 上传前校验(兼容 Vant Uploader 的 file/fileList 结构)
const beforeReadPdf = (file) => {
    const items = Array.isArray(file) ? file : [file]
    for (const it of items) {
        const raw = it?.file || it
        const fileName = raw?.name || it?.name || ''
        const ext = fileName.split('.').pop()?.toLowerCase()
        const sizeOk = (raw?.size || 0) <= 10 * 1024 * 1024
        if (ext !== 'pdf') {
            showToast('仅支持pdf文件')
            return false
        }
        if (!sizeOk) {
            showToast('上传文件大小不能超过10MB')
            return false
        }
    }
    return true
}
 
const uploadSingleFile = async (fileObj) => {
    return new Promise((resolve, reject) => {
        showLoadingToast({ message: '正在上传...' })
        const baseUrl = config.baseUrl + '/invoiceLedger/uploadFile'
 
        const filePath = fileObj?.url || fileObj?.tempFilePath || fileObj?.file?.path
        if (filePath) {
            uni.uploadFile({
                url: baseUrl,
                filePath,
                name: 'file',
                header: { Authorization: 'Bearer ' + getToken() },
                success: (res) => {
                    closeToast()
                    try {
                        const data = JSON.parse(res.data || '{}')
                        if (data.code === 200) {
                            resolve(data.data)
                        } else {
                            reject(new Error(data.msg || '上传失败'))
                        }
                    } catch (err) {
                        reject(err)
                    }
                },
                fail: (err) => {
                    closeToast()
                    reject(err)
                }
            })
            return
        }
 
        // H5: 使用原始 File(input 选择)
        const rawFile = fileObj?.file
        if (rawFile) {
            // uni.uploadFile 在 H5 不支持原生 File 对象,这里用 fetch 发送 FormData
            const formData = new FormData()
            formData.append('file', rawFile, rawFile.name || 'file.pdf')
            formData.append('salesLedgerId', form.value.salesLedgerId || currentId.value || '')
            fetch(baseUrl, {
                method: 'POST',
                headers: { Authorization: 'Bearer ' + getToken() },
                body: formData
            }).then(async (res) => {
                closeToast()
                const data = await res.json()
                if (data.code === 200) {
                    resolve(data.data)
                } else {
                    reject(new Error(data.msg || '上传失败'))
                }
            }).catch((err) => {
                closeToast()
                reject(err)
            })
            return
        }
 
        closeToast()
        reject(new Error('未找到可上传的文件'))
    })
}
 
const afterReadUpload = async (file) => {
    try {
        const files = Array.isArray(file) ? file : file?.file ? [file] : [file]
        for (const f of files) {
            const uploaded = await uploadSingleFile(f)
            fileList.value.push(uploaded)
        }
        showToast('上传成功')
    } catch (e) {
        showToast('上传失败')
    }
}
 
const removeUploaded = (index) => {
    fileList.value.splice(index, 1)
}
 
const getFileNameFromUrl = (url) => {
    try { if (!url) return ''; return decodeURIComponent(url.split('/').pop()) } catch (e) { return url }
}
 
const loadDetail = async (id) => {
    try {
        showLoadingToast({ message: '加载中...' })
        const res = await getProductRecordById({ id })
        const data = res?.data || res
        form.value = { ...data }
        temFutureTickets.value = data.futureTickets;
        fileList.value = data?.fileList || []
        if (!form.value.invoicePerson) {
            form.value.invoicePerson = userStore.nickName
        }
        if (!form.value.invoiceDate) {
            form.value.invoiceDate = dayjs().format('YYYY-MM-DD')
        }
        closeToast()
    } catch (e) {
        closeToast()
        showToast('加载失败')
    }
}
 
const submitForm = async () => {
    try {
        showLoadingToast({ message: '提交中...' })
        await updateRegistration(form.value)
        closeToast()
        showToast('提交成功')
        setTimeout(() => { goBack() }, 800)
    } catch (e) {
        closeToast()
        showToast('提交失败,请重试')
    }
}
 
onMounted(() => {
    const rowStr = uni.getStorageSync('invoiceLedgerEditRow')
    if (rowStr) {
        try {
            const row = JSON.parse(rowStr)
            currentId.value = row.id
            loadDetail(currentId.value)
        } catch (e) {
            // ignore
        }
    }
})
</script>
 
<style scoped lang="scss">
.account-detail {
    min-height: 100vh;
    background: #f8f9fa;
    padding-bottom: 5rem;
}
.uploaded-list { padding: 8px 16px 0 16px; }
.uploaded-item { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid #f5f5f5; }
.file-name { font-size: 12px; color: #333; margin-right: 8px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tip-text { padding: 4px 16px 0 16px; font-size: 12px; color: #888; }
.footer-btns { position: fixed; left: 0; right: 0; bottom: 0; background: #fff; display: flex; justify-content: space-around; align-items: center; padding: 0.75rem 0; box-shadow: 0 -0.125rem 0.5rem rgba(0,0,0,0.05); z-index: 1000; }
.cancel-btn { font-weight: 400; font-size: 1rem; color: #FFFFFF; width: 6.375rem; background: #C7C9CC; box-shadow: 0 0.25rem 0.625rem 0 rgba(3,88,185,0.2); border-radius: 2.5rem 2.5rem 2.5rem 2.5rem; }
.save-btn { font-weight: 400; font-size: 1rem; color: #FFFFFF; width: 14rem; background: linear-gradient( 140deg, #00BAFF 0%, #006CFB 100%); box-shadow: 0 0.25rem 0.625rem 0 rgba(3,88,185,0.2); border-radius: 2.5rem 2.5rem 2.5rem 2.5rem; }
</style>