using Quartz;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ICSSoft.FromERP
{
///
/// 进销存统计表
///
public class YERP_001 : IJob
{
private static object key = new object();
private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public void Execute(IJobExecutionContext context)
{
try
{
lock (key)
{
log.Info("开始……………………………………………………………………");
Execute();
log.Info("结束……………………………………………………………………");
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
public async void Execute()
{
try
{
Configuration config = GetConfig();
//string url = config.ConnectionStrings.ConnectionStrings["APIAddStdWorkHour"].ConnectionString.ToString();
var Dates = DateTime.Now;
//log.Info("获取创建定额工时接口 " + url);
//if (string.IsNullOrEmpty(url))
//{
// return;
//}
string conStr = ICSHelper.GetConnectString();
//
//
//
//
//
//
// 2024-08-20
// BR8040
// 103016
// 0.00
// 0.00
// 15000.00
// 245870.26
// 0.00
// 0.00
// 8305.00
// 157592.50
// 8388.00
// 151226.81
// 0.00
// 0.00
// 6450.0000
// 102513.2100
// 115839.8800
// 0
// 0
// 6450.00
// 108183.8000
// -5670.59
// ?
// 8467.00
// 144052.15
//
//
//
//
// 202404
// 过滤时间为上个月
string Date = Dates.ToString("yyyy");// 2024;
var beginPeriod = Dates.AddMonths(-1).AddDays(-Dates.Day + 1).ToString("yyyy-MM-dd");//"2023-06-01";
var endPeriod = Dates.AddDays(-Dates.Day + 1).ToString("yyyy-MM-dd");//"2023-07-01";
string sqls = @"
begin transaction
exec Sa_MoveSRMDetail N'JXCASKALL',N' ',N'{0}',N'{1}',N'{2}',N' ',N' ',N' ',N' ',N''
select identity(int,0,1) as baseid,isnull([存货名称],'') as [存货名称],isnull([规格型号],'') as [规格型号],isnull([存货编码],'') as [存货编码],(Convert(Decimal(38,2),SUM([cInvDefine12]))) as [cInvDefine12],(Convert(Decimal(38,2),SUM([cInvDefine11]))) as
[cInvDefine11],Convert(Decimal(38,2),SUM(isnull([期初金额],0)+isnull([采购金额],0)+isnull([其他入库金额],0)-isnull([销售成本],0)-isnull([其他出库金额],0))) as [期末金额],Convert(Decimal(38,2),SUM(isnull([期初数量],0)+isnull([采购数量],0)+isnull([其他入库数量],0)-isnull([其他出库数量],0)-isnull([出库数量],0))) as [期末数量],(Convert(Decimal(38,4),SUM([销售收入]))) as [销售收入],(Convert(Decimal(38,4),SUM([销售成本]))) as [销售成本],Convert(Decimal(38,2),SUM(isnull([销售收入],0)-isnull([销售成本],0))) as [毛利],(Convert(Decimal(38,2),SUM([出库数量]))) as [出库数量],(Convert(Decimal(38,4),SUM([销售金额]))) as [销售金额],(Convert(Decimal(38,4),SUM([销售数量]))) as [销售数量],(Convert(Decimal(38,2),SUM([调拨金额]))) as [调拨金额],(Convert(Decimal(38,2),SUM([调拨数量]))) as [调拨数量],(Convert(Decimal(38,2),SUM([其他出库金额]))) as [其他出库金额],(Convert(Decimal(38,2),SUM([其他出库数量]))) as [其他出库数量],(Convert(Decimal(38,2),SUM([其他入库金额]))) as [其他入库金额],(Convert(Decimal(38,2),SUM([其他入库数量]))) as [其他入库数量],(Convert(Decimal(38,2),SUM([暂估金额]))) as [暂估金额],(Convert(Decimal(38,2),SUM([暂估数量]))) as [暂估数量],(Convert(Decimal(38,2),SUM([采购金额]))) as [采购金额],(Convert(Decimal(38,2),SUM([采购数量]))) as [采购数量],(Convert(Decimal(38,2),SUM([期初金额]))) as [期初金额],(Convert(Decimal(38,2),SUM([期初数量]))) as [期初数量]
into tempdb..JXCASKALL_1 from tempdb..JXCASKALL
where 1 = 1 group by isnull([存货名称],''),isnull([规格型号],''),isnull([存货编码],'')
having isnull(sum([期初数量]),0)<> 0 or isnull(sum([采购数量]),0)<> 0 or isnull(sum([采购金额]),0) <> 0 or isnull(sum([其他入库数量]),0) <> 0 or isnull(sum([其他入库金额] ),0)<> 0 or isnull(sum([出库数量]),0)<>0 or isnull(sum([其他出库数量] ),0)<> 0 or isnull(sum([其他出库金额]),0) <> 0 or isnull(sum([期初金额]),0) <> 0 or isnull(sum([暂估数量]),0) <> 0 or isnull(sum([暂估金额] ),0)<> 0 or isnull(sum([销售数量]),0) <> 0 or isnull(sum([销售金额] ),0)<> 0 or isnull(sum([调拨数量]),0) <> 0 or isnull(sum([调拨金额]),0) <> 0 or isnull(sum([销售收入]),0) <> 0 or isnull(sum([销售成本]),0) <> 0
select A.* into TEMPDB..JXCASKALLNEW from TEMPDB..JXCASKALL A
inner join TEMPDB..JXCASKALL_1 sum on isnull(A.[存货名称],'')=isnull(sum.[存货名称],'') and isnull(A.[规格型号],'')=isnull(sum.[规格型号],'') and isnull(A.[存货编码],'')=isnull(sum.[存货编码],'')
select sum.* into TEMPDB..JXCASKALLNEW_1 from TEMPDB..JXCASKALL_1 sum
select identity(int,0,1) as index__id,* into tempdb..JXCASKALLNEW_index from
( select baseid +0 as baseid, [存货名称],[规格型号],[存货编码] from tempdb..JXCASKALLNEW_1)A where 1=0
insert into TEMPDB..JXCASKALLNEW_index
select * from (select top 1000 baseid +0 as baseid, [存货名称],[规格型号],[存货编码]
from TEMPDB..JXCASKALLNEW_1 order by [存货名称] asc)a
select B.index__id,a.存货名称 INVT_NM,a.存货编码 INVT_CD,a.期初数量 BSC_QTY,a.期初金额 BSC_AMT,a.采购数量 PCHS_QTY,a.采购金额 PCHS_AMT,a.暂估数量 ESTM_QTY,a.暂估金额 ESTM_AMT,a.其他入库数量 ETC_WHG_QTY,
a.其他入库金额 ETC_WHG_AMT,a.其他出库数量 ETC_RLS_QTY,a.其他出库金额 ETC_RLS_AMT,a.调拨数量 MOVE_QTY,a.调拨金额 MOVE_AMT,a.销售数量 SL_QTY,a.销售收入 SL_RVN,a.销售金额 SL_AMT,
'' FRTX_AVRG_PRC,
'' SMTX_AVRG_PRC,
a.出库数量 RLS_QTY,a.销售成本 SL_COST,a.毛利 SLS_TOT_GAIN,
'' SLS_TOT_PRTO,a.期末数量 TE_QTY,a.期末金额 TE_AMT
from TEMPDB..JXCASKALLNEW_1 A inner join
TEMPDB..JXCASKALLNEW_index B on A.baseid=B.baseid
drop table tempdb..JXCASKALL
drop table tempdb..JXCASKALL_1
drop table TEMPDB..JXCASKALLNEW
drop table TEMPDB..JXCASKALLNEW_1
drop table TEMPDB..JXCASKALLNEW_index
rollback transaction";
sqls = string.Format(sqls, Date, beginPeriod, endPeriod);
log.Info("YERP_001 sql:" + sqls);
DataTable vbsdt = ICSHelper.ExecuteTable(conStr, sqls);
log.Info("sql结果数量" + vbsdt.Rows.Count);
if (vbsdt.Rows.Count > 0)
{
StringBuilder soapRequestData = new StringBuilder();
soapRequestData.Append("");
soapRequestData.Append("");
soapRequestData.Append("");
soapRequestData.Append("");
//循环数据
foreach (DataRow itemRow in vbsdt.Rows)
{
//原循环部分 begin
soapRequestData.Append("");
soapRequestData.Append("" + Dates.ToString("yyyy-MM-dd") + "");
soapRequestData.Append(itemRow["INVT_NM"] != null ? "" + itemRow["INVT_NM"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["INVT_CD"] != null ? "" + itemRow["INVT_CD"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["BSC_QTY"] != null ? "" + itemRow["BSC_QTY"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["BSC_AMT"] != null ? "" + itemRow["BSC_AMT"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["PCHS_QTY"] != null ? "" + itemRow["PCHS_QTY"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["PCHS_AMT"] != null ? "" + itemRow["PCHS_AMT"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["ESTM_QTY"] != null ? "" + itemRow["ESTM_QTY"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["ESTM_AMT"] != null ? "" + itemRow["ESTM_AMT"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["ETC_WHG_QTY"] != null ? "" + itemRow["ETC_WHG_QTY"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["ETC_WHG_AMT"] != null ? "" + itemRow["ETC_WHG_AMT"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["ETC_RLS_QTY"] != null ? "" + itemRow["ETC_RLS_QTY"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["ETC_RLS_AMT"] != null ? "" + itemRow["ETC_RLS_AMT"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["MOVE_QTY"] != null ? "" + itemRow["MOVE_QTY"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["MOVE_AMT"] != null ? "" + itemRow["MOVE_AMT"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["SL_QTY"] != null ? "" + itemRow["SL_QTY"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["SL_RVN"] != null ? "" + itemRow["SL_RVN"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["SL_AMT"] != null ? "" + itemRow["SL_AMT"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["FRTX_AVRG_PRC"] != null ? "" + itemRow["FRTX_AVRG_PRC"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["SMTX_AVRG_PRC"] != null ? "" + itemRow["SMTX_AVRG_PRC"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["RLS_QTY"] != null ? "" + itemRow["RLS_QTY"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["SL_COST"] != null ? "" + itemRow["SL_COST"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["SLS_TOT_GAIN"] != null ? "" + itemRow["SLS_TOT_GAIN"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["SLS_TOT_PRTO"] != null ? "" + itemRow["SLS_TOT_PRTO"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["TE_QTY"] != null ? "" + itemRow["TE_QTY"].ToString() + "" : "" + "");
soapRequestData.Append(itemRow["TE_AMT"] != null ? "" + itemRow["TE_AMT"].ToString() + "" : "" + "");
soapRequestData.Append("");
//原循环部分 end
}
soapRequestData.Append("");
soapRequestData.Append("");
soapRequestData.Append("");
string postData = soapRequestData.ToString();
log.Info("YERP_001 xmlRequest:" + postData);
string statusCode;
string resultContent;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
RestOpHelper rop = new RestOpHelper();
rop.Clientp12path = @"C:\Users\Administrator\Desktop\yonyou-cert\wildcard.pfx";
rop.Clientp12PassWord = "Yonyou2024!";
rop.Url = @"https://yerp-proxy-sap-dev.skchemicals.com/XISOAPAdapter/MessageServlet?senderParty=&senderService=YERP_D&receiverParty=&receiverService=&interface=YERP_001_OA1_SI&interfaceNamespace=http://skch.com/YERP_001_INVENTORY_DATA";
rop.RequestSAP(rop.Url, 10000, postData, "IF_YERP", "Xtxc355860", out statusCode, out resultContent);
log.Info("YERP_001 statusCode:" + statusCode + "\r\n" + "resultContent:" + resultContent);
}
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
public static Configuration GetConfig()
{
Assembly assembly = Assembly.GetCallingAssembly();
string path = string.Format("{0}.config", assembly.Location);
if (!File.Exists(path))
{
throw new FileNotFoundException(path + "路径下的文件未找到!");
}
try
{
ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = path;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
return config;
}
catch (Exception)
{
throw;
}
}
}
}