<template>
|
<view class="account-detail">
|
<!-- 使用通用页面头部组件 -->
|
<PageHeader title="编辑来票台账"
|
@back="goBack" />
|
<up-form @submit="submitForm"
|
ref="formRef"
|
label-width="120"
|
:model="form">
|
<up-form-item label="采购合同号"
|
prop="purchaseContractNumber">
|
<up-input v-model="form.purchaseContractNumber"
|
placeholder="自动生成"
|
disabled />
|
</up-form-item>
|
<up-form-item label="销售合同号"
|
prop="salesContractNo">
|
<up-input v-model="form.salesContractNo"
|
placeholder="自动生成"
|
disabled />
|
</up-form-item>
|
<up-form-item label="含税单价(元)"
|
prop="taxInclusiveUnitPrice">
|
<up-input v-model="form.taxInclusiveUnitPrice"
|
placeholder="自动生成"
|
disabled />
|
</up-form-item>
|
<up-form-item label="创建时间"
|
prop="createdAt">
|
<up-input v-model="form.createdAt"
|
placeholder="自动生成"
|
disabled />
|
</up-form-item>
|
<up-form-item label="发票号"
|
prop="invoiceNumber">
|
<up-input v-model="form.invoiceNumber"
|
placeholder="请输入"
|
disabled />
|
</up-form-item>
|
<up-form-item label="来票数"
|
prop="ticketsNum"
|
required
|
:rules="rules.ticketsNum">
|
<up-input v-model="form.ticketsNum"
|
type="number"
|
placeholder="请输入"
|
@blur="inputTicketsNum" />
|
</up-form-item>
|
<up-form-item label="本次来票金额(元)"
|
prop="ticketsAmount"
|
required
|
:rules="rules.ticketsAmount">
|
<up-input v-model="form.ticketsAmount"
|
type="number"
|
placeholder="请输入"
|
@blur="inputTicketsAmount" />
|
</up-form-item>
|
<view class="tip-text">未来票数:{{ formatAmount(form.futureTickets) }} 元</view>
|
<!-- 使用公共底部按钮组件 -->
|
<FooterButtons show
|
cancelText="取消"
|
confirmText="保存"
|
@cancel="goBack"
|
@confirm="onSubmit" />
|
</up-form>
|
</view>
|
</template>
|
|
<script setup>
|
import { ref, onMounted } from "vue";
|
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";
|
import PageHeader from "@/components/PageHeader.vue";
|
import FooterButtons from "@/components/FooterButtons.vue";
|
|
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 rules = {
|
ticketsNum: [{ required: true, message: "请输入来票数", trigger: "blur" }],
|
ticketsAmount: [
|
{ required: true, message: "请输入本次来票金额", trigger: "blur" },
|
],
|
};
|
|
const goBack = () => {
|
uni.removeStorageSync("invoiceLedgerEditRow");
|
uni.navigateBack();
|
};
|
const inputTicketsNum = () => {
|
// 确保含税单价存在且不为零
|
if (
|
!form.value.taxInclusiveUnitPrice ||
|
Number(form.value.taxInclusiveUnitPrice) === 0
|
) {
|
uni.showToast({
|
title: "含税单价不能为零或未定义",
|
icon: "none",
|
});
|
return;
|
}
|
if (Number(form.value.ticketsNum) > Number(temFutureTickets.value)) {
|
uni.showToast({
|
title: "来票数不得大于未来票数",
|
icon: "none",
|
});
|
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 = () => {
|
// 确保含税单价存在且不为零
|
if (
|
!form.value.taxInclusiveUnitPrice ||
|
Number(form.value.taxInclusiveUnitPrice) === 0
|
) {
|
uni.showToast({
|
title: "含税单价不能为零或未定义",
|
icon: "none",
|
});
|
return;
|
}
|
|
if (
|
Number(form.value.ticketsAmount) >
|
Number(form.value.futureTickets * form.value.taxInclusiveUnitPrice)
|
) {
|
uni.showToast({
|
title: "本次来票金额不得大于总金额",
|
icon: "none",
|
});
|
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(form.value.ticketsAmount) / 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);
|
};
|
|
const loadDetail = async (id, purchaseLedgerId, productModelId) => {
|
try {
|
uni.showLoading({
|
title: "加载中...",
|
});
|
const res = await getProductRecordById({
|
id: id,
|
purchaseLedgerId: purchaseLedgerId,
|
productModelId: productModelId,
|
});
|
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");
|
}
|
uni.hideLoading();
|
} catch (e) {
|
uni.hideLoading();
|
uni.showToast({
|
title: "加载失败",
|
icon: "none",
|
});
|
}
|
};
|
|
const submitForm = async () => {
|
try {
|
// 提交表单的具体逻辑
|
await updateRegistration(form.value);
|
uni.showToast({
|
title: "提交成功",
|
icon: "success",
|
});
|
setTimeout(() => {
|
goBack();
|
}, 800);
|
} catch (e) {
|
uni.showToast({
|
title: "提交失败,请重试",
|
icon: "none",
|
});
|
}
|
};
|
|
// 表单提交
|
const onSubmit = () => {
|
formRef.value
|
.validate()
|
.then(() => {
|
// 表单验证通过,提交表单
|
submitForm();
|
})
|
.catch(error => {
|
// 表单验证失败
|
console.log("表单验证失败", error);
|
});
|
};
|
const purchaseLedgerId = ref("");
|
const productModelId = ref({});
|
|
onMounted(() => {
|
const rowStr = uni.getStorageSync("invoiceLedgerEditRow");
|
if (rowStr) {
|
try {
|
const row = JSON.parse(rowStr);
|
currentId.value = row.id;
|
purchaseLedgerId.value = row.purchaseLedgerId;
|
productModelId.value = row.productModelId;
|
loadDetail(currentId.value, purchaseLedgerId.value, productModelId.value);
|
} catch (e) {
|
// ignore
|
}
|
}
|
});
|
</script>
|
|
<style scoped lang="scss">
|
@import "@/static/scss/form-common.scss";
|
</style>
|