330 lines
12 KiB
C#
330 lines
12 KiB
C#
using ADODB;
|
|
using log4net;
|
|
using MSXML2;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using UFIDA.U8.Framework.Api.ControlObject.Common;
|
|
using UFIDA.U8.Framework.Api.ControlObject.Entity;
|
|
using UFIDA.U8.Framework.Api.ControlObject.Json;
|
|
using UFIDA.U8.Framework.Api.ControlObject.U8M;
|
|
// ReSharper disable StringLiteralTypo
|
|
// ReSharper disable CommentTypo
|
|
// ReSharper disable IdentifierTypo
|
|
// ReSharper disable InconsistentNaming
|
|
// ReSharper disable UnusedMember.Global
|
|
|
|
namespace UFIDA.U8.Framework.Api.ControlObject.ST
|
|
{
|
|
/// <summary>
|
|
/// 库存管理
|
|
/// </summary>
|
|
public class USERPCO
|
|
{
|
|
protected readonly ILog logInfo = LogManager.GetLogger("logInfo");
|
|
|
|
|
|
public async Task<BaseResult<string>> Insert(BaseLogin baseLogin, Dictionary<string, object> dictionary,string cCardNum, string sVouchType,bool bGetCode=false,string codeKey="",bool bCheck=true,bool bBeforCheckStock=true,bool bIsRedVouch=false,string sAddedState="", bool bReMote=false,bool bAudit=false,bool bPrintDom=false)
|
|
{
|
|
try
|
|
{
|
|
var co = "USERPCO.VoucherCO".GetComObject();
|
|
|
|
var errorMsg = "";
|
|
logInfo.Debug("开始输出登录信息");
|
|
logInfo.Debug(baseLogin._vbLogin.userToken);
|
|
|
|
if (!co.IniLogin(baseLogin._vbLogin, ref errorMsg))
|
|
{
|
|
await Task.Delay(1);
|
|
throw new Exception(errorMsg);
|
|
}
|
|
|
|
IXMLDOMDocument2 domHead = new DOMDocument();
|
|
IXMLDOMDocument2 domBody = new DOMDocument();
|
|
IXMLDOMDocument2 domPosition = new DOMDocument();
|
|
|
|
errorMsg = "";
|
|
|
|
try
|
|
{
|
|
logInfo.Debug("开始获取单据默认模板...");
|
|
if (!co.GetDefaultVoucherDom(sVouchType, cCardNum, ref domHead, ref domBody, ref domPosition,
|
|
ref errorMsg) || !string.IsNullOrWhiteSpace(errorMsg))
|
|
{
|
|
throw new Exception(errorMsg);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logInfo.Error($"获取单据默认模板失败,错误信息:{ex.Message}", ex);
|
|
|
|
errorMsg = "";
|
|
|
|
logInfo.Debug($"开始读取单据数据,当前单据编码:{cCardNum}");
|
|
|
|
if (!co.Load(sVouchType, "1=2", ref domHead, ref domBody, ref domPosition, ref errorMsg, true) || !string.IsNullOrWhiteSpace(errorMsg))
|
|
{
|
|
throw new Exception(errorMsg);
|
|
}
|
|
|
|
}
|
|
|
|
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<Dictionary<string, object>> dictionarys) || dictionarys.Count <= 0)
|
|
{
|
|
await Task.Delay(1);
|
|
throw new Exception("无法获取表体数据");
|
|
}
|
|
|
|
if (bGetCode && !string.IsNullOrWhiteSpace(codeKey))
|
|
{
|
|
errorMsg = "";
|
|
var newCode = co.GetNewVouchCode(sVouchType, domHead, ref errorMsg);
|
|
|
|
logInfo.Debug($"获取新单号:{newCode}");
|
|
|
|
if (!string.IsNullOrWhiteSpace(newCode))
|
|
{
|
|
CommonHelper.SetDomKeyValue(domHead, codeKey, newCode);
|
|
}
|
|
}
|
|
|
|
logInfo.Debug("填充表体DOM...");
|
|
domBody = dictionarys.ToDocument2(domBody);
|
|
|
|
IXMLDOMDocument2 domMsg = new DOMDocument();
|
|
errorMsg = "";
|
|
baseLogin.Connection.BeginTrans();
|
|
object vouchId = "";
|
|
|
|
var res = new BaseResult<string>();
|
|
try
|
|
{
|
|
if (bPrintDom)
|
|
{
|
|
logInfo.Debug("开始输入DOM...");
|
|
|
|
logInfo.Debug(domHead.xml);
|
|
|
|
logInfo.Debug(domBody.xml);
|
|
|
|
logInfo.Debug(domPosition.xml);
|
|
}
|
|
|
|
logInfo.Debug("执行插入操作...");
|
|
//var cmd = new Command
|
|
//{
|
|
// CommandText = "Select '开始执行插入操作'",
|
|
// ActiveConnection = baseLogin.Connection
|
|
//};
|
|
//object rsts = new Recordset();
|
|
//cmd.Execute(out rsts);
|
|
res.bSuccess = co.Insert(sVouchType, domHead, domBody, null, ref errorMsg, baseLogin.Connection, ref vouchId, ref domMsg, bCheck, bBeforCheckStock, bIsRedVouch, sAddedState, bReMote);
|
|
|
|
if (res.bSuccess)
|
|
{
|
|
logInfo.Debug($"操作完成,操作结果:成功!------[{vouchId}]------[{errorMsg}]------[{domMsg.xml}]");
|
|
}
|
|
else
|
|
{
|
|
logInfo.Debug($"操作完成,操作结果:失败!------[{vouchId}]------[{errorMsg}]------[{domMsg.xml}]");
|
|
}
|
|
|
|
}
|
|
catch (Exception)
|
|
{
|
|
await Task.Delay(1);
|
|
baseLogin.Connection.RollbackTrans();
|
|
throw;
|
|
}
|
|
|
|
res.message = errorMsg;
|
|
|
|
if (res.bSuccess && bAudit)
|
|
{
|
|
errorMsg = "";
|
|
|
|
if (!co.Verify(sVouchType, vouchId,ref errorMsg, baseLogin.Connection))
|
|
{
|
|
baseLogin.Connection.RollbackTrans();
|
|
|
|
if (!string.IsNullOrWhiteSpace(errorMsg))
|
|
throw new Exception(errorMsg);
|
|
|
|
throw new Exception("审核单据时发生异常,请检查数据");
|
|
}
|
|
}
|
|
|
|
if (!res.bSuccess)
|
|
{
|
|
baseLogin.Connection.RollbackTrans();
|
|
|
|
if(!string.IsNullOrWhiteSpace(errorMsg))
|
|
throw new Exception(errorMsg);
|
|
|
|
throw new Exception(domMsg.xml);
|
|
}
|
|
|
|
baseLogin.Connection.CommitTrans();
|
|
|
|
var rst = new Recordset();
|
|
rst.Open($"SELECT CardNumber,BTTblName,BTQName,BWTblName,BWQName,VchListQName,VchTblPrimarykeyNames,ReceiptNOFieldName FROM dbo.vouchers WITH(NOLOCK) WHERE CardNumber='{cCardNum}'", baseLogin.Connection);
|
|
|
|
if (rst.BOF || rst.EOF)
|
|
{
|
|
res.bSuccess = false;
|
|
res.result = $"已在U8生成单据ID为{vouchId}的单据,但是未能查询到该单据的配置信息!";
|
|
}
|
|
else
|
|
{
|
|
rst.MoveFirst();
|
|
var fieldName = $"{rst.Fields["VchTblPrimarykeyNames"].Value}";
|
|
var codeName = $"{rst.Fields["ReceiptNOFieldName"].Value}";
|
|
if (string.IsNullOrWhiteSpace(fieldName) || string.IsNullOrWhiteSpace(codeName))
|
|
{
|
|
res.voucherCode = $"{res.voucherId}";
|
|
}
|
|
else
|
|
{
|
|
if (codeName.Contains("{##}"))
|
|
{
|
|
codeName = codeName.Split('{', '#', '#', '}')[0];
|
|
}
|
|
|
|
var strSql = $"SELECT {codeName} AS cCode FROM {rst.Fields["BTTblName"].Value} WITH(NOLOCK) WHERE {fieldName}='{vouchId}'";
|
|
rst.Close();
|
|
|
|
rst.Open(strSql, baseLogin.Connection);
|
|
|
|
if (rst.EOF || rst.BOF)
|
|
{
|
|
res.voucherCode = "";
|
|
}
|
|
else
|
|
{
|
|
rst.MoveFirst();
|
|
res.voucherCode = $"{rst.Fields["cCode"].Value}";
|
|
}
|
|
}
|
|
}
|
|
|
|
res.message = "新增成功";
|
|
res.result =$"{vouchId}" ;
|
|
|
|
|
|
return res;
|
|
|
|
//var bodys = dictionary.TryGetValue() dictionary["body"];
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logInfo.Error(ex.Message, ex);
|
|
|
|
return new BaseResult<string> { bSuccess = false, message = "新增失败", result = ex.Message };
|
|
}
|
|
|
|
}
|
|
|
|
public async Task<BaseResult<string>> Update(BaseLogin baseLogin, Dictionary<string, object> dictionary, string sCardNum , string sVouchType,
|
|
bool bCheck = true, bool bBeforCheckStock = true, bool bIsRedVouch = false, string sAddedState = "",
|
|
bool bUpdateNeedEas = true)
|
|
{
|
|
try
|
|
{
|
|
var co = "USERPCO.VoucherCO".GetComObject();
|
|
|
|
var errorMsg = "";
|
|
|
|
if (!co.IniLogin(baseLogin._vbLogin, ref errorMsg))
|
|
{
|
|
await Task.Delay(1);
|
|
throw new Exception(errorMsg);
|
|
}
|
|
|
|
IXMLDOMDocument2 domHead = new DOMDocument();
|
|
IXMLDOMDocument2 domBody = new DOMDocument();
|
|
IXMLDOMDocument2 domPosition = new DOMDocument();
|
|
errorMsg = "";
|
|
if (!co.GetDefaultVoucherDom(sVouchType, sCardNum, ref domHead, ref domBody, ref domPosition, ref errorMsg))
|
|
{
|
|
throw new Exception(errorMsg);
|
|
}
|
|
|
|
domHead = dictionary.ToDocument2(domHead);
|
|
|
|
var bodys = dictionary["body"] as List<Dictionary<string, object>> ?? $"{dictionary["body"]}".ToObject<List<Dictionary<string, object>>>();
|
|
|
|
if (bodys==null || bodys.Count<=0)
|
|
{
|
|
await Task.Delay(1);
|
|
throw new Exception("无法获取表体数据");
|
|
}
|
|
|
|
//if (!(bodys is List<Dictionary<string, object>> dictionarys) || dictionarys.Count <= 0)
|
|
//{
|
|
|
|
// await Task.Delay(1);
|
|
// throw new Exception("无法获取表体数据");
|
|
//}
|
|
|
|
|
|
domBody = bodys.ToDocument2(domBody);
|
|
|
|
//IXMLDOMDocument2 domMsg = new DOMDocument();
|
|
|
|
errorMsg = "";
|
|
baseLogin.Connection.BeginTrans();
|
|
var vouchId = "";
|
|
|
|
var res = new BaseResult<string>();
|
|
try
|
|
{
|
|
res.bSuccess = co.Update(sVouchType, domHead, domBody, domPosition);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
baseLogin.Connection.RollbackTrans();
|
|
throw;
|
|
}
|
|
|
|
if (!res.bSuccess)
|
|
{
|
|
baseLogin.Connection.RollbackTrans();
|
|
res.message = "新增失败";
|
|
res.result = errorMsg;
|
|
}
|
|
else
|
|
{
|
|
baseLogin.Connection.CommitTrans();
|
|
res.message = "新增成功";
|
|
res.result = vouchId;
|
|
}
|
|
|
|
return res;
|
|
|
|
//var bodys = dictionary.TryGetValue() dictionary["body"];
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logInfo.Error(ex.Message, ex);
|
|
|
|
return new BaseResult<string> { bSuccess = false, message = "新增失败", result = ex.Message };
|
|
}
|
|
}
|
|
}
|
|
}
|