Advertisement
coasterka

InformationSystemSales.cs

Oct 7th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace VPEx2
  12. {
  13.     public partial class InformationSystemSales : Form
  14.     {
  15.         public InformationSystemSales()
  16.         {
  17.             InitializeComponent();
  18.             listviewSales.View = View.Details;
  19.             listviewSales.Columns.Add("Date", 100, HorizontalAlignment.Left);
  20.             listviewSales.Columns.Add("Article", 80, HorizontalAlignment.Left);
  21.             listviewSales.Columns.Add("Unit Price", 60, HorizontalAlignment.Left);
  22.             listviewSales.Columns.Add("Qty", 50, HorizontalAlignment.Left);
  23.             listviewSales.Columns.Add("Discount", 66, HorizontalAlignment.Left);
  24.             listviewSales.Columns.Add("Total Amount", 100, HorizontalAlignment.Left);
  25.         }
  26.  
  27.         int numOfSales = 0;
  28.         decimal grandDiscountValue = 0.0m;
  29.         decimal grandTotalValue = 0.0m;
  30.         ToolTip toolTip = new ToolTip();
  31.         MessageBoxButtons button = MessageBoxButtons.OK;
  32.         DialogResult result;
  33.  
  34.         private void newSaleBtn_Click(object sender, EventArgs e)
  35.         {
  36.             MessageBoxButtons button = MessageBoxButtons.OK;
  37.             DialogResult result;
  38.             string newSaleMessage = "Inputting data for a sale.";
  39.             string newSaleCaption = "New Sale";
  40.             result = MessageBox.Show(newSaleMessage, newSaleCaption, button);
  41.             article.Text = "";
  42.             unitPrice.Clear();
  43.             quantity.Clear();
  44.             discount.Clear();
  45.             totalAmount.Clear();
  46.             totalBtn.Enabled = true;
  47.             newSaleBtn.Enabled = false;
  48.             discountLabel.ForeColor = Color.Black;
  49.         }
  50.         private void totalBtn_Click(object sender, EventArgs e)
  51.         {
  52.             decimal discountValue = 0.0m;
  53.             decimal totalAmountValue = 0.0m;
  54.             string fieldIsIncorrect = "Some of the input data is incorrect. Please make sure you enter a valid data.";
  55.  
  56.             double n;
  57.             bool isNumeric = double.TryParse(unitPrice.Text, out n) &&
  58.                 double.TryParse(quantity.Text, out n);
  59.  
  60.             if (article.SelectedIndex == -1 || !isNumeric ||
  61.                 unitPrice.Text.Trim().Length == 0 || quantity.Text.Trim().Length == 0 ||
  62.                 selectedCurrency.SelectedIndex == -1 ||
  63.                 int.Parse(unitPrice.Text) <= 0 || int.Parse(quantity.Text) <= 0)
  64.             {
  65.                 MessageBox.Show(fieldIsIncorrect, "Error", button);
  66.             }
  67.             else
  68.             {
  69.                 string totalMessage = "Calculating discount and total amount of a current entered sale.";
  70.                 string totalCaption = "Total";
  71.                 result = MessageBox.Show(totalMessage, totalCaption, button);
  72.  
  73.                 decimal unitPriceValue = decimal.Parse(unitPrice.Text);
  74.                 int quantityValue = int.Parse(quantity.Text);
  75.                 totalAmountValue = unitPriceValue * quantityValue;
  76.                 double discountPercent = 0.1;
  77.  
  78.                 if (totalAmountValue >= 100)
  79.                 {
  80.                     discountValue = (decimal)discountPercent * totalAmountValue;
  81.                     totalAmountValue = totalAmountValue - discountValue;
  82.                 }
  83.                 else
  84.                 {
  85.                     discountValue = 0;
  86.                 }
  87.                 if (discountValue > 0)
  88.                 {
  89.                     grandDiscountValue += discountValue;
  90.                     discountLabel.ForeColor = Color.Red;
  91.                 }
  92.                 else
  93.                 {
  94.                     discountLabel.ForeColor = Color.Black;
  95.                 }
  96.  
  97.                 numOfSales++;
  98.                 grandTotalValue += totalAmountValue;
  99.  
  100.                 totalAmount.Text = string.Format("{0:0.00}", totalAmountValue);
  101.                 discount.Text = string.Format("{0:0.00}", discountValue);
  102.  
  103.                 newSaleBtn.Enabled = true;
  104.                 totalBtn.Enabled = false;
  105.                 selectDate.Enabled = false;
  106.             }
  107.             string[] currentRow = { article.Text, unitPrice.Text, quantity.Text,
  108.                                discount.Text, totalAmount.Text };
  109.             listviewSales.Items.Add(Convert.ToString(selectDate.Text)).SubItems.AddRange(currentRow);
  110.         }
  111.  
  112.         public Boolean IsNumber(String value)
  113.         {
  114.             Boolean isNum = true;
  115.             foreach (Char c in value.ToCharArray())
  116.             {
  117.                 isNum = isNum && Char.IsDigit(c);
  118.             }
  119.  
  120.             return isNum;
  121.         }
  122.  
  123.         private void finishAllSalesBtn_Click(object sender, EventArgs e)
  124.         {
  125.             if (finishAllSalesBtn.Text == "New Operation")
  126.             {
  127.                 numberOfSales.Clear();
  128.                 grandDiscount.Clear();
  129.                 grandTotal.Clear();
  130.                 grandDiscountBGN.Clear();
  131.                 grandTotalBGN.Clear();
  132.                 selectedCurrency.SelectedIndex = -1;
  133.                 listviewSales.Items.Clear();
  134.                
  135.                 article.Text = "";
  136.                 unitPrice.Clear();
  137.                 quantity.Clear();
  138.                 discount.Clear();
  139.                 totalAmount.Clear();
  140.                 numOfSales = 0;
  141.                 grandDiscountValue = 0;
  142.                 grandTotalValue = 0;
  143.                 grandDiscLabel.ForeColor = Color.Black;
  144.                 finishAllSalesBtn.Text = "Finish All Sales";
  145.                 totalBtn.Enabled = true;
  146.                 article.Enabled = true;
  147.                 unitPrice.ReadOnly = false;
  148.                 quantity.ReadOnly = false;
  149.                 selectDate.Enabled = true;
  150.             }
  151.             else
  152.             {
  153.                 string finishSalesMessage = "Conclusion of input sales and calculating the total discount and total amount of all entries.";
  154.                 string finishSalesCaption = "Finish Sales";
  155.                 result = MessageBox.Show(finishSalesMessage, finishSalesCaption, button);
  156.  
  157.                 numberOfSales.Text = numOfSales.ToString();
  158.                 grandDiscount.Text = string.Format("{0:0.00}", grandDiscountValue);
  159.                 grandTotal.Text = string.Format("{0:0.00}", grandTotalValue);
  160.  
  161.                 finishAllSalesBtn.Text = "New Operation";
  162.                 newSaleBtn.Enabled = false;
  163.                 totalBtn.Enabled = true;
  164.                 article.Enabled = true;
  165.                 unitPrice.ReadOnly = true;
  166.                 quantity.ReadOnly = true;
  167.  
  168.                 if (grandDiscountValue > 0)
  169.                 {
  170.                     grandDiscLabel.ForeColor = Color.Red;
  171.                 }
  172.                 else
  173.                 {
  174.                     grandDiscLabel.ForeColor = Color.Black;
  175.                 }
  176.                 discountLabel.ForeColor = Color.Black;
  177.  
  178.                 finishAllSalesBtn.Text = "New Operation";
  179.             }
  180.  
  181.             numberOfSalesBGN.Text = numberOfSales.Text;
  182.  
  183.             decimal exchangeRateEUR = 0.51m;
  184.             decimal exchangeRateUSD = 0.64m;
  185.  
  186.             switch (selectedCurrency.Text)
  187.             {
  188.                 case "BGN":
  189.                     currencyLabel.Text = "BGN";
  190.                     grandDiscountBGN.Text = grandDiscount.Text;
  191.                     grandTotalBGN.Text = grandTotal.Text;
  192.                     break;
  193.                 case "EUR":
  194.                     decimal grandDiscountEUR = grandDiscountValue * exchangeRateEUR;
  195.                     decimal grandTotalEUR = grandTotalValue * exchangeRateEUR;
  196.                     grandDiscountBGN.Text = string.Format("{0:0.00}", grandDiscountEUR);
  197.                     grandTotalBGN.Text = string.Format("{0:0.00}", grandTotalEUR);
  198.                     currencyLabel.Text = "EUR";
  199.                     break;
  200.                 case "USD":
  201.                     decimal grandDiscountUSD = grandDiscountValue * exchangeRateUSD;
  202.                     decimal grandTotalUSD = grandTotalValue * exchangeRateUSD;
  203.                     grandDiscountBGN.Text = string.Format("{0:0.00}", grandDiscountUSD);
  204.                     grandTotalBGN.Text = string.Format("{0:0.00}", grandTotalUSD);
  205.                     currencyLabel.Text = "USD";
  206.                     break;
  207.             }
  208.         }
  209.  
  210.         private void exitBtn_Click(object sender, EventArgs e)
  211.         {
  212.             string exitMessage = "Exit the application";
  213.             string exitCaption = "Exit";
  214.             result = MessageBox.Show(exitMessage, exitCaption, button);
  215.             this.Close();
  216.         }
  217.  
  218.         private void unitPrice_MouseHover(object sender, EventArgs e)
  219.         {
  220.             toolTip.Show("Positive value. Required field.", unitPrice);
  221.         }
  222.  
  223.         private void quantity_MouseHover(object sender, EventArgs e)
  224.         {
  225.             toolTip.Show("Positive whole value. Required field.", quantity);
  226.         }
  227.  
  228.         private void newSaleBtn_MouseHover(object sender, EventArgs e)
  229.         {
  230.             toolTip.Show("Adds a new sale to the current operation.", newSaleBtn);
  231.         }
  232.  
  233.         private void totalBtn_MouseHover(object sender, EventArgs e)
  234.         {
  235.             toolTip.Show("Calculates the total sum of the current sale.", totalBtn);
  236.         }
  237.  
  238.         private void finishAllSalesBtn_MouseHover(object sender, EventArgs e)
  239.         {
  240.             if (finishAllSalesBtn.Text == "New Operation")
  241.             {
  242.                 toolTip.Show("Begins a new operation.", finishAllSalesBtn);
  243.             }
  244.             toolTip.Show("Finishes the current operation and calculates grand sums.", finishAllSalesBtn);
  245.         }
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement