<?php 




 

// Instanciation of inherited class
	if(isset($_GET['accid'])){
		
		require('fpdf/fpdf.php');
		
        // $this->Cell(50, 30, 'Order #'.$this->orderNumber);
       
		require 'connection.php';

		$acId = $_GET['accid'];
		$from = $_GET['from'];
		$to = $_GET['to'];
	$baltyp = 'Dr.';
	
		$qry = "SELECT * from accreg where accid = '$acId'";
        $run = mysqli_query($connect, $qry);
        $row = mysqli_fetch_array($run);
        $accId = $row['accid'];
        $name = $row['accname'];
        // finding opening balance 
        $qry1 = "SELECT sum(debit)-sum(credit) as cashOp from ledger where  accid = '$acId' and dated < '$from'";
        $run_qry1 = mysqli_query($connect, $qry1);
        $row1 = mysqli_fetch_array($run_qry1);
        $cashOp = $row1['cashOp']+0;
        $cashOp2 = $cashOp+0;
        		if($cashOp2>0){
         		$baltyp='Dr.';
         	}
  		     

         	if($cashOp2<0){
         		        	$cashOp2=$cashOp2*(-1);

         		$baltyp='Cr.';
         	}


         	if($cashOp2==0){
         		$baltyp='';
         	}
      


        // end of opening balance
        // finding opening balance 
        $qry2 = "SELECT sum(debit)-sum(credit) as close, min(dated) as firstdate, max(dated) as lastdate  from ledger where accid = '$acId' and dated <= '$to'";
        $run_qry2 = mysqli_query($connect, $qry2);
        $row2 = mysqli_fetch_array($run_qry2);
        $closingB = $row2['close'];
        if($closingB<0){
        	$closingB=$closingB*(-1);
        }

       $qry2 = "SELECT  min(dated) as firstdate, max(dated) as lastdate  from ledger where accid = '$acId' and dated >= '$from' and dated<='$to'";
        $run_qry2 = mysqli_query($connect, $qry2);
        $row2 = mysqli_fetch_array($run_qry2);

$fdate = $row2['firstdate'];
if (!is_null($fdate)) {
    $dt1 = date_create($fdate);
    $fdt = date_format($dt1, "d-m-Y");
} else {
    $fdt = ""; // or some default value
}

$ldate = $row2['lastdate'];
if (!is_null($ldate)) {
    $dt2 = date_create($ldate);
    $ldt = date_format($dt2, "d-m-Y");
} else {
    $ldt = ""; // or some default value
}
// Page header


// Instanciation of inherited class




        // end of opening balance
		// end data
		$pdf = new FPDF('P','mm','letter');
		
		$pdf->AliasNbPages();
	
		// page settings start
		//add new page
		$pdf->AddPage();

		  $pdf->SetLeftMargin('5');
		 $pdf->SetRightMargin('5');
		//set font to arial, bold, 14pt
		


		 $pdf->SetTextColor(255, 255, 255);

		$image1 = "lgrheader.png";
		$pdf->Cell(0,0,$pdf->Image($image1, $pdf->GetX()-10, $pdf->GetY()-10, 230),'',true);
		

		$pdf->SetFont('Arial','B',16);
		$pdf->Cell(1, 10);
		$pdf->Cell(135 ,5,'Account Ledger',0,0,'L');

		$pdf->SetFont('Arial','B',10);

		$pdf->Cell(55 ,3,'From: '.$fdt.' To: '.$ldt,0,0,'L');

		$pdf->Cell(12,4,'',0,1);//end line

		//set font to arial, regular, 12pt
		$pdf->SetFont('Arial','B',12);

		//contact detail
		$pdf->Cell(10,4,'',0,1);//end line
		$pdf->Cell(135 ,3,'Account Title: '.$name,0,0,'L');
		$pdf->SetFont('Arial','B',10);

		$pdf->Cell(55 ,3,'Contact: '.$row['phone'],0,0,'L');
		$pdf->Cell(10,4,'',0,1);//end line

		$pdf->Cell(5 ,5,'Address: '.$row['address'],0,0,'L');
		// $pdf->Cell(59 ,3,'',0,1);//end of line


		$pdf->Cell(10,3,'',0,1);//end line
		$pdf->Cell(10,3,'',0,1);//end line
		$pdf->Cell(10,3,'',0,1);//end line
		$pdf->Cell(10,3,'',0,1);//end line


		
		$pdf->SetFont('Arial','',12);



		$pdf->SetTextColor(0,0,0);

		//invoice contents
		$pdf->SetFont('Arial','',8);
				 $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(33, 51, 67); // Light gray background color
		$pdf->Cell(15 ,5,'Dated','B,T,L',0,'C', true);
		$pdf->Cell(15 ,5,'Trx_ID','B,T',0,'C', true);
		$pdf->Cell(80 ,5,'Narration','B,T',0,'C', true);//end of line
		$pdf->Cell(15 ,5,'Ref.No.','B,T',0,'C', true);//end of line
		$pdf->Cell(20 ,5,'Transaction','B,T',0,'C', true);//end of line
		$pdf->Cell(19 ,5,'Debit','B,T',0,'R', true);//end of line
		$pdf->Cell(19 ,5,'Credit','B,T',0,'R', true);//end of line
		$pdf->Cell(20 ,5,'Balance','B,T,R',1,'R', true);//end of line



				 $pdf->SetTextColor(0, 0, 0);

		$pdf->SetFont('Arial','',8);

		$pdf->Cell(150 ,5,'*********Opening Balance*********','',0,'C');//end of line

		$pdf->Cell(53 ,5,number_format($cashOp2).' '.$baltyp,'',1,'R');

		//make a dummy empty cell as a vertical spacer

		$pdf->SetFont('Arial','',7);

		// fetch data
		$sum = 0;
		$bal2 = 0;
		$bal = $cashOp;
		$debitT = 0;
		// $count = 0;
		// $total = 0;

		$creditT = 0;
		$alternateColor = 0;
		$filename = $row['accname'];;
		$qry = "SELECT * from ledger where accid = '$acId' and dated >= '$from' and dated <= '$to' and itemId=0 and qty=0 order by dated,trid asc";
        $run_qry = mysqli_query($connect, $qry);
        while($row = mysqli_fetch_array($run_qry)){
        	// $count+=1;
        	// $total+=$row['debit'];

  if ($alternateColor==0) {
        $pdf->SetFillColor(240, 240, 240); // Light gray background color
		$alternateColor =1;

    } else {
        $pdf->SetFillColor(255, 255, 255); // Default background color
		$alternateColor =0;

    }

        	$creditT = $creditT+$row['credit'];
        	$debitT = $debitT+$row['debit'];
			//Numbers are right-aligned so we give 'R' after new line parameter
			$rundate = $row['dated'];
         $dat= date_create($rundate);
         $dated=date_format($dat,"d-m-Y");

         	$bal = $bal+($row['debit']-($row['credit']));
$bal2=$bal;
         	if($bal2>0){
         		$baltyp='Dr.';
         	}
  		     

         	if($bal2<0){
         		$bal2=$bal2*(-1);
         		$baltyp='Cr.';
         	}


         	if($bal2==0){
         		$baltyp='';
         	}

  		        $pdf->Cell(17, 6, $dated, 'B,L,T', 0, 'J', true);
		
			$pdf->Cell(15 ,6,$row["vno"],'B,T',0,'C', true);
			 $pdf->Cell(84 ,6,$row["description"],'B,T',0,'J', true);
			$pdf->Cell(10 ,6,$row["refno"],'B,T',0,'C', true);
			$pdf->Cell(19 ,6,$row["entrytype"],'B,T',0,'C', true);//end of line
			$pdf->Cell(19 ,6,number_format($row["debit"]),'B,T',0,'R', true);//end of line
			$pdf->Cell(19 ,6,number_format($row["credit"]),'B,T',0,'R', true);//end of line
			$pdf->Cell(20 ,6, number_format($bal2).' '.$baltyp,'B,R,T',1,'R', true);//end of line
			//Numbers are right-aligned so we give 'R' after new line parameter



		}
		$pdf->SetFont('Arial', '', 8);
		$pdf->Cell(40 ,2,"",'T',0,'C');
		$pdf->Cell(34 ,2,'','T',0,'C');
		$pdf->Cell(28 ,2,"",'T',0,'C');
		$pdf->Cell(31 ,2,"",'T',0,'C');
		$pdf->Cell(29 ,5,$debitT,'T',0,'R');
		$pdf->Cell(21 ,5,$creditT,'T',0,'R');
		$pdf->Cell(20 ,5,$debitT-$creditT,'T',1,'R');
		
		//summary
		$pdf->SetFont('Arial', 'B', 12);
		$pdf->Cell(50,2,'',0);
		$pdf->Cell(50 ,2,'',0,0);
		$pdf->Cell(25 ,2,"",0,1,'C');//end of line
		// summary
		$pdf->Cell(84 ,2,'',0,0);
		$pdf->Cell(36 ,6,'',0,0);
		$pdf->Cell(80 ,8,'Balance: '.(number_format($closingB)).' '.$baltyp,0,1,'R');//end of line

		// footer
		$pdf->SetFont('Arial', 'B', 12);
		// $pdf->Cell();
		// $pdf->SetFillColor(0,0,0);
		$pdf->SetTextColor(255,255,255);
	

		$pdf->SetTextColor(0,0,0);
		//make a dummy empty cell as a vertical spacer
		// $pdf->Cell(159 ,0,'',0,1);//end of line
		// $pdf->Cell(0, 5,'Page '.$pdf->PageNo().' of '.$pdf->PageNo(), 0,0,'L');
// $pdf->Cell(0, 5, "Page " . $pdf->PageNo() . "/{totalPages}", 0, 1);
		// page settings end
$pdf->Output($filename.'.pdf','I');
		// $pdf->Output();

	}
?>
