fixed import export

This commit is contained in:
Denis Duliçi 2021-05-23 02:27:44 +03:00
parent 35e29c2140
commit 21e14f1191
12 changed files with 112 additions and 34 deletions

View File

@ -143,10 +143,17 @@ abstract class Controller extends BaseController
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),
]);

View File

@ -21,8 +21,8 @@ class Items implements WithMultipleSheets
public function sheets(): array
{
return [
'items' => new Base($this->ids),
'item_taxes' => new ItemTaxes($this->ids),
new Base($this->ids),
new ItemTaxes($this->ids),
];
}
}

View File

@ -25,12 +25,12 @@ class Bills implements WithMultipleSheets
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),
];
}
}

View File

@ -25,12 +25,12 @@ class Invoices implements WithMultipleSheets
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),
];
}
}

View File

@ -4,10 +4,12 @@ namespace App\Imports\Common;
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;
@ -18,4 +20,9 @@ class Items implements WithMultipleSheets
'item_taxes' => new ItemTaxes(),
];
}
public function chunkSize(): int
{
return 100;
}
}

View File

@ -8,10 +8,12 @@ use App\Imports\Purchases\Sheets\BillItemTaxes;
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;
@ -26,4 +28,9 @@ class Bills implements WithMultipleSheets
'bill_transactions' => new BillTransactions(),
];
}
public function chunkSize(): int
{
return 100;
}
}

View File

@ -8,10 +8,12 @@ use App\Imports\Sales\Sheets\InvoiceItemTaxes;
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;
@ -26,4 +28,9 @@ class Invoices implements WithMultipleSheets
'invoice_transactions' => new InvoiceTransactions(),
];
}
public function chunkSize(): int
{
return 100;
}
}

View File

@ -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
{
@ -33,18 +36,7 @@ class CreateMediableForExport extends JobShouldQueue
*/
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');
@ -52,4 +44,55 @@ class CreateMediableForExport extends JobShouldQueue
$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;
}
}

View File

@ -232,8 +232,8 @@ return [
| 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'),
/*
|--------------------------------------------------------------------------
@ -250,7 +250,7 @@ return [
| processed it.
|
*/
'force_resync_remote' => env('EXCEL_TEMPORARY_FILES_FORCE_RESYNC_REMOTE', null),
'force_resync_remote' => env('EXCEL_TEMPORARY_FILES_FORCE_RESYNC_REMOTE'),
],

View File

@ -72,6 +72,13 @@ return [
'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'),

Binary file not shown.

Binary file not shown.