public void CreateStock(ref SqlConnection connection, ref SqlTransaction transaction, DataTable dataTable, string wh, string storetype, bool isbonusqty, bool isreturn) { var ta = new DsInventoryTableAdapters.STOCKMASTERTableAdapter(); var ds = new DsInventory().STOCKMASTER; ta.Connection = connection; ta.Transaction = transaction; foreach (DataRow d in dataTable.Rows) { if (d.RowState != DataRowState.Deleted) { int qty = 0; if (isbonusqty) { if (!string.IsNullOrEmpty(d["BONUSQTY"].ToString())) { if (!string.IsNullOrEmpty(d["QTY"].ToString())) { qty = int.Parse(d["QTY"].ToString()) + int.Parse(d["BONUSQTY"].ToString()); } else { qty = int.Parse(d["BONUSQTY"].ToString()); } } else { qty = int.Parse(d["QTY"].ToString()); } } else { qty = int.Parse(d["QTY"].ToString()); } int cnt = 0; // var avgcost = GetAvgCprice(d["ITEMNO"].ToString()); int negtivestock = ta.FillByItemcodeWithNegativeStock(ds, d["ITEMNO"].ToString(), storetype, wh, Utils.Company); if (negtivestock == 0) { cnt = !string.IsNullOrEmpty(d["EXPDATE"].ToString()) ? ta.FillByStockIDForUpdate(ds, d["ITEMNO"].ToString(), DateTime.Parse(d["EXPDATE"].ToString()), storetype, wh, Utils.Company,decimal.Parse(d["SPRICE"].ToString())) : ta.FillByStockIDForUpdateNullDate(ds, d["ITEMNO"].ToString(), storetype, wh, Utils.Company,decimal.Parse(d["SPRICE"].ToString())); } else { cnt = negtivestock; } if (isreturn) { qty = -1 * qty; } if (cnt > 0) { var d1 = ds.Rows[0]; bool isneg = false; foreach (DsInventory.STOCKMASTERRow d2 in ds.Rows) { if (d2.QTY < 0) { isneg = true; d1 = d2; } if (d2.QTY > 0 && !isneg) d1 = d2; } ta.UpdateQtyByStockID(qty, int.Parse(d1["STOCKID"].ToString())); if(negtivestock > 0) { ta.UpdateSPRICE(decimal.Parse(d["SPRICE"].ToString()), int.Parse(d1["STOCKID"].ToString())); } ta.UpdateSaleQtyByStockid(qty, int.Parse(d1["STOCKID"].ToString())); int qty2 = 0; decimal cp1 = 0; foreach (DataRow costrow in ds.Rows) { qty2 += int.Parse(costrow["QTY"].ToString()); if (!String.IsNullOrEmpty(costrow["CPRICE"].ToString())) cp1 = decimal.Parse(costrow["CPRICE"].ToString()); } if (qty2 < 0) qty2 *= -1; if ((qty + qty2) > 0) { var newavgcp = ((qty*decimal.Parse(d["CPRICE"].ToString())) + (qty2*cp1))/(qty + qty2); foreach (DataRow costrow in ds.Rows) { ta.UpdateCPRICE(newavgcp, int.Parse(costrow["STOCKID"].ToString())); } } } else { if (!isreturn) { if (string.IsNullOrEmpty(d["EXPDATE"].ToString())) { ta.Insert(d["ITEMNO"].ToString(), d["BARCODE"].ToString(), qty, decimal.Parse(d["SPRICE"].ToString()), decimal.Parse(d["CPRICE"].ToString()), null, storetype, null, null, null, false, wh,Utils.Company, qty,null); } else { ta.Insert(d["ITEMNO"].ToString(), d["BARCODE"].ToString(), qty, decimal.Parse(d["SPRICE"].ToString()), decimal.Parse(d["CPRICE"].ToString()), DateTime.Parse(d["EXPDATE"].ToString()), storetype, null, null, null, false, wh, Utils.Company, qty, null); } } } } } }