Initial support for importing supplier costs

This commit is contained in:
Deon George
2022-06-14 16:55:17 +10:00
parent 606f357839
commit 768744ad27
13 changed files with 735 additions and 0 deletions

43
app/Traits/Import.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
/**
* Import from files
*/
namespace App\Traits;
use Illuminate\Support\Collection;
trait Import
{
protected Collection $_columns;
/**
* Return the columns from the file that we'll work with.
* This creates an index of the position of each header in the file, which we use to find an value by getColumnKey()
*
* @param string $line
* @return Collection
* @throws \Exception
*/
private function setColumns(string $line): Collection
{
// If columns is not set, then this is an error
if (! $this->columns)
throw new \Exception('ERROR: Columns must be set before calling setColumns()');
$this->_columns = collect(explode(',',strtolower($line)))->filter();
return $this->_columns->intersect($this->columns);
}
/**
* Get the index for the column in the file
*
* @param string $key
* @return mixed
*/
private function getColumnKey(string $key)
{
return $this->_columns->search($this->columns->get($key));
}
}