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

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

mysqli_select_db($conn, $zdb);

$vouch = $_GET['vouchno'];

$maindb = 'salemain';
$trandb = 'saletran';

$getqry = mysqli_query($conn, "
SELECT *,
DATE_FORMAT(vouchdt,'%d-%m-%Y') as vouchdt
FROM `$maindb`
WHERE vouchno='$vouch'
");

$getdata = mysqli_fetch_array($getqry);

$cmpqry = mysqli_query($conn,"
SELECT *
FROM $zroot.company
WHERE code='$zcomp'
");

$getcmpname = mysqli_fetch_array($cmpqry);

$getparty = mysqli_query($conn,"
SELECT *
FROM accmast
WHERE accode='$getdata[party]'
");

$showparty = mysqli_fetch_array($getparty);

$gettrans = mysqli_query($conn,"
SELECT *
FROM transport
WHERE code='$getdata[transporter]'
");

$ltrans = mysqli_fetch_array($gettrans);

$formname = "Tax Invoice";

$invoice =
$getcmpname['init'] .
"/" .
date("y") .
"/" .
$getdata['vouchno'];

if ($getdata['cashcred'] == "R") {
    $paytype = "Credit";
} else {
    $paytype = "Cash";
}

if ($getdata["nameprint"] != '') {
    $printname = $getdata["nameprint"];
    $printmobile = $getdata["printmobile"];
} else {
    $printname = $showparty['name'];
    $printmobile = $showparty['mobile'];
}

if (
    $getdata["shipgstno"] == $showparty['gstno']
    || $getdata['shipgstno'] == ''
) {
    $gstno = $showparty['gstno'];
} else {
    $gstno = $getdata['shipgstno'];
}



class PDF extends FPDF
{
    function Header()
    {
        global $getcmpname, $formname;

        // LOGO
        if(file_exists("images/logo.png"))
        {
            $this->Image("images/logo.png",10,8,28);
        }

        // COMPANY NAME
        $this->SetFont('Arial','B',24);
        $this->Cell(0,10,$getcmpname['compname'],0,1,'C');

        // TAGLINE
        $this->SetFont('Arial','B',10);
        $this->Cell(
            0,
            5,
            'TRADER | WHOLESALER | MANUFACTURER | ALL KINDS OF FOOTWEAR',
            0,
            1,
            'C'
        );

        // ADDRESS
        $this->SetFont('Arial','',8);

        $address =
        $getcmpname['hoadd1'].' '.
        $getcmpname['hoadd2'].' '.
        $getcmpname['hoadd3'].' '.
        $getcmpname['hocity'].' '.
        $getcmpname['hopin'];

        $this->Cell(0,4,$address,0,1,'C');

        // CONTACT
        $contact =
        'Email : '.$getcmpname['email'].
        '    Contact : '.$getcmpname['htel'].
        '    GST : '.$getcmpname['gstno'];

        $this->Cell(0,4,$contact,0,1,'C');

        // TITLE
        $this->Ln(2);

        $this->SetFont('Arial','B',14);

        $this->Cell(190,10,$formname,1,1,'C');

        $this->Ln(3);
    }

    function Footer()
    {
        $this->SetY(-12);

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

        $this->Cell(
            0,
            5,
            'Page '.$this->PageNo().'/{nb}',
            0,
            0,
            'C'
        );
    }

    function ItemHeader()
    {
        $this->SetFont('Arial','B',9);

        $this->Cell(10,8,'Sr',1,0,'C');
        $this->Cell(60,8,'Item Name',1,0,'C');
        $this->Cell(18,8,'HSN',1,0,'C');
        $this->Cell(14,8,'GST%',1,0,'C');
        $this->Cell(16,8,'Qty',1,0,'C');
        $this->Cell(18,8,'Rate',1,0,'C');
        $this->Cell(22,8,'Amount',1,0,'C');
        $this->Cell(18,8,'Disc',1,0,'C');
        $this->Cell(24,8,'Taxable',1,1,'C');
    }
}



$pdf = new PDF('P','mm','A4');

$pdf->AliasNbPages();

$pdf->SetMargins(10,10,10);

$pdf->SetAutoPageBreak(true,20);

$pdf->AddPage();



// ======================================
// BILL TO / SHIP TO
// ======================================

$startY = $pdf->GetY();


// LEFT BOX

$pdf->SetXY(10,$startY);

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

$pdf->Cell(95,8,'Bill To Party',1,0,'C');

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

$leftData = [
    ['M/s.', $printname],
    ['Address', $showparty['add1'].' '.$showparty['add2']],
    ['', $showparty['add3'].' '.$showparty['city'].' '.$showparty['pin']],
    ['Mobile', $printmobile],
    ['GST No.', $showparty['gstno']],
    ['PAN', $showparty['pan']],
    [
        'State',
        $showparty['state'].'  State Code : '.$showparty['statecode']
    ]
];

$y = $startY + 8;

foreach($leftData as $row)
{
    $pdf->SetXY(10,$y);

    $pdf->Cell(18,8,$row[0],1);

    $pdf->MultiCell(77,8,$row[1],1);

    $y += 8;
}



// RIGHT BOX

$pdf->SetXY(105,$startY);

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

$pdf->Cell(95,8,'Ship To Party',1,0,'C');

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

$rightData = [
    ['M/s.', $getdata['shipname']],
    ['Address', $getdata['shipadd1'].' '.$getdata['shipadd2']],
    ['', $getdata['shipadd3']],
    ['Mobile', $printmobile],
    ['GST No.', $gstno],
    ['PAN', $showparty['pan']],
    [
        'State',
        $showparty['state'].'  State Code : '.$showparty['statecode']
    ]
];

$y2 = $startY + 8;

foreach($rightData as $row)
{
    $pdf->SetXY(105,$y2);

    $pdf->Cell(18,8,$row[0],1);

    $pdf->MultiCell(77,8,$row[1],1);

    $y2 += 8;
}



$pdf->SetY(max($y,$y2)+5);



// ======================================
// INVOICE DETAILS
// ======================================

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

$pdf->Cell(
    95,
    8,
    'Invoice No : '.$invoice,
    1,
    0
);

$pdf->Cell(
    95,
    8,
    'Date : '.$getdata['vouchdt'],
    1,
    1
);

$pdf->Cell(
    95,
    8,
    'Paid By : '.$paytype,
    1,
    0
);

$pdf->Cell(
    95,
    8,
    'Transporter : '.$ltrans['name'],
    1,
    1
);

$pdf->Cell(
    190,
    8,
    'Terms : '.$getdata['remark1'].' '.$getdata['remark2'],
    1,
    1
);

$pdf->Ln(4);



// ======================================
// ITEM TABLE
// ======================================

$pdf->ItemHeader();

$gettran = mysqli_query($conn,"
SELECT *
FROM `$trandb`
WHERE vouchno='$vouch'
ORDER BY srno
");

$sr = 1;

$totqty = 0;
$totamt = 0;
$totdisc = 0;
$tottax = 0;

while($showtran = mysqli_fetch_array($gettran))
{
    $getitem = mysqli_query($conn,"
    SELECT *
    FROM product
    WHERE code='$showtran[item]'
    ");

    $showitem = mysqli_fetch_array($getitem);

    $itemname = $showitem['name'];

    $amount =
    $showtran['qty'] *
    $showtran['rate'];

    $disc = $showtran['discount'];

    $taxable = $showtran['subtot'];

    $gst =
    $showtran['cgstrate'] * 2;

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

    $pdf->Cell(10,8,$sr,1,0,'C');

    $pdf->Cell(
        60,
        8,
        substr($itemname,0,35),
        1
    );

    $pdf->Cell(
        18,
        8,
        $showtran['hsncode'],
        1
    );

    $pdf->Cell(
        14,
        8,
        number_format($gst,0),
        1,
        0,
        'R'
    );

    $pdf->Cell(
        16,
        8,
        number_format($showtran['qty'],0),
        1,
        0,
        'R'
    );

    $pdf->Cell(
        18,
        8,
        number_format($showtran['rate'],0),
        1,
        0,
        'R'
    );

    $pdf->Cell(
        22,
        8,
        number_format($amount,2),
        1,
        0,
        'R'
    );

    $pdf->Cell(
        18,
        8,
        number_format($disc,2),
        1,
        0,
        'R'
    );

    $pdf->Cell(
        24,
        8,
        number_format($taxable,2),
        1,
        1,
        'R'
    );



    $totqty += $showtran['qty'];
    $totamt += $amount;
    $totdisc += $disc;
    $tottax += $taxable;

    $sr++;



    // PAGE BREAK
    if($pdf->GetY() > 240)
    {
        $pdf->AddPage();

        $pdf->ItemHeader();
    }
}



// ======================================
// EMPTY ROWS
// ======================================

while($sr <= 15)
{
    $pdf->Cell(10,8,'',1);
    $pdf->Cell(60,8,'',1);
    $pdf->Cell(18,8,'',1);
    $pdf->Cell(14,8,'',1);
    $pdf->Cell(16,8,'',1);
    $pdf->Cell(18,8,'',1);
    $pdf->Cell(22,8,'',1);
    $pdf->Cell(18,8,'',1);
    $pdf->Cell(24,8,'',1);
    $pdf->Ln();

    $sr++;
}



// ======================================
// TOTAL ROW
// ======================================

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

$pdf->Cell(102,8,'Total',1,0,'R');

$pdf->Cell(
    16,
    8,
    number_format($totqty,0),
    1,
    0,
    'R'
);

$pdf->Cell(18,8,'',1);

$pdf->Cell(
    22,
    8,
    number_format($totamt,2),
    1,
    0,
    'R'
);

$pdf->Cell(
    18,
    8,
    number_format($totdisc,2),
    1,
    0,
    'R'
);

$pdf->Cell(
    24,
    8,
    number_format($tottax,2),
    1,
    1,
    'R'
);



// ======================================
// NET AMOUNT
// ======================================

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

$pdf->Cell(
    158,
    10,
    'Net Amount',
    1,
    0,
    'R'
);

$pdf->Cell(
    32,
    10,
    number_format($getdata['netamt'],2),
    1,
    1,
    'R'
);



// ======================================
// AMOUNT IN WORDS
// ======================================

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

$pdf->MultiCell(
    190,
    8,
    'Rupees : '.rsword(round($getdata['netamt'])),
    1
);



// ======================================
// BANK DETAILS
// ======================================

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

$pdf->Cell(45,8,'Bank Name',1);
$pdf->Cell(145,8,$getcmpname['bankname'],1,1);

$pdf->Cell(45,8,'Branch',1);
$pdf->Cell(145,8,$getcmpname['branchname'],1,1);

$pdf->Cell(45,8,'Account No',1);
$pdf->Cell(145,8,$getcmpname['acno'],1,1);

$pdf->Cell(45,8,'IFSC Code',1);
$pdf->Cell(145,8,$getcmpname['ifccode'],1,1);



// ======================================
// DECLARATION
// ======================================

$pdf->Ln(5);

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

$note =
"I / We hereby certify that my/our registration certificate under GST rules is in force and the transaction covered by this invoice shall be accounted in turnover.";

$pdf->MultiCell(
    190,
    5,
    $note,
    1
);



// ======================================
// SIGNATURE
// ======================================

$pdf->Ln(10);

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

$pdf->Cell(
    190,
    6,
    'For '.$getcmpname['compname'],
    0,
    1,
    'R'
);

$pdf->Ln(15);

$pdf->Cell(
    190,
    6,
    'AUTHORIZED SIGNATORY',
    0,
    1,
    'R'
);



$pdf->Output();
?>