<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');


ini_set("display_errors", 1);
error_reporting(E_ERROR);

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
$zcomp = $_SESSION["comp_" . $dbname];

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$vouch = $_GET['vouchno'];

$cond = '';
$lcond = '';
$formname = "Invoice Print";

$maindb = 'sale';

$getqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as mydt   FROM `$maindb` WHERE vouchno='$vouch' ORDER BY `vouchno`") or die(mysqli_error($conn));
$getdata = mysqli_fetch_array($getqry);

$cmpname = mysqli_query($conn, "SELECT * From $zroot.`company` where `code`='$zcomp' ") or die(mysqli_error($conn));
$getcmpname = mysqli_fetch_array($cmpname);

$kcname = $getcmpname['compname'];
$kcadd = $getcmpname['kcadd'];
//$kname = "FLYHIGH BAGS";
mysqli_select_db($conn, $zdb);

$gcust = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode = '$getdata[party]' ") or die(mysqli_error($conn));
$cust = mysqli_fetch_array($gcust);



require_once("library/TCPDF/tcpdf.php");

$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);

//remove default header and footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

//$pdf->AliasNbPages();
$pdf->AddPage();

$fs = 11;
$ht = 5;
$hln = 8;


// $pdf->SetFont('helvetica', '', 12);
// $pdf->Cell(190, 0, "Subject to Mumbai jurisdiction", 0, 0, 'C');
// $pdf->Ln(5);

// $pdf->SetXY(10, 17);
// $pdf->SetFont('helvetica', 'BU', 14);
// $pdf->Cell(190, 2, "CASH MEMO", 0, 0, 'C');
// $pdf->Ln();

$pdf->SetXY(10, 5);
$pdf->SetFont('changaone', 'B', 30);
$pdf->Cell(190, 8, "$kcname", 0, 0, 'C');
$pdf->Ln(10);


$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(190, 6, "$kcadd", 0, 0, 'C');
$pdf->Ln(10);

$pdf->SetFont('aefurat', 'B', 15);
$pdf->Cell(4, 6, " ", 0, 0, 'L');
$pdf->Cell(13, 6, "No.", 0, 0, 'L');
$pdf->Cell(4, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', "B", 13);
$pdf->Cell(30, 6, "$getdata[vouchno]", "B", 0, 'L');
$pdf->Cell(82, 6, "", 0, 0, 'L');

$pdf->SetFont('aefurat', 'B', 12);
$pdf->Cell(25, 6, "Date", 0, 0, 'R');
$pdf->Cell(4, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 14);
$pdf->Cell(28, 6, "$getdata[mydt]", "B", 0, 'L');
$pdf->Ln(8);

$pdf->SetFont('aefurat', 'B', 15);
$pdf->Cell(4, 6, " ", 0, 0, 'L');
$pdf->Cell(13, 6, "M/s.", 0, 0, 'L');
$pdf->Cell(4, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 14);
$pdf->Cell(169, 6, "$cust[name] - $getdata[mobileno]", "B", 0, 'L');
$pdf->Ln(8);

$pdf->SetFont('aefurat', 'B', 15);
$pdf->Cell(4, 6, " ", 0, 0, 'L');
$pdf->Cell(13, 6, "Add.", 0, 0, 'L');
$pdf->Cell(4, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 14);
$pdf->Cell(169, 6, "$cust[add1] $cust[add2] $cust[add3] $cust[city] - $cust[pin]", "B", 0, 'L');
$pdf->Ln(8);

$typeOptions = [
    'A' => 'Air',
    'S' => 'Sea'
];

$typenm = $typeOptions[$getdata['type']] ?? '';

$pdf->SetFont('aefurat', 'B', 15);
$pdf->Cell(4, 6, " ", 0, 0, 'L');
$pdf->Cell(13, 6, "Type", 0, 0, 'L');
$pdf->Cell(4, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 14);
$pdf->Cell(30, 6, "$typenm", "B", 0, 'L');
$pdf->Cell(82, 6, "", "", 0, 'L');
$pdf->SetFont('aefurat', 'B', 12);
$pdf->Cell(25, 6, "Shipment No.", 0, 0, 'R');
$pdf->Cell(4, 6, ":", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 14);
$pdf->Cell(28, 6, "$getdata[invno]", "B", 0, 'L');
$pdf->Ln(8);

$pdf->SetFont('aefurat', 'B', 13);
$pdf->Cell(15, 6, "Sr. No.", 'LTB', 0, 'C');
$pdf->Cell(73, 6, "Description", 'LTB', 0, 'C');
$pdf->Cell(24, 6, "Quantity", 'TLB', 0, 'C');
$pdf->Cell(24, 6, "Unit", 'TLB', 0, 'C');
$pdf->Cell(24, 6, "Rate", 'TBL', 0, 'R');
$pdf->Cell(30, 6, "Amount", "BLRT", 0, 'R');
$pdf->Ln(6);

$pdf->SetFont('aefurat', 'B', 12);
$pdf->Cell(15, 6, "1", 'LTB', 0, 'C');
$pdf->Cell(73, 6, "$getdata[descrip]", 'L', 0, 'L');
$pdf->Cell(24, 6, number_format($getdata['qty']), 'L', 0, 'C');
$pdf->Cell(24, 6, "$getdata[unit]", 'LR', 0, 'C');
$pdf->Cell(24, 6, "$getdata[rate]", 'LR', 0, 'R');
$pdf->Cell(30, 6, "$getdata[amount]", 'R', 0, 'R');
$pdf->Ln(6);

// $a = 0;
// $newctr = 7 - $a;
// for ($i = 0; $i <= $newctr; $i++) {
//     $pdf->Cell(25, 6, "", 'L', 0, 'R');
//     $pdf->Cell(90, 6, "", 'L', 0, 'L');
//     $pdf->Cell(35, 6, "", 'LR', 0, 'R');
//     $pdf->Cell(40, 6, "", 'R', 0, 'R');
//     $pdf->Ln(6);
// }

$hb = 7;

$pdf->SetFont('aefurat', 'B', 12);
$pdf->Cell(120, $hb, "ROE", 'TBL', 0, 'R');
$pdf->Cell(40, $hb, "$getdata[roe]", 'TBR', 0, 'R');
$pdf->Cell(30, $hb, "$getdata[roeamt]", 'TBR', 0, 'R');
$pdf->Ln($hb);

if ($getdata["type"] == "A") {
    $pdf->SetFont('aefurat', 'B', 12);
    $pdf->Cell(120, $hb, "Carrying Charges", 'TBL', 0, 'R');
    $pdf->Cell(40, $hb, "$getdata[unitqty] $getdata[unitlabel] X $getdata[unitamt]", 'TBR', 0, 'R');
    $pdf->Cell(30, $hb, "$getdata[unittotal]", 'TBR', 0, 'R');
    $pdf->Ln($hb);

} elseif ($getdata["type"] == "S") {
    $pdf->SetFont('aefurat', 'B', 12);
    $pdf->Cell(120, $hb, "Carrying Charges", 'TBL', 0, 'R');
    $pdf->Cell(40, $hb, "$getdata[carryperc] %", 'TBR', 0, 'R');
    $pdf->Cell(30, $hb, "$getdata[carryamt]", 'TBR', 0, 'R');
    $pdf->Ln($hb);
}

$pdf->SetFont('aefurat', 'B', 12);
$pdf->Cell(120, $hb, "Ant-dumping", 'TBL', 0, 'R');
$pdf->Cell(40, $hb, "$getdata[dumpqty] $getdata[dumplabel] X $getdata[dumprate]", 'TBR', 0, 'R');
$pdf->Cell(30, $hb, "$getdata[dumpamt]", 'TBR', 0, 'R');
$pdf->Ln($hb);

// if ($getdata['othchgs'] > 0) {
//     $pdf->SetFont('aefurat', 'B', 12);
//     $pdf->Cell(150, $hb, "Other Charges", 'TBRL', 0, 'R');
//     $pdf->Cell(40, $hb, "$getdata[othchgs]", 'TBR', 0, 'R');
//     $pdf->Ln($hb);
// }

$pdf->SetFont('aefurat', 'B', 12);
$pdf->Cell(160, $hb, "TOTAL", 'TBRL', 0, 'R');
$pdf->Cell(30, $hb, "$getdata[netamt]", 'TBR', 0, 'R');
$pdf->Ln($hb);


$pdf->SetFont('aefurat', 'B', 11);
$pdf->Cell(114, 7, "", 0, 0, 'L');
$pdf->SetFont('aefurat', 'B', 15);
$pdf->Cell(76, 7, "For $kcname", 0, 0, 'R');
$pdf->Ln(10);


$q = mysqli_query($conn, "SELECT * FROM sale_images WHERE vouchno='$getdata[vouchno]' ORDER BY id");

while ($r = mysqli_fetch_assoc($q)) {


    $imgPath = "images/Sales/" . $r['imagename'];
   
    if (file_exists($imgPath)) {

        $pdf->AddPage();

        $pdf->Image($imgPath, 5, 50, 200, 170);
    }

}

ob_end_clean();
$pdf->Output("Annexure I Normal-" . $getmdata['vouchno'] . ".pdf", "I");
?>