connection = new Mysql(); } /** Carrega os campos de uma tabela */ public function Describe() { /** Consulta SQL */ $this->sql = "describe financial_movements"; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ $this->field = $this->stmt->fetchAll(\PDO::FETCH_OBJ); /** Declara o objeto */ $resultDescribe = new \stdClass(); $Field = ''; /** Lista os campos da tabela para objetos */ foreach($this->field as $UsersKey => $Result){ /** Pega o nome do Field/Campo */ $Field = $Result->Field; /** Carrega os objetos como null */ $resultDescribe->$Field = null; } /** Retorna os campos declarados como vazios */ return $resultDescribe; } /** Verifica o total de saídas pendentes */ public function amountOutput() { /** Consulta SQL */ $this->sql = 'select count(financial_movements_id) as amount_output, sum(movement_value) as total_value_output from financial_movements where financial_outputs_id > 0 and movement_date_paid is null'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Verifica o total de entradas pendentes */ public function amountEntrie($movementDatePaid, $delay) { /** Parametros de entrada */ $this->movementDatePaid = $movementDatePaid; $this->delay = $delay; $this->and = $this->movementDatePaid == true ? ' and movement_date_paid is null ' : ' and movement_date_paid is not null '; $this->and .= $this->delay > 0 ? ' and movement_date_scheduled = date_format(current_date()+ '.$delay.', \'%Y-%m-%d\')' : ''; /** Consulta SQL */ $this->sql = 'select count(financial_movements_id) as amount_entrie, sum(movement_value) as total_value_entrie, sum(movement_value_fees) as total_value_entrie_fees from financial_movements where financial_entries_id > 0 '; /** Aplica o filtro de pago ou não */ $this->sql .= $this->and; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Consulta uma movimentação por um período informado */ public function searchDateEntrie(string $dateStart, string $dateEnd) { /** Parametros de entrada */ $this->dateStart = $dateStart; $this->dateEnd = $dateEnd; /** Consulta SQL */ $this->sql = 'select sum(movement_value_paid) as total_paid from financial_movements where financial_entries_id > 0 and movement_date_paid between :date_start and :date_end and movement_date_paid is not null;'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':date_start', $this->dateStart); $this->stmt->bindParam(':date_end', $this->dateEnd); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Consulta as movimentações que irão vencer nos próximos 5 dias */ public function checkDelay(int $companyId, int $delay) { /** Parametros de entrada */ $this->companyId = $companyId; $this->delay = $delay; /** Consulta SQL */ $this->sql = 'select fm.financial_movements_id, fm.financial_accounts_id, fm.financial_entries_id, fm.financial_outputs_id, fm.user_id, fm.company_id, fm.description, fm.movement_value, fm.movement_value_paid, fm.movement_value_fees, fm.movement_date, fm.movement_date_scheduled, fm.movement_date_paid, fm.reference as movement_reference, fm.status, c.reference, c.name_fantasy, c.contract_type, u.name_first, u.name_last, u.email from financial_movements fm left join companies c on fm.company_id = c.company_id left join users u on c.company_id = u.company_id where fm.company_id = :company_id and fm.movement_date_scheduled = date_format(current_date()+ '.$delay.', \'%Y-%m-%d\')'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':company_id', $this->companyId); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchAll(\PDO::FETCH_OBJ); } /** Consulta as entradas de uma empresa pelas categorias */ public function searchEntriesCategories(string $dateStart, string $dateEnd) { /** Parametros de entrada */ $this->dateStart = $dateStart; $this->dateEnd = $dateEnd; /** Consulta SQL */ $this->sql = 'select distinct(fm.financial_entries_id), fe.financial_categories_id, (select fc.description from financial_categories fc where fc.financial_categories_id = fe.financial_categories_id) as categorie, (select count(fm2.financial_entries_id) from financial_movements fm2 where fm2.financial_entries_id = fm.financial_entries_id and fm2.movement_date_paid between :date_start and :date_end) as total from financial_movements fm left join financial_entries fe on fm.financial_entries_id = fe.financial_entries_id where fm.financial_entries_id > 0 and fm.movement_date_paid between :date_start and :date_end order by fm.financial_entries_id asc '; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':date_start', $this->dateStart); $this->stmt->bindParam(':date_end', $this->dateEnd); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchAll(\PDO::FETCH_OBJ); } /** Localiza os registros de um determinado orçamento */ public function GetBudgets(int $companiesBudgetsId) { /** Parametros de entrada */ $this->companiesBudgetsId = $companiesBudgetsId; /** Consulta SQL */ $this->sql = 'select fm.financial_movements_id, fm.financial_accounts_id, fm.financial_entries_id, fm.financial_outputs_id, fm.user_id, fm.company_id, fm.description, fm.movement_value, fm.movement_value_paid, fm.movement_value_fees, fm.movement_date, fm.movement_date_scheduled, fm.movement_date_paid, fm.status, fm.note, fm.movement_user_confirmed, c.cns as reference_client, fc.reference from financial_movements fm left join companies c on fm.company_id = c.company_id left join companies_budgets cb on fm.companies_budgets_id = cb.companies_budgets_id left join financial_categories fc on cb.financial_categories_id = fc.financial_categories_id where fm.companies_budgets_id = :companies_budgets_id and fm.status < 3'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':companies_budgets_id', $this->companiesBudgetsId); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchAll(\PDO::FETCH_OBJ); } /** Localiza os registros de um determinado arquivo de retorno pelo número do documento*/ public function SearchByDocumentNumber(string $reference, ? string $movementDateScheduled) { /** Parametros de entrada */ $this->reference = $reference; $this->movementDateScheduled = $movementDateScheduled; /** Se a data de vencimento tiver sido informada, localiza o registro para data de vencimento original */ if(!empty($this->movementDateScheduled)){ $this->and = ' and ((select fmp.movement_date_scheduled from financial_movements fmp where fmp.financial_movements_id = fm.movement_previous) = :movement_date_scheduled or fm.movement_date_scheduled = :movement_date_scheduled)'; }; /** Consulta SQL */ $this->sql = 'select fm.financial_movements_id, fm.financial_accounts_id, fm.financial_entries_id, fm.financial_outputs_id, fm.financial_consolidations_id, fm.user_id, fm.company_id, fm.description, fm.movement_value, fm.movement_value_paid, fm.movement_value_fees, fm.movement_date, fm.movement_date_scheduled, fm.movement_date_paid, fm.status, fm.note, fm.movement_user_confirmed, fm.reference, fm.sicoob_response, c.name_fantasy, c.cnpj as document, c.reference as client_reference, c.responsible, fc.import_date, u.name_first, u.name_last, u.email from financial_movements fm left join companies c on fm.company_id = c.company_id left join financial_consolidations fc on fm.financial_consolidations_id = fc.financial_consolidations_id left join users u on fc.user_id = u.user_id where fm.reference = :reference'; /** Verifica se a data de vencimento foi informada */ if(!empty($this->and)){ $this->sql .= $this->and; } /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':reference', $this->reference); /** Verifica se a data de vencimento foi informada */ if(!empty($this->and)){ $this->stmt->bindParam(':movement_date_scheduled', $this->movementDateScheduled); } /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Pesquisa por débito de um determinado cliente via código CNS */ public function SearchDebit(string $cns) { /** Parametros de entrada */ $this->cns = $cns; /** Consulta SQL */ $this->sql = '( -- Registros em atraso SELECT fm.*, DATEDIFF(CURRENT_DATE, fm.movement_date_scheduled) AS days_passed, \'em_atraso\' AS status_registro FROM financial_movements fm INNER JOIN companies c ON fm.company_id = c.company_id WHERE c.cns = :cns AND fm.movement_date_paid IS NULL AND fm.movement_date_scheduled <= CURRENT_DATE ) UNION ( -- Próximo vencimento (caso não haja atrasados) SELECT fm.*, DATEDIFF(CURRENT_DATE, fm.movement_date_scheduled) AS days_passed, \'proximo_vencimento\' AS status_registro FROM financial_movements fm INNER JOIN companies c ON fm.company_id = c.company_id WHERE c.cns = :cns AND fm.movement_date_paid IS NULL AND fm.movement_date_scheduled > CURRENT_DATE ORDER BY fm.movement_date_scheduled ASC LIMIT 1 );'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':cns', $this->cns); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchAll(\PDO::FETCH_OBJ); } /** Localiza um registro especifico */ public function Get(int $financialMovementsId) { /** Parametros de entrada */ $this->financialMovementsId = $financialMovementsId; /** Consulta SQL */ $this->sql = 'select fm.financial_movements_id, fm.financial_accounts_id, fm.financial_entries_id, fm.financial_outputs_id, fm.user_id, fm.company_id, fm.description, fm.movement_value, fm.movement_value_paid, fm.movement_value_fees, fm.movement_date, fm.movement_date_scheduled, fm.movement_date_paid, fm.status, fm.note, fm.movement_user_confirmed, fm.reference, fm.sicoob_response, u.name_first, u.name_last, (select name from users where user_id = fm.movement_user_confirmed) as user_confirmed_name, c.name_business, c.cnpj as document, c.name_fantasy, c.cep, c.adress, c.number, c.complement, c.district, (select name from cities where city_id = c.city_id limit 1) as city, (select name from states where city_id = c.city_id limit 1) as state, c.email from financial_movements fm left join users u on fm.user_id = u.user_id left join companies c on fm.company_id = c.company_id where fm.financial_movements_id = :financial_movements_id'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':financial_movements_id', $this->financialMovementsId); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Localiza um registro especifico */ public function GetConsolidated(int $financialConsolidationsId) { /** Parametros de entrada */ $this->financialConsolidationsId = $financialConsolidationsId; /** Consulta SQL */ $this->sql = 'select fm.financial_movements_id, fm.financial_accounts_id, fm.financial_entries_id, fm.financial_outputs_id, fm.financial_consolidations_id, fm.user_id, fm.company_id, fm.companies_budgets_id, fm.user_id_update, fm.description, fm.movement_value, fm.movement_value_paid, fm.movement_value_fees, fm.movement_date, fm.movement_date_scheduled, fm.movement_date_maturity, fm.movement_date_paid, fm.movement_date_update, fm.movement_date_cancel, fm.status, fm.note, fm.movement_user_confirmed, fm.reference, fm.print, fm.movement_previous, fm.sicoob_response, c.name_fantasy from financial_movements fm left join companies c on fm.company_id = c.company_id where fm.financial_consolidations_id = :financial_consolidations_id'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':financial_consolidations_id', $this->financialConsolidationsId); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchAll(\PDO::FETCH_OBJ); } /** Localiza um registro especifico */ public function GetReference(string $reference) { /** Parametros de entrada */ $this->reference = $reference; /** Consulta SQL */ $this->sql = 'select fm.financial_movements_id, fm.financial_accounts_id, fm.financial_entries_id, fm.financial_outputs_id, fm.user_id, fm.company_id, fm.description, fm.movement_value, fm.movement_value_paid, fm.movement_value_fees, fm.movement_date, fm.movement_date_scheduled, fm.movement_date_paid, fm.status, fm.note, fm.movement_user_confirmed, fm.reference, fm.sicoob_response, c.name_fantasy from financial_movements fm left join companies c on fm.company_id = c.company_id where fm.reference = :reference'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':reference', $this->reference); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Atualiza a referencia de uma movimentação */ public function UpdateReference(int $financialMovementsId, string $reference) { /** Parametros de entrada */ $this->financialMovementsId = $financialMovementsId; $this->reference = $reference; /** Consulta SQL */ $this->sql = 'update financial_movements set reference = :reference where financial_movements_id = :financial_movements_id'; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('reference', $this->reference); $this->stmt->bindParam('financial_movements_id', $this->financialMovementsId); /** Executo o SQL */ return $this->stmt->execute(); } /** Atualiza o retorno junto a Sicoob */ public function UpdateResponseSicoob(int $financialMovementsId, string $response) { /** Parametros de entrada */ $this->financialMovementsId = $financialMovementsId; $this->response = $response; /** Consulta SQL */ $this->sql = 'update financial_movements set sicoob_response = :response where financial_movements_id = :financial_movements_id'; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('response', $this->response); $this->stmt->bindParam('financial_movements_id', $this->financialMovementsId); /** Executo o SQL */ return $this->stmt->execute(); } /** Lista as movimentações que não possuem referência informada */ public function NoReference(int $companyId, string $description) { /** Parametros de entrada */ $this->companyId = $companyId; $this->description = $description; /** Consulta SQL */ $this->sql = 'select fm.financial_movements_id, fm.financial_accounts_id, fm.financial_entries_id, fm.financial_outputs_id, fm.user_id, fm.company_id, fm.description, fm.movement_value, fm.movement_value_paid, fm.movement_value_fees, fm.movement_date, fm.movement_date_scheduled, fm.movement_date_paid, fm.status, fm.reference, c.reference, c.name_fantasy, c.contract_type from financial_movements fm left join companies c on fm.company_id = c.company_id where fm.company_id = :company_id and fm.description like concat(\'%\', :description, \'%\')'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('company_id', $this->companyId);/** Informa a qual empresa pertence o cliente */ $this->stmt->bindParam('description', $this->description); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchAll(\PDO::FETCH_OBJ); } /** Lista todos os egistros do banco com ou sem paginação*/ public function All(int $companyId, int $start, int $max, string $search, string $type, int $status, string $dateStart, string $dateEnd) { /** Parametros de entrada */ $this->companyId = $companyId; $this->start = $start; $this->max = $max; $this->search = $search; $this->type = $type; $this->status = $status; $this->dateStart = $dateStart; $this->dateEnd = $dateEnd; $this->and = ''; /** Consulta SQL */ $this->sql = 'select fm.financial_movements_id, fm.financial_accounts_id, fm.financial_entries_id, fm.financial_outputs_id, fm.user_id, fm.company_id, fm.description, fm.movement_value, fm.movement_value_paid, fm.movement_value_fees, fm.movement_date, fm.movement_date_scheduled, fm.movement_date_paid, fm.reference as movement_reference, fm.status, c.reference, c.cns, c.name_fantasy, c.contract_type, (select fmn.notification_date from financial_movements_notify fmn where fmn.financial_movements_id = fm.financial_movements_id order by fmn.financial_movements_notify_id desc limit 0, 1) as notification_date, (select fmn.message from financial_movements_notify fmn where fmn.financial_movements_id = fm.financial_movements_id order by fmn.financial_movements_notify_id desc limit 0, 1) as message, cbc.companies_budgets_commissions_id, cbc.value, cbc.commission_date_paid, u.name_first, u.name_last from financial_movements fm left join companies c on fm.company_id = c.company_id left join companies_budgets_commissions cbc on fm.financial_movements_id = cbc.financial_movements_id left join users u on cbc.user_id = u.user_id where fm.movement_date_cancel is null '; /** verifica se a consulta será pela data de pagamento */ if($this->status == 2){ $this->and .= ' and fm.movement_date_paid is not null '; } elseif($this->status == 1){ $this->and .= ' and fm.movement_date_paid is null '; } /** Verifica se existem filtros a serem aplicados */ if(!empty($this->search)){ /** Verifica se é uma consulta por número */ if( (int)$this->search > 0 ){ $this->and .= ' and c.cns = :cns '; } else { $this->and .= ' and fm.description like concat(\'%\', :description, \'%\')'; $this->and .= ' or c.cns like concat(\'%\', :cns, \'%\')'; $this->and .= ' or c.name_fantasy like concat(\'%\', :fantasy_name, \'%\')'; $this->and .= ' or fm.reference = :movement_reference'; } } /** Verifica o tipo de movimentação */ if(!empty($this->type)){ $this->and .= ' and '.($this->type == 'E' ? 'financial_entries_id > 0 ' : 'financial_outputs_id > 0 '); } /** Período de consulta */ if(!empty($this->dateStart) && !empty($this->dateEnd)){ /** verifica se a consulta será pela data de pagamento */ if($this->status == 2){ $this->between = ' and fm.movement_date_paid between :date_start and :date_end '; } else { $this->between = ' and fm.movement_date_scheduled between :date_start and :date_end '; } } /** Verifico se há paginação */ if($this->max > 0){ $this->limit = ' limit '.$this->start.', '.$this->max; } /** Informe os filtros informados */ $this->sql .= $this->and; /** Informe o periodo entre datas */ $this->sql .= $this->between; /** Ordenação */ $this->sql .= ' order by fm.movement_date_scheduled asc '; /** Informa a paginação */ $this->sql .= $this->limit; //exit; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Verifica se existem filtros a serem aplicados */ if(!empty($this->search)){ if( (int)$this->search > 0 ){ $this->stmt->bindParam('cns', $this->search); } else { $this->stmt->bindParam('description', $this->search); $this->stmt->bindParam('cns', $this->search); $this->stmt->bindParam('fantasy_name', $this->search); $this->stmt->bindParam('movement_reference', $this->search); } } /** Período de consulta */ if(!empty($this->dateStart) && !empty($this->dateEnd)){ /** Período de consulta */ $this->stmt->bindParam('date_start', $this->dateStart); $this->stmt->bindParam('date_end', $this->dateEnd); } /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchAll(\PDO::FETCH_OBJ); } /** Conta a quantidades de registros */ public function Count(int $companyId, string $search, string $type, int $status, string $dateStart, string $dateEnd) { /** Parametros de entrada */ $this->companyId = $companyId; $this->search = $search; $this->type = $type; $this->status = $status; $this->dateStart = $dateStart; $this->dateEnd = $dateEnd; /** Consulta SQL */ $this->sql = 'select count(distinct(fm.financial_movements_id)) as qtde from financial_movements fm left join companies c on fm.company_id = c.company_id where fm.movement_date_cancel is null'; /** verifica se a consulta será pela data de pagamento */ if($this->status == 2){ $this->and .= ' and fm.movement_date_paid is not null '; } elseif($this->status == 1){ $this->and .= ' and fm.movement_date_paid is null '; } /** Verifica se existem filtros a serem aplicados */ if(!empty($this->search)){ /** Verifica se é uma consulta por número */ if( (int)$this->search > 0 ){ $this->and .= ' and c.cns = :cns '; } else { $this->and .= ' and fm.description like concat(\'%\', :description, \'%\')'; $this->and .= ' or c.cns like concat(\'%\', :cns, \'%\')'; $this->and .= ' or c.name_fantasy like concat(\'%\', :fantasy_name, \'%\')'; $this->and .= ' or fm.reference = :movement_reference'; } } /** Verifica o tipo de movimentação */ if(!empty($this->type)){ $this->and .= ' and '.($this->type == 'E' ? 'financial_entries_id > 0 ' : 'financial_outputs_id > 0 '); } /** Período de consulta */ if(!empty($this->dateStart) && !empty($this->dateEnd)){ /** verifica se a consulta será pela data de pagamento */ if($this->status == 2){ $this->between = ' and fm.movement_date_paid between :date_start and :date_end '; } else { $this->between = ' and fm.movement_date_scheduled between :date_start and :date_end '; } } /** Informe os filtros informados */ $this->sql .= $this->and; /** Informe o periodo entre datas */ echo $this->sql .= $this->between; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Verifica se existem filtros a serem aplicados */ if(!empty($this->search)){ if( (int)$this->search > 0 ){ $this->stmt->bindParam('cns', $this->search); } else { $this->stmt->bindParam('description', $this->search); $this->stmt->bindParam('cns', $this->search); $this->stmt->bindParam('fantasy_name', $this->search); $this->stmt->bindParam('movement_reference', $this->search); } } /** Período de consulta */ if(!empty($this->dateStart) && !empty($this->dateEnd)){ /** Período de consulta */ $this->stmt->bindParam('date_start', $this->dateStart); $this->stmt->bindParam('date_end', $this->dateEnd); } /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Conta a quantidades de registros não pagos */ public function CountNotPaid() { /** Consulta SQL */ $this->sql = 'SELECT COUNT(fm.financial_movements_id) AS qtde FROM financial_movements fm WHERE fm.financial_entries_id > 0 AND fm.sicoob_response IS NOT NULL AND fm.movement_date_paid IS NULL AND fm.status = 1 AND fm.movement_date_scheduled < CURRENT_DATE'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Lista todos os registros do banco com ou sem paginação*/ public function AllNotPaid() { /** Consulta SQL */ $this->sql = 'SELECT fm.financial_movements_id, fm.financial_accounts_id, fm.financial_entries_id, fm.financial_outputs_id, fm.user_id, fm.company_id, fm.description, fm.movement_value, fm.movement_value_paid, fm.movement_value_fees, fm.movement_date, fm.movement_date_scheduled, fm.movement_date_paid, fm.reference as movement_reference, fm.status, fm.sicoob_response, c.cns AS reference, c.nickname, c.contract_type FROM financial_movements fm LEFT JOIN companies c ON fm.company_id = c.company_id WHERE fm.financial_entries_id > 0 AND fm.sicoob_response IS NOT NULL AND fm.movement_date_paid IS NULL AND fm.status = 1 AND fm.movement_date_scheduled < CURRENT_DATE ORDER BY fm.movement_date_scheduled ASC'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchAll(\PDO::FETCH_OBJ); } /** Conta a quantidades de registros não pagos */ public function CountNotify(int $companyId, string $financialMovementsId) { /** Parametros de entrada */ $this->companyId = $companyId; $this->financialMovementsId = $financialMovementsId; /** Consulta SQL */ $this->sql = 'SELECT count(fm.financial_movements_id) as qtde FROM financial_movements fm WHERE fm.company_id = :company_id and fm.movement_date_paid is null and fm.status = 1 and fm.financial_movements_id in('.$this->financialMovementsId.')'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('company_id', $this->companyId);/** Informa a qual empresa pertence o cliente */ /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Lista todos os registros do banco com sem paginação*/ public function Notify(int $companyId, string $financialMovementsId) { /** Parametros de entrada */ $this->companyId = $companyId; $this->financialMovementsId = $financialMovementsId; /** Consulta SQL */ $this->sql = 'select fm.financial_movements_id, fm.financial_accounts_id, fm.financial_entries_id, fm.financial_outputs_id, fm.user_id, fm.company_id, fm.description, fm.movement_value, fm.movement_value_paid, fm.movement_value_fees, fm.movement_date, fm.movement_date_scheduled, fm.movement_date_paid, fm.reference as movement_reference, fm.status, fm.sicoob_response, c.reference, c.name_fantasy, c.contract_type from financial_movements fm left join companies c on fm.company_id = c.company_id WHERE fm.company_id = :company_id and fm.movement_date_paid is null and fm.status = 1 and fm.financial_movements_id in('.$this->financialMovementsId.') order by fm.movement_date_scheduled asc'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('company_id', $this->companyId);/** Informa a qual empresa pertence o cliente */ /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchAll(\PDO::FETCH_OBJ); } /** Insere um novo registro no banco */ public function InsertMovements(int $financialAccountsId, int $financialEntriesId, int $financialOutputsId, int $companyId, float $movementValue, string $movementDateScheduled, ? string $reference, ? string $description) { /** Parametros */ $this->financialAccountsId = $financialAccountsId; $this->financialEntriesId = $financialEntriesId; $this->financialOutputsId = $financialOutputsId; $this->companyId = $companyId; $this->movementValue = $movementValue; $this->movementDateScheduled = $movementDateScheduled; $this->reference = $reference; $this->description = $description; /** Consulta SQL */ $this->sql = 'insert into financial_movements(financial_accounts_id, financial_entries_id, financial_outputs_id, user_id, company_id, movement_value, movement_date_scheduled, reference, description ) values (:financial_accounts_id, :financial_entries_id, :financial_outputs_id, :user_id, :company_id, :movement_value, :movement_date_scheduled, :reference, :description)'; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('user_id', $_SESSION['USERSID']);/** Informa o usuário responsável pelo novo cliente cadastrado */ $this->stmt->bindParam('company_id', $_SESSION['USERSCOMPANYID']);/** Informa a qual empresa pertence o cliente */ $this->stmt->bindParam('financial_accounts_id', $this->financialAccountsId); $this->stmt->bindParam('financial_entries_id', $this->financialEntriesId); $this->stmt->bindParam('financial_outputs_id', $this->financialOutputsId); $this->stmt->bindParam('movement_value', $this->movementValue); $this->stmt->bindParam('movement_date_scheduled', $this->movementDateScheduled); $this->stmt->bindParam('reference', $this->reference); $this->stmt->bindParam('description', $this->description); /** Executo o SQL */ return $this->stmt->execute(); } /** Atualiza uma entrada de um orçamento especifico */ public function SaveMovementBudgets(int $financialMovementsId, int $financialEntriesId, string $movementDateScheduled, float $movementValue, string $description, string $reference) { /** Parametros de entrada */ $this->financialMovementsId = $financialMovementsId; $this->financialEntriesId = $financialEntriesId; $this->movementDateScheduled = $movementDateScheduled; $this->movementValue = $movementValue; $this->description = $description; $this->reference = $reference; /** Consulta SQL */ $this->sql = "update financial_movements set movement_date_scheduled = :movement_date_scheduled, movement_value = :movement_value, movement_date_update = CURRENT_TIMESTAMP, user_id_update = :user_id_update, description = :description, reference = :reference where financial_movements_id = :financial_movements_id and financial_entries_id = :financial_entries_id and movement_date_paid is null and status = 1"; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('movement_date_scheduled', $this->movementDateScheduled); $this->stmt->bindParam('movement_value', $this->movementValue); $this->stmt->bindParam('description', $this->description); $this->stmt->bindParam('reference', $this->reference); $this->stmt->bindParam('user_id_update', $_SESSION['USERSID']);/** Informa o usuário responsável pela movimentação cadastrada */ $this->stmt->bindParam('financial_movements_id', $this->financialMovementsId); $this->stmt->bindParam('financial_entries_id', $this->financialEntriesId); /** Adiciona o id da entrada */ $this->stmt->bindParam('financial_entries_id', $this->financialEntriesId); /** Executo o SQL */ return $this->stmt->execute(); } /** Atualiza uma saída/entrada */ public function SaveMovement(int $financialMovementsId, int $financialOutputsId, int $financialEntriesId, string $movementDatePaid, float $movementValuePaid, string $note, ? float $movementValueFees, ? float $movementValueRegistrationTariff, ? float $movementValueSettlementTariff) { /** Parametros de entrada */ $this->financialMovementsId = $financialMovementsId; $this->financialOutputsId = $financialOutputsId; $this->financialEntriesId = $financialEntriesId; $this->movementDatePaid = $movementDatePaid; $this->movementValuePaid = $movementValuePaid; $this->note = $note; $this->movementValueFees = $movementValueFees; $this->movementValueRegistrationTariff = $movementValueRegistrationTariff; $this->movementValueSettlementTariff = $movementValueSettlementTariff; /** Consulta SQL */ $this->sql = "update financial_movements set movement_date_paid = :movement_date_paid, movement_value_paid = :movement_value_paid, note = :note, movement_user_confirmed = :movement_user_confirmed, movement_value_fees = :movement_value_fees, movement_value_registration_tariff = :movement_value_registration_tariff, movement_value_settlement_tariff = :movement_value_settlement_tariff, status = 2 where financial_movements_id = :financial_movements_id "; /** Verifica se é uma entrada*/ if($this->financialEntriesId > 0){ $this->sql .= " and financial_entries_id = :financial_entries_id"; /** Verifica se é uma saída */ }elseif($this->financialOutputsId > 0){ $this->sql .= " and financial_outputs_id = :financial_outputs_id"; } /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('movement_date_paid', $this->movementDatePaid); $this->stmt->bindParam('movement_value_paid', $this->movementValuePaid); $this->stmt->bindParam('movement_value_fees', $this->movementValueFees); $this->stmt->bindParam('movement_value_registration_tariff', $this->movementValueRegistrationTariff); $this->stmt->bindParam('movement_value_settlement_tariff', $this->movementValueSettlementTariff); $this->stmt->bindParam('note', $this->note); $this->stmt->bindParam('movement_user_confirmed', $_SESSION['USERSID']);/** Informa o usuário responsável pela movimentação cadastrada */ $this->stmt->bindParam('financial_movements_id', $this->financialMovementsId); /** Verifica se é uma entrada*/ if($this->financialEntriesId > 0){ /** Adiciona o id da entrada */ $this->stmt->bindParam('financial_entries_id', $this->financialEntriesId); /** Verifica se é uma saída */ }elseif($this->financialOutputsId > 0){ /** Adiciona o id da saída */ $this->stmt->bindParam('financial_outputs_id', $this->financialOutputsId); } /** Executo o SQL */ return $this->stmt->execute(); } /** Atualiza a consolidação do item */ public function updateConsolidatedItem(int $financialMovementsId, int $financialConsolidationsId, ? float $movementValueFees, float $movementValuePaid, string $movementDatePaid, string $note, ? float $valueRegistrationTariff, ? float $valueSettlementTariff) { /** Parametros de entrada */ $this->financialMovementsId = $financialMovementsId; $this->financialConsolidationsId = $financialConsolidationsId; $this->movementValueFees = $movementValueFees; $this->movementValuePaid = $movementValuePaid; $this->movementDatePaid = $movementDatePaid; $this->note = $note; $this->valueRegistrationTariff = $valueRegistrationTariff; $this->valueSettlementTariff = $valueSettlementTariff; /** Consulta SQL */ $this->sql = 'update financial_movements set financial_consolidations_id = :financial_consolidations_id, movement_value_fees = :movement_value_fees, movement_value_paid = :movement_value_paid, movement_date_paid = :movement_date_paid, note = :note, status = 2, movement_value_registration_tariff = :movement_value_registration_tariff, movement_value_settlement_tariff = :movement_value_settlement_tariff where financial_movements_id = :financial_movements_id '; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('financial_consolidations_id', $this->financialConsolidationsId); $this->stmt->bindParam('movement_value_fees', $this->movementValueFees); $this->stmt->bindParam('movement_value_paid', $this->movementValuePaid); $this->stmt->bindParam('movement_date_paid', $this->movementDatePaid); $this->stmt->bindParam('note', $this->note); $this->stmt->bindParam('financial_movements_id', $this->financialMovementsId); $this->stmt->bindParam('movement_value_registration_tariff', $this->valueRegistrationTariff); $this->stmt->bindParam('movement_value_settlement_tariff', $this->valueSettlementTariff); /** Executo o SQL */ return $this->stmt->execute(); } /** Atualiza o valor da saída/entrada */ public function SaveMovementValue(int $financialMovementsId, float $movementValuePaid) { /** Parametros de entrada */ $this->financialMovementsId = $financialMovementsId; $this->movementValuePaid = $movementValuePaid; /** Consulta SQL */ $this->sql = "update financial_movements set movement_value = :movement_value where financial_movements_id = :financial_movements_id "; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('movement_value', $this->movementValuePaid); $this->stmt->bindParam('financial_movements_id', $this->financialMovementsId); /** Executo o SQL */ return $this->stmt->execute(); } /** Atualiza o nosso número Sicoob */ public function SaveOurNumber(string $ourNumber, int $financialMovementsId) { /** Parametros de entrada */ $this->ourNumber = $ourNumber; $this->financialMovementsId = $financialMovementsId; /** Consulta SQL */ $this->sql = "update financial_movements set sicoob_response = :sicoob_response where financial_movements_id = :financial_movements_id "; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('sicoob_response', $this->ourNumber); $this->stmt->bindParam('financial_movements_id', $this->financialMovementsId); /** Executo o SQL */ return $this->stmt->execute(); } /** Deleta um determinado registro no banco de dados */ public function DeleteMovements(int $financialEntriesId) { /** Parametros de entrada */ $this->financialEntriesId = $financialEntriesId; /** Consulta SQL */ $this->sql = 'delete from financial_movements where financial_entries_id = :financial_entries_id'; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('financial_entries_id', $this->financialEntriesId); /** Executo o SQL */ return $this->stmt->execute(); } /** Retorna o valor total a partir de uma data inicial e final */ public function SumMOnth(int $company_id, string $dateStart, string $dateEnd, string $type) { /** Parametros de entrada */ $this->companyId = $company_id; $this->dateStart = $dateStart; $this->dateEnd = $dateEnd; $this->type = $type; $this->and = $type == 'O' ? ' and financial_outputs_id > 0 ' : ' and financial_entries_id > 0 '; /** Consulta SQL */ $this->sql = 'select sum(fm.movement_value) as total , sum(fm.movement_value_paid+fm.movement_value_fees) as total_received from financial_movements fm where fm.company_id = :company_id '.$this->and.' and fm.movement_date_scheduled between \''.$this->dateStart.'\' and \''.$this->dateEnd.'\''; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('company_id', $this->companyId);/** Informa a qual empresa pertence a movimentação */ /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Consulta os arquivos de um determinado movimento financeiro */ public function loadFiles(int $financialMovementsId) { /** Parametros de entrada */ $this->financialMovementsId = $financialMovementsId; /** Consulta SQL */ $this->sql = 'select documents_id, documents_drafts_id, documents_categorys_id, user_id, company_id, financial_movements_id, description, date_register, archive, extension, active, tag from documents where financial_movements_id = :financial_movements_id'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':financial_movements_id', $this->financialMovementsId); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchAll(\PDO::FETCH_OBJ); } /** Conta a quantidade de registros de saídas em atraso */ public function OutputsNotPaidCount() { /** Consulta SQL */ $this->sql = 'SELECT count(fm.financial_movements_id) as qtde FROM financial_movements fm LEFT JOIN companies c ON fm.company_id = c.company_id WHERE fm.financial_outputs_id > 0 AND fm.movement_date_paid IS NULL AND fm.status = 1 AND fm.movement_date_scheduled <= CURRENT_DATE + INTERVAL 7 DAY -- até hoje + 7 dias ORDER BY fm.movement_date_scheduled ASC;'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject()->qtde; } /** Lista os registros de saídas em atraso */ public function OutputsNotPaidAll() { /** Consulta SQL */ $this->sql = 'SELECT fm.financial_movements_id, fm.description as output, fm.movement_date_scheduled, fm.movement_value FROM financial_movements fm LEFT JOIN companies c ON fm.company_id = c.company_id WHERE fm.financial_outputs_id > 0 AND fm.movement_date_paid IS NULL AND fm.status = 1 AND fm.movement_date_scheduled <= CURRENT_DATE + INTERVAL 7 DAY -- até hoje + 7 dias ORDER BY fm.movement_date_scheduled ASC;'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchAll(\PDO::FETCH_OBJ); } /** Fecha uma conexão aberta anteriormente com o banco de dados */ function __destruct() { $this->connection = null; } }