432 lines
22 KiB
JavaScript
432 lines
22 KiB
JavaScript
'use strict';
|
|
const dbName = require("../../../../dao/config");
|
|
const dayjs = require("dayjs");
|
|
var isBetween = require('dayjs/plugin/isBetween')
|
|
dayjs.extend(isBetween)
|
|
|
|
const PROCESS_PAY_METHOD = ['预付', '货到验收后付款', '提货付全款', '信誉采购,货到签单后账期:']
|
|
const WRITE_INVOICE_TIME = ['付款当月(限20号之前付款,20号之后付款次月开票)', '付款后次月', '付款前']
|
|
const INVOICE_TYPE = ['增值税专用发票13%', '增值税普通发票', '普通发票']
|
|
|
|
module.exports = {
|
|
/**
|
|
* 新增合同
|
|
* @url client/contract/kh/add 前端调用的url参数地址
|
|
* data 请求参数
|
|
* @param {String} params1 参数1
|
|
*/
|
|
main: async (event) => {
|
|
let {data = {}, userInfo, util, filterResponse, originalParam} = event;
|
|
let {customUtil, uniID, config, pubFun, vk, db, _} = util;
|
|
let {uid} = data;
|
|
let res = {code: 0, msg: ""};
|
|
// 业务逻辑开始-----------------------------------------------------------
|
|
|
|
// await vk.baseDao.updateById({
|
|
// dbName: 'jjx-process-sheet',
|
|
// id: data.sheetId,
|
|
// dataJson: {
|
|
// status: 1
|
|
// }
|
|
// })
|
|
const quote = await vk.baseDao.updateAndReturn({
|
|
dbName: 'jjx-process-quote',
|
|
whereJson: {
|
|
_id: data.quoteId
|
|
},
|
|
dataJson: {
|
|
status: 1
|
|
}
|
|
})
|
|
|
|
// const quote = await vk.baseDao.findById({
|
|
// dbName: 'jjx-process-quote',
|
|
// id: data.quoteId
|
|
// })
|
|
|
|
const sheet = await vk.baseDao.updateAndReturn({
|
|
dbName: 'jjx-process-sheet',
|
|
whereJson: {
|
|
_id: quote.sheetId
|
|
},
|
|
dataJson: {
|
|
otherUid: data.seller,
|
|
status: 1
|
|
}
|
|
})
|
|
|
|
const checkMessage = await vk.baseDao.findByWhereJson({
|
|
dbName: "jjx-message-config",
|
|
whereJson: {
|
|
process: true,
|
|
uid: quote.userId
|
|
}
|
|
})
|
|
|
|
if (checkMessage && sheet) {
|
|
let send = true
|
|
if (checkMessage) {
|
|
// if (checkMessage.doNotDisturbRange && checkMessage.doNotDisturbRange[0]) {
|
|
// const date = dayjs().format("YYYY-MM-DD")
|
|
// const doNotDisturbRangeStart = dayjs(checkMessage.doNotDisturbRange[0]).format("HH:mm")
|
|
// const doNotDisturbRangeEnd = dayjs(checkMessage.doNotDisturbRange[1]).format("HH:mm")
|
|
// if (dayjs().isBetween(dayjs(`${date} ${doNotDisturbRangeStart}`), dayjs(`${date} ${doNotDisturbRangeEnd}`)))
|
|
// send = false
|
|
// }
|
|
const date = dayjs().format("YYYY-MM-DD")
|
|
if (checkMessage.doNotDisturbStart && checkMessage.doNotDisturbStart.length) {
|
|
const doNotDisturbRangeStart = dayjs(`${date} ${checkMessage.doNotDisturbStart[0]}:${checkMessage.doNotDisturbStart[1]}`).format("HH:mm")
|
|
const doNotDisturbRangeEnd = dayjs(`${date} ${checkMessage.doNotDisturbEnd[0]}:${checkMessage.doNotDisturbEnd[1]}`).format("HH:mm")
|
|
if (dayjs().isBetween(dayjs(`${date} ${doNotDisturbRangeStart}`), dayjs(`${date} ${doNotDisturbRangeEnd}`)))
|
|
send = false
|
|
}
|
|
}
|
|
if (send) {
|
|
await vk.baseDao.add({
|
|
dbName: "jjx-message",
|
|
dataJson: {
|
|
uid: quote.userId,
|
|
title: `加工:${sheet.title}被选商`,
|
|
content: `您报价的加工信息已被选商,赶紧去签合同吧`,
|
|
type: 'contract',
|
|
hasRead: false,
|
|
pk: quote.sheetId
|
|
}
|
|
});
|
|
await pubFun.sendNotifySms(quote.userId, 'SMS_474210523')
|
|
}
|
|
}
|
|
|
|
if (sheet.signMethod !== 1) {
|
|
const fadadaContractId = vk.pubfn.createOrderNo("P", 20)
|
|
let contractTransactionId = vk.pubfn.createOrderNo("C", 20)
|
|
data.transactionId = contractTransactionId
|
|
const contractId = await vk.baseDao.add({
|
|
dbName: 'jjx-contract',
|
|
dataJson: {
|
|
...data, ...{
|
|
fadadaContractId,
|
|
status: 0,
|
|
userId: uid,
|
|
changeNum: 0,
|
|
tradeMethod: sheet.tradeMethod
|
|
}
|
|
}
|
|
})
|
|
|
|
await vk.daoCenter.userContractDao.adds([
|
|
{
|
|
transactionId: vk.pubfn.createOrderNo("P", 20),
|
|
userId: uid,
|
|
role: 'buyer',
|
|
sign: false,
|
|
contractId,
|
|
},
|
|
{
|
|
transactionId: vk.pubfn.createOrderNo("P", 20),
|
|
userId: data.seller,
|
|
role: 'seller',
|
|
sign: false,
|
|
contractId,
|
|
}
|
|
])
|
|
|
|
// 标准合同
|
|
if (sheet.signMethod === 0) {
|
|
|
|
const templateId = await vk.baseDao.findById({
|
|
dbName: 'jjx-dict',
|
|
id: '80e3bed065a5560e075799d24532b14f'
|
|
})
|
|
const buyerSignKeyword = await vk.baseDao.findById({
|
|
dbName: 'jjx-dict',
|
|
id: '0b153f9a65a56a2007227cf5157133bf'
|
|
})
|
|
const sellerSignKeyword = await vk.baseDao.findById({
|
|
dbName: 'jjx-dict',
|
|
id: '09e7876865a56a320731cb244bbef818'
|
|
})
|
|
const signMaxDate = await vk.baseDao.findById({
|
|
dbName: 'jjx-dict',
|
|
id: '657d78958b0da41d311bd94c'
|
|
})
|
|
const signEndDate = vk.pubfn.getOffsetTime(new Date(), {
|
|
day: parseInt(signMaxDate.content),
|
|
mode: "after",
|
|
});
|
|
// const fieldList = await vk.baseDao.select({
|
|
// dbName: 'jjx-contract-template',
|
|
// whereJson: {
|
|
// isOn: true
|
|
// }
|
|
// })
|
|
const sellerUser = await vk.baseDao.findById({
|
|
dbName: 'uni-id-users',
|
|
id: quote.userId
|
|
})
|
|
const buyerUser = await vk.baseDao.findById({
|
|
dbName: 'uni-id-users',
|
|
id: uid
|
|
})
|
|
|
|
let tradeInfo = PROCESS_PAY_METHOD[sheet.payMethod]
|
|
if (sheet.payMethod === 0) tradeInfo += `${sheet.advancePayRate}%货到付全款`
|
|
if (sheet.payMethod === 3) tradeInfo += `${sheet.reputationPayDay}天`
|
|
tradeInfo += `,${sheet.invoice ? '含税' : '不含税'},`
|
|
if (sheet.invoiceOther) tradeInfo += `,${sheet.invoiceOther}`
|
|
else {
|
|
if (sheet.taxType && sheet.invoiceType && sheet.taxRate) tradeInfo += `,${sheet.taxType},${sheet.invoiceType},${sheet.taxRate}`
|
|
}
|
|
// tradeInfo += `,${INVOICE_TYPE[sheet.invoiceType]}${sheet.invoiceOther ? sheet.invoiceOther : '无'}`
|
|
let expressRequire = ''
|
|
expressRequire += `随货资料:${sheet.transportDocuments.join()},其他要求:${sheet.otherTransportDocuments ? sheet.otherTransportDocuments : '无'},运输限制要求:${sheet.transportLimit ? '有' : '无'}`
|
|
if (sheet.transportLimit && sheet.otherTransportRequest) expressRequire += `运输限制说明:${sheet.otherTransportRequest}`
|
|
let region = sheet.addressRegion.province.name + sheet.addressRegion.city.name + sheet.addressRegion.area.name
|
|
// if (sheet.type === 'process') {
|
|
if (buyerUser.accountType === 2 && buyerUser.company && buyerUser.company.addressRegion && buyerUser.company.addressRegion.province) {
|
|
region = buyerUser.company.addressRegion.province.name + buyerUser.company.addressRegion.city.name + buyerUser.company.addressRegion.area.name
|
|
}
|
|
if (buyerUser.accountType === 1 && buyerUser.person && buyerUser.person.addressRegion && buyerUser.person.addressRegion.province) {
|
|
region = buyerUser.person.addressRegion.province.name + buyerUser.person.addressRegion.city.name + buyerUser.person.addressRegion.area.name
|
|
}
|
|
|
|
// }
|
|
// console.log(buyerUser)
|
|
const accountType = sheet.invoice ? 'company' : 'person'
|
|
const buyerBank = await vk.baseDao.findByWhereJson({
|
|
dbName: "jjx-account",
|
|
whereJson: {
|
|
uid: buyerUser._id,
|
|
type: accountType
|
|
}
|
|
})
|
|
let buyerBankName = buyerUser.accountType === 2 ? buyerUser.company.companyBankName : buyerUser.person.personBankName
|
|
let buyerAccount = buyerUser.accountType === 2 ? buyerUser.company.companyAccount : buyerUser.person.personAccount
|
|
let buyerBankAccountName = buyerUser.accountType === 2 ? buyerUser.company.companyAccount : buyerUser.person.personAccount
|
|
let sellerBankName = sellerUser.accountType === 2 ? sellerUser.company.companyBankName : sellerUser.person.personBankName
|
|
let sellerAccount = sellerUser.accountType === 2 ? sellerUser.company.companyAccount : sellerUser.person.personAccount
|
|
let sellerBankAccountName = sellerUser.accountType === 2 ? sellerUser.company.companyAccount : sellerUser.person.personAccount
|
|
if (buyerBank) {
|
|
buyerBankName = buyerBank.bankName
|
|
buyerAccount = buyerBank.account
|
|
buyerBankAccountName = buyerBank.accountName
|
|
}
|
|
const sellerBank = await vk.baseDao.findByWhereJson({
|
|
dbName: "jjx-account",
|
|
whereJson: {
|
|
uid: sellerUser._id,
|
|
type: accountType
|
|
}
|
|
})
|
|
if (sellerBank) {
|
|
sellerBankName = sellerBank.bankName
|
|
sellerAccount = sellerBank.account
|
|
sellerBankAccountName = sellerBank.accountName
|
|
}
|
|
|
|
const parameter = {
|
|
seller: sellerUser.accountType === 2 ? sellerUser.company.companyName : sellerUser.person.personName,
|
|
buyer: buyerUser.accountType === 2 ? buyerUser.company.companyName : buyerUser.person.personName,
|
|
date: dayjs().format('YYYY年MM月DD日'),
|
|
sellerSignDate: dayjs(signEndDate).format('YYYY年MM月DD日 HH时mm分'),
|
|
buyerSignDate: dayjs(signEndDate).format('YYYY年MM月DD日 HH时mm分'),
|
|
contractNo: fadadaContractId,
|
|
region,
|
|
detail: sheet.title,
|
|
info: sheet.remark ? sheet.remark : '无',
|
|
tradeInfo,
|
|
expressRequire,
|
|
defaultDuty: '1.乙方所交产品品种、型号、规格、质量不符合规定的,如果甲方同意利用,应当按质论价;如果甲方不能利用的,应根据产品的具体情况,由乙方负责包换或包修,并承担修理、调换或退货而支付的实际费用;2.乙方不予供货违约责任为甲方的实际损失+违约金,如乙方材料符合订货标准,甲方不能退货和不收货,甲方无条件应按实际本合同订货数量支付100%货款,并支付乙方因此产生的实际费用。带有甲乙双方单位相关收货人和发货人签字的本合同的发货清单事项和内容均视为本合同的补充合同,与本合同有相同的法律效力,双方应共同遵照执行。',
|
|
solve: '1、凡因本合同引起的或与本合同有关的任何争议,均可友好协商或仲裁解决,协商或仲裁不成的,双方协商同意在合同签订地人民法院管辖地起诉,违约一方或对合同争议承担主要责任的一方,应承担对方因仲裁和起诉而产生的全部律师代理费、鉴定(检验)费、诉讼保全费、诉讼保险费、公告费、差旅费、执行费、误工費和获取证据产生费用等其他相关费用。2、双方本合同相关权利放弃均应有双方书面作出并加盖公章或授权委托人签字后生效。',
|
|
forceMajeure: '甲乙双方的任何一方由于不可抗力或突发公共事件的原因不能履行合同时,应及时向对方通报不能履行或不能完全履行的理由,以减轻可能给对方造成的损失,在取得有关机构证明以后,允许延期履行、部分履行或者不履行合同,并根据情况可部分或全部免予承担违约责任。',
|
|
other: '本合同的全部内容由双方平等共同协商一致,共同起草制定,非一方事先拟定,不属于格式条款。本合同自双方签字盖章后生效(包括电子签字、盖章),按本合同内容地址、联系方式寄送的一切文书、函件送达与否,均为视为送达有法律效力,通知途径和本合同内联系方式或地址送达文件双方均予以验证。',
|
|
buyerSupplement: sheet.supplementary ? sheet.supplementary : '无',
|
|
sellerSupplement: sheet.supplierExt ? sheet.supplierExt : (quote.remark ? quote.remark : '无'),
|
|
buyerAddress: buyerUser.accountType === 2 ? buyerUser.company.mailAddress : buyerUser.person.address,
|
|
sellerAddress: sellerUser.accountType === 2 ? sellerUser.company.mailAddress : sellerUser.person.address,
|
|
buyerLegal: buyerUser.accountType === 2 ? buyerUser.company.legalName : '',
|
|
sellerLegal: sellerUser.accountType === 2 ? sellerUser.company.legalName : '',
|
|
buyerAgent: buyerUser.accountType === 2 ? buyerUser.manager.personName : '',
|
|
sellerAgent: sellerUser.accountType === 2 ? sellerUser.manager.personName : '',
|
|
buyerMobile: buyerUser.accountType === 2 ? buyerUser.manager.mobile : buyerUser.mobile,
|
|
sellerMobile: sellerUser.accountType === 2 ? sellerUser.manager.mobile : sellerUser.mobile,
|
|
buyerBankName,
|
|
sellerBankName,
|
|
buyerAccount,
|
|
sellerAccount,
|
|
buyerBankAccountName,
|
|
sellerBankAccountName,
|
|
}
|
|
// for (let i = 0; i < fieldList.length; i++) {
|
|
//
|
|
// const field = fieldList[i]
|
|
// parameter[field.field] = ''
|
|
// }
|
|
const uploaddocs = await pubFun.fadada('generate_contract.api', {
|
|
template_id: templateId.content,
|
|
contract_id: contractTransactionId,
|
|
parameter_map: JSON.stringify(parameter)
|
|
})
|
|
let otherFileResList = []
|
|
if (sheet.files && sheet.files.length > 0) {
|
|
for (let i = 0; i < sheet.files.length; i++) {
|
|
otherFileResList.push(contractTransactionId + 'f' + i.toString())
|
|
await pubFun.fadada('uploaddocs.api', {
|
|
contract_id: contractTransactionId + 'f' + i.toString(),
|
|
doc_title: sheet.title + `:附件${i + 1}`,
|
|
doc_url: sheet.files[i],
|
|
doc_type: '.pdf'
|
|
})
|
|
}
|
|
}
|
|
if (sheet.multiFiles && sheet.multiFiles.length > 0) {
|
|
for (let i = 0; i < sheet.multiFiles.length; i++) {
|
|
otherFileResList.push(contractTransactionId + 'mf' + i.toString())
|
|
await pubFun.fadada('uploaddocs.api', {
|
|
contract_id: contractTransactionId + 'mf' + i.toString(),
|
|
doc_title: sheet.title + `:加工商附件${i + 1}`,
|
|
doc_url: sheet.multiFiles[i],
|
|
doc_type: '.pdf'
|
|
})
|
|
}
|
|
// otherFileResList.unshift(contractTransactionId)
|
|
|
|
// 合并接口
|
|
// contractTransactionId = vk.pubfn.createOrderNo("MC", 20)
|
|
// const mergeRes = await pubFun.fadada('merge_contract.api', {
|
|
// contract_id: contractTransactionId,
|
|
// doc_title: sheet.title,
|
|
// contract_ids: otherFileResList.join()
|
|
// })
|
|
// console.log('mergeRes', mergeRes)
|
|
// console.log('otherFileResList', otherFileResList)
|
|
}
|
|
// 存在附件则合并
|
|
|
|
if (quote.files && quote.files.length > 0) {
|
|
for (let i = 0; i < quote.files.length; i++) {
|
|
otherFileResList.push(contractTransactionId + 'qf' + i.toString())
|
|
await pubFun.fadada('uploaddocs.api', {
|
|
contract_id: contractTransactionId + 'qf' + i.toString(),
|
|
doc_title: sheet.title + `:报价附件${i + 1}`,
|
|
doc_url: quote.files[i],
|
|
doc_type: '.pdf'
|
|
})
|
|
}
|
|
// otherFileResList.unshift(contractTransactionId)
|
|
|
|
// 合并接口
|
|
// contractTransactionId = vk.pubfn.createOrderNo("MC", 20)
|
|
// const mergeRes = await pubFun.fadada('merge_contract.api', {
|
|
// contract_id: contractTransactionId,
|
|
// doc_title: sheet.title,
|
|
// contract_ids: otherFileResList.join()
|
|
// })
|
|
// console.log('quote mergeRes', mergeRes)
|
|
// console.log('quote otherFileResList', otherFileResList)
|
|
}
|
|
if (otherFileResList.length) {
|
|
otherFileResList.unshift(contractTransactionId)
|
|
// 合并接口
|
|
contractTransactionId = vk.pubfn.createOrderNo("MC", 20)
|
|
const mergeRes = await pubFun.fadada('merge_contract.api', {
|
|
contract_id: contractTransactionId,
|
|
doc_title: sheet.title,
|
|
contract_ids: otherFileResList.join()
|
|
})
|
|
console.log('mergeRes', mergeRes)
|
|
console.log('otherFileResList', otherFileResList)
|
|
}
|
|
await vk.baseDao.update({
|
|
dbName: dbName.contract,
|
|
whereJson: {
|
|
_id: contractId
|
|
},
|
|
dataJson: {
|
|
transactionId: contractTransactionId,
|
|
}
|
|
})
|
|
if (parseInt(uploaddocs.code) === 1000) {
|
|
await vk.baseDao.updateAndReturn({
|
|
dbName: dbName.contract,
|
|
whereJson: {
|
|
_id: contractId,
|
|
userId: uid
|
|
},
|
|
dataJson: {
|
|
status: 1,
|
|
docTitle: sheet.title,
|
|
signEndDate,
|
|
otherFile: '',
|
|
contractBase: null,
|
|
buyerSignKeyword: buyerSignKeyword.content,
|
|
sellerSignKeyword: sellerSignKeyword.content
|
|
}
|
|
})
|
|
// await vk.baseDao.updateAndReturn({
|
|
// dbName: 'jjx-process-sheet',
|
|
// whereJson: {
|
|
// _id: data.sheetId
|
|
// },
|
|
// dataJson: {
|
|
// status: 2
|
|
// }
|
|
// })
|
|
|
|
res.data = uploaddocs.result
|
|
|
|
await vk.baseDao.add({
|
|
dbName: "jjx-message",
|
|
dataJson: {
|
|
uid: quote.userId,
|
|
title: `加工:${sheet.title}待签合同`,
|
|
content: `您报价的加工信息已创建合同,赶紧去签订合同吧`,
|
|
type: 'contract',
|
|
hasRead: false,
|
|
pk: quote.sheetId
|
|
}
|
|
});
|
|
await pubFun.sendNotifySms(quote.userId, 'SMS_474295535')
|
|
} else {
|
|
return {
|
|
code: -1,
|
|
msg: uploaddocs.msg
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
const orderId = await vk.baseDao.add({
|
|
dbName: "jjx-order",
|
|
dataJson: {
|
|
orderNo: vk.pubfn.createOrderNo("39XU", 20),
|
|
uid: uid,
|
|
sellerUid: quote.userId,
|
|
orderStatus: 0,
|
|
payStatus: 0,
|
|
type: 5,
|
|
amount: quote.totalPrice,
|
|
offAmount: quote.totalPrice,
|
|
signMethod: 1,
|
|
sheetId: quote.sheetId
|
|
}
|
|
})
|
|
|
|
await vk.baseDao.add({
|
|
dbName: "jjx-order-goods",
|
|
dataJson: {
|
|
orderId,
|
|
goodsId: null,
|
|
num: 1,
|
|
goodsName: '加工单',
|
|
goodsPrice: quote.totalPrice,
|
|
goodsCover: null,
|
|
}
|
|
})
|
|
}
|
|
|
|
|
|
// 业务逻辑结束-----------------------------------------------------------
|
|
return res;
|
|
}
|
|
}
|