Open sidebar
<?php namespace App\Imports; use App\Services\BalanceSheetService; //Chamando a service criada espeficicamente para esse controller //use Maatwebsite\Excel\Concerns\ToModel; use Illuminate\Support\Collection; //Foi inserido para trabalhar com Collection use Maatwebsite\Excel\Concerns\ToCollection; //Foi inserido para trabalhar com Collection use App\Models\Client; //Pai use App\Models\BalanceSheet; use App\Helpers\BalanceSheetHelper; class BalanceSheetImport implements ToCollection { /** * @param array $row * * @return \Illuminate\Database\Eloquent\Model|null */ protected $client; protected $period; protected $fileLayout; //Layout do arquivo protected $loggedUserId; //id do usuário logado protected $fileId; //Id do lote que criamos protected $fileName; //Nome da planilha private $rows = 0; private $emptyField = 0; private $wrongFormat = "no"; public function __construct($client, $period, $fileLayout, $loggedUserId, $fileName){ $this->client = $client; //Objeto do client vindo instanciado do Controller (Isso deixa o código fracamente acoplado - o que é bom) $this->period = $period; $this->fileLayout = $fileLayout; $this->loggedUserId = $loggedUserId; $this->fileName = $fileName; } public function collection(Collection $rows) { $formatedSpreedsheet = BalanceSheetService::formatSpreedsheetToArray($rows, $this->fileLayout); if($formatedSpreedsheet === "falhalayoutcolunas") { $this->wrongFormat = "yes"; return false; exit(); } //Validando os campos (verificando se os campos estão em branco: description, code e classification) $this->emptyField = BalanceSheetHelper::verifyEmptyField($formatedSpreedsheet); if($this->emptyField === 0) { $this->client->balanceSheets()->where('period', $this->period)->delete(); //Criando um id do lote $fileId = md5(time() . rand(0, 9999) . time()); print_r($fileId); foreach($formatedSpreedsheet as $row) { ++$this->rows; $data = [ 'code' => $row['code'], 'classification' => $row['classification'], 'description' => $row['description'], 'period' => $this->period, 'prior_balance' => $row['prior_balance'], 'debit' => $row['debit'], 'credit' => $row['credit'], 'current_balance' => $row['current_balance'], 'file_id' => $fileId, 'file_name' => $this->fileName, 'importing_user_id' => $this->loggedUserId, ]; $this->client->balanceSheets()->create($data); } } return true; //return $this->client->balanceSheets()->createMany($dataForm); } //Método retorna o número de linhas que foram incluídas public function getRowCount(): int { return $this->rows; } //Método retorna o número de linhas em branco que foram encontradas no arquivo public function getEmptyFieldCount(): int { return $this->emptyField; } //Método retorna se a planilha veio no formato errado. public function getWrongFormat(): string { return $this->wrongFormat; } }
Close sidebar
Back
Please note that all pasted data is publicly available.
X (fomerly Twitter)
GitHub
Use setting
Back
Please note that all pasted data is publicly available.
X (fomerly Twitter)
GitHub
Use setting