1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 20:52:56 +01:00
invoiceninja/app/Http/Controllers/ExportController.php

198 lines
6.4 KiB
PHP
Raw Normal View History

2015-11-12 21:36:28 +01:00
<?php namespace App\Http\Controllers;
use Auth;
use Excel;
use Illuminate\Http\Request;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
use App\Ninja\Serializers\ArraySerializer;
use App\Ninja\Transformers\AccountTransformer;
use App\Models\Client;
use App\Models\Contact;
use App\Models\Credit;
use App\Models\Task;
use App\Models\Invoice;
use App\Models\Payment;
2016-01-06 15:23:58 +01:00
use App\Models\Vendor;
use App\Models\VendorContact;
2015-11-12 21:36:28 +01:00
2015-11-18 15:40:50 +01:00
class ExportController extends BaseController
2015-11-12 21:36:28 +01:00
{
public function doExport(Request $request)
{
$format = $request->input('format');
$date = date('Y-m-d');
$fileName = "invoice-ninja-{$date}";
if ($format === 'JSON') {
return $this->returnJSON($request, $fileName);
} elseif ($format === 'CSV') {
return $this->returnCSV($request, $fileName);
} else {
return $this->returnXLS($request, $fileName);
}
}
private function returnJSON($request, $fileName)
{
$output = fopen('php://output', 'w') or Utils::fatalError();
header('Content-Type:application/json');
header("Content-Disposition:attachment;filename={$fileName}.json");
$manager = new Manager();
$manager->setSerializer(new ArraySerializer());
// eager load data, include archived but exclude deleted
2015-11-12 21:36:28 +01:00
$account = Auth::user()->account;
$account->load(['clients' => function($query) {
$query->withArchived()
->with(['contacts', 'invoices' => function($query) {
$query->withArchived()
->with(['invoice_items', 'payments' => function($query) {
$query->withArchived();
}]);
}]);
}]);
2015-11-12 21:36:28 +01:00
$resource = new Item($account, new AccountTransformer);
2016-06-02 21:03:59 +02:00
$data = $manager->parseIncludes('clients.invoices.payments')
->createData($resource)
->toArray();
2015-11-12 21:36:28 +01:00
return response()->json($data);
}
private function returnCSV($request, $fileName)
{
$data = $this->getData($request);
return Excel::create($fileName, function($excel) use ($data) {
$excel->sheet('', function($sheet) use ($data) {
$sheet->loadView('export', $data);
});
})->download('csv');
}
private function returnXLS($request, $fileName)
{
$user = Auth::user();
$data = $this->getData($request);
return Excel::create($fileName, function($excel) use ($user, $data) {
$excel->setTitle($data['title'])
->setCreator($user->getDisplayName())
->setLastModifiedBy($user->getDisplayName())
->setDescription('')
->setSubject('')
->setKeywords('')
->setCategory('')
->setManager('')
->setCompany($user->account->getDisplayName());
foreach ($data as $key => $val) {
if ($key === 'account' || $key === 'title' || $key === 'multiUser') {
continue;
}
2016-05-22 19:01:37 +02:00
if ($key === 'recurringInvoices') {
$key = 'recurring_invoices';
}
2015-11-12 21:36:28 +01:00
$label = trans("texts.{$key}");
$excel->sheet($label, function($sheet) use ($key, $data) {
if ($key === 'quotes') {
$key = 'invoices';
$data['entityType'] = ENTITY_QUOTE;
}
$sheet->loadView("export.{$key}", $data);
});
}
})->download('xls');
}
private function getData($request)
{
$account = Auth::user()->account;
$data = [
'account' => $account,
'title' => 'Invoice Ninja v' . NINJA_VERSION . ' - ' . $account->formatDateTime($account->getDateTime()),
'multiUser' => $account->users->count() > 1
];
2016-05-22 19:01:37 +02:00
2015-11-12 21:36:28 +01:00
if ($request->input(ENTITY_CLIENT)) {
$data['clients'] = Client::scope()
->with('user', 'contacts', 'country')
2016-02-25 10:25:07 +01:00
->withArchived()
2015-11-12 21:36:28 +01:00
->get();
$data['contacts'] = Contact::scope()
->with('user', 'client.contacts')
2015-11-15 18:23:45 +01:00
->withTrashed()
2015-11-12 21:36:28 +01:00
->get();
$data['credits'] = Credit::scope()
->with('user', 'client.contacts')
->get();
}
2016-05-22 19:01:37 +02:00
2015-11-12 21:36:28 +01:00
if ($request->input(ENTITY_TASK)) {
$data['tasks'] = Task::scope()
->with('user', 'client.contacts')
2016-02-25 10:25:07 +01:00
->withArchived()
2015-11-12 21:36:28 +01:00
->get();
}
2016-05-22 19:01:37 +02:00
2015-11-12 21:36:28 +01:00
if ($request->input(ENTITY_INVOICE)) {
$data['invoices'] = Invoice::scope()
2016-05-26 16:56:54 +02:00
->invoiceType(INVOICE_TYPE_STANDARD)
2015-11-12 21:36:28 +01:00
->with('user', 'client.contacts', 'invoice_status')
2016-02-25 10:25:07 +01:00
->withArchived()
2015-11-12 21:36:28 +01:00
->where('is_recurring', '=', false)
->get();
2016-05-22 19:01:37 +02:00
2015-11-12 21:36:28 +01:00
$data['quotes'] = Invoice::scope()
2016-05-26 16:56:54 +02:00
->invoiceType(INVOICE_TYPE_QUOTE)
2015-11-12 21:36:28 +01:00
->with('user', 'client.contacts', 'invoice_status')
2016-02-25 10:25:07 +01:00
->withArchived()
2015-11-12 21:36:28 +01:00
->where('is_recurring', '=', false)
->get();
2016-02-25 10:25:07 +01:00
$data['recurringInvoices'] = Invoice::scope()
2016-05-26 16:56:54 +02:00
->invoiceType(INVOICE_TYPE_STANDARD)
2016-02-25 10:25:07 +01:00
->with('user', 'client.contacts', 'invoice_status', 'frequency')
->withArchived()
->where('is_recurring', '=', true)
->get();
2015-11-12 21:36:28 +01:00
}
2016-05-22 19:01:37 +02:00
2015-11-12 21:36:28 +01:00
if ($request->input(ENTITY_PAYMENT)) {
$data['payments'] = Payment::scope()
2016-02-25 10:25:07 +01:00
->withArchived()
2015-11-12 21:36:28 +01:00
->with('user', 'client.contacts', 'payment_type', 'invoice', 'account_gateway.gateway')
->get();
}
2016-05-22 19:01:37 +02:00
2016-01-06 15:23:58 +01:00
if ($request->input(ENTITY_VENDOR)) {
$data['clients'] = Vendor::scope()
->with('user', 'vendor_contacts', 'country')
2016-02-25 10:25:07 +01:00
->withArchived()
2016-01-06 15:23:58 +01:00
->get();
$data['vendor_contacts'] = VendorContact::scope()
->with('user', 'vendor.vendor_contacts')
2016-01-06 15:23:58 +01:00
->withTrashed()
->get();
2016-05-22 19:01:37 +02:00
2016-01-06 15:23:58 +01:00
/*
$data['expenses'] = Credit::scope()
->with('user', 'client.contacts')
->get();
*/
}
2016-05-22 19:01:37 +02:00
2015-11-12 21:36:28 +01:00
return $data;
}
2016-05-22 19:01:37 +02:00
}