<?php
 //require_once('../auth/auth.php');
// $conn = mysqli_connect('localhost', 'root', '', 'welldiv');
        //  mysqli_set_charset($conn,"utf8");
        
        
         $conn = mysqli_connect('localhost', 'risingba_erp', 'RW@gzfaryjVp', 'risingba_erp');

        
if (isset($_GET["invoice"])) {
	 $invoice   = $_GET["invoice"];
    $pinvoice_query= mysqli_query($conn,"SELECT 
      co_name,co_email,co_phone,co_address,c.coid,b.brid,branch_name,branch_number,branch_email,branch_address,t.tid,t.invoice,t.m_invoice,date(t.date) tdate,t.date date_time,t.amount,t.details,t.em_id,a.acid,a.ac_name,a.ac_number,a.ac_details,a.ac_email,a.outlet_id
      FROM transaction t 
      LEFT JOIN company c ON(t.coid=c.coid)
      LEFT JOIN branch b ON(t.brid=b.brid)
      LEFT JOIN accounts a ON(a.acid=t.dr_acid)
      WHERE t.invoice ='$invoice' AND ttype='ERSL' LIMIT 1") or die(mysqli_error($conn));
    $pin_row= mysqli_fetch_assoc($pinvoice_query);
    $coidget        = $pin_row['coid'];
    
    $get_brid       = $pin_row['brid'];
    $branch_name    = $pin_row['branch_name'];
    $branch_address = $pin_row['branch_address'];
    $branch_number  = $pin_row['branch_number'];
    $branch_email   = $pin_row['branch_email'];

    $cacid          = $pin_row['acid'];
    $ac_name        = $pin_row['ac_name'];
    $ac_number      = $pin_row['ac_number'];
    $ac_address     = $pin_row['ac_details'];
    $ac_email       = $pin_row['ac_email'];
    $outlet_id      = $pin_row['outlet_id'];

    $em_id         = $pin_row['em_id'];
    $em_name=mysqli_fetch_assoc(mysqli_query($conn,"SELECT em_acname FROM em_employee WHERE  em_id='$em_id' LIMIT 1 "))['em_acname'];

    $tid            = $pin_row['tid'];
    $tdate          = $pin_row['tdate'];
    $m_invoice      = $pin_row['m_invoice'];
    $date_time      = $pin_row['date_time'];
    $details        = $pin_row['details'];

    $delivery  = 0;
    $discount = 0;
    $payment  = 0;


    $pritner_setup_query= mysqli_query($conn,"SELECT *,company.signature,company.co_logo FROM printer_setup 
       INNER JOIN company ON (printer_setup.coid = company.coid)
       LEFT JOIN branch b ON(printer_setup.brid=b.brid)
       WHERE  printer_setup.coid='$coidget'  AND printer_setup.brid='$get_brid' AND layout=1 LIMIT 1 ") or die (mysql_error());
       $rowps= mysqli_fetch_assoc ($pritner_setup_query);
       $psid=$rowps['psid'];
       $co_logo=$rowps['co_logo'];
       $terms = $rowps['terms'];

      $co_name        = $rowps['company_name'];
      $co_address     = $rowps['company_address'];
      $co_phone       = $rowps['company_phone'];
      $co_email       = $rowps['company_email'];

  function getCurrency(float $number)
{
    $decimal = round($number - ($no = floor($number)), 2) * 100;
    $hundred = null;
    $digits_length = strlen($no);
    $i = 0;
    $str = array();
    $words = array(0 => '', 1 => 'one', 2 => 'two',
        3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six',
        7 => 'seven', 8 => 'eight', 9 => 'nine',
        10 => 'ten', 11 => 'eleven', 12 => 'twelve',
        13 => 'thirteen', 14 => 'fourteen', 15 => 'fifteen',
        16 => 'sixteen', 17 => 'seventeen', 18 => 'eighteen',
        19 => 'nineteen', 20 => 'twenty', 30 => 'thirty',
        40 => 'forty', 50 => 'fifty', 60 => 'sixty',
        70 => 'seventy', 80 => 'eighty', 90 => 'ninety');
    $digits = array('', 'hundred','thousand','lakh', 'crore');
    while( $i < $digits_length ) {
        $divider = ($i == 2) ? 10 : 100;
        $number = floor($no % $divider);
        $no = floor($no / $divider);
        $i += $divider == 10 ? 1 : 2;
        if ($number) {
            $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
            $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
            $str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
        } else $str[] = null;
    }
    $Tk = implode('', array_reverse($str));
    $paise = ($decimal > 0) ? "." . ($words[$decimal / 10] . " " . $words[$decimal % 10]) . ' Paise' : '';
    return ($Tk ? $Tk . 'Tk ' : '') . $paise;
}


  require_once('../PDFmaster/tcpdf.php');
  class PDF extends TCPDF{
	public function Header(){
                    // $imageFile = K_PATH_IMAGES.'logo_example.png';
                    // $this->Image($imageFile,20, 10, 25,'','PNG','','T',false,300,'',false,false,0,false,false,false);
                    // $this->SetFont('helvetica','B','16');
                    // $this->Cell(90,5,'Welldiv Privet Limited',0,0,'C');
                    // $this->SetFont('helvetica','B','12');
                    // $this->Cell(69,10,'Sales invoice ',1,1,'C');
                    // $this->SetFont('helvetica','B','9');
                }

     	public function Footer(){
                    $this->SetY(-148); // position at 15 mm from bottom
                    $this->SetFont('times','B',10);
                    // MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0)
                    $this->Ln(105);  // height

                    $this->SetFont('times','B',8);
                   // $this->writeHTMLCell(189,5,'','','<div style="text-align:center;color:red;">This is computer generated invoice no signature required.
                     //   </div>',0,1);
                    $this->Ln(10);

                    $this->Cell(47,1,'DSR :','T',0,'C');
                    $this->Cell(47,1,'Receiver :','T',0,'C');
                    $this->Cell(47,1,'RSO :','T',0,'C');
                    $this->Cell(48,1,'Authorized :','T',1,'C');
                    $this->Ln(5);


                    $this->SetFont('helvetica','I',9);
                    // page number 
                    $this->Cell(189,5,'Powered by : www.welldiv.com',0,1,'C');
                    $this->Ln(1);

                    date_default_timezone_set("Asia/Dhaka");
                    $today= date("F j, Y/ g:i A",time());

                    $this->Cell(25,5,'Generation date time:'.$today,0,0,'L');
                    $this->Cell(164,5,'Page'.$this->getAliasNumPage().'of'.$this->getAliasNbPages(),0,false,'R',0,'',0,false,'T','M');
	            } // Footer()  close
        }  // Header()  close



// create new PDF document
// portrait or landscape
$pdf = new PDF('P', 'mm', 'A4', true, 'UTF-8', false,false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('WellDiv');
$pdf->SetTitle('Invoice/Vouchar');
$pdf->SetSubject('');
$pdf->SetKeywords('');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(10, 10, 10);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

// set default font subsetting mode
$pdf->setFontSubsetting(true);

// Set font
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 14, '', true);



// Add a page
$pdf->AddPage();

$pdf->Ln(5);  //height

$pdf->SetFont('helvetica','B','16');

$pdf->Cell(40,5,'' ,0,0,'L');
$imgdata = base64_decode($co_logo);
//$x = 165; $y = 28; $w = 50; $h = 50; // Image example with resizing $pdf-
$pdf->Image('@'.$imgdata,10,10,25,22);


$pdf->Cell(100,5,$co_name ,0,0,'C');
$pdf->SetFont('helvetica','B','12');
$pdf->Cell(49,8,'Sales invoice ',1,1,'C');
$pdf->SetFont('helvetica','B','9');

$pdf->Cell(40,5,'',0,0,'L');
$pdf->Cell(100,5,$co_email.' # '.$co_phone,0,0,'C');
$pdf->Cell(49,5,' ',0,1,'C');
$pdf->SetFont('helvetica','B','9');

$pdf->Cell(40,5,'',0,0,'L');
$pdf->Cell(100,5,$co_address,0,0,'C');
$pdf->Cell(49,5,' ',0,1,'C');
$pdf->SetFont('helvetica','B','9');

$pdf->Ln(3);

$pdf->SetFont('times','B',10);
$pdf->setCellPaddings(4, 0, 1, 0);

$pdf->Cell(110,5,'Bill To ...','LRT',0,'L');
$pdf->Cell(79,5,'Other Info ...','LRT',1,'L');
$pdf->SetFont('times','',10);
$pdf->Cell(110,5,'OUTLET -    : ' .$outlet_id,'L',0,'L');
$strBNFont = TCPDF_FONTS::addTTFfont('../PDFmaster/fonts/SolaimanLipi_22-02-2012.ttf', 'TrueTypeUnicode', '', 32);

$pdf->SetFont($strBNFont, '', 8, 'SolaimanLipi', 'false');
$pdf->Cell(79,5,'Memo No  -  : ' .$m_invoice,'LR',1,'L');

$pdf->Cell(110,5,'ACCOUNT   : ' .$ac_name.' '.$cacid,'L',0,'L');
$pdf->Cell(79,5,'Created Date   : ' .$date_time.' '.$cacid,'LR',1,'L');

$pdf->Cell(110,5,'CONTACT   : ' .$ac_number,'L',0,'L');
$pdf->Cell(79,5,'Created   -    : ' .$em_name,'LR',1,'L');


$pdf->Cell(110,5,'E-MAIL -    : ' .$ac_email,'L',0,'L');
$pdf->Cell(79,5,'Invoice   -    : ' .$invoice,'LR',1,'L');


$pdf->Cell(189,5,'ADDRESS   : ' .$ac_address,'LB',0,'L');
//$pdf->Cell(79,5,'Demo   -    : ' .$invoice,'LRB',1,'L');

$pdf->Ln(5);  //height

$pdf->SetFillColor(0,0,0,1);
$pdf->SetFont('times','',10);
$pdf->Cell(10,10,'S/L','LBT',0,'C',1);
$pdf->Cell(69,10,'Item Description',1,0,'C',1);
$pdf->Cell(20,10,'QTY',1,0,'C',1);
$pdf->Cell(30,10,'Price',1,0,'C',1);
$pdf->Cell(15,10,'Discount',1,0,'C',1);
$pdf->Cell(15,10,'Tax',1,0,'C',1);
$pdf->Cell(30,10,'Total','BRT',1,'C',1);
$pdf->SetFont('times','B',10);

   $order_list_query= mysqli_query($conn,"SELECT i.item_name,i.item_details,i.item_code,i.war_type,i.wa_dmy,
                      o.oid,o.qty,o.net_amount,o.amount,o.dis_amount,o.amount,o.wh_id,
                      item_group.pgroup_name,item_catagory.pcat_name,item_brand.pbrand_name  FROM order_list o 
                      LEFT JOIN item i ON(o.pro_id=i.pro_id)
                      LEFT JOIN item_group ON(i.pgid=item_group.pgid)
                      LEFT JOIN item_catagory ON (i.pcid=item_catagory.pcid)
                      LEFT JOIN item_brand ON (i.pbid=item_brand.pbid)

                      WHERE  o.invoice='$invoice' ORDER BY oid ASC") or die(mysqli_error($conn));
                      $i=0;
                      $sum_qty=0;
                      $invoice_bill=0;

                    $i=1;  // no of page start
                    $max=11;  // while sl no == 6 go to next page
                    $ic=0;
                    while($orw= mysqli_fetch_assoc($order_list_query)){
                        $sum_qty   += $orw['qty'];
                        $item_details     =  $orw['item_details'];
                        $item_name     =  $orw['item_name'];

 if ($rowps['item_group']=="On") {
                      @$pgroup_name = $orw['pgroup_name']." ";
                      } if ($rowps['item_brand']=="On"){
                      @$pbrand_name= $orw['pbrand_name']." ";
                      } if ($rowps['item_catagory']=="On"){
                      @$pcat_name= $orw['pcat_name']." ";
                      } 
 

          if ($rowps['item_code']=="On") { @$item_code = $orw['item_code']; } 


        if ($rowps['item_warranty']=="On"){
                  if ($orw['war_type']=='N') {
          @$warrenty = '';
        } else if ($orw['war_type']=='D') {
          @$warrenty = $orw['wa_dmy'].'Days';
        } else if ($orw['war_type']=='M') {
          @$warrenty = $orw['wa_dmy'].' Month';
        } else if ($orw['war_type']=='Y') {
          @$warrenty = $orw['wa_dmy'].' Years';
        } else if ($orw['war_type']=='L') {
          @$warrenty = $orw['wa_dmy'].' Lifetime';
        } 
    }


                        $qty           =  $orw['qty'];
                        $amount        = $orw['amount'];
                        $dis_amount    = $orw['dis_amount'];
                        $row_bill      = $orw['net_amount'];
                        $invoice_bill += $row_bill;

                          $rowcount=mysqli_num_rows($order_list_query);


            if (($i%$max)==0) {
              	// Add a page
				// Add a page
$pdf->AddPage();

$pdf->Ln(5);  //height

$pdf->SetFont('helvetica','B','16');
$pdf->Cell(40,5,'' ,0,0,'L');
$imgdata = base64_decode($co_logo);
//$x = 165; $y = 28; $w = 50; $h = 50; // Image example with resizing $pdf-
$pdf->Image('@'.$imgdata,10,10,25,22);

$pdf->Cell(100,5,$co_name ,0,0,'C');
$pdf->SetFont('helvetica','B','12');
$pdf->Cell(49,8,'Sales invoice ',1,1,'C');
$pdf->SetFont('helvetica','B','9');

$pdf->Cell(40,5,'',0,0,'L');
$pdf->Cell(100,5,$co_email.' # '.$co_phone,0,0,'C');
$pdf->Cell(49,5,' ',0,1,'C');
$pdf->SetFont('helvetica','B','9');

$pdf->Cell(40,5,'',0,0,'L');
$pdf->Cell(100,5,$co_address,0,0,'C');
$pdf->Cell(49,5,' ',0,1,'C');
$pdf->SetFont('helvetica','B','9');

$pdf->Ln(3);


$pdf->SetFont('times','B',10);
 $pdf->setCellPaddings(4, 0, 1, 0);

$pdf->Cell(110,5,'Bill To ...','LRT',0,'L');
$pdf->Cell(79,5,'Other Info ...','LRT',1,'L');
$pdf->SetFont('times','',10);
$pdf->Cell(110,5,'OUTLET -    : ' .$outlet_id,'L',0,'L');

$strBNFont = TCPDF_FONTS::addTTFfont('../PDFmaster/fonts/SolaimanLipi_22-02-2012.ttf', 'TrueTypeUnicode', '', 32);
$pdf->SetFont($strBNFont, '', 8, '', 'false');
$pdf->Cell(79,5,'Memo No  -  : ' .$m_invoice,'LR',1,'L');


$pdf->Cell(110,5,'ACCOUNT   : ' .$ac_name.' '.$cacid,'L',0,'L');
$pdf->Cell(79,5,'Created Date   : ' .$date_time.' '.$cacid,'LR',1,'L');

$pdf->Cell(110,5,'CONTACT   : ' .$ac_number,'L',0,'L');
$pdf->Cell(79,5,'Created   -    : ' .$em_name,'LR',1,'L');


$pdf->Cell(110,5,'E-MAIL -    : ' .$ac_email,'L',0,'L');
$pdf->Cell(79,5,'Invoice   -    : ' .$invoice,'LR',1,'L');


$pdf->Cell(189,5,'ADDRESS   : ' .$ac_address,'LB',0,'L');
//$pdf->Cell(94,5,'Demo   -    : ' .$invoice,'LRB',1,'L');

$pdf->Ln(5);  //height


$pdf->SetFillColor(0,0,0,1);
$pdf->SetFont('times','',10);
$pdf->Cell(10,10,'S/L','LBT',0,'C',1);
$pdf->Cell(69,10,'Item Description',1,0,'C',1);
$pdf->Cell(20,10,'QTY',1,0,'C',1);
$pdf->Cell(30,10,'Price',1,0,'C',1);
$pdf->Cell(15,10,'Discount',1,0,'C',1);
$pdf->Cell(15,10,'Tax',1,0,'C',1);
$pdf->Cell(30,10,'Total','BRT',1,'C',1);
$pdf->SetFont('times','B',10);
            }


              //Display table empty rows
 
 
            // $pdf->Ln(4);
			$pdf->SetFont('times','B',8);
			$pdf->setCellPaddings(1, 2, 1, 1);
			$pdf->Cell(10,10,++$ic,'LR',0, 'C', 0); // it has 6 paramiter , width,height,textcontent,border,newline,text-align
			$pdf->MultiCell(69, 10,@$pgroup_name.@$pbrand_name.@$pcat_name.@$item_name.@$item_details.@$item_code.@$warrenty,'R', 'L', 0, 0, '', '', true);
			$pdf->Cell(20,10,$qty,'LR',0,'C');
			$pdf->Cell(30,10,$amount,'R',0,'C');
			$pdf->Cell(15,10,$dis_amount,'R',0,'C');
			$pdf->Cell(15,10,00.00,'R',0,'C');
			$pdf->Cell(30,10,number_format($row_bill),'R',1,'C');






			$i++;



        }

 $little= $max -$rowcount -11;
 $big  =  $rowcount - $max;
$uuuu=  $max -9 -$big ;
if($rowcount <  $max){
 for($vi=0;$vi<$little;$vi++){
 $pdf->Cell(10,10,'','LR',0,'C',1);
 $pdf->Cell(69,10,' ','LR',0,'C',1);
 $pdf->Cell(20,10,'','LR',0,'C',1);
 $pdf->Cell(30,10,'','LR',0,'C',1);
$pdf->Cell(15,10,'','LR',0,'C',1);
 $pdf->Cell(15,10,'','LR',0,'C',1);
 $pdf->Cell(30,10,'','LR',1,'C',1);
 }
}else {
     for($vi=0;$vi<$uuuu;$vi++){
 $pdf->Cell(10,10,'','LR',0,'C',1);
 $pdf->Cell(69,10,' ','LR',0,'C',1);
 $pdf->Cell(20,10,'','LR',0,'C',1);
 $pdf->Cell(30,10,'','LR',0,'C',1);
 $pdf->Cell(15,10,'','LR',0,'C',1);
 $pdf->Cell(15,10,'','LR',0,'C',1);
 $pdf->Cell(30,10,'','LR',1,'C',1);
}
 }





            $pdf->SetFont('times','B',8);
            $pdf->setCellPaddings(1, 2, 1, 1);
            $pdf->Cell(10,7,'',1,0, 'C', 0); // it has 6 paramiter , width,height,textcontent,border,newline,text-align
            $pdf->MultiCell(69, 7,'Total',1, 'R', 0, 0, '', '', true);
            $pdf->Cell(20,7,$sum_qty,1,0,'C');
            $pdf->Cell(30,7,'',1,0,'C');
            $pdf->Cell(15,7,'',1,0,'C');
            $pdf->Cell(15,7,'',1,0,'C');
            $pdf->Cell(30,7,number_format($invoice_bill),1,1,'C');


@$delivery= mysqli_fetch_assoc(mysqli_query($conn,"SELECT amount FROM transaction WHERE coid='$coidget' AND invoice ='$invoice' AND ttype='R' AND note = 'esale_delivary' LIMIT 1"))['amount'];

            $pdf->Cell(99,7,'',0,0,'R');
            $pdf->Cell(60,7,'Delivery Charge :',1,0,'R');
            $pdf->Cell(30,7,number_format($delivery),1,1,'C');

@$discount= mysqli_fetch_assoc(mysqli_query($conn,"SELECT amount FROM transaction WHERE coid='$coidget' AND invoice ='$invoice' AND ttype='P' AND note = 'esale_discount' LIMIT 1"))['amount'];

            $pdf->Cell(99,7,'',0,0,'R');
            $pdf->Cell(60,7,'Discount :',1,0,'R');
            $pdf->Cell(30,7,number_format($discount),1,1,'C');



            $pdf->Cell(99,7,'',0,0,'R');
            $pdf->Cell(60,7,'Sub Total :',1,0,'R');
            $pdf->Cell(30,7,number_format($invoice_bill + $delivery - $discount),1,1,'C');


 @$payment= mysqli_fetch_assoc(mysqli_query($conn,"SELECT amount FROM transaction t WHERE t.coid='$coidget' AND t.invoice ='$invoice' AND t.ttype='R_R' AND t.cr_acid='$cacid' AND note='esale_payment' LIMIT 1"))['amount'];

            $pdf->Cell(99,7,'',0,0,'R');
            $pdf->Cell(60,7,'Payment :',1,0,'R');
            $pdf->Cell(30,7,number_format($payment),1,1,'C');


            $pdf->Cell(99,7,'',0,0,'R');
            $pdf->Cell(60,7,'Current Invoice Due :',1,0,'R');
            $pdf->Cell(30,7,number_format($invoice_bill + $delivery - $discount-$payment),1,1,'C');


            $pdf->Cell(99,7,'',0,0,'R');
            $pdf->Cell(60,7,'Previous Balance :',1,0,'R');
            $pdf->Cell(30,7,number_format($invoice_bill + $delivery - $discount-$payment),1,1,'C');

$pdf->Ln(5);

            $pdf->Cell(15,7,'In Word :',0,0,'L');
            $pdf->Cell(130,7, getCurrency($invoice_bill),0,0,'L');    
            $pdf->Cell(34,7,'',0,1,'R');


$pdf->Ln(5);

            $pdf->Cell(29,5,'Narration :',0,0,'L');
            $pdf->Cell(160,5,$details,0,1,'L');


 
$pdf->Ln(10);



            $pdf->Cell(189,5,'Terms & Condition :',0,1,'L');
            $strBNFont = TCPDF_FONTS::addTTFfont('../PDFmaster/fonts/SolaimanLipi_22-02-2012.ttf', 'TrueTypeUnicode', '', 32);

            $pdf->SetFont($strBNFont, '', 8, '', 'false');
            $pdf->MultiCell(189,5,$terms,0,1,'L');


            

  
















    }   // isset function close 
       $pdf->Output(); // Close and output PDF document b 
















