<?php


class money_expense {
	private $mox_id;
	private $mox_user_coach_ua_id;
	private $mox_coach = null; //user obj
	private $mox_date;
	private $mox_item;
	private $mox_sum;
	private $mox_moxc_id = null;
	private $mox_moxc = null;
	private $mox_payment_type;
	private $mox_deleted;


	public function set_mox_id($_item) {
		$this->mox_id = $_item;
	}

	public function set_mox_user_coach_ua_id($_item) {
		$this->mox_user_coach_ua_id = $_item;
	}

	public function set_mox_date($_item) {
		$this->mox_date = $_item;
	}

	public function set_mox_item($_item) {
		$this->mox_item = $_item;
	}

	public function set_mox_sum($_item) {
		$this->mox_sum = $_item;
	}

	public function set_mox_money_expense_category_moxc_id($_item) {
		$this->mox_moxc_id = $_item;
	}

	public function set_mox_moxc($_item) {
		$this->mox_moxc = $_item;
	}

	public function set_mox_coach($_item) {
		$this->mox_coach = $_item;
	}

	public function set_mox_payment_type_pt_id($_payment_type) {
		$this->mox_payment_type = $_payment_type;
	}

	public function set_mox_deleted($_item) {
		$this->mox_deleted = $_item;
	}

	public function get_mox_id() {
		return $this->mox_id;
	}

	public function get_mox_payment_type_pt_id() {
		return $this->mox_payment_type;
	}

	public function get_mox_user_coach_ua_id() {
		return $this->mox_user_coach_ua_id;
	}

	public function get_mox_date($_formatted = false) {
		return !$_formatted ? $this->mox_date : date("Y. F d. H:i", strtotime($this->mox_date));
	}

	public function get_mox_date_day() {
		$day = date("d", strtotime($this->mox_date));
		if (substr($day, 0, 1) == '0') return substr($day, 1, 1);
		return date("d", strtotime($this->mox_date));
	}

	public function get_mox_item() {
		return $this->mox_item;
	}

	public function get_mox_sum() {
		return $this->mox_sum;
	}

	public function get_mox_moxc() {
		return $this->mox_moxc;
	}

	public function get_mox_coach() {
		return $this->mox_coach;
	}

	public function get_mox_money_expense_category_moxc_id() {
		return $this->mox_moxc_id;
	}

	public function get_mox_deleted() {
		return $this->mox_deleted;
	}

	private static function name_to_ua_id($_ua_name) {
		global $sql;
		//a paraméterül kapott nevet megpróbálja coach ID-ra alakítani
		//ha sikerül, akkor visszaadja a coach ID-t
		//egyébként beszúr egy újat, törölt stáusszal, majd visszatér annak az id-jával
		if ($sql->num_of_rows("select ua_id from user_coach where ua_name = '".$_ua_name."' and ua_deleted = 0;")) {
			return $sql->single_variable("select ua_id from user_coach where ua_name = '".$_ua_name."' and ua_deleted = 0;");
		}
		elseif ($sql->num_of_rows("select ua_id from user_coach where ua_name = '".$_ua_name."' and ua_deleted = 1;")) {
			return $sql->single_variable("select ua_id from user_coach where ua_name = '".$_ua_name."' and ua_deleted = 1;");
		}
		else {
			return $sql->insert_into('user_coach', array('ua_name' => $_ua_name, 'ua_deleted' => 1));
		}

	}

	public function set_mox_data_by_id($_id) {
		global $sql;
		$set_data_assoc_array = $sql->assoc_array("select * from money_expense where mox_id = " . $_id);
		$set_data_array = $set_data_assoc_array[0];
		foreach ($set_data_array as $field => $value) {
			$function_name = "set_" . $field;
			$this->$function_name($value); //alapadatok beállítása

			if ($field == 'mox_money_expense_category_moxc_id' && $value != null) {
				$new_moxc = new money_expense_category();
				$new_moxc->set_moxc_data_by_id($value);
				$this->set_mox_moxc($new_moxc);
			}

			if ($field == 'mox_user_coach_ua_id' && $value != null) {
				$new_user = new user();
				$new_user->set_user_data_by_id($value);
				$this->set_mox_coach($new_user);
			}

			if ($field == "mox_payment_type") {
				$new_pt = new payment_type();
				$new_pt->set_pt_data_by_id($value);
				$this->set_mox_payment_type_pt_id($new_pt);
			}
		}

	}

	public static function create_money_expense($_name, $_date, $_item, $_sum, $_moxc_id, $_pt) {
		global $sql;

		return $sql->insert_into('money_expense', array(
				'mox_user_coach_ua_id' => (empty($_name)?'null':self::name_to_ua_id($_name)),
				'mox_date' => $_date,
				'mox_item' => $_item,
				'mox_sum' => $_sum,
				'mox_payment_type_pt_id' => $_pt,
				'mox_money_expense_category_moxc_id' => $_moxc_id
			));
	}

	public static function update_money_expense($_name, $_date, $_item, $_sum, $_moxc_id, $_id, $_pt) {
		global $sql;

		$sql->update_table('money_expense',
			array(
				'mox_user_coach_ua_id' => (empty($_name)?'null':self::name_to_ua_id($_name)),
				'mox_date' => $_date,
				'mox_item' => $_item,
				'mox_sum' => $_sum,
				'mox_payment_type_pt_id' => $_pt,
				'mox_money_expense_category_moxc_id' => $_moxc_id
			),
			array(
				'mox_id' => $_id
			)


		);
	}
}


?>
