Skip to content

Commit

Permalink
Format excel date cells as date for import/export
Browse files Browse the repository at this point in the history
  • Loading branch information
burakcakirel committed Oct 8, 2020
1 parent b76269e commit 691e787
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/Abstracts/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithTitle;
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;

abstract class Export implements FromCollection, ShouldAutoSize, WithHeadings, WithMapping, WithTitle
{
Expand Down Expand Up @@ -41,7 +42,7 @@ public function map($model): array
$value = $model->$field;

if (in_array($field, $date_fields)) {
$value = Date::parse($value)->format('Y-m-d');
$value = ExcelDate::PHPToExcel(Date::parse($value)->format('Y-m-d'));
}

// Prevent CSV injection https://security.stackexchange.com/a/190848
Expand Down
3 changes: 2 additions & 1 deletion app/Abstracts/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Validators\Failure;
use Illuminate\Support\Facades\Validator;
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;

abstract class Import implements ToModel, SkipsOnError, SkipsOnFailure, WithBatchInserts, WithChunkReading, WithHeadingRow, WithMapping, WithValidation
{
Expand Down Expand Up @@ -44,7 +45,7 @@ public function map($row): array
continue;
}

$row[$date_field] = Date::parse($row[$date_field])->format('Y-m-d H:i:s');
$row[$date_field] = Date::parse(ExcelDate::excelToDateTimeObject($row[$date_field]))->format('Y-m-d H:i:s');
}

return $row;
Expand Down
3 changes: 3 additions & 0 deletions app/Imports/Banking/Transfers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use App\Models\Banking\Transfer as Model;
use App\Models\Setting\Category;
use App\Traits\Currencies;
use App\Utilities\Date;
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;

class Transfers extends Import
{
Expand All @@ -21,6 +23,7 @@ public function map($row): array
{
$row = parent::map($row);

$row['transferred_at'] = Date::parse(ExcelDate::excelToDateTimeObject($row['transferred_at']))->format('Y-m-d');
$row['from_account_id'] = $this->getFromAccountId($row);
$row['to_account_id'] = $this->getToAccountId($row);
$row['expense_transaction_id'] = $this->getExpenseTransactionId($row);
Expand Down
Binary file modified public/files/import/bills.xlsx
Binary file not shown.
Binary file modified public/files/import/invoices.xlsx
Binary file not shown.
Binary file modified public/files/import/payments.xlsx
Binary file not shown.
Binary file modified public/files/import/revenues.xlsx
Binary file not shown.
Binary file modified public/files/import/transactions.xlsx
Binary file not shown.
Binary file modified public/files/import/transfers.xlsx
Binary file not shown.

0 comments on commit 691e787

Please sign in to comment.