public void AddInvoiceProducts(InvoiceProduct invoiceProduct)
 {
     Db.ExecuteNonQuery("usp_Invoice_InsertInvoiceProducts", CommandType.StoredProcedure,
         new DbParameter[] {
                        Db.CreateParameter("InvoiceId", invoiceProduct.InvoiceId),
                        Db.CreateParameter("ProductId", invoiceProduct.ProductId),
                        Db.CreateParameter("ProductName", invoiceProduct.ProductName),
                        Db.CreateParameter("Description", invoiceProduct.Description),
                        Db.CreateParameter("Quantity", invoiceProduct.Quantity),
                        Db.CreateParameter("UnitPrice", invoiceProduct.UnitPrice),
                        Db.CreateParameter("TotalPrice", invoiceProduct.TotalPrice),
                        Db.CreateParameter("TaxId", invoiceProduct.TaxId),
                        Db.CreateParameter("TaxValue", invoiceProduct.TaxValue)
          });
 }
 public void UpdateInvoiceProducts(InvoiceProduct invoiceProduct)
 {
     this.invoiceDBObj.UpdateInvoiceProducts(invoiceProduct);
 }
        private void btnSaveInvoice_Click(object sender, EventArgs e)
        {
            try
            {
                int invoiceId = string.IsNullOrEmpty(txtInvoiceNumber.Text) ? 0 : Convert.ToInt32(txtInvoiceNumber.Text);
                var invoiceDetails = invoiceService.GetInvoice(invoiceId);
                var comapnyDetails = (from company in companyService.GetAllCompany()
                                     .Where(v => v.Status == true)
                                      select company).SingleOrDefault();

                Invoice invoice = new Invoice();
                invoice.InvoiceId = invoiceId;
                invoice.CompanyId = comapnyDetails.CompanyId;
                invoice.ClientId = ClientId;
                invoice.IssueDate = dtpIssueDate.Value.Date;
                invoice.PurchaseOrderNo = string.IsNullOrEmpty(txtPONumber.Text) ? String.Empty : txtPONumber.Text;
                invoice.PaymentTermId = Convert.ToInt32(cmbPaymentTerms.SelectedValue);
                invoice.DueDate = dtpDueDate.Value.Date;
                invoice.Discount = string.IsNullOrEmpty(txtDiscount.Text) ? decimal.Zero : Convert.ToDecimal(txtDiscount.Text);
                invoice.RoundOffTotal = chkRoundOff.Checked;
                invoice.MarkInvoicePaid = chkMarkInvoicePaid.Checked;

                if (chkMarkInvoicePaid.Checked)
                {
                    invoice.PaymentTypeId = Convert.ToInt32(cmbPaymentType.SelectedValue);
                    invoice.AmountPaid = string.IsNullOrEmpty(txtAmountPaid.Text) ? decimal.Zero : Convert.ToDecimal(txtAmountPaid.Text);

                    if (invoiceDetails == null || invoiceDetails.InvoiceId == 0)
                    {
                        invoice.PaymentDate = DateTime.Now.Date;
                    }
                    else
                    {
                        if (invoiceDetails.AmountPaid > 0)
                        {
                            if (invoiceDetails.AmountPaid == invoice.AmountPaid)
                            {
                                invoice.PaymentDate = invoiceDetails.PaymentDate;
                            }
                            else
                            {
                                invoice.PaymentDate = DateTime.Now.Date;
                            }
                        }
                    }
                    string paymentStatus = String.Empty;

                    if (invoice.AmountPaid == 0)
                    {
                        paymentStatus = ePaymentStatus.Unpaid.ToString();
                    }
                    else if (invoice.AmountPaid > 0)
                    {
                        if (invoice.AmountPaid < TotalValue)
                        {
                            paymentStatus = ePaymentStatus.Partial.ToString();
                        }
                        else if (invoice.AmountPaid == TotalValue)
                        {
                            paymentStatus = ePaymentStatus.Paid.ToString();
                        }
                        else
                        {
                            paymentStatus = ePaymentStatus.Paid.ToString();
                        }
                    }

                    if (invoice.PaymentDate.Date > invoice.DueDate.Date)
                    {
                        if (String.IsNullOrEmpty(paymentStatus))
                        {
                            paymentStatus = String.Concat(paymentStatus, ",", ePaymentStatus.Overdue.ToString());
                        }
                        else
                        {
                            paymentStatus = ePaymentStatus.Overdue.ToString();
                        }
                    }

                    invoice.PaymentStatus = paymentStatus;
                }
                else
                {
                    invoice.PaymentTypeId = 0;
                    invoice.AmountPaid = decimal.Zero;
                }

                invoice.Notes = string.IsNullOrEmpty(txtNotes.Text) ? String.Empty : txtNotes.Text;
                invoice.TotalAmount = TotalValue;
                invoice.NotesForClient = string.IsNullOrEmpty(txtNoteForClient.Text) ? String.Empty : txtNoteForClient.Text;
                invoice.PrivateNotes = string.IsNullOrEmpty(txtprivateNotes.Text) ? String.Empty : txtprivateNotes.Text;
                invoice.Status = true;

                if (invoiceDetails == null || invoiceDetails.InvoiceId == 0)
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        invoiceService.AddInvoice(invoice);
                        lstInvoiceProduct = lstInvoiceProduct.OrderBy(s => s.InvoiceProductId).ToList<InvoiceProduct>();

                        for (int i = 0; i < lstInvoiceProduct.Count; i++)
                        {
                            InvoiceProduct invoiceProduct = new InvoiceProduct();
                            invoiceProduct.InvoiceId = lstInvoiceProduct[i].InvoiceId;
                            invoiceProduct.ProductId = lstInvoiceProduct[i].ProductId;
                            invoiceProduct.ProductName = lstInvoiceProduct[i].ProductName;
                            invoiceProduct.Description = lstInvoiceProduct[i].Description;
                            invoiceProduct.Quantity = lstInvoiceProduct[i].Quantity;
                            invoiceProduct.TaxId = lstInvoiceProduct[i].TaxId;
                            invoiceProduct.TaxValue = lstInvoiceProduct[i].TaxValue;
                            invoiceProduct.UnitPrice = lstInvoiceProduct[i].UnitPrice;
                            invoiceProduct.TotalPrice = lstInvoiceProduct[i].TotalPrice;
                            invoiceService.AddInvoiceProducts(invoiceProduct);
                        }

                        scope.Complete();
                    }
                }
                else
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        invoiceService.UpdateInvoice(invoice);

                        for (int i = 0; i < lstInvoiceProduct.Count; i++)
                        {
                            InvoiceProduct invoiceProduct = new InvoiceProduct();
                            invoiceProduct.InvoiceId = lstInvoiceProduct[i].InvoiceId;
                            invoiceProduct.ProductId = lstInvoiceProduct[i].ProductId;
                            invoiceProduct.ProductName = lstInvoiceProduct[i].ProductName;
                            invoiceProduct.Description = lstInvoiceProduct[i].Description;
                            invoiceProduct.Quantity = lstInvoiceProduct[i].Quantity;
                            invoiceProduct.TaxId = lstInvoiceProduct[i].TaxId;
                            invoiceProduct.TaxValue = lstInvoiceProduct[i].TaxValue;
                            invoiceProduct.UnitPrice = lstInvoiceProduct[i].UnitPrice;
                            invoiceProduct.TotalPrice = lstInvoiceProduct[i].TotalPrice;
                            invoiceService.UpdateInvoiceProducts(invoiceProduct);
                        }

                        scope.Complete();
                    }
                }

                CustomMessageBox.Show(string.Format(Constants.SUCCESSFULL_ADD_INVOICE_MESSAGE, invoice.InvoiceId),
                                                              Constants.CONSTANT_INFORMATION,
                                                              Sleek_Bill.Controls.CustomMessageBox.eDialogButtons.OK,
                                                              CustomImages.GetDialogImage(Sleek_Bill.Controls.CustomImages.eCustomDialogImages.Success));
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Saving invoice: " + ex.Message);
            }
        }
 public void AddInvoiceProducts(InvoiceProduct invoiceProduct)
 {
     this.invoiceDBObj.AddInvoiceProducts(invoiceProduct);
 }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            List<Tax> lstTax = masterService.GetAllTaxes();
            invoiceProduct = new InvoiceProduct();
            invoiceProduct.InvoiceProductId = lstInvoiceProduct.Count;
            invoiceProduct.InvoiceId = String.IsNullOrEmpty(txtInvoiceNumber.Text) ? 0 : Convert.ToInt32(txtInvoiceNumber.Text);
            invoiceProduct.ProductId = Convert.ToInt32(cmbProduct.SelectedValue);
            invoiceProduct.ProductName = cmbProduct.Text.Trim();
            invoiceProduct.Description = txtDescription.Text.Trim();
            invoiceProduct.Quantity = String.IsNullOrEmpty(txtQuantity.Text) ? 0 : Convert.ToInt32(txtQuantity.Text);
            invoiceProduct.UnitPrice = String.IsNullOrEmpty(txtUnitPrice.Text) ? decimal.Zero : Math.Round(Convert.ToDecimal(txtUnitPrice.Text), 2);
            invoiceProduct.TotalPrice = Math.Round(invoiceProduct.Quantity * invoiceProduct.UnitPrice, 2);
            invoiceProduct.TaxId = Convert.ToInt32(cmbTax.SelectedValue);
            var objTax = (from tax in lstTax
                      .Where(v => v.TaxId == invoiceProduct.TaxId)
                          select tax).SingleOrDefault();

            invoiceProduct.TaxValue = Math.Round((invoiceProduct.Quantity * invoiceProduct.UnitPrice) * objTax.TaxPercentage * (decimal)0.01, 2);
            lstInvoiceProduct.Add(invoiceProduct);
            BindInvoiceProductGrid();
            ClearProductControls();
            CalculateInvoice();
        }