<?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
 */

class Payment_method extends Main {
	protected $base_object;

	public $list_table_config = array (
		'title' => 'Zahlungsarten',
		'db_table' => 'payment_methods',
		'list_fields' => array(
			array(
				'db_field' => 'name',
				'name' => 'Zahlungsart',
				'sortable' => 1
			),
			array(
				'db_field' => 'active',
				'name' => 'Status',
				'rewrite_function' => 'status_text',
				'sortable' => 1
			)
		),
		'search_fields' => array('name'),
		'db_id_field' => 'id',
		'edit_link' => 'index.php?admin_modul=admin_payment_method_editor&id=',
		'toolbar' => array(
			'delete' => 0,
			'new' => 0,
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		),
	);

	// private instance variables
	private $object_fields = array(
		'name' => 'text',
		'invoice_text' => 'text',
		'order_text' => 'text',
		'order_email_text' => 'text',
		'active' => 'integer'
	);



	function __construct($base_object) {
		$this->base_object = $base_object;
		$this->db = $base_object->db;
		$this->id = false;
	} // end consturctor

	public function status_text() {
		return array (
			'0' => 'inaktiv',
			'1' => 'aktiv'
		);
	}

	public function set_id($id) {
		$this->id = $id;
	} // end set_id


	public function get_all($active_only = false) {
		$sql = "SELECT * FROM payment_methods";

		if ($active_only) {
			$sql .= ' WHERE active=1';
		}

		$result = $this->db->query($sql);

		if ($result->num_rows) {
			$data = array();

			while ($obj = $result->fetch_object()) {
				$data[] = $obj;
			}

			return $data;
		}

		return false;
	} // end get_all

	public function get_all_names() {
		$sql = "SELECT * FROM payment_methods";

		$result = $this->db->query($sql);

		if ($result->num_rows) {
			$data = array();

			while ($obj = $result->fetch_object()) {
				$data[$obj->id] = $obj->name;
			}

			return $data;
		}

		return false;
	} // end get_all


	public function get_data() {
		if ($this->id) {
			$sql = "SELECT * FROM payment_methods WHERE id = ".$this->id;

			$result = $this->db->query($sql);

			if ($result->num_rows) {
				return $result->fetch_object();
			}

			return false;
		} else {
			throw Exception("Keine Zahlungsarten ID");
		}
	} // end get_data


	public function delete() {
		if ($this->id) {
			$sql = "DELETE FROM payment_methods WHERE id=".$this->id;

			return $this->db->query($sql);
		} else {
			throw Exception("Keine Zahlungsarten ID");
		}
	} // end delete


	public function get_by_id($id) {
		$sql = "SELECT * FROM payment_methods WHERE id=$id";

		$result = $this->db->query($sql);

		if ($result->num_rows) {
			return $result->fetch_object();
		}

		return false;
	} // end get_by_id


	public function get_module_by_id($id) {
		$sql = "SELECT module FROM payment_methods WHERE id=$id";

		$result = $this->db->query($sql);

		if ($result->num_rows) {
			return $result->fetch_object()->module;
		}

		return false;
	} // end get_name_by_id


	public function delete_by_id($id) {
		$sql = "DELETE FROM payment_methods WHERE id=$id";

		return $this->db->query($sql);
	} // end delete_by_id


	public function data_filter($request) {
		$data = array();

		foreach ($this->object_fields as $field_name => $var_type) {
			if (isset($request[$field_name])) {
				if ($var_type == 'text') {
					$data[$field_name] = $request[$field_name];
				} else {
					$data[$field_name] = (int)$request[$field_name];
				}
			}
		}

		return $data;
	} // end data_filter


	// TODO: clean, document
	public function create($data) {
		if ($data) {
			$sql = "INSERT INTO payment_methods SET ";
			foreach ($data as $var_name => $value) {
				$value = $this->db->real_escape_string($value);
				if ($this->object_fields[$var_name] == 'integer') {
					$sql .= $var_name.' = '.$value.', ';
				} else {
					$sql .= $var_name.' = "'.$value.'", ';
				}
			}
			$sql = substr($sql, 0, -2);
			$this->db->query($sql);
			$this->id = $this->db->insert_id;
		}

		return;
	} // end create


	// TODO: clean, document
	public function update($data) {
		if ($data) {
			$sql = "UPDATE payment_methods SET ";
			foreach ($data as $var_name => $value) {
				$value = $this->db->real_escape_string($value);
				if ($this->object_fields[$var_name] == 'integer') {
					$sql .= $var_name.'='.$value.', ';
				} else {
					$sql .= $var_name.'="'.$value.'", ';
				}
			}
			$sql = substr($sql, 0, -2);
			$sql .= ' WHERE id='.$this->id;
			$this->db->query($sql);
		}

		return;
	} // end update




	public function getMethodsForGroup($groupId, $price = -1, $shippingArea = false, $exclude = false) {
		if (!$shippingArea) {
			$shippingArea = 7;
		}

		$query = "SELECT sa.* FROM shipping_area sa WHERE sa.id = ".$shippingArea;

		$rs = $this->db->query($query);

		$areas = array();
		while ($PM = $rs->fetch_object()) { 
			$areas[$PM->id] = $PM;
		}

		$query = "SELECT pc.payment_method_id, pc.active FROM customer_group_payment_method_configuration pc
				WHERE pc.shipping_area_id = ".$shippingArea." AND pc.group_id = ".$groupId;
		$rs = $this->db->query($query);

		$pmConfig = array();
		while ($PM = $rs->fetch_object()) { 
			$pmConfig[$PM->payment_method_id] = $PM;
		}


		$query = "
			SELECT
				pm.id, pm.name, pm.module, pm.argument, pm.helperjoinkey, pm.order_text
			FROM
				payment_methods pm
			WHERE
				pm.active = 1
			ORDER BY
				pm.rang
		";

		if ($exclude) {
			$query = "
					SELECT
						pm.id, pm.name, pm.module, pm.argument, pm.helperjoinkey, pm.order_text
					FROM
						payment_methods pm
					WHERE
						pm.active = 1
					AND
						pm.id NOT IN (".$exclude.")
					ORDER BY
						pm.rang
				";
		}
		$rs = $this->db->query($query);

		$methods = array();
		while ($PM = $rs->fetch_object()) {
			$PM->activeType = $areas[$shippingArea]->{'active_'.$PM->helperjoinkey};
			$PM->rawAdd = $areas[$shippingArea]->{'add_'.$PM->helperjoinkey};
			$PM->paymentAdd = $areas[$shippingArea]->{'add_'.$PM->helperjoinkey};
			$PM->accountType = $areas[$shippingArea]->{'account_type_'.$PM->helperjoinkey};
			$PM->freePayment = $areas[$shippingArea]->{'free_payment_'.$PM->helperjoinkey};

			if ($PM->freePayment > 0 && $PM->freePayment < $price) {
				$PM->rawAdd = $areas[$shippingArea]->{'add_'.$PM->helperjoinkey} = 0;
				$PM->paymentAdd = $areas[$shippingArea]->{'add_'.$PM->helperjoinkey} = 0;
			}

			if ($PM->accountType == '0') {
				// FIXED MONETARY ADD
				$PM->paymentAddNet = $PM->paymentAdd / (100 + $this->base_object->config->shopConfiguration['default_vat_value']) * 100;
				$PM->paymentAddVat = $PM->paymentAdd - $PM->paymentAddNet;

				if ($this->base_object->customer_group->show_tax) {
					// show taxes
					$PM->paymentAddShown = $PM->paymentAddNet + $PM->paymentAddVat;
				} else {
					// no tax
					$PM->paymentAddShown = $PM->paymentAddNet;
				}
			} else if ($PM->accountType == '1') {
				// FIXED MONETARY SUB
				$PM->paymentAddNet = $PM->paymentAdd / (100 + $this->base_object->config->shopConfiguration['default_vat_value']) * 100;
				$PM->paymentAddVat = $PM->paymentAdd - $PM->paymentAddNet;

				if ($this->base_object->customer_group->show_tax) {
					// show taxes
					$PM->paymentAddShown = $PM->paymentAddNet + $PM->paymentAddVat;
				} else {
					// no tax
					$PM->paymentAddShown = $PM->paymentAddNet;
				}

				// make negative
				$PM->paymentAddNet = - $PM->paymentAddNet;
				$PM->paymentAddVat = - $PM->paymentAddVat;
				$PM->paymentAddShown = - $PM->paymentAddShown;
			} else if ($PM->accountType == '2') {
				// PERCENTUAL ADD
				$PM->paymentAdd = $PM->paymentAdd / 100 * $price;
				$PM->paymentAddNet = $PM->paymentAdd / (100 + $this->base_object->config->shopConfiguration['default_vat_value']) * 100;
				$PM->paymentAddVat = $PM->paymentAdd - $PM->paymentAddNet;

				if ($this->base_object->customer_group->show_tax) {
					// show taxes
					$PM->paymentAddShown = $PM->paymentAddNet + $PM->paymentAddVat;
				} else {
					// no tax
					$PM->paymentAddShown = $PM->paymentAddNet;
				}
			} else if ($PM->accountType == '3') {
				// PERCENTUAL SUB
				$PM->paymentAdd = $PM->paymentAdd / 100 * $price;
				$PM->paymentAddNet = $PM->paymentAdd / (100 + $this->base_object->config->shopConfiguration['default_vat_value']) * 100;
				$PM->paymentAddVat = $PM->paymentAdd - $PM->paymentAddNet;

				if ($this->base_object->customer_group->show_tax) {
					// show taxes
					$PM->paymentAddShown = $PM->paymentAddNet + $PM->paymentAddVat;
				} else {
					// no tax
					$PM->paymentAddShown = $PM->paymentAddNet;
				}

				// make negative
				$PM->paymentAdd = - $PM->paymentAdd;
				$PM->paymentAddNet = - $PM->paymentAddNet;
				$PM->paymentAddVat = - $PM->paymentAddVat;
				$PM->paymentAddShown = - $PM->paymentAddShown;
			}

			if ($PM->activeType != 2 || $pmConfig[$PM->id]->active) {
				$methods[$PM->id] = $PM;
			}
		}

		return $methods;
	}


	public function checkMethodForGroup($methodId, $groupId, $shippingArea = 7) {
		$methods = $this->getMethodsForGroup($groupId, shippingArea: $shippingArea);

		if (isset($methods[$methodId])) {
			return $methods[$methodId];
		}

		return false;
	}

	public function get_all_states() {

		$sql = "SELECT * FROM payment_status";

		$result = $this->db->query($sql);

		if ($result->num_rows > 0) {
			$data = array();
			while ($obj = $result->fetch_object()) {
				$data[$obj->id] = $obj->name;
			}

			return $data;
		}

		return false;
	}

}