You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
16 KiB

6 months ago
  1. using ICSSoft.ERPWMS.Entity;
  2. using Microsoft.Data.SqlClient;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ICSSoft.ERPWMS.SQL
  11. {
  12. public class CreateVendorInventoryPrice
  13. {
  14. public static Result Create(List<CreateVendorInventoryPriceEntity> list)
  15. {
  16. Result res = new Result();
  17. try
  18. {
  19. string jsonstr = JsonConvert.SerializeObject(list);
  20. Log.WriteLogFile(jsonstr, "创建价格表日志");
  21. StringBuilder sb = new StringBuilder();//接口返回Message
  22. foreach (CreateVendorInventoryPriceEntity entity in list)
  23. {
  24. SqlConnection conn = new SqlConnection(ICSHelper.ReadConfig(ICSHelper.FileNameCompanyCon)["ERP"].ToString());
  25. conn.Open();
  26. SqlTransaction sqlTran = conn.BeginTransaction();
  27. SqlCommand cmd = new SqlCommand();
  28. cmd.Transaction = sqlTran;
  29. cmd.Connection = conn;
  30. try
  31. {
  32. if (string.IsNullOrWhiteSpace(entity.cVenCode))
  33. {
  34. throw new Exception("存在供应商编码为空!");
  35. }
  36. if (string.IsNullOrWhiteSpace(entity.cInvCode))
  37. {
  38. throw new Exception("物料编码为空!");
  39. }
  40. if (entity.bTaxCost == null)
  41. {
  42. throw new Exception("价格标识为空!");
  43. }
  44. if (entity.iSupplyType == null)
  45. {
  46. throw new Exception("供应类型为空!");
  47. }
  48. int iSupplyType = 1;
  49. if (entity.iSupplyType == 0)
  50. {
  51. iSupplyType = 2;
  52. }
  53. if (entity.iSupplyType == 2)
  54. {
  55. iSupplyType = 4;
  56. }
  57. if (string.IsNullOrWhiteSpace(entity.cExch_Name))
  58. {
  59. throw new Exception("币种为空!");
  60. }
  61. if (entity.dEnableDate == null)
  62. {
  63. throw new Exception("生效日期为空!");
  64. }
  65. if (entity.dDisableDate == null)
  66. {
  67. throw new Exception("失效日期为空!");
  68. }
  69. if (string.IsNullOrWhiteSpace(entity.WorkPoint))
  70. {
  71. throw new Exception("表头站点为空!");
  72. }
  73. string WorkPoint = ICSHelper.GetConnectStringTest(entity.WorkPoint);
  74. if (WorkPoint == "NotExit")
  75. {
  76. throw new Exception("站点编码不存在!");
  77. }
  78. //检查供应商编码是否存在
  79. string sqlVenCheck = "select cVenCode from {0}.dbo.Vendor where cVenCode='{1}'";
  80. sqlVenCheck = string.Format(sqlVenCheck, WorkPoint, entity.cVenCode);
  81. DataTable dtVenCheck = ICSHelper.SQlReturnData(sqlVenCheck, cmd);
  82. if (dtVenCheck.Rows.Count < 1)
  83. {
  84. throw new Exception("供应商编码不存在!");
  85. }
  86. //检查物料编码是否存在
  87. string sqlInvCheck = "select cInvCode from {0}.dbo.Inventory where cInvCode='{1}'";
  88. sqlInvCheck = string.Format(sqlInvCheck, WorkPoint, entity.cInvCode);
  89. DataTable dtInvCheck = ICSHelper.SQlReturnData(sqlInvCheck, cmd);
  90. if (dtInvCheck.Rows.Count < 1)
  91. {
  92. throw new Exception("物料编码不存在!");
  93. }
  94. if (entity.list.Count > 0)
  95. {
  96. List<CreateVendorInventoryPriceDetailEntity> listDownCheck = entity.list.Where(x => x.iLowerLimit == null).ToList();
  97. if (listDownCheck.Count > 0)
  98. {
  99. throw new Exception("表体数量下限为空");
  100. //sb.Append("表体数量下限为空,表头供应商编码为:" + entity.cVenCode + "!!!");
  101. //continue;
  102. }
  103. List<float?> listDown = entity.list.Select(x => x.iLowerLimit).Distinct().ToList();
  104. if (listDown.Count != entity.list.Count)
  105. {
  106. throw new Exception("表体数量下限存在重复");
  107. //sb.Append("表体数量下限存在重复,表头供应商编码为:" + entity.cVenCode + "!!!");
  108. //continue;
  109. }
  110. foreach (CreateVendorInventoryPriceDetailEntity detail in entity.list)
  111. {
  112. if (detail.iUpperLimit != null)
  113. {
  114. if (detail.iUpperLimit <= detail.iLowerLimit)
  115. {
  116. throw new Exception("表体数量下限大于数量上限");
  117. }
  118. }
  119. }
  120. List<CreateVendorInventoryPriceDetailEntity> listDownOrder = entity.list.OrderBy(x => x.iLowerLimit).ToList();
  121. float? UpLimit = null;
  122. foreach (CreateVendorInventoryPriceDetailEntity item in listDownOrder)
  123. {
  124. if (UpLimit != null)
  125. {
  126. if (item.iLowerLimit <= UpLimit)
  127. {
  128. throw new Exception("表体数量下限与数量上限存在交叉");
  129. }
  130. }
  131. if (item.iUpperLimit != null)
  132. {
  133. UpLimit = item.iUpperLimit;
  134. }
  135. else
  136. {
  137. UpLimit = null;
  138. }
  139. if (item.iUnitPrice == null)
  140. {
  141. throw new Exception("表体无税单价为空");
  142. }
  143. if (item.iTaxRate == null)
  144. {
  145. throw new Exception("表体税率为空");
  146. }
  147. if (item.iTaxUnitPrice == null)
  148. {
  149. throw new Exception("表体含税单价为空");
  150. }
  151. if (item.WorkPoint == null)
  152. {
  153. throw new Exception("表体站点为空");
  154. }
  155. }
  156. string UpdateSQL = "";
  157. //将上个版本全部失效
  158. string sqlLastCheck = @"Select Autoid,dEnableDate,dDisableDate from {0}.dbo.Ven_Inv_Price Where cVenCode ='{1}' and cInvCode ='{2}'
  159. and iSupplyType ='{3}' and (dDisableDate>='{4}' or dDisableDate is null)";//检查是否存在交叉失效日期
  160. sqlLastCheck = string.Format(sqlLastCheck, WorkPoint, entity.cVenCode, entity.cInvCode, iSupplyType, entity.dEnableDate);
  161. Log.WriteLogFile(sqlLastCheck, "创建价格表SQL日志");
  162. DataTable dtNew = ICSHelper.SQlReturnData(sqlLastCheck, cmd);
  163. if (dtNew != null && dtNew.Rows.Count > 0)
  164. {
  165. //将上个版本的失效日期改为当前生效日期的前一天
  166. foreach (DataRow dr in dtNew.Rows)
  167. {
  168. if (entity.dEnableDate <= Convert.ToDateTime(dr["dEnableDate"]))
  169. {
  170. UpdateSQL += " Update {0}.dbo.Ven_Inv_Price Set dDisableDate ='{1}',dEnableDate ='{1}' Where Autoid ='{2}';";
  171. UpdateSQL = string.Format(UpdateSQL, WorkPoint, Convert.ToDateTime(entity.dEnableDate).AddDays(-1), dr["Autoid"].ToString());
  172. }
  173. else
  174. {
  175. UpdateSQL += " Update {0}.dbo.Ven_Inv_Price Set dDisableDate ='{1}' Where Autoid ='{2}';";
  176. UpdateSQL = string.Format(UpdateSQL, WorkPoint, Convert.ToDateTime(entity.dEnableDate).AddDays(-1), dr["Autoid"].ToString());
  177. }
  178. }
  179. }
  180. foreach (CreateVendorInventoryPriceDetailEntity detail in entity.list)
  181. {
  182. if (detail.iUpperLimit == null)
  183. {
  184. UpdateSQL += @" Insert into {0}.dbo.Ven_Inv_Price(cVenCode,cInvCode,btaxcost,iSupplyType,cExch_Name,dEnableDate,dDisableDate,iLowerLimit,
  185. iUpperLimit,iUnitPrice,iTaxRate,iTaxUnitPrice,bPromotion) Values ('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}',null,'{9}','{10}','{11}',0);";
  186. UpdateSQL = string.Format(UpdateSQL, WorkPoint, entity.cVenCode, entity.cInvCode, entity.bTaxCost, iSupplyType, entity.cExch_Name,
  187. entity.dEnableDate, entity.dDisableDate, detail.iLowerLimit, detail.iUnitPrice, detail.iTaxRate, detail.iTaxUnitPrice);
  188. }
  189. else
  190. {
  191. UpdateSQL += @" Insert into {0}.dbo.Ven_Inv_Price(cVenCode,cInvCode,btaxcost,iSupplyType,cExch_Name,dEnableDate,dDisableDate,iLowerLimit,
  192. iUpperLimit,iUnitPrice,iTaxRate,iTaxUnitPrice,bPromotion) Values ('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}',0);";
  193. UpdateSQL = string.Format(UpdateSQL, WorkPoint, entity.cVenCode, entity.cInvCode, entity.bTaxCost, iSupplyType, entity.cExch_Name,
  194. entity.dEnableDate, entity.dDisableDate, detail.iLowerLimit, detail.iUpperLimit, detail.iUnitPrice, detail.iTaxRate, detail.iTaxUnitPrice);
  195. }
  196. }
  197. //if (iSupplyType == 1)
  198. //{
  199. // float? MaxiUnitPrice = entity.list.Select(x => x.iUnitPrice).Max();
  200. // decimal CkUnitPrice = (decimal)entity.list.Select(x => x.iUnitPrice).Max();
  201. // if (entity.cExch_Name != "人民币")
  202. // {
  203. // string Year = DateTime.Now.Year.ToString();
  204. // string Month = DateTime.Now.Month.ToString();
  205. // string sqlEx = "select nflat from {0}.dbo.exch where cexch_name ='" + entity.cExch_Name + "' and iYear ='" + Year + "' and iperiod='" + Month + "' and itype =2";
  206. // sqlEx = string.Format(sqlEx, WorkPoint);
  207. // DataTable dtEx = ICSHelper.SQlReturnData(sqlEx, cmd);
  208. // if (dtEx == null || dtEx.Rows.Count < 1)
  209. // {
  210. // throw new Exception("币种:" + entity.cExch_Name + ",在年份:" + Year + ",月份:" + Month + ",未维护汇率关系!");
  211. // }
  212. // MaxiUnitPrice = MaxiUnitPrice * float.Parse(dtEx.Rows[0]["nflat"].ToString());
  213. // CkUnitPrice = decimal.Round(CkUnitPrice * decimal.Parse(dtEx.Rows[0]["nflat"].ToString()), 2);
  214. // }
  215. // else
  216. // {
  217. // CkUnitPrice = decimal.Round(CkUnitPrice, 2);
  218. // }
  219. // UpdateSQL += " Update {0}.dbo.Inventory set cVenCode ='{1}',iInvRCost ='{3}',iInvSPrice ='{4}' Where cInvCode ='{2}'";
  220. // UpdateSQL = String.Format(UpdateSQL, WorkPoint, entity.cVenCode, entity.cInvCode, MaxiUnitPrice, CkUnitPrice);
  221. //}
  222. //else
  223. //{
  224. // UpdateSQL += " Update {0}.dbo.Inventory set cVenCode ='{1}' Where cInvCode ='{2}'";
  225. // UpdateSQL = String.Format(UpdateSQL, WorkPoint, entity.cVenCode, entity.cInvCode);
  226. //}
  227. UpdateSQL += " Update {0}.dbo.Inventory set cVenCode ='{1}' Where cInvCode ='{2}'";
  228. UpdateSQL = String.Format(UpdateSQL, WorkPoint, entity.cVenCode, entity.cInvCode);
  229. Log.WriteLogFile(UpdateSQL, "创建价格表SQL日志");
  230. if (!ICSHelper.ExecuteNonQuery(UpdateSQL, cmd))
  231. {
  232. throw new Exception("新增供应商料品价格表失败!");
  233. }
  234. cmd.Transaction.Commit();
  235. }
  236. else
  237. {
  238. throw new Exception("表体为空");
  239. }
  240. }
  241. catch (Exception ex)
  242. {
  243. cmd.Transaction.Rollback();
  244. sb.Append("执行报错,供应商编码为:" + entity.cVenCode + ",物料编码为:" + entity.cInvCode + ",报错信息:" + ex.Message + "!!!");
  245. continue;
  246. }
  247. finally
  248. {
  249. if (conn.State == ConnectionState.Open)
  250. {
  251. conn.Close();
  252. }
  253. conn.Dispose();
  254. }
  255. }
  256. if (sb.Length > 0)
  257. {
  258. res.IsSuccess = false;
  259. res.Message = sb.ToString();
  260. }
  261. else
  262. {
  263. res.IsSuccess = true;
  264. res.Message = "执行成功!";
  265. }
  266. return res;
  267. }
  268. catch (Exception ex)
  269. {
  270. res.IsSuccess = false;
  271. res.Message = ex.Message;
  272. return res;
  273. }
  274. }
  275. }
  276. }