<?php
/*
 * @version			$Id: index.php 10381 2008-06-01 03:35:53Z $
 * @package			Carteasy
 * @copyright		Copyright (C) 2005 - 2011 Wlanium
 * @license			proprietary
 * @author			Thomas Bartelt
 * Carteasy is a web shop system
 */

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

class DeliveryNotePDF extends TCPDF {

	private $_image_path;

	// fill color for header
	private $_fill_r = 102;
	private $_fill_g = 102;
	private $_fill_b = 102;

	// table border color
	private $_border_r = 0;
	private $_border_g = 0;
	private $_boerder_b = 0;

	// stripe color for items list
	private $_stripe_r = 249;
	private $_stripe_g = 249;
	private $_stripe_b = 249;

	// normal text color
	private $_text_r = 0;
	private $_text_g = 0;
	private $_text_b = 0;

	// light text color
	private $_light_text_r = 128;
	private $_light_text_g = 128;
	private $_light_text_b = 128;

	private $_line_width = 0.2;


	// new page
	private $new_page_lines = 260;

	// header image
	private $header_image_x = 5;
	private $header_image_y = 5;

	// footer image
	private $footer_image_x = 5;
	private $footer_image_y = 260;

	// invoice data
	private $invoice_data_y = 53;

	public function __construct() {
		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);

		if (Config::has_key('bill_new_page_lines')) {
			$this->new_page_lines = Config::get_value('bill_new_page_lines');
		}
		if (Config::has_key('bill_invoice_data_y')) {
			$this->invoice_data_y = Config::get_value('bill_invoice_data_y');
		}
		if (Config::has_key('bill_header_image_x')) {
			$this->header_image_x = Config::get_value('bill_header_image_x');
		}
		if (Config::has_key('bill_header_image_y')) {
			$this->header_image_y = Config::get_value('bill_header_image_y');
		}
		if (Config::has_key('bill_footer_image_x')) {
			$this->footer_image_x = Config::get_value('bill_footer_image_x');
		}
		if (Config::has_key('bill_footer_image_y')) {
			$this->footer_image_y = Config::get_value('bill_footer_image_y');
		}
	}

	private function CheckForNewPage($lines = false) {
		if ($lines && ($this->GetY() + $lines * 3.52778) > $this->new_page_lines) {
			$this->addPage('', '', true, true);
		} else if ($this->GetY() > $this->new_page_lines) {
			$this->addPage('', '', true, true);
		}
	}

	public function Header() {
		if (Config::has_key('bill_header_image')) {
			$this->Image($this->_image_path.(string)Config::get_value('bill_header_image'), $this->header_image_x, $this->header_image_y, 200, 0);
		}

		if (Config::has_key('bill_header_text_window')) {
			$this->SetXY(7, 50);
			$this->SetFont('', '', 6);
			$this->Cell(50, 0, Config::get_value('bill_header_text_window'), 0, 0, 'L', 0);
		}

		if (Config::has_key('bill_header_text_address')) {
			$address = Config::get_value('bill_header_text_address');
			if (Config::has_key('bill_header_alignment_address')) {
				$alignment = $this->_getAlignment(Config::get_value('bill_header_alignment_address'));
			} else {
				$alignment = 'L';
			}

			$this->SetXY(125, 50);
			$this->SetFont('', '', 7);
			$this->MultiCell(70, 20, $address, 0, $alignment);
		}

		$this->setTopMargin(60 + 5);
	}

	public function Footer() {
		$this->SetFont('', '', 8);

		if (Config::has_key('bill_footer_image')) {
			$this->Image($this->_image_path.(string)Config::get_value('bill_footer_image'), $this->footer_image_x, $this->footer_image_y, 200, 0);
		}

		$footer_position_y = -25;
		if (Config::has_key('bill_footer_text_margin_bottom')) {
			$footer_position_y = (int)Config::get_value('bill_footer_text_margin_bottom');
		}

		// left
		if (Config::has_key('bill_footer_text_left')) {
			$footer_left_text = (string)Config::get_value('bill_footer_text_left');
			if ($footer_left_text != '') {
				if (Config::has_key('bill_footer_alignment_left')) {
					$alignment = $this->_getAlignment(Config::get_value('bill_footer_alignment_left'));
				} else {
					$alignment = 'L';
				}
				$this->SetXY(5, $footer_position_y);
				$this->MultiCell(65, 20, $footer_left_text, 0, $alignment);
			}
		}

		// center
		if (Config::has_key('bill_footer_text_center')) {
			$footer_center_text = (string)Config::get_value('bill_footer_text_center');
			if ($footer_center_text != '') {
				if (Config::has_key('bill_footer_alignment_center')) {
					$alignment = $this->_getAlignment(Config::get_value('bill_footer_alignment_center'));
				} else {
					$alignment = 'L';
				}
				$this->SetXY(70, $footer_position_y);
				$this->MultiCell(70, 20, $footer_center_text, 0, $alignment);
			}
		}

		// right
		if (Config::has_key('bill_footer_text_right')) {
			$footer_right_text = (string)Config::get_value('bill_footer_text_right');
			if ($footer_right_text != '') {
				if (Config::has_key('bill_footer_alignment_right')) {
					$alignment = $this->_getAlignment(Config::get_value('bill_footer_alignment_right'));
				} else {
					$alignment = 'R';
				}
				$this->SetXY(140, $footer_position_y);
				$this->MultiCell(65, 20, $footer_right_text, 0, $alignment);
			}
		}
	}

	public function PrintCustomerAddress($data) {
		$this->SetX(5);
		$this->SetY(53);
		$this->SetFont('', '', 10);

		$this->MultiCell(80,35, $data, 0, 'L');
	}

	public function PrintInvoiceData($data) {
		$this->SetFont('', '', 10);
		$key_string = '';
		$value_string = '';
		foreach ($data as $key => $value) {
			if ($key_string == '') {
				$key_string = $key;
				$value_string = $value;
			} else {
				$key_string .= "\n".$key;
				$value_string .= "\n".$value;
			}
		}

		$this->SetXY(125, $this->invoice_data_y);
		$this->MultiCell(40, 35, $key_string, 0, 'L');

		$this->SetXY(165, $this->invoice_data_y);
		$this->MultiCell(40, 35, $value_string, 0, 'R');
	}

	public function PrintInvoiceOrder($data) {
		$this->SetFont('', 'B', 18);
		$this->Cell(120, 0, "Lieferschein", '', 0, 'L');
		$this->Ln();

		$this->SetX(5);
		$this->SetY($this->tMargin + 40);
		$this->SetFont('', '', 8);

		// conf header
		$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');

		$header_width = array(15, 10, 100, 20, 30, 25);
		$length = count($header_width);

		// draw header
		$this->Cell($header_width[0], 0, 'Pos.', 'LTB', 0, 'C', 1); // Pos.
		$this->Cell($header_width[1], 0, 'St.', 'TB', 0, 'C', 1); // St.
		$this->Cell($header_width[2], 0, 'Beschreibung/Art-Nr.', 'TB', 0, 'L', 1); // Art-Nr./Beschreibung
		$this->Cell($header_width[3], 0, '', 'TB', 0, 'R', 1); // 
		$this->Cell($header_width[4], 0, '', 'TB', 0, 'R', 1); // 
		$this->Cell($header_width[5], 0, '', 'RTB', 0, 'R', 1); // mwst
		$this->Ln();

		// conf data
		$this->SetFillColor($this->_stripe_r, $this->_stripe_g, $this->_stripe_b);
		$this->SetTextColor(0);
		$this->SetFont('');
		$fill = 0;

		// draw data
		$i = 1;
		foreach ($data->items as $row) {
			$this->CheckForNewPage();

			// first row
			$this->SetFont('', '', 8);
			$this->SetTextColor($this->_text_r, $this->_text_g, $this->_text_b);
			$this->Cell($header_width[0], 0, $i, 'L', 0, 'C', $fill); // Pos.
			$this->Cell($header_width[1], 0, $row->amount, 0, 0, 'C', $fill); // St.
			$this->Cell($header_width[2], 0, $this->_stringCleaner($row->item_name), 0, 0, 'L', $fill); // Beschreibung
			$this->Cell($header_width[3], 0, '', 0, 0, 'R', $fill); // 
			$this->Cell($header_width[4], 0, '', 0, 0, 'R', $fill); // 
			$this->Cell($header_width[5], 0, '', 'R', 0, 'R', $fill); // mwst
			$this->Ln();

			//$this->SetTextColor($this->_light_text_r, $this->_light_text_g, $this->_light_text_b);
			$this->Cell($header_width[0], 0, '', 'L', 0, 'C', $fill);
			$this->Cell($header_width[1], 0, '', 0, 0, 'R', $fill);
			$this->Cell($header_width[2], 0, 'Art-Nr.: '.$row->item_number, 0, 0, 'L', $fill); // Art-Nr.
			$this->Cell($header_width[3], 0, '', 0, 0, 'R', $fill);
			$this->Cell($header_width[4], 0, '', 0, 0, 'R', $fill);
			$this->Cell($header_width[5], 0, '', 'R', 0, 'R', $fill);
			$this->Ln();
			if ($row->variant1) {
				$this->Cell($header_width[0], 0, '', 'L', 0, 'C', $fill);
				$this->Cell($header_width[1], 0, '', 0, 0, 'R', $fill);
				if ($row->vn1) {
					$txt=$row->vn1.': ';
				} else {
					$txt='Attr: ';
				}
				$this->Cell($header_width[2], 0,$txt.$row->variant1, 0, 0, 'L', $fill); // Varieant1
				$this->Cell($header_width[3], 0, '', 0, 0, 'R', $fill);
				$this->Cell($header_width[4], 0, '', 0, 0, 'R', $fill);
				$this->Cell($header_width[5], 0, '', 'R', 0, 'R', $fill);
				$this->Ln();
			}
			if ($row->variant2) {
				$this->Cell($header_width[0], 0, '', 'L', 0, 'C', $fill);
				$this->Cell($header_width[1], 0, '', 0, 0, 'R', $fill);
				if ($row->vn2) {
					$txt=$row->vn2.': ';
				} else {
					$txt='Attr: ';
				}
				$this->Cell($header_width[2], 0,$txt.$row->variant2, 0, 0, 'L', $fill); // Varieant1
				$this->Cell($header_width[3], 0, '', 0, 0, 'R', $fill);
				$this->Cell($header_width[4], 0, '', 0, 0, 'R', $fill);
				$this->Cell($header_width[5], 0, '', 'R', 0, 'R', $fill);
				$this->Ln();
			}
			// last row
			if ($row->item_status) {
				$this->SetFont('', '', 8);
				//$this->SetTextColor($this->_text_r, $this->_text_g, $this->_text_b);
				$this->Cell($header_width[0] + $header_width[1], 0, '', 'L', 0, 'C', $fill);

				$text = '';
				if ($row->item_status == 1) {
					// nicht lieferbar

					if ($row->item_status_text) {
						$text = $row->item_status_text;
					} else {
						$text = 'nicht lieferbar';
					}
				} else if ($row->item_status == 2) {
					// nachliefern

					if ($row->backorder_quantity) {
						$text .= $row->backorder_quantity ;
					}
					if ($text == '' && $row->item_status_text) {
						$text .= $row->item_status_text;
					} else if ($row->item_status_text) {
						$text .= ' '.$row->item_status_text;
					} else {
						$text .= ' nachliefern';
					}
					if ($row->delivery_date) {
						if ($text != '') {
							$date = explode(' ', $row->delivery_date);
							$text .= ' - '.$date[2].'.'.$date[1].'.'.$date[0];
						}
					}
				} else if ($row->item_status == 3) {
					// info

					if ($row->backorder_quantity) {
						$text .= $row->backorder_quantity ;
					}
					if ($text == '' && $row->item_status_text) {
						$text .= $row->item_status_text;
					} else if ($row->item_status_text) {
						$text .= ' '.$row->item_status_text;
					} else {
						$text .= ' info';
					}
					if ($row->delivery_date) {
						if ($text != '') {
							$date = explode(' ', $row->delivery_date);
							$text .= ' - '.$date[2].'.'.$date[1].'.'.$date[0];
						}
					}
				}

				$this->Cell($header_width[2] + $header_width[3] + $header_width[4], 0, $this->_stringCleaner($text), 0, 0, 'L', $fill);

				$this->Cell($header_width[5], 0, '', 'R', 0, 'L', $fill);
				$this->Ln();
			}
			$fill = !$fill;
			$i++;
		}

		$this->Cell(array_sum($header_width), 0, '', 'T');
		$this->Ln();
	}

	public function PrintNotice($title, $notice) {
		$this->CheckForNewPage(substr_count($notice, "\n") + 1);

		$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');

		$this->Cell(200, 1, $title, 1, 0, 'L', 1);

		$this->Ln();
		$this->SetTextColor(0);
		$this->SetFont('');

		$this->MultiCell(200, 1, $notice, 'LRB', 'L', 0);
		$this->Ln(4);
	}

	protected function _getAlignment($num) {
		if ($num == 0) {
			return 'L';
		} else if ($num == 1) {
			return 'C';
		} else if ($num == 2) {
			return 'R';
		}

		return '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);
	}
}