<?php
defined('BASEPATH') OR exit('No direct script access allowed');

ini_set('max_execution_time',0);

class Rapaportctrl extends CI_Controller {
	public $Rapaportmdl;
	public $excel;
	public $nusoap_lib;
	public $operation;
	public function __construct()
	{
		parent::__construct();
		$this->load->model('rapaport/Rapaportmdl');
		$this->load->library('session');
		$this->load->library('db_operations');
		$this->config->load('extra');
	}

	public function index()
	{	
		if(sessionExist()) {
			$this->load->view('pages/master/index');
		} else {
			redirect('loginctrl/logout');	
		}
	}

	public function update_rp_rate($type) 
	{
		// echo "111"; exit();
		$this->load->library('Nusoap_lib'); //load the library here

		//prepare soap request to Rapaport: 
		$rap_soapUrl = "https://technet.rapaport.com/webservices/prices/rapaportprices.asmx?wsdl"; 
		$soap_Client = new nusoap_client($rap_soapUrl, 'wsdl'); 
		
		$rap_credentials['Username'] = "ex36i6flqmgja5gm9zms6fkh5ghzwd"; 
		$rap_credentials['Password'] = "j8mYPHPn"; 

		//do login, and save authentication ticket for further use: 
		$result 			= $soap_Client->call("Login", $rap_credentials); 
		$rap_auth_ticket 	= $soap_Client->getHeaders(); 

		//get complete price sheet, and save as a file (call this both for Round and Pear): 
		$paramsB["shape"] = $type == 1 ? "Round" : "Pear"; 
		$soap_Client->setHeaders($rap_auth_ticket); 
		$result = $soap_Client->call("GetPriceSheet", $paramsB);
		$response = [];
		// echo "<pre>";print_r($result);exit();
		if(!empty($result) && !empty($result['GetPriceSheetResult']) && !empty($result['GetPriceSheetResult']['Prices']) && !empty($result['GetPriceSheetResult']['Prices']['diffgram']) && !empty($result['GetPriceSheetResult']['Prices']['diffgram']['NewDataSet']) && !empty($result['GetPriceSheetResult']['Prices']['diffgram']['NewDataSet']['Table']))
		{
			$data = $result['GetPriceSheetResult']['Prices']['diffgram']['NewDataSet']['Table']; 

			if (!empty($data)) 
			{
				$flag = 1;
				$this->db->trans_begin();
				$this->db_operations->delete_record('rapaport_master', array('rp_diamond_type' => $type));

				foreach ($data as $key => $value) 
				{
					$rp_id 		 = 0; 
					$insert_data = array();

					$insert_data['rp_diamond_type'] = $type;
					$insert_data['rp_clarity'] 		= $value['Clarity'];
					$insert_data['rp_range_from'] 	= $value['LowSize'];
					$insert_data['rp_range_to'] 	= $value['HighSize'];
					$insert_data['rp_color'] 		= $value['Color'];
					$insert_data['rp_result'] 		= $value['Price'];
					$insert_data['rp_user_id'] 		= $_SESSION['user_id'];
					$insert_data['rp_created_at']	= date('Y-m-d H:i:s');
					$insert_data['rp_modify_uid']	= $_SESSION['user_id'];
					$insert_data['rp_updated_at']	= date('Y-m-d H:i:s');

					$rp_id = $this->db_operations->data_insert('rapaport_master', $insert_data);
					if(empty($rp_id))
					{
						$flag = 0;
					}
					else
					{
						$diamond_data = $this->Rapaportmdl->get_diamond_data($insert_data);	
						if(!empty($diamond_data))
						{
							foreach ($diamond_data as $k => $v) 
							{
								if(strtoupper($v['dm_lab']) == 'GIA')
                                {
                                    $sub_total = $v['dm_wt'] * $insert_data['rp_result'];
                                    
                                   	if(!empty($v['dm_disc']))
                                   	{
                                   		$disc = explode('-', $v['dm_disc']);
	                                    if(empty($disc[0]))
	                                    {
	                                        $disc = $disc[1];
	                                    }
	                                    else
	                                    {
	                                        $disc = $disc[0];
	                                    }
                                   	}
                                   	else
                                   	{
                                   		$disc = 0;
                                   	}

                                    $disc_amt = ($sub_total * $disc)/100;
                                    $diamond_master['dm_dollar'] = $sub_total - $disc_amt;
                                    
                                    $diamond_master['dm_ppc'] = $diamond_master['dm_dollar'] / $v['dm_wt'];


                                }
                                elseif(strtoupper($v['dm_lab']) == 'IGI' || strtoupper($v['dm_lab']) == 'HRD')
                                {
                                    $sub_total = $v['dm_wt'] * $insert_data['rp_result'];
                                    
                                    if(!empty($v['dm_disc']))
                                   	{
                                   		$disc = explode('-', $v['dm_disc']);
	                                    if(empty($disc[0]))
	                                    {
	                                        $disc = $disc[1];
	                                    }
	                                    else
	                                    {
	                                        $disc = $disc[0];
	                                    }
                                   	}
                                   	else
                                   	{
                                   		$disc = 0;
                                   	}

                                    $disc_amt = ($sub_total * $disc)/100;
                                    $total_dollar = $sub_total - $disc_amt;
                                    
                                    $disc_amt = ($total_dollar * 2) / 100;
                                    $diamond_master['dm_dollar'] = $total_dollar - $disc_amt;
                                    
                                    $diamond_master['dm_ppc'] = $diamond_master['dm_dollar'] / $v['dm_wt'];
                                }
                                else
                                {
                                    $diamond_master['dm_dollar'] = 0;
                                    $diamond_master['dm_ppc'] = 0;
                                }
                                $diamond_master['dm_rap'] = $insert_data['rp_result'];
								if($this->db_operations->data_update('diamond_master', $diamond_master, 'dm_id', $v['dm_id']) < 1)
								{
									$flag = 0;
								}
							}
						}
					}
				}

				if(!empty($flag))
				{
					if ($this->db->trans_status() === FALSE) 
					{
					    $this->db->trans_rollback();
					    $response['resp'] = 0;
					} 
					else 
					{
					    $this->db->trans_commit();
					    $response['resp'] = 1;
					}
				}
				else
				{
					$this->db->trans_rollback();
				    $response['resp'] = 0;
				}
						
			}
			else
			{
				$response['resp'] = 0;		
			}
		}
		else
		{
		    $response['resp'] = 0;
		}
		echo json_encode($response);
		
	}

	
}
