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


// $financial_id = $_REQUEST['financial_id'];
// $sup_id = $_REQUEST['sel_supplier'];
// $date = $_REQUEST['date'];


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_supplier']) || isset($_REQUEST['frmdate']) || isset($_REQUEST['todate'])) 
{
    
    $financial_id = $_REQUEST['financial_id'];
    $sup_id = $_REQUEST['sel_supplier'];
    $frmdate = $_REQUEST['frmdate'];
    $todate = $_REQUEST['todate'];


// echo  $frmdate;
// echo  $todate;
// exit();
 $company_name =  'SRI SARASWATHI SILKS ' ;
	
    
    if ($_REQUEST['financial_id']) {
        $q .= " AND financial_id='" . $financial_id . "'";
    }
    if ($_REQUEST['sel_supplier']) {
        $q .= " AND t1.sup_id='" . $sup_id . "'";
    }
    if ($_REQUEST['frmdate'] && $_REQUEST['todate']) {
        $q .= " AND t1.p_date BETWEEN '" . $frmdate . "' AND '" . $todate . "'";
    }
    
    $sql = $db->get_rsltset("  SELECT t1.*, 
                                        DATE_FORMAT(t1.p_date,'%d-%m-%Y') AS disp_date, 
                                            t1.IsActive AS status, 
                                            t1.p_flow,
                                            t1.qty_grm,
                                            t2.product_name, 
                                            t3.sup_name
                                        FROM t_product_flow_details t1 
                                        
                                        INNER JOIN m_product t2 ON t1.pdt_type = t2.product_id
                                        INNER JOIN m_supplier t3 ON t1.sup_id = t3.sup_id 
                                        where 1=1 $q and t1.IsActive <> 2" );

        $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);
    
        // $pdf->SetFont('Arial', 'B', 8);
        //     $pdf->SetTextColor(51, 102, 0);
        //     $pdf->Cell(50, 8, '', 0, 0, 'L');
        //     $pdf->Cell(90, 8, 'OLD NO.26 B/NEW NO.71, NADU STREET, SHEIKPET, KANCHIPURAM, TAMILNADU - 631501', 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: 33ABZPH9990G1ZV' , 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 9940734544', 0, 0, 'C');
            $pdf->Ln(10);

        
        $pdf->SetTextColor(0);

        $product_totals = [];
        foreach ($sql as $row) {
            $product_name = $row['product_name'];
            $qty_grm = $row['qty_grm'];
            $p_flow = $row['p_flow'];
            
            // Initialize total grams for the product type if not set
            if (!isset($product_totals[$product_name])) {
                $product_totals[$product_name] = ['debit' => 0, 'credit' => 0];
            }
            
            // Increment total grams based on flow type
            if ($p_flow == '1') {
                // Inward flow, add to debit total
                $product_totals[$product_name]['debit'] += $qty_grm;
            } elseif ($p_flow == '2') {
                // Outward flow, add to credit total
                $product_totals[$product_name]['credit'] += $qty_grm;
            }
        }
        
        // Display product totals before the table
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->cell(30);
        $pdf->Cell(50, 8, 'Product Type', 1, 0, 'C');
        $pdf->Cell(40, 8, 'Total Debit (grams)', 1, 0, 'C');
        $pdf->Cell(40, 8, 'Total Credit (grams)', 1, 1, 'C');
        
        foreach ($product_totals as $product_name => $totals) {
            $pdf->cell(30);
            $pdf->Cell(50, 8, $product_name, 1, 0, 'L');
            $pdf->Cell(40, 8, $totals['debit'], 1, 0, 'C');
            $pdf->Cell(40, 8, $totals['credit'], 1, 1, 'C');
        }
        
        // Add space between product totals and the table
        $pdf->Ln(10);
        
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetFillColor(43,61,81);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetDrawColor(255,255, 255);

    $pdf->Cell(12,8, 'S.no','TLR',0,'C',1);
    $pdf->Cell(25, 8, 'Date', 'TLR', 0, 'C',1);
    $pdf->Cell(63, 8, 'Supplier', 1, 0, 'C',1);
    $pdf->Cell(40, 8, 'Product Name', 1, 0, 'C',1);
    $pdf->Cell(26, 6, 'Debit', 'TLR', 0, 'C',1);
    $pdf->Cell(25, 6, 'Credit ', 'TLR', 0, 'C',1);
    $pdf->Ln(6);

        $pdf->Cell(12,4, '', 'BLR', 0, 'C',1);
        $pdf->Cell(25, 4, '', 'BLR', 0, 'L',1);
        $pdf->Cell(63, 4, '', 'BLR', 0, 'C',1);
        $pdf->Cell(40, 4, '', 'BLR', 0, 'C',1);
        $pdf->Cell(26, 4, '(grams)', 'BLR', 0, 'C',1);
        $pdf->Cell(25, 4, '(grams)', 'BLR', 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 {

foreach ($sql as $row) {
      

    $pdf->Cell(12, 8, $serial_no++, 1, 0, 'C');
    $pdf->Cell(25, 8, $row['disp_date'], 1, 0, 'C');
    // $pdf->Cell(18, 8, $row['p_id'], 1, 0, 'C');
    $pdf->Cell(63, 8, $row['sup_name'], 1, 0, 'L');
    $pdf->Cell(40, 8, $row['product_name'], 1, 0, 'L');
    // Check if p_flow is 1 (Inward) or 2 (Outward) and set the cell value accordingly
    if ($row['p_flow'] == '1') {
        // Inward flow, quantity in Debit column
        $pdf->Cell(25, 8, $row['qty_grm'], 1, 0, 'C');
        $pdf->Cell(26, 8, '', 1, 1, 'C'); // Empty cell for Credit
    } elseif ($row['p_flow'] == '2') {
        // Outward flow, quantity in Credit column
        $pdf->Cell(25, 8, '', 1, 0, 'C'); // Empty cell for Debit
        $pdf->Cell(26, 8, $row['qty_grm'], 1, 1, 'C'); // Quantity in Credit
    } else {
        // Handle other cases if necessary
        $pdf->Cell(25, 8, '', 1, 0, 'C'); // Empty cell for Debit
        $pdf->Cell(26, 8, '', 1, 1, 'C'); // Empty cell for Credit
    }
}
}
    
$pdf->Ln();
$pdf->Output('weavers report.pdf', 'I');
}
else {
    $pdf = new ConductPDF();
    $pdf->AddPage();
    $pdf->Output('Salesstatics.pdf', 'd');
    $pdf->Close();
}





