هل يمكنك شرح عمل هذه الدالة بالتفصيل    // MrSales.MrSControls.StockHandling
public static decimal ReduceStocksWithMultiCombinations(int ItemID, int pitem_ID, int PurchasesID, int vendorID, string VID, int ItemImdex, int Store, decimal PurchasesPrice, decimal PurchasesPrice_beforeDisc, decimal unitQntity, decimal Bounce, DateTime expireDate, string BatchNum, string SourceType, bool ReverseStart, bool ReverseSold)
{
	decimal num = unitQntity + Bounce;
	using (mrsalesdbEntities mrsalesdbEntities = ConnectionTools.OpenConn())
	{
		if (mrsalesdbEntities.st_items.Find(new object[]
		{
			ItemID
		}).stitems_Type.Value == 0)
		{
			if (pitem_ID != 0)
			{
				if (num > 0m)
				{
					mrsalesdbEntities mrsalesdbEntities2 = ConnectionTools.OpenConn();
					List<stock_noserials> list = (from u in mrsalesdbEntities2.stock_noserials
					where u.stitems_ID == (int?)ItemID && u.StoreID == (int?)Store && u.ExpireDate == (DateTime?)expireDate && u.purchases_price_withoutDiscOrXtra == (decimal?)PurchasesPrice_beforeDisc && u.purchases_price == (decimal?)PurchasesPrice && u.purchasesID == (int?)PurchasesID && u.PurchasesIds == SourceType && u.purchases_ItemID == (int?)pitem_ID && u.StockQnty > (decimal?)((decimal)0)
					select u into o
					orderby o.id
					select o).ToList<stock_noserials>();
					bool flag;
					if (list.Count<stock_noserials>() > 0)
					{
						decimal? num2 = list.Sum(new Func<stock_noserials, decimal?>(StockHandling.<>c.<>c_0.qiGvItyEhRe));
						decimal d = 0m;
						flag = (num2.GetValueOrDefault() > d & num2 != null);
					}
					else
					{
						flag = false;
					}
					if (flag)
					{
						foreach (stock_noserials stock_noserials in list)
						{
							decimal? num2 = stock_noserials.StockQnty;
							decimal d = num;
							if ((num2.GetValueOrDefault() >= d & num2 != null) && num != 0m)
							{
								decimal value = stock_noserials.StockQnty.Value;
								if (ReverseStart)
								{
									stock_noserials stock_noserials2 = stock_noserials;
									num2 = stock_noserials2.StartQnt;
									d = num;
									stock_noserials2.StartQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
								}
								stock_noserials.StockQnty -= num;
								if (ReverseSold)
								{
									stock_noserials stock_noserials3 = stock_noserials;
									num2 = stock_noserials3.SoldQnt;
									d = num;
									stock_noserials3.SoldQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() + d));
								}
								mrsalesdbEntities2.SaveChanges();
								decimal num3 = -1m * num;
								num -= num;
								decimal value2 = stock_noserials.StockQnty.Value;
								LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have Full qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
								{
									PurchasesID,
									stock_noserials.id,
									value,
									num3,
									value2
								}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_1 " + SourceType, "Functions.cs", (value + num3 == value2) ? "INFO" : "ERORR: #-THE EQUATION IS WRONG-#", 0, null);
								break;
							}
							num2 = stock_noserials.StockQnty;
							d = num;
							if ((num2.GetValueOrDefault() < d & num2 != null) && num != 0m)
							{
								decimal num4 = Convert.ToDecimal(stock_noserials.StockQnty);
								decimal value3 = stock_noserials.StockQnty.Value;
								if (ReverseStart)
								{
									stock_noserials stock_noserials4 = stock_noserials;
									num2 = stock_noserials4.StartQnt;
									d = num4;
									stock_noserials4.StartQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
								}
								if (ReverseSold)
								{
									stock_noserials.SoldQnt += num4;
								}
								stock_noserials.StockQnty = new decimal?(0m);
								mrsalesdbEntities2.SaveChanges();
								decimal num5 = -1m * num4;
								num -= num4;
								decimal value4 = stock_noserials.StockQnty.Value;
								LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have Only Part of the qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
								{
									PurchasesID,
									stock_noserials.id,
									value3,
									num5,
									value4
								}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_2 " + SourceType, "Functions.cs", (value3 + num5 == value4) ? "INFO" : "ERORR: #-THE EQUATION IS WRONG-#", 0, null);
							}
						}
					}
				}
				if (num != 0m && pitem_ID != 0)
				{
					mrsalesdbEntities mrsalesdbEntities3 = ConnectionTools.OpenConn();
					List<stock_noserials> list2 = (from u in mrsalesdbEntities3.stock_noserials
					where u.stitems_ID == (int?)ItemID && u.purchasesID == (int?)PurchasesID && u.PurchasesIds == SourceType && u.purchases_ItemID == (int?)pitem_ID && u.StockQnty > (decimal?)((decimal)0)
					select u into o
					orderby o.id
					select o).ToList<stock_noserials>();
					bool flag2;
					if (list2.Count<stock_noserials>() > 0)
					{
						decimal? num2 = list2.Sum(new Func<stock_noserials, decimal?>(StockHandling.<>c.<>c_0.method_0));
						decimal d = 0m;
						flag2 = (num2.GetValueOrDefault() > d & num2 != null);
					}
					else
					{
						flag2 = false;
					}
					if (flag2)
					{
						foreach (stock_noserials stock_noserials5 in list2)
						{
							decimal? num2 = stock_noserials5.StockQnty;
							decimal d = num;
							if ((num2.GetValueOrDefault() >= d & num2 != null) && num != 0m)
							{
								decimal value5 = stock_noserials5.StockQnty.Value;
								decimal num6 = -1m * num;
								if (ReverseStart)
								{
									stock_noserials5.StartQnt -= num;
								}
								stock_noserials stock_noserials6 = stock_noserials5;
								num2 = stock_noserials6.StockQnty;
								d = num;
								stock_noserials6.StockQnty = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
								if (ReverseSold)
								{
									stock_noserials5.SoldQnt += num;
								}
								mrsalesdbEntities3.SaveChanges();
								num -= num;
								decimal value6 = stock_noserials5.StockQnty.Value;
								LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have a full of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
								{
									PurchasesID,
									stock_noserials5.id,
									value5,
									num6,
									value6
								}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_3 ", "Functions.cs", (value5 + num == value6) ? "INFO" : "ERORR: #-THE EQUATION IS WRONG-#", 0, null);
								break;
							}
							num2 = stock_noserials5.StockQnty;
							d = num;
							if ((num2.GetValueOrDefault() < d & num2 != null) && num != 0m)
							{
								decimal num7 = Convert.ToDecimal(stock_noserials5.StockQnty);
								decimal value7 = stock_noserials5.StockQnty.Value;
								-1m * num7;
								if (ReverseStart)
								{
									stock_noserials5.StartQnt -= num7;
								}
								stock_noserials5.StockQnty = new decimal?(0m);
								if (ReverseSold)
								{
									stock_noserials stock_noserials7 = stock_noserials5;
									num2 = stock_noserials7.SoldQnt;
									d = num7;
									stock_noserials7.SoldQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() + d));
								}
								mrsalesdbEntities3.SaveChanges();
								num -= num7;
								decimal value8 = stock_noserials5.StockQnty.Value;
								LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have Part only of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
								{
									PurchasesID,
									stock_noserials5.id,
									value7,
									num7,
									value8
								}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_4 ", "Functions.cs", (!(value7 + num == value8)) ? "ERORR: #-THE EQUATION IS WRONG-#" : "INFO", 0, null);
							}
						}
					}
				}
			}
			if (PurchasesID != 0)
			{
				if (num != 0m)
				{
					mrsalesdbEntities mrsalesdbEntities4 = ConnectionTools.OpenConn();
					List<stock_noserials> list3 = (from u in mrsalesdbEntities4.stock_noserials
					where u.stitems_ID == (int?)ItemID && u.StoreID == (int?)Store && u.ExpireDate == (DateTime?)expireDate && u.purchases_price_withoutDiscOrXtra == (decimal?)PurchasesPrice_beforeDisc && u.purchases_price == (decimal?)PurchasesPrice && u.purchasesID == (int?)PurchasesID && u.PurchasesIds == SourceType && u.StockQnty > (decimal?)((decimal)0)
					select u into o
					orderby o.id
					select o).ToList<stock_noserials>();
					bool flag3;
					if (list3.Count<stock_noserials>() <= 0)
					{
						flag3 = false;
					}
					else
					{
						decimal? num2 = list3.Sum(new Func<stock_noserials, decimal?>(StockHandling.<>c.<>c_0.method_1));
						decimal d = 0m;
						flag3 = (num2.GetValueOrDefault() > d & num2 != null);
					}
					if (flag3)
					{
						foreach (stock_noserials stock_noserials8 in list3)
						{
							decimal? num2 = stock_noserials8.StockQnty;
							decimal d = num;
							if ((num2.GetValueOrDefault() >= d & num2 != null) && num != 0m)
							{
								decimal value9 = stock_noserials8.StockQnty.Value;
								decimal num8 = -1m * num;
								if (ReverseStart)
								{
									stock_noserials stock_noserials9 = stock_noserials8;
									num2 = stock_noserials9.StartQnt;
									d = num;
									stock_noserials9.StartQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
								}
								stock_noserials8.StockQnty -= num;
								if (ReverseSold)
								{
									stock_noserials8.SoldQnt += num;
								}
								mrsalesdbEntities4.SaveChanges();
								num -= num;
								decimal value10 = stock_noserials8.StockQnty.Value;
								LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have a full of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
								{
									PurchasesID,
									stock_noserials8.id,
									value9,
									num8,
									value10
								}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_5 ", "Functions.cs", (value9 + num == value10) ? "INFO" : "ERORR: #-THE EQUATION IS WRONG-#", 0, null);
								break;
							}
							num2 = stock_noserials8.StockQnty;
							d = num;
							if ((num2.GetValueOrDefault() < d & num2 != null) && num != 0m)
							{
								decimal d2 = Convert.ToDecimal(stock_noserials8.StockQnty);
								decimal value11 = stock_noserials8.StockQnty.Value;
								decimal num9 = -1m * d2;
								if (ReverseStart)
								{
									stock_noserials8.StartQnt -= d2;
								}
								stock_noserials8.StockQnty = new decimal?(0m);
								if (ReverseSold)
								{
									stock_noserials8.SoldQnt += d2;
								}
								mrsalesdbEntities4.SaveChanges();
								num -= d2;
								decimal value12 = stock_noserials8.StockQnty.Value;
								LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have Part only of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
								{
									PurchasesID,
									stock_noserials8.id,
									value11,
									num9,
									value12
								}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_6 ", "Functions.cs", (value11 + num == value12) ? "INFO" : "ERORR: #-THE EQUATION IS WRONG-#", 0, null);
							}
						}
					}
				}
				if (num != 0m && PurchasesID != 0)
				{
					mrsalesdbEntities mrsalesdbEntities5 = ConnectionTools.OpenConn();
					List<stock_noserials> list4 = (from u in mrsalesdbEntities5.stock_noserials
					where u.stitems_ID == (int?)ItemID && u.purchasesID == (int?)PurchasesID && u.PurchasesIds == SourceType && u.StockQnty > (decimal?)((decimal)0)
					select u into o
					orderby o.id
					select o).ToList<stock_noserials>();
					bool flag4;
					if (list4.Count<stock_noserials>() <= 0)
					{
						flag4 = false;
					}
					else
					{
						decimal? num2 = list4.Sum(new Func<stock_noserials, decimal?>(StockHandling.<>c.<>c_0.method_2));
						decimal d = 0m;
						flag4 = (num2.GetValueOrDefault() > d & num2 != null);
					}
					if (flag4)
					{
						foreach (stock_noserials stock_noserials10 in list4)
						{
							decimal? num2 = stock_noserials10.StockQnty;
							decimal d = num;
							if ((num2.GetValueOrDefault() >= d & num2 != null) && num != 0m)
							{
								decimal value13 = stock_noserials10.StockQnty.Value;
								decimal num10 = -1m * num;
								if (ReverseStart)
								{
									stock_noserials stock_noserials11 = stock_noserials10;
									num2 = stock_noserials11.StartQnt;
									d = num;
									stock_noserials11.StartQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
								}
								stock_noserials stock_noserials12 = stock_noserials10;
								num2 = stock_noserials12.StockQnty;
								d = num;
								stock_noserials12.StockQnty = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
								if (ReverseSold)
								{
									stock_noserials10.SoldQnt += num;
								}
								mrsalesdbEntities5.SaveChanges();
								num -= num;
								decimal value14 = stock_noserials10.StockQnty.Value;
								LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have a full qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
								{
									PurchasesID,
									stock_noserials10.id,
									value13,
									num10,
									value14
								}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_7 ", "Functions.cs", (value13 + num == value14) ? "INFO" : "ERORR: #-THE EQUATION IS WRONG-#", 0, null);
								break;
							}
							num2 = stock_noserials10.StockQnty;
							d = num;
							if ((num2.GetValueOrDefault() < d & num2 != null) && num != 0m)
							{
								decimal num11 = Convert.ToDecimal(stock_noserials10.StockQnty);
								decimal value15 = stock_noserials10.StockQnty.Value;
								decimal num12 = -1m * num11;
								if (ReverseStart)
								{
									stock_noserials stock_noserials13 = stock_noserials10;
									num2 = stock_noserials13.StartQnt;
									d = num11;
									stock_noserials13.StartQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
								}
								stock_noserials10.StockQnty = new decimal?(0m);
								if (ReverseSold)
								{
									stock_noserials10.SoldQnt += num11;
								}
								mrsalesdbEntities5.SaveChanges();
								num -= num11;
								decimal value16 = stock_noserials10.StockQnty.Value;
								LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have Part only of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
								{
									PurchasesID,
									stock_noserials10.id,
									value15,
									num12,
									value16
								}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_8 ", "Functions.cs", (value15 + num == value16) ? "INFO" : "ERORR: #-THE EQUATION IS WRONG-#", 0, null);
							}
						}
					}
				}
			}
			if (num != 0m)
			{
				mrsalesdbEntities mrsalesdbEntities6 = ConnectionTools.OpenConn();
				List<stock_noserials> list5 = (from u in mrsalesdbEntities6.stock_noserials
				where u.stitems_ID == (int?)ItemID && u.StoreID == (int?)Store && u.ExpireDate == (DateTime?)expireDate && u.purchases_price_withoutDiscOrXtra == (decimal?)PurchasesPrice_beforeDisc && u.purchases_price == (decimal?)PurchasesPrice && (u.purchases_ItemID == null || u.purchases_ItemID == (int?)0) && u.StockQnty > (decimal?)((decimal)0)
				select u into o
				orderby o.id
				select o).ToList<stock_noserials>();
				bool flag5;
				if (list5.Count<stock_noserials>() <= 0)
				{
					flag5 = false;
				}
				else
				{
					decimal? num2 = list5.Sum(new Func<stock_noserials, decimal?>(StockHandling.<>c.<>c_0.method_3));
					decimal d = 0m;
					flag5 = (num2.GetValueOrDefault() > d & num2 != null);
				}
				if (flag5)
				{
					foreach (stock_noserials stock_noserials14 in list5)
					{
						decimal? num2 = stock_noserials14.StockQnty;
						decimal d = num;
						if ((num2.GetValueOrDefault() >= d & num2 != null) && num != 0m)
						{
							decimal value17 = stock_noserials14.StockQnty.Value;
							decimal num13 = -1m * num;
							if (ReverseStart)
							{
								stock_noserials14.StartQnt -= num;
							}
							stock_noserials14.StockQnty -= num;
							if (ReverseSold)
							{
								stock_noserials14.SoldQnt += num;
							}
							mrsalesdbEntities6.SaveChanges();
							num -= num;
							decimal value18 = stock_noserials14.StockQnty.Value;
							LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have a full of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
							{
								PurchasesID,
								stock_noserials14.id,
								value17,
								num13,
								value18
							}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_9 ", "Functions.cs", (value17 + num == value18) ? "INFO" : "ERORR: #-THE EQUATION IS WRONG-#", 0, null);
							break;
						}
						num2 = stock_noserials14.StockQnty;
						d = num;
						if ((num2.GetValueOrDefault() < d & num2 != null) && num != 0m)
						{
							decimal d3 = Convert.ToDecimal(stock_noserials14.StockQnty);
							decimal value19 = stock_noserials14.StockQnty.Value;
							decimal num14 = -1m * d3;
							if (ReverseStart)
							{
								stock_noserials14.StartQnt -= d3;
							}
							stock_noserials14.StockQnty = new decimal?(0m);
							if (ReverseSold)
							{
								stock_noserials14.SoldQnt += d3;
							}
							mrsalesdbEntities6.SaveChanges();
							num -= d3;
							decimal value20 = stock_noserials14.StockQnty.Value;
							LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have a full of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
							{
								PurchasesID,
								stock_noserials14.id,
								value19,
								num14,
								value20
							}), ItemID, MethodBase.GetCurrentMethod().Name + "LOG_10 ", "Functions.cs", (!(value19 + num == value20)) ? "ERORR: #-THE EQUATION IS WRONG-#" : "INFO", 0, null);
						}
					}
				}
			}
			if (num > 0m)
			{
				mrsalesdbEntities mrsalesdbEntities7 = ConnectionTools.OpenConn();
				List<stock_noserials> list6 = (from u in mrsalesdbEntities7.stock_noserials
				where u.stitems_ID == (int?)ItemID && u.StoreID == (int?)Store && u.ExpireDate == (DateTime?)expireDate && u.purchases_price == (decimal?)PurchasesPrice && (u.purchases_ItemID == null || u.purchases_ItemID == (int?)0) && u.StockQnty > (decimal?)((decimal)0)
				select u into o
				orderby o.id
				select o).ToList<stock_noserials>();
				foreach (stock_noserials stock_noserials15 in list6)
				{
					decimal? num2 = stock_noserials15.StockQnty;
					decimal d = num;
					if ((num2.GetValueOrDefault() >= d & num2 != null) && num != 0m)
					{
						decimal value21 = stock_noserials15.StockQnty.Value;
						decimal num15 = -1m * num;
						if (ReverseStart)
						{
							stock_noserials15.StartQnt -= num;
						}
						stock_noserials stock_noserials16 = stock_noserials15;
						num2 = stock_noserials16.StockQnty;
						d = num;
						stock_noserials16.StockQnty = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
						if (ReverseSold)
						{
							stock_noserials stock_noserials17 = stock_noserials15;
							num2 = stock_noserials17.SoldQnt;
							d = num;
							stock_noserials17.SoldQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() + d));
						}
						mrsalesdbEntities7.SaveChanges();
						num -= num;
						decimal value22 = stock_noserials15.StockQnty.Value;
						LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have a full of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
						{
							PurchasesID,
							stock_noserials15.id,
							value21,
							num15,
							value22
						}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_11 ", "Functions.cs", (!(value21 + num == value22)) ? "ERORR: #-THE EQUATION IS WRONG-#" : "INFO", 0, null);
						break;
					}
					num2 = stock_noserials15.StockQnty;
					d = num;
					if ((num2.GetValueOrDefault() < d & num2 != null) && num != 0m)
					{
						decimal num16 = Convert.ToDecimal(stock_noserials15.StockQnty);
						decimal value23 = stock_noserials15.StockQnty.Value;
						decimal num17 = -1m * num16;
						if (ReverseStart)
						{
							stock_noserials15.StartQnt -= num16;
						}
						stock_noserials15.StockQnty = new decimal?(0m);
						if (ReverseSold)
						{
							stock_noserials stock_noserials18 = stock_noserials15;
							num2 = stock_noserials18.SoldQnt;
							d = num16;
							stock_noserials18.SoldQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() + d));
						}
						mrsalesdbEntities7.SaveChanges();
						num -= num16;
						decimal value24 = stock_noserials15.StockQnty.Value;
						LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have a full of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
						{
							PurchasesID,
							stock_noserials15.id,
							value23,
							num17,
							value24
						}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_12 ", "Functions.cs", (!(value23 + num == value24)) ? "ERORR: #-THE EQUATION IS WRONG-#" : "INFO", 0, null);
					}
				}
			}
			if (num > 0m)
			{
				mrsalesdbEntities mrsalesdbEntities8 = ConnectionTools.OpenConn();
				List<stock_noserials> list7 = (from u in mrsalesdbEntities8.stock_noserials
				where u.stitems_ID == (int?)ItemID && u.StoreID == (int?)Store && u.purchases_price == (decimal?)PurchasesPrice && (u.purchases_ItemID == null || u.purchases_ItemID == (int?)0) && u.StockQnty > (decimal?)((decimal)0)
				select u into o
				orderby o.id
				select o).ToList<stock_noserials>();
				foreach (stock_noserials stock_noserials19 in list7)
				{
					decimal? num2 = stock_noserials19.StockQnty;
					decimal d = num;
					if ((num2.GetValueOrDefault() >= d & num2 != null) && num != 0m)
					{
						decimal value25 = stock_noserials19.StockQnty.Value;
						decimal num18 = -1m * num;
						if (ReverseStart)
						{
							stock_noserials stock_noserials20 = stock_noserials19;
							num2 = stock_noserials20.StartQnt;
							d = num;
							stock_noserials20.StartQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
						}
						stock_noserials stock_noserials21 = stock_noserials19;
						num2 = stock_noserials21.StockQnty;
						d = num;
						stock_noserials21.StockQnty = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
						if (ReverseSold)
						{
							stock_noserials19.SoldQnt += num;
						}
						mrsalesdbEntities8.SaveChanges();
						num -= num;
						decimal value26 = stock_noserials19.StockQnty.Value;
						LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have a full of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
						{
							PurchasesID,
							stock_noserials19.id,
							value25,
							num18,
							value26
						}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_13 ", "Functions.cs", (!(value25 + num == value26)) ? "ERORR: #-THE EQUATION IS WRONG-#" : "INFO", 0, null);
						break;
					}
					num2 = stock_noserials19.StockQnty;
					d = num;
					if ((num2.GetValueOrDefault() < d & num2 != null) && num != 0m)
					{
						decimal num19 = Convert.ToDecimal(stock_noserials19.StockQnty);
						decimal value27 = stock_noserials19.StockQnty.Value;
						decimal num20 = -1m * num19;
						if (ReverseStart)
						{
							stock_noserials stock_noserials22 = stock_noserials19;
							num2 = stock_noserials22.StartQnt;
							d = num19;
							stock_noserials22.StartQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
						}
						stock_noserials19.StockQnty = new decimal?(0m);
						if (ReverseSold)
						{
							stock_noserials19.SoldQnt += num19;
						}
						mrsalesdbEntities8.SaveChanges();
						num -= num19;
						decimal value28 = stock_noserials19.StockQnty.Value;
						LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have a full of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
						{
							PurchasesID,
							stock_noserials19.id,
							value27,
							num20,
							value28
						}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_14 ", "Functions.cs", (value27 + num == value28) ? "INFO" : "ERORR: #-THE EQUATION IS WRONG-#", 0, null);
					}
				}
			}
			if (num > 0m)
			{
				mrsalesdbEntities mrsalesdbEntities9 = ConnectionTools.OpenConn();
				List<stock_noserials> list8 = (from u in mrsalesdbEntities9.stock_noserials
				where u.stitems_ID == (int?)ItemID && u.StoreID == (int?)Store && u.StockQnty > (decimal?)((decimal)0)
				select u into o
				orderby o.id
				select o).ToList<stock_noserials>();
				foreach (stock_noserials stock_noserials23 in list8)
				{
					decimal? num2 = stock_noserials23.StockQnty;
					decimal d = num;
					if ((num2.GetValueOrDefault() >= d & num2 != null) && num != 0m)
					{
						decimal value29 = stock_noserials23.StockQnty.Value;
						decimal num21 = -1m * num;
						if (ReverseStart)
						{
							stock_noserials23.StartQnt -= num;
						}
						stock_noserials23.StockQnty -= num;
						if (ReverseSold)
						{
							stock_noserials23.SoldQnt += num;
						}
						mrsalesdbEntities9.SaveChanges();
						num -= num;
						decimal value30 = stock_noserials23.StockQnty.Value;
						LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have a full of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
						{
							PurchasesID,
							stock_noserials23.id,
							value29,
							num21,
							value30
						}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_15 ", "Functions.cs", (value29 + num == value30) ? "INFO" : "ERORR: #-THE EQUATION IS WRONG-#", 0, null);
						break;
					}
					num2 = stock_noserials23.StockQnty;
					d = num;
					if ((num2.GetValueOrDefault() < d & num2 != null) && num != 0m)
					{
						decimal num22 = Convert.ToDecimal(stock_noserials23.StockQnty);
						decimal value31 = stock_noserials23.StockQnty.Value;
						decimal num23 = -1m * num22;
						if (ReverseStart)
						{
							stock_noserials23.StartQnt -= num22;
						}
						stock_noserials23.StockQnty = new decimal?(0m);
						if (ReverseSold)
						{
							stock_noserials stock_noserials24 = stock_noserials23;
							num2 = stock_noserials24.SoldQnt;
							d = num22;
							stock_noserials24.SoldQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() + d));
						}
						mrsalesdbEntities9.SaveChanges();
						num -= num22;
						decimal value32 = stock_noserials23.StockQnty.Value;
						LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have a full of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
						{
							PurchasesID,
							stock_noserials23.id,
							value31,
							num23,
							value32
						}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_16 ", "Functions.cs", (value31 + num == value32) ? "INFO" : "ERORR: #-THE EQUATION IS WRONG-#", 0, null);
					}
				}
			}
			if (num > 0m)
			{
				mrsalesdbEntities mrsalesdbEntities10 = ConnectionTools.OpenConn();
				List<stock_noserials> list9 = (from u in mrsalesdbEntities10.stock_noserials
				where u.stitems_ID == (int?)ItemID && u.purchases_price == (decimal?)PurchasesPrice && (u.purchases_ItemID == null || u.purchases_ItemID == (int?)0) && u.StockQnty > (decimal?)((decimal)0)
				select u into o
				orderby o.id
				select o).ToList<stock_noserials>();
				foreach (stock_noserials stock_noserials25 in list9)
				{
					decimal? num2 = stock_noserials25.StockQnty;
					decimal d = num;
					if ((num2.GetValueOrDefault() >= d & num2 != null) && num != 0m)
					{
						decimal value33 = stock_noserials25.StockQnty.Value;
						decimal num24 = -1m * num;
						if (ReverseStart)
						{
							stock_noserials stock_noserials26 = stock_noserials25;
							num2 = stock_noserials26.StartQnt;
							d = num;
							stock_noserials26.StartQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
						}
						stock_noserials25.StockQnty -= num;
						if (ReverseSold)
						{
							stock_noserials stock_noserials27 = stock_noserials25;
							num2 = stock_noserials27.SoldQnt;
							d = num;
							stock_noserials27.SoldQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() + d));
						}
						mrsalesdbEntities10.SaveChanges();
						num -= num;
						decimal value34 = stock_noserials25.StockQnty.Value;
						LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have a full of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
						{
							PurchasesID,
							stock_noserials25.id,
							value33,
							num24,
							value34
						}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_17 ", "Functions.cs", (!(value33 + num == value34)) ? "ERORR: #-THE EQUATION IS WRONG-#" : "INFO", 0, null);
						break;
					}
					num2 = stock_noserials25.StockQnty;
					d = num;
					if ((num2.GetValueOrDefault() < d & num2 != null) && num != 0m)
					{
						decimal num25 = Convert.ToDecimal(stock_noserials25.StockQnty);
						decimal value35 = stock_noserials25.StockQnty.Value;
						decimal num26 = -1m * num25;
						if (ReverseStart)
						{
							stock_noserials stock_noserials28 = stock_noserials25;
							num2 = stock_noserials28.StartQnt;
							d = num25;
							stock_noserials28.StartQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
						}
						stock_noserials25.StockQnty = new decimal?(0m);
						if (ReverseSold)
						{
							stock_noserials stock_noserials29 = stock_noserials25;
							num2 = stock_noserials29.SoldQnt;
							d = num25;
							stock_noserials29.SoldQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() + d));
						}
						mrsalesdbEntities10.SaveChanges();
						num -= num25;
						decimal value36 = stock_noserials25.StockQnty.Value;
						LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have Part only of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
						{
							PurchasesID,
							stock_noserials25.id,
							value35,
							num26,
							value36
						}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_18 ", "Functions.cs", (!(value35 + num == value36)) ? "ERORR: #-THE EQUATION IS WRONG-#" : "INFO", 0, null);
					}
				}
			}
			if (num > 0m)
			{
				mrsalesdbEntities mrsalesdbEntities11 = ConnectionTools.OpenConn();
				List<stock_noserials> list10 = (from u in mrsalesdbEntities11.stock_noserials
				where u.stitems_ID == (int?)ItemID && u.purchases_price == (decimal?)PurchasesPrice && (u.purchases_ItemID == null || u.purchases_ItemID == (int?)0) && u.StockQnty > (decimal?)((decimal)0)
				select u into o
				orderby o.id
				select o).ToList<stock_noserials>();
				foreach (stock_noserials stock_noserials30 in list10)
				{
					decimal? num2 = stock_noserials30.StockQnty;
					decimal d = num;
					if ((num2.GetValueOrDefault() >= d & num2 != null) && num != 0m)
					{
						decimal value37 = stock_noserials30.StockQnty.Value;
						decimal num27 = -1m * num;
						if (ReverseStart)
						{
							stock_noserials30.StartQnt -= num;
						}
						stock_noserials stock_noserials31 = stock_noserials30;
						num2 = stock_noserials31.StockQnty;
						d = num;
						stock_noserials31.StockQnty = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
						if (ReverseSold)
						{
							stock_noserials stock_noserials32 = stock_noserials30;
							num2 = stock_noserials32.SoldQnt;
							d = num;
							stock_noserials32.SoldQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() + d));
						}
						mrsalesdbEntities11.SaveChanges();
						num -= num;
						decimal value38 = stock_noserials30.StockQnty.Value;
						LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have a full of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
						{
							PurchasesID,
							stock_noserials30.id,
							value37,
							num27,
							value38
						}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_19 ", "Functions.cs", (!(value37 + num == value38)) ? "ERORR: #-THE EQUATION IS WRONG-#" : "INFO", 0, null);
						break;
					}
					num2 = stock_noserials30.StockQnty;
					d = num;
					if ((num2.GetValueOrDefault() < d & num2 != null) && num != 0m)
					{
						decimal num28 = Convert.ToDecimal(stock_noserials30.StockQnty);
						decimal value39 = stock_noserials30.StockQnty.Value;
						decimal num29 = -1m * num28;
						if (ReverseStart)
						{
							stock_noserials stock_noserials33 = stock_noserials30;
							num2 = stock_noserials33.StartQnt;
							d = num28;
							stock_noserials33.StartQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
						}
						stock_noserials30.StockQnty = new decimal?(0m);
						if (ReverseSold)
						{
							stock_noserials30.SoldQnt += num28;
						}
						mrsalesdbEntities11.SaveChanges();
						num -= num28;
						decimal value40 = stock_noserials30.StockQnty.Value;
						LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have Part only of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
						{
							PurchasesID,
							stock_noserials30.id,
							value39,
							num29,
							value40
						}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_20 ", "Functions.cs", (value39 + num == value40) ? "INFO" : "ERORR: #-THE EQUATION IS WRONG-#", 0, null);
					}
				}
			}
			if (num > 0m)
			{
				mrsalesdbEntities mrsalesdbEntities12 = ConnectionTools.OpenConn();
				List<stock_noserials> list11 = (from u in mrsalesdbEntities12.stock_noserials
				where u.stitems_ID == (int?)ItemID && u.StockQnty > (decimal?)((decimal)0)
				select u into o
				orderby o.id
				select o).ToList<stock_noserials>();
				foreach (stock_noserials stock_noserials34 in list11)
				{
					decimal? num2 = stock_noserials34.StockQnty;
					decimal d = num;
					if ((num2.GetValueOrDefault() >= d & num2 != null) && num != 0m)
					{
						decimal value41 = stock_noserials34.StockQnty.Value;
						decimal num30 = -1m * num;
						if (ReverseStart)
						{
							stock_noserials stock_noserials35 = stock_noserials34;
							num2 = stock_noserials35.StartQnt;
							d = num;
							stock_noserials35.StartQnt = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
						}
						stock_noserials stock_noserials36 = stock_noserials34;
						num2 = stock_noserials36.StockQnty;
						d = num;
						stock_noserials36.StockQnty = ((num2 == null) ? null : new decimal?(num2.GetValueOrDefault() - d));
						if (ReverseSold)
						{
							stock_noserials34.SoldQnt += num;
						}
						mrsalesdbEntities12.SaveChanges();
						num -= num;
						decimal value42 = stock_noserials34.StockQnty.Value;
						LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have a full of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
						{
							PurchasesID,
							stock_noserials34.id,
							value41,
							num30,
							value42
						}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_21 ", "Functions.cs", (!(value41 + num == value42)) ? "ERORR: #-THE EQUATION IS WRONG-#" : "INFO", 0, null);
						break;
					}
					num2 = stock_noserials34.StockQnty;
					d = num;
					if ((num2.GetValueOrDefault() < d & num2 != null) && num != 0m)
					{
						decimal d4 = Convert.ToDecimal(stock_noserials34.StockQnty);
						decimal value43 = stock_noserials34.StockQnty.Value;
						decimal num31 = -1m * d4;
						if (ReverseStart)
						{
							stock_noserials34.StartQnt -= d4;
						}
						stock_noserials34.StockQnty = new decimal?(0m);
						if (ReverseSold)
						{
							stock_noserials34.SoldQnt += d4;
						}
						mrsalesdbEntities12.SaveChanges();
						num -= d4;
						decimal value44 = stock_noserials34.StockQnty.Value;
						LogWriter.Log(string.Format("INVOICE-ID:{0} , stock have Part only of qnty : STOCK-ID: {1}, PreStock: {2}, Qnts: {3}, NewqStock: {4} ", new object[]
						{
							PurchasesID,
							stock_noserials34.id,
							value43,
							num31,
							value44
						}), ItemID, MethodBase.GetCurrentMethod().Name + " LOG_22 ", "Functions.cs", (value43 + num == value44) ? "INFO" : "ERORR: #-THE EQUATION IS WRONG-#", 0, null);
					}
				}
			}
			if (num > 0m)
			{
				LogWriter.errLog(string.Concat(new object[]
				{
					SourceType,
					"-ID : ",
					PurchasesID,
					"-ItemID : ",
					pitem_ID,
					" : Reduce Stock Stocks , There are not enough Stock from this istem:",
					ItemID
				}), null);
			}
		}
	}
	return num;
}