HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.30
System: Linux iZj6c1151k3ad370bosnmsZ 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64
User: root (0)
PHP: 7.4.30
Disabled: NONE
Upload Files
File: /var/www/html/amberconcept/po_pdf.php.bak20230309a
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//include wp libriries and set timezone to HK
require_once('wp-load.php'); 
require_once('PHPExcel.php');
require_once('TCPDF/data/tcpdf_include.php');
date_default_timezone_set("Asia/Hong_Kong"); 
if(isset($_GET['id']) && $_GET['id']!="") {
	$sql = "SELECT * FROM custom_po_master WHERE id = '".$_GET['id']."%' LIMIT 1";		
	$result = $wpdb->get_results($sql);
	
	if(count($result)>0){
		$poObj = $result[0];
	} else {	
		echo "Invalid PO";
		exit();
	}	
} else {
	echo "Invalid Parameter";
	exit();
}

$order_id = $poObj->order_id;
$order = wc_get_order($order_id);
$isFotomax = $order->get_user_id()==7?true:false;
$htmlHeader = '';
function getHeaderString($po_number, $po_line, $po_date, $order_id){
// draw header table
    return $htmlHeader = '<div id="headerSection">
        <table cellspacing="2" cellpadding="2">
            <tr>
                <td>
                    <table id="po_table" cellspacing="1" cellpadding="1">
                        <tr>
                            <td colspan="2"><h1>PURCHASE ORDER</h1></td>
                        </tr>
                        <tr>					
                            <td>PO Number:</td><td>'.$po_number.'-'.$po_line.'</td>					
                        </tr>
                        <tr>					
                            <td>DATE:</td><td>'.date('F d, Y', strtotime($po_date)).'</td>
                        </tr>
                        <tr>					
                            <td>VENDOR NO.:</td><td>Wink Impression</td>
                        </tr>
                        <tr>					
                            <td>ORDER ID:</td><td>'.$order_id.'</td>					
                        </tr>
                    </table>
                </td>
                <td></td>
                <td>
                    <table id="amber_table" cellspacing="1" cellpadding="1" style="text-align: right;">
                        <tr>
                            <td><img src="custom/image/Amber.png" width="150"></td>
                        </tr>				
                        <tr>					
                            <td><small>Flat 6A, Kin Ho Industrial Building, Block 1<br>14-24 Au Pui Wan Street, Fotan, NT, HK</small></td>
                        </tr>
                        <tr>					
                            <td><small>Tel: 852-2278 5768<br>Fax: 852-2691 0724<br>Email: cs.hk@amberconcept.com</small></td>
                        </tr>				
                    </table>
                </td>
            </tr>
        </table>
    </div>';
}

$vendor_detail = "";
$arr_vendor_detail = explode("\r\n", $poObj->vendor_detail);
foreach($arr_vendor_detail as $vendor_detail_line){
	$vendor_detail .= "<tr><td>".$vendor_detail_line."</td></tr>";
}

$ship_detail = "";
$arr_ship_detail = explode("\r\n", $poObj->ship_detail);
foreach($arr_ship_detail as $ship_detail_line){
	$ship_detail .= "<tr><td>".$ship_detail_line."</td></tr>";
}

// draw address table
$htmlAddress = '<div id="addressSection">
	<table cellspacing="0" cellpadding="0" border="0.01">
		<tr>
			<td width="30%">
				<table id="vendor_table" cellspacing="0" cellpadding="2">
					<tr style="background-color:#000;color:#FFF">
						<td>Vendor</td>
					</tr>'.$vendor_detail.'																								
				</table>
			</td>  
			<td width="30%">
				<table id="ship_table" cellspacing="1" cellpadding="1">
					<tr style="background-color:#000;color:#FFF">
						<td>Ship To</td>
					</tr>'.$ship_detail.'																								
				</table>
			</td> 
			<td width="40%">
				<table id="other_table" cellspacing="1" cellpadding="1">
					<tr style="background-color:#000;color:#FFF">
						<td colspan="2">Other Information</td>
					</tr>
					<tr>
						<td>Ship Via: </td><td>'.$order->get_shipping_method().'</td>
					</tr>
					<tr>
						<td>Shipment Term: </td><td>FOB</td>
					</tr>
					<tr>
						<td>Payment Term: </td><td>Net 30 Days</td>
					</tr>	
					<tr>
						<td>Request Delivery Date: </td><td>'.($isFotomax?'Fotomax單, 7日內交':'10日內交').'</td>
					</tr>
					<tr>
						<td>Courier: </td><td>Fedex/SF Express/GE-DHL</td>
					</tr>	
					<tr>
						<td>Shippng Document Value: </td><td>US$50 /pc</td>
					</tr>																									
					<tr>
						<td>Weight & Ctn Size: </td><td>Pls see packing list</td>
					</tr>
				</table>
			</td> 				      
		</tr>	
	</table>
</div>';



// draw footer cell (right)
$htmlPrice = '
<table cellspacing="0" cellpadding="1" border="0.01">
	<tr>
		<td width="70%" align="right" style="background-color:#000;color:#FFF">Sub Total ('.$poObj->currency.'):</td>
		<td width="30%" align="center" style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray">'.number_format($poObj->po_amount, 2, '.', ',').'</td>					
	</tr>
	<tr>
		<td width="70%" align="right" style="background-color:#000;color:#FFF">Discount ('.$poObj->currency.'):</td>
		<td width="30%" align="center" style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray"></td>					
	</tr>
	<tr>
		<td width="70%" align="right" style="background-color:#000;color:#FFF">Sales Tax Rate ('.$poObj->currency.'):</td>
		<td width="30%" align="center" style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray"></td>					
	</tr>
	<tr>
		<td width="70%" align="right" style="background-color:#000;color:#FFF">Sales Tax ('.$poObj->currency.'):</td>
		<td width="30%" align="center" style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray"></td>					
	</tr>
	<tr>
		<td width="70%" align="right" style="background-color:#000;color:#FFF">Other Cost ('.$poObj->currency.'):</td>
		<td width="30%" align="center" style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray"></td>					
	</tr>
	<tr>
		<td width="70%" align="right" style="background-color:#000;color:#FFF">Grand Total ('.$poObj->currency.'):</td>
		<td width="30%" align="center" style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray">'.number_format($poObj->po_amount, 2, '.', ',').'</td>					
	</tr>
</table>';


// draw date cell (left)
$htmlDate = '<table id="dateSection" width="100%">
				<tr>
					<td><h1>'.date('F d, Y', strtotime($poObj->po_date)).'</h1></td>
			 	</tr>
				<tr>
					<td><hr></td>
			 	</tr>
				<tr>
					<td>Date</td>
			 	</tr>								
			 </table>';
			 		 
// draw sign cell (right)			 
$htmlSign = '<table id="signSection" width="100%">
				<tr>
					<td><h1>Sam Lee</h1></td>
			 	</tr>
				<tr>
					<td><hr></td>
			 	</tr>
				<tr>
					<td>Authorized Signature</td>
			 	</tr>								
			 </table>';	

// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {

	public $htmlHeader;
	public $htmlAddress;
	public $htmlShipping;
	public $htmlPrice;
	public $htmlDate;
	public $htmlSign;
	public $poNumber;
	public $materialName;
	public $size;
	public $binding;
	public $spreads;

    //Page header
    public function Header() {		
		$this->writeHTML($this->htmlHeader, true, false, false, false, '');		
		$this->Line(10, 38, 200, 38, array('width' => 0.7));	
		$this->writeHTML($this->htmlAddress, true, false, false, false, '');			
    }

    // Page footer
    public function Footer() {
        // Position at 60 mm from bottom
        $this->SetY(-60);
		
		$this->SetFillColor(204,204,204);
		$this->SetTextColor(0, 0, 0);
		$this->writeHTMLCell(60, '', 10, 235, $this->htmlShipping, 0, 1, 1, true, 'L', true);		
		
		$this->SetFillColor(255, 255, 255);
		$this->writeHTMLCell(60, '', 140, 235, $this->htmlPrice, 0, 1, 1, true, 'J', true);
		$this->writeHTMLCell(60, '', 10, 265, $this->htmlDate, 0, 1, 1, true, 'J', true);
		$this->writeHTMLCell(60, '', 140, 265, $this->htmlSign, 0, 1, 1, true, 'J', true);	
		
		$this->Line(10, 286, 200, 286, array('width' => 0.3));	
        // Page number
		$customDescription = $this->poNumber."_".$this->materialName."_".$this->size."_".$this->binding."_".$this->spreads;
		$this->Cell(10, 20, $customDescription, 0, false, 'L', 0, '', 0, false, 'T', 'M');
        $this->Cell(195, 20, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
		
    }
}

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->htmlHeader = getHeaderString($poObj->po_number, 1, $poObj->po_date, $poObj->order_id);
$pdf->htmlAddress = $htmlAddress;
$pdf->htmlPrice = $htmlPrice;
$pdf->htmlDate = $htmlDate;	
$pdf->htmlSign = $htmlSign;	

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('WH-HKMIS');
$pdf->SetTitle('AMBER PO');
$pdf->SetSubject('AMBER PO');
$pdf->SetKeywords('AMBER PO');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

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

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

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

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

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

// ---------------------------------------------------------

// set font
$pdf->SetFont('DroidSansFallback', '', 7, '', true);

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

// draw product table

$sql = "SELECT * FROM custom_po_detail WHERE po_id = '".$_GET['id']."%' ";		
$result = $wpdb->get_results($sql);
$i = 1;
foreach($result as $poLineObj){	
    $pdf->resetHeaderTemplate();
    $pdf->htmlHeader = getHeaderString($poObj->po_number, $i, $poObj->po_date, $poObj->order_id);	
	$pdf->AddPage();
	$pdf->poNumber = $poObj->po_number."-".$i;
	$htmlProduct = '<div id="productSection">';
	$htmlProduct .= '<table cellspacing="0" cellpadding="0" border="0.001" nobr="true" class="productTable"><tr><td>
	<table cellspacing="0" cellpadding="3">
		<tr style="background-color:#000;color:#FFF">
			<td width="20%" align="left">Description '.$i.'</td>
			<td width="48%" align="left"></td>
			<td width="10%" align="center">Qty.</td>
			<td width="12%" align="center">Unit Price ('.$poObj->currency.')</td>
			<td width="10%" align="center">Total ('.$poObj->currency.')</td>
		</tr> ';
	$i++;	
	$bill_to_name = $order->get_formatted_billing_full_name();
	$array_logo_file = array(
		'Upload' => get_customer_logo_po($bill_to_name)!=""?get_customer_logo_po(trim($bill_to_name)):'Logo圖樣',
		'Letterpress' => "壓字粒"
	);		
	
	
	if($poLineObj->item_type=="Product Cost"){		
		$arr_content = explode("\r\n", $poLineObj->item_name);
		foreach($arr_content as $content_line){		
			$arr_content_line = explode("::",$content_line);
			if($arr_content_line[0]=="Name 產品名稱") {	
				$itemName = $arr_content_line[1];
				$htmlProduct .= '<tr><td style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray">'.$arr_content_line[0].'</td><td style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray">'.$arr_content_line[1].'</td style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray"><td align="right" style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray">'.$poLineObj->item_qty.'</td><td align="right" style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray">'.number_format($poLineObj->item_unit_price,2,".",",").'</td><td align="right" style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray">'.number_format($poLineObj->item_total,2,".",",").'</td></tr>';
			} else {

				if($arr_content_line[0]=="Spreads 頁數") {
					$pdf->spreads = trim($arr_content_line[1]);
				}

				if($arr_content_line[0]=="Size 尺寸") {
					$pdf->size = trim($arr_content_line[1]);
				}
				
				if($arr_content_line[0]=="Binding 釘裝方式") {
					$lineContents = explode("(",trim($arr_content_line[1]));
					$pdf->binding = rtrim($lineContents[1],")");
				}
				
				if($arr_content_line[0]=="Color 封殼物料") {
					$lineContents = explode("(",trim($arr_content_line[1]));
					$lineContents2 = explode("+",trim($lineContents[1]));
					$arr_adjusted = explode(" ",trim($lineContents2[0]));
					array_shift($arr_adjusted);
					$finaliszed_string = implode($arr_adjusted, " ");
					$finaliszed_string = str_replace(":","",$finaliszed_string);
					$pdf->materialName = trim($finaliszed_string);
				}				


				$htmlProduct .= '<tr><td style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray">'.$arr_content_line[0].'</td><td colspan="4" style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray">'.$arr_content_line[1].'</td></tr>';
			}	
			
			if($arr_content_line[0]=="Fotomax PO")	 {
				$fotomaxPo = $arr_content_line[1];
			}
		}	
	} else {
		$itemName = $poLineObj->item_name;
		$htmlProduct .= '<tr><td style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray">'.$poLineObj->item_type.'</td><td style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray">'.$poLineObj->item_name.'</td><td align="right" style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray">'.$poLineObj->item_qty.'</td><td align="right" style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray">'.number_format($poLineObj->item_unit_price,2,".",",").'</td><td align="right" style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray">'.number_format($poLineObj->item_total,2,".",",").'</td></tr>';		
	}
	
	if(trim($itemName)=="Canvas"){
		$file = get_post_meta($order_id, '_wcuf_uploaded_files', true);
		if($file!=""){
			foreach($file as $fileArr){					
				$htmlProduct .= '<tr><td colspan="5" style="border-right:0.01px solid lightgray; border-bottom:0.01px solid lightgray"><img src="'.$fileArr['absolute_path'][0].'" width="300"><img></td></tr>';	
			}
		}
	}	
	
	$htmlProduct .= '</table></td></tr></table>';
	$htmlProduct .= '</div>';
	$pdf->writeHTML($htmlProduct, true, false, false, false, '');
	
	// draw footer cell (left)
	if($isFotomax){
		$htmlShipping = "Fotomax<br>(".$fotomaxPo.")";
	} else {
		
		$customer_address = "";
		$arr_customer_address = explode("\r\n", $poObj->customer_ship_address);
		foreach($arr_customer_address as $customer_address_line){
			$customer_address .= $customer_address_line."<br>";
		}		
		$htmlShipping = $customer_address;
	}
	
	$pdf->htmlShipping = $htmlShipping;		
	
}

//$pdf->lastPage();
// ---------------------------------------------------------

//Close and output PDF document
//$output_path = "/var/www/html/amberconcept/document/po/";
//Close and output PDF document
//$pdf->Output($output_path.'PO'.$order->get_order_number().'.pdf', 'F');

$pdf->Output($poObj->po_number.'.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+