Biskilog POS desktop appilcation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

343 lines
15 KiB

using BiskLog_Point_Of_Sale.Classes;
using BiskLog_Point_Of_Sale.Dialogs;
using BiskLog_Point_Of_Sale.Multiple_Login;
using BiskLog_Point_Of_Sale.Properties;
using Point_Of_Sale_Managment;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BiskLog_Point_Of_Sale.Products_Module
{
public partial class Prices : Form
{
SqlConnection cn;
SqlCommand cm, cm1;
DatabaseConn databasecon = new DatabaseConn();
SqlDataReader dr;
ProductsMainHolder mainHolder;
List<ProductAmendment> productAmendments = new List<ProductAmendment>();
public Prices(ProductsMainHolder productsMain, string previous_price = null, string code = null, string name = null)
{
InitializeComponent();
mainHolder = productsMain;
cn = new SqlConnection(databasecon.MyConnection());
holding.Left = (ClientSize.Width - holding.Width) / 2;
updateBox.Left = (ClientSize.Width - updateBox.Width) / 2;
holding2.Left = (ClientSize.Width - holding2.Width) / 2;
if (!String.IsNullOrEmpty(code))
{
txtProductID.Text = code;
lblProductName.Text = name;
previousPrice.Text = previous_price;
newPrice.Enabled = true;
BTNsave.Enabled = true;
}
}
private async void Products_CellClick(object sender, DataGridViewCellEventArgs e)
{
string productCode = products.Rows[e.RowIndex].Cells[1].Value.ToString();
string productName = products.Rows[e.RowIndex].Cells[2].Value.ToString();
string currentPrice = products.Rows[e.RowIndex].Cells[7].Value.ToString();
PriceAmendmentD priceAmendment = new PriceAmendmentD(previous: currentPrice, pname: productName, pcode: productCode);
priceAmendment.BringToFront();
priceAmendment.ShowDialog();
if (priceAmendment.DialogResult == DialogResult.OK)
{
string priceATM = currentPrice.Substring(Settings.Default.currrencyCode.Length);
decimal pricetag = decimal.Parse(priceAmendment.newprice);
Task<int> task = new Task<int>(() => updatePrice(pricetag, priceATM, productCode));
holding.Visible = true;
task.Start();
int result = await task;
if (result == 1)
{
string Message = "You have successfully changed the price of " + productName + " from " + currentPrice + " to "
+ Settings.Default.currrencyCode + " " + pricetag.ToString();
string Title = "Price changes successful";
NoAction action = new NoAction(message: Message, title: Title);
action.BringToFront();
action.ShowDialog();
LoadRecords();
}
else
{
string Message = "An error occurred while changing the price of " + productName + " please try again later";
string Title = "Error while making changes";
NoAction action = new NoAction(message: Message, title: Title);
action.BringToFront();
action.ShowDialog();
}
holding.Visible = false;
}
}
public int updatePrice(decimal newprice, string current, string productID)
{
SqlTransaction sqlTransaction = null;
try
{
cn.Open();
sqlTransaction = cn.BeginTransaction();
cm = new SqlCommand("Insert into tblPriceChanges (pcode,previous_price,current_price,change_date,branchID,countID) values " +
"(@pcode,@previous_price,@current_price,@change_date,@branchID,@countID)", cn);
cm.Parameters.AddWithValue("@pcode", productID);
cm.Parameters.AddWithValue("@previous_price", current);
cm.Parameters.AddWithValue("@current_price", newprice);
cm.Parameters.AddWithValue("@change_date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff"));
cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID);
cm.Parameters.AddWithValue("@countID", productID + Settings.Default.BranchID + DateTime.Now.ToString("yyyyMMddHHmmssfff"));
cm.Transaction = sqlTransaction;
cm.ExecuteNonQuery();
cm1 = new SqlCommand("Update tblProduct set price = @price where pcode = @pcode and branchID = @branchID", cn);
cm1.Parameters.AddWithValue("@pcode", productID);
cm1.Parameters.AddWithValue("@price", newprice);
cm1.Parameters.AddWithValue("@branchID", Settings.Default.BranchID);
cm1.Transaction = sqlTransaction;
cm1.ExecuteNonQuery();
sqlTransaction.Commit();
cn.Close();
return 1;
}
catch
{
sqlTransaction.Rollback();
cn.Close();
return 0;
}
}
public int LoadRecords()
{
try
{
List<string> id = new List<string>();
int i = 0;
products.Invoke(new Action(() =>
{
products.Rows.Clear();
cn.Open();
cm = new SqlCommand("Select p.pcode,p.product_name,p.pdesc,b.brand,c.change_date,c.previous_price,c.current_price from tblProduct as p " +
"inner join tblBrand as b on b.id = p.bid inner join tblInventory as I on I.pcode = p.pcode inner join tblPriceChanges as c on c.pcode = p.pcode " +
"where p.pcode in (Select Distinct (pcode) from tblProduct where branchID = @branch and status = 'ACTIVE' and (pdesc like '%" + txtSearch.Text + "%' or " +
"product_name like '%" + txtSearch.Text + "%' or pcode like '%" + txtSearch.Text + "%')) order by c.change_date desc", cn);
cm.Parameters.AddWithValue("@branch", Settings.Default.BranchID);
cm.ExecuteNonQuery();
dr = cm.ExecuteReader();
while (dr.Read())
{
if (!id.Contains(dr[0].ToString()))
{
id.Add(dr[0].ToString());
i += 1;
products.Rows.Add(i, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), Convert.ToDateTime(dr[4].ToString()).ToString("dddd, dd MMMM yyyy HH:mm:ss"),
Settings.Default.currrencyCode + " " + dr[5].ToString(), Settings.Default.currrencyCode + " " + dr[6].ToString());
}
}
dr.Close();
cn.Close();
products.ClearSelection();
}));
return 1;
}
catch
{
cn.Close();
return 0;
}
}
public int loadProduct()
{
productAmendments.Clear();
try
{
string name = "";
cn.Open();
cm = new SqlCommand("Select product_name,price from tblProduct where pcode = @pcode " +
"Select tp.product_name,tp.pcode,tp.pdesc,tp.price as basePrice,uni.unitname as baseUnit," +
"tp.baseUnit as baseunitCode,um.unitname,PAU.unitCode as unitCode,PAU.[price/unit] as unitPrice," +
"PAU.distinctiveCode as DistinctiveCode from tblProduct tp Inner Join ProductAltUnit PAU on PAU.pcode = tp.pcode " +
"Inner Join UnitOfMeasure um On um.unitCode = PAU.unitCode Inner Join UnitOfMeasure uni On uni.unitCode = tp.baseUnit " +
"where tp.pcode = @pcode order by DistinctiveCode asc", cn);
txtProductID.Invoke(new Action(() => cm.Parameters.AddWithValue("@pcode", txtProductID.Text)));
cm.ExecuteNonQuery();
dr = cm.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
lblProductName.Invoke(new Action(() =>
{
lblProductName.Text = dr[0].ToString().ToUpper();
previousPrice.Text = Settings.Default.currrencyCode + " " + dr[1].ToString();
newPrice.Enabled = true;
BTNsave.Enabled = true;
name = lblProductName.Text;
}));
}
dr.NextResult();
while (dr.Read())
{
ProductAmendment product = new ProductAmendment();
product.productCode = dr["pcode"].ToString();
product.productname = dr["product_name"].ToString();
product.productDescription = dr["pdesc"].ToString();
product.baseUnitCode = dr["baseunitCode"].ToString();
product.baseUnitName = dr["baseUnit"].ToString();
product.baseUnitprice = dr["basePrice"].ToString();
product.unitCode = dr["unitCode"].ToString();
product.unitName = dr["unitname"].ToString();
product.unitprice = dr["unitPrice"].ToString();
product.unitDistinctive = dr["DistinctiveCode"].ToString();
productAmendments.Add(product);
}
dr.Close();
cn.Close();
if (String.IsNullOrEmpty(name))
{
lblProductName.Invoke(new Action(() =>
{
lblProductName.Text = "";
previousPrice.Text = "";
newPrice.Enabled = false;
BTNsave.Enabled = false;
}));
dr.Close();
cn.Close();
return 2;
}
return 1;
}
catch
{
lblProductName.Invoke(new Action(() =>
{
lblProductName.Text = "";
previousPrice.Text = "";
newPrice.Enabled = false;
BTNsave.Enabled = false;
}));
cn.Close();
return 0;
}
}
private async void Prices_Load(object sender, EventArgs e)
{
Task<int> task = new Task<int>(LoadRecords);
holding.Visible = true;
task.Start();
await task;
holding.Visible = false;
}
private async void BTNsave_Click(object sender, EventArgs e)
{
decimal newCost = decimal.Parse(newPrice.Text);
string previous = previousPrice.Text.Substring(Settings.Default.currrencyCode.Length);
string pcode = txtProductID.Text;
Task<int> task = new Task<int>(() => updatePrice(newCost, previous, pcode));
holding2.Visible = true;
task.Start();
int result = await task;
if (result == 1)
{
string Message = "You have successfully changed the price of " + lblProductName.Text + " from " + previousPrice.Text + " to "
+ Settings.Default.currrencyCode + " " + newCost.ToString();
string Title = "Price changes successful";
NoAction action = new NoAction(message: Message, title: Title);
action.BringToFront();
action.ShowDialog();
LoadRecords();
}
else
{
string Message = "An error occurred while changing the price of " + lblProductName.Text + " please try again later";
string Title = "Error while making changes";
NoAction action = new NoAction(message: Message, title: Title);
action.BringToFront();
action.ShowDialog();
}
holding2.Visible = false;
}
private async void Button1_Click(object sender, EventArgs e)
{
Task<int> task = new Task<int>(loadProduct);
holding2.Visible = true;
task.Start();
int result = await task;
if (result == 1)
{
if (productAmendments.Count > 0)
{
PriceAmendentForm form = new PriceAmendentForm(productAmendments);
form.BringToFront();
form.ShowDialog();
if(form.DialogResult == DialogResult.OK)
{
LoadRecords();
}
txtProductID.Text = "";
lblProductName.Text = "";
newPrice.Text = "";
previousPrice.Text = "";
BTNsave.Enabled = false;
}
}
else if (result == 2)
{
string Message = "The product code you have enter could not be found!!!";
string Title = "Product not found";
NoAction action = new NoAction(message: Message, title: Title);
action.BringToFront();
action.ShowDialog();
}
else
{
string Message = "An error has occurred while looking up the product code!!!";
string Title = "Error Occurred";
NoAction action = new NoAction(message: Message, title: Title);
action.BringToFront();
action.ShowDialog();
}
}
private async void TxtProductID_TextChanged(object sender, EventArgs e)
{
lblProductName.Text = "";
previousPrice.Text = "";
BTNsave.Enabled = false;
}
private void newPrice_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 46)
{
// accepts . character
}
else if (e.KeyChar == 8)
{
//accepts backspace
}
else if ((e.KeyChar < 48) || (e.KeyChar > 57)) //ascii code 48-57 between 0-9
{
e.Handled = true;
}
}
private async void txtSearch_TextChanged(object sender, EventArgs e)
{
Task<int> task = new Task<int>(LoadRecords);
holding.Visible = true;
task.Start();
await task;
holding.Visible = false;
}
}
}