<?php
/**
 * @package    VP Email Manager
 * @copyright  Copyright (C) 2012-2021 VirtuePlanet Services LLP. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 * @author     Abhishek Das <info@virtueplanet.com>
 * @link       https://www.virtueplanet.com
 */

defined('_JEXEC') or die;

extract($displayData);

VirtueMartModelCustomfields::$useAbsUrls = true;

$descColspan   = 3;
$pricesColspan = 3;

if (!$params->get('show_quantity', 1))
{
	$descColspan -= 1;
}

if ($params->get('show_prices', 1))
{
	if (!$params->get('show_tax', 1))
	{
		$pricesColspan -= 1;
	}

	if (!$params->get('show_discount', 1))
	{
		$pricesColspan -= 1;
	}
}

$model      = VmModel::getModel('product');
$menuItemID = shopFunctionsF::getMenuItemId($orderDetails->details['BT']->order_language);
?>
<table class="wrapper" width="100%" cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse; margin: 0 auto; font-family: Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22px;">
	<thead>
		<tr>
			<th align="left" bgcolor="#eeeeee" valign="top" style="background-color: #eeeeee; border: 1px solid #dddddd; padding: 5px 8px;" width="40%">
				<?php echo vmText::_('COM_VIRTUEMART_PRODUCT_NAME_TITLE'); ?>
			</th>
			<?php if ($params->get('show_prices', 1)) : ?>
				<th align="center" bgcolor="#eeeeee" valign="top" style="background-color: #eeeeee; border: 1px solid #dddddd; padding: 5px 8px;">
					<?php echo vmText::_('COM_VIRTUEMART_ORDER_PRINT_PRICE'); ?>
				</th>
			<?php endif; ?>
			<?php if ($params->get('show_quantity', 1)) : ?>
				<th align="center" bgcolor="#eeeeee" valign="top" style="background-color: #eeeeee; border: 1px solid #dddddd; padding: 5px 8px;" width="1%">
					<?php echo vmText::_('COM_VIRTUEMART_ORDER_PRINT_QTY'); ?>
				</th>
			<?php endif; ?>
			<?php if ($params->get('show_prices', 1)) : ?>
				<?php if ($params->get('show_tax', 1)) : ?>
					<th align="right" bgcolor="#eeeeee"valign="top"  style="background-color: #eeeeee; border: 1px solid #dddddd; padding: 5px 8px;">
						<?php if (is_array($taxBill) && count($taxBill) == 1)
						{
							reset($taxBill);
							$t = current($taxBill);
							
							echo shopFunctionsF::getTaxNameWithValue($t->calc_rule_name, $t->calc_value);
						}
						else
						{
							echo vmText::_('COM_VIRTUEMART_ORDER_PRINT_PRODUCT_TAX');
						} ?>
					</th>
				<?php endif; ?>
				<?php if ($params->get('show_discount', 1)) : ?>
					<th align="right" bgcolor="#eeeeee" valign="top" style="background-color: #eeeeee; border: 1px solid #dddddd; padding: 5px 8px;">
						<?php echo vmText::_('COM_VIRTUEMART_ORDER_PRINT_SUBTOTAL_DISCOUNT_AMOUNT'); ?>
					</th>
				<?php endif; ?>
				<th align="right" bgcolor="#eeeeee" valign="top" style="background-color: #eeeeee; border: 1px solid #dddddd; padding: 5px 8px;">
					<?php echo vmText::_('COM_VIRTUEMART_ORDER_PRINT_TOTAL'); ?>
				</th>
			<?php endif; ?>
		</tr>
	</thead>
	<tbody>
		<?php foreach ($orderDetails->items as $item) : ?>
			<?php
			$product_name = '<a style="color: inherit; text-decoration: none;" href="' . JUri::root() . 'index.php?option=com_virtuemart&view=productdetails&virtuemart_category_id=' . $item->virtuemart_category_id . '&virtuemart_product_id=' . $item->virtuemart_product_id . '&Itemid=' . $menuItemID . '">' . $item->order_item_name . '</a>';
			$product_attribute = VirtueMartModelCustomfields::CustomsFieldOrderDisplay($item, 'FE');
			$image = '';

			if ($params->get('show_product_images', 0))
			{
				$product = $model->getProduct($item->virtuemart_product_id, true, true, true, 1);
				
				$model->addImages($product);

				if (!empty($product->images[0]) && !empty($product->images[0]->virtuemart_media_id))
				{
					$image = '<img src="'. JUri::root() . $product->images[0]->file_url . '" style="max-width: 100%; height: auto; max-height: 150px; border: 0; outline: none;" alt="' . $item->order_item_name . '" />';
					
					if ($product->images[0] instanceof VmImage)
					{
						$image = $product->images[0]->displayMediaThumb(['style' => 'max-width: 100%; height: auto; max-height: 150px; border: 0; outline: none;'], false, '', true, false, true);
					}
					
					$image = '<a style="color: inherit; text-decoration: none; border: none;" href="' . JUri::root() . 'index.php?option=com_virtuemart&view=productdetails&virtuemart_category_id=' . $item->virtuemart_category_id . '&virtuemart_product_id=' . $item->virtuemart_product_id . '&Itemid=' . $menuItemID . '">' . $image . '</a>';
				}
			}
			?>
			<tr>
				<td align="left" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px;">
					<table width="100%" cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse; margin: 0 auto; font-family: Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22px;">
						<tbody>
							<tr>
								<?php if (!empty($image)) : ?>
									<td align="left" valign="top" style="width: 100px; padding-right: 5px; padding-bottom: 5px;">
										<?php echo $image; ?>
									</td>
								<?php endif; ?>
								<td align="left" valign="top">
									<div><strong><?php echo $product_name; ?></strong></div>
									<div><?php echo $product_attribute; ?></div>
									<?php if (!empty($item->order_item_sku) && $params->get('show_sku', 1)) : ?>
										<div><?php echo vmText::_('COM_VIRTUEMART_ORDER_PRINT_SKU'); ?>: <?php echo $item->order_item_sku; ?></div>
									<?php endif; ?>
									<?php if ($params->get('show_item_order_status', 1)) : ?>
										<div><?php echo vmText::_('COM_VIRTUEMART_ORDER_PRINT_PRODUCT_STATUS'); ?>: <?php echo $orderDetails->getOrderStatusName($item->order_status); ?></div>
									<?php endif; ?>
								</td>
							</tr>
						</tbody>
					</table>
				</td>
				<?php if ($params->get('show_prices', 1)) : ?>
					<td align="center" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
					<?php 
					$item->product_discountedPriceWithoutTax = (float) $item->product_discountedPriceWithoutTax;
					
					if (!empty($item->product_priceWithoutTax) && $item->product_discountedPriceWithoutTax != $item->product_priceWithoutTax)
					{
						echo '<div style="text-decoration: line-through;">' . $currency->priceDisplay($item->product_item_price, $user_currency_id) . '</div>';
						echo '<div>' . $currency->priceDisplay($item->product_discountedPriceWithoutTax, $user_currency_id) . '</div>';
					}
					else
					{
						echo '<div>' . $currency->priceDisplay($item->product_item_price, $user_currency_id) . '</div>';
					}
					?>
					</td>
				<?php endif; ?>
				<?php if ($params->get('show_quantity', 1)) : ?>
					<td align="center" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px;">
						<?php echo $item->product_quantity; ?>
					</td>
				<?php endif; ?>
				<?php if ($params->get('show_prices', 1)) : ?>
					<?php if ($params->get('show_tax', 1)) : ?>
						<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
							<?php echo $currency->priceDisplay($item->product_tax, $user_currency_id, $item->product_quantity); ?>
						</td>
					<?php endif; ?>
					<?php if ($params->get('show_discount', 1)) : ?>
						<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
							<?php echo $currency->priceDisplay($item->product_subtotal_discount, $user_currency_id); ?>
						</td>
					<?php endif; ?>
					<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
						<?php
						$item->product_basePriceWithTax = (float) $item->product_basePriceWithTax;
						
						if (!empty($item->product_basePriceWithTax) && $item->product_basePriceWithTax != $item->product_final_price)
						{
							echo '<div style="text-decoration: line-through;">' . $currency->priceDisplay($item->product_basePriceWithTax, $user_currency_id, $item->product_quantity) . '</div>' ;
						}
						elseif (empty($item->product_basePriceWithTax) && $item->product_item_price != $item->product_final_price)
						{
							echo '<div style="text-decoration: line-through;">' . $currency->priceDisplay($item->product_item_price, $user_currency_id, $item->product_quantity) . '</div>';
						}

						echo $currency->priceDisplay($item->product_subtotal_with_tax, $user_currency_id); ?>
					</td>
				<?php endif; ?>
			</tr>
		<?php endforeach; ?>
		
		<?php if ($params->get('show_prices', 1)) : ?>
			<!--Product subtotal-->
			<tr>
				<td align="right" valign="top" colspan="<?php echo $descColspan; ?>" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px;">
					<?php echo vmText::_('COM_VIRTUEMART_ORDER_PRINT_PRODUCT_PRICES_TOTAL'); ?>
				</td>
				<?php if ($params->get('show_tax', 1)) : ?>
					<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
						<?php echo $currency->priceDisplay($orderDetails->details['BT']->order_tax, $user_currency_id); ?>
					</td>
				<?php endif; ?>
				<?php if ($params->get('show_discount', 1)) : ?>
					<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
						<?php echo $currency->priceDisplay($orderDetails->details['BT']->order_discountAmount, $user_currency_id); ?>
					</td>
				<?php endif; ?>
				<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
					<?php echo $currency->priceDisplay($orderDetails->details['BT']->order_salesPrice, $user_currency_id); ?>
				</td>
			</tr>
			
			
			<?php if ($orderDetails->details['BT']->coupon_discount <> 0.00) : ?>
				<?php $coupon_code = $orderDetails->details['BT']->coupon_code ? ' (' . $orderDetails->details['BT']->coupon_code . ')' : ''; ?>
				<!--Coupon discount-->
				<tr>
					<td align="right" valign="top" colspan="<?php echo $descColspan; ?>" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px;">
						<?php echo vmText::_('COM_VIRTUEMART_COUPON_DISCOUNT') . $coupon_code; ?>
					</td>
					<?php if ($params->get('show_tax', 1)) : ?>
						<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
							&nbsp;
						</td>
					<?php endif; ?>
					<?php if ($params->get('show_discount', 1)) : ?>
						<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
							&nbsp;
						</td>
					<?php endif; ?>
					<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
						<?php echo $currency->priceDisplay($orderDetails->details['BT']->coupon_discount, $user_currency_id); ?>
					</td>
				</tr>
			<?php endif; ?>
			
			<?php if (!empty($discountsBill)) : ?>
				<?php foreach ($discountsBill as $rule) : ?>
					<!--Discount rules on Bill-->
					<tr>
						<td align="right" valign="top" colspan="<?php echo $descColspan; ?>" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px;">
							<?php echo $rule->calc_rule_name; ?>
						</td>
						<?php if ($params->get('show_tax', 1)) : ?>
							<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
								&nbsp;
							</td>
						<?php endif; ?>
						<?php if ($params->get('show_discount', 1)) : ?>
							<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
								<?php echo $currency->priceDisplay($rule->calc_amount, $user_currency_id); ?>
							</td>
						<?php endif; ?>
						<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
							<?php echo $currency->priceDisplay($rule->calc_amount, $user_currency_id); ?>
						</td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
			
		<?php endif; ?>
		
		<?php $totalShipmentPrice = ($orderDetails->details['BT']->order_shipment + $orderDetails->details['BT']->order_shipment_tax); ?>
		<?php $show_shipment_method = $params->get('show_shipment_method', 2); ?>
		<?php if ($show_shipment_method == 1 || ($show_shipment_method == 2 && !empty($totalShipmentPrice))) : ?>
			<!--Shipment method-->
			<tr>
				<td align="right" valign="top" colspan="<?php echo $descColspan; ?>" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px;">
					<?php echo $orderDetails->shipment_method; ?>
				</td>
				<?php if ($params->get('show_prices', 1)) : ?>
					<?php if ($params->get('show_tax', 1)) : ?>
						<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
							<?php echo $currency->priceDisplay($orderDetails->details['BT']->order_shipment_tax, $user_currency_id); ?>
						</td>
					<?php endif; ?>
					<?php if ($params->get('show_discount', 1)) : ?>
						<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
							&nbsp;
						</td>
					<?php endif; ?>
					<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
						<?php echo $currency->priceDisplay(($orderDetails->details['BT']->order_shipment + $orderDetails->details['BT']->order_shipment_tax), $user_currency_id); ?>
					</td>
				<?php endif; ?>
			</tr>
		<?php endif; ?>
		
		<?php $totalPaymentPrice = ($orderDetails->details['BT']->order_payment + $orderDetails->details['BT']->order_payment_tax); ?>
		<?php $show_payment_method = $params->get('show_payment_method', 2); ?>
		<?php if ($show_payment_method == 1 || ($show_payment_method == 2 && !empty($totalPaymentPrice))) : ?>
			<!--Payment method-->
			<tr>
				<td align="right" valign="top" colspan="<?php echo $descColspan; ?>" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px;">
					<?php echo $orderDetails->payment_method; ?>
				</td>
				<?php if ($params->get('show_prices', 1)) : ?>
					<?php if ($params->get('show_tax', 1)) : ?>
						<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
							<?php echo $currency->priceDisplay($orderDetails->details['BT']->order_payment_tax, $user_currency_id); ?>
						</td>
					<?php endif; ?>
					<?php if ($params->get('show_discount', 1)) : ?>
						<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
							&nbsp;
						</td>
					<?php endif; ?>
					<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
						<?php echo $currency->priceDisplay(($orderDetails->details['BT']->order_payment + $orderDetails->details['BT']->order_payment_tax), $user_currency_id); ?>
					</td>
				<?php endif; ?>
			</tr>
		<?php endif; ?>
		
		<?php if ($params->get('show_prices', 1)) : ?>
			<tr>
				<td align="right" valign="top" colspan="<?php echo $descColspan; ?>" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px;">
					<strong><?php echo vmText::_('COM_VIRTUEMART_ORDER_PRINT_TOTAL'); ?></strong>
				</td>
				<?php if ($params->get('show_tax', 1)) : ?>
					<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
						<?php echo $currency->priceDisplay($orderDetails->details['BT']->order_billTaxAmount, $user_currency_id); ?>
					</td>
				<?php endif; ?>
				<?php if ($params->get('show_discount', 1)) : ?>
					<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
						<?php echo $currency->priceDisplay($orderDetails->details['BT']->order_billDiscountAmount, $user_currency_id); ?>
					</td>
				<?php endif; ?>
				<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
					<strong><?php echo $currency->priceDisplay($orderDetails->details['BT']->order_total, $user_currency_id); ?></strong>
				</td>
			</tr>
		<?php endif; ?>
		
		<?php $payment_currency_id = !empty($orderDetails->isAdmin) ? $orderDetails->details['BT']->order_currency : $user_currency_id; ?>
		<?php if ($params->get('show_prices', 1) && !empty($orderDetails->details['BT']->payment_currency_rate) && $orderDetails->details['BT']->payment_currency_id != $payment_currency_id && $orderDetails->details['BT']->payment_currency_rate != 1.0) : ?>
			<tr>
				<td align="right" valign="top" colspan="<?php echo $descColspan; ?>" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px;">
					<strong><?php echo vmText::_('COM_VM_TOTAL_IN_PAYMENT_CURRENCY'); ?></strong>
				</td>
				<td align="right" valign="top" colspan="<?php echo $pricesColspan; ?>" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
					<?php if ($orderDetails->details['BT']->order_currency == $orderDetails->details['BT']->user_currency_id && $orderDetails->details['BT']->user_currency_id != $orderDetails->details['BT']->payment_currency_id)
					{
						echo $orderDetails->details['BT']->payment_currency_rate;
					}
					elseif ($orderDetails->details['BT']->order_currency == $orderDetails->details['BT']->payment_currency_id && $orderDetails->details['BT']->payment_currency_id != $orderDetails->details['BT']->user_currency_id)
					{
						echo $orderDetails->details['BT']->user_currency_rate;
					}
					echo '<strong>';
					echo $currencyP->priceDisplay($orderDetails->details['BT']->order_total, $orderDetails->details['BT']->payment_currency_id);
					echo '</strong>'; ?>
				</td>
			</tr>
		<?php endif; ?>
		
		<?php if (!empty($taxBill) && $params->get('show_prices', 1)) : ?>
			<tr>
				<td align="right" valign="top" colspan="<?php echo $descColspan; ?>" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px;">
					<strong><?php echo vmText::_('COM_VIRTUEMART_TOTAL_INCL_TAX'); ?></strong>
				</td>
				<?php if ($params->get('show_tax', 1)) : ?>
					<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
						&nbsp;
					</td>
				<?php endif; ?>
				<?php if ($params->get('show_discount', 1)) : ?>
					<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
						&nbsp;
					</td>
				<?php endif; ?>
				<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
					&nbsp;
				</td>
			</tr>
			
			<?php foreach ($taxBill as $rule) : ?>
				<?php if ($rule->calc_kind == 'taxRulesBill' || $rule->calc_kind == 'VatTax') : ?>
					<tr>
						<td align="right" valign="top" colspan="<?php echo $descColspan; ?>" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px;">
							<?php echo $rule->label; ?>
						</td>
						<?php if ($params->get('show_tax', 1)) : ?>
							<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
								<?php echo $currency->priceDisplay($rule->calc_result, $user_currency_id); ?>
							</td>
						<?php endif; ?>
						<?php if ($params->get('show_discount', 1)) : ?>
							<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
								&nbsp;
							</td>
						<?php endif; ?>
						<td align="right" valign="top" style="border: 1px solid #dddddd; padding: 5px 8px; line-height: 22px; font-size: 14px; white-space: nowrap;">
							&nbsp;
						</td>
					</tr>
				<?php endif; ?>
			<?php endforeach; ?>
		<?php endif; ?>

	</tbody>
</table>