コード例 #1
0
ファイル: ucSale.cs プロジェクト: gvhung/Store-Management
        private void RecordSaleDetails(Sale sales, string InvoiceNo, int locationID)
        {
            Product    product;
            SaleDetail salesDetails;

            foreach (DataGridViewRow dr in gridViewItemLayout.Rows)
            {
                if (dr.Cells[0].Value == null)
                {
                    throw new Exception("Value cannot be null");
                }

                salesDetails = new SaleDetail();
                product      = new Product();

                salesDetails.SaleID        = sales.SaleID;
                salesDetails.Quantity      = int.Parse(dr.Cells["ColQty"].Value.ToString());
                salesDetails.ProductID     = dr.Cells["ColItmNumber"].Value.ToString();
                salesDetails.UnitPrice     = decimal.Parse(dr.Cells["ColPrice"].Value.ToString());
                salesDetails.Discount      = decimal.Parse(dr.Cells["ColDiscount"].Value.ToString());
                salesDetails.Tax           = decimal.Parse(dr.Cells["ColTax"].Value.ToString());
                salesDetails.InvoiceNumber = InvoiceNo;
                salesDetails.LineTotal     = decimal.Parse(dr.Cells["ColTotal"].Value.ToString());
                salesDetails.LocationID    = cmbLocation.SelectedValue.ToInt();

                try
                {
                    _saleDetail.Add(salesDetails);
                }
                catch (Exception ex)
                {
                    _sale.RollBackSaleTransaction(InvoiceNo);
                    _logger.LogError(ex, "An error occurred", "ucSale", "");
                    Helper.ShowMessage("Transaction was not completed, An error occured", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                int qty       = int.Parse(dr.Cells["ColQty"].Value.ToString());
                int productID = dr.Cells["ColItmNumber"].Value.ToInt();

                try
                {
                    _product.ReduceInventory(qty, productID, locationID);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "An error occurred", "ucSale", "");
                    _sale.RollBackSaleTransaction(InvoiceNo);

                    Helper.ShowMessage("Transaction was not completed, An error occured", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
コード例 #2
0
        private void btnRecord_Click(object sender, EventArgs e)
        {
            if (btnRecord.Text == "OK")
            {
                ((frmControlHost)this.Parent).Close();
                return;
            }

            if (gridViewItemLayout.Rows.Count == 0)
            {
                Helper.ShowMessage("Sale must contain at least one product",
                                   "Sale Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            //if (!UtilityClass.ValidateTexBox(txtAmountPaid))
            //    return;

            try
            {
                Product    product;
                SaleDetail salesDetails;
                Sale       sales = new Sale();

                string   InvoiceNo   = txtInvoiceNo.Text;
                string   AmountPaid  = txtAmountPaid.Text;
                string   subTotalAmt = txtSubTotal.Text;
                DateTime?SaleDate    = dtpSaleDate.Value;
                decimal  tax         = txtSalTax.Text.ToDecimal();
                int      locationID  = cmbLocation.SelectedValue.ToInt();
                decimal  saletax     = _sale.CalculateTax(subTotalAmt.ToDecimal(), tax);
                decimal  totalAmount = txtTotalAmt.Text.ToDecimal();

                sales.CustomerID    = txtCustomer.Tag.ToString();
                sales.Amount        = totalAmount;
                sales.SubTotal      = decimal.Parse(subTotalAmt);
                sales.InvoiceDate   = SaleDate.Value;
                sales.InvoiceNumber = InvoiceNo;
                sales.StoreID       = DEFAULT_LOCALSTOREID; //arbituary value, to bo replaced
                sales.Tax           = Math.Round(saletax, 2);
                sales.PaymentModeID = int.Parse(cmbPaymentMode.SelectedValue.ToString());

                //sales.Discount = txtSaleDiscount.Text == "" ? 0 : txtSaleDiscount.Text.ToDecimal();
                sales.AmountPaid = 0;
                sales.Balance    = 0;
                sales.DateClosed = null;
                //sales.SaleTypeID = (int)SaleStatus.Order;

                sales.SaleStatusID = (int)SaleStatuses.Order;//_sale.GetSaleStatus(totalAmount, AmountPaid.ToDecimal());
                sales.EmployeeID   = txtSalesPerson.Tag == null ? DEFAULT_SALESPERSONID : txtSalesPerson.Tag.ToString();
                sales.Discount     = txtSaleDiscount.Text == "" ? 0 : decimal.Parse(txtSaleDiscount.Text);
                sales.VoidSale     = false;
                sales.LocationID   = cmbLocation.SelectedValue.ToInt();

                try
                {
                    sales = _sale.GetSingle(new Sale {
                        SaleID = _sale.Add(sales)
                    });
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "An error occurred", "ucOrder", "btnRecord");
                    Helper.ShowMessage("An error occured " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                foreach (DataGridViewRow dr in gridViewItemLayout.Rows)
                {
                    if (dr.Cells[0].Value == null)
                    {
                        return;
                    }

                    salesDetails = new SaleDetail();
                    product      = new Product();

                    salesDetails.SaleID        = sales.SaleID;
                    salesDetails.Quantity      = int.Parse(dr.Cells["ColQty"].Value.ToString());
                    salesDetails.ProductID     = dr.Cells["ColItmNumber"].Value.ToString();
                    salesDetails.UnitPrice     = decimal.Parse(dr.Cells["ColPrice"].Value.ToString());
                    salesDetails.Discount      = decimal.Parse(dr.Cells["ColDiscount"].Value.ToString());
                    salesDetails.Tax           = decimal.Parse(dr.Cells["ColTax"].Value.ToString());
                    salesDetails.InvoiceNumber = InvoiceNo;
                    salesDetails.LineTotal     = decimal.Parse(dr.Cells["ColTotal"].Value.ToString());
                    salesDetails.LocationID    = cmbLocation.SelectedValue.ToInt();

                    try
                    {
                        _saleDetail.Add(salesDetails);
                    }
                    catch
                    {
                        _sale.RollBackSaleTransaction(InvoiceNo);

                        Helper.ShowMessage("Transaction was not completed, An error occured", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    int qty       = int.Parse(dr.Cells["ColQty"].Value.ToString());
                    int productID = dr.Cells["ColItmNumber"].Value.ToInt();

                    try
                    {
                        _product.ReduceInventory(qty, productID, locationID);
                    }
                    catch (Exception ex)
                    {
                        _sale.RollBackSaleTransaction(InvoiceNo);
                        _logger.LogError(ex, "An error occurred", "ucOrder", "btnRecord");
                    }
                }

                txtInvoiceNo.Tag = false;

                txtSubTotal.Tag = null;
                txtTotalAmt.Tag = null;

                Helper.ClearForm(this);
                gridViewItemLayout.Rows.Clear();

                lblStatus.Text = "Order recorded successfully";
                GetInvoiceNo();
            }
            catch (Exception ex)
            {
                Helper.ShowMessage("An error occured sale has been cancelled \n" + ex.Message,
                                   "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }