@include('set_tenant_db'); @php ob_start(); require('fpdf/fpdf.php'); date_default_timezone_set('Asia/Kolkata'); class PDF extends FPDF { function Header() { $user = auth()->user(); if(isset($user->proloc)){ $locn_cd = $user->proloc; }else{ $locn_cd = ''; } $companyrow = "SELECT * from g_locpar where locn_cd = '$locn_cd'"; $result = DB::select($companyrow); $this->SetFont('Arial','B',12); $this->Cell(0,5,isset($result[0]->company) ? trim($result[0]->company) : '',0,1,'C'); $this->SetFont('Arial','',10); $this->Cell(0,5,(isset($result[0]->addr1) ? trim($result[0]->addr1) : ''). " ". (isset($result[0]->addr2) ? trim($result[0]->addr2) : ''),0,1,'C'); $this->Cell(0,5,(isset($result[0]->addr3) ? trim($result[0]->addr3) : ''). " ". (isset($result[0]->addr4) ? trim($result[0]->addr4) : ''),0,1,'C'); $this->Ln(5); $this->SetFont('Arial','B',12); $this->Cell(0,5,'Leave Card',0,1,'C'); $this->Ln(5); } function Footer() { $this->SetY(-15); $this->SetFont('Arial','I',8); $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C'); } } $user = auth()->user(); if(isset($user->proloc)){ $locn_cd = $user->proloc; }else{ $locn_cd = ''; } $empl_cd = $user->userid; $empl_qry = "SELECT * from employee where empl_cd = '$empl_cd'"; $employee = DB::select($empl_qry); $employee = $employee[0] ?? ''; $dept_qry = "SELECT * from multmast where tag = 'DEPT' AND code = '".($employee->dept ?? '')."'"; $dept = DB::select($dept_qry); $dept = $dept[0] ?? ''; $designation_qry = "SELECT * from multmast where tag = 'DESIG' AND code = '".($employee->designation ?? '')."'"; $designation = DB::select($designation_qry); $designation = $designation[0] ?? ''; $g_locpar_qry = "SELECT leaveyr_beg,leaveyr_end from g_locpar where locn_cd = '$locn_cd'"; $leave_year = DB::select($g_locpar_qry); $leave_year = $leave_year[0] ?? ''; $leaveyr_beg = $leave_year->leaveyr_beg ?? ''; $leaveyr_end = $leave_year->leaveyr_end ?? ''; $leave_dtl = DB::select("SELECT * FROM attend_leaveappl WHERE lvdate_from >= '$leaveyr_beg' AND lvdate_to <= '$leaveyr_end' AND empl_cd = '$empl_cd'"); // Get leave balances $leave_balances = DB::select("SELECT SUM(IF(crdr = 'C' AND leavecode='PL',noofdays,0)) AS pl_credit_bal, SUM(IF(crdr = 'D' AND leavecode='PL',noofdays,0)) AS pl_debit_bal, SUM(IF(crdr = 'C' AND leavecode='SL',noofdays,0)) AS sl_credit_bal, SUM(IF(crdr = 'D' AND leavecode='SL',noofdays,0)) AS sl_debit_bal, SUM(IF(crdr = 'C' AND leavecode='CL',noofdays,0)) AS cl_credit_bal, SUM(IF(crdr = 'D' AND leavecode='CL',noofdays,0)) AS cl_debit_bal FROM attend_leaveappl WHERE empl_cd = '$empl_cd'"); $leave_balances = $leave_balances[0] ?? ''; $pdf = new PDF(); $pdf->AliasNbPages(); $pdf->AddPage(); $pdf->SetFont('Arial','B',10); $pdf->Cell(60,5,'EMPLOYEE CODE',1,0); $pdf->Cell(130,5,$empl_cd.' - '.($employee->name ?? ''),1,1); $pdf->Cell(60,5,'LOCATION',1,0); $pdf->Cell(130,5,$employee->locn_cd ?? '',1,1); $pdf->Cell(60,5,'DESIGNATION',1,0); $pdf->Cell(130,5,$designation->description ?? '',1,1); $pdf->Cell(60,5,'DEPARTMENT',1,0); $pdf->Cell(130,5,$dept->description ?? '',1,1); $pdf->Ln(10); // Calculate table width $table_width = array_sum(array(25, 25, 25, 30, 20, 20, 20)); $left_margin = ($pdf->GetPageWidth() - $table_width) / 2; // Leave table headers $pdf->SetFont('Arial','B',8); $header = array('Application No', 'Date From', 'Date To', 'Leave Type', 'PL Days', 'SL Days', 'CL Days'); $widths = array(25, 25, 25, 30, 20, 20, 20); // Set left margin for centering $pdf->SetLeftMargin($left_margin); // Print table headers for($i=0;$iCell($widths[$i],7,$header[$i],1,0,'C'); } $pdf->Ln(); // Set font for table data $pdf->SetFont('Arial','',8); // Initialize totals $total_pl = 0; $total_sl = 0; $total_cl = 0; // Print table rows foreach ($leave_dtl as $leave) { // Determine leave type counts $pl_days = ($leave->leavecode == 'PL') ? $leave->noofdays : 0; $sl_days = ($leave->leavecode == 'SL') ? $leave->noofdays : 0; $cl_days = ($leave->leavecode == 'CL') ? $leave->noofdays : 0; // Add to totals $total_pl += $pl_days; $total_sl += $sl_days; $total_cl += $cl_days; // Print row $pdf->Cell($widths[0],6,$leave->applno ?? '','LR',0,'C'); $pdf->Cell($widths[1],6,$leave->lvdate_from ?? '','LR',0,'C'); $pdf->Cell($widths[2],6,$leave->lvdate_to ?? '','LR',0,'C'); $pdf->Cell($widths[3],6,$leave->lv_type ?? '','LR',0,'C'); $pdf->Cell($widths[4],6,$pl_days,'LR',0,'C'); $pdf->Cell($widths[5],6,$sl_days,'LR',0,'C'); $pdf->Cell($widths[6],6,$cl_days,'LR',1,'C'); } // Calculate closing balances if available if($leave_balances) { // Calculate net balances $pl_balance = ($leave_balances->pl_credit_bal ?? 0) - ($leave_balances->pl_debit_bal ?? 0); $sl_balance = ($leave_balances->sl_credit_bal ?? 0) - ($leave_balances->sl_debit_bal ?? 0); $cl_balance = ($leave_balances->cl_credit_bal ?? 0) - ($leave_balances->cl_debit_bal ?? 0); // Print closing balance row $pdf->SetFont('Arial','B',8); $pdf->Cell(array_sum(array_slice($widths, 0, 4)),6,'Closing Balance:','LTB',0,'R'); $pdf->Cell($widths[4],6,$pl_balance,'TB',0,'C'); $pdf->Cell($widths[5],6,$sl_balance,'TB',0,'C'); $pdf->Cell($widths[6],6,$cl_balance,'TBR',1,'C'); } // Reset left margin to default $pdf->SetLeftMargin(10); $filename = $empl_cd . "_Leave_Card_".date('Y-m-d').".pdf"; // Output the PDF directly to browser header('Content-Type: application/pdf'); header('Content-Disposition: inline; filename="Leave_Card_'.date('Y-m-d').'.pdf"'); $pdf->Output('D',$filename); exit; @endphp