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.

380 lines
17 KiB

using BarcodeLib;
using BiskLog_Point_Of_Sale.Classes;
using BiskLog_Point_Of_Sale.Company_Setup;
using BiskLog_Point_Of_Sale.POSDialogs;
using BiskLog_Point_Of_Sale.Properties;
using Microsoft.Reporting.WinForms;
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.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BiskLog_Point_Of_Sale.Invoice
{
public partial class AddInvoice : Form
{
SqlConnection cn;
DatabaseConn conn = new DatabaseConn();
SqlCommand cm;
SqlDataReader dr;
SqlTransaction transaction;
InvoiceMain invoiceMain;
List<InvoiceObj> ProductNCode = new List<InvoiceObj>();
List<InvoiceObj> Selected = new List<InvoiceObj>();
List<string> invoiced = new List<string>();
public static List<ReceiptObject> receiptClasses = new List<ReceiptObject>();
public static List<bottom> receiptClasses2 = new List<bottom>();
string printLocation = "";
string printerReceiptDefault = Settings.Default.ReportPrinter;
int i = 1;
string invoiceIDGenerated;
string name, address;
public AddInvoice(InvoiceMain main)
{
InitializeComponent();
invoiceMain = main;
cn = new SqlConnection(conn.MyConnection());
}
private void CloseBTN_Click(object sender, EventArgs e)
{
this.Close();
}
public int LoadProducts()
{
ProductNCode.Clear();
try
{
cn.Open();
cm = new SqlCommand("Select pcode, product_name,baseUnit,uni.unitName,pdesc from tblProduct Inner Join UnitOfMeasure uni On uni.unitCode = tblProduct.baseUnit " +
"where tblProduct.branchID = @branchID " +
"Select tblProduct.pcode, product_name,uni.unitName,PAU.unitCode,tblProduct.pdesc,PAU.distinctiveCode from tblProduct Inner Join ProductAltUnit PAU On PAU.pcode = tblProduct.pcode Inner " +
"Join UnitOfMeasure uni On uni.unitCode = PAU.unitCode where tblProduct.branchID = @branchID", cn);
cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID);
cm.ExecuteNonQuery();
dr = cm.ExecuteReader();
while (dr.Read())
{
InvoiceObj invoice = new InvoiceObj();
invoice.pcode = dr[0].ToString();
invoice.productName = dr[1].ToString();
invoice.unitCode = dr[2].ToString();
invoice.unitName = dr[3].ToString();
invoice.description = dr[4].ToString();
invoice.distinctive = dr[0].ToString();
ProductNCode.Add(invoice);
}
dr.NextResult();
while (dr.Read())
{
InvoiceObj invoice = new InvoiceObj();
invoice.pcode = dr[0].ToString();
invoice.productName = dr[1].ToString();
invoice.unitCode = dr[3].ToString();
invoice.unitName = dr[2].ToString();
invoice.description = dr[4].ToString();
invoice.distinctive = dr[5].ToString();
ProductNCode.Add(invoice);
}
dr.Close();
cn.Close();
return 1;
}
catch
{
cn.Close();
return 0;
}
}
private void CloseBTN_MouseEnter(object sender, EventArgs e)
{
closeBTN.BackColor = Color.Crimson;
}
private void CloseBTN_MouseLeave(object sender, EventArgs e)
{
closeBTN.BackColor = Color.Transparent;
}
private async void AddInvoice_Load(object sender, EventArgs e)
{
Task<int> task = new Task<int>(LoadProducts);
task.Start();
await task;
comboBox2.DataSource = ProductNCode;
comboBox2.DisplayMember = "productName";
comboBox2.ValueMember = "pcode";
comboBox2.SelectedIndex = -1;
}
public int AddToList(int quantity, string productCode)
{
try
{
string kel = "";
invoiceDetails.Invoke(new Action(() =>
{
kel = (comboBox3.SelectedItem as InvoiceObj).unitCode;
}));
cn.Open();
cm = new SqlCommand("Declare @baseunit varchar(150) = (Select baseUnit from tblProduct where pcode = @pcode) " +
"Declare @dquantity int = Case when @baseUnit = @unit then 1 else " +
"(Select [quantity/unit] as quantity from ProductAltUnit where pcode = @pcode and unitCode = @unit and branchID = @branchID) " +
"end " +
"Declare @costprice decimal(19, 2) = Case when @baseUnit = @unit then (Select price from tblProduct where tblProduct.pcode = @pcode " +
"and tblProduct.branchID = @branchID) else " +
"(Select [price/unit] from ProductAltUnit where pcode = @pcode and unitCode = @unit and branchID = @branchID) " +
"end " +
"Select @costprice,I.quantity / @dquantity as quantity from tblProduct Inner Join tblInventory I On tblProduct.pcode = I.pcode " +
"where tblProduct.pcode = @pcode and tblProduct.branchID = @branchID", cn);
cm.Parameters.AddWithValue("@pcode", productCode);
cm.Parameters.AddWithValue("@unit", kel);
cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID);
cm.ExecuteNonQuery();
dr = cm.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
if (quantity <= int.Parse(dr[1].ToString()))
{
decimal price = decimal.Parse(dr[0].ToString());
invoiceDetails.Invoke(new Action(() =>
{
string distinctive = (comboBox3.SelectedItem as InvoiceObj).distinctive.ToString();
if (!invoiced.Contains(distinctive))
{
decimal totalPrice = quantity * price;
invoiceDetails.Rows.Add(i, productCode, comboBox2.Text, quantity.ToString(), comboBox3.Text, Settings.Default.currrencyCode + " " +
totalPrice.ToString(), kel, distinctive);
invoiced.Add(distinctive);
}
else
{
foreach (DataGridViewRow row in invoiceDetails.Rows)
{
if (row.Cells[7].Value.ToString().Equals(distinctive))
{
int total_quantity = int.Parse(row.Cells[3].Value.ToString());
total_quantity += quantity;
decimal totalPrice = total_quantity * price;
row.Cells[3].Value = total_quantity;
row.Cells[5].Value = Settings.Default.currrencyCode + " " + totalPrice;
}
}
}
}));
i++;
dr.Close();
cn.Close();
return 1;
}
else
{
invoiceDetails.Invoke(new Action(() =>
{
DESC.Text = DESC.Text + " \n only " + dr[1].ToString() + " " + comboBox3.Text + " of " + comboBox2.Text + " available !!!";
}));
dr.Close();
cn.Close();
return 2;
}
}
else
{
dr.Close();
cn.Close();
return 0;
}
}
catch
{
cn.Close();
return 0;
}
}
private void Button1_Click(object sender, EventArgs e)
{
string code = (comboBox2.SelectedItem as InvoiceObj).pcode.ToString();
quantityInvoice quantity = new quantityInvoice(this, code);
quantity.BringToFront();
quantity.ShowDialog();
}
private void InvoiceDetails_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
public Image getBarcodeImage(string Bcode)
{
Barcode barcodeAPI = new Barcode();
Color foreColor = Color.Black;
Color backColor = Color.Transparent;
return barcodeAPI.Encode(TYPE.UPCA, Bcode, foreColor, backColor, 290, 120);
}
public int GenerateInvoice()
{
try
{
receiptClasses.Clear();
receiptClasses2.Clear();
cn.Open();
transaction = cn.BeginTransaction();
invoiceIDGenerated = String.Format("{0:d12}", (DateTime.Now.Ticks / 10) % 1000000000);
invoiceDetails.Invoke(new Action(() =>
{
decimal allTotal = 0;
foreach (DataGridViewRow row in invoiceDetails.Rows)
{
decimal total = decimal.Parse(row.Cells[5].Value.ToString().Substring(Settings.Default.currrencyCode.Length));
int qty = int.Parse(row.Cells[3].Value.ToString());
cm = new SqlCommand("Insert into tblInvoice (invoiceID,pcode,quantity,totalprice,unitprice,dateGenerated,status,generatedBy,branchID,countID,unit) " +
"Values (@invoiceID,@pcode,@quantity,@totalprice,@unitprice,@dateGenerated,@status,@generatedBy,@branchID,@countID,@unit)", cn, transaction);
cm.Parameters.AddWithValue("@invoiceID", invoiceIDGenerated);
cm.Parameters.AddWithValue("@pcode", row.Cells[1].Value.ToString());
cm.Parameters.AddWithValue("@unit", row.Cells[6].Value.ToString());
cm.Parameters.AddWithValue("@quantity", qty);
cm.Parameters.AddWithValue("@totalprice", total);
cm.Parameters.AddWithValue("@unitprice", (total / qty));
cm.Parameters.AddWithValue("@dateGenerated", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
cm.Parameters.AddWithValue("@status", "PENDING PURCHASE");
cm.Parameters.AddWithValue("@generatedBy", MainLogin.login_user);
cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID);
cm.Parameters.AddWithValue("@countID", row.Cells[1].Value.ToString() + DateTime.Now.ToString("yyyyMMddHHmmssfff"));
cm.ExecuteNonQuery();
ReceiptObject receipt = new ReceiptObject(row.Cells[2].Value.ToString(), total / qty, qty, total);
receiptClasses.Add(receipt);
allTotal += total;
}
transaction.Commit();
MemoryStream ms = new MemoryStream();
getBarcodeImage(invoiceIDGenerated).Save(ms, ImageFormat.Png);
byte[] imageBytes = ms.ToArray();
string barcodeStringImage = Convert.ToBase64String(imageBytes);
bottom bottomReceipt = new bottom("", invoiceIDGenerated, MainLogin.login_user, allTotal, allTotal, "", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
"", "", Form1.bCompanyName, Form1.branchName, Form1.branchLocation, Form1.branchTel, Form1.tin, Form1.vatno,
barcodeStringImage, name, address);
receiptClasses2.Add(bottomReceipt);
if (printerReceiptDefault == "Microsoft XPS Document Writer" || printerReceiptDefault == "Microsoft Print to PDF")
{
FolderBrowserDialog folder = new FolderBrowserDialog();
DialogResult result = folder.ShowDialog();
if (result == DialogResult.OK && !String.IsNullOrEmpty(folder.SelectedPath))
{
printLocation = folder.SelectedPath;
LocalReport report = new LocalReport();
string path = Path.GetDirectoryName(Application.ExecutablePath);
string fullpath = Path.GetDirectoryName(Application.ExecutablePath).Remove(path.Length - 10) +
@"\ProFormar.rdlc";
report.ReportPath = fullpath;
report.DataSources.Add(new ReportDataSource("ReceiptReport", receiptClasses));
report.DataSources.Add(new ReportDataSource("ReceiptRest", receiptClasses2));
ReportConfig.PrintToPrinter(report, printLocation, printerReceiptDefault);
}
}
else
{
printLocation = "";
LocalReport report = new LocalReport();
string path = Path.GetDirectoryName(Application.ExecutablePath);
string fullpath = Path.GetDirectoryName(Application.ExecutablePath).Remove(path.Length - 10) +
@"\ProFormar.rdlc";
report.ReportPath = fullpath;
report.DataSources.Add(new ReportDataSource("ReceiptReport", receiptClasses));
report.DataSources.Add(new ReportDataSource("ReceiptRest", receiptClasses2));
ReportConfig.PrintToPrinter(report, printLocation, printerReceiptDefault);
}
}));
cn.Close();
return 1;
}
catch
{
transaction.Rollback();
cn.Close();
return 0;
}
}
private async void BtnGenerate_Click(object sender, EventArgs e)
{
FillDetails fillDetails = new FillDetails();
fillDetails.BringToFront();
fillDetails.ShowDialog();
if (fillDetails.DialogResult == DialogResult.OK)
{
name = fillDetails.customerName;
address = fillDetails.address;
Task<int> task = new Task<int>(() =>
{
GenerateInvoice();
invoiceMain.LoadInvoice();
return 1;
});
task.Start();
await task;
}
}
private void ComboBox2_TextUpdate(object sender, EventArgs e)
{
//string filter_param = comboBox2.Text;
//List<string> filteredItems = ProductNCode.ToList().FindAll(x => x.Contains(filter_param.ToUpper()));
//comboBox2.DataSource = filteredItems;
//if (String.IsNullOrWhiteSpace(filter_param))
//{
// comboBox2.DataSource = ProductNCode.Values.ToList();
//}
//comboBox2.DroppedDown = true;
//comboBox2.IntegralHeight = true;
//comboBox2.SelectedIndex = -1;
//comboBox2.Text = filter_param;
//comboBox2.SelectionStart = filter_param.Length;
//comboBox2.SelectionLength = 0;
//selectProductCode(comboBox2.Text);
}
private void ComboBox2_TextChanged(object sender, EventArgs e)
{
Selected.Clear();
if (!String.IsNullOrEmpty(comboBox2.Text))
{
foreach (InvoiceObj invoiceObj in ProductNCode)
{
if (invoiceObj.productName.Equals(comboBox2.Text))
{
if (!Selected.Contains(invoiceObj))
Selected.Add(invoiceObj);
}
}
comboBox3.DataSource = null;
comboBox3.DataSource = Selected;
comboBox3.DisplayMember = "unitName";
comboBox3.ValueMember = "unitCode";
comboBox3.SelectedIndex = -1;
if (comboBox2.SelectedItem != null)
{
comboBox3.Text = (comboBox2.SelectedItem as InvoiceObj).unitName.ToString();
DESC.Text = (comboBox2.SelectedItem as InvoiceObj).description.ToString();
}
}
else
{
comboBox3.DataSource = null;
comboBox3.DataSource = Selected;
DESC.Text = "";
}
}
}
}