Files
bio-concept-labo/web/override/classes/pdf/HTMLTemplateInvoice.php
2019-11-20 07:44:43 +01:00

373 lines
20 KiB
PHP

<?php
class HTMLTemplateInvoice extends HTMLTemplateInvoiceCore
{
/*
* module: btpartialshipment
* date: 2019-06-27 08:59:21
* version: 1.1.8
*/
public function __construct(OrderInvoice $order_invoice, $smarty, $bulk_mode = false)
{
$orderObj = new Order((int)$order_invoice->id_order);
$brotherOrder = array();
if (Validate::isLoadedObject($orderObj)) {
$brothers = Db::getInstance()->executeS('SELECT `id_order` FROM `'._DB_PREFIX_.'orders` WHERE reference = "'.$orderObj->reference.'"');
if ($brothers) {
foreach ($brothers as $brother) {
$brotherOrder[] = $brother['id_order'];
}
$brotherOrder = implode(',', $brotherOrder);
}
}
if (count($brotherOrder)) {
$this->sql = ' AND id_order IN ('.$brotherOrder.')';
} else {
$this->sql = '';
}
$idParentOrderInvoice = Db::getInstance()->getValue('
SELECT MIN(id_order_invoice)
FROM `'._DB_PREFIX_.'order_invoice`
WHERE `number` = '.(int) $order_invoice->number.$this->sql);
$order_invoice = new OrderInvoice((int) $idParentOrderInvoice);
$this->order_invoice = $order_invoice;
$this->order = new Order((int)$this->order_invoice->id_order);
$this->smarty = $smarty;
if (version_compare(_PS_VERSION_, '1.6.1.1', '>=')) {
if (!isset($this->order_invoice->shop_address) || !$this->order_invoice->shop_address) {
$this->order_invoice->shop_address = OrderInvoice::getCurrentFormattedShopAddress((int)$this->order->id_shop);
if (!$bulk_mode) {
OrderInvoice::fixAllShopAddresses();
}
}
}
$this->date = Tools::displayDate($order_invoice->date_add);
$id_lang = Context::getContext()->language->id;
$this->title = $order_invoice->getInvoiceNumberFormatted($id_lang);
$this->shop = new Shop((int)$this->order->id_shop);
}
/*
* module: btpartialshipment
* date: 2019-06-27 08:59:21
* version: 1.1.8
*/
protected function computeLayout($params)
{
$layout = array(
'reference' => array(
'width' => 15,
),
'product' => array(
'width' => 40,
),
'quantity' => array(
'width' => 8,
),
'tax_code' => array(
'width' => 8,
),
'unit_price_tax_excl' => array(
'width' => 0,
),
'total_tax_excl' => array(
'width' => 0,
)
);
if (isset($params['has_discount']) && $params['has_discount']) {
$layout['before_discount'] = array('width' => 0);
$layout['product']['width'] -= 7;
$layout['reference']['width'] -= 3;
}
$total_width = 0;
$free_columns_count = 0;
foreach ($layout as $data) {
if ($data['width'] === 0) {
++$free_columns_count;
}
$total_width += $data['width'];
}
$delta = 100 - $total_width;
foreach ($layout as $row => $data) {
if ($data['width'] === 0) {
$layout[$row]['width'] = $delta / $free_columns_count;
}
}
$layout['_colCount'] = count($layout);
return $layout;
}
/*
* module: btpartialshipment
* date: 2019-06-27 08:59:21
* version: 1.1.8
*/
public function getContent()
{
if (version_compare(_PS_VERSION_, '1.6.1', '>=')) { //FOR PRESTASHOP 1.6.1.0 + 1.7.X
$invoiceAddressPatternRules = json_decode(Configuration::get('PS_INVCE_INVOICE_ADDR_RULES'), true);
$deliveryAddressPatternRules = json_decode(Configuration::get('PS_INVCE_DELIVERY_ADDR_RULES'), true);
$invoice_address = new Address((int)$this->order->id_address_invoice);
$country = new Country((int)$invoice_address->id_country);
$formatted_invoice_address = AddressFormat::generateAddress($invoice_address, $invoiceAddressPatternRules, '<br />', ' ');
$delivery_address = null;
$formatted_delivery_address = '';
if (isset($this->order->id_address_delivery) && $this->order->id_address_delivery) {
$delivery_address = new Address((int)$this->order->id_address_delivery);
$formatted_delivery_address = AddressFormat::generateAddress($delivery_address, $deliveryAddressPatternRules, '<br />', ' ');
}
$customer = new Customer((int)$this->order->id_customer);
$carrier = new Carrier((int)$this->order->id_carrier);
$order_details = $this->order_invoice->getProducts();
$ordersInvoice = Db::getInstance()->ExecuteS('SELECT `id_order_invoice` FROM `'._DB_PREFIX_.'order_invoice`
WHERE `number` = '.(int) $this->order_invoice->number.' AND `id_order_invoice` != '.(int) $this->order_invoice->id.$this->sql);
if ($ordersInvoice && count($ordersInvoice)) {
foreach ($ordersInvoice as $orderInvoice) {
$orderInvoiceObject = new OrderInvoice((int) $orderInvoice['id_order_invoice']);
if (Validate::isLoadedobject($orderInvoiceObject)) {
$products = $orderInvoiceObject->getProducts();
if ($products) {
foreach ($products as $product) {
$order_details[(int) $product['id_order_detail']] = $product;
}
}
}
}
}
$has_discount = false;
foreach ($order_details as $id => &$order_detail) {
if ($order_detail['reduction_amount_tax_excl'] > 0) {
$has_discount = true;
$order_detail['unit_price_tax_excl_before_specific_price'] = $order_detail['unit_price_tax_excl_including_ecotax'] + $order_detail['reduction_amount_tax_excl'];
} elseif ($order_detail['reduction_percent'] > 0) {
$has_discount = true;
$order_detail['unit_price_tax_excl_before_specific_price'] = (100 * $order_detail['unit_price_tax_excl_including_ecotax']) / (100 - $order_detail['reduction_percent']);
}
$taxes = OrderDetail::getTaxListStatic($id);
$tax_temp = array();
foreach ($taxes as $tax) {
$obj = new Tax($tax['id_tax']);
$tax_temp[] = sprintf($this->l('%1$s%2$s%%'), ($obj->rate + 0), '&nbsp;');
}
$order_detail['order_detail_tax'] = $taxes;
$order_detail['order_detail_tax_label'] = implode(', ', $tax_temp);
}
unset($tax_temp);
unset($order_detail);
if (Configuration::get('PS_PDF_IMG_INVOICE')) {
foreach ($order_details as &$order_detail) {
if ($order_detail['image'] != null) {
$name = 'product_mini_'.(int)$order_detail['product_id'].(isset($order_detail['product_attribute_id']) ? '_'.(int)$order_detail['product_attribute_id'] : '').'.jpg';
$path = _PS_PROD_IMG_DIR_.$order_detail['image']->getExistingImgPath().'.jpg';
$order_detail['image_tag'] = preg_replace(
'/\.*'.preg_quote(__PS_BASE_URI__, '/').'/',
_PS_ROOT_DIR_.DIRECTORY_SEPARATOR,
ImageManager::thumbnail($path, $name, 45, 'jpg', false),
1
);
if (file_exists(_PS_TMP_IMG_DIR_.$name)) {
$order_detail['image_size'] = getimagesize(_PS_TMP_IMG_DIR_.$name);
} else {
$order_detail['image_size'] = false;
}
}
}
unset($order_detail); // don't overwrite the last order_detail later
}
$cart_rules = $this->order->getCartRules($this->order_invoice->id);
$free_shipping = false;
foreach ($cart_rules as $key => $cart_rule) {
if ($cart_rule['free_shipping']) {
$free_shipping = true;
$cart_rules[$key]['value_tax_excl'] -= $this->order_invoice->total_shipping_tax_excl;
$cart_rules[$key]['value'] -= $this->order_invoice->total_shipping_tax_incl;
if ($cart_rules[$key]['value'] == 0) {
unset($cart_rules[$key]);
}
}
}
$product_taxes = 0;
foreach ($this->order_invoice->getProductTaxesBreakdown($this->order) as $details) {
$product_taxes += $details['total_amount'];
}
$product_discounts_tax_excl = $this->order_invoice->total_discount_tax_excl;
$product_discounts_tax_incl = $this->order_invoice->total_discount_tax_incl;
if ($free_shipping) {
$product_discounts_tax_excl -= $this->order_invoice->total_shipping_tax_excl;
$product_discounts_tax_incl -= $this->order_invoice->total_shipping_tax_incl;
}
$products_after_discounts_tax_excl = $this->order_invoice->total_products - $product_discounts_tax_excl;
$products_after_discounts_tax_incl = $this->order_invoice->total_products_wt - $product_discounts_tax_incl;
$shipping_tax_excl = $free_shipping ? 0 : $this->order_invoice->total_shipping_tax_excl;
$shipping_tax_incl = $free_shipping ? 0 : $this->order_invoice->total_shipping_tax_incl;
$shipping_taxes = $shipping_tax_incl - $shipping_tax_excl;
$wrapping_taxes = $this->order_invoice->total_wrapping_tax_incl - $this->order_invoice->total_wrapping_tax_excl;
$total_taxes = $this->order_invoice->total_paid_tax_incl - $this->order_invoice->total_paid_tax_excl;
$footer = array(
'products_before_discounts_tax_excl' => $this->order_invoice->total_products,
'product_discounts_tax_excl' => $product_discounts_tax_excl,
'products_after_discounts_tax_excl' => $products_after_discounts_tax_excl,
'products_before_discounts_tax_incl' => $this->order_invoice->total_products_wt,
'product_discounts_tax_incl' => $product_discounts_tax_incl,
'products_after_discounts_tax_incl' => $products_after_discounts_tax_incl,
'product_taxes' => $product_taxes,
'shipping_tax_excl' => $shipping_tax_excl,
'shipping_taxes' => $shipping_taxes,
'shipping_tax_incl' => $shipping_tax_incl,
'wrapping_tax_excl' => $this->order_invoice->total_wrapping_tax_excl,
'wrapping_taxes' => $wrapping_taxes,
'wrapping_tax_incl' => $this->order_invoice->total_wrapping_tax_incl,
'ecotax_taxes' => $total_taxes - $product_taxes - $wrapping_taxes - $shipping_taxes,
'total_taxes' => $total_taxes,
'total_paid_tax_excl' => $this->order_invoice->total_paid_tax_excl,
'total_paid_tax_incl' => $this->order_invoice->total_paid_tax_incl
);
foreach ($footer as $key => $value) {
$footer[$key] = Tools::ps_round($value, _PS_PRICE_COMPUTE_PRECISION_, $this->order->round_mode);
}
$round_type = null;
switch ($this->order->round_type) {
case Order::ROUND_TOTAL:
$round_type = 'total';
break;
case Order::ROUND_LINE:
$round_type = 'line';
break;
case Order::ROUND_ITEM:
$round_type = 'item';
break;
default:
$round_type = 'line';
break;
}
$display_product_images = Configuration::get('PS_PDF_IMG_INVOICE');
$tax_excluded_display = Group::getPriceDisplayMethod($customer->id_default_group);
$layout = $this->computeLayout(array('has_discount' => $has_discount));
$legal_free_text = Hook::exec('displayInvoiceLegalFreeText', array('order' => $this->order));
if (!$legal_free_text) {
$legal_free_text = Configuration::get('PS_INVOICE_LEGAL_FREE_TEXT', (int)Context::getContext()->language->id, null, (int)$this->order->id_shop);
}
$data = array(
'order' => $this->order,
'order_invoice' => $this->order_invoice,
'order_details' => $order_details,
'carrier' => $carrier,
'cart_rules' => $cart_rules,
'delivery_address' => $formatted_delivery_address,
'invoice_address' => $formatted_invoice_address,
'addresses' => array('invoice' => $invoice_address, 'delivery' => $delivery_address),
'tax_excluded_display' => $tax_excluded_display,
'display_product_images' => $display_product_images,
'layout' => $layout,
'tax_tab' => $this->getTaxTabContent(),
'customer' => $customer,
'footer' => $footer,
'ps_price_compute_precision' => _PS_PRICE_COMPUTE_PRECISION_,
'round_type' => $round_type,
'legal_free_text' => $legal_free_text,
);
if (Tools::getValue('debug')) {
die(json_encode($data));
}
$this->smarty->assign($data);
$iso_code = Context::getContext()->language->iso_code;
$sql = 'SELECT * FROM `' . _DB_PREFIX_ . 'installmentpayment` WHERE id_cart=' . (int) $this->order->id_cart;
$installmentpayment = Db::getInstance()->getRow($sql);
if (isset($installmentpayment['rest']) && $installmentpayment['rest'] > 0) {
$data = array(
'paid' => $installmentpayment['payer'],
'rest' => $installmentpayment['rest'],
'acompte' => Tools::getIsset('installmentpayment') ? 1 : 0
);
$this->smarty->assign($data);
}
/*$tpls = array(
'style_tab' => $this->smarty->fetch($this->getTemplate('invoice.style-tab')),
'addresses_tab' => $this->smarty->fetch($this->getTemplate('invoice.addresses-tab')),
'summary_tab' => $this->smarty->fetch($this->getTemplate('invoice.summary-tab')),
'product_tab' => $this->smarty->fetch($this->getTemplate('invoice.product-tab')),
'tax_tab' => $this->getTaxTabContent(),
'payment_tab' => $this->smarty->fetch($this->getTemplate('invoice.payment-tab')),
'note_tab' => $this->smarty->fetch($this->getTemplate('invoice.note-tab')),
'total_tab' => $this->smarty->fetch($this->getTemplate('invoice.total-tab')),
'shipping_tab' => $this->smarty->fetch($this->getTemplate('invoice.shipping-tab')),
);*/
if (Tools::getIsset('installmentpayment')) {
$tpls = array(
'style_tab' => $this->smarty->fetch($this->getTemplate('invoice.style-tab')),
'addresses_tab' => $this->smarty->fetch($this->getTemplate('invoice.addresses-tab')),
'summary_tab' => $this->smarty->fetch(_PS_MODULE_DIR_ . 'installmentpayment/views/templates/hook/pdf/invoice.empty.tpl'),
'tax_tab' => $this->smarty->fetch(_PS_MODULE_DIR_ . 'installmentpayment/views/templates/hook/pdf/invoice.tax-tab2_' . $iso_code . '.tpl'),
'product_tab' => $this->smarty->fetch(_PS_MODULE_DIR_ . 'installmentpayment/views/templates/hook/pdf/invoice.empty2.tpl'),
'payment_tab' => $this->smarty->fetch(_PS_MODULE_DIR_ . 'installmentpayment/views/templates/hook/pdf/invoice.empty2.tpl'),
'total_tab' => $this->smarty->fetch(_PS_MODULE_DIR_ . 'installmentpayment/views/templates/hook/pdf/invoice.total-tab2_' . $iso_code . '.tpl')
);
} else {
$tpls = array(
'style_tab' => $this->smarty->fetch($this->getTemplate('invoice.style-tab')),
'addresses_tab' => $this->smarty->fetch($this->getTemplate('invoice.addresses-tab')),
'summary_tab' => $this->smarty->fetch($this->getTemplate('invoice.summary-tab')),
'product_tab' => $this->smarty->fetch($this->getTemplate('invoice.product-tab')),
'tax_tab' => $this->getTaxTabContent(),
'payment_tab' => $this->smarty->fetch($this->getTemplate('invoice.payment-tab')),
'total_tab' => $this->smarty->fetch(_PS_MODULE_DIR_ . 'installmentpayment/views/templates/hook/invoice.total-tab_' . $iso_code . '.tpl')
);
}
$this->smarty->assign($tpls);
return $this->smarty->fetch($this->getTemplateByCountry($country->iso_code));
} else { //FOR PRESTASHOP 1.5 AND < 1.6.1.0
$country = new Country((int)$this->order->id_address_invoice);
$invoice_address = new Address((int)$this->order->id_address_invoice);
$formatted_invoice_address = AddressFormat::generateAddress($invoice_address, array(), '<br />', ' ');
$formatted_delivery_address = '';
if ($this->order->id_address_delivery != $this->order->id_address_invoice) {
$delivery_address = new Address((int)$this->order->id_address_delivery);
$formatted_delivery_address = AddressFormat::generateAddress($delivery_address, array(), '<br />', ' ');
}
$customer = new Customer((int)$this->order->id_customer);
$order_details = $this->order_invoice->getProducts();
$ordersInvoice = Db::getInstance()->ExecuteS('SELECT `id_order_invoice` FROM `'._DB_PREFIX_.'order_invoice`
WHERE `number` = '.(int) $this->order_invoice->number.' AND `id_order_invoice` != '.(int) $this->order_invoice->id.$this->sql);
if ($ordersInvoice && count($ordersInvoice)) {
foreach ($ordersInvoice as $orderInvoice) {
$orderInvoiceObject = new OrderInvoice((int) $orderInvoice['id_order_invoice']);
if (Validate::isLoadedobject($orderInvoiceObject)) {
$products = $orderInvoiceObject->getProducts();
if ($products) {
foreach ($products as $product) {
$order_details[(int) $product['id_order_detail']] = $product;
}
}
}
}
}
$this->smarty->assign(array(
'order' => $this->order,
'order_details' => $order_details,
'cart_rules' => $this->order->getCartRules($this->order_invoice->id),
'delivery_address' => $formatted_delivery_address,
'invoice_address' => $formatted_invoice_address,
'tax_excluded_display' => Group::getPriceDisplayMethod($customer->id_default_group),
'tax_tab' => $this->getTaxTabContent(),
'customer' => $customer
));
return $this->smarty->fetch($this->getTemplateByCountry($country->iso_code));
}
}
}