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.
291 lines
13 KiB
291 lines
13 KiB
3 months ago
|
using BiskLog_Point_Of_Sale.Classes;
|
||
|
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 PriceAmendentForm : Form
|
||
|
{
|
||
|
SqlConnection cn;
|
||
|
SqlCommand cm;
|
||
|
DatabaseConn conn = new DatabaseConn();
|
||
|
SqlTransaction transaction;
|
||
|
List<ProductAmendment> products;
|
||
|
string productCode,description,productname;
|
||
|
public PriceAmendentForm(List<ProductAmendment> products = null,string productCode = null,string productname = null,string productdescription = null)
|
||
|
{
|
||
|
InitializeComponent();
|
||
|
cn = new SqlConnection(conn.MyConnection());
|
||
|
holding.Left = (ClientSize.Width - holding.Width) / 2;
|
||
|
holding.Top = (ClientSize.Height - holding.Height) / 2;
|
||
|
this.products = products;
|
||
|
this.productCode = productCode;
|
||
|
this.productname = productname;
|
||
|
this.description = productdescription;
|
||
|
|
||
|
}
|
||
|
|
||
|
private void exitBTN_Click(object sender, EventArgs e)
|
||
|
{
|
||
|
this.Close();
|
||
|
}
|
||
|
|
||
|
private void exitBTN_MouseEnter(object sender, EventArgs e)
|
||
|
{
|
||
|
exitBTN.BackColor = Color.Crimson;
|
||
|
}
|
||
|
|
||
|
private void exitBTN_MouseLeave(object sender, EventArgs e)
|
||
|
{
|
||
|
exitBTN.BackColor = Color.Transparent;
|
||
|
}
|
||
|
|
||
|
private async void PriceAmendentForm_Load(object sender, EventArgs e)
|
||
|
{
|
||
|
if (String.IsNullOrEmpty(productCode))
|
||
|
{
|
||
|
setBoxes();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
txtDesc.Text = description;
|
||
|
name.Text = productname;
|
||
|
pcode.Text = productCode;
|
||
|
await getUnits();
|
||
|
setBoxes();
|
||
|
}
|
||
|
}
|
||
|
private void setBoxes()
|
||
|
{
|
||
|
foreach (ProductAmendment product in products)
|
||
|
{
|
||
|
//name.Text = product.productname;
|
||
|
//pcode.Text = product.productCode;
|
||
|
//txtDesc.Text = product.productDescription;
|
||
|
if (String.IsNullOrEmpty(baseUnitCode.Text))
|
||
|
{
|
||
|
baseName.Text = product.baseUnitName;
|
||
|
baseCurrent.Text = product.baseUnitprice;
|
||
|
baseUnitCode.Text = product.baseUnitCode;
|
||
|
baseNew.Enabled = true;
|
||
|
baseNew.Text = product.baseUnitprice; ;
|
||
|
}
|
||
|
if (String.IsNullOrEmpty(unit1Code.Text))
|
||
|
{
|
||
|
unit1Code.Text = product.unitDistinctive;
|
||
|
unit1Name.Text = product.unitName;
|
||
|
unit1Current.Text = product.unitprice;
|
||
|
unit1New.Enabled = true;
|
||
|
unit1New.Text = product.unitprice;
|
||
|
}
|
||
|
else if (String.IsNullOrEmpty(unit2Code.Text))
|
||
|
{
|
||
|
unit2Code.Text = product.unitDistinctive;
|
||
|
unit2Name.Text = product.unitName;
|
||
|
unit2Current.Text = product.unitprice;
|
||
|
unit2New.Enabled = true;
|
||
|
unit2New.Text = product.unitprice;
|
||
|
}
|
||
|
else if (String.IsNullOrEmpty(unit3Code.Text))
|
||
|
{
|
||
|
unit3Code.Text = product.unitDistinctive;
|
||
|
unit3Name.Text = product.unitName;
|
||
|
unit3Current.Text = product.unitprice;
|
||
|
unit3New.Enabled = true;
|
||
|
unit3New.Text = product.unitprice;
|
||
|
}
|
||
|
else if (String.IsNullOrEmpty(unit4Code.Text))
|
||
|
{
|
||
|
unit4Code.Text = product.unitDistinctive;
|
||
|
unit4Name.Text = product.unitName;
|
||
|
unit4Current.Text = product.unitprice;
|
||
|
unit4New.Enabled = true;
|
||
|
unit4New.Text = product.unitprice;
|
||
|
}
|
||
|
else if (String.IsNullOrEmpty(unit5Code.Text))
|
||
|
{
|
||
|
unit5Code.Text = product.unitDistinctive;
|
||
|
unit5Name.Text = product.unitName;
|
||
|
unit5Current.Text = product.unitprice;
|
||
|
unit5New.Enabled = true;
|
||
|
unit5New.Text = product.unitprice;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
private async void btnSave_Click(object sender, EventArgs e)
|
||
|
{
|
||
|
holding.Visible = true;
|
||
|
Task<int> task = new Task<int>(amendPricing);
|
||
|
task.Start();
|
||
|
int result = await task;
|
||
|
if (result == 1)
|
||
|
{
|
||
|
this.DialogResult = DialogResult.OK;
|
||
|
string Message = "You have successfully changed the pricing of " + name.Text;
|
||
|
string Title = "Price changes successful";
|
||
|
NoAction action = new NoAction(message: Message, title: Title);
|
||
|
action.BringToFront();
|
||
|
action.ShowDialog();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
string Message = "An error occurred while changing the price of " + name.Text + " 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 amendPricing()
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
baseUnitCode.Invoke(new Action(() =>
|
||
|
{
|
||
|
cn.Open();
|
||
|
transaction = cn.BeginTransaction();
|
||
|
if (!String.IsNullOrEmpty(baseUnitCode.Text) && !String.IsNullOrEmpty(baseNew.Text))
|
||
|
{
|
||
|
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", pcode.Text);
|
||
|
cm.Parameters.AddWithValue("@previous_price", baseCurrent.Text);
|
||
|
cm.Parameters.AddWithValue("@current_price", baseNew.Text);
|
||
|
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", pcode.Text + Settings.Default.BranchID + DateTime.Now.ToString("yyyyMMddHHmmssfff"));
|
||
|
cm.Transaction = transaction;
|
||
|
cm.ExecuteNonQuery();
|
||
|
cm = new SqlCommand("Update tblProduct set price = @price where pcode = @pcode and branchID = @branchID", cn);
|
||
|
cm.Parameters.AddWithValue("@pcode", pcode.Text);
|
||
|
cm.Parameters.AddWithValue("@price", baseNew.Text);
|
||
|
cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID);
|
||
|
cm.Transaction = transaction;
|
||
|
cm.ExecuteNonQuery();
|
||
|
}
|
||
|
if (!String.IsNullOrEmpty(unit1Code.Text) && !String.IsNullOrEmpty(unit1New.Text))
|
||
|
{
|
||
|
cm = new SqlCommand("Update ProductAltUnit set [price/unit] = @price where distinctiveCode = @distinctiveCode and branchID = @branchID", cn);
|
||
|
cm.Parameters.AddWithValue("@distinctiveCode", unit1Code.Text);
|
||
|
cm.Parameters.AddWithValue("@price", unit1New.Text);
|
||
|
cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID);
|
||
|
cm.Transaction = transaction;
|
||
|
cm.ExecuteNonQuery();
|
||
|
}
|
||
|
if (!String.IsNullOrEmpty(unit2Code.Text) && !String.IsNullOrEmpty(unit2New.Text))
|
||
|
{
|
||
|
cm = new SqlCommand("Update ProductAltUnit set [price/unit] = @price where distinctiveCode = @distinctiveCode and branchID = @branchID", cn);
|
||
|
cm.Parameters.AddWithValue("@distinctiveCode", unit2Code.Text);
|
||
|
cm.Parameters.AddWithValue("@price", unit2New.Text);
|
||
|
cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID);
|
||
|
cm.Transaction = transaction;
|
||
|
cm.ExecuteNonQuery();
|
||
|
}
|
||
|
if (!String.IsNullOrEmpty(unit3Code.Text) && !String.IsNullOrEmpty(unit3New.Text))
|
||
|
{
|
||
|
cm = new SqlCommand("Update ProductAltUnit set [price/unit] = @price where distinctiveCode = @distinctiveCode and branchID = @branchID", cn);
|
||
|
cm.Parameters.AddWithValue("@distinctiveCode", unit3Code.Text);
|
||
|
cm.Parameters.AddWithValue("@price", unit3New.Text);
|
||
|
cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID);
|
||
|
cm.Transaction = transaction;
|
||
|
cm.ExecuteNonQuery();
|
||
|
}
|
||
|
if (!String.IsNullOrEmpty(unit4Code.Text) && !String.IsNullOrEmpty(unit4New.Text))
|
||
|
{
|
||
|
cm = new SqlCommand("Update ProductAltUnit set [price/unit] = @price where distinctiveCode = @distinctiveCode and branchID = @branchID", cn);
|
||
|
cm.Parameters.AddWithValue("@distinctiveCode", unit4Code.Text);
|
||
|
cm.Parameters.AddWithValue("@price", unit4New.Text);
|
||
|
cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID);
|
||
|
cm.Transaction = transaction;
|
||
|
cm.ExecuteNonQuery();
|
||
|
}
|
||
|
if (!String.IsNullOrEmpty(unit5Code.Text) && !String.IsNullOrEmpty(unit5New.Text))
|
||
|
{
|
||
|
cm = new SqlCommand("Update ProductAltUnit set [price/unit] = @price where distinctiveCode = @distinctiveCode and branchID = @branchID", cn);
|
||
|
cm.Parameters.AddWithValue("@distinctiveCode", unit5Code.Text);
|
||
|
cm.Parameters.AddWithValue("@price", unit5New.Text);
|
||
|
cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID);
|
||
|
cm.Transaction = transaction;
|
||
|
cm.ExecuteNonQuery();
|
||
|
}
|
||
|
transaction.Commit();
|
||
|
cn.Close();
|
||
|
}));
|
||
|
return 1;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
transaction.Rollback();
|
||
|
cn.Close();
|
||
|
ErrorLogging.WriteToFile(ex.ToString());
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
public async Task getUnits()
|
||
|
{
|
||
|
cn.Open();
|
||
|
try
|
||
|
{
|
||
|
products = new List<ProductAmendment>();
|
||
|
SqlDataReader dr;
|
||
|
SqlCommand sqlCommand = new SqlCommand($"EXEC usp_get_product_unit_price_amendmentForm @pcode = @pcode1,@branchID = @branchID1", cn);
|
||
|
sqlCommand.Parameters.AddWithValue("@pcode1", productCode);
|
||
|
sqlCommand.Parameters.AddWithValue("@branchID1", Form1.branch);
|
||
|
sqlCommand.ExecuteNonQuery();
|
||
|
dr = sqlCommand.ExecuteReader();
|
||
|
while (dr.Read())
|
||
|
{
|
||
|
ProductAmendment productUnits = new ProductAmendment
|
||
|
{
|
||
|
productCode = productCode,
|
||
|
productname = productname,
|
||
|
productDescription = description,
|
||
|
baseUnitName = dr["baseUnit"].ToString(),
|
||
|
baseUnitCode = dr["baseunitCode"].ToString(),
|
||
|
baseUnitprice = dr["basePrice"].ToString(),
|
||
|
unitCode = dr["unitCode"].ToString(),
|
||
|
unitName = dr["unitname"].ToString(),
|
||
|
unitprice = dr["unitPrice"].ToString(),
|
||
|
unitDistinctive = dr["DistinctiveCode"].ToString(),
|
||
|
};
|
||
|
this.products.Add(productUnits);
|
||
|
}
|
||
|
cn.Close();
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
ErrorLogging.WriteToFile(ex.ToString());
|
||
|
cn.Close();
|
||
|
}
|
||
|
}
|
||
|
private void baseNew_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;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|