<?php
class ControllerCatalogCostChangeByProduct extends Controller {
	
private $error = array();


public function index() {


$this->document->setTitle('Ürün Bazlı Fiyat Etkilemesi');

$this->getForm();
}



 
	public function add() {

 

  if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {


	$clean_spacial      = (isset($this->request->post['clean_spacial'])) ? $this->request->post['clean_spacial'] : null;;
	$product_list       = $this->request->post['product_list'];
	$normal_price       = $this->request->post['normal_price'];
	$percent_price      = $this->request->post['percent_price'];
	$normal_spacial     = $this->request->post['normal_spacial'];
	$percent_spacial    = $this->request->post['percent_spacial'];

	$list_of_price_and_spacial  = array();

	if (strpos($product_list, ',') !== false) {

		$products = explode(',',$product_list);

	}else{

		$products[0]                   = $product_list;

	}

	if($clean_spacial){

	foreach($products as $product){

		$this->db->query("DELETE FROM " . DB_PREFIX . "product_special WHERE product_id='".$product."'");

	}

	$list_of_price_and_spacial['clean'] = $products;

	$date     = date('Y-m-d');
			
	$this->db->query("
		
		INSERT IGNORE INTO " . DB_PREFIX . "product_price_change 
		
		SET 
		data              = '".json_encode($list_of_price_and_spacial)."',
		date              = '".$date."'
		
	");

	$product_price_change_id = $this->db->getLastId();

	$this->response->redirect($this->url->link('catalog/cost_change_by_product', 'token=' . $this->session->data['token'] .'&product_price_change_id='.$product_price_change_id.'  ', true));


	}else{

	$list_of_price_and_spacial['normal_price']          = $normal_price;
	$list_of_price_and_spacial['percent_price']         = $percent_price;
	$list_of_price_and_spacial['normal_spacial']        = $normal_spacial;
	$list_of_price_and_spacial['percent_spacial']       = $percent_spacial;
	$list_of_price_and_spacial['product_list']          = $products;

	
    foreach($products as $product){
		
		$get_product_query        = $this->db->query("SELECT p.product_id,p.price FROM ".DB_PREFIX."product p WHERE p.product_id = '".$product."' ");
		$get_product_data         = $get_product_query->row ;
		$product_price            = $get_product_data['price'] ;
		$product_product_id       = $get_product_data['product_id'] ;

		$current_price = 0;
 
		if( $normal_price ){

			if (  (strpos($normal_price, '+') !== false)  ||  (strpos($normal_price, '-') !== false)) {
				
					
				$current_price = $product_price + ($normal_price);
 
			}else{

				$current_price = $normal_price  ;
			}
 
		}


		if( $percent_price ){

	 
			if (  (strpos($normal_price, '+') !== false)  &&  (strpos($normal_price, '-') !== false)) {


			}else{

				$current_price = $product_price + ($product_price * abs($percent_price) / 100);

			}	
			 
					
			if (  (strpos($percent_price, '+') !== false)) {
				
					
				$current_price = $product_price + ($product_price * abs($percent_price) / 100);
 
			}

			if (  (strpos($percent_price, '-') !== false)) {
				
					
				$current_price = $product_price - ($product_price * abs($percent_price) / 100);
 
			}
			
			
		}

		if($current_price > 0){
			
		   $this->db->query(" UPDATE " . DB_PREFIX . "product  SET price = '".$current_price."' WHERE product_id = '".$product_product_id."' ");

	
		   $list_of_price_and_spacial['price'][] = array('product_id' => ''.$product_product_id.'' , 'new_price' => ''.$current_price  , 'old_price' => ''.$product_price.'' );
		   
		}	
 

		$current_spacial_price = 0;

		$customer_group_id = $this->config->get('config_customer_group_id');


		if( $normal_spacial ){
			
			$current_spacial_price = $normal_spacial;

		}
 
		if( $percent_spacial ){

			$current_spacial_price = $product_price - ($product_price * abs($percent_spacial) / 100);
		}

		if($current_spacial_price > 0 ){

			$timeonedayago     = date('Y-m-d', strtotime("-1 day"));
			$timeoneyearslater = date('Y-m-d', strtotime("+365 day"));

			$this->db->query("DELETE FROM " . DB_PREFIX . "product_special WHERE product_id='".$product_product_id."'");
			$this->db->query("
				
				INSERT IGNORE INTO " . DB_PREFIX . "product_special 
				
				SET 
				product_id        = '".$product_product_id."',
				customer_group_id = '".$customer_group_id."',
				priority          = 0 ,
				price             = '".$current_spacial_price."',
				date_start        = '".$timeonedayago."',
				date_end          = '".$timeoneyearslater."'
 
			");
			
			// indirimli eklenenler için log
			$list_of_price_and_spacial['special'][] = array('product_id' => ''.$product_product_id.'' , 'price' => ''.$current_spacial_price );
		}
 
		 
	}   
	
			$date     = date('Y-m-d');
			
			$this->db->query("
				
				INSERT IGNORE INTO " . DB_PREFIX . "product_price_change 
				
				SET 
				data              = '".json_encode($list_of_price_and_spacial)."',
				date              = '".$date."'
				
			");

			$product_price_change_id = $this->db->getLastId();
	
			$this->response->redirect($this->url->link('catalog/cost_change_by_product', 'token=' . $this->session->data['token'] .'&product_price_change_id='.$product_price_change_id.'  ', true));
	

		}






    }
	 
	$this->getForm();
	
    }



	
		
    public function getForm() {
 
		
		$data['heading_title'] = 'Ürün Bazlı Fiyat Etkilemesi';
		$data['text_list']     = 'Ürün Bazlı Değişim';
        $data['token']         = $this->session->data['token'];
		$data['header']        = $this->load->controller('common/header');
		$data['column_left']   = $this->load->controller('common/column_left');
		$data['footer']        = $this->load->controller('common/footer');

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}







		$data['product_price_change'] = '';

		if(isset($this->request->get['product_price_change_id'])){
			
			$product_price_change_id   = $this->request->get['product_price_change_id'];  
			$product_price_change_data = $this->db->query("SELECT * FROM ".DB_PREFIX."product_price_change  WHERE  id = '".$product_price_change_id."'");
			$product_price_change      = $product_price_change_data->row;
			$product_price_change      = json_decode($product_price_change['data'],true);
			
			$data['product_price_change'] = $product_price_change;
			
		}


        $data['action'] = $this->url->link('catalog/cost_change_by_product/add', 'token=' . $this->session->data['token'] . '', true);


	
        if(file_exists(DIR_LOCAL_TEMPLATE .'catalog/cost_change_by_product_form.tpl')){
            $this->response->setOutput($this->load->view( DIR_LOCAL_TEMPLATE .'catalog/cost_change_by_product_form', $data));
        }else{ 
            $this->response->setOutput($this->load->view(DIR_TEMPLATE . 'catalog/cost_change_by_product_form', $data));
        }
	
		
	}


	protected function validateForm() {
		
		
		if (!$this->user->hasPermission('modify', 'catalog/cost_change_by_product')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		$clean_spacial      = (isset($this->request->post['clean_spacial'])) ? $this->request->post['clean_spacial'] : null;
		$product_list       = $this->request->post['product_list'];
		$normal_price       = $this->request->post['normal_price'];
		$percent_price      = $this->request->post['percent_price'];
		$normal_spacial     = $this->request->post['normal_spacial'];
		$percent_spacial    = $this->request->post['percent_spacial'];
 

		if ( empty($product_list) ) {
			$this->error['warning'] = 'Lütfen! Ürün seçimi yapınız.';
		}

		if ( !$clean_spacial ) {
			 
			if (  utf8_strlen($normal_price == 0) &&  utf8_strlen($percent_price == 0) && utf8_strlen($normal_spacial == 0)  && utf8_strlen($percent_spacial == 0) ) {
				$this->error['warning'] = 'Lütfen! Boş Bırakmayınız.';
			}


		}

		if (  utf8_strlen($normal_price > 0) &&  utf8_strlen($percent_price > 0)   ) {
			$this->error['warning'] = 'Lütfen! Aynı anda hem normal fiyat hemde yüzde fiyat girmeyiniz.';
		}

		if (  utf8_strlen($normal_spacial > 0)  && utf8_strlen($percent_spacial > 0)   ) {
			$this->error['warning'] = 'Lütfen! Aynı anda hem indirimli normal değer  hemde indirimli yüzde değer girmeyiniz.';
		}

		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}
		
		return !$this->error;
	}


}
