connection = new Mysql(); } /** Carrega os campos de uma tabela */ public function Describe() { /** Consulta SQL */ $this->sql = "describe companies_budgets"; /** 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 $userKey => $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; } /** Lista os registros do banco de dados com limitação */ public function Get(int $companiesBudgetsId) { /** Parametros de entrada */ $this->companiesBudgetsId = $companiesBudgetsId; /** Consulta SQL */ $this->sql = 'select cb.companies_budgets_id, cb.company_id, cb.user_id, cb.user_id_update, cb.financial_categories_id, cb.financial_accounts_id, cb.budget, cb.date_create, cb.date_update, cb.day_due, cb.readjustment_date, cb.readjustment_index, cb.readjustment_value, cb.readjustment_budget, cb.readjustment_type, cb.readjustment_year, cb.readjustment_month, cb.often, cb.date_start, cb.description, cb.products_id, c.name_fantasy from companies_budgets cb left join companies c on cb.company_id = c.company_id where cb.companies_budgets_id = :companies_budgets_id'; /** 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->fetchObject(); } /** Lista todos os egistros do banco com ou sem paginação*/ public function All(int $companyId) { /** Parametros de entrada */ $this->companyId = $companyId; /** Consulta SQL */ $this->sql = 'select cb.companies_budgets_id, cb.company_id, cb.user_id, cb.products_id, cb.budget, cb.date_create, cb.day_due, cb.readjustment_date, cb.readjustment_index, cb.readjustment_value, cb.readjustment_budget, cb.readjustment_type, cb.readjustment_year, cb.readjustment_month, cb.often, cb.date_start, cb.description, u.name as responsible from companies_budgets cb left join users u on cb.user_id = u.user_id where cb.company_id = :company_id'; /** 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); } /** Conta a quantidades de registros */ public function Count(int $companyId) { /** Parametros de entrada */ $this->companyId = $companyId; /** Consulta SQL */ $this->sql = 'select count(companies_budgets_id) as qtde from companies_budgets where company_id = :company_id'; /** 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->fetchObject()->qtde; } /** Insere um novo registro no banco */ public function Save(int $companiesBudgetsId, int $companyId, string $budget, int $dayDue, string $readjustmentIndex, string $readjustmentValue, string $readjustmentBudget, int $readjustmentType, int $readjustmentYear, int $readjustmentMonth, int $often, string $dateStart, string $description, int $financialCategoriesId, int $financialAccountsId, string $productsId) { /** Parametros */ $this->companiesBudgetsId = $companiesBudgetsId; $this->companyId = $companyId; $this->userId = $_SESSION['USERSID'];//Carrega o ID do usuário logado $this->budget = $budget; $this->dayDue = $dayDue; $this->readjustmentIndex = $readjustmentIndex; $this->readjustmentValue = $readjustmentValue; $this->readjustmentBudget = $readjustmentBudget; $this->readjustmentType = $readjustmentType; $this->readjustmentYear = $readjustmentYear; $this->readjustmentMonth = $readjustmentMonth; $this->often = $often; $this->dateStart = $dateStart; $this->description = $description; $this->financialCategoriesId = $financialCategoriesId; $this->financialAccountsId = $financialAccountsId; $this->productsId = $productsId; /** Verifica se o ID do registro foi informado */ if($this->companiesBudgetsId > 0){ /** Consulta SQL */ $this->sql = 'update companies_budgets set budget = :budget, day_due = :day_due, readjustment_index = :readjustment_index, readjustment_value = :readjustment_value, readjustment_budget = :readjustment_budget, readjustment_type = :readjustment_type, readjustment_year = :readjustment_year, readjustment_month = :readjustment_month, often = :often, date_start = :date_start, description = :description, financial_categories_id = :financial_categories_id, financial_accounts_id = :financial_accounts_id, products_id = :products_id where companies_budgets_id = :companies_budgets_id'; }else{//Se o ID não foi informado, grava-se um novo registro /** Consulta SQL */ $this->sql = 'insert into companies_budgets(company_id, user_id, budget, day_due, readjustment_index, readjustment_value, readjustment_budget, readjustment_type, readjustment_year, readjustment_month, often, date_start, description, financial_categories_id, financial_accounts_id, products_id ) values (:company_id, :user_id, :budget, :day_due, :readjustment_index, :readjustment_value, :readjustment_budget, :readjustment_type, :readjustment_year, :readjustment_month, :often, :date_start, :description, :financial_categories_id, :financial_accounts_id, :products_id)'; } /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); try{ /** Inicia a transação */ $this->connection->connect()->beginTransaction(); if($this->companiesBudgetsId > 0){ /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('companies_budgets_id', $this->companiesBudgetsId); }else{ /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('user_id', $this->userId); $this->stmt->bindParam('company_id', $this->companyId); } $this->stmt->bindParam('budget', $this->budget); $this->stmt->bindParam('day_due', $this->dayDue); $this->stmt->bindParam('readjustment_index', $this->readjustmentIndex); $this->stmt->bindParam('readjustment_value', $this->readjustmentValue); $this->stmt->bindParam('readjustment_budget', $this->readjustmentBudget); $this->stmt->bindParam('readjustment_type', $this->readjustmentType); $this->stmt->bindParam('readjustment_year', $this->readjustmentYear); $this->stmt->bindParam('readjustment_month', $this->readjustmentMonth); $this->stmt->bindParam('often', $this->often); $this->stmt->bindParam('date_start', $this->dateStart); $this->stmt->bindParam('description', $this->description); $this->stmt->bindParam('financial_categories_id', $this->financialCategoriesId); $this->stmt->bindParam('financial_accounts_id', $this->financialAccountsId); $this->stmt->bindParam('products_id', $this->productsId); /** Executo o SQL */ $this->stmt->execute(); /** Retorna o ID do novo registro */ $this->setId($this->connection->connect()->lastInsertId()); /** Confirma a transação */ $this->connection->connect()->commit(); return true; }catch(\Exception $exception) { /** Desfaz a transação */ $this->connection->connect()->rollback(); /** Captura o erro */ array_push($this->errors, $exception->getMessage()); return false; } } /** Deleta um determinado registro no banco de dados */ function Delete(int $companiesBudgetsId) { /** Parametros de entrada */ $this->companiesBudgetsId = $companiesBudgetsId; /** Consulta SQL */ $this->sql = 'delete from companies_budgets where companies_budgets_id = :companies_budgets_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('companies_budgets_id', $this->companiesBudgetsId); /** Executo o SQL */ return $this->stmt->execute(); } /** Define o Último ID inserido */ public function setId($lastId) : void { $this->lastId = $lastId; } /** Recupera o Último ID inserido */ public function getId(): ? int { return (int)$this->lastId; } /** Verifica se há erros a serem visualizadas */ public function getErrors(): ? string { /** Verifico se deve informar os erros */ if (count($this->errors)) { /** Verifica a quantidade de erros para informar a legenda */ $this->info = count($this->errors) > 1 ? '
Os seguintes erros foram encontrados
' : '
O seguinte erro foi encontrado
'; /** Lista os erros */ foreach ($this->errors as $keyError => $error) { /** Monto a mensagem de erro */ $this->info .= '
' . ($keyError + 1) . ' - ' . $error; } /** Retorno os erros encontrados */ return (string)$this->info; } else { return false; } } /** Fecha uma conexão aberta anteriormente com o banco de dados */ function __destruct() { $this->connection = null; } }