Skip to content

Commit

Permalink
fixed import export
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed May 22, 2021
1 parent 35e29c2 commit 21e14f1
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 34 deletions.
11 changes: 9 additions & 2 deletions app/Abstracts/Http/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,17 @@ public function importExcel($class, $request, $translation)
public function exportExcel($class, $translation, $extension = 'xlsx')
{
try {
$file_name = Str::filename($translation) . '.' . $extension;
$file_name = Str::filename($translation) . '-' . time() . '.' . $extension;

if (should_queue()) {
$class->queue($file_name)->onQueue('exports')->chain([
$disk = 'temp';

if (config('excel.temporary_files.remote_disk') !== null) {
$disk = config('excel.temporary_files.remote_disk');
$file_name = config('excel.temporary_files.remote_prefix') . $file_name;
}

$class->queue($file_name, $disk)->onQueue('exports')->chain([
new CreateMediableForExport(user(), $file_name),
]);

Expand Down
4 changes: 2 additions & 2 deletions app/Exports/Common/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function __construct($ids = null)
public function sheets(): array
{
return [
'items' => new Base($this->ids),
'item_taxes' => new ItemTaxes($this->ids),
new Base($this->ids),
new ItemTaxes($this->ids),
];
}
}
12 changes: 6 additions & 6 deletions app/Exports/Purchases/Bills.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function __construct($ids = null)
public function sheets(): array
{
return [
'bills' => new Base($this->ids),
'bill_items' => new BillItems($this->ids),
'bill_item_taxes' => new BillItemTaxes($this->ids),
'bill_histories' => new BillHistories($this->ids),
'bill_totals' => new BillTotals($this->ids),
'bill_transactions' => new BillTransactions($this->ids),
new Base($this->ids),
new BillItems($this->ids),
new BillItemTaxes($this->ids),
new BillHistories($this->ids),
new BillTotals($this->ids),
new BillTransactions($this->ids),
];
}
}
12 changes: 6 additions & 6 deletions app/Exports/Sales/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function __construct($ids = null)
public function sheets(): array
{
return [
'invoices' => new Base($this->ids),
'invoice_items' => new InvoiceItems($this->ids),
'invoice_item_taxes' => new InvoiceItemTaxes($this->ids),
'invoice_histories' => new InvoiceHistories($this->ids),
'invoice_totals' => new InvoiceTotals($this->ids),
'invoice_transactions' => new InvoiceTransactions($this->ids),
new Base($this->ids),
new InvoiceItems($this->ids),
new InvoiceItemTaxes($this->ids),
new InvoiceHistories($this->ids),
new InvoiceTotals($this->ids),
new InvoiceTransactions($this->ids),
];
}
}
9 changes: 8 additions & 1 deletion app/Imports/Common/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use App\Imports\Common\Sheets\Items as Base;
use App\Imports\Common\Sheets\ItemTaxes;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class Items implements WithMultipleSheets
class Items implements ShouldQueue, WithChunkReading, WithMultipleSheets
{
use Importable;

Expand All @@ -18,4 +20,9 @@ public function sheets(): array
'item_taxes' => new ItemTaxes(),
];
}

public function chunkSize(): int
{
return 100;
}
}
9 changes: 8 additions & 1 deletion app/Imports/Purchases/Bills.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use App\Imports\Purchases\Sheets\BillHistories;
use App\Imports\Purchases\Sheets\BillTotals;
use App\Imports\Purchases\Sheets\BillTransactions;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class Bills implements WithMultipleSheets
class Bills implements ShouldQueue, WithChunkReading, WithMultipleSheets
{
use Importable;

Expand All @@ -26,4 +28,9 @@ public function sheets(): array
'bill_transactions' => new BillTransactions(),
];
}

public function chunkSize(): int
{
return 100;
}
}
9 changes: 8 additions & 1 deletion app/Imports/Sales/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use App\Imports\Sales\Sheets\InvoiceHistories;
use App\Imports\Sales\Sheets\InvoiceTotals;
use App\Imports\Sales\Sheets\InvoiceTransactions;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class Invoices implements WithMultipleSheets
class Invoices implements ShouldQueue, WithChunkReading, WithMultipleSheets
{
use Importable;

Expand All @@ -26,4 +28,9 @@ public function sheets(): array
'invoice_transactions' => new InvoiceTransactions(),
];
}

public function chunkSize(): int
{
return 100;
}
}
67 changes: 55 additions & 12 deletions app/Jobs/Common/CreateMediableForExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace App\Jobs\Common;

use App\Abstracts\JobShouldQueue;
use App\Models\Common\Media as MediaModel;
use App\Notifications\Common\ExportCompleted;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use MediaUploader;

class CreateMediableForExport extends JobShouldQueue
{
Expand Down Expand Up @@ -33,23 +36,63 @@ public function __construct($user, $file_name)
*/
public function handle()
{
$source = storage_path('app/uploads/' . $this->file_name);
$destination = storage_path('app/uploads/' . company_id() . '/exports/');

// Create exports directory
if (!File::isDirectory($destination)) {
File::makeDirectory($destination);
}

File::move($source, $destination . $this->file_name);

// Create the media record
$media = $this->importMedia($this->file_name, 'exports');
$media = $this->getQueuedMedia();

$this->user->attachMedia($media, 'export');

$download_url = route('uploads.download', ['id' => $media->id, 'company_id' => company_id()]);

$this->user->notify(new ExportCompleted($download_url));
}

public function getQueuedMedia()
{
return config('excel.temporary_files.remote_disk') !== null
? $this->getRemoteQueuedMedia()
: $this->getLocalQueuedMedia();
}

public function getLocalQueuedMedia()
{
$source = storage_path('app/temp/' . $this->file_name);

$destination = $this->getMediaFolder('exports');

$media = MediaUploader::makePrivate()
->beforeSave(function(MediaModel $media) {
$media->company_id = company_id();
})
->fromSource($source)
->toDirectory($destination)
->upload();

File::delete($source);

return $media;
}

public function getRemoteQueuedMedia()
{
$disk = config('excel.temporary_files.remote_disk');
$prefix = config('excel.temporary_files.remote_prefix');

$content = Storage::disk($disk)->get($this->file_name);

$file_name = str_replace([$prefix, '.xlsx', '.xls'], '', $this->file_name);

$destination = $this->getMediaFolder('exports');

$media = MediaUploader::makePrivate()
->beforeSave(function(MediaModel $media) {
$media->company_id = company_id();
})
->fromString($content)
->useFilename($file_name)
->toDirectory($destination)
->upload();

Storage::disk($disk)->delete($this->file_name);

return $media;
}
}
6 changes: 3 additions & 3 deletions config/excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@
| in conjunction with queued imports and exports.
|
*/
'remote_disk' => env('EXCEL_TEMPORARY_FILES_REMOTE_DISK', null),
'remote_prefix' => env('EXCEL_TEMPORARY_FILES_REMOTE_PREFIX', null),
'remote_disk' => env('EXCEL_TEMPORARY_FILES_REMOTE_DISK'),
'remote_prefix' => env('EXCEL_TEMPORARY_FILES_REMOTE_PREFIX'),

/*
|--------------------------------------------------------------------------
Expand All @@ -250,7 +250,7 @@
| processed it.
|
*/
'force_resync_remote' => env('EXCEL_TEMPORARY_FILES_FORCE_RESYNC_REMOTE', null),
'force_resync_remote' => env('EXCEL_TEMPORARY_FILES_FORCE_RESYNC_REMOTE'),

],

Expand Down
7 changes: 7 additions & 0 deletions config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
'visibility' => 'public',
],

'temp' => [
'driver' => 'local',
'root' => storage_path('app/temp'),
'url' => app()->runningInConsole() ? '' : url('/') . '/temp',
'visibility' => 'private',
],

'uploads' => [
'driver' => 'local',
'root' => storage_path('app/uploads'),
Expand Down
Binary file modified public/files/import/customers.xlsx
Binary file not shown.
Binary file modified public/files/import/vendors.xlsx
Binary file not shown.

0 comments on commit 21e14f1

Please sign in to comment.