using ADODB;
using log4net;
using MSXML2;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using UFGeneralFilterOCX;
using UFIDA.U8.Framework.Api.ControlObject.Common;
using UFIDA.U8.Framework.Api.ControlObject.Entity;
using UFIDA.U8.Framework.Api.ControlObject.PU;
using UFIDA.U8.Framework.Api.ControlObject.U8M;
namespace UFIDA.U8.Framework.Api.ControlObject.SA
{
///
/// 销售管理
///
public class ClsVoucherCO_SA
{
protected readonly ILog _logInfo = LogManager.GetLogger("logInfo");
private dynamic InitCo(BaseLogin login, int enmVoucherType)
{
var errorMsg = "";
var oSystem = "USSAServer.clsSystem".GetComObject();
if (!oSystem.Init(login._vbLogin, ref errorMsg))
{
throw new Exception($"初始化失败:{errorMsg}");
}
var co = "VoucherCO_Sa.ClsVoucherCO_SA".GetComObject();
try
{
co.Init(enmVoucherType: enmVoucherType, oLogin: ref login._vbLogin, oConn: ref login.Connection, sysObj: oSystem);
}
catch (Exception ex)
{
throw new Exception($"初始化失败:{ex.Message}");
}
return co;
}
public async Task> Save(BaseLogin baseLogin, Dictionary dictionary, string cardNumber, VoucherTypeSA enmVoucherType, int VoucherState = 0, bool bGetCode = false, string codeKey = null, bool? bVerify = false, bool iswfcontrolled = false, string cFlowFieldName = "IsWfControlled")
{
try
{
var errorMsg = "";
_logInfo.Debug("开始输出登录信息");
_logInfo.Debug(baseLogin._vbLogin.userToken);
var co = InitCo(baseLogin, (int)enmVoucherType);
IXMLDOMDocument2 domHead = new DOMDocument();
IXMLDOMDocument2 domBody = new DOMDocument();
IXMLDOMDocument2 domCur = new DOMDocument();
_logInfo.Debug("开始获取单据默认模板...");
co.GetVoucherData(ref domHead, ref domBody, null, true, "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, ref newCode, ref errorMsg))
{
throw new Exception(errorMsg);
}
_logInfo.Debug($"获取新单号:{newCode}");
if (!string.IsNullOrWhiteSpace(newCode))
{
domHead.SetDomKeyValue(codeKey, newCode);
}
}
_logInfo.Debug("填充表体DOM...");
domBody = dictionarys.ToDocument2(domBody);
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();
_logInfo.Debug("开始保存...");
errorMsg = co.Save(ref domHead, ref domBody, VoucherState, ref vouchId, ref domCur);
_logInfo.Debug("保存结束...");
_logInfo.Debug(errorMsg);
_logInfo.Debug($"{vouchId}");
res.bSuccess = (string.IsNullOrWhiteSpace(errorMsg) && !string.IsNullOrWhiteSpace($"{vouchId}")) ||
(errorMsg == "True" && !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 == 0)
//{
// 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;
res.message = "保存成功";
_logInfo.Debug($"{baseLogin.Connection.State},{baseLogin.Connection.ConnectionString}");
var strWhere = CommonHelper.GetVoucherWhereSqlById(vouchers, vouchId);
if (res.bSuccess && bVerify == true)
{
var verifyRes =await VerifyVouch(co, baseLogin.Connection, enmVoucherType, res.voucherCode, res.voucherId, strWhere, true);
if (verifyRes.bSuccess == false)
{
return verifyRes;
}
}
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> VerifyVouch(dynamic voucherCo, Connection conn, VoucherTypeSA sVouchType, string cCode, object vouchId, string strWhere, bool bVerify)
{
try
{
IXMLDOMDocument2 domHead = new DOMDocument();
IXMLDOMDocument2 domBody = new DOMDocument();
IXMLDOMDocument2 domMsg = new DOMDocument();
_logInfo.Debug("开始获取单据数据...");
voucherCo.GetVoucherData(ref domHead, ref domBody, vouchId, true, "");
var res = new BaseResult
{
voucherCode = cCode,
voucherId = $"{vouchId}"
};
var errorMsg = voucherCo.VerifyVouch(ref domHead, bVerify);
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;
}
catch (Exception ex)
{
_logInfo.Error(ex.Message, ex);
await Task.Delay(1);
return new BaseResult { bSuccess = false, message = "审核失败", result = ex.Message };
}
}
}
}