<?php
class ControllerMyocLivePriceUpdate extends Controller {
	public function index() {
		$this->language->load('myoc/live_price_update');

		$json = array();

		$cart_session = $this->session->data['cart'];

		$this->cart->clear();

		if (isset($this->request->post['product_id'])) {
			$product_id = $this->request->post['product_id'];
		} else {
			$product_id = 0;
		}

		if (isset($this->request->post['profile_id'])) {
			$profile_id = $this->request->post['profile_id'];
		} else {
			$profile_id = 0;
		}
		
		$this->load->model('catalog/product');
						
		$product_info = $this->model_catalog_product->getProduct($product_id);
		
		if ($product_info) {			
			if (isset($this->request->post['quantity'])) {
				$quantity = $this->request->post['quantity'];
			} else {
				$quantity = 1;
			}
														
			if (isset($this->request->post['option'])) {
				$option = array_filter($this->request->post['option']);
			} else {
				$option = array();
			}

			$this->cart->add($product_id, $quantity, $option, $profile_id);
		} else {
			$json['error'] = $this->language->get('error_product');
		}		

		if(!$json) {
			$json['price'] = $this->currency->format($product_info['special'] ? $this->tax->calculate($this->cart->getSubTotal() + ($product_info['price'] - $product_info['special']), $product_info['tax_class_id'], $this->config->get('config_tax')) : $this->cart->getTotal());
			$json['special'] = $this->currency->format($this->cart->getTotal());
			$json['extax'] = $this->currency->format($this->cart->getSubTotal());
		}

		$this->cart->clear();

		$this->session->data['cart'] = $cart_session;

		$this->response->setOutput(json_encode($json));
	}
}
?>