private void SaveInvoiceFunction() { string ErrorMessage = ""; ErrorMessage = ValidateInvoice(); if (ErrorMessage.Length > 0) { MessageBox.Show("Incomplete / Invalid Invoice Details: \n" + ErrorMessage); } else { AccountsRecievableDao DAO = new AccountsRecievableDao(); if (!DAO.CustomerExist(txtCustomerName.Text)) { CustomerId = DAO.InsertCustomer( txtCustomerName.Text.ToString().Trim(), txtAddress.Text.ToString().Trim(), Convert.ToInt32(txtContactNumber.Text.ToString().Trim()) ); } else { var CustomerDetails = new Dictionary<string, object>(); CustomerDetails["ADDRESS"] = txtAddress.Text; CustomerDetails["CONTACT_NO"] = txtContactNumber.Text; DAO.UpdateQuery(Constants.TableNames.CUSTOMER, CustomerDetails, "CUSTOMER_ID", CustomerId.ToString()); } if (!DAO.PersonnelExist(txtSalesRepresentative.Text)) { PersonnelId = DAO.InsertPersonnel( txtSalesRepresentative.Text.ToString().Trim(), txtBranch.Text.ToString().Trim() ); } else { var PersonnelDetails = new Dictionary<string, object>(); PersonnelDetails["BRANCH"] = txtBranch.Text; DAO.UpdateQuery(Constants.TableNames.PERSONNEL, PersonnelDetails, "PERSONNEL_ID", PersonnelId.ToString()); } //update invoice var InvoiceDetails = new Dictionary<string, object>(); InvoiceDetails["ENTRY_DATE"] = pkrEntryDate.Value; InvoiceDetails["INVOICE_DATE"] = pkrInvoiceDate.Value; InvoiceDetails["PAYMENT_TERMS"] = txtPaymentTerms.Value; InvoiceDetails["RECEIVABLE_STATUS"] = GetRecievableStatusCode(ddlRecievableStatus.SelectedItem.ToString()); InvoiceDetails["DISCOUNT"] = txtInvoiceDiscount.Text; InvoiceDetails["DUE_DATE"] = pkrInvoiceDate.Value.AddDays(int.Parse(txtPaymentTerms.Value.ToString())); InvoiceDetails["REMARKS"] = txtRemarks.Text; InvoiceDetails["AMOUNT"] = txtInvoiceTotal.Text.Replace(",",""); InvoiceDetails["PERSONNEL_ID"] = PersonnelId; InvoiceDetails["CUSTOMER_ID"] = CustomerId; InvoiceDetails["DATE_UPDATED"] = DateTime.Now; InvoiceDetails["UPDATED_BY"] = DAO.GetUserId(UserId); DAO.UpdateQuery(Constants.TableNames.RECEIVABLES, InvoiceDetails, "INVOICE_NO", txtInvoiceNumber.Text); //update transactions DAO.ExecuteSQL("DELETE FROM " + Constants.TableNames.TRANSACTIONS + " WHERE INVOICE_ID = '" +InvoiceId.ToString()+ "'"); int InvoiceDiscount = 0; int ProductId = 0; int InvoiceItemDiscount = 0; int.TryParse(txtInvoiceDiscount.Text, out InvoiceDiscount); foreach (DataGridViewRow Row in InvoiceItems.Rows) { InvoiceDiscount = 0; if (Row.Cells[Constants.RowColumnCodeForAddInvoice.DISCOUNT].Value != null) { int.TryParse(Row.Cells[Constants.RowColumnCodeForAddInvoice.DISCOUNT].Value.ToString().Trim(), out InvoiceDiscount); } if (Row.Cells[1].Value != null) { if (!DAO.ItemExist(Row.Cells[Constants.RowColumnCodeForAddInvoice.ITEM_CODE].Value.ToString().Trim(), Convert.ToDecimal(Row.Cells[Constants.RowColumnCodeForAddInvoice.PRICE].Value.ToString().Trim()))) { ProductId = DAO.InsertItem(Row.Cells[Constants.RowColumnCodeForAddInvoice.ITEM_CODE].Value.ToString().Trim(), Row.Cells[Constants.RowColumnCodeForAddInvoice.BRAND].Value.ToString().Trim(), Convert.ToDecimal(Row.Cells[Constants.RowColumnCodeForAddInvoice.PRICE].Value.ToString().Trim())); } else { ProductId = Convert.ToInt32(DAO.SelectQuery("SELECT PRODUCT_ID FROM " + Constants.TableNames.PRODUCT + " WHERE PRODUCT_CODE ='" + Row.Cells[Constants.RowColumnCodeForAddInvoice.ITEM_CODE].Value.ToString().Trim() + "'").Rows[0][0].ToString()); } DAO.InsertTransaction(InvoiceId, ProductId, Convert.ToInt32(Row.Cells[Constants.RowColumnCodeForAddInvoice.QUANTITY].Value.ToString().Trim()), InvoiceDiscount); } } MessageBox.Show(this, "Invoice Saved.", "Success", MessageBoxButtons.OK); this.Close(); } }
private void SavePayments_Click(object sender, EventArgs e) { AccountsRecievableDao DAO = new AccountsRecievableDao(); string InvoiceTotal = txtInvoiceTotal.Text.Replace(",", ""); decimal PaymentTotal = 0; foreach (DataGridViewRow row in dgvPaymentDetails.Rows) { if (row.Cells[3].Value != null) { PaymentTotal += decimal.Parse(row.Cells[3].Value.ToString()); } } if (PaymentTotal >= decimal.Parse(InvoiceTotal)) { ddlRecievableStatus.Text = Constants.RecievableStatusDisplay.FULLY_PAID; DAO.ExecuteSQL("UPDATE "+Constants.TableNames.RECEIVABLES+" SET RECEIVABLE_STATUS='" + Constants.RecievableStatusCode.FULLY_PAID + "' WHERE INVOICE_ID = '" + InvoiceId + "'"); } else if (PaymentTotal > 0) { ddlRecievableStatus.Text = Constants.RecievableStatusDisplay.PARTIALLY_PAID; DAO.ExecuteSQL("UPDATE "+Constants.TableNames.RECEIVABLES+" SET RECEIVABLE_STATUS='" + Constants.RecievableStatusCode.PARTIALLY_PAID + "' WHERE INVOICE_ID = '" + InvoiceId + "'"); } else { ddlRecievableStatus.Text = Constants.RecievableStatusDisplay.UNPAID; DAO.ExecuteSQL("UPDATE "+Constants.TableNames.RECEIVABLES+" SET RECEIVABLE_STATUS='" + Constants.RecievableStatusCode.UNPAID + "' WHERE INVOICE_ID = '" + InvoiceId + "'"); } DAO.ExecuteSQL("DELETE FROM " + Constants.TableNames.PAYMENTS + " WHERE INVOICE_NO='" + txtInvoiceNumber.Text + "'"); foreach (DataGridViewRow row in dgvPaymentDetails.Rows) { string[] PaymentTypeExtract = row.Cells[2].Value.ToString().Split('-'); int PaymentType = GetPaymentTypeCode(PaymentTypeExtract[0].Trim()); string CheckNo = ""; string CheckDate = ""; if (PaymentType == Constants.PaymentTypeCode.CHECK) { CheckNo = PaymentTypeExtract[1].Trim(); CheckDate = PaymentTypeExtract[2].Trim(); } PaymentId = DAO.InsertPayment(int.Parse(txtInvoiceNumber.Text) ,DateTime.Parse( row.Cells[1].Value.ToString()) , PaymentType ,int.Parse(UserId) ,DateTime.Now ,int.Parse(UserId) ,row.Cells[4].Value.ToString().Replace(",","") ,DateTime.Now ,decimal.Parse(row.Cells[3].Value.ToString().Replace(",","")), CheckNo, String.IsNullOrEmpty(CheckDate) ? DateTime.MinValue : DateTime.ParseExact(CheckDate, "MMMM dd, yyyy", CultureInfo.InvariantCulture) ); } MessageBox.Show("Payment Successfully Saved."); }
private void DeleteInvoice_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show(this, "Are you sure you want to delete the invoice?", "Warning", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { AccountsRecievableDao DAO = new AccountsRecievableDao(); foreach(DataGridViewRow row in InvoiceItems.Rows) { if (row.Cells[7].Value != null) { //DAO.DeleteQuery(Constants.TableNames.TRANSACTIONS, row.Cells[7].Value.ToString()); DAO.ExecuteSQL("DELETE FROM TRANSACTIONS WHERE TRANSACTION_NO = " + row.Cells[7].Value.ToString()); } } DAO.ExecuteSQL("DELETE FROM RECEIVABLES WHERE INVOICE_ID = " + InvoiceId.ToString()); this.Close(); //DAO.DeleteQuery(Constants.TableNames.RECEIVABLES, InvoiceId.ToString()); } }