<?php
class ModelShippingRumahKaosBintang extends Model {
    function getQuote($address) {
        $this->load->language('shipping/rumahkaosbintang');

        $method_data = array();
        $quote_data = array();

            $weight = $this->weight->convert($this->cart->getWeight(), $this->config->get('config_weight_class_id'), '2');
			if($weight == 0){$weight = 1;}
            if((isset($address['city']))) {
			$myArray = explode('-', $address['city']);
			} else {
			$myArray = explode('-', $_SESSION['shipping_address']['city']);
			}
            $city = trim($myArray[0]);
            $type = trim($myArray[1]);
            $json_string = 'system/cities.json';
            $_cities = file_get_contents($json_string);
            $cities = json_decode($_cities);
            $result = array_filter($cities, function($c) use ($city,$type) {
            if (stripos($c->city_name, $city) !== false && stripos($c->type, $type) !== false) {
                return true;
            }
            return false;
            });
            $array = json_decode(json_encode($result), true);
            $myarray = array_shift($array);
            $address['city_id'] = $myarray['city_id'];

            if(isset($address['city_id']) && !empty($address['city_id'])) {

            $_SESSION['city_id'] = $address['city_id'];
            $kurirs = array("jne", "tiki", "pos");
            foreach ($kurirs as $kurir) {
			
                $curley = curl_init();
                $url = "http://rumahkaosbintang.com/execute.php?cost=true";
                $fields = array( 'origin'=> $this->config->get('rumahkaosbintang_city_id'), 'destination'=>$address['city_id'], 'weight'=>$weight, 'kur'=>$kurir);
				//print_r($fields);
                $postvars = '';
                foreach($fields as $key=>$value) {
                    $postvars .= $key . "=" . $value . "&";
					
                }
				$postvars = str_replace("null","",$postvars);
                curl_setopt($curley,CURLOPT_URL,$url);
				curl_setopt($curley,CURLINFO_HEADER_OUT ,true);
                curl_setopt($curley,CURLOPT_POST, 1);                //0 for a get request
                curl_setopt($curley,CURLOPT_POSTFIELDS,$postvars);
                curl_setopt($curley,CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curley,CURLOPT_CONNECTTIMEOUT ,10);
                curl_setopt($curley,CURLOPT_TIMEOUT, 20);
                $responses = curl_exec($curley);
                $array = json_decode( $responses, true );
                curl_close ($curley);
	
                foreach($array as $kuror){
                $nama = $kuror['name'];
                foreach($kuror['costs'] as $kuror2){
                $service = $kuror2['service'];
                foreach($kuror2['cost'] as $kuror3){
				if ($this->currency->getCode() != 'IDR') {
					$coste = $this->currency->convert($kuror3['value'], 'IDR',$this->currency->getCode());
				} else {
					$coste = $kuror3['value'] / $this->currency->getValue('IDR');
				}
                $quote_data[$kurir.$service] = array(
                'code'         => 'rumahkaosbintang'.'.'.$kurir.$service,
                'title'        => $nama.' - '.$service,
                'cost'         => $coste,
                'tax_class_id' => $address['zone_id'],
                'text'         => $this->currency->format($this->tax->calculate($coste, $this->config->get('rumahkaosbintang_tax_class_id'), $this->config->get('config_tax')))
                );
            
    
                }
                }
                }
                };
            }
			if(isset($address['city'])){
			$tujuan = current(explode('-', $address['city']  ));
			} else {
			$tujuan = current(explode('-', $_SESSION['shipping_address']['city']  ));
			}
            $method_data = array(
                'code'         => 'rumahkaosbintang',
				//'title'      => 'tes' ,
                'title'      => current(explode('-', $this->config->get('rumahkaosbintang_city')  )) . ' - ' . $tujuan . ' (' . $weight / 1000 . ' Kgram)' ,
                'quote'      => $quote_data,
                'sort_order' => $this->config->get('rumahkaosbintang_sort_order'),
                'error'      => FALSE
              );
  			
        return $method_data;
    }
}