<?php 

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


function convertNumberToWords($number) {
    $number = str_replace(',', '', $number); // Remove commas
    $words = array(
        '0' => 'zero',
        '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',
        '100' => 'hundred',
        '1000' => 'thousand',
        '1000000' => 'million',
        '1000000000' => 'billion',
        '1000000000000' => 'trillion',
    );

    if (!is_numeric($number)) {
        return false;
    }

    if (isset($words[$number])) {
        return ucwords($words[$number]);
    }

    if ($number < 100) {
        $tens = $number - $number % 10;
        $units = $number % 10;
        return ucwords($words[$tens] . ($units > 0 ? ' ' . $words[$units] : ''));
    }

    if ($number < 1000) {
        $hundreds = $number / 100;
        $remainder = $number % 100;
        return ucwords($words[$hundreds] . ' ' . $words[100] . ($remainder > 0 ? ' and ' . convertNumberToWords($remainder) : ''));
    }

    foreach (array(1000000000, 1000000, 1000) as $num) {
        if ($number < $num) {
            continue;
        }
        $quotient = (int)($number / $num);
        $remainder = $number % $num;
        return ucwords(convertNumberToWords($quotient) . ' ' . $words[$num] . ($remainder > 0 ? ' ' . convertNumberToWords($remainder) : ''));
    }

    return false;
}
class PDF_MC_Table extends FPDF
{
    protected $widths;
    protected $aligns;

//     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');
// }
    function SetWidths($w)
    {
        // Set the array of column widths
        $this->widths = $w;
    }

    function SetAligns($a)
    {
        // Set the array of column alignments
        $this->aligns = $a;
    }

    function Row($data)
    {
        // Calculate the height of the row
        $nb = 0;
        for($i=0;$i<count($data);$i++)
            $nb = max($nb,$this->NbLines($this->widths[$i],$data[$i]));
        $h = 5*$nb;
        // Issue a page break first if needed
        $this->CheckPageBreak($h);
        // Draw the cells of the row
        for($i=0;$i<count($data);$i++)
        {
            $w = $this->widths[$i];
            $a = isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
            // Save the current position
            $x = $this->GetX();
            $y = $this->GetY();
            // Draw the border
            $this->Rect($x,$y,$w,$h);
            // Print the text
            $this->MultiCell($w,5,$data[$i],0,$a);
            // Put the position to the right of the cell
            $this->SetXY($x+$w,$y);
        }
        // Go to the next line
        $this->Ln($h);
    }

    function CheckPageBreak($h)
    {
        // If the height h would cause an overflow, add a new page immediately
        if($this->GetY()+$h>$this->PageBreakTrigger)
            $this->AddPage($this->CurOrientation);
    }

    function NbLines($w, $txt)
    {
        // Compute the number of lines a MultiCell of width w will take
        if(!isset($this->CurrentFont))
            $this->Error('No font has been set');
        $cw = $this->CurrentFont['cw'];
        if($w==0)
            $w = $this->w-$this->rMargin-$this->x;
        $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
        $s = str_replace("\r",'',(string)$txt);
        $nb = strlen($s);
        if($nb>0 && $s[$nb-1]=="\n")
            $nb--;
        $sep = -1;
        $i = 0;
        $j = 0;
        $l = 0;
        $nl = 1;
        while($i<$nb)
        {
            $c = $s[$i];
            if($c=="\n")
            {
                $i++;
                $sep = -1;
                $j = $i;
                $l = 0;
                $nl++;
                continue;
            }
            if($c==' ')
                $sep = $i;
            $l += $cw[$c];
            if($l>$wmax)
            {
                if($sep==-1)
                {
                    if($i==$j)
                        $i++;
                }
                else
                    $i = $sep+1;
                $sep = -1;
                $j = $i;
                $l = 0;
                $nl++;
            }
            else
                $i++;
        }
        return $nl;
    }

    function DrawLineAfterLn($height)
    {
        $y = $this->GetY() + $height;
        $this->Line(10, $y, 200, $y); // Adjust the coordinates as needed
    }

   
    
    
}


class ConductPDF extends FPDF {
    function vcell($c_width,$c_height,$x_axis,$text){
    $w_w=$c_height/3;
    $w_w_1=$w_w+2;
    $w_w1=$w_w+$w_w+$w_w+3;
    $len=strlen($text);// check the length of the cell and splits the text into 7 character each and saves in a array 
    
    $lengthToSplit = 7;
    if($len>$lengthToSplit){
    $w_text=str_split($text,$lengthToSplit);
    $this->SetX($x_axis);
    $this->Cell($c_width,$w_w_1,$w_text[0],'','','');
    if(isset($w_text[1])) {
        $this->SetX($x_axis);
        $this->Cell($c_width,$w_w1,$w_text[1],'','','');
    }
    $this->SetX($x_axis);
    $this->Cell($c_width,$c_height,'','LTRB',0,'L',0);
    }
    else{
        $this->SetX($x_axis);
        $this->Cell($c_width,$c_height,$text,'LTRB',0,'L',0);}
        }
    }

    $pdf = new PDF_MC_Table();
    $pdf->AddPage();
    

    if(isset($_REQUEST['r_bill_no']) && !empty($_REQUEST['r_bill_no']) && ($_REQUEST['action'] == 'sales')  ){
    
    $id=$_REQUEST['r_bill_no'];
    $current_date  = date('d-M-Y');
        
$rtn_ed = "select * from t_return_entry_main where IsActive != '2' and r_bill_no = '".base64_decode($id)."' ";

$rtn_details =  $db->get_rsltset("SELECT * FROM t_return_entry_details WHERE r_bill_no = '".base64_decode($id)."' ");

// echo $rtn_ed;
// exit();
    $rtn_all = $db->get_rsltset($rtn_ed);
    
    
    $total_sale = $rtn_all[0]['total_sale_amount'];
    $discount = $rtn_all[0]['discount_per'];
    $discount_amt = $rtn_all[0]['after_discount_amt'];
    $cgst_total = $rtn_all[0]['cgst_total'];
    $sgst_total = $rtn_all[0]['sgst_total'];
    $igst_total = $rtn_all[0]['igst_total'];
    
    $cgst_per = $rtn_all[0]['cgst_per'] ;
    $sgst_per = $rtn_all[0]['sgst_per'] ;
    $igst_per = $rtn_all[0]['igst_per'] ;
    $final_sale_amt = $rtn_all[0]['final_sale_amt'] ;
    $notes = $rtn_all[0]['notes'] ;



    $cus_id = $rtn_all[0]['cus_id'];
    $company_id = $rtn_all[0]['company_id'];
    $invoice_no = $rtn_all[0]['actu_bill_no'];
    $invoice_id = $rtn_all[0]['invoice_id'];
    $debit_notes = $rtn_all[0]['debit_notes'];
    $invoice_date =  date('d-m-Y', strtotime($rtn_all[0]['s_bill_date']));
    $return_date =  date('d-m-Y', strtotime($rtn_all[0]['return_date']));
 


    $customerRow = $db->get_rsltset("SELECT cus_id,cus_address, cus_name,cus_city,cus_state,cus_gstno,cus_postcode FROM m_customer WHERE cus_id='" . $cus_id . "'");
    $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 . "'");

    $company_name = $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'];

	$customer_name = $customerRow[0]['cus_name'];
    $cus_address = $customerRow[0]['cus_address'];
    $cus_city = $customerRow[0]['cus_city'];
    $cus_state = $customerRow[0]['cus_state'];
    $cus_country = $customerRow[0]['cus_country'];
    $cus_postcode = $customerRow[0]['cus_postcode'];
    $cus_gstno = $customerRow[0]['cus_gstno'];
		

    $pdf->SetFont('Arial', 'B', 12);
    $pdf->Cell(50, 8, '', 0, 0, 'L');
    $pdf->Cell(90,8,'SALES RETURN / CREDIT NOTE',0,0,'C');
    $pdf->Cell(50, 8, $current_date, 0, 0, 'R');
    
    $pdf->Line(10, 20, 200, 20);
    $pdf->Ln(12);


    $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, 'SILK AND LACE CLOTH MERCHANTS', 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, $c_address.", ".$c_city.", ".$c_state." - ".$c_postcode, 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, "GSTIN: ".$gst_no, 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, "MOBILE NO: +91 ".$c_mobile, 0, 0, 'C');
    $pdf->ln(12);

    $pdf->Rect(10, 55, 90, 40); // x, y, width, height
    $pdf->Rect(105, 55, 95, 40); // x, y, width, height
    
    $pdf->SetTextColor(0);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(2, 6, '', 0, 0, 'L');
    $pdf->Cell(90, 6, 'Receiver Details :', 0, 0, 'L');
    $pdf->Cell(5, 6, '', 0, 0, 'L');
   
    $pdf->Cell(40, 6, 'Invoice No: '.$invoice_no , 0, 0, 'L');
    $pdf->Cell(50, 6, 'Invoice Date: '.$invoice_date , 0, 0, 'R');
    $pdf->Ln();

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(2, 6, '', 0, 0, 'L');
    $pdf->Cell(90, 6, $customer_name, 0, 0, 'L');
    $pdf->Cell(5, 6, '', 0, 0, 'L');
    
    $pdf->SetTextColor(96,96,96); 
    $pdf->Cell(60, 6, 'Return Date: ' . $return_date, 0, 0, 'L');
    $pdf->Ln();

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

    // Break the address into multiple lines
    $address_lines = explode("\n", $cus_address.',');
    $line_height = 6; // Line height for the address
    
    foreach ($address_lines as $line) {
        $pdf->Cell(2, $line_height, '', 0, 0, 'L');
        $pdf->MultiCell(90, $line_height, $line, 0, 'L'); // Use MultiCell to allow multi-line text
    }
    
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor(96, 96, 96); 
  
    $pdf->SetFont('Arial', 'B', 8);
    $pdf->Cell(2, 6, '', 0, 0, 'L');
    $pdf->Cell(90, 6, $cus_city.','.$c_state.' - '.$c_postcode, 0, 0, 'L');
    $pdf->Cell(5, 6, '', 0, 0, 'L');
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(80, 6, 'Credit Note: ' .$invoice_id, 0, 0, 'L');
    $pdf->Ln();

    $pdf->SetTextColor(96, 96, 96); 
    $pdf->SetFont('Arial', 'B', 8);
    $pdf->Cell(2, 6, '', 0, 0, 'L');
    $pdf->Cell(90, 6,'GST NO: '. $gst_no, 0, 0, 'L');
    $pdf->Cell(5, 6, '', 0, 0, 'L');
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(80, 6, 'Debit Note: ' .$debit_notes, 0, 0, 'L');
    $pdf->Ln(15);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'B', 8);
        $pdf->SetFillColor(43,61,81);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetDrawColor(255,255, 255);
        $pdf->Cell(8, 10, 'S.no', 1, 0, 'C',1);
        $pdf->Cell(16, 6, 'Product ', 'TLR', 0, 'L',1);
        $pdf->Cell(78, 10, 'Description', 1, 0, 'C',1);
        $pdf->Cell(15, 10, 'HSN', 1, 0, 'L',1);
        $pdf->Cell(20, 10, 'Price', 1, 0, 'L',1);
        $pdf->Cell(13, 6, 'Discount', 'TLR', 0, 'C',1);
        $pdf->Cell(20, 10, 'Discount', 1, 0, 'L',1);
        $pdf->Cell(20, 10, 'Final Amount', 1, 0, 'L',1);
        // NEXT LINE
        $pdf->Ln(6);
        $pdf->Cell(8, 0, '', 0, 0, 'C',1);
        $pdf->Cell(16, 4, ' code', 'BLR', 0, 'L',1);
        $pdf->Cell(78, 4, '', 'BLR', 0, 'C',1);
        $pdf->Cell(15, 4, '', 'BLR', 0, 'C',1);
        $pdf->Cell(20, 4, '', 'BLR', 0, 'C',1);
        $pdf->Cell(13, 4, '(%)', 'BLR', 0, 'C',1);
        $pdf->Cell(20, 4, '', 'BLR', 0, 'C',1);
        $pdf->Cell(20, 4, '', 'BLR', 0, 'C',1);

        $pdf->SetFillColor(0);
        $pdf->SetTextColor(0);
        $pdf->SetDrawColor(0);
    $pdf->Ln();


$sno = 1;
foreach ($rtn_details as $key)
{
    $formattedDate = date('d-m-Y', strtotime($key['createddate']));
    $cellWidth =78;
    $cellHeight =5;
    if ($pdf->GetStringWidth($key['saree_desc']) > $cellWidth) {
        $line = ceil($pdf->GetStringWidth($key['saree_desc']) / ($cellWidth - $errMargin));
    } else {
        $line = 1;
    }

    $description = $key['saree_desc'];
    $hsn = '5007 - 9010';

    $pdf->SetFont('Arial', 'B', 8);
    // Table with 1 row and 6 columns
    // $pdf->SetTextColor(200, 150, 155);

    $pdf->SetWidths(array(8,16,78,15,20,13,20,20));
    // $alignments = array('', 'C', '', 'R', 'R','R'); 
    // $pdf->SetAligns($alignments);
    // if ($status === 'Pending') {
    //     $pdf->SetTextColor(200, 0, 0); // Set text color to red for "Pending"
    // }
    $pdf->Row(
        array(
            $sno, 
            $key['pdt_sku'], 
            $description, 
            $hsn,
            $key['sale_price'], 
            $key['pdt_discount_percent'], 
            $key['pdt_discount_amt'], 
            $key['tot_amount']
        )
    );
    $pdf->SetTextColor(0);
$sno++;

}


$pdf->ln();

    $pdf->SetFont('Arial', 'B', 8); 
    $pdf->cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, 'Total Amount', 1, 0, 'L');
    $pdf->Cell(40, 6, $total_sale, 1, 0, 'R');
    $pdf->ln();


    $pdf->SetFont('Arial', 'B', 8); 
    $pdf->cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, 'Discount (%)', 1, 0, 'L');
    $pdf->Cell(40, 6, $discount, 1, 0, 'R');
    $pdf->ln();

    $pdf->SetFont('Arial', 'B', 8); 
    $pdf->cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, 'Discount Amount', 1, 0, 'L');
    $pdf->Cell(40, 6, $discount_amt, 1, 0, 'R');
    $pdf->ln();

$total = number_format($total_sale - $discount_amt, 2);

    $pdf->SetFont('Arial', 'B', 8); 
    $pdf->cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, 'Total', 1, 0, 'L');
    $pdf->Cell(40, 6, $total, 1, 0, 'R');
    $pdf->ln();

    $pdf->SetFont('Arial', 'B', 8); 
    $pdf->cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, 'CGST Amount ('.$cgst_per.'%)', 1, 0, 'L');
    $pdf->Cell(40, 6, $cgst_total, 1, 0, 'R');
    $pdf->ln();

    $pdf->SetFont('Arial', 'B', 8); 
    $pdf->cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, 'SGST Amount ('.$sgst_per.'%)', 1, 0, 'L');
    $pdf->Cell(40, 6, $sgst_total, 1, 0, 'R');
    $pdf->ln();

    $pdf->SetFont('Arial', 'B', 8); 
    $pdf->cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, 'IGST Amount ('.$igst_per.'%)', 1, 0, 'L');
    $pdf->Cell(40, 6, $igst_total, 1, 0, 'R');
    $pdf->ln();

    $pdf->SetFont('Arial', 'B', 10); 
    $pdf->cell(50, 8, '', 0, 0, 'L');
    $pdf->Cell(50, 8, '', 0, 0, 'L');
    $pdf->Cell(50, 8, 'Final Total', 1, 0, 'L');
    $pdf->Cell(40, 8, $final_sale_amt, 1, 0, 'R');// Ensure that the number is formatted correctly
    $pdf->Ln();

    $pdf->DrawLineAfterLn(8);
    $pdf->Ln();
    $final_sale_amt_in_words = convertNumberToWords($final_sale_amt);
    $pdf->SetFont('Arial', 'B', 10); 
    $pdf->cell(0, 15, 'Amount in Words: '.$final_sale_amt_in_words .'  rupees only/-', 0, 0, 'L');
    $pdf->Ln();

    $pdf->DrawLineAfterLn(1);
    $pdf->Ln(4);

    $cellWidth = 110;
    $cellHeight = 10;
    
    // Calculate the number of lines needed for the notes
    if ($pdf->GetStringWidth($notes) > $cellWidth) {
        $line = ceil($pdf->GetStringWidth($notes) / $cellWidth);
    } else {
        $line = 1;
    }
    
    $pdf->SetTextColor(0);
    $pdf->SetFont('Arial', 'B', 10);
$notes = 'Notes: '.$notes;
$company_name = 'For: '.$company_name;

// $pdf->SetWidths(array(110));
// // $pdf->SetRowBorderStyle('');

//     $pdf->Row(
//         array(
//             $notes, 
//         ),
//         array(
//             0, // Indicates whether the cell should be drawn with borders
//             0 // Indicates whether the cell should be drawn with borders
//         ),
       
//     );
    
    // Output the notes with proper alignment and height based on the number of lines
    $cellWidth = 120;
    $cellHeight = 8;
    
    // Calculate the number of lines needed for the notes
    if ($pdf->GetStringWidth($notes) > $cellWidth) {
        $line = ceil($pdf->GetStringWidth($notes) / $cellWidth);
    } else {
        $line = 1;
    }
    
    $pdf->SetTextColor(0);
    $pdf->SetFont('Arial', 'B', 10);
    
    // Output the notes with proper alignment and height based on the number of lines
    $pdf->MultiCell($cellWidth, $cellHeight, $notes, 0, 'LT');
    
    // Adjust the y position for the next cell
    // $pdf->SetY($pdf->GetY() + $cellHeight * $line);
    
    
    $pdf->SetFont('Arial', 'B', 8); // Set the font size and style before outputting the company name
    $pdf->SetFillColor(204,255,255);
    $pdf->SetTextColor(255,0,0);
    $pdf->SetDrawColor(255,255, 255);
    $pdf->Cell(120, 10, '', 0, 0, 'LT');
    $pdf->Cell(70, -10, $company_name, 1, 1, B,1); // Note the change in y position to move to the next line
    // $pdf->Ln();

    // $pdf->SetTextColor(0);
    // $pdf->SetFont('Arial', 'B', 10); 
    // $pdf->Cell(120, 10, '', 0, 0, 'LT'); // Ensure top alignment ('T')
    
    // $pdf->SetFont('Arial', 'B', 8); // Set the font size and style before outputting the company name
    // $pdf->SetFillColor(204,255,255);
    // $pdf->SetTextColor(0);
    // $pdf->SetDrawColor(255,255, 255);
    
    // $pdf->Cell(70, 20, '', 0, 1, B,1); // Note the change in y position to move to the next line
    

    $pdf->Output('return_invoice.pdf', 'I');
    $pdf->Close();
}

/////////////////////////////////////////////////////////// purchase return //////////////////////////////////////////////

   else if(isset($_REQUEST['r_bill_no']) && !empty($_REQUEST['r_bill_no']) && ($_REQUEST['action'] == 'purchase')  ){
    
    $id=$_REQUEST['r_bill_no'];
    $current_date  = date('d-M-Y');
        
$rtn_ed = "select * from t_pur_return_entry_main where IsActive != '2' and r_bill_no = '".base64_decode($id)."' ";

$rtn_details =  $db->get_rsltset("SELECT * FROM t_pur_return_entry_details WHERE r_bill_no = '".base64_decode($id)."' ");

// echo $rtn_ed;
// exit();
    $rtn_all = $db->get_rsltset($rtn_ed);
    
    
    $total_purchase = $rtn_all[0]['tot_pur_amount'];
    $discount = $rtn_all[0]['discount_per'];
    $discount_amt = $rtn_all[0]['after_discount_amt'];
    $cgst_total = $rtn_all[0]['cgst_total'];
    $sgst_total = $rtn_all[0]['sgst_total'];
    $igst_total = $rtn_all[0]['igst_total'];
    
    $cgst_per = $rtn_all[0]['cgst_per'] ;
    $sgst_per = $rtn_all[0]['sgst_per'] ;
    $igst_per = $rtn_all[0]['igst_per'] ;
    $final_pur_amt = $rtn_all[0]['final_pur_amt'] ;
    $notes = $rtn_all[0]['notes'] ;



    $sup_id = $rtn_all[0]['sup_id'];
    $company_id = $rtn_all[0]['company_id'];
    $invoice_no = $rtn_all[0]['actu_pur_bill_no'];
    $invoice_id = $rtn_all[0]['p_invoice_id'];
    $credit_notes = $rtn_all[0]['credit_notes'];
    $invoice_date =  date('d-m-Y', strtotime($rtn_all[0]['p_bill_date']));
    $return_date =  date('d-m-Y', strtotime($rtn_all[0]['return_date']));
 


    $customerRow = $db->get_rsltset("SELECT sup_id,sup_address, sup_name,sup_city,sup_state,sup_gstno,sup_postcode FROM m_supplier WHERE sup_id='" . $sup_id . "'");
    $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 . "'");

    $company_name = $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'];

	$customer_name = $customerRow[0]['sup_name'];
    $sup_address = $customerRow[0]['sup_address'];
    $sup_city = $customerRow[0]['sup_city'];
    $sup_state = $customerRow[0]['sup_state'];
    $sup_country = $customerRow[0]['sup_country'];
    $sup_postcode = $customerRow[0]['sup_postcode'];
    $sup_gstno = $customerRow[0]['sup_gstno'];
		

    $pdf->SetFont('Arial', 'B', 12);
    $pdf->Cell(50, 8, '', 0, 0, 'L');
    $pdf->Cell(90,8,'PURCHASE RETURN / CREDIT NOTE',0,0,'C');
    $pdf->Cell(50, 8, $current_date, 0, 0, 'R');
    
    $pdf->Line(10, 20, 200, 20);
    $pdf->Ln(12);


    $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, 'SILK AND LACE CLOTH MERCHANTS', 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, $c_address.", ".$c_city.", ".$c_state." - ".$c_postcode, 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, "GSTIN: ".$gst_no, 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, "MOBILE NO: +91 ".$c_mobile, 0, 0, 'C');
    $pdf->ln(12);

    $pdf->Rect(10, 55, 90, 40); // x, y, width, height
    $pdf->Rect(105, 55, 95, 40); // x, y, width, height
    
    $pdf->SetTextColor(0);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(2, 6, '', 0, 0, 'L');
    $pdf->Cell(90, 6, 'Receiver Details :', 0, 0, 'L');
    $pdf->Cell(5, 6, '', 0, 0, 'L');
   
    $pdf->Cell(40, 6, 'Invoice No: '.$invoice_no , 0, 0, 'L');
    $pdf->Cell(50, 6, 'Invoice Date: '.$invoice_date , 0, 0, 'R');
    $pdf->Ln();

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(2, 6, '', 0, 0, 'L');
    $pdf->Cell(90, 6, $customer_name, 0, 0, 'L');
    $pdf->Cell(5, 6, '', 0, 0, 'L');
    
    $pdf->SetTextColor(96,96,96); 
    $pdf->Cell(60, 6, 'Return Date: ' . $return_date, 0, 0, 'L');
    $pdf->Ln();

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

    // Break the address into multiple lines
    $address_lines = explode("\n", $cus_address.',');
    $line_height = 6; // Line height for the address
    
    foreach ($address_lines as $line) {
        $pdf->Cell(2, $line_height, '', 0, 0, 'L');
        $pdf->MultiCell(90, $line_height, $line, 0, 'L'); // Use MultiCell to allow multi-line text
    }
    
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor(96, 96, 96); 
  
    $pdf->SetFont('Arial', 'B', 8);
    $pdf->Cell(2, 6, '', 0, 0, 'L');
    $pdf->Cell(90, 6, $cus_city.','.$c_state.' - '.$c_postcode, 0, 0, 'L');
    $pdf->Cell(5, 6, '', 0, 0, 'L');
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(80, 6, 'Credit Note: ' .$invoice_id, 0, 0, 'L');
    $pdf->Ln();

    $pdf->SetTextColor(96, 96, 96); 
    $pdf->SetFont('Arial', 'B', 8);
    $pdf->Cell(2, 6, '', 0, 0, 'L');
    $pdf->Cell(90, 6,'GST NO: '. $gst_no, 0, 0, 'L');
    $pdf->Cell(5, 6, '', 0, 0, 'L');
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(80, 6, 'Debit Note: ' .$credit_notes, 0, 0, 'L');
    $pdf->Ln(15);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'B', 8);
        $pdf->SetFillColor(43,61,81);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetDrawColor(255,255, 255);
        $pdf->Cell(8, 10, 'S.no', 1, 0, 'C',1);
        $pdf->Cell(16, 6, 'Product ', 'TLR', 0, 'L',1);
        $pdf->Cell(78, 10, 'Description', 1, 0, 'C',1);
        $pdf->Cell(15, 10, 'HSN', 1, 0, 'L',1);
        $pdf->Cell(20, 10, 'Price', 1, 0, 'L',1);
        $pdf->Cell(13, 6, 'Discount', 'TLR', 0, 'C',1);
        $pdf->Cell(20, 10, 'Discount', 1, 0, 'L',1);
        $pdf->Cell(20, 10, 'Final Amount', 1, 0, 'L',1);
        // NEXT LINE
        $pdf->Ln(6);
        $pdf->Cell(8, 0, '', 0, 0, 'C',1);
        $pdf->Cell(16, 4, ' code', 'BLR', 0, 'L',1);
        $pdf->Cell(78, 4, '', 'BLR', 0, 'C',1);
        $pdf->Cell(15, 4, '', 'BLR', 0, 'C',1);
        $pdf->Cell(20, 4, '', 'BLR', 0, 'C',1);
        $pdf->Cell(13, 4, '(%)', 'BLR', 0, 'C',1);
        $pdf->Cell(20, 4, '', 'BLR', 0, 'C',1);
        $pdf->Cell(20, 4, '', 'BLR', 0, 'C',1);

        $pdf->SetFillColor(0);
        $pdf->SetTextColor(0);
        $pdf->SetDrawColor(0);
    $pdf->Ln();


$sno = 1;
foreach ($rtn_details as $key)
{
    $formattedDate = date('d-m-Y', strtotime($key['createddate']));
    $cellWidth =78;
    $cellHeight =5;
    if ($pdf->GetStringWidth($key['saree_desc']) > $cellWidth) {
        $line = ceil($pdf->GetStringWidth($key['saree_desc']) / ($cellWidth - $errMargin));
    } else {
        $line = 1;
    }

    $description = $key['saree_desc'];
    $hsn = '5007 - 9010';

    $pdf->SetFont('Arial', 'B', 8);
    // Table with 1 row and 6 columns
    // $pdf->SetTextColor(200, 150, 155);

    $pdf->SetWidths(array(8,16,78,15,20,13,20,20));
    // $alignments = array('', 'C', '', 'R', 'R','R'); 
    // $pdf->SetAligns($alignments);
    // if ($status === 'Pending') {
    //     $pdf->SetTextColor(200, 0, 0); // Set text color to red for "Pending"
    // }
    $pdf->Row(
        array(
            $sno, 
            $key['pdt_sku'], 
            $description, 
            $hsn,
            $key['purchase_price'], 
            $key['pdt_discount_percent'], 
            $key['pdt_discount_amt'], 
            $key['tot_amount']
        )
    );
    $pdf->SetTextColor(0);
$sno++;

}


$pdf->ln();

    $pdf->SetFont('Arial', 'B', 8); 
    $pdf->cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, 'Total Amount', 1, 0, 'L');
    $pdf->Cell(40, 6, $total_purchase, 1, 0, 'R');
    $pdf->ln();


    $pdf->SetFont('Arial', 'B', 8); 
    $pdf->cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, 'Discount (%)', 1, 0, 'L');
    $pdf->Cell(40, 6, $discount, 1, 0, 'R');
    $pdf->ln();

    $pdf->SetFont('Arial', 'B', 8); 
    $pdf->cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, 'Discount Amount', 1, 0, 'L');
    $pdf->Cell(40, 6, $discount_amt, 1, 0, 'R');
    $pdf->ln();

$total = number_format($total_purchase - $discount_amt, 2);

    $pdf->SetFont('Arial', 'B', 8); 
    $pdf->cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, 'Total', 1, 0, 'L');
    $pdf->Cell(40, 6, $total, 1, 0, 'R');
    $pdf->ln();

    $pdf->SetFont('Arial', 'B', 8); 
    $pdf->cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, 'CGST Amount ('.$cgst_per.'%)', 1, 0, 'L');
    $pdf->Cell(40, 6, $cgst_total, 1, 0, 'R');
    $pdf->ln();

    $pdf->SetFont('Arial', 'B', 8); 
    $pdf->cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, 'SGST Amount ('.$sgst_per.'%)', 1, 0, 'L');
    $pdf->Cell(40, 6, $sgst_total, 1, 0, 'R');
    $pdf->ln();

    $pdf->SetFont('Arial', 'B', 8); 
    $pdf->cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, '', 0, 0, 'L');
    $pdf->Cell(50, 6, 'IGST Amount ('.$igst_per.'%)', 1, 0, 'L');
    $pdf->Cell(40, 6, $igst_total, 1, 0, 'R');
    $pdf->ln();

    $pdf->SetFont('Arial', 'B', 10); 
    $pdf->cell(50, 8, '', 0, 0, 'L');
    $pdf->Cell(50, 8, '', 0, 0, 'L');
    $pdf->Cell(50, 8, 'Final Total', 1, 0, 'L');
    $pdf->Cell(40, 8, $final_pur_amt, 1, 0, 'R');// Ensure that the number is formatted correctly
    $pdf->Ln();

    $pdf->DrawLineAfterLn(8);
    $pdf->Ln();
    $final_pur_amt_in_words = convertNumberToWords($final_pur_amt);
    $pdf->SetFont('Arial', 'B', 10); 
    $pdf->cell(0, 15, 'Amount in Words: '.$final_pur_amt_in_words .'  rupees only/-', 0, 0, 'L');
    $pdf->Ln();

    $pdf->DrawLineAfterLn(1);
    $pdf->Ln(4);

    $cellWidth = 110;
    $cellHeight = 10;
    
    // Calculate the number of lines needed for the notes
    if ($pdf->GetStringWidth($notes) > $cellWidth) {
        $line = ceil($pdf->GetStringWidth($notes) / $cellWidth);
    } else {
        $line = 1;
    }
    
    $pdf->SetTextColor(0);
    $pdf->SetFont('Arial', 'B', 10);
$notes = 'Notes: '.$notes;
$company_name = 'For: '.$company_name;

// $pdf->SetWidths(array(110));
// // $pdf->SetRowBorderStyle('');

//     $pdf->Row(
//         array(
//             $notes, 
//         ),
//         array(
//             0, // Indicates whether the cell should be drawn with borders
//             0 // Indicates whether the cell should be drawn with borders
//         ),
       
//     );
    
    // Output the notes with proper alignment and height based on the number of lines
    $cellWidth = 120;
    $cellHeight = 8;
    
    // Calculate the number of lines needed for the notes
    if ($pdf->GetStringWidth($notes) > $cellWidth) {
        $line = ceil($pdf->GetStringWidth($notes) / $cellWidth);
    } else {
        $line = 1;
    }
    
    $pdf->SetTextColor(0);
    $pdf->SetFont('Arial', 'B', 10);
    
    // Output the notes with proper alignment and height based on the number of lines
    $pdf->MultiCell($cellWidth, $cellHeight, $notes, 0, 'LT');
    
    // Adjust the y position for the next cell
    // $pdf->SetY($pdf->GetY() + $cellHeight * $line);
    
    
    $pdf->SetFont('Arial', 'B', 8); // Set the font size and style before outputting the company name
    $pdf->SetFillColor(204,255,255);
    $pdf->SetTextColor(255,0,0);
    $pdf->SetDrawColor(255,255, 255);
    $pdf->Cell(120, 10, '', 0, 0, 'LT');
    $pdf->Cell(70, -10, $company_name, 1, 1, B,1); // Note the change in y position to move to the next line
    // $pdf->Ln();

    // $pdf->SetTextColor(0);
    // $pdf->SetFont('Arial', 'B', 10); 
    // $pdf->Cell(120, 10, '', 0, 0, 'LT'); // Ensure top alignment ('T')
    
    // $pdf->SetFont('Arial', 'B', 8); // Set the font size and style before outputting the company name
    // $pdf->SetFillColor(204,255,255);
    // $pdf->SetTextColor(0);
    // $pdf->SetDrawColor(255,255, 255);
    
    // $pdf->Cell(70, 20, '', 0, 1, B,1); // Note the change in y position to move to the next line
    

    $pdf->Output('Purchase_return_invoice.pdf', 'I');
    $pdf->Close();
}