Facebook
From Sole Lemur, 1 Year ago, written in C#.
This paste is a reply to sssssssss from ssssss - view diff
Embed
Download Paste or View Raw
Hits: 83
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12.  
  13. namespace POS_v1.FORMS
  14. {
  15.     public partial class frmPOS : Form
  16.     {
  17.         public frmPOS()
  18.         {
  19.             InitializeComponent();
  20.  
  21.         }
  22.  
  23.  
  24.         void FillCBxPay()
  25.         {
  26.             using (var DB = new DBEntities())
  27.             {
  28.                 CmbPAY.DataSource = DB.TBL_PAY.Select(x => new { x.ID, x.PAY_DESC }).ToList();
  29.                 CmbPAY.DisplayMember = "PAY_DESC";
  30.                 CmbPAY.ValueMember = "ID";
  31.             }
  32.  
  33.         }
  34.         private void frmPOS_Load(object sender, EventArgs e)
  35.         {
  36.  
  37.             FillCBxPay();
  38.             fillingCategory();
  39.             //PRDPNL.Controls.Clear();
  40.             Prodcuts();
  41.             DgVPRD.CellValueChanged += DgVPRD_CellValueChanged;
  42.  
  43.         }
  44.  
  45.         private void DgVPRD_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  46.         {
  47.             double totalx = 0;
  48.             for (int i = 0; i <= DgVPRD.Rows.Count - 1; i++)
  49.             {
  50.                 totalx += Convert.ToDouble(DgVPRD.Rows[i].Cells["TotalAmount"].Value);
  51.             }
  52.             TXTTOTAL.Text = Math.Round(totalx, 2).ToString();
  53.  
  54.         }
  55.  
  56.         void fillingCategory()
  57.         {
  58.             CatPNL.Controls.Clear();
  59.             DBEntities DBs = new DBEntities();
  60.             var PrdCat = DBs.TBL_PRODUCT_CAT.Select(s => new { s.ID, s.PRD_DESC }).ToList();
  61.             foreach (var x in PrdCat)
  62.             {
  63.                 Button btn = new Button();
  64.                 btn.BackColor = Color.FromArgb(46, 134, 222);
  65.                 btn.Size = new Size(110, 62);
  66.                 btn.ForeColor = Color.White;
  67.                 btn.FlatStyle = FlatStyle.Flat;
  68.                 btn.FlatAppearance.BorderSize = 0;
  69.                 btn.Tag = x.ID;
  70.                 btn.Text = x.PRD_DESC;
  71.                 CatPNL.Controls.Add(btn);
  72.                 btn.Click += new EventHandler(Clickss);
  73.             }
  74.         }
  75.  
  76.         private void Clickss(object sender, EventArgs e)
  77.         {
  78.             Button btn = ((Button)sender);
  79.  
  80.             foreach (var prd in PRDPNL.Controls)
  81.             {
  82.                 var prdct = (UserControlProducts)prd;
  83.                 prdct.Visible = prdct.PRDCat.ToLower().Contains(btn.Text.Trim().ToLower());
  84.             }
  85.         }
  86.         void AddProdeuct(string Id, string PrdName, string PrdPrice, Image image, string PRDCategory)
  87.         {
  88.             var UC = new UserControlProducts()
  89.             {
  90.                 Id = Convert.ToInt32(Id),
  91.                 PRDname = PrdName,
  92.                 PRDCat = PRDCategory,
  93.                 Price = PrdPrice,
  94.                 pRdimage = image
  95.             };
  96.             PRDPNL.Controls.Add(UC);
  97.             UC.Select += (ss, ee) =>
  98.              {
  99.                  var prd = (UserControlProducts)ss;
  100.                  try
  101.                  {
  102.                      double x = 0;
  103.                      double.TryParse(TxtPay.Text, out x);
  104.                      if (x == 0)
  105.                      {
  106.                          x = 1;
  107.                      }
  108.                      foreach (DataGridViewRow prdcts in DgVPRD.Rows)
  109.                      {
  110.                          if (Convert.ToInt32(prdcts.Cells["ID"].Value) == prd.Id)
  111.                          {
  112.                              prdcts.Cells["Qty"].Value = Convert.ToString(x + Convert.ToInt16(prdcts.Cells["Qty"].Value));
  113.                              prdcts.Cells["TotalAmount"].Value = Convert.ToDouble(prdcts.Cells["Qty"].Value) * Convert.ToDouble(prdcts.Cells["PRD_PRICE"].Value);
  114.                              return;
  115.                          }
  116.                      }
  117.  
  118.  
  119.                      DgVPRD.Rows.Add(new object[] { 0, prd.Id, prd.PRDname, x, prd.Price, prd.Price });
  120.                      TxtPay.Text = "1";
  121.                  }
  122.                  catch (Exception) { }
  123.                  try
  124.                  {
  125.                      Total();
  126.                  }
  127.                  catch (Exception) { }
  128.              };
  129.  
  130.         }
  131.         void Total()
  132.         {
  133.             double totals = 0;
  134.             for (int i = 0; i <= DgVPRD.Rows.Count - 1; i++)
  135.             {
  136.                 totals += Convert.ToDouble(DgVPRD.Rows[i].Cells["TotalAmount"].Value);
  137.                 DgVPRD.ClearSelection();
  138.                 DgVPRD.FirstDisplayedScrollingRowIndex = DgVPRD.Rows.Count - 1;
  139.                 DgVPRD.Rows[DgVPRD.Rows.Count - 1].Selected = true;
  140.             }
  141.             TXTTOTAL.Text = totals.ToString();
  142.         }
  143.         private void Prodcuts()
  144.         {
  145.             DBEntities DB1 = new DBEntities();
  146.             var query = from a in DB1.TBL_PRODUCT
  147.                         join c in DB1.TBL_PRODUCT_CAT on a.PRD_CATEGORY_ID equals c.ID
  148.                         select new
  149.                         {
  150.                             a.ID,
  151.                             a.PRD_IMAGE,
  152.                             a.PRD_PRICE,
  153.                             a.PRD_NAME,
  154.                             c.PRD_DESC
  155.                         }
  156.                         ;
  157.  
  158.             foreach (var x in query)
  159.             {
  160.                 AddProdeuct(Convert.ToString(x.ID),
  161.                     x.PRD_NAME, Convert.ToString(x.PRD_PRICE),
  162.                     GetImageFromByteArray(x.PRD_IMAGE), x.PRD_DESC);
  163.             }
  164.         }
  165.         Image GetImageFromByteArray(Byte[] ByteArray)
  166.         {
  167.             Image img;
  168.             try
  169.             {
  170.                 Byte[] imgbyte = ByteArray;
  171.                 MemoryStream stream = new MemoryStream(imgbyte, false);
  172.                 img = Image.FromStream(stream);
  173.             }
  174.             catch { img = null; }
  175.             return img;
  176.         }
  177.  
  178.         private void DgVPRD_CellContentClick(object sender, DataGridViewCellEventArgs e)
  179.         {
  180.  
  181.         }
  182.  
  183.         private void textBox2_TextChanged(object sender, EventArgs e)
  184.         {
  185.             foreach (var prd in PRDPNL.Controls)
  186.             {
  187.                 var prdct = (UserControlProducts)prd;
  188.                 prdct.Visible = prdct.PRDname.ToLower().Contains(textBox2.Text.Trim().ToLower());
  189.             }
  190.         }
  191.  
  192.         private void DgVPRD_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
  193.         {
  194.             int count = 0;
  195.             foreach (DataGridViewRow dgv in DgVPRD.Rows)
  196.             {
  197.                 count++;
  198.                 dgv.Cells[0].Value = count;
  199.             }
  200.         }
  201.  
  202.         #region buttons
  203.  
  204.         #endregion
  205.  
  206.         private void BtnDelete_Click(object sender, EventArgs e)
  207.         {
  208.             if (DgVPRD.Rows.Count > 0)
  209.             {
  210.                 DgVPRD.Rows.Remove(DgVPRD.CurrentRow);
  211.                 Total();
  212.             }
  213.         }
  214.  
  215.         private void BtnClear_Click(object sender, EventArgs e)
  216.         {
  217.             DgVPRD.Rows.Clear();
  218.             Total();
  219.         }
  220.  
  221.         private void BtnClose_Click(object sender, EventArgs e)
  222.         {
  223.             this.Close();
  224.         }
  225.  
  226.         private void Clear()
  227.         {
  228.             TXTTOTAL.Text = "0";
  229.             TxtPay.Text = "0";
  230.             TxtPayed.Text = "0";
  231.             DgVPRD.Rows.Clear();
  232.         }
  233.         private void BtnNew_Click(object sender, EventArgs e)
  234.         {
  235.             Clear();
  236.  
  237.             //SrDgv = 0;
  238.         }
  239.  
  240.  
  241.  
  242.         private void BtnC_Click(object sender, EventArgs e)
  243.         {
  244.             TxtPay.Text = "0";
  245.         }
  246.  
  247.         private void BTN7_Click(object sender, EventArgs e)
  248.         {
  249.             Button b = (Button)sender;
  250.             if (TxtPay.Text == "0")
  251.                 TxtPay.Clear();
  252.  
  253.             TxtPay.Text = TxtPay.Text + b.Text;
  254.             if (TxtPay.Text == "." && b.Text == ".")
  255.             {
  256.                 TxtPay.Text = "0.";
  257.             }
  258.         }
  259.  
  260.         private void SaveBill()
  261.         {
  262.             double Txttotal = 0;
  263.             double totalPay = 0;
  264.             double.TryParse(TXTTOTAL.Text, out Txttotal);
  265.             double.TryParse(TxtPay.Text, out totalPay);
  266.  
  267.             if (Txttotal == 0)
  268.             {
  269.                 MessageBox.Show("Cant Save Empty Bill");
  270.                 return;
  271.             }
  272.             if (totalPay == 0)
  273.             {
  274.                 MessageBox.Show("Cant Pay Without Money");
  275.                 return;
  276.             }
  277.             if (totalPay < Txttotal)
  278.             {
  279.                 MessageBox.Show("The Payment Not Enough");
  280.                 return;
  281.             }
  282.  
  283.             TxtPayed.Text = totalPay.ToString();
  284.             TxtChange.Text = (totalPay - Txttotal).ToString();
  285.             DBEntities DBsx = new DBEntities();
  286.             try
  287.             {
  288.                 TBL_BILL tblBill = new TBL_BILL();
  289.                 tblBill.PAY_DATE = DateTime.Now;
  290.                 tblBill.UserID = Declaration.UserID;
  291.                 tblBill.TotalAmount = Convert.ToDecimal(TXTTOTAL.Text);
  292.                 tblBill.STATUS = "Close";
  293.                 List<TBL_PRODUCT_BILL> list = new List<TBL_PRODUCT_BILL>();
  294.                 for (int i = 0; i < DgVPRD.Rows.Count; i++)
  295.                 {
  296.                     TBL_PRODUCT_BILL PRDBILLDB = new TBL_PRODUCT_BILL();
  297.                     PRDBILLDB.PRD_ID = int.Parse(DgVPRD.Rows[i].Cells["ID"].Value.ToString());
  298.                     PRDBILLDB.QTY = int.Parse(DgVPRD.Rows[i].Cells["Qty"].Value.ToString());
  299.                     PRDBILLDB.PRICE = Convert.ToDecimal(DgVPRD.Rows[i].Cells["PRD_PRICE"].Value);
  300.                     PRDBILLDB.TOTAL_PRICE = Convert.ToDecimal(DgVPRD.Rows[i].Cells["TotalAmount"].Value);
  301.                     list.Add(PRDBILLDB);
  302.                 }
  303.                 List<TBL_BILL_PAY> list2 = new List<TBL_BILL_PAY>();
  304.                 try
  305.                 {
  306.                     TBL_BILL_PAY tblBillPay = new TBL_BILL_PAY();
  307.                     tblBillPay.PAY_ID = Convert.ToInt32(CmbPAY.SelectedValue);
  308.                     tblBillPay.PAY_VALUE = Convert.ToDecimal(TxtPayed.Text);
  309.                     list2.Add(tblBillPay);
  310.                 }
  311.                 catch (Exception) { }
  312.  
  313.                 tblBill.TBL_PRODUCT_BILL = list;
  314.                 tblBill.TBL_BILL_PAY = list2;
  315.                 DBsx.TBL_BILL.Add(tblBill);
  316.                 DBsx.SaveChanges();
  317.                 MessageBox.Show("Save Successfully");
  318.  
  319.             }
  320.             catch (Exception) { throw; }
  321.         }
  322.  
  323.         private void SavePRDBILL()
  324.         {
  325.             try
  326.             {
  327.  
  328.  
  329.             }
  330.             catch (Exception)
  331.             {
  332.  
  333.                 throw;
  334.             }
  335.         }
  336.         private void SavePRDPAY()
  337.         {
  338.  
  339.         }
  340.  
  341.         private void BtnPrint_Click(object sender, EventArgs e)
  342.         {
  343.             SaveBill();
  344.         }
  345.  
  346.         private void TxtPayed_TextChanged(object sender, EventArgs e)
  347.         {
  348.             TxtPay.Text = TxtPayed.Text;
  349.         }
  350.     }
  351. }
  352.  
captcha