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

	public function index() {
		$this->load->type('tipocomum');
		$this->load->language('module/modulofutura');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('setting/setting');
		$this->load->dmodel('wsceo/cadastro/empresa');
		$this->load->helper('ftr_geral');

		$this->document->addScript('admin/public/js/module/modulofutura.js');

		$this->data['ftr_geral'] = new ftr_geral();
		$this->data['Type_Status'] = $this->load->typeForTwig('Type_Status');
		$this->data['Type_DsimDnao'] = $this->load->typeForTwig('Type_DsimDnao');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
			$this->model_setting_setting->editSettingModuloFutura('modulofutura', $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'));
		}

		$language = $this->load->language('module/modulofutura');
		$this->data = array_merge($this->data, $language);

		$this->data['link'] = $this->url->link('catalog/product/update', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['error_warning'] = (isset($this->error['warning']) ? $this->error['warning'] : '');
		$this->data['error_serverftp'] = (isset($this->error['futura_serverftp']) ? $this->error['futura_serverftp'] : '');
		$this->data['error_userftp'] = (isset($this->error['futura_userftp']) ? $this->error['futura_userftp'] : '');
		$this->data['error_senhaftp'] = (isset($this->error['futura_senhaftp']) ? $this->error['futura_senhaftp'] : '');


		 //Quando o usuario tem somente marketplace 
		 $showBreadcrumb = !$this->config->get('config_sistema_ecommerce') && $this->config->get('config_sistema_marketplace');
		 $this->data['breadcrumbs'] = array();

		 if($showBreadcrumb){
			$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('module/modulofutura', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL'),
				'separator' => ' :: '
			);
		 }else{
			 $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('text_module'),
				 'href'      => $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'),
				 'separator' => ' :: '
			 );
	 
			 $this->data['breadcrumbs'][] = array(
				 'text'      => $this->language->get('heading_title'),
				 'href'      => $this->url->link('module/modulofutura', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL'),
				 'separator' => ' :: '
			 );
		 }

		$url_repl = $this->url->link(
			'module/modulofutura/getParametrosReplicacao',
			'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 
			'SSL'
		);

		$this->data['url_repl'] = $url_repl;
		$this->data['action'] = $this->url->link('module/modulofutura', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL');
		$this->data['cancel'] = $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL');

		$this->children = array(
			'common/header',
			'common/footer',
		);

		$this->data['select_order_status_pagseguro_id'] = (isset($this->request->post['select_order_status_pagseguro_id']) ? $this->request->post['select_order_status_pagseguro_id'] : $this->config->get('select_order_status_pagseguro_id'));
		
		$this->data['select_order_status_mercadopago_id'] = (isset($this->request->post['select_order_status_mercadopago_id']) ? $this->request->post['select_order_status_mercadopago_id'] : $this->config->get('select_order_status_mercadopago_id'));
		
		$this->data['select_order_status_cielo_id'] = (isset($this->request->post['select_order_status_cielo_id']) ? $this->request->post['select_order_status_cielo_id'] : $this->config->get('select_order_status_cielo_id')); 

		$this->data['select_order_status_erede_id'] = (isset($this->request->post['select_order_status_erede_id']) ? $this->request->post['select_order_status_erede_id'] : $this->config->get('select_order_status_erede_id'));

		$this->data['select_order_status_bcash_id'] = (isset($this->request->post['select_order_status_bcash_id']) ? $this->request->post['select_order_status_bcash_id'] : $this->config->get('select_order_status_bcash_id'));

		$this->data['select_order_status_paypal_id'] = (isset($this->request->post['select_order_status_paypal_id']) ? $this->request->post['select_order_status_paypal_id'] : $this->config->get('select_order_status_paypal_id'));

		$this->data['select_order_status_itaushopline_id'] = (isset($this->request->post['select_order_status_itaushopline_id']) ? $this->request->post['select_order_status_itaushopline_id'] : $this->config->get('select_order_status_itaushopline_id'));
		
		$this->data['select_order_status_id'] = (isset($this->request->post['select_order_status_id']) ? $this->request->post['select_order_status_id'] : $this->config->get('select_order_status_id'));

		$this->data['select_order_status_canc_id'] = (isset($this->request->post['select_order_status_canc_id']) ? $this->request->post['select_order_status_canc_id'] : $this->config->get('select_order_status_canc_id'));
		
		$this->data['select_order_status_aban_id'] = (isset($this->request->post['select_order_status_aban_id']) ? $this->request->post['select_order_status_aban_id'] : $this->config->get('select_order_status_aban_id'));
		
		$this->data['select_order_status_nota_fiscal_emitida_id'] = (isset($this->request->post['select_order_status_nota_fiscal_emitida_id']) ? $this->request->post['select_order_status_nota_fiscal_emitida_id'] : $this->config->get('select_order_status_nota_fiscal_emitida_id'));

		$this->data['select_store_default'] = (isset($this->request->post['select_store_default']) ? $this->request->post['select_store_default'] : $this->config->get('select_store_default'));
		
		$this->data['select_language_default'] = (isset($this->request->post['select_language_default']) ? $this->request->post['select_language_default'] : $this->config->get('select_language_default'));
		
		$this->data['select_peso'] = (isset($this->request->post['select_peso']) ? $this->request->post['select_peso'] : $this->config->get('select_peso'));
		
		$this->data['select_comprimento'] = (isset($this->request->post['select_comprimento']) ? $this->request->post['select_comprimento'] : $this->config->get('select_comprimento'));
		
		$this->data['select_conta_status'] = (isset($this->request->post['select_conta_status']) ? $this->request->post['select_conta_status'] : $this->config->get('select_conta_status'));
		
		$this->data['futura_serverftp'] = (isset($this->request->post['futura_serverftp']) ? $this->request->post['futura_serverftp'] : $this->config->get('futura_serverftp'));

		$this->data['futura_userftp'] = ( isset($this->request->post['futura_userftp']) ? $this->request->post['futura_userftp'] : $this->config->get('futura_userftp'));
		
		$this->data['futura_senhaftp'] = (isset($this->request->post['futura_senhaftp'])? $this->request->post['futura_senhaftp'] : $this->config->get('futura_senhaftp'));

		$this->data['futura_portaftp'] = (isset($this->request->post['futura_portaftp']) ? $this->request->post['futura_portaftp']: $this->config->get('futura_portaftp'));

		$this->data['futura_diretorioftp'] = (isset($this->request->post['futura_diretorioftp']) ? $this->request->post['futura_diretorioftp'] : $this->config->get('futura_diretorioftp'));

		$this->data['futura_desenvolvidopor'] = (isset($this->request->post['futura_desenvolvidopor']) ? $this->request->post['futura_desenvolvidopor'] : $this->config->get('futura_desenvolvidopor')); 

		$this->data['futura_id_empresa'] = (isset($this->request->post['futura_id_empresa']) ? $this->request->post['futura_id_empresa'] : $this->config->get('futura_id_empresa')); 
		
		$empresas = $this->dmodel_wsceo_cadastro_empresa->consultarEmpresa();
		$this->data['empresas'] = array ();
		if ( is_array($empresas) && count($empresas)>0 ){
			foreach ($empresas as $key => $empresa) {
				$this->data['empresas'][] = array(
					'id'     => $empresa['id'],
					'name'   => $empresa['fantasia'],
					'select' => (ftr_geral::isIsset($this->config->get('futura_id_empresa')) && ($this->config->get('futura_id_empresa') == $empresa['id'])) ? true : false
				);
			}
		}		

		$this->load->model('localisation/order_status');
		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();

		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();

		$this->load->model('localisation/weight_class');
		$this->data['weight_classes'] = $this->model_localisation_weight_class->getWeightClasses();

		$this->load->model('setting/store');
		$this->data['stores'] = $this->model_setting_store->getStores();

		$this->load->model('localisation/length_class');
		$this->data['length_classes'] = $this->model_localisation_length_class->getLengthClasses();

    $this->data['userGroup'] = ($this->user->getUserGroup() == Type_UserGroupAdm::Tp_UserFutura) ? true : false ;

		$this->data['parametros_replicacao'] = $this->getParametrosReplicacao();

		$this->response->setOutput($this->render('module/modulofutura'));
	}

	public function getParametrosReplicacao(){

		$atualizar = isset($this->request->post['atualizar']) ? $this->request->post['atualizar'] : false;

		$query = $this->db->query("select parametros_replicacao from parametros_efutura limit 1")->row;

		if ($query['parametros_replicacao'] && $atualizar == false) {
			return $query['parametros_replicacao']; 
		} else {
			$sql = "select first 1
				'{'||
				'\"tipo_pedido\" : { \"ID\" :'||p.SITE_FK_TIPO_PEDIDO||',\"Descricao\":\"'||tp.DESCRICAO||'\"},'||
				'\"empresa\" : { \"ID\" :'||p.SITE_FK_EMPRESA||',\"Descricao\":\"'||c.RAZAO_SOCIAL||'\"},'||
				'\"tabela_preco\" : { \"ID\" :'||p.SITE_FK_TABELA_PRECO||',\"Descricao\":\"'||tpr.DESCRICAO||'\"},'||
				'\"conta_corrente\" : { \"ID\" :'||p.SITE_FK_CONTA_CORRENTE||',\"Descricao\":\"'||cc.DESCRICAO||'\"},'||
				'\"tipo_pedido_orcamento\" : { \"ID\" :'||p.SITE_FK_TIPO_PEDIDO_ORCAMENTO||',\"Descricao\":\"'||tor.DESCRICAO||'\"}'
				||'}' as parametros_replicacao 

			from PARAMETROS P
			left join TIPO_PEDIDO tp on tp.ID = p.SITE_FK_TIPO_PEDIDO
			left join CADASTRO c on c.ID = p.SITE_FK_EMPRESA
			left join TABELA_PRECO tpr on tpr.ID = p.SITE_FK_TABELA_PRECO
			left join CONTA_CORRENTE cc on cc.ID = p.SITE_FK_CONTA_CORRENTE
			left join TIPO_PEDIDO tor on tor.ID = p.SITE_FK_TIPO_PEDIDO_ORCAMENTO";

			$response = $this->wsceo->getConsultaFromSqlSemCache($sql);
			if (isset($response[0]['parametros_replicacao'])){
				$query = $this->db->query("UPDATE parametros_efutura SET parametros_replicacao ='".$response[0]['parametros_replicacao'] . "'");
				$querySelect = $this->db->query("select parametros_replicacao from parametros_efutura limit 1")->row;
				return $this->response->setOutput($querySelect['parametros_replicacao']);
			}
		}
	}

	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/modulofutura')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (!$this->request->post['futura_serverftp']) {
			$this->error['futura_serverftp'] = $this->language->get('error_serverftp');
		}

		if (!$this->request->post['futura_userftp']) {
			$this->error['futura_userftp'] = $this->language->get('error_userftp');
		}

		if (!$this->request->post['futura_senhaftp']) {
			$this->error['futura_senhaftp'] = $this->language->get('error_senhaftp');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}
}