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.

207 lines
9.5 KiB

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;
using BiskLog_Point_Of_Sale.Classes;
using BiskLog_Point_Of_Sale.Delivery;
using BiskLog_Point_Of_Sale.Multiple_Login;
using BiskLog_Point_Of_Sale.Properties;
namespace Point_Of_Sale_Managment
{
public partial class AddProductTruck : Form
{
SqlConnection cn;
SqlCommand command;
DatabaseConn conn = new DatabaseConn();
SqlDataReader dr;
string currency;
public static string id, product_name, price, quantity, unitname, unitcode, distinctive;
FreeLoading formSale;
string truckID;
public AddProductTruck(FreeLoading formsales, string truckID)
{
InitializeComponent();
cn = new SqlConnection(conn.MyConnection());
formSale = formsales;
this.truckID = truckID;
currency = Settings.Default.currrencyCode + " ";
}
private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1)
{
id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
product_name = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
price = (dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString()).Substring(Settings.Default.currrencyCode.Length);
quantity = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
unitname = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
unitcode = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
distinctive = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
if (int.Parse(quantity) <= 0)
{
string title = "Out of stock";
string message = "Sorry, " + product_name + " is out of stock!!";
NoAction noAction = new NoAction(title, message);
noAction.BringToFront();
noAction.ShowDialog();
}
else
{
quantityTruck quant = new quantityTruck(this, int.Parse(quantity));
quant.BringToFront();
quant.ShowDialog();
}
}
}
public async void AddtoCart(int quantity, decimal pPrice)
{
Task<int> task = new Task<int>(() => restock(quantity));
task.Start();
int result = await task;
if (result == 1)
{
formSale.loadProductManual(product_name, quantity, pPrice, id, unitname, unitcode, distinctive);
}
else
{
string title = "Error Occurred";
string message = "An error occurred while adding product to truck inventory";
NoAction noAction = new NoAction(title, message);
noAction.BringToFront();
noAction.ShowDialog();
}
dataGridView1.Rows.Clear();
proID.Text = "";
proNAME.Text = "";
}
public void products(string key)
{
dataGridView1.Rows.Clear();
cn.Open();
command = new SqlCommand("Select tblProduct.pcode,tblProduct.product_name,tblInventory.quantity,tblProduct.price,u.unitname as unitname,tblProduct.baseUnit as unitCode " +
"from tblProduct Inner Join tblInventory On tblInventory.pcode = tblProduct.pcode Inner Join UnitOfMeasure u On u.unitCode = tblProduct.baseUnit " +
"where tblProduct.branchID = @branch and tblProduct.status = 'ACTIVE' and tblProduct.pcode like '%" + key + "%' or " +
"tblProduct.branchID = @branch and tblProduct.status = 'ACTIVE' and tblProduct.product_name like '%" + key + "%' " +
"SELECT PAU.pcode,tp.product_name,uni.unitname as unitname,[price/unit] as price,(ti.quantity / [quantity/unit]) as quantity,PAU.unitCode ,distinctiveCode " +
"FROM ProductAltUnit PAU Inner Join UnitOfMeasure uni On uni.unitCode = PAU.unitCode Inner Join tblProduct tp On tp.pcode = PAU.pcode " +
"Inner Join tblInventory ti On ti.pcode = PAU.pcode where tp.branchID = @branch and tp.status = 'ACTIVE' and PAU.pcode like '%" + key + "%' or " +
"tp.branchID = @branch and tp.status = 'ACTIVE' and tp.product_name like '%" + key + "%'", cn);
command.Parameters.AddWithValue("@branch", Settings.Default.BranchID);
command.ExecuteNonQuery();
dr = command.ExecuteReader();
while (dr.Read())
{
dataGridView1.Rows.Add(dr[0].ToString(), dr["product_name"].ToString(), currency + dr["price"].ToString(), dr["quantity"].ToString(),
dr["unitname"].ToString(), dr["unitCode"].ToString(), dr[0].ToString());
}
dr.NextResult();
while (dr.Read())
{
dataGridView1.Rows.Add(dr[0].ToString(), dr["product_name"].ToString(), currency + dr["price"].ToString(), dr["quantity"].ToString(),
dr["unitname"].ToString(), dr["unitCode"].ToString(), dr["distinctiveCode"].ToString());
}
dr.Close();
cn.Close();
}
private void Label2_Click(object sender, EventArgs e)
{
this.Close();
}
private void Label2_MouseEnter(object sender, EventArgs e)
{
label2.BackColor = Color.Crimson;
}
private void Label2_MouseLeave(object sender, EventArgs e)
{
label2.BackColor = Color.FromArgb(20, 158, 37);
}
private void AddProducttoCart_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}
private void ProID_TextChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(proID.Text))
{
dataGridView1.Rows.Clear();
}
else
{
products(proID.Text);
}
}
private void ProNAME_TextChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(proNAME.Text))
{
dataGridView1.Rows.Clear();
}
else
{
products(proNAME.Text);
}
}
private int restock(int quantity)
{
cn.Open();
SqlTransaction transaction;
transaction = cn.BeginTransaction();
try
{
command = new SqlCommand("; With NewValues AS (Select truckID = @truckID,pcode = @pcode,quantity = @quantity,countID = @countID,unit = @unit) " +
"Merge Into tblTruckInventory As T Using NewValues As NV On(T.pcode = NV.pcode and NV.truckID = t.truckID and T.unit = NV.unit) When Not Matched By Target Then " +
"Insert(truckID, pcode, quantity, countID,unit) Values(NV.truckID, NV.pcode, NV.quantity, NV.countID,NV.unit) When Matched Then " +
"Update set T.quantity = T.quantity + NV.quantity; ", cn, transaction);
command.Parameters.AddWithValue("@truckID", truckID);
command.Parameters.AddWithValue("@quantity", quantity);
command.Parameters.AddWithValue("@pcode", id);
command.Parameters.AddWithValue("@unit", unitcode);
command.Parameters.AddWithValue("@countID", id + Form1.branch + DateTime.Now.ToString("yyyyMMddHHmmssfff"));
command.ExecuteNonQuery();
command = new SqlCommand("Declare @baseunit varchar(150) = (Select baseUnit from tblProduct where pcode = @pcode) " +
"Declare @dquantity int = Case when @baseUnit = @unit then 1 * @quantity else " +
"(Select[quantity/unit] * @quantity as quantity from ProductAltUnit where pcode = @pcode and unitCode = @unit and branchID = @branchID) " +
"end Declare @newQuantity int = (Select MAX(quantity) - MIN(@dquantity) from tblInventory where pcode = @pcode and branchID = @branchID) " +
"Update tblInventory set quantity = @newQuantity where pcode = @pcode and branchID = @branchID " +
"Insert into tblInventoryEntries(pcode, quantity, date, countID, branchID) values (@pcode, @newQuantity, @date, @countID, @branchID)", cn, transaction);
command.Parameters.AddWithValue("@pcode", id);
command.Parameters.AddWithValue("@date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
command.Parameters.AddWithValue("@branchID", Form1.branch);
command.Parameters.AddWithValue("@countID", id + Form1.branch + DateTime.Now.ToString("yyyyMMddHHmmssfff"));
command.Parameters.AddWithValue("@quantity", quantity);
command.Parameters.AddWithValue("@unit", unitcode);
command.ExecuteNonQuery();
transaction.Commit();
cn.Close();
return 1;
}
catch (Exception ex)
{
cn.Close();
ErrorLogging.WriteToFile(ex.ToString());
return 0;
}
}
}
}