- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace POS_v1.FORMS
- {
- public partial class frmPOS : Form
- {
- public frmPOS()
- {
- InitializeComponent();
- }
- void FillCBxPay()
- {
- using (var DB = new DBEntities())
- {
- CmbPAY.DataSource = DB.TBL_PAY.Select(x => new { x.ID, x.PAY_DESC }).ToList();
- CmbPAY.DisplayMember = "PAY_DESC";
- CmbPAY.ValueMember = "ID";
- }
- }
- private void frmPOS_Load(object sender, EventArgs e)
- {
- FillCBxPay();
- fillingCategory();
- //PRDPNL.Controls.Clear();
- Prodcuts();
- DgVPRD.CellValueChanged += DgVPRD_CellValueChanged;
- }
- private void DgVPRD_CellValueChanged(object sender, DataGridViewCellEventArgs e)
- {
- double totalx = 0;
- for (int i = 0; i <= DgVPRD.Rows.Count - 1; i++)
- {
- totalx += Convert.ToDouble(DgVPRD.Rows[i].Cells["TotalAmount"].Value);
- }
- TXTTOTAL.Text = Math.Round(totalx, 2).ToString();
- }
- void fillingCategory()
- {
- CatPNL.Controls.Clear();
- DBEntities DBs = new DBEntities();
- var PrdCat = DBs.TBL_PRODUCT_CAT.Select(s => new { s.ID, s.PRD_DESC }).ToList();
- foreach (var x in PrdCat)
- {
- Button btn = new Button();
- btn.BackColor = Color.FromArgb(46, 134, 222);
- btn.Size = new Size(110, 62);
- btn.ForeColor = Color.White;
- btn.FlatStyle = FlatStyle.Flat;
- btn.FlatAppearance.BorderSize = 0;
- btn.Tag = x.ID;
- btn.Text = x.PRD_DESC;
- CatPNL.Controls.Add(btn);
- btn.Click += new EventHandler(Clickss);
- }
- }
- private void Clickss(object sender, EventArgs e)
- {
- Button btn = ((Button)sender);
- foreach (var prd in PRDPNL.Controls)
- {
- var prdct = (UserControlProducts)prd;
- prdct.Visible = prdct.PRDCat.ToLower().Contains(btn.Text.Trim().ToLower());
- }
- }
- void AddProdeuct(string Id, string PrdName, string PrdPrice, Image image, string PRDCategory)
- {
- var UC = new UserControlProducts()
- {
- Id = Convert.ToInt32(Id),
- PRDname = PrdName,
- PRDCat = PRDCategory,
- Price = PrdPrice,
- pRdimage = image
- };
- PRDPNL.Controls.Add(UC);
- UC.Select += (ss, ee) =>
- {
- var prd = (UserControlProducts)ss;
- try
- {
- double x = 0;
- double.TryParse(TxtPay.Text, out x);
- if (x == 0)
- {
- x = 1;
- }
- foreach (DataGridViewRow prdcts in DgVPRD.Rows)
- {
- if (Convert.ToInt32(prdcts.Cells["ID"].Value) == prd.Id)
- {
- prdcts.Cells["Qty"].Value = Convert.ToString(x + Convert.ToInt16(prdcts.Cells["Qty"].Value));
- prdcts.Cells["TotalAmount"].Value = Convert.ToDouble(prdcts.Cells["Qty"].Value) * Convert.ToDouble(prdcts.Cells["PRD_PRICE"].Value);
- return;
- }
- }
- DgVPRD.Rows.Add(new object[] { 0, prd.Id, prd.PRDname, x, prd.Price, prd.Price });
- TxtPay.Text = "1";
- }
- catch (Exception) { }
- try
- {
- Total();
- }
- catch (Exception) { }
- };
- }
- void Total()
- {
- double totals = 0;
- for (int i = 0; i <= DgVPRD.Rows.Count - 1; i++)
- {
- totals += Convert.ToDouble(DgVPRD.Rows[i].Cells["TotalAmount"].Value);
- DgVPRD.ClearSelection();
- DgVPRD.FirstDisplayedScrollingRowIndex = DgVPRD.Rows.Count - 1;
- DgVPRD.Rows[DgVPRD.Rows.Count - 1].Selected = true;
- }
- TXTTOTAL.Text = totals.ToString();
- }
- private void Prodcuts()
- {
- DBEntities DB1 = new DBEntities();
- var query = from a in DB1.TBL_PRODUCT
- join c in DB1.TBL_PRODUCT_CAT on a.PRD_CATEGORY_ID equals c.ID
- select new
- {
- a.ID,
- a.PRD_IMAGE,
- a.PRD_PRICE,
- a.PRD_NAME,
- c.PRD_DESC
- }
- ;
- foreach (var x in query)
- {
- AddProdeuct(Convert.ToString(x.ID),
- x.PRD_NAME, Convert.ToString(x.PRD_PRICE),
- GetImageFromByteArray(x.PRD_IMAGE), x.PRD_DESC);
- }
- }
- Image GetImageFromByteArray(Byte[] ByteArray)
- {
- Image img;
- try
- {
- Byte[] imgbyte = ByteArray;
- MemoryStream stream = new MemoryStream(imgbyte, false);
- img = Image.FromStream(stream);
- }
- catch { img = null; }
- return img;
- }
- private void DgVPRD_CellContentClick(object sender, DataGridViewCellEventArgs e)
- {
- }
- private void textBox2_TextChanged(object sender, EventArgs e)
- {
- foreach (var prd in PRDPNL.Controls)
- {
- var prdct = (UserControlProducts)prd;
- prdct.Visible = prdct.PRDname.ToLower().Contains(textBox2.Text.Trim().ToLower());
- }
- }
- private void DgVPRD_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
- {
- int count = 0;
- foreach (DataGridViewRow dgv in DgVPRD.Rows)
- {
- count++;
- dgv.Cells[0].Value = count;
- }
- }
- #region buttons
- #endregion
- private void BtnDelete_Click(object sender, EventArgs e)
- {
- if (DgVPRD.Rows.Count > 0)
- {
- DgVPRD.Rows.Remove(DgVPRD.CurrentRow);
- Total();
- }
- }
- private void BtnClear_Click(object sender, EventArgs e)
- {
- DgVPRD.Rows.Clear();
- Total();
- }
- private void BtnClose_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void Clear()
- {
- TXTTOTAL.Text = "0";
- TxtPay.Text = "0";
- TxtPayed.Text = "0";
- DgVPRD.Rows.Clear();
- }
- private void BtnNew_Click(object sender, EventArgs e)
- {
- Clear();
- //SrDgv = 0;
- }
- private void BtnC_Click(object sender, EventArgs e)
- {
- TxtPay.Text = "0";
- }
- private void BTN7_Click(object sender, EventArgs e)
- {
- Button b = (Button)sender;
- if (TxtPay.Text == "0")
- TxtPay.Clear();
- TxtPay.Text = TxtPay.Text + b.Text;
- if (TxtPay.Text == "." && b.Text == ".")
- {
- TxtPay.Text = "0.";
- }
- }
- private void SaveBill()
- {
- double Txttotal = 0;
- double totalPay = 0;
- double.TryParse(TXTTOTAL.Text, out Txttotal);
- double.TryParse(TxtPay.Text, out totalPay);
- if (Txttotal == 0)
- {
- MessageBox.Show("Cant Save Empty Bill");
- return;
- }
- if (totalPay == 0)
- {
- MessageBox.Show("Cant Pay Without Money");
- return;
- }
- if (totalPay < Txttotal)
- {
- MessageBox.Show("The Payment Not Enough");
- return;
- }
- TxtPayed.Text = totalPay.ToString();
- TxtChange.Text = (totalPay - Txttotal).ToString();
- DBEntities DBsx = new DBEntities();
- try
- {
- TBL_BILL tblBill = new TBL_BILL();
- tblBill.PAY_DATE = DateTime.Now;
- tblBill.UserID = Declaration.UserID;
- tblBill.TotalAmount = Convert.ToDecimal(TXTTOTAL.Text);
- tblBill.STATUS = "Close";
- List<TBL_PRODUCT_BILL> list = new List<TBL_PRODUCT_BILL>();
- for (int i = 0; i < DgVPRD.Rows.Count; i++)
- {
- TBL_PRODUCT_BILL PRDBILLDB = new TBL_PRODUCT_BILL();
- PRDBILLDB.PRD_ID = int.Parse(DgVPRD.Rows[i].Cells["ID"].Value.ToString());
- PRDBILLDB.QTY = int.Parse(DgVPRD.Rows[i].Cells["Qty"].Value.ToString());
- PRDBILLDB.PRICE = Convert.ToDecimal(DgVPRD.Rows[i].Cells["PRD_PRICE"].Value);
- PRDBILLDB.TOTAL_PRICE = Convert.ToDecimal(DgVPRD.Rows[i].Cells["TotalAmount"].Value);
- list.Add(PRDBILLDB);
- }
- List<TBL_BILL_PAY> list2 = new List<TBL_BILL_PAY>();
- try
- {
- TBL_BILL_PAY tblBillPay = new TBL_BILL_PAY();
- tblBillPay.PAY_ID = Convert.ToInt32(CmbPAY.SelectedValue);
- tblBillPay.PAY_VALUE = Convert.ToDecimal(TxtPayed.Text);
- list2.Add(tblBillPay);
- }
- catch (Exception) { }
- tblBill.TBL_PRODUCT_BILL = list;
- tblBill.TBL_BILL_PAY = list2;
- DBsx.TBL_BILL.Add(tblBill);
- DBsx.SaveChanges();
- MessageBox.Show("Save Successfully");
- }
- catch (Exception) { throw; }
- }
- private void SavePRDBILL()
- {
- try
- {
- }
- catch (Exception)
- {
- throw;
- }
- }
- private void SavePRDPAY()
- {
- }
- private void BtnPrint_Click(object sender, EventArgs e)
- {
- SaveBill();
- }
- private void TxtPayed_TextChanged(object sender, EventArgs e)
- {
- TxtPay.Text = TxtPayed.Text;
- }
- }
- }