コード例 #1
0
ファイル: order.printing.php プロジェクト: hungnv0789/vhtm
/**
 * Generate a single printable invoice.
 * @param int
 * @return strong The generated printable invoice (HTML)
 */
function generatePrintableInvoice($orderId)
{
	$db = $GLOBALS['ISC_CLASS_DB'];

	$template = TEMPLATE::GetInstance();
	$template->assign('StoreAddressFormatted', nl2br(getConfig('StoreAddress')));

	// allow access to deleted orders if printing from within control panel
	$isAdmin = (defined('ISC_ADMIN_CP') && ISC_ADMIN_CP);

	$query = "
		SELECT o.*, CONCAT(c.custconfirstname, ' ', c.custconlastname) AS ordcustname, c.custconemail AS ordcustemail, c.custconphone AS ordcustphone
		FROM [|PREFIX|]orders o
		LEFT JOIN [|PREFIX|]customers c ON o.ordcustid = c.customerid
		WHERE o.orderid = '".(int)$orderId."'
	";

	if (!$isAdmin) {
		$query .= " AND o.deleted = 0 ";
	}

	$result = $db->Query($query);
	$row = $db->Fetch($result);
	$order = $row;

	if(!$row) {
		return false;
	}

	$template->assign('OrderId', $row['orderid']);
	$template->assign('OrderDate', cDate($row['orddate']));

	if($row['ordcustmessage']) {
		$template->assign('Comments', nl2br(isc_html_escape($row['ordcustmessage'])));
		$template->assign('HideComments', '');
	}
	else {
		$template->assign('Comments', '');
		$template->assign('HideComments', 'display: none');
	}

	$template->assign('InvoiceTitle', sprintf(getLang('InvoiceTitle'), $orderId));

	$showShipping = true;
	$template->assign('totalRowColspan', 4);
	$template->assign('hideAddressColumn', 'display: none');

	if($row['shipping_address_count'] > 1) {
		$showShipping = false;
		$template->assign('totalRowColspan', 5);
		$template->assign('hideAddressColumn', '');
		$template->assign('hideInvoiceShippingDetails', 'display: none');
	}
	else if($row['ordisdigital']) {
		$template->assign('hideInvoiceShippingDetails', 'display: none');
		$showShipping = false;
	}

	$totalRows = getOrderTotalRows($row, $showShipping);
	$templateTotalRows = '';
	foreach($totalRows as $id => $totalRow) {
		$template->assign('label', $totalRow['label']);
		$template->assign('classNameAppend', ucfirst($id));
		$value = currencyConvertFormatPrice(
			$totalRow['value'],
			$row['ordcurrencyid'],
			$row['ordcurrencyexchangerate'],
			true
		);
		$template->assign('value', $value);
		$templateTotalRows .= $template->getSnippet('PrintableInvoiceTotalRow');
	}

	$template->assign('totals', $templateTotalRows);

	// Fetch shipping addresses in this order
	$addresses = array();
	$query = "
		SELECT *
		FROM [|PREFIX|]order_addresses
		WHERE order_id='".(int)$orderId."'
		ORDER BY `id`
	";
	$result = $db->query($query);
	while($address = $db->fetch($result)) {
		$addresses[$address['id']] = $address;
	}

	// Fetch shippng details
	$query = "
		SELECT *
		FROM [|PREFIX|]order_shipping
		WHERE order_id='".(int)$orderId."'
		ORDER BY order_address_id
	";
	$result = $db->query($query);
	while($shipping = $db->fetch($result)) {
		$addresses[$shipping['order_address_id']]['shipping'] = $shipping;
	}

	// Order has a single shipping address
	if($row['shipping_address_count'] == 1) {
		$address = current($addresses);
		$template->assign('ShippingAddress', getInvoiceShippingAddressBlock($address));
		$template->assign('ShippingEmail', isc_html_escape($address['email']));
		if(!$address['email']) {
			$template->assign('HideShippingEmail', 'display: none');
		}
		$template->assign('ShippingMethod', isc_html_escape($address['shipping']['method']));
	}

	// Format the customer details
	if($row['ordcustid'] == 0) {
		$template->assign('HideCustomerDetails', 'display: none');
	}
	$template->assign('CustomerId', $row['ordcustid']);
	$template->assign('CustomerName', isc_html_escape($row['ordcustname']));
	$template->assign('CustomerEmail', $row['ordcustemail']);
	$template->assign('CustomerPhone', $row['ordcustphone']);

	// Format the billing address
	$template->assign('ShipFullName', isc_html_escape($row['ordbillfirstname'].' '.$row['ordbilllastname']));

	if($row['ordbillcompany']) {
		$template->assign('ShipCompany', '<br />'.isc_html_escape($row['ordbillcompany']));
	}
	else {
		$template->assign('ShipCompany', '');
	}

	$addressLine = isc_html_escape($row['ordbillstreet1']);
	if ($row['ordbillstreet2'] != "") {
		$addressLine .=  '<br />' . isc_html_escape($row['ordbillstreet2']);
	}
	$template->assign('ShipAddressLines', $addressLine);

	$template->assign('ShipSuburb', isc_html_escape($row['ordbillsuburb']));
	$template->assign('ShipState', isc_html_escape($row['ordbillstate']));
	$template->assign('ShipZip', isc_html_escape($row['ordbillzip']));
	$template->assign('ShipCountry', isc_html_escape($row['ordbillcountry']));
	$template->assign('BillingAddress', $template->getSnippet('AddressLabel'));
	$template->assign('BillingPhone', isc_html_escape($row['ordbillphone']));
	if(!$row['ordbillphone']) {
		$template->assign('HideBillingPhone', 'display: none');
	}
	$template->assign('BillingEmail', isc_html_escape($row['ordbillemail']));
	if(!$row['ordbillemail']) {
		$template->assign('HideBillingEmail', 'display: none');
	}

	// Set the payment method
	$paymentMethod = $row['orderpaymentmethod'];
	if($row['orderpaymentmethod'] == '') {
		$paymentMethod = getLang('NA');
	}

	if($row['orderpaymentmethod'] != 'storecredit' && $row['orderpaymentmethod'] != 'giftcertificate') {
		$paymentMethod .= " (". formatPriceInCurrency($row['total_inc_tax'], $row['orddefaultcurrencyid']).")";
	}

	$template->assign('PaymentMethod', $paymentMethod);

	// Get the products in the order
	$fieldsArray = array();
	$query = "
		SELECT o.*
		FROM [|PREFIX|]order_configurable_fields o
		JOIN [|PREFIX|]product_configurable_fields p ON o.fieldid = p.productfieldid
		WHERE o.orderid=".(int)$orderId."
		ORDER BY p.fieldsortorder ASC
	";
	$result = $db->Query($query);
	$fields = array();
	while ($row = $db->Fetch($result)) {
		$fieldsArray[$row['ordprodid']][] = $row;
	}

	$query = "
		SELECT
			op.*,
			p.productid,
			p.prodpreorder,
			p.prodreleasedate,
			p.prodpreordermessage
		FROM
			[|PREFIX|]order_products op
			LEFT JOIN [|PREFIX|]products p ON p.productid = op.ordprodid
		WHERE
			op.orderorderid='".(int)$orderId."'
		ORDER BY op.order_address_id
	";
	$result = $db->query($query);

	$productsTable = '';
	$lastAddressId = -1;

	$shippingCostColumn = 'cost_ex_tax';
	$itemPriceColumn = 'price_ex_tax';
	$itemTotalColumn = 'total_ex_tax';

	if(getConfig('taxDefaultTaxDisplayOrders') == TAX_PRICES_DISPLAY_INCLUSIVE) {
		$shippingCostColumn = 'cost_inc_tax';
		$itemPriceColumn = 'price_inc_tax';
		$itemTotalColumn = 'total_inc_tax';
	}

	$addressProducts = array();
	while($product = $db->fetch($result)) {
		if(!isset($addressProducts[$product['order_address_id']])) {
			$addressProducts[$product['order_address_id']] = array();
		}

		$addressProducts[$product['order_address_id']][] = $product;
	}

	foreach($addressProducts as $addressId => $products) {
		$numProducts = count($products);
		if(!isset($addresses[$addressId])) {
			$template->assign('productShippingAddress', getLang('NA'));
		}
		else {
			$address = $addresses[$addressId];
			$template->assign('productShippingAddress', getInvoiceShippingAddressBlock($address));
		}
		$template->assign('addressColumnRowSpan', $numProducts);
		if($order['shipping_address_count'] > 1) {
			$template->assign('hideAddressColumn', '');
			$template->assign('invoiceItemClass', 'InvoiceItemDivider');
		}

		foreach($products as $product) {
			$template->assign('ProductName', isc_html_escape($product['ordprodname']));
			if($product['ordprodsku']) {
				$template->assign('ProductSku', isc_html_escape($product['ordprodsku']));
			}
			else {
				$template->assign('ProductSku', getLang('NA'));
			}
			$template->assign('ProductQuantity', $product['ordprodqty']);

			$pOptions = '';
			if($product['ordprodoptions'] != '') {
				$options = @unserialize($product['ordprodoptions']);
				if(!empty($options)) {
					foreach($options as $name => $value) {
						$template->assign('FieldName', isc_html_escape($name));
						$template->assign('FieldValue', isc_html_escape($value));
						$pOptions .= $template->GetSnippet('PrintableInvoiceItemConfigurableField');
					}
				}
			}

			if($pOptions) {
				$template->assign('ProductOptions', $pOptions);
				$template->assign('HideVariationOptions', '');
			}
			else {
				$template->assign('HideVariationOptions', 'display: none');
			}

			$productFields = '';
			if(!empty($fieldsArray[$product['orderprodid']])) {
				$fields = $fieldsArray[$product['orderprodid']];
				foreach($fields as $field) {
					if(empty($field['textcontents']) && empty($field['filename'])) {
						continue;
					}

					$fieldValue = '-';
					$template->assign('FieldName', isc_html_escape($field['fieldname']));

					if($field['fieldtype'] == 'file') {
						$fieldValue = '<a href="'.GetConfig('ShopPath').'/'.GetConfig('ImageDirectory').'/configured_products/'.urlencode($field['originalfilename']).'">'.isc_html_escape($field['originalfilename']).'</a>';
					}
					else {
						$fieldValue = isc_html_escape($field['textcontents']);
					}

					$template->assign('FieldValue', $fieldValue);
					$productFields .= $template->getSnippet('PrintableInvoiceItemConfigurableField');
				}
			}
			$template->assign('ProductConfigurableFields', $productFields);
			if(!$productFields) {
				$template->assign('HideConfigurableFields', 'display: none');
			}
			else {
				$template->assign('HideConfigurableFields', '');
			}

			$template->assign('ProductCost', currencyConvertFormatPrice(
				$product[$itemPriceColumn],
				$order['ordcurrencyid'],
				$order['ordcurrencyexchangerate'],
				true)
			);

			$template->assign('ProductTotalCost', currencyConvertFormatPrice(
				$product[$itemTotalColumn],
				$order['ordcurrencyid'],
				$order['ordcurrencyexchangerate'],
				true)
			);

			if($product['ordprodwrapname']) {
				$template->assign('FieldName', getLang('GiftWrapping'));
				$template->assign('FieldValue', isc_html_escape($product['ordprodwrapname']));
				$template->assign('ProductGiftWrapping', $template->getSnippet('PrintableInvoiceItemConfigurableField'));
				$template->assign('HideGiftWrapping', '');
			}
			else {
				$template->assign('ProductGiftWrapping', '');
				$template->assign('HideGiftWrapping', 'display: none');
			}

			if($product['ordprodeventdate']) {
				$template->assign('FieldName', isc_html_escape($product['ordprodeventname']));
				$template->assign('FieldValue', isc_date('dS M Y', $product['ordprodeventdate']));
				$template->assign('ProductEventDate', $template->getSnippet('PrintableInvoiceItemConfigurableField'));
				$template->assign('HideEventDate', '');
			}
			else {
				$template->assign('ProductEventDate', '');
				$template->assign('HideEventDate', 'display: none');
			}

			// determine preorder status
			$template->Assign('HidePreOrder', '');
			$template->Assign('ProductPreOrder', '');

			if ($product['productid'] && $product['prodpreorder']) {
				// product is pre-order because it exists in current db with preorder status
				if ($product['prodreleasedate']) {
					$message = $product['prodpreordermessage'];
					if (!$message) {
						$message = GetConfig('DefaultPreOrderMessage');
					}
					$message = str_replace('%%DATE%%', isc_date(GetConfig('DisplayDateFormat'), $product['prodreleasedate']), $message);
				} else {
					$message = GetLang('PreOrderProduct');
				}
				$template->Assign('ProductPreOrder', $message);
			} else {
				$template->Assign('HidePreOrder', 'display:none;');
			}

			$productsTable .= $template->GetSnippet('PrintableInvoiceItem');

			$template->assign('hideAddressColumn', 'display: none');
			$template->assign('productShippingAddress', '');
			$template->assign('addressColumnRowSpan', 1);
			$template->assign('invoiceItemClass', '');
		}
	}

	if($order['shipping_address_count'] > 1) {
		$template->assign('hideAddressColumn', '');
	}

	$template->assign('ProductsTable', $productsTable);

	return $template->GetSnippet('PrintableInvoice');
}