using ADODB; using log4net; using Microsoft.VisualBasic; using MSXML2; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection.Emit; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; using UFGeneralFilterOCX; using UFIDA.U8.Framework.Api.ControlObject.Common; using UFIDA.U8.Framework.Api.ControlObject.Entity; using UFIDA.U8.Framework.Api.ControlObject.ST; using UFIDA.U8.Framework.Api.ControlObject.U8M; // ReSharper disable InconsistentNaming namespace UFIDA.U8.Framework.Api.ControlObject.PU { /// /// 采购管理 /// public class clsVoucherCO_PU { protected readonly ILog logInfo = LogManager.GetLogger("logInfo"); private dynamic InitCo(BaseLogin login, vouchertype enmVoucherType, bool bPositive = true, string sBillType = null, string sBusType = null, UseMode UserMode = UseMode.CS, string sOverDetailsXml = null) { var errorMsg = ""; var infor = "Info_PU.ClsS_Infor".GetComObject(); errorMsg = infor.Init(login._vbLogin, sBusType); if (!string.IsNullOrWhiteSpace(errorMsg)) { throw new Exception(errorMsg); } var co = "VoucherCO_PU.clsVoucherCO_PU".GetComObject(); co.Init(enmVoucherType, login._vbLogin, login.Connection, infor, bPositive, sBillType, sBusType, UserMode, sOverDetailsXml); return co; } /// /// 保存单据 /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// public async Task> VoucherSave(BaseLogin baseLogin, Dictionary dictionary, string cardNumber, vouchertype enmVoucherType, bool bPositive = true, string sBillType = null, string sBusType = null, int VoucherState = 2, UseMode UserMode = UseMode.CS, string sOverDetailsXml = null, bool bGetCode = false, string codeKey = null, bool bAudit = false, bool bPrintDom = false,bool iswfcontrolled=false,string cFlowFieldName= "IsWfControlled") { try { var errorMsg = ""; logInfo.Debug("开始输出登录信息"); logInfo.Debug(baseLogin._vbLogin.userToken); var co = InitCo(baseLogin, enmVoucherType, bPositive, sBillType, sBusType, UserMode, sOverDetailsXml); IXMLDOMDocument2 domHead = new DOMDocument(); IXMLDOMDocument2 domBody = new DOMDocument(); IXMLDOMDocument2 domCur = new DOMDocument(); logInfo.Debug("开始获取单据默认模板..."); co.GetVoucherData(ref domHead, ref domBody, "1=2"); logInfo.Debug("填充表头DOM..."); domHead = dictionary.ToDocument2(domHead); if (!dictionary.TryGetValue("body", out var bodys)) { await Task.Delay(1); throw new Exception("无法获取表体数据"); } if (!(bodys is List> dictionarys) || dictionarys.Count <= 0) { await Task.Delay(1); throw new Exception("无法获取表体数据"); } if (bGetCode) { errorMsg = ""; var newCode = ""; if (!co.GetVoucherNO(ref domHead, $"{enmVoucherType}", ref errorMsg, ref newCode)) { throw new Exception(errorMsg); } logInfo.Debug($"获取新单号:{newCode}"); if (!string.IsNullOrWhiteSpace(newCode)) { domHead.SetDomKeyValue(codeKey, newCode); } } logInfo.Debug("填充表体DOM..."); domBody = dictionarys.ToDocument2(domBody); if (bPrintDom) { logInfo.Debug("开始输入DOM..."); logInfo.Debug(domHead.xml); logInfo.Debug(domBody.xml); } object vouchId = ""; IXMLDOMDocument2 domMsg = new DOMDocument(); IXMLDOMDocument2 curDom = new DOMDocument(); logInfo.Debug($"{baseLogin.Connection.State},{baseLogin.Connection.ConnectionString}"); logInfo.Debug("开始查询单据配置..."); var vouchers = CommonHelper.GetVouchers(baseLogin.Connection, cardNumber); var res = new BaseResult(); if (bAudit) { logInfo.Debug("开始事务..."); co.bOutTrans = true; baseLogin.Connection.BeginTrans(); } try { logInfo.Debug("开始保存..."); errorMsg = bAudit ? (string)co.VoucherSave2(ref domHead, ref domBody, VoucherState, ref vouchId, sOverDetailsXml, ref domMsg) : (string)co.VoucherSave(ref domHead, ref domBody, VoucherState, ref vouchId, ref curDom, UserMode, sOverDetailsXml, ref domMsg); logInfo.Debug("保存结束..."); logInfo.Debug(errorMsg); logInfo.Debug($"{vouchId}"); res.bSuccess = string.IsNullOrWhiteSpace(errorMsg) && !string.IsNullOrWhiteSpace($"{vouchId}"); if (!res.bSuccess) { if (!string.IsNullOrWhiteSpace(errorMsg)) { throw new Exception(errorMsg); } if (!string.IsNullOrWhiteSpace(domMsg.xml)) { throw new Exception(domMsg.xml); } if (!string.IsNullOrWhiteSpace(curDom.xml)) { throw new Exception(curDom.xml); } throw new Exception("未知错误"); } if (iswfcontrolled == false && VoucherState == 2) { var strSql = $"UPDATE {vouchers.cBTTblName} SET {cFlowFieldName} = 0 WHERE {vouchers.voucherIdFieldName}={vouchId}"; object rsts = new Recordset(); baseLogin.Connection.Execute(strSql,out rsts); } res.voucherCode = CommonHelper.GetVoucherCode(baseLogin.Connection, vouchId, vouchers); res.voucherId = $"{vouchId}"; res.result = errorMsg; } catch (Exception) { try { if (bAudit) baseLogin.Connection.RollbackTrans(); } catch(Exception ex) { Trace.WriteLine(ex.Message); } throw; } if (!res.bSuccess) { res.message = "保存失败"; if (bAudit) baseLogin.Connection.RollbackTrans(); return res; } res.message = "保存成功"; logInfo.Debug($"{baseLogin.Connection.State},{baseLogin.Connection.ConnectionString}"); var strWhere = CommonHelper.GetVoucherWhereSqlById(vouchers, vouchId); if (res.bSuccess && bAudit) { try { errorMsg = Confirm(co, baseLogin.Connection, enmVoucherType, res.voucherCode, res.voucherId, strWhere); if (!string.IsNullOrWhiteSpace(errorMsg)) { baseLogin.Connection.RollbackTrans(); throw new Exception(errorMsg); } } catch (Exception) { baseLogin.Connection.RollbackTrans(); res.bSuccess = false; throw; } } if (bAudit) { logInfo.Debug("提交事务..."); baseLogin.Connection.CommitTrans(); } return res; } catch (Exception ex) { logInfo.Error(ex.Message, ex); await Task.Delay(1); return new BaseResult { bSuccess = false, message = "保存失败", result = ex.Message }; } } /// /// 批量审核单据(单号) /// /// /// /// /// /// /// /// /// /// /// /// public async Task>> Confirm(BaseLogin baseLogin, string sCardNum, vouchertype enmVoucherType, List cCodes, bool bPositive = true, string sBillType = null, string sBusType = null, UseMode UserMode = UseMode.CS, string sOverDetailsXml = null) { logInfo.Debug("开始输出登录信息"); logInfo.Debug(baseLogin._vbLogin.userToken); var co = InitCo(baseLogin, enmVoucherType, bPositive, sBillType, sBusType, UserMode, sOverDetailsXml); var conn = baseLogin.Connection; var vouchers = CommonHelper.GetVouchers(conn, sCardNum); var resList = new List>(); foreach (var cCode in cCodes) { try { var strWhere = CommonHelper.GetPUVoucherWhereSqlByCode(vouchers, cCode); var curId = CommonHelper.GetVoucherId(conn, cCode, vouchers); var verifyResult = await Confirm(co, conn, enmVoucherType, cCode, curId, strWhere); resList.Add(verifyResult); } catch (Exception ex) { await Task.Delay(1); logInfo.Error(ex.Message, ex); } } return resList; } /// /// 批量审核单据(ID) /// /// /// /// /// /// /// /// /// /// /// /// public async Task>> Confirm(BaseLogin baseLogin, string sCardNum, vouchertype enmVoucherType, List vouchIds, bool bPositive = true, string sBillType = null, string sBusType = null, UseMode UserMode = UseMode.CS, string sOverDetailsXml = null) { var co = InitCo(baseLogin, enmVoucherType, bPositive, sBillType, sBusType, UserMode, sOverDetailsXml); var conn = baseLogin.Connection; var vouchers = CommonHelper.GetVouchers(conn, sCardNum); var resList = new List>(); foreach (var vouchId in vouchIds) { var strWhere = CommonHelper.GetPUVoucherWhereSqlById(vouchers, vouchId); var cCode = CommonHelper.GetVoucherCode(conn, vouchId, vouchers); var verifyResult = await Confirm(co, conn, enmVoucherType, cCode, vouchId, strWhere); resList.Add(verifyResult); } return resList; } /// /// 审核单据(单号) /// /// /// /// /// /// /// /// /// /// /// /// public async Task> Confirm(BaseLogin baseLogin, string sCardNum, vouchertype enmVoucherType, string cCode, bool bPositive = true, string sBillType = null, string sBusType = null, UseMode UserMode = UseMode.CS, string sOverDetailsXml = null) { var co = InitCo(baseLogin, enmVoucherType, bPositive, sBillType, sBusType, UserMode, sOverDetailsXml); var conn = baseLogin.Connection; var vouchers = CommonHelper.GetVouchers(conn, sCardNum); var curId = CommonHelper.GetVoucherId(conn, cCode, vouchers); var strWhere = CommonHelper.GetPUVoucherWhereSqlByCode(vouchers, cCode); var verifyResult = await Confirm(co, conn, enmVoucherType, cCode, curId, strWhere); return verifyResult; } /// /// 审核单据(ID) /// /// /// /// /// /// /// /// /// /// /// /// public async Task> Confirm(BaseLogin baseLogin, string sCardNum, vouchertype enmVoucherType, long vouchId, bool bPositive = true, string sBillType = null, string sBusType = null, UseMode UserMode = UseMode.CS, string sOverDetailsXml = null) { var co = InitCo(baseLogin, enmVoucherType, bPositive, sBillType, sBusType, UserMode, sOverDetailsXml); var conn = baseLogin.Connection; var vouchers = CommonHelper.GetVouchers(conn, sCardNum); var strWhere = CommonHelper.GetPUVoucherWhereSqlById(vouchers, vouchId); var cCode = CommonHelper.GetVoucherCode(conn, vouchId, vouchers); var verifyResult = await Confirm(co, conn, enmVoucherType, cCode, vouchId, strWhere); return verifyResult; } /// /// 审核单据 /// /// /// /// /// /// /// /// public async Task> Confirm(dynamic voucherCO, Connection conn, vouchertype sVouchType, string cCode, object vouchId, string strWhere) { IXMLDOMDocument2 domHead = new DOMDocument(); IXMLDOMDocument2 domBody = new DOMDocument(); IXMLDOMDocument2 domMsg = new DOMDocument(); logInfo.Debug("开始获取单据数据..."); voucherCO.GetVoucherData(ref domHead, ref domBody, strWhere); var res = new BaseResult { voucherCode = cCode, voucherId = $"{vouchId}" }; var errorMsg = ""; switch (sVouchType) { case vouchertype.采购请购单: { #if DEBUG errorMsg = voucherCO.ConfirmApp(ref domHead); #else errorMsg = voucherCO.ConfirmApp(ref domHead, ref domMsg); #endif break; } case vouchertype.采购订单: { #if DEBUG errorMsg = voucherCO.ConfirmPO(ref domHead, ref domMsg); #else errorMsg = voucherCO.ConfirmPO(ref domHead, ref domMsg,ref domBody); #endif break; } case vouchertype.采购到货单: { errorMsg = voucherCO.ConfirmArr(ref domHead); break; } case vouchertype.采购发票: { errorMsg = voucherCO.ConfirmBill(ref domHead); break; } default: await Task.Delay(1); errorMsg = "未知业务"; break; } res.bSuccess = string.IsNullOrWhiteSpace(errorMsg); if (res.bSuccess) { res.message = "审核成功"; res.result = res.voucherId; } else { res.message = "审核失败"; res.result = string.IsNullOrWhiteSpace(errorMsg) ? domMsg.xml : errorMsg; } return res; } /// /// 批量弃审单据(单号) /// /// /// /// /// /// /// /// /// /// /// /// public async Task>> CancelConfirm(BaseLogin baseLogin, string sCardNum, vouchertype enmVoucherType, List cCodes, bool bPositive = true, string sBillType = null, string sBusType = null, UseMode UserMode = UseMode.CS, string sOverDetailsXml = null) { logInfo.Debug("开始输出登录信息"); logInfo.Debug(baseLogin._vbLogin.userToken); var co = InitCo(baseLogin, enmVoucherType, bPositive, sBillType, sBusType, UserMode, sOverDetailsXml); var conn = baseLogin.Connection; var vouchers = CommonHelper.GetVouchers(conn, sCardNum); var resList = new List>(); foreach (var cCode in cCodes) { try { var strWhere = CommonHelper.GetPUVoucherWhereSqlByCode(vouchers, cCode); var curId = CommonHelper.GetVoucherId(conn, cCode, vouchers); var verifyResult = await CancelConfirm(co, conn, enmVoucherType, cCode, curId, strWhere); resList.Add(verifyResult); } catch (Exception ex) { await Task.Delay(1); logInfo.Error(ex.Message, ex); } } return resList; } /// /// 批量弃审单据(ID) /// /// /// /// /// /// /// /// /// /// /// public async Task>> CancelConfirm(BaseLogin baseLogin, string sCardNum, vouchertype enmVoucherType, List vouchIds, bool bPositive = true, string sBillType = null, string sBusType = null, UseMode UserMode = UseMode.CS, string sOverDetailsXml = null) { var co = InitCo(baseLogin, enmVoucherType, bPositive, sBillType, sBusType, UserMode, sOverDetailsXml); var conn = baseLogin.Connection; var vouchers = CommonHelper.GetVouchers(conn, sCardNum); var resList = new List>(); foreach (var vouchId in vouchIds) { var strWhere = CommonHelper.GetPUVoucherWhereSqlById(vouchers, vouchId); var cCode = CommonHelper.GetVoucherCode(conn, vouchId, vouchers); var verifyResult = await CancelConfirm(co, conn, enmVoucherType, cCode, vouchId, strWhere); resList.Add(verifyResult); } return resList; } /// /// 弃审单据(单号) /// /// /// /// /// /// /// /// /// /// /// /// public async Task> CancelConfirm(BaseLogin baseLogin, string sCardNum, vouchertype enmVoucherType, string cCode, bool bPositive = true, string sBillType = null, string sBusType = null, UseMode UserMode = UseMode.CS, string sOverDetailsXml = null) { var co = InitCo(baseLogin, enmVoucherType, bPositive, sBillType, sBusType, UserMode, sOverDetailsXml); var conn = baseLogin.Connection; var vouchers = CommonHelper.GetVouchers(conn, sCardNum); var curId = CommonHelper.GetVoucherId(conn, cCode, vouchers); var strWhere = CommonHelper.GetPUVoucherWhereSqlByCode(vouchers, cCode); var verifyResult = await CancelConfirm(co, conn, enmVoucherType, cCode, curId, strWhere); return verifyResult; } /// /// 弃审单据(ID) /// /// /// /// /// /// /// /// /// /// /// /// public async Task> CancelConfirm(BaseLogin baseLogin, string sCardNum, vouchertype enmVoucherType, long vouchId, bool bPositive = true, string sBillType = null, string sBusType = null, UseMode UserMode = UseMode.CS, string sOverDetailsXml = null) { var co = InitCo(baseLogin, enmVoucherType, bPositive, sBillType, sBusType, UserMode, sOverDetailsXml); var conn = baseLogin.Connection; var vouchers = CommonHelper.GetVouchers(conn, sCardNum); var strWhere = CommonHelper.GetPUVoucherWhereSqlById(vouchers, vouchId); var cCode = CommonHelper.GetVoucherCode(conn, vouchId, vouchers); var verifyResult = await CancelConfirm(co, conn, enmVoucherType, cCode, vouchId, strWhere); return verifyResult; } /// /// 弃审单据 /// /// /// /// /// /// /// /// public async Task> CancelConfirm(dynamic voucherCO, Connection conn, vouchertype sVouchType, string cCode, object vouchId, string strWhere) { IXMLDOMDocument2 domHead = new DOMDocument(); IXMLDOMDocument2 domBody = new DOMDocument(); IXMLDOMDocument2 domMsg = new DOMDocument(); logInfo.Debug("开始获取单据数据..."); voucherCO.GetVoucherData(ref domHead, ref domBody, strWhere); var res = new BaseResult { voucherCode = cCode, voucherId = $"{vouchId}" }; var errorMsg = ""; switch (sVouchType) { case vouchertype.采购请购单: { errorMsg = voucherCO.CancelconfirmApp(ref domHead); break; } case vouchertype.采购订单: { #if DEBUG errorMsg = voucherCO.CancelconfirmPO(ref domHead); #else errorMsg = voucherCO.CancelconfirmPO(ref domHead,ref domBody); #endif break; } case vouchertype.采购到货单: { errorMsg = voucherCO.CancelconfirmArr(ref domHead); break; } case vouchertype.采购发票: { errorMsg = voucherCO.CancelconfirmBill(ref domHead); break; } default: await Task.Delay(1); errorMsg = "未知业务"; break; } res.bSuccess = string.IsNullOrWhiteSpace(errorMsg); if (res.bSuccess) { res.message = "弃审成功"; res.result = res.voucherId; } else { res.message = "弃审失败"; res.result = string.IsNullOrWhiteSpace(errorMsg) ? domMsg.xml : errorMsg; } return res; } /// /// 批量删除单据(单号) /// /// /// /// /// /// /// /// /// /// /// /// public async Task>> Delete(BaseLogin baseLogin, string sCardNum, vouchertype enmVoucherType, List cCodes, bool bPositive = true, string sBillType = null, string sBusType = null, UseMode UserMode = UseMode.CS, string sOverDetailsXml = null) { logInfo.Debug("开始输出登录信息"); logInfo.Debug(baseLogin._vbLogin.userToken); var co = InitCo(baseLogin, enmVoucherType, bPositive, sBillType, sBusType, UserMode, sOverDetailsXml); var conn = baseLogin.Connection; var vouchers = CommonHelper.GetVouchers(conn, sCardNum); var resList = new List>(); foreach (var cCode in cCodes) { try { var strWhere = CommonHelper.GetPUVoucherWhereSqlByCode(vouchers, cCode); var curId = CommonHelper.GetVoucherId(conn, cCode, vouchers); var verifyResult = await Delete(co, conn, enmVoucherType, cCode, curId, strWhere); resList.Add(verifyResult); } catch (Exception ex) { await Task.Delay(1); logInfo.Error(ex.Message, ex); } } return resList; } /// /// 批量删除单据(ID) /// /// /// /// /// /// /// /// /// /// /// public async Task>> Delete(BaseLogin baseLogin, string sCardNum, vouchertype enmVoucherType, List vouchIds, bool bPositive = true, string sBillType = null, string sBusType = null, UseMode UserMode = UseMode.CS, string sOverDetailsXml = null) { var co = InitCo(baseLogin, enmVoucherType, bPositive, sBillType, sBusType, UserMode, sOverDetailsXml); var conn = baseLogin.Connection; var vouchers = CommonHelper.GetVouchers(conn, sCardNum); var resList = new List>(); foreach (var vouchId in vouchIds) { var strWhere = CommonHelper.GetPUVoucherWhereSqlById(vouchers, vouchId); var cCode = CommonHelper.GetVoucherCode(conn, vouchId, vouchers); var verifyResult = await Delete(co, conn, enmVoucherType, cCode, vouchId, strWhere); resList.Add(verifyResult); } return resList; } /// /// 删除单据(单号) /// /// /// /// /// /// /// /// /// /// /// /// public async Task> Delete(BaseLogin baseLogin, string sCardNum, vouchertype enmVoucherType, string cCode, bool bPositive = true, string sBillType = null, string sBusType = null, UseMode UserMode = UseMode.CS, string sOverDetailsXml = null) { var co = InitCo(baseLogin, enmVoucherType, bPositive, sBillType, sBusType, UserMode, sOverDetailsXml); var conn = baseLogin.Connection; var vouchers = CommonHelper.GetVouchers(conn, sCardNum); var curId = CommonHelper.GetVoucherId(conn, cCode, vouchers); var strWhere = CommonHelper.GetPUVoucherWhereSqlByCode(vouchers, cCode); var verifyResult = await Delete(co, conn, enmVoucherType, cCode, curId, strWhere); return verifyResult; } /// /// 删除单据(ID) /// /// /// /// /// /// /// /// /// /// /// /// public async Task> Delete(BaseLogin baseLogin, string sCardNum, vouchertype enmVoucherType, long vouchId, bool bPositive = true, string sBillType = null, string sBusType = null, UseMode UserMode = UseMode.CS, string sOverDetailsXml = null) { var co = InitCo(baseLogin, enmVoucherType, bPositive, sBillType, sBusType, UserMode, sOverDetailsXml); var conn = baseLogin.Connection; var vouchers = CommonHelper.GetVouchers(conn, sCardNum); var strWhere = CommonHelper.GetPUVoucherWhereSqlById(vouchers, vouchId); var cCode = CommonHelper.GetVoucherCode(conn, vouchId, vouchers); var verifyResult = await Delete(co, conn, enmVoucherType, cCode, vouchId, strWhere); return verifyResult; } /// /// 删除单据 /// /// /// /// /// /// /// /// public async Task> Delete(dynamic voucherCO, Connection conn, vouchertype sVouchType, string cCode, object vouchId, string strWhere) { IXMLDOMDocument2 domHead = new DOMDocument(); IXMLDOMDocument2 domBody = new DOMDocument(); IXMLDOMDocument2 domMsg = new DOMDocument(); logInfo.Debug("开始获取单据数据..."); voucherCO.GetVoucherData(ref domHead, ref domBody, strWhere); var res = new BaseResult { voucherCode = cCode, voucherId = $"{vouchId}" }; var errorMsg = voucherCO.Delete(domHead, domBody); res.bSuccess = string.IsNullOrWhiteSpace(errorMsg); if (res.bSuccess) { res.message = "删除成功"; res.result = res.voucherId; } else { res.message = "删除失败"; res.result = string.IsNullOrWhiteSpace(errorMsg) ? "未知错误" : errorMsg; await Task.Delay(1); } return res; } } }