<?php

class PaypalRefund {

	private $id;
	private $db;

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

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

	public function get_id() {
		return $this->id;
	}

	public function get_data($id = false) {
		$sql = "SELECT *
				FROM paypal_refund
				WHERE id=";

		if ($id) {
			$sql .= $this->db->real_escape_string($id);
		} else {
			if ($this->id) {
				$sql .= $this->db->real_escape_string($this->id);
			} else {
				return false;
			}
		}

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

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

		return false;
	}

	public function create($data) {
		$data_line = "";
		$value_line = "";

		$size = count($data);
		$i = 1;

		foreach ($data as $key => $value) {
			$data_line .= $key;
			$value_line .= "'".$value."'";

			if ($i < $size) {
				$data_line .= ", ";
				$value_line .= ", ";

				$i++;
			}
		}

		$sql = "INSERT INTO paypal_refund(".$data_line.") ";
		$sql .= "VALUES (".$value_line.")";

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

		if ($result) {
			return $this->db->insert_id;
		}

		return false;
	}

	public function update($transaction_data, $id = false) {
		$sql = "UPDATE paypal_refund ";

		$size = $count($data);
		$i = 1;

		foreach ($data as $key => $value) {
			$sql .= $key."='".$value."'";

			if ($i < $size) {
				$sql .= ", ";
			}
		}

		if ($id) {
			$sql .= " WHERE id=".$this->db->real_escape_string($id);
		} else {
			if ($this->id) {
				$sql .= " WHERE id=".$this->db->real_escape_string($this->id);
			} else {
				return false;
			}
		}

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

		if ($result) {
			if ($id) {
				return $id;
			}

			return $this->id;
		}

		return false;
	}
}