public string InsertStock(StockHeader header, string User) { string err = ""; try { conn.BeginTransaction(); DataSet ds = new DataSet(); Int32 HeaderID = 0; bool chk = false; List <SqlParameter> param = new List <SqlParameter>(); param.Add(new SqlParameter() { ParameterName = "StockType", Value = header.StockType }); param.Add(new SqlParameter() { ParameterName = "StockTime", Value = header.StockTime }); param.Add(new SqlParameter() { ParameterName = "Remark", Value = header.Remark }); param.Add(new SqlParameter() { ParameterName = "CreatedBy", Value = User }); conn.CallStoredProcedure("InsStockHeader", param, out ds, out err); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && string.IsNullOrEmpty(err) && header.detail != null && header.detail.Count > 0) { HeaderID = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()); string cal = header.StockType == "IN" ? "+" : "-"; foreach (StockDetail item in header.detail) { param = new List <SqlParameter>(); param.Add(new SqlParameter() { ParameterName = "StockHeaderID", Value = HeaderID }); param.Add(new SqlParameter() { ParameterName = "ProductID", Value = item.ProductID, DbType = DbType.Int32 }); param.Add(new SqlParameter() { ParameterName = "Amount", Value = item.Amount, DbType = DbType.Int32 }); param.Add(new SqlParameter() { ParameterName = "CAL", Value = cal }); param.Add(new SqlParameter() { ParameterName = "SerialNumber", Value = item.SNConcat }); conn.CallStoredProcedure("InsStockDetail", param, out err); if (item.lstSerial != null && item.lstSerial.Count > 0) { foreach (TransProductSerial tps in item.lstSerial) { ds = new DataSet(); param = new List <SqlParameter>(); param.Add(new SqlParameter() { ParameterName = "ProductID", Value = item.ProductID, DbType = DbType.Int32 }); param.Add(new SqlParameter() { ParameterName = "SerialNumber", Value = tps.SerialNumber }); param.Add(new SqlParameter() { ParameterName = "CAL", Value = cal }); conn.CallStoredProcedure("InsTransProductSerial", param, out ds, out err); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null) { err = err + tps.SerialNumber + ", "; chk = true; } } if (chk) { err = "ไม่พบ S/N หมายเลข : " + err.Substring(0, err.Length - 2); } } } } if (string.IsNullOrEmpty(err)) { conn.Commit(); } else { conn.RollBack(); } } catch (Exception ex) { err = ex.Message; } return(err); }
public string InsertSaleHeader(TransSaleHeader o, List <SaleDetailDTO> lstDetail) { string error = ""; try { DataSet ds = new DataSet(); List <SqlParameter> param = new List <SqlParameter>(); Int32 HeaderID = 0, DetailID = 0; conn.BeginTransaction(); #region Header Params param.Add(new SqlParameter() { ParameterName = "tel", Value = o.Tel, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "CustomerName", Value = o.CustomerName, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "CustomerAddress", Value = o.CustomerAddress, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "CustomerDistrict", Value = o.CustomerDistrict, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "CustomerCountry", Value = o.CustomerCountry, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "CustomerProvince", Value = o.CustomerProvince, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "CustomerPostalCode", Value = o.CustomerPostalCode, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "DeliverName", Value = o.DeliveryName, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "DeliverAddress", Value = o.DeliverAdd, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "DeliverDistrict", Value = o.DeliverDistrict, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "DeliverCountry", Value = o.DeliverCountry, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "DeliverProvince", Value = o.DeliverProvince, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "DeliverPostalCode", Value = o.DeliverPostalCode, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "ReceivedDate", Value = o.ReceivedDate, DbType = DbType.DateTime }); param.Add(new SqlParameter() { ParameterName = "ReceivedBy", Value = o.ReceivedBy, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "SaleNumber", Value = o.SaleNumber, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "Remark", Value = o.Remark, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "ConsignmentNo", Value = o.ConsignmentNo, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "COD", Value = o.COD, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "WarrantyDate", Value = o.WarrantyDate, DbType = DbType.DateTime }); param.Add(new SqlParameter() { ParameterName = "BillType", Value = o.BillType, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "PayType", Value = o.PayType, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "SaleName", Value = o.SaleName, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "AccountTransfer", Value = o.AccountTransfer, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "TimeTransfer", Value = o.TimeTransfer, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "Installment", Value = o.Installment, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "Active", Value = o.Active, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "CreatedBy", Value = o.CreatedBy, DbType = DbType.String }); #endregion conn.CallStoredProcedure("InsTransSaleHeader", param, out ds, out error); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0 && string.IsNullOrEmpty(error)) { HeaderID = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()); if (lstDetail != null && lstDetail.Count > 0) { Int32 TransID = 0; string tid = ""; string[] tidArr; foreach (SaleDetailDTO item in lstDetail) { param = new List <SqlParameter>(); param.Add(new SqlParameter() { ParameterName = "HeaderID", Value = HeaderID, DbType = DbType.Int32 }); param.Add(new SqlParameter() { ParameterName = "ItemID", Value = item.ItemID, DbType = DbType.Int32 }); param.Add(new SqlParameter() { ParameterName = "ItemPrice", Value = item.ItemPrice, DbType = DbType.Decimal }); param.Add(new SqlParameter() { ParameterName = "Amount", Value = item.Amount, DbType = DbType.Decimal }); param.Add(new SqlParameter() { ParameterName = "Discount", Value = item.Discount, DbType = DbType.Decimal }); param.Add(new SqlParameter() { ParameterName = "SerialNumber", Value = item.SerialNumber, DbType = DbType.String }); param.Add(new SqlParameter() { ParameterName = "ItemDetail", Value = item.ItemDescription, DbType = DbType.String }); conn.CallStoredProcedure("InsTransSaleDetail", param, out ds, out error); #region Status = 'N' on SerialNumber tid = item.SNID; if (!string.IsNullOrEmpty(tid) && string.IsNullOrEmpty(error)) { tidArr = tid.Split(','); if (tidArr != null && tidArr.Length > 0) { foreach (string s in tidArr) { TransID = Convert.ToInt32(s); param = new List <SqlParameter>(); param.Add(new SqlParameter() { ParameterName = "TransID", Value = TransID, DbType = DbType.Int32 }); conn.CallStoredProcedure("UpdTransProductSerialStatus", param, out error); } } } #endregion if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0 && item.ProductDetail != null && item.ProductDetail.Count > 0 && string.IsNullOrEmpty(error)) { DetailID = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()); foreach (MasPackageDetail pd in item.ProductDetail) { param = new List <SqlParameter>(); param.Add(new SqlParameter() { ParameterName = "SaleDetailID", Value = DetailID, DbType = DbType.Int32 }); param.Add(new SqlParameter() { ParameterName = "ProductID", Value = pd.ProductID, DbType = DbType.Int32 }); param.Add(new SqlParameter() { ParameterName = "Amount", Value = pd.Amount, DbType = DbType.Int32 }); param.Add(new SqlParameter() { ParameterName = "IsFree", Value = pd.IsFree }); conn.CallStoredProcedure("InsTransSaleDetailProduct", param, out error); } } } } } if (string.IsNullOrEmpty(error)) { conn.Commit(); } else { conn.RollBack(); } } catch (Exception ex) { error = ex.Message; } return(error); }