2018-06-23 15:59:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Utilities;
|
|
|
|
|
2018-09-12 15:15:23 +03:00
|
|
|
use Date;
|
2018-06-23 15:59:13 +03:00
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
|
|
|
|
class Import
|
|
|
|
{
|
|
|
|
|
|
|
|
public static function createFromFile($import, $slug)
|
|
|
|
{
|
|
|
|
$success = true;
|
|
|
|
|
|
|
|
// Loop through all sheets
|
|
|
|
$import->each(function ($sheet) use (&$success, $slug) {
|
2018-06-27 17:01:26 +03:00
|
|
|
if (!static::isValidSheetName($sheet, $slug)) {
|
|
|
|
$message = trans('messages.error.import_sheet');
|
|
|
|
|
|
|
|
flash($message)->error()->important();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-23 16:45:03 +03:00
|
|
|
if (!$success = static::createFromSheet($sheet, $slug)) {
|
2018-06-23 15:59:13 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function createFromSheet($sheet, $slug)
|
|
|
|
{
|
|
|
|
$success = true;
|
|
|
|
|
|
|
|
$model = '\App\Models\\' . $slug;
|
|
|
|
$request = '\App\Http\Requests\\' . $slug;
|
|
|
|
|
|
|
|
if (!class_exists($model) || !class_exists($request)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through all rows
|
|
|
|
$sheet->each(function ($row, $index) use ($sheet, &$success, $model, $request) {
|
2018-09-12 15:56:08 +03:00
|
|
|
$data = static::fixRow($row->toArray());
|
2018-06-23 15:59:13 +03:00
|
|
|
|
|
|
|
// Set the line values so that request class could validate
|
|
|
|
request()->merge($data);
|
|
|
|
|
|
|
|
try {
|
|
|
|
app($request);
|
|
|
|
|
|
|
|
$data['company_id'] = session('company_id');
|
|
|
|
|
|
|
|
$model::create($data);
|
|
|
|
} catch (ValidationException $e) {
|
2018-06-27 17:01:26 +03:00
|
|
|
$message = trans('messages.error.import_column', [
|
2018-06-23 15:59:13 +03:00
|
|
|
'message' => $e->validator->errors()->first(),
|
|
|
|
'sheet' => $sheet->getTitle(),
|
|
|
|
'line' => $index + 2,
|
|
|
|
]);
|
|
|
|
|
|
|
|
flash($message)->error()->important();
|
|
|
|
|
|
|
|
$success = false;
|
|
|
|
|
|
|
|
// Break the import process
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unset added line values
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
request()->offsetUnset($key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
2018-06-27 17:01:26 +03:00
|
|
|
public static function isValidSheetName($sheet, $slug)
|
|
|
|
{
|
|
|
|
$t = explode('\\', $slug);
|
|
|
|
|
|
|
|
if (empty($t[1])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($sheet->getTitle() != str_plural(snake_case($t[1]))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-12 15:56:08 +03:00
|
|
|
|
|
|
|
protected static function fixRow($data)
|
|
|
|
{
|
|
|
|
// Fix the date fields
|
|
|
|
$date_fields = ['paid_at', 'due_at', 'billed_at', 'invoiced_at'];
|
|
|
|
foreach ($date_fields as $date_field) {
|
|
|
|
if (empty($data[$date_field])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$new_date = Date::parse($data[$date_field])->format('Y-m-d') . ' ' . Date::now()->format('H:i:s');
|
|
|
|
|
|
|
|
$data[$date_field] = $new_date;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make enabled field integer
|
|
|
|
if (isset($data['enabled'])) {
|
|
|
|
$data['enabled'] = (int) $data['enabled'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2018-06-23 15:59:13 +03:00
|
|
|
}
|