using BiskLog_Point_Of_Sale.Classes; using BiskLog_Point_Of_Sale.Properties; using Microsoft.Reporting.WinForms; using Point_Of_Sale_Managment; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace BiskLog_Point_Of_Sale.Delivery { public class RegenerateInvoice { public static void generateFile(string deliveryID) { SqlConnection cn; SqlCommand cm; SqlDataReader dr; DatabaseConn conn = new DatabaseConn(); cn = new SqlConnection(conn.MyConnection()); try { List deliveryInvoice = new List(); List productsList = new List(); string fullname, address, telephone, email, charges, itemTotal, fromDate, toDate, allTotal; OrderInvoice order = null; cn.Open(); cm = new SqlCommand("Declare @totalProduct decimal(19,2) = (Select Sum(cost) from tblDeliveryDetails where deliveryID = @deliveryID) " + "Declare @Addition decimal(19, 2) = case when(Select totalCost from tblDeliveryHead where deliveryID = @deliveryID) - @totalProduct <= 0 then 0 " + "else (Select totalCost from tblDeliveryHead where deliveryID = @deliveryID) - @totalProduct end " + "Select DR.fullname,DR.address,DR.telephone,DR.email,DR.fromDate,DR.toDate,DH.totalCost,DP.product_name,DD.quantity,DD.cost,@totalProduct,@Addition,DD.pcode,u.unitname from " + "tblDeliveryDetails DD Inner Join tblDeliveryRecipients DR on DR.deliveryID = DD.deliveryID Inner Join tblDeliveryHead DH On DH.deliveryID = DD.deliveryID Inner Join " + "tblProduct DP On DP.pcode = DD.pcode Inner Join UnitOfMeasure u On u.unitCode = DD.unit where DD.deliveryID = @deliveryID and DH.branchID = @branchID", cn); cm.Parameters.AddWithValue("@deliveryID", deliveryID); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); while (dr.Read()) { fullname = dr[0].ToString().ToUpper(); address = dr[1].ToString().ToUpper(); telephone = dr[2].ToString(); email = dr[3].ToString(); fromDate = dr[4].ToString(); toDate = dr[5].ToString(); charges = Settings.Default.currrencyCode + " " + dr[11].ToString(); itemTotal = Settings.Default.currrencyCode + " " + dr[10].ToString(); allTotal = Settings.Default.currrencyCode + " " + dr[6].ToString(); products p = new products(dr[7].ToString().ToUpper(), dr[8].ToString(), Settings.Default.currrencyCode + " " + dr[9].ToString(),dr[12].ToString()); productsList.Add(p); order = new OrderInvoice( orderDate: DateTime.Now.ToString("yyyy-MM-dd"), beginDate: fromDate, endDate: toDate, ProductsCost: itemTotal, additional: charges, All: allTotal, address: address, email: email, telephone: telephone, branchtelephone: Settings.Default.BranchTelephone, branchLocation: Settings.Default.BranchCity, branchName: Settings.Default.BranchName, orderid: deliveryID, companyName: Settings.Default.CompanyName, customer: fullname ); } dr.Close(); cn.Close(); deliveryInvoice.Add(order); if (!Directory.Exists(Environment.CurrentDirectory + @"\Order Invoice")) { Directory.CreateDirectory(Environment.CurrentDirectory + @"\Order Invoice"); } string printLocation = Environment.CurrentDirectory + @"\Order Invoice\" + deliveryID + ".pdf"; LocalReport report = new LocalReport(); string path = Path.GetDirectoryName(Application.ExecutablePath); string fullpath = Path.GetDirectoryName(Application.ExecutablePath).Remove(path.Length - 10) + @"\Delivery\OrderInvoice.rdlc"; report.ReportPath = fullpath; report.DataSources.Add(new ReportDataSource("Details", deliveryInvoice)); report.DataSources.Add(new ReportDataSource("products", productsList)); OrderInvoiceConfig.PrintToPrinter(report, printLocation, "Microsoft Print to PDF"); } catch (Exception ex) { cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); } } } }