<?php

namespace App\Services;

use App\DAO\CentroCustoDAO;
use App\DAO\DAOResponse\DAOResponseInterface;
use App\DAO\Filter\QueryFilter;
use App\DAO\Pagination\Pagination;
use App\Entities\CentroCusto\CentroCusto;
use App\Mappers\CentroCustoMapper;
use App\Services\ServiceResponse\ServiceResponse;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Utils\ResponseException;
use App\Utils\Collections\QueryFilterCollection;
use App\Utils\Collections\QueryOrderCollection;
use App\ViewModels\Orcamento\OrcamentoClienteItemDetalhe\OrcamentoClienteItemDetalhe;
// use const False\MyClass\true;
use App\DAO\ComandaDAO;
use App\DAO\UnidadeDAO;
use App\DAO\DAOResponse\DAOResponse;
use App\DAO\PetDAO;
use App\Entities\relatorio\RelatorioComandaItensExectadosNaoExecutado;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

class ComandaService extends Service {

	public function findComandasByClienteIdByUnidadeId(int $clienteid, int $unidadeid, int $usuarioid): ServiceResponse {

		$service_response = new ServiceResponse();
		$unidades = array ();
		$unidades = $this->getUnidadeToUsuarioId( $usuarioid );

		if (count( $unidades ) == 0) {
			$service_response->setStatus( ServiceResponseInterface::FORBIDDEN );
		} else {
			$permitir = 0;
			for($i = 0; $i < count( $unidades ); $i++) {
				if (intval( $unidades [$i] ['id'] ) == $unidadeid) {
					$permitir = 1;
					break;
				}
			}

			if ($permitir == 0) {
				$service_response->setStatus( ServiceResponseInterface::FORBIDDEN );
			} else {
				$comandas = $this->getComandasByClienteIdByUnidadeId( $clienteid, $unidadeid );
				$service_response->setStatus( $comandas->getStatus() );
				$service_response->setData( $comandas->getData() );
			}
		}

		return $service_response;

	}

	public function findComandasByClienteIdByPetIdByUnidadeId(int $clienteid, int $unidadeid, int $usuarioid, int $petid): ServiceResponse {

		$service_response = new ServiceResponse();
		$unidades = array ();
		$unidades = $this->getUnidadeToUsuarioId( $usuarioid );

		if (count( $unidades ) == 0) {
			$service_response->setStatus( ServiceResponseInterface::FORBIDDEN );
		} else {
			$permitir = 0;
			for($i = 0; $i < count( $unidades ); $i++) {
				if (intval( $unidades [$i] ['id'] ) == $unidadeid) {
					$permitir = 1;
					break;
				}
			}

			if ($permitir == 0) {
				$service_response->setStatus( ServiceResponseInterface::FORBIDDEN );
			} else {
				$comandas = $this->getComandasByClienteIdByPetIdByUnidadeId( $clienteid, $unidadeid, $petid );
				$service_response->setStatus( $comandas->getStatus() );
				$service_response->setData( $comandas->getData() );
			}
		}

		return $service_response;

	}

	public function findComandaItensEmAbertoByIdPetByUnidadeId(int $petid, int $unidadeid, int $usuarioid): ServiceResponse {

		$service_response = new ServiceResponse();
		$unidades = array ();
		$unidades = $this->getUnidadeToUsuarioId( $usuarioid );

		if (count( $unidades ) == 0) {
			$service_response->setStatus( ServiceResponseInterface::FORBIDDEN );
		} else {
			$permitir = 0;
			for($i = 0; $i < count( $unidades ); $i++) {
				if (intval( $unidades [$i] ['id'] ) == $unidadeid) {
					$permitir = 1;
					break;
				}
			}

			if ($permitir == 0) {
				$service_response->setStatus( ServiceResponseInterface::FORBIDDEN );
			} else {
				$comandas = $this->getComandaItensEmAbertoByIdPetByUnidadeId( $petid, $unidadeid );
				$service_response->setStatus( $comandas->getStatus() );
				$service_response->setData( $comandas->getData() );
			}
		}

		return $service_response;

	}

	public function getClienteIdByComandaId(int $comandaid): ServiceResponse {

		$service_response = new ServiceResponse();
		$comanda_dao = new ComandaDAO();
		$dao_response = $comanda_dao->getClienteIdByComandaId( $comandaid );

		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response->setStatus( ServiceResponseInterface::SUCESS );
				$service_response->setData( $dao_response->getData() );
				break;
			case DAOResponseInterface::FOREIGN_KEY_DONT_EXISTS :
				$service_response->setStatus( ServiceResponseInterface::VALIDATION_PARAMS_ERROR );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
			case DAOResponseInterface::DUPLICATED_UNIQUE_KEY :
				$service_response->setStatus( ServiceResponseInterface::VALIDATION_PARAMS_ERROR );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
			case DAOResponseInterface::INTERNAL_DAO_ERROR :
				$service_response->setStatus( ServiceResponseInterface::SERVICE_INTERNAL_ERROR );
				break;
			default :
				$service_response->setStatus( ServiceResponseInterface::DATABASE_CONNECTION_ERROR );
				break;
		}

		return $service_response;

	}

	private function getComandasByClienteIdByUnidadeId(int $clienteid, int $unidadeid): DAOResponse {

		$camandaDao = new ComandaDAO();
		return $camandaDao->getComandasByClienteIdByUnidadeId( $clienteid, $unidadeid );

	}

	private function getComandasAbertasByClienteIdByPetIdByUnidadeId(int $petid, int $unidadeid): DAOResponse {

		$camandaDao = new ComandaDAO();
		$dao_responde = $this->getClientIdByPetId( $petid );
		if ($dao_responde->getStatus() == DAOResponseInterface::SUCESS) {
			$resp = $dao_responde->getData();
			$clienteid = $resp [0] ['id_cliente'];
		} else {
			return $dao_responde;
		}
		return $camandaDao->getComandasByClienteIdByPetIdByUnidadeId( $clienteid, $unidadeid, $petid );

	}

	private function getClientIdByPetId(int $petid): DAOResponse {

		$petDao = new PetDAO();
		return $petDao->getClienteIdByPetId( $petid );

	}

	private function getComandasByClienteIdByPetIdByUnidadeId(int $clienteid, int $unidadeid, int $petid): DAOResponse {

		$camandaDao = new ComandaDAO();
		return $camandaDao->getComandasByClienteIdByPetIdByUnidadeId( $clienteid, $unidadeid, $petid );

	}

	private function getComandaItensEmAbertoByIdPetByUnidadeId(int $petid, int $unidadeid): DAOResponse {

		$camandaDao = new ComandaDAO();
		return $camandaDao->getComandaItensEmAbertoByIdPetByUnidadeId( $petid, $unidadeid );

	}

	private function getUnidadeToUsuarioId(int $usuarioid): array {

		$unidade_dao = new UnidadeDAO();
		$unidades = array ();
		$unidades = $unidade_dao->getUnidadeToId( $usuarioid );

		return $unidades;

	}

	public function getComandasMeusRecebimentosByUnidadeByPeriodoToHtml(int $idunidade, string $data_ini, string $data_fim): string {

		$camandaDao = new ComandaDAO();

		$totais = $camandaDao->getMeusRecibementosTotalByUnidadeByPeriodo( $idunidade, $data_ini, $data_fim );

		return "";

	}

	public function registerComanda(array $data): ServiceResponse {

		try {
			$paciente_registration_vm = CentroCustoMapper::CentroCustoRegistrationViewModelMapper( $data );
		} catch ( ResponseException $response_exception ) {
			$log_service = new LogService();
			$log_service->log_error( ServiceResponseInterface::SERVICE_INTERNAL_ERROR,
				'Ocorreu um erro durante o mapeamento da entidade CentroCustoRegistrationViewModel', $response_exception->getData() );
			return ServiceResponse::builder()->setStatus( ServiceResponseInterface::SERVICE_INTERNAL_ERROR )
				->setErrors( $response_exception->getData() )
				->build();
		}
		$paciente_dao = new CentroCustoDAO();
		$dao_response = $paciente_dao->persistCentroCustoRegistrationVM( $paciente_registration_vm );
		$service_response = new ServiceResponse();
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response->setStatus( ServiceResponseInterface::SUCESS );
				$paciente = (function (CentroCusto $paciente): CentroCusto {
					return $paciente;
				})( $dao_response->getData() );
				$service_response->setData( [ 
					'id_centro_custo' => $paciente->getId()
				] );
				break;
			case DAOResponseInterface::FOREIGN_KEY_DONT_EXISTS :
				break;
			case DAOResponseInterface::DUPLICATED_UNIQUE_KEY :
				$service_response->setStatus( ServiceResponseInterface::VALIDATION_PARAMS_ERROR );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
			case DAOResponseInterface::INTERNAL_DAO_ERROR :
				$service_response->setStatus( ServiceResponseInterface::SERVICE_INTERNAL_ERROR );
				break;
			default :
				$service_response->setStatus( ServiceResponseInterface::DATABASE_CONNECTION_ERROR );
				break;
		}
		return $service_response;

	}

	public function getComandaList(?Pagination $pagination, ?QueryFilterCollection $query_filter_collection,
			?QueryOrderCollection $query_order_collection = null): ServiceResponse {

		$paciente_dao = new CentroCustoDAO();
		$dao_response = $paciente_dao->getCentroCustoList( $pagination, $query_filter_collection, $query_order_collection );
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response_status = ServiceResponseInterface::SUCESS;
				break;
			case DAOResponseInterface::RECORD_NOT_FOUND :
				$service_response_status = ServiceResponseInterface::RECORD_NOT_FOUND;
				break;
			case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
				$service_response_status = ServiceResponseInterface::DATABASE_CONNECTION_ERROR;
				break;
		}
		$service_response = ServiceResponse::builder()->setStatus( $service_response_status )
			->build();
		if ($service_response_status === DAOResponseInterface::SUCESS) {
			$dao_response_data = $dao_response->getData();
			$paciente_details_array = CentroCustoMapper::getCentroCustosDetailsArrayFromCentroCustoDetailsVMList( $dao_response_data );
			$pagination = $dao_response->getPagination();
			$data = [ 
				'centro_custo' => $paciente_details_array,
				'pagination' => $pagination ? $pagination->toResponseArray() : null
			];
			$service_response->setData( $data );
		}
		return $service_response;

	}

	public function getCentroCusto(QueryFilter $query_filter): ServiceResponse {

		$paciente_dao = new CentroCustoDAO();
		$query_filter_collection = new QueryFilterCollection();
		$query_filter_collection->push( $query_filter );
		$dao_response = $paciente_dao->getCentroCusto( $query_filter_collection );
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response_status = ServiceResponseInterface::SUCESS;
				break;
			case DAOResponseInterface::RECORD_NOT_FOUND :
				$service_response_status = ServiceResponseInterface::RECORD_NOT_FOUND;
				break;
			case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
				$service_response_status = ServiceResponseInterface::DATABASE_CONNECTION_ERROR;
				break;
		}
		$service_response = ServiceResponse::builder()->setStatus( $service_response_status )
			->build();
		if ($service_response_status === DAOResponseInterface::SUCESS) {
			$service_response->setData( $dao_response->getData()
				->toArray() );
		}
		return $service_response;

	}

	public function updateCentroCustoData(int $id_paciente, array $data): ServiceResponse {

		try {
			$paciente_update_vm = CentroCustoMapper::getCentroCustoUpdateVMFromArray( $data );
			if (count( $paciente_update_vm->toArray( true ) ) === 0) {
				return ServiceResponse::builder()->setStatus( ServiceResponseInterface::INSUFFICIENT_DATA )
					->build();
			}
		} catch ( ResponseException $response_exception ) {
			$log_service = new LogService();
			$log_service->log_error( ServiceResponseInterface::SERVICE_INTERNAL_ERROR,
				'Ocorreu um erro durante o mapeamento da entidade CentroCustoUpdateViewModel', $response_exception->getData() );
			return ServiceResponse::builder()->setStatus( ServiceResponseInterface::SERVICE_INTERNAL_ERROR )
				->setErrors( $response_exception->getData() )
				->build();
		}

		$paciente_dao = new CentroCustoDAO();
		$dao_response = $paciente_dao->persistCentroCustoUpdateVM( $id_paciente, $paciente_update_vm );
		$service_response = new ServiceResponse();

		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response->setStatus( ServiceResponseInterface::SUCESS );
				break;
			case DAOResponseInterface::FOREIGN_KEY_DONT_EXISTS :
				$service_response->setStatus( ServiceResponseInterface::VALIDATION_PARAMS_ERROR );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
			case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
				$service_response->setStatus( ServiceResponseInterface::DATABASE_CONNECTION_ERROR );
				break;
		}
		return $service_response;

	}

	public function deleteCentroCusto(int $id_centro_custo): ServiceResponse {

		$paciente_dao = new CentroCustoDAO();
		$dao_response = $paciente_dao->persistCentroCustoDeleteVM( $id_centro_custo );
		$service_response = new ServiceResponse();

		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$service_response->setStatus( ServiceResponseInterface::SUCESS );
				break;
			case DAOResponseInterface::FOREIGN_KEY_DONT_EXISTS :
				$service_response->setStatus( ServiceResponseInterface::VALIDATION_PARAMS_ERROR );
				$service_response->setErrors( $dao_response->getErrors() );
				break;
			case DAOResponseInterface::DATABASE_CONNECTION_ERROR :
				$service_response->setStatus( ServiceResponseInterface::DATABASE_CONNECTION_ERROR );
				break;
		}
		return $service_response;

	}

	public function getDadosComandaID(int $comanda_id): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new ComandaDAO();
		$daoResponse = $dao->getDadosComandaID( $comanda_id );
		$service_response = $this->setServiceResponse( $daoResponse );

		return $service_response;

	}

	public function adminNewReqExecExameInComanda(array $request, int $requisicao_id): ServiceResponse {


	}

	public function getComandaAtivaByPetByClienteByUnidade(int $id_pet, int $id_cliente, int $id_unidade): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new ComandaDAO();
		$daoResponse = $dao->getComandaAtivaByPetByClienteByUnidade( $id_pet, $id_cliente, $id_unidade );
		$service_response = $this->setServiceResponse( $daoResponse );

		return $service_response;

	}

	public function registerComandaFromOrcamento(array $orcamento): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new ComandaDAO();
		$daoResponse = $dao->registerComnadanFromOrcamento( $orcamento );
		$service_response = $this->setServiceResponse( $daoResponse );

		return $service_response;

	}

	public function registerComandaItemFromOrcamento(OrcamentoClienteItemDetalhe $orcamentoDetalhe, int $id_comanda, int $id_requisicao): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new ComandaDAO();
		$daoResponse = $dao->registerComandaItemFromOrcamento( $orcamentoDetalhe, $id_comanda, $id_requisicao );
		$service_response = $this->setServiceResponse( $daoResponse );

		return $service_response;

	}

	public function getHtmlRelatorioComandaFullItemPorConvenio($dataInicial, $dataFinal, $unidade, $convenio, $execucao): ServiceResponse {

		$service_response = new ServiceResponse();

		$svr_exec_Response = $this->getComandaItemExecutadoPorConvenioParaPdf( $dataInicial, $dataFinal, $unidade, $convenio, $execucao );
		$svr_nexec_Response = $this->getComandaItemNaoExecutadoPorConvenioParaPdf( $dataInicial, $dataFinal, $unidade, $convenio, $execucao );

		$lista = array ();
		$dados_executados = $svr_exec_Response->getData();
		$dados_nao_executados = $svr_nexec_Response->getData();
		
		$lista = $this->getListItensExecNaoExec( $dados_executados, $dados_nao_executados );

		$html = '';
		$html .= '<html>';
		$html .= '<head> ';
		$html .= '<style> ';
		$html .= "
                <style>
                    @page {
                        margin: 0cm 0cm;
                    }
                    body {
                        margin-top: 3cm;
                        margin-bottom: 2cm;
                    }
				
                    /** Define the header rules **/
                    header {
                        position: fixed;
                        top: -1cm;
                        left: 0cm;
                        right: 0cm;
                        height: 2cm;
                        text-align: center;
                    }
                </style>
				
        ";
		$html .= '</style> ';
		$html .= '</head> ';
		$html .= '<body> ';
		$total_exec = 0;
		$total_nexec = 0;
		foreach ( $lista as $key => $item ) {
			$key = $key++;
			$table_hover = $key % 2 ? "background-color: #DCDCDC;" : "";

			$total_exec = $total_exec + $item->getExecutados();
			$total_nexec = $total_nexec + $item->getNaoExecutados();

			$comanda_item .= '
                    <tr style="' . $table_hover . '">
                        <td style="text-align: center;">' . $item->getDescricao() . ' </td>
                        <td style="text-align: center;">'. $item->getConvenio() .' </td>
                        <td style="text-align: center;"> </td>
                        <td style="text-align: center;">' . $item->getTotal() . '</td>
                    </tr>
                ';
		}

		$html .= '<header>';
		$html .= "<h2 style='text-align: center; font-size: 30px'> Relatório de Produção </h2>";
		$html .= '	<table border="0" width="100%">
                <tr>
                    <td>Período: ' . date( "d/m/Y", strtotime( $dataInicial ) ) . ' até ' . date( "d/m/Y", strtotime( $dataFinal ) ) .
				'</td>
					<td style="text-align: center">Produto/Serviços</td>
                    <td style="text-align: right">Data do Sistema: ' . date( "d/m/Y" ) . '</td>
                </tr>
            </table>';
		$html .= '<hr>';
		$html .= '</header>';
		$html .= '<main>';
		$html .= '	<table style="font-size: 13px" border="0" width="100%">
                <thead>
					<tr>
                    <td style="text-align: center"><strong>Descrição </strong></td>
                    <td style="text-align: center"><strong> Convenio </strong></td>
                    <td style="text-align: center"><strong> </strong></td>
                    <td style="text-align: center"><strong>Total</strong></td>
					</tr>
					<tr>
					<td colspan=4>
						<hr>
					</td>
					</tr>
                </thead>

				<tbody>	
                ' . $comanda_item . '
				</tbody>
                <br>
                <tr>
                    <td style="text-align: center"><strong>Total </strong></td>
					<td style="text-align: center"><strong></strong></td>
                    <td style="text-align: center"><strong></strong></td>
                    <td style="text-align: center"><strong>' . ($total_exec + $total_nexec) . '</strong></td>
                    
                </tr>
            </table>';
		$html .= '<hr>';
		$html .= '</main>';

		$html .= '</html>';

		$service_response->setData( $html );
		$service_response->setStatus( ServiceResponseInterface::SUCESS );

		return $service_response;

	}

	public function getExcelRelatorioComandaFullItemPorConvenio($dataInicial, $dataFinal, $unidade, $convenio, $execucao): ServiceResponse {

		$service_response = new ServiceResponse();

		$svr_exec_Response = $this->getComandaItemExecutadoPorConvenioParaPdf( $dataInicial, $dataFinal, $unidade, $convenio, $execucao );
		$svr_nexec_Response = $this->getComandaItemNaoExecutadoPorConvenioParaPdf( $dataInicial, $dataFinal, $unidade, $convenio, $execucao );

		$lista = array ();
		$dados_executados = $svr_exec_Response->getData();
		$dados_nao_executados = $svr_nexec_Response->getData();

		$lista = $this->getListItensExecNaoExec( $dados_executados, $dados_nao_executados );

		$spreadsheet = new Spreadsheet();
		$sheet = $spreadsheet->getActiveSheet();

		$cols = [ 
			"Descrição",
			"Executado",
			"Não Executados",
			"Total"
		];

		$sheet->setCellValue( 'A1', 'Relatório de Produção' );
		$sheet->getStyle( 'A1' )
			->getFont()
			->setBold( true );
		$sheet->getStyle( 'A1' )
			->getFont()
			->setSize( 14 );
		$sheet->getStyle( 'A1:J1' )
			->getAlignment()
			->setHorizontal( Alignment::HORIZONTAL_CENTER );
		$sheet->mergeCells( 'A1:J1' );

		$sheet->mergeCells( 'A2:F2' );
		$sheet->mergeCells( 'G2:J2' );
		$sheet->setCellValue( 'A2', 'Período: ' . date( 'd/m/Y', strtotime( $dataInicial ) ) . '  até  ' . date( 'd/m/Y', strtotime( $dataFinal ) ) );
		$sheet->setCellValue( 'G2', 'Data no Sistema: ' . date( 'd/m/Y' ) );
		$sheet->getStyle( 'G2:J2' )
			->getFont()
			->setBold( true );
		$sheet->getStyle( 'A3:F3' )
			->getFont()
			->setBold( true );
		$sheet->mergeCells( 'A3:D3' );
		$sheet->mergeCells( 'E3:F3' );

		$sheet->setCellValue( 'A3', 'Produto/Serviço' );
		$sheet->setCellValue( 'E3', '' );

		$sheet->getStyle( 'A4:J4' )
			->getFont()
			->setBold( true );
		$sheet->mergeCells( 'A4:D4' );
		$sheet->mergeCells( 'E4:F4' );
		$sheet->mergeCells( 'G4:H4' );
		$sheet->mergeCells( 'I4:J4' );

		$sheet->setCellValue( 'A4', 'Descrição' );
		$sheet->setCellValue( 'E4', 'Convênio' );
		$sheet->setCellValue( 'G4', '' );
		$sheet->setCellValue( 'I4', 'Total' );

		$ultima_col = 4;

		$total_exec = 0;
		$total_nexec = 0;
		$total = 0;
		foreach ( $lista as $key => $value ) {
			$ultima_col++;
			$sheet->mergeCells( 'A' . $ultima_col . ':D' . $ultima_col );
			$sheet->mergeCells( 'E' . $ultima_col . ':F' . $ultima_col );
			$sheet->mergeCells( 'G' . $ultima_col . ':H' . $ultima_col );
			$sheet->mergeCells( 'I' . $ultima_col . ':J' . $ultima_col );

			$sheet->getStyle( 'G' . $ultima_col )
				->getAlignment()
				->setHorizontal( Alignment::HORIZONTAL_RIGHT );
			$sheet->getStyle( 'I' . $ultima_col )
				->getAlignment()
				->setHorizontal( Alignment::HORIZONTAL_RIGHT );

			$sheet->setCellValue( 'A' . $ultima_col, $value->getDescricao() );
			$sheet->setCellValue( 'E' . $ultima_col, $value->getConvenio() );
			$sheet->setCellValue( 'G' . $ultima_col, '' );
			$sheet->setCellValue( 'I' . $ultima_col, $value->getTotal() );

			$total_exec += $value->getExecutados();
			$total_nexec += $value->getNaoExecutados();
			$total += $value->getTotal();
		}
		$ultima_col = $ultima_col + 1;
		$sheet->mergeCells( 'A' . $ultima_col . ':D' . $ultima_col );
		$sheet->mergeCells( 'E' . $ultima_col . ':F' . $ultima_col );
		$sheet->mergeCells( 'G' . $ultima_col . ':H' . $ultima_col );
		$sheet->mergeCells( 'I' . $ultima_col . ':J' . $ultima_col );
		$sheet->getStyle( 'E' . $ultima_col )
			->getFont()
			->setBold( true );
		$sheet->getStyle( 'G' . $ultima_col )
			->getAlignment()
			->setHorizontal( Alignment::HORIZONTAL_RIGHT );
		$sheet->getStyle( 'I' . $ultima_col )
			->getAlignment()
			->setHorizontal( Alignment::HORIZONTAL_RIGHT );
		$sheet->setCellValue( 'A' . $ultima_col, 'Total' );
		$sheet->getStyle( 'G' . $ultima_col )
			->getFont()
			->getColor()
			->setARGB( '00000000' );
		$sheet->getStyle( 'I' . $ultima_col )
			->getFont()
			->getColor()
			->setARGB( '00000000' );
		$sheet->setCellValue( 'E' . $ultima_col, '' );
		$sheet->setCellValue( 'G' . $ultima_col, '' );
		$sheet->setCellValue( 'I' . $ultima_col, $total );

		$service_response->setData( $spreadsheet );
		$service_response->setStatus( ServiceResponseInterface::SUCESS );

		return $service_response;

	}

	public function getComandaItemExecutadoPorConvenioParaPdf($dataInicial, $dataFinal, $unidade, $convenio, $execucao): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new ComandaDAO();
		$daoResponse = $dao->getComandaItemExecutadoPorConvenioParaPdf( $dataInicial, $dataFinal, $unidade, $convenio, $execucao );
		$service_response = $this->setServiceResponse( $daoResponse );

		return $service_response;

	}

	public function getComandaItemNaoExecutadoPorConvenioParaPdf($dataInicial, $dataFinal, $unidade, $convenio, $execucao): ServiceResponse {

		$service_response = new ServiceResponse();
		$dao = new ComandaDAO();
		$daoResponse = $dao->getComandaItemNaoExecutadoPorConvenioParaPdf( $dataInicial, $dataFinal, $unidade, $convenio, $execucao );
		$service_response = $this->setServiceResponse( $daoResponse );

		return $service_response;

	}

	private function getListItensExecNaoExec(array $exec, array $nao_exec): array {

		$lista = array ();
		foreach ( $exec as $executados ) {
			$obj = RelatorioComandaItensExectadosNaoExecutado::builder()->setId( intval( $executados->id ) )
				->setDescricao( $executados->descricao )
				->setNaoExecutados( 0 )
				->setExecutados( intval( $executados->total ) )
				->setTotal( intval( $executados->total ) )
				->setConvenio( $executados->convenio_descricao )
				->build();

			foreach ( $nao_exec as $nao_executados ) {
				if (intval( $nao_executados->id ) == $obj->getId()) {
					$obj->setNaoExecutados( intval( $nao_executados->total ) );
					$obj->setTotal( intval( $nao_executados->total ) + $obj->getExecutados() );
					break;
				}
			}

			array_push( $lista, $obj );
		}

		foreach ( $nao_exec as $nao_executados ) {

			$obj = RelatorioComandaItensExectadosNaoExecutado::builder()->setId( intval( $nao_executados->id ) )
				->setDescricao( $nao_executados->descricao )
				->setNaoExecutados( intval( $nao_executados->total ) )
				->setExecutados( 0 )
				->setTotal( intval( $nao_executados->total ) )
				->setConvenio( $nao_executados->convenio_descricao )
				->build();
			$incluir = 1;
			foreach ( $exec as $executados ) {
				if (intval( $executados->id ) == $obj->getId()) {
					$incluir = 0;
					break;
				}
			}

			if ($incluir == 1) {
				array_push( $lista, $obj );
			}
		}

		return $lista;

	}

}