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.Delivery { public partial class TruckStockDown : Form { public int quantity = 0; public string total; string truckID; string pcode; string unit; int oldstock; SqlConnection cn; SqlCommand cm; DatabaseConn conn = new DatabaseConn(); SqlDataReader dr; public TruckStockDown(string truckID, string pcode, string name, int current,string unit,string unitCode) { InitializeComponent(); this.truckID = truckID; this.pcode = pcode; this.unit = unitCode; lblPname.Text = name; lblUnit.Text = unit; cn = new SqlConnection(conn.MyConnection()); oldstock = current; } private void Exit_MouseEnter(object sender, EventArgs e) { exit.BackColor = Color.Crimson; } private void Exit_MouseLeave(object sender, EventArgs e) { exit.BackColor = Color.FromArgb(20, 158, 132); } private void Exit_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.None; this.Close(); } private async void Button1_Click(object sender, EventArgs e) { quantity = int.Parse(stockQuantity.Text); if (quantity > oldstock) { this.DialogResult = DialogResult.Cancel; this.Close(); } else { Task task = new Task(offLoad); holding.Visible = true; task.Start(); int result = await task; if (result == 1) { this.DialogResult = DialogResult.OK; this.Close(); } else { this.DialogResult = DialogResult.Abort; this.Close(); } holding.Visible = false; } } private int offLoad() { cn.Open(); SqlTransaction transaction; transaction = cn.BeginTransaction(); try { cm = new SqlCommand("Update tblTruckInventory set quantity = quantity - @quantity where truckID = @truckID and pcode = @pcode and unit = @unit", cn, transaction); cm.Parameters.AddWithValue("@truckID", truckID); cm.Parameters.AddWithValue("@quantity", quantity); cm.Parameters.AddWithValue("@pcode", pcode); cm.Parameters.AddWithValue("@unit", unit); cm.ExecuteNonQuery(); cm = 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); cm.Parameters.AddWithValue("@pcode", pcode); cm.Parameters.AddWithValue("@date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); cm.Parameters.AddWithValue("@branchID", Form1.branch); cm.Parameters.AddWithValue("@countID", pcode + Form1.branch + DateTime.Now.ToString("yyyyMMddHHmmssfff")); cm.Parameters.AddWithValue("@unit", unit); cm.Parameters.AddWithValue("@quantity", quantity); cm.ExecuteNonQuery(); cm = new SqlCommand("Select SUM(TP.price * TI.quantity) from tblTruckInventory TI Inner Join tblProduct TP On TP.pcode = TI.pcode " + "where TI.truckID = @truckID and TP.branchID = @branchID and TI.pcode = @pcode and TI.unit = TP.baseUnit " + "Select SUM(PAU.[price/unit] * TI.quantity) from tblTruckInventory TI Inner Join ProductAltUnit PAU On PAU.pcode = TI.pcode " + "where TI.truckID = @truckID and PAU.branchID = @branchID and TI.pcode = @pcode and TI.unit = PAU.unitCode", cn, transaction); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@truckID", truckID); cm.Parameters.AddWithValue("@pcode", pcode); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); dr.Read(); if (dr.HasRows) { total = Settings.Default.currrencyCode + " " + dr[0].ToString(); } else { dr.NextResult(); if (dr.HasRows) total = Settings.Default.currrencyCode + " " + dr[0].ToString(); } dr.Close(); transaction.Commit(); cn.Close(); return 1; } catch (Exception ex) { cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); return 0; } } private void StockQuantity_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 8) { //accepts backspace } else if ((e.KeyChar < 48) || (e.KeyChar > 57)) //ascii code 48-57 between 0-9 { e.Handled = true; } } } }