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.

295 lines
15 KiB

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