<?php
// packing slip
include_once './core/order.class.php';
include_once './core/customergroups.class.php';

include_once './libs/tcpdf/tcpdf.php';
include_once './core/config.class.php';


class PackingSlipPDF extends TCPDF {

	private $order_id = NULL;
	public  $data = array();

	private $_image_path = NULL;

	private $_fill_r = 102;
	private $_fill_g = 102;
	private $_fill_b = 102;

	private $_border_r = 0;
	private $_border_g = 0;
	private $_border_b = 0;

	private $_stripe_r = 249;
	private $_stripe_g = 249;
	private $_stripe_b = 249;

	private $_text_r = 0;
	private $_text_g = 0;
	private $_text_b = 0;

	private $_light_text_r = 128;
	private $_light_text_g = 128;
	private $_light_text_b = 128;

	private $_line_width = 0.2;

	private $fontfamily = 'Arial';

	public function __construct($order_id) {
		$this->order_id = $order_id;

		$db = Registry::get('base')->db;
		$order = new Order(Registry::get('base'));
		$this->data = $order->get_order_by_id($order_id);

		$this->writePackingSlipPDF();
	}

	private function writePackingSlipPDF() {
		$this->pdfSettings();

		$this->writeOrderData();
		$this->writeCustomerData();
		$this->writePaymentsData();
		$this->writeBillingData();
		$this->writeBilingAddress();
		$this->writeShippingAddress();
		$this->writeItemData();
		$this->writebopUser();

		$this->getPdf();
	}

	private function pdfSettings() {
		parent::__construct(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
		$this->_image_path = ROOT_DIR.'web/'.SHOP_SYSTEM.'/documents/';
		$this->SetMargins(6, 5, 5);
		$this->startPage();
	}

	public function Header() {}

	public function Footer() {}

	private function writeOrderData() {
		$order_dt = explode('-', $this->data['header']->order_date);

		$orderdata = 'Bst. Nr.: '.$this->data['header']->order_number.PHP_EOL;

		if ($this->data['header']->order_status_text) {
			$orderdata.= 'Bst. Status: '.$this->data['header']->order_status_text.PHP_EOL;
		}

		$orderdata.= 'Bst. Datum: '.$order_dt[2].'.'.$order_dt[1].'.'.$order_dt[0].PHP_EOL;

		$this->SetFont('', '',10);
		$this->MultiCell(40,15, $orderdata, 0, 'L',0,1,5,10);
	}

	private function writeCustomerData() {
		$customerdata = 'Kde. Nr.: '.$this->data['header']->customer_number.PHP_EOL;
		$customerdata.= 'Kde. Name: '.$this->data['header']->customer_name.PHP_EOL;

		if ($this->data['header']->customer_group_text) {
			$customerdata.= 'Kde. Gruppe: '.$this->data['header']->customer_group_text.PHP_EOL;
		}

		$this->SetFont('', '',10);
		$this->MultiCell(50,20, $customerdata, 0, 'L',0,1,50,10);
	}

	private function writePaymentsData() {
		$paymentdata='';

		if ($this->data['header']->payment_method_text) {
			$paymentdata .= 'Zahlungsart: '.$this->data['header']->payment_method_text.PHP_EOL;
		}

		if ($this->data['header']->payment_status_text) {
			$paymentdata .= 'Zahlungsstatus: '.$this->data['header']->payment_status_text.PHP_EOL;
		}

		if ($this->data['header']->payment_date) {
			$pay_dt = list($y,$m,$d)=explode('-', $this->data['header']->payment_date);
			$paymentdata.= 'Zahlungs Datum: '.$pay_dt[2].'.'.$pay_dt[1].'.'.$pay_dt[0].PHP_EOL;
		}
		$this->SetFont('', '',10);
		$this->MultiCell(50,35, $paymentdata, 0, 'L',0,1,105,10);
	}

	private function writeBillingData() {
		$billing_dt = explode('-', $this->data['header']->billing_date);

		$billingdata .= 'Rechnungs Datum: '.$billing_dt[2].'.'.$billing_dt[1].'.'.$billing_dt[0].PHP_EOL;
		if ($this->data['header']->invoice_number) {
			$billingdata .= 'Rechnungs Nr.: '.$this->data['header']->invoice_number.PHP_EOL;
		}

		if ($this->data['header']->shipping_method_tesxt) {
			$billingdata .= 'Lieferant.: '.$this->data['header']->shipping_method_tesxt.PHP_EOL;
		}
		if ($this->data['header']->trackingcode) {
			$billingdata.= 'Tracking-Code: '.$this->data['header']->trackingcode.PHP_EOL;
		}

		$this->SetFont('', '',10);
		$this->MultiCell(50,35, $billingdata, 0, 'L',0,1,155,10);
	}

	private function writeBilingAddress() {
		$bill_address = 'Rechnungsadresse:'.PHP_EOL;

		switch ($this->data['header']->billing_honorific) {
			case '0':   $bill_address .= "Herr".PHP_EOL; break;
			case '1':   $bill_address .="Frau".PHP_EOL; break;
			case '2':   $bill_address .= "Firma".PHP_EOL; break;
		}

		if ($this->data['header']->billing_company) {
			$bill_address .= $this->data['header']->billing_company.PHP_EOL;
		}

		if ($this->data['header']->billing_firstname || $this->data['order']->billing_surname) {
			if ($this->data['header']->billing_firstname) {
				$bill_address .= $this->data['header']->billing_firstname." ";
			}
			if ($this->data['header']->billing_surname) {
				$bill_address .= $this->data['header']->billing_surname;
			}
			$bill_address .= PHP_EOL;
		}

		$bill_address .= $this->data['header']->billing_street.' '.$this->data['header']->billing_house_number.PHP_EOL;
		$bill_address .= $this->data['header']->billing_zip_code.' '.$this->data['header']->billing_city.PHP_EOL;
		$bill_address .= $this->data['header']->billing_country_text.PHP_EOL;

		$this->SetFont('', '',10);
		$this->MultiCell(80,20, $bill_address, 0, 'L',0,1,5,32);
	}

	private function writeShippingAddress() {
		$ship_address = 'Lieferadresse:'.PHP_EOL;

		switch ($this->data['header']->shipping_honorific) {
			case '0':   $ship_address .= "Herr".PHP_EOL; break;
			case '1':   $ship_address .= "Frau".PHP_EOL; break;
			case '2':   $ship_address .= "Firma".PHP_EOL; break;
		}

		if ($this->data['header']->shipping_company) {
			$ship_address .= $this->data['header']->shipping_company.PHP_EOL;
		}

		if ($this->data['header']->shipping_firstname || $this->data['header']->shipping_surname) {
			if ($this->data['header']->shipping_firstname) {
				$ship_address.=$this->data['header']->shipping_firstname." ";
			}
			if ($this->data['header']->shipping_surname) {
				$ship_address.=$this->data['header']->shipping_surname;
			}
			$ship_address .= PHP_EOL;
		}

		$ship_address .= $this->data['header']->shipping_street.' '.$this->data['header']->shipping_house_number.PHP_EOL;
		$ship_address .= $this->data['header']->shipping_zip_code.' '.$this->data['header']->shipping_city.PHP_EOL;
		$ship_address .= $this->data['header']->shipping_country_text.PHP_EOL;


		$this->SetFont('', '',10);
		$this->MultiCell(80,20, $ship_address, 0, 'L',0,1,105,32);
	}

	private function writeItemData() {
		if (sizeof($this->data['items']) > 0) {

			$this->header_width = array(5, 5, 75, 10, 30, 25, 10, 40);
			$this->writeItemHeader();

			$i = 1;
			$fill = 0;
			foreach ($this->data['items'] as $row) {
				$this->SetFont('', '', 8);
				$this->SetTextColor($this->_text_r, $this->_text_g, $this->_text_b);
				$this->Cell($this->header_width[0], 0, $i, 'L', 0, 'C', $fill); // Pos.
				$this->Cell($this->header_width[1], 0, $row->amount, 0, 0, 'C', $fill); // St.
				$this->Cell($this->header_width[2] + $this->header_width[3] + $this->header_width[4] + $this->header_width[5], 0, $this->_stringCleaner($row->item_name), 0, 0, 'L', $fill); // Beschreibung
				$this->Cell($this->header_width[6], 0, '', 'L', 0, 'R', $fill); // Lagerist Anzahl
				$this->Cell($this->header_width[7], 0, '', 'LR', 0, 'R', $fill); // Lagerist Comment
				$this->Ln();

				$this->SetFont('', '', 2);
				$this->Cell($this->header_width[0]+$this->header_width[1]+$this->header_width[2]+$this->header_width[3]+$this->header_width[4]+$this->header_width[5], 0, '', 'LR', 0, 'C', $fill);
				$this->Cell($this->header_width[6], 0, '', 'LR', 0, 'C', $fill);
				$this->Cell($this->header_width[7], 0, '', 'R', 0, 'C', $fill);
				$this->Ln();
				$this->SetFont('', '', 8);

				$artikelnr ="Art-Nr.: ".$row->item_number;
				if ($row->variant1) {
					$artikelnr.=" / ";
					if ($row->vn1) {
						$artikelnr.=$row->vn1.': ';
					} else {
						$artikelnr.='Attr: ';
					}
					$artikelnr.=$row->variant1;
				}
				if ($row->variant2) {
					$artikelnr.=" / ";
					if ($row->vn2) {
						$artikelnr.=$row->vn2.': ';
					} else {
						$artikelnr.='Attr: ';
					}
					$artikelnr.=$row->variant2;
				}

				if ($row->item_status == 2) {
					$this->Cell($this->header_width[0], 0, '', 'L', 0, 'C', $fill);
					$this->Cell($this->header_width[1], 0, '', '', 0, 'R', $fill);
					$this->Cell($this->header_width[2] + $this->header_width[3] + $this->header_width[4] + $this->header_width[5], 0, $artikelnr, '', 0, 'L', $fill); // Art-Nr.
					$this->Cell($this->header_width[6], 0, '', 'L', 0, 'R', $fill); // Lagerist Anzahl
					$this->Cell($this->header_width[7], 0, '', 'LR', 0, 'R', $fill); // Lagerist Comment
					$this->Ln();

					// nachliefern
					$text = $row->amount.' bestellt '.$row->backorder_quantity.' auf nachlieferung';
					$width = 0;
					foreach ($this->header_width as $header_width) {
						$width += $header_width;
					}

					$width  -= ($this->header_width[0] + $this->header_width[1] + $this->header_width[6] + $this->header_width[7]);

					$this->Cell($this->header_width[0], 0, '', 'L', 0, 'C', $fill);
					$this->Cell($this->header_width[1], 0, '', '', 0, 'R', $fill);
					$this->Cell($this->header_width[2] + $this->header_width[3] + $this->header_width[4] + $this->header_width[5], 0, $text, 'R', 0, 'L', $fill);
					$this->Cell($this->header_width[6], 0, '', 'L', 0, 'R', $fill); // Lagerist Anzahl
					$this->Cell($this->header_width[7], 0, '', 'LR', 0, 'R', $fill); // Lagerist Comment
					$this->Ln();
				} else {
					$this->Cell($this->header_width[0], 0, '', 'L', 0, 'C', $fill);
					$this->Cell($this->header_width[1], 0, '', '', 0, 'R', $fill);
					$this->Cell($this->header_width[2] + $this->header_width[3] + $this->header_width[4] + $this->header_width[5], 0, $artikelnr, '', 0, 'L', $fill); // Art-Nr.
					$this->Cell($this->header_width[6], 0, '', 'L', 0, 'R', $fill); // Lagerist Anzahl
					$this->Cell($this->header_width[7], 0, '', 'LR', 0, 'R', $fill); // Lagerist Comment
					$this->Ln();
				}

				if (isset($row->children)) {
					foreach ($row->children as $child) {
						$childtext = $child->item_name;
						if ($child->variant1 != '') {
							$childtext .= ' '.$child->variant1;
						}
						if ($child->variant2 != '') {
							$childtext .= ' '.$child->variant2;
						}
						if ($child->user_data != '') {
							$childtext .= " '".$child->user_data."'";
						}
						$thildtext .= ' '.$child->item_number;

						$this->Cell($this->header_width[0], 0, '', 'L', 0, 'C', $fill);
						$this->Cell($this->header_width[1], 0, '', '', 0, 'R', $fill);
						$this->Cell($this->header_width[2] + $this->header_width[3] + $this->header_width[4] + $this->header_width[5], 0, $this->_stringCleaner($childtext), 0, 0, 'L', $fill);
						$this->Cell($this->header_width[6], 0, '', 'L', 0, 'R', $fill); // Lagerist Anzahl
						$this->Cell($this->header_width[7], 0, '', 'LR', 0, 'R', $fill); // Lagerist Comment
						$this->Ln();
					}
				}

				$this->Cell($this->header_width[0], 0, '', 'LB', 0, 'C', $fill);
				$this->Cell($this->header_width[1] + $this->header_width[2] + $this->header_width[3] + $this->header_width[4] + $this->header_width[5] + $this->header_width[6], 0, '', 'B', 0, 'C', $fill);
				$this->Cell($this->header_width[7], 0, '', 'R', 0, 'C', $fill);

				$i++;
			}
		}
	}

	private function writeItemHeader($header_width) {
		$this->SetY($this->gety()+5);
		$this->SetFont('', 'B', 18);
		$this->Cell(120, 0, "Packschein", '', 0, 'L');
		$this->Ln();

		$this->SetFont('', '', 8);
		$this->SetFillColor($this->_fill_r, $this->_fill_g, $this->_fill_b);
		$this->SetTextColor(255);
		$this->SetDrawColor($this->_border_r, $this->_border_g, $this->_border_b);
		$this->SetLineWidth($this->_line_width);
		$this->SetFont('', 'B');

		// draw header
		$this->Cell($this->header_width[0], 0, 'P', 'LTB', 0, 'C', 1); // Pos.
		$this->Cell($this->header_width[1], 0, 'St.', 'TB', 0, 'C', 1); // St.
		$this->Cell($this->header_width[2] + $this->header_width[3] + $this->header_width[4] + $this->header_width[5], 0, 'Beschreibung/Art-Nr.', 'TB', 0, 'L', 1); // Art-Nr./Beschreibung
		$this->Cell($this->header_width[6], 0, 'St.', 'LTB', 0, 'R', 1);
		$this->Cell($this->header_width[7], 0, 'Kommentar', 'LRTB', 0, 'R', 1); // comment
		$this->Ln();
	}

	private function writebopUser() {
		$this->SetFont('', '', 10);
		$this->SetY($this->gety()+10);
		$this->Cell(10, 0, '', 0, 0, 'C');
		$this->Cell(80, 0, 'Datum / gepackt von', 'T', 0, 'C');
		$this->Cell(15, 0, '', 0, 0, 'C');
		$this->Cell(80, 0, 'Datum / gepr�ft von', 'T', 0, 'C');
	}

	protected function _stringCleaner($sting) {
		$search = array('€', 'ß', 'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü');
		$replace = array($this->unichr(8364), $this->unichr(223), $this->unichr(228), $this->unichr(246), $this->unichr(252), $this->unichr(196), $this->unichr(214), $this->unichr(220));
		return str_replace($search, $replace, $sting);
	}

	private function getPdf() {
		$this->Output('Packschein-'.$this->data['order']->invoice_number.'.pdf', 'D');
	}

}
