<?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 './core/order.class.php';
include_once './core/orderhelper.class.php';
include_once './core/orderstatus.class.php';
include_once './core/delivererhelper.class.php';
include_once './core/cs_ticket.class.php';
include_once './core/customergroups.class.php';
include_once './core/item.class.php';
include_once './core/payment_method.class.php';
include_once './core/shippingareahelper.class.php';

class admin_order_actions {

	private $base_object;
	private $layout_object;
	private $order_object;

	function __construct($base_object, $layout_object) {
		$this->layout_object = $layout_object;
		$this->base_object = $base_object;
		$this->order_object = new Order($base_object);
	}

	function run() {
		if (isset($_GET['action'])) {
			$action = $_GET['action'];
		} else if (isset($_POST['action'])) {
			$action = $_POST['action'];
		} else {
			$action = false;
		}

		if (isset($_GET['id'])) {
			$id = $_GET['id'];
		} else if (isset($_POST['id'])) {
			$id = $_POST['id'];
		} else {
			$id = false;
		}

		if ($action == "set_payment_status") {
			$this->set_payment_status($id);
		} else if ($action == "set_payment_method") {
			$this->set_payment_method($id);
		} else if ($action == "set_order_status") {
			$this->set_order_status($id);
		} else if ($action == "set_shipping_method") {
			$this->set_shipping_method($id);
		} else if ($action == "set_order_statuses") {
			$this->set_order_statuses();
		} else if ($action == "set_payment_statuses") {
			$this->set_payment_statuses();
		} else if ($action == "get_tab") {
			$this->get_tab();
		} else if ($action == "get_deliverers_by_country") {
			$this->get_deliverers_by_country();
		} else if ($action == "get_order_item") {
			$this->get_order_item();
		} else if ($action == "get_order_invoice_pdf") {
			$this->get_order_invoice_pdf();
		} else if ($action == "get_order_delivery_note_pdf") {
			$this->get_order_delivery_note_pdf();
		} else if ($action == "get_payment_data_by_id_and_country") {
			$this->get_payment_data_by_id_and_country(); // deprecated can be removed
		} else if ($action == "get_payment_method_list") {
			$this->get_payment_method_list();
		} else if ($action == "setup_payment_methods") {
			$this->setup_payment_methods();
		} else if ($action == "send_invoice_pdf_by_email") {
			$this->send_invoice_pdf_by_email();
		} else if ($action == "get_bop_pdf") { // bill of parcels
			$this->get_bop_pdf(intval($_GET['order_id']));
		} else if ($action == "get_order_pdf") { // bill of parcels
			$this->get_order_pdf($_GET['order_id']);
		} else if ($action == "get_packing_slip_pdf") {
			$this->get_packing_slip_pdf(intval($_GET['order_id']));
		} else if ($action == "ping") {
			$this->ping();
		}
	}

	private function send_invoice_pdf_by_email() {
		if (isset($_GET['order_id'])) {
			$order_id = $_GET['order_id'];
		} else {
			$order_id = false;
		}

		if (isset($_GET['email'])) {
			$email = $_GET['email'];
		} else {
			$email = false;
		}

		if ($order_id && $email) {
			#include_once './core/cs_ticket.class.php';
			$ticket_object = new Cs_ticket($this->base_object);
			$order = $this->order_object->get_by_id($order_id);
			$invoice_pdf = OrderHelper::get_order_invoice_pdf($order_id, 1);
			$attachment1 = new stdClass();
			$attachment1->file_name = 'rechnung.pdf';
			$attachment1->mime_type = 'application/pdf';
			$attachment1->content = $invoice_pdf;
			$data['attachments'][] = $attachment1;
  
			$ticket_id = $ticket_object->event(11, $order->customer_id, $order_id, $data);
			if ($ticket_id) {
				$this->order_object->invoice_email_sent($order_id, $email);
				$return_data = array('status' => 'success', 'data' => 'Rechnung wurde versendet.');
			} else {
				$return_data = array('status' => 'error', 'message' => 'no result');
			}
		} else {
			$return_data = array('status' => 'error', 'message' => 'missing argument');
		}

		echo json_encode($return_data);
		exit();
	}

	private function set_payment_status($id) {
		if ($id) {
			if (isset($_GET['value'])) {
				$value = $_GET['value'];
			} else if (isset($_POST['value'])) {
				$value = $_POST['value'];
			} else {
				$value = false;
			}
			if ($value) {
				$result = OrderHelper::set_payment_status($id, $value);
				if ($result) {
					// success
					// send email event if order status is bezahlt and paymentmethod is Vorkasse
					$order_data = $this->order_object->get_by_id($id);
					if ($value == 2 && $order_data->payment_method == 1) {
						$ticket_object = new Cs_ticket($this->base_object);
						$ticket_object->event(9, $order_data->customer_id, $order_id);
					}
					$return = array('status' => 'success', 'message' => 'status set');
				} else {
					// cant save
					$return = array('status' => 'error', 'message' => 'can not save');
				}
			} else {
				// no active marker
				$return = array('status' => 'error', 'message' => 'no value');
			}
		} else {
			// no id
			$return = array('status' => 'error', 'message' => 'no id');
		}

		echo json_encode($return);
		exit();
	}

	private function set_payment_method($id) {
		if ($id) {
			if (isset($_GET['value'])) {
				$value = $_GET['value'];
			} else if (isset($_POST['value'])) {
				$value = $_POST['value'];
			} else {
				$value = false;
			}

			if ($value) {
				$result = OrderHelper::set_payment_method($id, $value);

				if ($result) {
					// success
					$return = array('status' => 'success', 'message' => 'method set');
				} else {
					// cant save
					$return = array('status' => 'error', 'message' => 'can not save');
				}
			} else {
				// no active marker
				$return = array('status' => 'error', 'message' => 'no value');
			}
		} else {
			// no id
			$return = array('status' => 'error', 'message' => 'no id');
		}

		echo json_encode($return);
		exit();
	}

	private function set_order_status($id) {
		if ($id) {
			if (isset($_GET['value'])) {
				$value = $_GET['value'];
			} else if (isset($_POST['value'])) {
				$value = $_POST['value'];
			} else {
				$value = false;
			}

			if ($value) {
				$result = OrderHelper::set_order_status($id, $value);

				if ($result) {
					// success
					// change order state event
					$this->startTicketEvent($id);
					$return = array('status' => 'success', 'message' => 'status set');
				} else {
					// cant save
					$return = array('status' => 'error', 'message' => 'can not save');
				}
			} else {
				// no active marker
				$return = array('status' => 'error', 'message' => 'no value');
			}
		} else {
			// no id
			$return = array('status' => 'error', 'message' => 'no id');
		}

		echo json_encode($return);
		exit();
	}

	private function startTicketEvent($id)
	{
		$order_data = $this->order_object->get_by_id($id);

		$ticket_object = new Cs_ticket($this->base_object);

		$ticket_object->event(4, $order_data->customer_id, $id);
		$event_type = 0;
		if ($order_data->order_status == 3) {
			$event_type = 5;
		} else if ($order_data->order_status == 4) {
			$event_type = 6;
		} else if ($order_data->order_status == 5) {
			$event_type = 7;
		} else if ($order_data->order_status == 6) {
			$event_type = 8;
		}

		if ($event_type) {
			$ticket_object->event($event_type, $order_data->customer_id, $id);
		}
		return;
	}

	private function set_shipping_method($id) {
		if ($id) {
			if (isset($_GET['value'])) {
				$value = $_GET['value'];
			} else if (isset($_POST['value'])) {
				$value = $_POST['value'];
			} else {
				$value = false;
			}

			if ($value) {
				$result = OrderHelper::set_shipping_method($id, $value);

				if ($result) {
					// success
					$return = array('status' => 'success', 'message' => 'method set');
				} else {
					// cant save
					$return = array('status' => 'error', 'message' => 'can not save');
				}
			} else {
				// no active marker
				$return = array('status' => 'error', 'message' => 'no value');
			}
		} else {
			// no id
			$return = array('status' => 'error', 'message' => 'no id');
		}

		echo json_encode($return);
		exit();
	}

	private function set_payment_statuses() {
		if (isset($_GET['seting'])) {
			$seting = $_GET['seting'];
		} else if (isset($_POST['seting'])) {
			$seting = $_POST['seting'];
		} else {
			$seting = false;
		}

		if (isset($_GET['ids'])) {
			$ids = $_GET['ids'];
		} else if (isset($_POST['ids'])) {
			$ids = $_POST['ids'];
		} else {
			$ids = false;
		}

		if ($seting && $ids) {
			$result = OrderHelper::set_payment_statuses($seting, $ids);

			if ($result) {
				// success
				$return = array('status' => 'success', 'message' => 'method set');
			} else {
				// cant save
				$return = array('status' => 'error', 'message' => 'can not save');
			}
		} else {
			// no active marker
			$return = array('status' => 'error', 'message' => 'no values');
		}

		echo json_encode($return);
		exit();
	}

	private function set_order_statuses() {
		if (isset($_GET['seting'])) {
			$seting = $_GET['seting'];
		} else if (isset($_POST['seting'])) {
			$seting = $_POST['seting'];
		} else {
			$seting = false;
		}

		if (isset($_GET['ids'])) {
			$ids = $_GET['ids'];
		} else if (isset($_POST['ids'])) {
			$ids = $_POST['ids'];
		} else {
			$ids = false;
		}

		if ($seting && $ids) {
			$result = OrderHelper::set_order_statuses($seting, $ids);
			$idArray=explode(',',$ids);
			if (sizeof($idArray) > 0) {
				foreach ($idArray As $id) {
					if (intval($id)) {
						$this->startTicketEvent($id);
					}
				}
			}
			if ($result) {
				// success
				$return = array('status' => 'success', 'message' => 'method set');
			} else {
				// cant save
				$return = array('status' => 'error', 'message' => 'can not save');
			}
		} else {
			// no active marker
			$return = array('status' => 'error', 'message' => 'no values');
		}

		echo json_encode($return);
		exit();
	}

	private function get_tab() {
		$order_status = $this->get_order_status();

		if ($order_status) {
			$items = $this->get_items();
			$page = $this->get_page();
			$pages = $this->order_object->get_number_of_pages($items, $order_status);
			$pagination = $this->order_object->get_pagination_array($items, $page, $order_status);
			$sort = $this->get_sort();
			$sort_direction = $this->get_sort_direction();
			$data = $this->order_object->get_all_paginated($items, $page, $order_status, $sort, $sort_direction);
			$order_selector = OrderStatus::get_selector($order_status);

			// modernize
			$order_status_list = $this->order_object->get_order_status_object();
			$this->layout_object->assign('order_status_list', $order_status_list);

			$billing_status_list = $this->order_object->get_billing_status_object();
			$this->layout_object->assign('billing_status_list', $billing_status_list);

			$payment_method_list = $this->order_object->get_payment_method_object();
			$this->layout_object->assign('payment_method_list', $payment_method_list);
			// end modernize

			$this->layout_object->assign('items', $items);
			$this->layout_object->assign('page', $page);
			$this->layout_object->assign('pages', $pages);
			$this->layout_object->assign('pagination', $pagination);
			$this->layout_object->assign('range', $range);
			$this->layout_object->assign('order_status', $order_status);
			$this->layout_object->assign('order_selector', $order_selector);
			if ($sort) {
				$this->layout_object->assign('sort', $sort);
			}
			if ($sort_direction) {
				$this->layout_object->assign('sort_direction', $sort_direction);
			}

			$this->layout_object->assign('order_list', $data);

			echo $this->layout_object->fetch('table_order_list.tpl');
		} else {
			// no range
			echo "ERROR: no order status";
		}

		exit();
	}

	private function get_deliverers_by_country() {
		if (isset($_GET['country_id'])) {
			$country_id = $_GET['country_id'];
		} else {
			$country_id = false;
		}

		if ($country_id) {
			$result = DelivererHelper::get_deliverers_by_country($country_id);

			if ($result) {
				$return = array('status' => 'success', 'data' => $result);
			} else {
				$return = array('status' => 'error', 'message' => 'no result');
			}
		} else {
			$return = array('status' => 'error', 'message' => 'no country id');
		}

		echo json_encode($return);
		exit();
	}

	private function get_order_status() {
		if (isset($_GET['order_status'])) {
			$order_status = $_GET['order_status'];
		} else if (isset($_POST['order_status'])) {
			$order_status = $_POST['order_status'];
		} else {
			$order_status = false;
		}

		return $order_status;
	}

	private function get_items() {
		if (isset($_GET['items']) && is_numeric($_GET['items'])) {
			$items = (int)$_GET['items'];
			$this->base_object->customer->set_config_item('pagination', $items);
		} else {
			$items = $this->base_object->customer->get_config_item('pagination');
			if (is_numeric($items)) {
				$items = (int)$items;
			} else {
				$items = 10;
			}
		}

		return $items;
	} // end get_items

	private function get_page() {
		if (isset($_GET['page']) && is_numeric($_GET['page'])) {
			$page = (int)$_GET['page'];
		} else {
			$page = 1;
		}

		return $page;
	} // end get_page

	private function get_sort() {
		if (isset($_GET['sort']) && $_GET['sort']) {
			$sort = $_GET['sort'];
		} else if (isset($_POST['sort']) && $_POST['sort']) {
			$sort = $_POST['sort'];
		} else {
			$sort = false;
		}

		return $sort;
	} // end get_sort

	private function get_sort_direction() {
		if (isset($_GET['sort_direction']) && $_GET['sort_direction']) {
			$sort_direction = $_GET['sort_direction'];
		} else if (isset($_POST['sort_direction']) && $_POST['sort_direction']) {
			$sort_direction = $_POST['sort_direction'];
		} else {
			$sort_direction = false;
		}

		return $sort_direction;
	}

	private function get_order_item() {
		if (isset($_GET['id'])) {
			$id = $_GET['id'];
		} else {
			$id = false;
		}

		if (isset($_GET['group_id'])) {
			$customer_group = $_GET['group_id'];
		} else {
			$customer_group = 1; // defaults to 
		}

		$customer_group_object = new CustomerGroups();
		$show_with_tax = $customer_group_object->get($customer_group)->show_tax;
		$this->layout_object->assign('show_with_tax', $show_with_tax);

		if ($id) {
			$item = new Item($this->base_object, $id);
			$data = $item->get_data($customer_group);

			$taxes = new Tax($this->base_object);
			$this->layout_object->assign('tax_rates', $taxes->get_all());

			$this->layout_object->assign('item', $data);

			if ($data->combination_item) {
				$this->layout_object->assign('stamp', uniqid());

				echo $this->layout_object->fetch('table_order_order_combination_list_item.tpl');
			} else {
				echo $this->layout_object->fetch('table_order_order_list_item.tpl');
			}
		}
		exit();
	}

	private function get_order_invoice_pdf() {
		if (isset($_GET['order_id'])) {
			$order_id = $_GET['order_id'];
		} else {
			$order_id = false;
		}

		if ($order_id) {
			OrderHelper::get_order_invoice_pdf($order_id);
		}

		exit();
	}

	private function get_order_delivery_note_pdf() {
		if (isset($_GET['order_id'])) {
			$order_id = $_GET['order_id'];
		} else {
			$order_id = false;
		}

		if ($order_id) {
			OrderHelper::get_order_delivery_note_pdf($order_id);
		}

		exit();
	}

	private function get_bop_pdf($order_id) {
		if (intval($order_id)) {
			include_once './core/output/bopPDF.class.php';

			$pdf = new bopPDF(intval($order_id));
		}

		exit();
	} // end get_bop_pdf

	private function get_order_pdf($order_id) {
		if ($order_id) {
			include_once './core/output/orderPDF.class.php';
			$pdf = new orderPDF($order_id);
		}

		exit();
	} // end get_bop_pdf

	private function get_packing_slip_pdf($order_id) {
		if (intval($order_id)) {
			include_once './core/output/packingslippdf.class.php';

			$pdf = new PackingSlipPDF(intval($order_id));
		}

		exit();
	} // end get_packing_slip_pdf

	private function ping() {
		$data = $_GET;

		unset($data['admin_modul']);
		unset($data['action']);

		foreach ($data as $key => $value) {
			echo $key.' : '.$value.'<br />';
		}

		exit();
	}

	// TODO: is a duplicate of setup_payment_method_chargesin admin payment method actions? should be get instead of setup?
	private function get_payment_data_by_id_and_country() {
		if (isset($_GET['method_id'])) {
			$method_id = $_GET['method_id'];
		} else {
			$method_id = false;
		}

		if (isset($_GET['country_id'])) {
			$country_id = $_GET['country_id'];
		} else {
			$country_id = false;
		}

		if (isset($_GET['order_items_value'])) {
			$order_items_value = $_GET['order_items_value'];
		} else {
			$order_items_value = false;
		}

		if ($method_id && $country_id && $order_items_value) {
			$default_vat_value = $this->base_object->config->shopConfiguration['default_vat_value'];
			$result = PaymentHelper::getPaymentDataByIdAndCountry($method_id, $country_id, $default_vat_value, $order_items_value);

			if ($result) {
				$return = array('status' => 'success', 'data' => $result, 'arguments' => array('method_id' => $method_id, 'country_id' => $country_id, 'order_items_value' => $order_items_value));
			} else {
				$return = array('status' => 'error', 'message' => 'no result');
			}
		} else {
			$return = array('status' => 'error', 'message' => 'missing argument');
		}

		echo json_encode($return);
		exit();
	}

	private function get_payment_method_list() {
		if (isset($_GET['countryId'])) {
			$countryId = $_GET['countryId'];
		} else {
			$countryId = false;
		}

		if (isset($_GET['groupId'])) {
			$groupId = $_GET['groupId'];
		} else {
			$groupId = false;
		}

		if (isset($_GET['itemsTotal'])) {
			$itemsTotal = $_GET['itemsTotal'];
		} else {
			$itemsTotal = false;
		}

		if ($countryId && $groupId && $itemsTotal) {
			$shippingArea = ShippingAreaHelper::get_shipping_area_by_country_id($countryId,$groupId);
			if ($shippingArea) {
				$payment_method_object = new Payment_method($this->base_object);
				$result = $payment_method_object->getMethodsForGroup($groupId, $itemsTotal, $shippingArea);

				if ($result) {
					$return = array('status' => 'success', 'data' => $result);
				} else {
					$return = array('status' => 'error', 'message' => 'no result');
				}
			} else {
				$return = array('status' => 'error', 'message' => "can't fetch shipping area id");
			}
		} else {
			$return = array('status' => 'error', 'message' => 'missing argument');
		}

		echo json_encode($return);
		exit();
	}

	public function get_order_talbe($id) {
		if ($id) {
			$order_data = $this->order_object->get_order_by_id($id);

			$header = $order['header'];
			$order_items = $order_data['items'];
			$footer = $order_data['footer'];

			$this->layout_object->assign('order', $header);
			$this->layout_object->assign('order_items', $order_items);
			$this->layout_object->assign('footer', $footer);

			return $this->layout_object->fetch('table_order_order_list.tpl');
		}
	}
}

?>