public List<DeliveryInvoice> GetClientDeliveryInvoices(int clientId) { var con = new SqlConnection(_connectionString); var delInvoices = new List<DeliveryInvoice>(); using (con) { con.Open(); const string sqlDelivery = "Select * from DeliveryInvoices where ClientId=@cid"; var cmdDelivery = new SqlCommand(sqlDelivery, con); cmdDelivery.Parameters.AddWithValue("cid", clientId); var rdrDelivery = cmdDelivery.ExecuteReader(); using (rdrDelivery) { while (rdrDelivery.Read()) { var receipt = new DeliveryInvoice { Date = Convert.ToDateTime(rdrDelivery["Date"]), IndexNumber = Convert.ToInt32(rdrDelivery["IndexNumber"]), TotalAmount = Convert.ToDouble(rdrDelivery["TotalAmount"]), NetAmount = Convert.ToDouble(rdrDelivery["NetAmount"]), VatAmount = Convert.ToDouble(rdrDelivery["VatAmount"]), IsPrinted = Convert.ToBoolean(rdrDelivery["IsPrinted"]), VatPercent = Convert.ToInt32(rdrDelivery["VatPercent"]), ReceiptDescription = rdrDelivery["ReceiptDescription"].ToString(), PlaceOfDelivery = rdrDelivery["PlaceOfDelivery"].ToString(), PlaceOfOrigin = rdrDelivery["PlaceOfOrigin"].ToString(), ReceiptType = 3 }; delInvoices.Add(receipt); } } foreach (var inv in delInvoices) { const string sqlSelectItems = "Select * from DeliveryInvoiceItems where DeliveryInvoiceId=@invoiceid"; var cmdSelectItems = new SqlCommand(sqlSelectItems, con); cmdSelectItems.Parameters.AddWithValue("@invoiceid", inv.IndexNumber); var rdrSelectItems = cmdSelectItems.ExecuteReader(); inv.DeliveryInvoiceItems = new List<DeliveryInvoiceItem>(); using (rdrSelectItems) { while (rdrSelectItems.Read()) { var item = new DeliveryInvoiceItem { Description = rdrSelectItems["Description"].ToString(), Id = Convert.ToInt32(rdrSelectItems["Id"]), InvoiceId = Convert.ToInt32(rdrSelectItems["DeliveryInvoiceId"]), Quantity = Convert.ToInt32(rdrSelectItems["Quantity"]), UnitPrice = Convert.ToDouble(rdrSelectItems["UnitPrice"]) }; inv.DeliveryInvoiceItems.Add(item); } } } } return delInvoices; }
public DeliveryInvoice GetDeliveryInvoice(int deliveryInvoiceId) { var receipt = new DeliveryInvoice(); var con = new SqlConnection(_connectionString); using (con) { con.Open(); var sql = "Select * from DeliveryInvoices where IndexNumber=@rid"; var cmd = new SqlCommand(sql, con); cmd.Parameters.AddWithValue("@rid", deliveryInvoiceId); var rdr = cmd.ExecuteReader(); using (rdr) { while (rdr.Read()) { receipt.ClientId = Convert.ToInt32(rdr["ClientId"].ToString()); receipt.Date = Convert.ToDateTime(rdr["Date"].ToString()); receipt.IndexNumber = Convert.ToInt32(rdr["IndexNumber"]); receipt.TotalAmount = Convert.ToDouble(rdr["TotalAmount"]); receipt.NetAmount = Convert.ToDouble(rdr["NetAmount"]); receipt.VatAmount = Convert.ToDouble(rdr["VatAmount"]); receipt.VatPercent = Convert.ToInt32(rdr["VatPercent"]); receipt.IsPrinted = Convert.ToBoolean(rdr["IsPrinted"]); receipt.ReceiptDescription = rdr["ReceiptDescription"].ToString(); } } var sqlItems = "Select * from DeliveryInvoiceItems where DeliveryInvoiceId=@id"; var cmdItems = new SqlCommand(sqlItems, con); cmdItems.Parameters.AddWithValue("@id", deliveryInvoiceId); var rdrItems = cmdItems.ExecuteReader(); var items = new List<DeliveryInvoiceItem>(); using (rdrItems) { while (rdrItems.Read()) { items.Add(new DeliveryInvoiceItem { Description = rdrItems["Description"].ToString(), Quantity = Convert.ToInt32(rdrItems["Quantity"]), UnitPrice = Convert.ToDouble(rdrItems["UnitPrice"]) }); } } receipt.DeliveryInvoiceItems = items; } return receipt; }