diff --git a/Payroll.Report/Class/rptIncomeTax.cs b/Payroll.Report/Class/rptIncomeTax.cs index 10bb017..743a8b4 100644 --- a/Payroll.Report/Class/rptIncomeTax.cs +++ b/Payroll.Report/Class/rptIncomeTax.cs @@ -1803,92 +1803,152 @@ namespace Payroll.Report public void showTaxCardMultiple() { - bool isCurrentYear = false; - DataSet orootDataset = new DataSet(); - fReportViewer form = new fReportViewer(); - IncomeTax oIncomeTax = new IncomeTax(); _TaxParameterID = _selectedParameter.TaxParameters[0].ID; - ObjectsTemplate _taxParameters = TaxParameter.Get(); - if (Payroll.BO.SystemInformation.CurrentSysInfo.TaxParamID == _TaxParameterID) - isCurrentYear = true; - else - isCurrentYear = false; + bool isCurrentYear = Payroll.BO.SystemInformation.CurrentSysInfo.TaxParamID == _TaxParameterID; string sEmpID = _selectedParameter.ReportItem.INSQL; ObjectsTemplate oEMployees = Employee.GetEmpsWithDiscontinue(sEmpID); - if (_TaxParameterID == SystemInformation.CurrentSysInfo.TaxParamID) + BeginTaxCardDoc(isCurrentYear); + foreach (Employee oEmp in oEMployees) { - //SystemInformation.CurrentSysInfo.TaxParamID - _taxParameter = TaxParameter.Get(SystemInformation.CurrentSysInfo.TaxParamID); - m_oDoc = new EPDoc(); - InitDoc(); - foreach (Employee oEmp in oEMployees) - { - _employee = oEmp; - _incometaxes = IncomeTax.Get(oIncomeTax.GetEnumForSalaryIT(oEmp.ID), oEmp.ID, SystemInformation.CurrentSysInfo.TaxParamID); - //As this method has not been used yet then A Boolean value is used as Parameter - _dSet = PrepareDataset(oEmp.ID, isCurrentYear); - AddPage(); - PrintBody(); - PrintSlab(); - } - } - else - { - ObjectsTemplate oTaxChallans = new ObjectsTemplate(); - _taxParameter = TaxParameter.Get(_TaxParameterID); - m_oDoc = new EPDoc(); - InitDoc(); - _isPrvs = true; - foreach (Employee oEmp in oEMployees) - { - _employee = oEmp; - //EnumIncomeTaxDataFrom.ProcessedData - //oIncomeTax.GetEnumForSalaryIT(oEmp.ID) - _incometaxes = IncomeTax.Get(EnumIncomeTaxDataFrom.ProcessedData, oEmp.ID, _TaxParameterID); - //As this method has not been used yet then A Boolean value is used as Parameter - _dSet = PrepareDataset(oEmp.ID, isCurrentYear); - - PayrollDataSet.PayrollDataSet.TaxChallanDataTable dTChallan = new Payroll.Report.PayrollDataSet.PayrollDataSet.TaxChallanDataTable(); - DataRow dChallan = null; - dChallan = dTChallan.NewRow(); - - oTaxChallans = TaxChallan.Get(oEmp.ID, _TaxParameterID); - int count = 1; - string sChallanNo = "";// "# 57 dated 06-Sep-2010, # 56/88 dated 05-Oct-2010, # 54 dated 03-Nov-2010, # 58 dated 07-Dec-2010, # 57 dated 06-Jan-2011, # 56 dated 10-Feb-2011, # 58 dated 09-Mar-2011, # 50 dated 13-Apr-2011, # 47 dated 03-May-2011, # 55 dated 09-Jun-2011, # 71/49 dated 21-Jul-2011."; - if (oTaxChallans != null && oTaxChallans.Count > 0) - { - - foreach (TaxChallan oItem in oTaxChallans) - { - if (count == oTaxChallans.Count) - { - sChallanNo += "Challan: " + oItem.ChallanNo + " Date: " + oItem.DepositDate.ToString("dd-MMM-yyyy"); - //sChallanNo += oItem.ChallanNo; - } - else - { - sChallanNo += "Challan: " + oItem.ChallanNo + " Date: " + oItem.DepositDate.ToString("dd-MMM-yyyy") + ", "; - //sChallanNo += oItem.ChallanNo + ","; - count++; - } - } - } - dChallan["ChallanNo"] = sChallanNo; - dTChallan.Rows.Add(dChallan); - dTChallan.TableName = "PayrollDataSet_TaxChallan"; - _dSet.Tables.Add(dTChallan); - - AddPageForTC(); - PrintBodyForTC(); - PrintSlabForTC(); - } - + AddTaxCardPages(oEmp, isCurrentYear); } m_oDoc.PreviewDoc(); } + /// + /// Builds one employee's tax card (same layout as "Tax Card For Multiple Employee") + /// and returns it as PDF bytes, without any print dialog. Returns null if nothing was drawn. + /// + public byte[] GetTaxCardPdf(Employee oEmp, ID taxParameterID) + { + _TaxParameterID = taxParameterID; + bool isCurrentYear = Payroll.BO.SystemInformation.CurrentSysInfo.TaxParamID == _TaxParameterID; + + BeginTaxCardDoc(isCurrentYear); + AddTaxCardPages(oEmp, isCurrentYear); + return RenderDocToPdf(m_oDoc); + } + + /// + /// Renders an EPDoc's pages to an image-based A4 PDF in memory. + /// EPDoc.PrintDoc() always opens a PrintDialog, so this does the same setup it does + /// (setDevice + start page) via reflection and prints through a PreviewPrintController, + /// which records each page as a metafile instead of sending it to a printer. + /// + private static byte[] RenderDocToPdf(EPDoc doc) + { + const string pdfPrinter = "Microsoft Print to PDF"; + System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings(); + ps.PrinterName = pdfPrinter; + if (ps.IsValid) + doc.PrinterSettings.PrinterName = pdfPrinter; + + System.Reflection.BindingFlags bf = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance; + Type docType = typeof(EPDoc); + docType.GetMethod("setDevice", bf, null, Type.EmptyTypes, null).Invoke(doc, null); + int startPage = (int)docType.GetField("_startPage", bf).GetValue(doc); + docType.GetField("_curPageIndex", bf).SetValue(doc, startPage - 1); + + System.Drawing.Printing.PreviewPrintController controller = new System.Drawing.Printing.PreviewPrintController(); + doc.PrintController = controller; + doc.Print(); + System.Drawing.Printing.PreviewPageInfo[] pages = controller.GetPreviewPageInfo(); + if (pages == null || pages.Length == 0) + return null; + + const int dpi = 200; + using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) + { + iTextSharp.text.Document pdf = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0); + iTextSharp.text.pdf.PdfWriter.GetInstance(pdf, ms); + pdf.Open(); + foreach (System.Drawing.Printing.PreviewPageInfo page in pages) + { + // PhysicalSize is in 1/100 inch + int w = page.PhysicalSize.Width * dpi / 100; + int h = page.PhysicalSize.Height * dpi / 100; + using (Bitmap bmp = new Bitmap(w, h)) + { + bmp.SetResolution(dpi, dpi); + using (Graphics g = Graphics.FromImage(bmp)) + { + g.Clear(Color.White); + g.DrawImage(page.Image, 0, 0, w, h); + } + iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bmp, System.Drawing.Imaging.ImageFormat.Png); + img.ScaleAbsolute(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height); + img.SetAbsolutePosition(0, 0); + pdf.NewPage(); + pdf.Add(img); + } + page.Image.Dispose(); + } + pdf.Close(); + return ms.ToArray(); + } + } + + private void BeginTaxCardDoc(bool isCurrentYear) + { + _taxParameter = TaxParameter.Get(_TaxParameterID); + _isPrvs = !isCurrentYear; + m_nCurPage = 0; + m_oDoc = new EPDoc(); + InitDoc(); + } + + private void AddTaxCardPages(Employee oEmp, bool isCurrentYear) + { + _employee = oEmp; + if (isCurrentYear) + { + IncomeTax oIncomeTax = new IncomeTax(); + _incometaxes = IncomeTax.Get(oIncomeTax.GetEnumForSalaryIT(oEmp.ID), oEmp.ID, _TaxParameterID); + //As this method has not been used yet then A Boolean value is used as Parameter + _dSet = PrepareDataset(oEmp.ID, isCurrentYear); + AddPage(); + PrintBody(); + PrintSlab(); + return; + } + + _incometaxes = IncomeTax.Get(EnumIncomeTaxDataFrom.ProcessedData, oEmp.ID, _TaxParameterID); + //As this method has not been used yet then A Boolean value is used as Parameter + _dSet = PrepareDataset(oEmp.ID, isCurrentYear); + + PayrollDataSet.PayrollDataSet.TaxChallanDataTable dTChallan = new Payroll.Report.PayrollDataSet.PayrollDataSet.TaxChallanDataTable(); + DataRow dChallan = dTChallan.NewRow(); + + ObjectsTemplate oTaxChallans = TaxChallan.Get(oEmp.ID, _TaxParameterID); + int count = 1; + string sChallanNo = ""; + if (oTaxChallans != null && oTaxChallans.Count > 0) + { + foreach (TaxChallan oItem in oTaxChallans) + { + if (count == oTaxChallans.Count) + { + sChallanNo += "Challan: " + oItem.ChallanNo + " Date: " + oItem.DepositDate.ToString("dd-MMM-yyyy"); + } + else + { + sChallanNo += "Challan: " + oItem.ChallanNo + " Date: " + oItem.DepositDate.ToString("dd-MMM-yyyy") + ", "; + count++; + } + } + } + dChallan["ChallanNo"] = sChallanNo; + dTChallan.Rows.Add(dChallan); + dTChallan.TableName = "PayrollDataSet_TaxChallan"; + _dSet.Tables.Add(dTChallan); + + AddPageForTC(); + PrintBodyForTC(); + PrintSlabForTC(); + } + public void showTaxCardMultiple(ObjectsTemplate oincometaxes, Employee oEmp, bool ITStatus) { diff --git a/Payroll.Report/Payroll.Report.csproj b/Payroll.Report/Payroll.Report.csproj index 153e9db..9bb6fa9 100644 --- a/Payroll.Report/Payroll.Report.csproj +++ b/Payroll.Report/Payroll.Report.csproj @@ -64,6 +64,9 @@ False ..\Library\Ease.Printing.dll + + ..\Library\itextsharp.dll + False ..\packages\Microsoft.Report.Viewer.11.0.0.0\lib\net\Microsoft.ReportViewer.WebForms.DLL diff --git a/Payroll.UI/Payroll.UI/Salary/fPayslipSendMail.cs b/Payroll.UI/Payroll.UI/Salary/fPayslipSendMail.cs index 0f8a173..02026f2 100644 --- a/Payroll.UI/Payroll.UI/Salary/fPayslipSendMail.cs +++ b/Payroll.UI/Payroll.UI/Salary/fPayslipSendMail.cs @@ -597,53 +597,95 @@ namespace Payroll.UI return; } + // Tax card is generated together with the salary payslip (Type 1). + // Fiscal year follows the salary month: Jul-Dec => YYYY-(YYYY+1), Jan-Jun => (YYYY-1)-YYYY. + bool withTaxCard = _Type == 1; + int fyStart = month >= 7 ? year : year - 1; + string fiscalYear = fyStart + "-" + (fyStart + 1); + TaxParameter taxParameter = null; + rptIncomeTax taxReport = null; + List taxSkipped = new List(); + if (withTaxCard) + { + taxParameter = FindTaxParameter(fyStart); + if (taxParameter != null) + taxReport = new rptIncomeTax(); + } + int count = 0; + int payslipCount = 0; + int taxCardCount = 0; foreach (SearchEmployee emp in employees) { - lblEmployee.Text = $"Generating Payslip : {++count}/{employees.Count}"; + lblEmployee.Text = $"Generating : {++count}/{employees.Count}"; lblEmployee.Refresh(); - byte[] pdfBytes = (_Type != 1) - ? paySlip.SKFExpencesPaySlip1(dtpfromDate.Value, emp.EmployeeID.Integer.ToString()) - : paySlip.MailReportWithAccountNoMask(dtpfromDate.Value, emp.EmployeeID.Integer.ToString()); - - if (pdfBytes == null) - continue; - - // ✅ FILE NAME: ID-Year-Month.pdf - string fileName = string.Format( - "{0}_{1}_{2}.pdf", - "CEL-" + emp.Employee.EmployeeNo, - year, - month.ToString("00") - ); - - string filePath = Path.Combine(basePath, fileName); - string phoneNo = emp.Employee.MobileNo ?? ""; string password = phoneNo.Length >= 4 ? phoneNo.Substring(phoneNo.Length - 4) : phoneNo.PadLeft(4, '0'); - using (MemoryStream ms = new MemoryStream(pdfBytes)) - using (PdfReader reader = new PdfReader(ms)) - using (FileStream fs = new FileStream(filePath, FileMode.Create)) + byte[] pdfBytes = (_Type != 1) + ? paySlip.SKFExpencesPaySlip1(dtpfromDate.Value, emp.EmployeeID.Integer.ToString()) + : paySlip.MailReportWithAccountNoMask(dtpfromDate.Value, emp.EmployeeID.Integer.ToString()); + + if (pdfBytes != null) { - PdfEncryptor.Encrypt( - reader, - fs, - true, - password, - null, - 2052 + // ✅ FILE NAME: CEL-EmpNo_Year_Month.pdf + string fileName = string.Format( + "{0}_{1}_{2}.pdf", + "CEL-" + emp.Employee.EmployeeNo, + year, + month.ToString("00") ); + SaveEncryptedPdf(pdfBytes, Path.Combine(basePath, fileName), password); + payslipCount++; + } + + if (taxReport != null) + { + try + { + byte[] taxBytes = taxReport.GetTaxCardPdf(emp.Employee, taxParameter.ID); + if (taxBytes == null) + { + taxSkipped.Add(emp.EmployeeNo + " (no tax data)"); + } + else + { + // FILE NAME: CEL-EmpNo_TaxCard_FiscalYear.pdf + string taxFileName = string.Format("CEL-{0}_TaxCard_{1}.pdf", emp.Employee.EmployeeNo, fiscalYear); + SaveEncryptedPdf(taxBytes, Path.Combine(basePath, taxFileName), password); + taxCardCount++; + } + } + catch (Exception taxEx) + { + Exception inner = taxEx.InnerException ?? taxEx; + taxSkipped.Add(emp.EmployeeNo + " (" + inner.Message + ")"); + } } } lblEmployee.Text = string.Empty; - MessageBox.Show("Payslips generated successfully.", "Payslip", - MessageBoxButtons.OK, MessageBoxIcon.Information); + string msg = payslipCount + " payslip(s) generated."; + if (withTaxCard) + { + if (taxParameter == null) + msg += "\n\nTax card not generated: no Income Tax Parameter found for fiscal year " + fiscalYear + "."; + else + { + msg += "\n" + taxCardCount + " tax card(s) generated for fiscal year " + fiscalYear + "."; + if (taxSkipped.Count > 0) + msg += "\n\nTax card skipped " + taxSkipped.Count + ":\n" + + string.Join("\n", taxSkipped.GetRange(0, Math.Min(20, taxSkipped.Count)).ToArray()) + + (taxSkipped.Count > 20 ? "\n..." : ""); + } + } + bool warn = withTaxCard && (taxParameter == null || taxSkipped.Count > 0); + MessageBox.Show(msg, "Payslip", MessageBoxButtons.OK, + warn ? MessageBoxIcon.Warning : MessageBoxIcon.Information); } } catch (Exception ex) @@ -752,6 +794,40 @@ namespace Payroll.UI } + private static void SaveEncryptedPdf(byte[] pdfBytes, string filePath, string password) + { + using (MemoryStream ms = new MemoryStream(pdfBytes)) + using (PdfReader reader = new PdfReader(ms)) + using (FileStream fs = new FileStream(filePath, FileMode.Create)) + { + PdfEncryptor.Encrypt(reader, fs, true, password, null, 2052); + } + } + + /// + /// Income Tax Parameter of the fiscal year starting in July of + /// (e.g. 2026 => 2026-2027). Matched by its fiscal year dates, else by the FiscalYear text. + /// + private static TaxParameter FindTaxParameter(int fyStartYear) + { + DateTime probe = new DateTime(fyStartYear, 12, 31); + ObjectsTemplate all = TaxParameter.Get(); + if (all == null) return null; + foreach (TaxParameter p in all) + { + if (p.FiscalyearDatefrom <= probe && probe <= p.FiscalyearDateTo) + return p; + } + string from = fyStartYear.ToString(), to = (fyStartYear + 1).ToString(), toShort = to.Substring(2); + foreach (TaxParameter p in all) + { + string fy = p.FiscalYear ?? ""; + if (fy.Contains(from) && (fy.Contains(to) || fy.EndsWith(toShort))) + return p; + } + return null; + } + //private void btnDownload_Click(object sender, EventArgs e) //{ // try