<?php
class ModelCatalogProductPrice extends Model {
	public function addProductPrices($product_id, $data) {
		if (isset($data['product_prices'])) {
			foreach ($data['product_prices'] as $store_id => $prices) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_prices SET product_id = '" . (int)$product_id . "', store_id = '" . (int)$store_id . "', price = '" . (float)$prices['price'] . "', price_2 = '" . (float)$prices['price_2'] . "', price_3 = '" . (float)$prices['price_3'] . "'");
			}
		}
	}

	public function editProductPrices($product_id, $data) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_prices WHERE product_id = '" . (int)$product_id . "'");

		if (isset($data['product_prices'])) {
			foreach ($data['product_prices'] as $store_id => $prices) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "product_prices SET product_id = '" . (int)$product_id . "', store_id = '" . (int)$store_id . "', price = '" . (float)$prices['price'] . "', price_2 = '" . (float)$prices['price_2'] . "', price_3 = '" . (float)$prices['price_3'] . "'");
			}
		}
	}

	public function getProductPrices($product_id) {
		$prices = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_prices WHERE product_id = '" . (int)$product_id . "'");

		foreach ($query->rows as $row) {
			$prices[$row['store_id']] = array(
				'price'   => $row['price'],
				'price_2' => $row['price_2'],
				'price_3' => $row['price_3']
			);
		}

		return $prices;
	}

	public function deleteProductPrices($product_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_prices WHERE product_id = '" . (int)$product_id . "'");
	}
}
