-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaleForm.cs
157 lines (146 loc) · 6.27 KB
/
SaleForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using Inventory_Managment_System.Data;
using Inventory_Managment_System.Services;
using MaterialSkin;
using MaterialSkin.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Inventory_Managment_System
{
public partial class SaleForm : MaterialForm
{
private readonly IProductService _productService;
private readonly ICustomerService _customerService;
private readonly ISaleService _saleService;
public Models.Sale Sale { get; set; } = new Models.Sale();
public SaleForm()
{
InitializeComponent();
_productService = new Services.ProductService(new IMS_DataContext());
_customerService = new Services.CustomerService(new IMS_DataContext());
_saleService = new Services.SaleService(new IMS_DataContext());
productsCbx.DataSource = _productService.GetProducts();
customersCbx.DataSource = _customerService.GetCustomers();
var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
materialSkinManager.ColorScheme = new ColorScheme(Primary.BlueGrey800, Primary.BlueGrey900, Primary.BlueGrey500, Accent.Red700, TextShade.WHITE);
}
private void productsCbx_SelectedIndexChanged(object sender, EventArgs e)
{
var product = _productService.GetProduct(Int32.Parse(productsCbx.SelectedValue.ToString()));
this.addProduct(product);
this.totalBill();
}
private void submitSale_Click(object sender, EventArgs e)
{
if (productsGrid.Rows.Count > 0)
{
bool check = false;
Sale.SoldProducts = new List<Models.SoldProduct>();
decimal totalPrice = 0;
foreach (DataGridViewRow row in productsGrid.Rows)
{
if (Int32.Parse(row.Cells["product_quantity"].Value.ToString()) <= Int32.Parse(row.Cells["Product_On_Stock"].Value.ToString()))
{
Sale.SoldProducts.Add(new Models.SoldProduct
{
Price = Decimal.Parse(row.Cells["Product_Price"].Value.ToString()),
ProductId = Int32.Parse(row.Cells["product_id"].Value.ToString()),
Qty = Int32.Parse(row.Cells["product_quantity"].Value.ToString()),
TotalAmount = Int32.Parse(row.Cells["product_quantity"].Value.ToString()) * Decimal.Parse(row.Cells["Product_Price"].Value.ToString()),
CreatedAt = DateTime.Now,
});
}
else
{
check = true;
}
totalPrice += (Decimal.Parse(row.Cells["Product_Price"].Value.ToString()) * Int32.Parse(row.Cells["product_quantity"].Value.ToString()));
}
if (!check && Decimal.Parse(totalBillLbl.Text) <= Decimal.Parse(payTbx.Text))
{
foreach (DataGridViewRow row in productsGrid.Rows)
{
var product = _productService.GetProduct(Int32.Parse(row.Cells["product_id"].Value.ToString()));
product.Stock -= Int32.Parse(row.Cells["product_quantity"].Value.ToString());
_productService.UpdateProduct(product);
}
Sale.ClientId = Int32.Parse(customersCbx.SelectedValue.ToString());
Sale.UserId = 1;
Sale.TotalAmount = Decimal.Parse(totalBillLbl.Text);
Sale.Paid = Decimal.Parse(payTbx.Text.ToString());
Sale.Due = Sale.Paid - Sale.TotalAmount;
Sale.FinalizedAt = DateTime.Now;
_saleService.AddSale(Sale);
MessageBox.Show("Success");
this.reset();
}
else
{
MessageBox.Show("Out of stock / Pay bill");
}
}
else
{
MessageBox.Show("Add products");
}
}
private void reset()
{
productsGrid.Rows.Clear();
Sale = new Models.Sale();
totalBillLbl.Text = "0";
payTbx.Text = "0";
discauntTbx.Text = "0";
}
private void productsGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
this.totalBill();
}
private void totalBill(int discount = 0)
{
decimal totalPrice = 0;
foreach (DataGridViewRow row in productsGrid.Rows)
{
totalPrice += (Decimal.Parse(row.Cells["Product_Price"].Value.ToString()) * Int32.Parse(row.Cells["product_quantity"].Value.ToString()));
}
totalBillLbl.Text = (totalPrice -= totalPrice * discount / 100).ToString();
}
private void addProduct(Models.Product product)
{
bool exist = false;
if (productsGrid.Rows.Count > 0)
{
foreach (DataGridViewRow row in productsGrid.Rows)
{
if (Int32.Parse(row.Cells["product_id"].Value.ToString()) == product.Id)
{
exist = true;
}
}
if (!exist)
{
productsGrid.Rows.Add(product.Id, product.Name, product.Price, 1, product.Stock);
}
}
else
{
productsGrid.Rows.Add(product.Id, product.Name, product.Price, 1, product.Stock);
}
}
private void discauntTbx_TextChanged(object sender, EventArgs e)
{
if (Int32.Parse(discauntTbx.Text.ToString())>=0)
this.totalBill(Int32.Parse(discauntTbx.Text.ToString()));
}
private void materialButton1_Click(object sender, EventArgs e)
{
this.reset();
}
}
}