'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/enquiry/quote/kh/confirmQuote 前端调用的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, quoteId } = data; let res = { code: 0, msg: "" }; // 业务逻辑开始----------------------------------------------------------- // 修改报价单状态 const quote = await vk.baseDao.updateAndReturn({ dbName: 'jjx-old-quote', whereJson: { _id: quoteId, status: 0 }, dataJson: { status: 1 } }) if (!quote) { return { code: -1, msg: '请刷新页面' } } // 获取乙方信息 const quoteUser = await vk.baseDao.select({ dbName: 'uni-id-users', getMain: true, getOne: true, whereJson: { _id: quote.userId } }) // 获取询价单信息 const sheet = await vk.baseDao.updateAndReturn({ dbName: 'jjx-old', whereJson: {_id: quote.sheetId}, dataJson: { status: 1, quoteUid: quote.userId } }) // 获取商品信息 const productTitle = sheet.title if (sheet.type !== 3 && sheet.signMethod !== 1) { const fadadaContractId = vk.pubfn.createOrderNo("E", 20) // 新增合同记录 let contractTransactionId = vk.pubfn.createOrderNo("C", 20) const contractId = await vk.baseDao.add({ dbName: 'jjx-contract', dataJson: { fadadaContractId: fadadaContractId, contractBase: '', // 原始合同 contractHistory: [], // 历史合同 contractFinal: '', // 归档合同 status: 0, userId: uid, type: 'old', quoteId: quoteId, sheetId: quote.sheetId, productList: [], productTitle, tradeMethod: 0, partyCount: 2, signCount: 0, contractType: sheet.contractType, signMethod: sheet.signMethod, sellerSupplement: sheet.sellerSupplement, transactionId: contractTransactionId, amount: quote.totalPrice, docTitle: sheet.title } }) await vk.baseDao.updateById({ dbName: 'jjx-old', id: quote.sheetId, dataJson: { status: 1 } }) const checkMessage = await vk.baseDao.findByWhereJson({ dbName: "jjx-message-config", whereJson: { old: true, uid: quote.userId } }) if (checkMessage) { if (checkMessage.doNotDisturbRange) { if (dayjs().isBetween(checkMessage.doNotDisturbRange[0], checkMessage.doNotDisturbRange[1])) return } 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_474130453') } // 上传合同模板 //新增 用户与合同的关联记录 let buyerUid = uid let sellerUid = quote.userId if (sheet.type === 0) { buyerUid = quote.userId sellerUid = uid } await vk.daoCenter.userContractDao.adds([ { transactionId: vk.pubfn.createOrderNo("E", 20), userId: buyerUid, role: 'buyer', sign: false, contractId }, { transactionId: vk.pubfn.createOrderNo("E", 20), userId: sellerUid, 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 // } // }) let buyerSupplement = sheet.supplementary ? sheet.supplementary : '' let sellerSupplement = quote.remark ? quote.remark : '' if (quote.supplierExt) sellerSupplement += `${quote.remark ? ',' : ''}${quote.supplierExt}` let sellerUser = await vk.baseDao.findById({ dbName: 'uni-id-users', id: quote.userId }) let buyerUser = await vk.baseDao.findById({ dbName: 'uni-id-users', id: uid }) if (sheet.type === 0) { sellerUser = await vk.baseDao.findById({ dbName: 'uni-id-users', id: uid }) buyerUser = await vk.baseDao.findById({ dbName: 'uni-id-users', id: quote.userId }) buyerSupplement = quote.remark ? quote.remark : '' if (quote.supplierExt) buyerSupplement += `,${quote.supplierExt}` sellerSupplement = sheet.supplementary ? sheet.supplementary : '' } let detail = sheet.title if (sheet.otherCate) detail += `,${sheet.otherCateName}` if (sheet.type === 0 && sheet.unit) { detail += `,计价单位: ${sheet.unit},人民币单价: ${quote.totalPrice}元` } let tradeInfo = '' if (sheet.type === 0) tradeInfo = `${sheet.payMethod}` else tradeInfo = `报价:${quote.totalPrice}元,${sheet.payMethod}` if (sheet.payMethod === '预付' && sheet.prePayRate) tradeInfo += `,${sheet.prePayRate}%货到付全款` if (sheet.payMethod === '其他付款方式描述') tradeInfo += `:${sheet.otherPayMethodDesc}` if (sheet.type === 2) { tradeInfo += (sheet.invoice ? ',含税' : ',不含税') if (sheet.invoice) tradeInfo += (',开票时间:' + (sheet.writeInvoiceTime ? WRITE_INVOICE_TIME[sheet.writeInvoiceTime] : '无')) // tradeInfo += (',发票种类:' + (sheet.invoiceType ? INVOICE_TYPE[sheet.invoiceType] : '无')) // tradeInfo += (',其他要求:' + (sheet.invoiceOther ? sheet.invoiceOther : '无')) if (sheet.invoiceOther) tradeInfo += (',其他要求:' + sheet.invoiceOther) else { if (sheet.taxType && sheet.invoiceType && sheet.taxRate) tradeInfo += (`,纳税人身份:${sheet.taxType},发票种类:${sheet.invoiceType},税率:${sheet.taxRate}`) } tradeInfo += (',报价明细:' + (sheet.invoiceDetail ? sheet.invoiceDetail : '无')) } else if ([0, 1].includes(sheet.type)) { if (sheet.invoiceOther) tradeInfo += (',其他要求:' + sheet.invoiceOther) else { if (sheet.taxType && sheet.invoiceType && sheet.taxRate) tradeInfo += (`,纳税人身份:${sheet.taxType},发票种类:${sheet.invoiceType},税率:${sheet.taxRate}`) } } let expressRequire = '无' // expressRequire += `随货资料:${sheet.transportDocuments.join()},其他要求:${sheet.otherTransportDocuments ? sheet.otherTransportDocuments : '无'},运输限制要求:${sheet.transportLimit ? '有' : '无'}` // if (sheet.transportLimit && sheet.otherTransportRequest) expressRequire += `运输限制说明:${sheet.otherTransportRequest}` let region = '' if (sheet.type === 1) region = sheet.deliverAddressRegion.province.name + ' ' + sheet.deliverAddressRegion.city.name + ' ' + sheet.deliverAddressRegion.area.name else region = sheet.addressRegion.province.name + ' ' + sheet.addressRegion.city.name + ' ' + sheet.addressRegion.area.name 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().format('YYYY年MM月DD日 HH时mm'), buyerSignDate: dayjs().format('YYYY年MM月DD日 HH时mm'), contractNo: fadadaContractId, region, detail, info: '无', tradeInfo, expressRequire, defaultDuty: '1.乙方所交产品品种、型号、规格、质量不符合规定的,如果甲方同意利用,应当按质论价;如果甲方不能利用的,应根据产品的具体情况,由乙方负责包换或包修,并承担修理、调换或退货而支付的实际费用;2.乙方不予供货违约责任为甲方的实际损失+违约金,如乙方材料符合订货标准,甲方不能退货和不收货,甲方无条件应按实际本合同订货数量支付100%货款,并支付乙方因此产生的实际费用。带有甲乙双方单位相关收货人和发货人签字的本合同的发货清单事项和内容均视为本合同的补充合同,与本合同有相同的法律效力,双方应共同遵照执行。', solve: '1、凡因本合同引起的或与本合同有关的任何争议,均可友好协商或仲裁解决,协商或仲裁不成的,双方协商同意在合同签订地人民法院管辖地起诉,违约一方或对合同争议承担主要责任的一方,应承担对方因仲裁和起诉而产生的全部律师代理费、鉴定(检验)费、诉讼保全费、诉讼保险费、公告费、差旅费、执行费、误工費和获取证据产生费用等其他相关费用。2、双方本合同相关权利放弃均应有双方书面作出并加盖公章或授权委托人签字后生效。', forceMajeure: '甲乙双方的任何一方由于不可抗力或突发公共事件的原因不能履行合同时,应及时向对方通报不能履行或不能完全履行的理由,以减轻可能给对方造成的损失,在取得有关机构证明以后,允许延期履行、部分履行或者不履行合同,并根据情况可部分或全部免予承担违约责任。', other: '本合同的全部内容由双方平等共同协商一致,共同起草制定,非一方事先拟定,不属于格式条款。本合同自双方签字盖章后生效(包括电子签字、盖章),按本合同内容地址、联系方式寄送的一切文书、函件送达与否,均为视为送达有法律效力,通知途径和本合同内联系方式或地址送达文件双方均予以验证。', buyerSupplement, sellerSupplement, 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: buyerUser.accountType === 2 ? buyerUser.company.companyBankName : buyerUser.person.personBankName, sellerBankName: sellerUser.accountType === 2 ? sellerUser.company.companyBankName : sellerUser.person.personBankName, buyerAccount: buyerUser.accountType === 2 ? buyerUser.company.companyAccount : buyerUser.person.personAccount, sellerAccount: sellerUser.accountType === 2 ? sellerUser.company.companyAccount : sellerUser.person.personAccount, } // 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 (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' }) } } 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-old', whereJson: { _id: quote.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_474195497') } else { return { code: -1, msg: uploaddocs.msg } } } } // else { // const orderId = await vk.baseDao.add({ // dbName: "jjx-order", // dataJson: { // orderNo: vk.pubfn.createOrderNo("CO", 20), // uid: uid, // sellerUid: quote.userId, // orderStatus: 0, // type: 4, // amount: quote.totalPrice, // offAmount: quote.totalPrice, // signMethod: 1 // } // }) // // await vk.baseDao.add({ // dbName: "jjx-order-goods", // dataJson: { // orderId, // goodsId: null, // num: 1, // goodsName: '旧货交易', // goodsPrice: quote.totalPrice, // goodsCover: null, // } // }) // } // 业务逻辑结束----------------------------------------------------------- return res; } }