<?php
ob_start();
require('session.php');
require('pdf-generate/fpdf.php');
extract($_REQUEST);


$financial_id = $_REQUEST['financial_id'];
$company_id = $_REQUEST['sel_company'];


class ConductPDF extends FPDF {
    
    function Footer()
    {
        // Position at 1.5 cm from bottom
        $this->SetY(-15);
        // Arial italic 8
        $this->SetFont('Arial','B',8);
        // Page number
        $this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
    }

    
    
}



$pdf = new ConductPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 12);
if (isset($_REQUEST['financial_id']) || isset($_REQUEST['sel_company'])) 
{
    
    $financial_id = $_REQUEST['financial_id'];
    $company_id = $_REQUEST['sel_company'];

    $companyRow = $db->get_rsltset("SELECT company_name,c_address,c_city,c_state,c_country,c_postcode,c_mobile,gst_no FROM m_company WHERE company_id='" . $company_id . "'");
// echo  $customer_id;
 $company_name = $_REQUEST['sel_company'] ? $companyRow[0]['company_name'] : '' ;
	$c_address =  $companyRow[0]['c_address']  ;
	$c_city = $companyRow[0]['c_city'];
	$c_state = $companyRow[0]['c_state'];
	$c_country = $companyRow[0]['c_country'];
	$c_postcode = $companyRow[0]['c_postcode'];
	$c_mobile = $companyRow[0]['c_mobile'];
	$gst_no = $companyRow[0]['gst_no'];
    
    if ($_REQUEST['financial_id']) {
        $q .= " AND t1.financial_id='" . $financial_id . "'";
    }
    if ($_REQUEST['sel_company']) {
        $q .= " AND t1.company_id='" . $company_id . "'";
    }
    
    $sql = $db->get_rsltset("  SELECT 
                                t1.r_bill_no,
                                t1.actu_pur_bill_no,
                                DATE_FORMAT(t1.return_date,'%d-%m-%Y') AS disp_bill_date,
                                t3.sup_name,
                                t1.final_pur_amt,
                                COUNT(t2.r_bill_det_id) AS qty
                            FROM 
                            t_pur_return_entry_main AS t1
                            LEFT JOIN 
                            t_pur_return_entry_details AS t2 ON t1.r_bill_no = t2.r_bill_no
                            inner join m_supplier t3 on t1.sup_id = t3.sup_id
                            WHERE 
                                t1.IsActive <> 2 $q GROUP BY t1.r_bill_no" );

        $pdf->ln();

        $pdf->SetFont('Arial', 'B', 18);
        $pdf->SetTextColor(0,102,204); 
        $pdf->Cell(50, 8, '', 0, 0, 'L');
        $pdf->Cell(90, 8, $company_name, 0, 0, 'C');
        $pdf->ln();
    
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->SetTextColor(51,102,0); 
        $pdf->Cell(50, 8, '', 0, 0, 'L');
        $pdf->Cell(90, 8, '', 0, 0, 'C');
        $pdf->ln(5);
    
        if ($_REQUEST['sel_company']) {
            $pdf->SetFont('Arial', 'B', 8);
            $pdf->SetTextColor(51, 102, 0);
            $pdf->Cell(50, 8, '', 0, 0, 'L');
            $pdf->Cell(90, 8, $c_address . ", " . $c_city . ", " . $c_state . " - " . $c_postcode, 0, 0, 'C');
            $pdf->Ln(5);
        
            $pdf->SetFont('Arial', 'B', 8);
            $pdf->SetTextColor(102,51,0);
            $pdf->Cell(50, 8, '', 0, 0, 'L');
            $pdf->Cell(90, 8, "GSTIN: " . $gst_no , 0, 0, 'C');
            $pdf->Ln(5);

            $pdf->SetFont('Arial', 'B', 8);
            $pdf->SetTextColor(102,51,0);
            $pdf->Cell(50, 8, '', 0, 0, 'L');
            $pdf->Cell(90, 8,  " MOBILE NO: +91 " . $c_mobile, 0, 0, 'C');
            $pdf->Ln(10);
        } else {
            $pdf->ln(5);
        }
    
        // $pdf->SetFont('Arial', 'B', 8);
        // $pdf->SetTextColor(51,102,0); 
        // $pdf->Cell(50, 8, '', 0, 0, 'L');
        // $pdf->Cell(90, 8, "MOBILE NO: +91 ".$c_mobile, 0, 0, 'C');
        // $pdf->ln(12);


    $pdf->SetFont('Arial', 'B', 12);
    $pdf->SetFillColor(43,61,81);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetDrawColor(255,255, 255);

    $pdf->Cell(12,10, 'S.no',1,0,'C',1);
    $pdf->Cell(25, 10, 'Bill Date', 1, 0, 'C',1);
    $pdf->Cell(18, 10, 'Bill No', 1, 0, 'C',1);
    $pdf->Cell(95, 10, 'Supplier', 1, 0, 'C',1);
    $pdf->Cell(15, 10, 'Qty', 1, 0, 'C',1);
    $pdf->Cell(26, 10, 'Amount', 1, 0, 'C',1);

    $pdf->SetFillColor(0);
    $pdf->SetTextColor(0);
    $pdf->SetDrawColor(0);
    
    $serial_no = 1; // Initialize serial number
$pdf->Ln();
$pdf->SetFont('Arial', 'B', 8);

if (empty($sql)) {
    // Display "No records found" message
    $pdf->Cell(190, 8, 'No records found', 1, 1, 'C');
} else {

    $total_qty = 0; // Initialize total quantity
    $total_amount = 0; // Initialize total amount

foreach ($sql as $row) {
    $pdf->Cell(12, 8, $serial_no++, 1, 0, 'C');
    $pdf->Cell(25, 8, $row['disp_bill_date'], 1, 0, 'C');
    $pdf->Cell(18, 8, $row['actu_pur_bill_no'], 1, 0, 'C');
    $pdf->Cell(95, 8, $row['sup_name'], 1, 0, 'L');
    $pdf->Cell(15, 8, $row['qty'], 1, 0, 'C');
    $pdf->Cell(26, 8, number_format($row['final_pur_amt'], 2), 1, 1, 'R'); // Add 1 at the end to move to the next line after this cell
    
    $total_qty += $row['qty'];
    $total_amount += $row['final_pur_amt'];
    }
        $pdf->SetFont('Arial', 'B', 12);
        $pdf->Cell(150, 8, 'Total', o, 0, 'C');

        $pdf->SetFont('Arial', 'B', 9);
        $pdf->SetFillColor(255, 255, 153);
        $pdf->SetTextColor(0);
        $pdf->SetDrawColor(0, 0, 0);
        $pdf->Cell(15, 8, $total_qty, 1, 0, 'C');
        $pdf->Cell(26, 8, number_format($total_amount, 2), 1, 1, 'R',1);
    }
    
$pdf->Ln();
$pdf->Output('Purchase Return Register.pdf', 'I');
}
else {
    $pdf = new ConductPDF();
    $pdf->AddPage();
    $pdf->Output('Salesstatics.pdf', 'd');
    $pdf->Close();
}





