public static void CreateNewInvoice(int ConsumerID, string dateInvoice, string dateSell, string datePayment, string paymentForm, string alreadyPaid, DataGridView articlesList) { MainProgram.Connection = DatabaseMySQL.Connect(MainProgram.DatabaseName); try { DatabaseMySQL.OpenConnection(MainProgram.Connection); CreateNumberForNewInvoice(); // tworzy numer faktury string queryText = string.Format("INSERT INTO faktura SET "); queryText += string.Format("numer='{0}', id_kontrahenta={1}, id_pracownika={2}, ", MainProgram.EditedInvoiceNumber, ConsumerID, MainProgram.LoggeUserID); queryText += string.Format("data_wystawienia='{0}', termin_platnosci='{1}', forma_platnosci='{2}', zaplacona_kwota={3}", dateInvoice, datePayment, paymentForm, alreadyPaid); MySqlCommand query = new MySqlCommand(queryText, MainProgram.Connection); query.ExecuteNonQuery(); for (int i = 0; i < articlesList.RowCount; i++) { DataGridViewRow row = articlesList.Rows[i]; queryText = string.Format("INSERT INTO pozycja_faktury SET "); queryText += string.Format("kod_artykulu='{0}', nr_faktury='{1}', ilosc={2}, rabat={3}, cena={4}", row.Cells[0].Value, MainProgram.EditedInvoiceNumber, row.Cells[6].Value, row.Cells[5].Value, row.Cells[2].Value); query = new MySqlCommand(queryText, MainProgram.Connection); query.ExecuteNonQuery(); } } catch (Exception e) { MessageBox.Show(e.Message); } finally { DatabaseMySQL.CloseConnection(MainProgram.Connection); } }
public static void LoadInvoicesList() { MainProgram.Connection = DatabaseMySQL.Connect(MainProgram.DatabaseName); try { DatabaseMySQL.OpenConnection(MainProgram.Connection); string queryText = string.Format("SELECT f.id,numer,id_pracownika, k.nazwa,concat(p.imie,' ',p.nazwisko),ifnull(nip,'-'),ROUND(sum(pf.cena*ilosc),2), ROUND(sum(pf.cena*ilosc)-zaplacona_kwota,2),data_wystawienia,termin_platnosci,data_sprzedazy,forma_platnosci,id_kontrahenta,zaplacona_kwota FROM faktura f, kontrahent k, pracownik p, pozycja_faktury pf WHERE (id_kontrahenta = k.id and id_pracownika = p.id and numer=pf.nr_faktury) GROUP BY numer HAVING (numer LIKE '%%')"); MySqlCommand query = new MySqlCommand(queryText, MainProgram.Connection); MySqlDataReader dataReader = query.ExecuteReader(); while (dataReader.Read()) { string[] cols = new string[dataReader.FieldCount]; for (int i = 0; i < dataReader.FieldCount; i++) { cols[i] = dataReader[i].ToString(); } MainProgram.InvoiceObjectsList.Add(new Invoice(cols)); } dataReader.Close(); } catch (Exception e) { MessageBox.Show(e.Message); } finally { DatabaseMySQL.CloseConnection(MainProgram.Connection); } }
private void buttonAddArticleToInvoice_Click(object sender, EventArgs e) { /// ======== TO DO -> potrzebny obiekt artykułu! =============== /// ============================================================ /// ============================================================ FormArticles addingArticleToInvoice_Window = new FormArticles(); string[] addedArticleData = addingArticleToInvoice_Window.ShowWindowToAddNewArticle(); if (addedArticleData == null) // no data receive { return; } string productCode = addedArticleData[0]; string productAmount = addedArticleData[1]; string productDiscount = addedArticleData[2]; if (!productCodesOnInvoice.Contains(productCode)) { MainProgram.Connection = DatabaseMySQL.Connect(MainProgram.DatabaseName); try { DatabaseMySQL.OpenConnection(MainProgram.Connection); string[] productData = DatabaseMySQL.GetArticleData(productCode); string productName = productData[0]; string productPriceNetto = productData[1]; string productVAT = productData[2]; dataGridView1.Rows.Add(productCode, productName, productPriceNetto, productVAT, "*", productDiscount, productAmount, "szt.", "=", "="); productCodesOnInvoice.Add(productCode); } catch (Exception exc) { MessageBox.Show(exc.Message); } finally { DatabaseMySQL.CloseConnection(MainProgram.Connection); } } else { MessageBox.Show("Ten produkt występuje już na fakturze i nie można go dodać ponownie.\nWybierz element na liście, a następnie dokonaj stosownych poprawek."); } }
public static string CreateNumberForNewInvoice() { // Generowanie kolejnego numeru dla nowej faktury. string lastInvoiceNumber = "-1"; string[] lastInvoiceNumberParts = { "" }; MainProgram.Connection = DatabaseMySQL.Connect(MainProgram.DatabaseName); try { DatabaseMySQL.OpenConnection(MainProgram.Connection); string queryText = string.Format("SELECT numer from faktura WHERE id=(SELECT MAX(id) FROM faktura)"); MySqlCommand query = new MySqlCommand(queryText, MainProgram.Connection); MySqlDataReader dataReader = query.ExecuteReader(); dataReader.Read(); lastInvoiceNumber = dataReader[0].ToString(); lastInvoiceNumberParts = lastInvoiceNumber.Split('/'); if (lastInvoiceNumberParts[1] == DateTime.Now.Month.ToString("00") && lastInvoiceNumberParts[2] == DateTime.Now.Year.ToString()) { lastInvoiceNumberParts[0] = (Convert.ToInt16(lastInvoiceNumberParts[0]) + 1).ToString(); } else { lastInvoiceNumberParts[0] = "1"; } dataReader.Close(); } catch (Exception exc) { MessageBox.Show("Unable to create new number for invoice: " + exc.Message); } finally { DatabaseMySQL.CloseConnection(MainProgram.Connection); } return(String.Format("{0}/{1}/{2}", lastInvoiceNumberParts[0], DateTime.Now.Month.ToString("00"), DateTime.Now.Year.ToString())); }
//public static string[] GetInvoiceData(string invoiceNumber) //{ // // NIE WIEM CZY POTRZEBNE [?] // string[] data = null; // try // { // string queryText = string.Format("SELECT * from faktura WHERE numer='{0}'", invoiceNumber); // MySqlCommand query = new MySqlCommand(queryText, MainProgram.Connection); // MySqlDataReader dataReader = query.ExecuteReader(); // dataReader.Read(); // data = new string[dataReader.FieldCount]; // for (int i = 0; i < dataReader.FieldCount; i++) // { // data[i] = dataReader[i].ToString(); // } // dataReader.Close(); // } // catch (Exception e) // { // MessageBox.Show(e.Message); // } // return data; //} public static int ExecuteQuery(string query) { // POZWALA ZREALIZOWAĆ DOWOLNE ZAPYTANIE WYSŁANE JAKO TEKST. // Uwaga: zwracana wartość to tylko ilość zmodyfikowanych krotek int n = -1; MainProgram.Connection = DatabaseMySQL.Connect(MainProgram.DatabaseName); try { DatabaseMySQL.OpenConnection(MainProgram.Connection); MySqlCommand command = new MySqlCommand(query, MainProgram.Connection); n = command.ExecuteNonQuery(); } catch (Exception exc) { MessageBox.Show(exc.Message); } finally { DatabaseMySQL.CloseConnection(MainProgram.Connection); } return(n); // zwraca liczbę zmodyfikowanych rekordów. }