Import/export expenses #194

This commit is contained in:
Nabeel Shahzad
2018-03-22 17:17:37 -05:00
parent 4e3a9fd9ea
commit a44204b185
32 changed files with 613 additions and 139 deletions

View File

@@ -8,8 +8,9 @@ namespace App\Interfaces;
*/
abstract class Enum
{
protected static $labels = [];
protected static $cache = [];
protected static $codes = [];
protected static $labels = [];
/**
* @var integer
@@ -59,6 +60,31 @@ abstract class Enum
return $labels;
}
/**
* Get the numeric value from a string code
* @param $code
* @return mixed|null
*/
public static function getFromCode($code)
{
$code = strtoupper($code);
if(!array_key_exists($code, static::$codes)) {
return null;
}
return static::$codes[$code];
}
/**
* Convert the integer value into one of the codes
* @param $value
* @return false|int|string
*/
public static function convertToCode($value)
{
return array_search($value, static::$codes, true);
}
/**
* Select box entry items
* @param bool $add_blank

View File

@@ -3,6 +3,9 @@
namespace App\Interfaces;
use App\Models\Airline;
use Illuminate\Validation\ValidationException;
use Log;
use Validator;
/**
* Common functionality used across all of the importers
@@ -11,7 +14,10 @@ use App\Models\Airline;
class ImportExport
{
public $assetType;
public $status;
public $status = [
'success' => [],
'errors' => [],
];
/**
* Hold the columns for the particular table
@@ -40,6 +46,54 @@ class ImportExport
return static::$columns;
}
/**
* Do a basic check that the number of columns match
* @param $row
* @return bool
*/
public function checkColumns($row): bool
{
return \count($row) === \count($this->getColumns());
}
/**
* Bubble up an error to the interface that we need to stop
* @param $error
* @param $e
* @throws ValidationException
*/
protected function throwError($error, \Exception $e = null): void
{
Log::error($error);
if ($e) {
Log::error($e->getMessage());
}
$validator = Validator::make([], []);
$validator->errors()->add('csv_file', $error);
throw new ValidationException($validator);
}
/**
* Add to the log messages for this importer
* @param $msg
*/
public function log($msg): void
{
$this->status['success'][] = $msg;
Log::info($msg);
}
/**
* Add to the error log for this import
* @param $msg
*/
public function errorLog($msg): void
{
$this->status['errors'][] = $msg;
Log::error($msg);
}
/**
* Set a key-value pair to an array
* @param $kvp_str