added export and improved import

This commit is contained in:
denisdulici
2018-06-11 03:47:32 +03:00
parent c274d38f13
commit 3d68496145
35 changed files with 347 additions and 115 deletions

View File

@ -312,14 +312,44 @@ class Invoices extends Controller
*/
public function import(ImportFile $import)
{
$rows = $import->all();
// Loop through all sheets
$import->each(function ($sheet) {
$class = '\App\Models\Income\\' . str_singular(studly_case($sheet->getTitle()));
foreach ($rows as $row) {
$data = $row->toArray();
$data['company_id'] = session('company_id');
if (!class_exists($class)) {
return;
}
Invoice::create($data);
}
$sheet->each(function ($row) use ($sheet, $class) {
$data = $row->toArray();
$data['company_id'] = session('company_id');
switch ($sheet->getTitle()) {
case 'invoices':
if (empty($data['customer_email'])) {
$data['customer_email'] = '';
}
break;
case 'invoice_items':
if (empty($data['tax_id'])) {
$data['tax_id'] = '0';
}
break;
case 'invoice_histories':
if (empty($data['notify'])) {
$data['notify'] = '0';
}
break;
case 'invoice_totals':
if (empty($data['amount'])) {
$data['amount'] = '0';
}
break;
}
$class::create($data);
});
});
$message = trans('messages.success.imported', ['type' => trans_choice('general.invoices', 2)]);
@ -511,6 +541,35 @@ class Invoices extends Controller
return redirect('incomes/invoices');
}
/**
* Export the specified resource.
*
* @return Response
*/
public function export()
{
\Excel::create('invoices', function($excel) {
$invoices = Invoice::with(['items', 'payments', 'totals'])->filter(request()->input())->get();
$excel->sheet('invoices', function($sheet) use ($invoices) {
$sheet->fromModel($invoices->makeHidden([
'company_id', 'parent_id', 'created_at', 'updated_at', 'deleted_at', 'attachment', 'discount', 'items', 'payments', 'totals', 'media'
]));
});
$tables = ['items', 'histories', 'payments', 'totals'];
foreach ($tables as $table) {
$excel->sheet('invoice_' . $table, function($sheet) use ($invoices, $table) {
$invoices->each(function ($bill) use ($sheet, $table) {
$sheet->fromModel($bill->$table->makeHidden([
'id', 'company_id', 'created_at', 'updated_at', 'deleted_at'
]));
});
});
}
})->download('xlsx');
}
/**
* Mark the invoice as sent.
*