<?php
class ControllerReportProductReport2 extends Controller { 
	public function index() {   
		$this->load->language('report/product_report');

		$this->document->setTitle($this->language->get('heading_title'));
		
   		$this->data['breadcrumbs'] = array();
		$url = '';
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('report/product_report', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);		
		
		$this->load->model('report/product');
		
		$this->data['Excel'] = $this->url->link('report/product_report1', 'SSL');
		$this->data['products'] = array();
		
		$data = array(
			'filter_date_start'	     => '', 
			'filter_date_end'	     => '', 
			'filter_order_status_id' => '',
			'limit'                  => $this->config->get('config_admin_limit')
		);
				
		$product_total = $this->model_report_product->getTotalPurchased($data);
		$this->session->data['product_details'] = '';
//	$store_products = $this->model_report_product->getProducts();
//	foreach ($store_products as $store_product) {

//		$product_id = $store_product['product_id'];

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

		$products_dtl = $this->model_report_product->getProductReports($product_id); 
	if($products_dtl[0]['type']){		// Non  Customizable Products
			$products_dtl=$products_dtl[0];
			$query = $this->db->query("delete from product_combinations where product_id='" . $product_id . "'"); 

			$products_gemcontent = $this->model_report_product->getProductGemContent($product_id);
			$insertsql = "insert into product_combinations "; 
			$insertsql .= "(product_id,product_metalavail_id,product_gemavail_id,metal_id,gemstone_id,metal_default,gem_default,shape,weight_cost,total_metal_cost,total_gem_cost,product_cost,additional_cost,final_price,image) ";
			$insertsql .= "values (";
			$insertsql .= "'" . $products_dtl['product_id'] . "','" . $products_dtl['product_metalavail_id'] . "', '" . $products_dtl['product_gemavail_id'] . "',";
			$insertsql .= "'" . $products_dtl['metal_id'] . "','" . $products_dtl['gemstone_id'] . "', '" . $products_dtl['metaldefault'] . "', '" . $products_dtl['gemdefault'] . "', '" . $products_gemcontent[0]['shape_id'] . "',"; 
			$insertsql .= "'0','0','0',";
			$insertsql .= "'0','0','" . $products_dtl['final_price'] . "','" . $products_dtl['product_image'] . "'";
			$insertsql .= ")";
			$query = $this->db->query($insertsql); 
			
	}else{	
		$products_metalcontent = $this->model_report_product->getProductMetalContent($product_id);
		$products_gemcontent = $this->model_report_product->getProductGemContent($product_id);
		$products_metalavail = $this->model_report_product->getProductMetalDtl($product_id);
		$products_gemavail = $this->model_report_product->getProductGemDtl($product_id);
		
		/*
		$shapes_dtl = $this->model_report_product->getShapes();
		foreach($shapes_dtl as $shape1){
			$shapes = array(
					$shape1['shape'] => $shape1['shape_id']
				);
		}
		*/
		
		$products_all_metalavail = $this->model_report_product->updateProductMetalDtl($product_id);
		$products_all_gemavail = $this->model_report_product->updateProductGemDtl($product_id);
		
		
//		$m2cost	=  (isset($products_metalavail[1])) ? $this->model_report_product->getProductMetalCost($products_metalavail[1]['product_metalavail_id']) : 0;
		if(isset($products_metalavail[1])){
			$metalcost2 =  split(':',$this->model_report_product->getProductMetalCost($products_metalavail[1]['product_metalavail_id']));
			$m2cost = (float)$metalcost2[0];
			$m2weight = $metalcost2[1];
			$m2rate = $metalcost2[2];
		}else{
			$m2cost = 0;
			$m2weight = 0;
			$m2rate = 0;
		}
		
//		$m3cost	=  (isset($products_metalavail[2])) ? $this->model_report_product->getProductMetalCost($products_metalavail[2]['product_metalavail_id']) : 0;
		if(isset($products_metalavail[2])){
			$metalcost3 =  split(':',$this->model_report_product->getProductMetalCost($products_metalavail[2]['product_metalavail_id']));
			$m3cost = (float)$metalcost3[0];
			$m3weight = $metalcost3[1];
			$m3rate = $metalcost3[2];
		}else{
			$m3cost = 0;
			$m3weight = 0;
			$m3rate = 0;
		}		


//		$g2cost =  (isset($products_gemavail[2])) ? $this->model_report_product->getProductGemstoneCost($products_gemavail[1]['product_gemavail_id']) : 0;
		if(isset($products_gemavail[1])){
			$gemcost2 =  split(':',$this->model_report_product->getProductGemstoneCost($products_gemavail[1]['product_gemavail_id']));
			$g2cost = (float)$gemcost2[0];
			$g2weight = $gemcost2[1];
			$g2size = $gemcost2[2];
			$g2rate = $gemcost2[3];
			$g2shape = $this->model_report_product->getShapes($gemcost2[4]);
		}else{
			$g2cost = 0;
			$g2weight = 0;
			$g2size = 0;
			$g2rate = 0;
			$g2shape = 0;

		}
		
//		$g4cost =  (isset($products_gemavail[3])) ? $this->model_report_product->getProductGemstoneCost($products_gemavail[2]['product_gemavail_id']) : 0;
		if(isset($products_gemavail[2])){
			$gemcost3 =  split(':',$this->model_report_product->getProductGemstoneCost($products_gemavail[2]['product_gemavail_id']));
			$g3cost = (float)$gemcost3[0];
			$g3weight = $gemcost3[1];
			$g3size = $gemcost3[2];
			$g3rate = $gemcost3[3];
			$g3shape = $this->model_report_product->getShapes($gemcost3[4]);
		}else{
			$g3cost = 0;
			$g3weight = 0;
			$g3size = 0;
			$g3rate = 0;
			$g3shape = 0;

		}

//		$g4cost =  (isset($products_gemavail[4])) ? $this->model_report_product->getProductGemstoneCost($products_gemavail[3]['product_gemavail_id']) : 0;
		if(isset($products_gemavail[3])){
			$gemcost4 =  split(':',$this->model_report_product->getProductGemstoneCost($products_gemavail[3]['product_gemavail_id']));
			$g4cost = (float)$gemcost4[0];
			$g4weight = $gemcost4[1];
			$g4size = $gemcost4[2];
			$g4rate = $gemcost4[3];
			$g4shape = $this->model_report_product->getShapes($gemcost4[4]);
		}else{
			$g4cost = 0;
			$g4weight = 0;
			$g4size = 0;
			$g4rate = 0;
			$g4shape = 0;
		}

	
//		$g5cost =  (isset($products_gemavail[1])) ? $this->model_report_product->getProductGemstoneCost($products_gemavail[4]['product_gemavail_id']) : 0;
		if(isset($products_gemavail[4])){
			$gemcost5 =  split(':',$this->model_report_product->getProductGemstoneCost($products_gemavail[4]['product_gemavail_id']));
			$g5cost = (float)$gemcost5[0];
			$g5weight = $gemcost5[1];
			$g5size = $gemcost5[2];
			$g5rate = $gemcost5[3];
			$g5shape = $this->model_report_product->getShapes($gemcost5[4]);
		}else{
			$g5cost = 0;
			$g5weight = 0;
			$g5size = 0;
			$g5rate = 0;
			$g5shape = '';
		}		
		
		
		
		$metals = $this->model_report_product->getProductMetalAvalability_Not1($product_id);
		foreach ($metals as $metal) {
			if( $metal['defaultnew'] )
			{
				$product_details[] = array(
					'type' => 'M',
					'id' => $metal['metal'],
					'value' => $metal['product_metalavail_id'],						
					'image' => $metal['image'],						
					'price' => $metal['price'],
					'name' => $metal['description']
				);
			}
		}
		$gemstones = $this->model_report_product->getProductGemstonesAvalability_Not1($product_id);
		foreach ($gemstones as $gemstone) {
			if( $gemstone['defaultnew'] )
			{
				$product_details[] = array(
					'type' => 'G',
					'id' => $gemstone['gem'],
					'value' => $gemstone['product_gemavail_id'],
					'image' => $gemstone['image'],
					'name' => $gemstone['gemname']
				);
			}
		}
		if(isset($product_details)){
			$product_details_bk = $product_details;
		}else{
			$product_details_bk = '';
		}
		$metal_count=0;
		$gem_count=0;
					
		$settingcost = $this->model_report_product->getProductSettingCost($product_id);

		
		$query = $this->db->query("delete from product_combinations where product_id='" . $product_id . "'"); 

		$updateSQL = "update product set setting_cost='". $settingcost ."' where product_id='". $product_id ."'";
		$query = $this->db->query($updateSQL);

		foreach ($products_dtl as $result) {
		
			$product_details = $product_details_bk;
			$product_details[] = array(
					'type' => 'M',
					'id' => 1,
					'value' => $result['product_metalavail_id'],
					'image' => $result['image'],
					'name' => $result['metalname']
				);
			$product_details[] = array(
					'type' => 'G',
					'id' => 1,
					'value' => $result['product_gemavail_id'],
					'image' => $result['image'],
					'name' => $result['gemname']
				);
				
			$weightcost = $this->model_report_product->getProductWeightCost($product_id,$product_details);
			$metalcost1 =  split(':',$this->model_report_product->getProductMetalCost($result['product_metalavail_id']));
			$m1cost = (float)$metalcost1[0];
			$m1weight = $metalcost1[1];
			$m1rate = $metalcost1[2];
			
//			$g4cost	=  $this->model_report_product->getProductGemstoneCost($result['product_gemavail_id']);
	
			if(isset($products_gemavail[0])){
				$gemcost1 =  split(':',$this->model_report_product->getProductGemstoneCost($result['product_gemavail_id']));
				$g1cost = (float)$gemcost1[0];
				$g1weight = $gemcost1[1];
				$g1size = $gemcost1[2];
				$g1rate = $gemcost1[3];
				$g1shape = $this->model_report_product->getShapes($gemcost1[4]);
			}else{
				$g1cost = 0;
				$g1weight = 0;
				$g1size = 0;
				$g1rate = 0;
				$g1shape = '';
			}
		
			$product_cost = $m1cost + $m2cost + $m3cost + $g1cost + $g2cost + $g3cost + $g4cost + $g5cost + $settingcost + $weightcost;
			$product_cost_total = $product_cost;
		 
			if( (int)$this->config->get('pricelimit_four') <= (int)$product_cost  ){
				$additional = (int)$this->config->get('additionalcost_five') ; 
			}else if( (int)$this->config->get('pricelimit_three') <= $product_cost ){
				$additional = (int)$this->config->get('additionalcost_four') ; 
			}else if( (int)$this->config->get('pricelimit_two') <= $product_cost ){
				$additional = (int)$this->config->get('additionalcost_three') ; 
			}else if( (int)$this->config->get('pricelimit_one') <= $product_cost ){
				$additional = (int)$this->config->get('additionalcost_two') ; 
			}else{ 
				$additional = (int)$this->config->get('additionalcost_one') ; 
			} 
		
			$product_cost_addl = $additional ;
			$product_cost_markup = $this->model_report_product->getProductMarkupCost( $product_id , $product_cost + $additional );
			$product_cost_markup=  (ceil($product_cost_markup/5)) * 5  ;

			
			$this->data['products'][] = array(
				'name'       => $result['name'],
				'model'      => $result['model'],
				
				'metal1'       	=> $result['metalname'],
				'metal2'       	=> (isset($products_metalavail[1])) ? $products_metalavail[1]['metalname'] : "" ,
				'metal3'       	=> (isset($products_metalavail[2])) ? $products_metalavail[2]['metalname'] : "",
				'gem1'       	=> $result['gemname'],
				'gem2'       	=> (isset($products_gemavail[1])) ? $products_gemavail[1]['gemname'] : "",
				'gem3'       	=> (isset($products_gemavail[2])) ? $products_gemavail[2]['gemname'] : "",
				'gem4'       	=> (isset($products_gemavail[3])) ? $products_gemavail[3]['gemname'] : "",
				'gem5'       	=> (isset($products_gemavail[4])) ? $products_gemavail[4]['gemname'] : "",
				
				'currency'		=>  $this->config->get('config_currency'),
				
				'm1weight'      => $m1weight ,
				'm1rate'       	=> $m1rate ,
				'm2weight'      => (isset($products_metalcontent[1])) ? $m2weight : "" ,
				'm2rate'       	=> (isset($products_metalcontent[1])) ? $m2rate : "" ,
				'm3weight'      => (isset($products_metalcontent[2])) ? $m3weight : "" ,
				'm3rate'       	=> (isset($products_metalcontent[2])) ? $m3rate : "" ,

				'g1count'       => (isset($products_gemcontent[0])) ? $products_gemcontent[0]['count'] : "",
				'g1weight'      => $g1weight,
				'g1rate'       	=> $g1rate,
				'g1size'       	=> $g1size,
				'g1shape'       => $g1shape,
				'g2count'       => (isset($products_gemcontent[1])) ? $products_gemcontent[1]['count'] : "",
				'g2weight'      => $g2weight,
				'g2rate'       	=> $g2rate,
				'g2size'       	=> $g2size,
				'g2shape'       => $g2shape,
				'g3count'       => (isset($products_gemcontent[2])) ? $products_gemcontent[2]['count'] : "",
				'g3weight'      => $g3weight,
				'g3rate'       	=> $g3rate,
				'g3size'       	=> $g3size,
				'g3shape'       => $g3shape,
				'g4count'       => (isset($products_gemcontent[3])) ? $products_gemcontent[3]['count'] : "",
				'g4weight'      => $g4weight,
				'g4rate'       	=> $g4rate,
				'g4size'       	=> $g4size,
				'g4shape'       => $g4shape,
				'g5count'       => (isset($products_gemcontent[4])) ? $products_gemcontent[4]['count'] : "",
				'g5weight'      => $g5weight,
				'g5rate'       	=> $g5rate,
				'g5size'       	=> $g5size,
				'g5shape'       => $g5shape,
				
				'm1cost'       	=>  $m1cost,
				'm2cost'       	=>  $m2cost,
				'm3cost'       	=>  $m3cost,
				'g1cost'       	=>  $g1cost,
				'g2cost'       	=>  $g2cost,
				'g3cost'       	=>  $g3cost,
				'g4cost'       	=>  $g4cost,
				'g5cost'       	=>  $g5cost,

				'settingcost'   =>  $settingcost,
				'weightcost'    =>  $weightcost,

				'totalmetalcost' =>  $m1cost + $m2cost + $m3cost,
				'totalgemcost'   =>  $g1cost + $g2cost + $g3cost + $g4cost + $g5cost,
				'product_cost'   =>  $product_cost_total,
				'additionalcost' =>  $product_cost_addl,
				'totalcost'      =>  $product_cost_markup,
				
				'gemresult'		=> $this->model_report_product->getProductGemavail($result['product_id'])
			//	'total'      => $this->currency->format($result['total'], $this->config->get('config_currency'))
			);
			
			$metaltotalcost = $m1cost + $m2cost + $m3cost;
			$gemtotalcost = $g1cost + $g2cost + $g3cost + $g4cost + $g5cost;
			$insertsql = "insert into product_combinations "; 
			$insertsql .= "(product_id,product_metalavail_id,product_gemavail_id,metal_id,gemstone_id,metal_default,gem_default,shape,weight_cost,total_metal_cost,total_gem_cost,product_cost,additional_cost,final_price) ";
			$insertsql .= "values (";
			$insertsql .= "'" . $result['product_id'] . "','" . $result['product_metalavail_id'] . "', '" . $result['product_gemavail_id'] . "',";
			$insertsql .= "'" . $result['metal_id'] . "','" . $result['gemstone_id'] . "', '" . $result['metaldefault'] . "', '" . $result['gemdefault'] . "', '" . $gemcost1[4] . "',"; 
			$insertsql .= "'" . $weightcost . "','" . $metaltotalcost . "','" . $gemtotalcost . "',";
			$insertsql .= "'" . $product_cost_total . "','" . $product_cost_addl . "','" . $product_cost_markup . "'";
			$insertsql .= ")";
			$query = $this->db->query($insertsql); 
			
		}
		
		
//	}
		$WeightCost = $this->db->query("select max(weight_cost) as weightcost from product_combinations where product_id='". $product_id ."'");
		$updateSQL = "update product set weight_cost='". $WeightCost->row['weightcost']  ."' where product_id='". $product_id ."'";
		$query = $this->db->query($updateSQL);

		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['text_all_status'] = $this->language->get('text_all_status');
		
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_model'] = $this->language->get('column_model');
		$this->data['column_quantity'] = $this->language->get('column_quantity');
		$this->data['column_total'] = $this->language->get('column_total');
		
		$this->data['entry_date_start'] = $this->language->get('entry_date_start');
		$this->data['entry_date_end'] = $this->language->get('entry_date_end');
		$this->data['entry_status'] = $this->language->get('entry_status');

		$this->data['button_filter'] = $this->language->get('button_filter');
		
		$this->data['token'] = $this->session->data['token'];
		
		$this->load->model('localisation/order_status');
		
		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
		
		$url = '';
		
		$pagination = new Pagination();
		$pagination->total = $product_total;
		$pagination->page = '';
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('report/product_report', 'token=' . $this->session->data['token'] . $url . '&page={page}');
		$this->data['pagination'] = $pagination->render();		
	}
		
		$this->template = 'report/product_report1.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}	 
	
	public function updateMetalDetails() {   
		$this->load->language('report/product_report');
		$this->load->model('report/product');

		if (isset($this->request->get['metal_id'])) {
			$metal_id = $this->request->get['metal_id'];
			$products_all_metalavail = $this->model_report_product->updateAllProductsMetalDtl($metal_id);
		}
	
	}
	public function updateGemDetails() {    
		$this->load->language('report/product_report');
		$this->load->model('report/product');

		if (isset($this->request->get['gem_id'])) {
			$gem_id = $this->request->get['gem_id'];
			$products_all_gemavail = $this->model_report_product->updateAllProductsGemDtl($gem_id);
		}
	}
	
}
?>