1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-19 16:01:34 +02:00
invoiceninja/app/Http/Controllers/ExportController.php

262 lines
8.6 KiB
PHP
Raw Permalink Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Http\Controllers;
2015-11-12 21:36:28 +01:00
use App\Models\Client;
use App\Models\Contact;
use App\Models\Credit;
2017-01-30 20:40:43 +01:00
use App\Models\Expense;
2015-11-12 21:36:28 +01:00
use App\Models\Invoice;
use App\Models\Payment;
2017-01-30 20:40:43 +01:00
use App\Models\Product;
use App\Models\Task;
2016-01-06 15:23:58 +01:00
use App\Models\Vendor;
use App\Models\VendorContact;
2017-01-30 20:40:43 +01:00
use App\Ninja\Serializers\ArraySerializer;
use App\Ninja\Transformers\AccountTransformer;
use Auth;
use Excel;
use Illuminate\Http\Request;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
2015-11-12 21:36:28 +01:00
/**
2017-01-30 20:40:43 +01:00
* Class ExportController.
*/
2015-11-18 15:40:50 +01:00
class ExportController extends BaseController
2015-11-12 21:36:28 +01:00
{
/**
* @param Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
2015-11-12 21:36:28 +01:00
public function doExport(Request $request)
{
$format = $request->input('format');
$date = date('Y-m-d');
2017-01-15 19:38:36 +01:00
// set the filename based on the entity types selected
if ($request->include == 'all') {
$fileName = "{$date}-invoiceninja";
2017-01-15 19:38:36 +01:00
} else {
$fields = $request->all();
$fields = array_filter(array_map(function ($key) {
2017-01-30 17:05:31 +01:00
if (! in_array($key, ['format', 'include', '_token'])) {
2017-01-15 19:38:36 +01:00
return $key;
} else {
return null;
}
}, array_keys($fields), $fields));
$fileName = $date. '-invoiceninja-' . implode('-', $fields);
2017-01-15 19:38:36 +01:00
}
2015-11-12 21:36:28 +01:00
if ($format === 'JSON') {
return $this->returnJSON($request, $fileName);
} elseif ($format === 'CSV') {
return $this->returnCSV($request, $fileName);
} else {
return $this->returnXLS($request, $fileName);
}
}
/**
* @param $request
* @param $fileName
*
* @return \Illuminate\Http\JsonResponse
*/
2015-11-12 21:36:28 +01:00
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;
2017-01-30 17:05:31 +01:00
$account->load(['clients' => function ($query) {
$query->withArchived()
2017-01-30 17:05:31 +01:00
->with(['contacts', 'invoices' => function ($query) {
$query->withArchived()
2017-01-30 17:05:31 +01:00
->with(['invoice_items', 'payments' => function ($query) {
$query->withArchived();
}]);
}]);
}]);
2015-11-12 21:36:28 +01:00
2017-01-30 20:40:43 +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);
}
/**
* @param $request
* @param $fileName
*
* @return mixed
*/
2015-11-12 21:36:28 +01:00
private function returnCSV($request, $fileName)
{
$data = $this->getData($request);
2017-01-30 17:05:31 +01:00
return Excel::create($fileName, function ($excel) use ($data) {
$excel->sheet('', function ($sheet) use ($data) {
2015-11-12 21:36:28 +01:00
$sheet->loadView('export', $data);
});
})->download('csv');
}
/**
* @param $request
* @param $fileName
*
* @return mixed
*/
2015-11-12 21:36:28 +01:00
private function returnXLS($request, $fileName)
{
$user = Auth::user();
$data = $this->getData($request);
2017-01-30 17:05:31 +01:00
return Excel::create($fileName, function ($excel) use ($user, $data) {
2015-11-12 21:36:28 +01:00
$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}");
2017-01-30 17:05:31 +01:00
$excel->sheet($label, function ($sheet) use ($key, $data) {
2015-11-12 21:36:28 +01:00
if ($key === 'quotes') {
$key = 'invoices';
$data['entityType'] = ENTITY_QUOTE;
2016-09-06 09:20:51 +02:00
$data['invoices'] = $data['quotes'];
2015-11-12 21:36:28 +01:00
}
$sheet->loadView("export.{$key}", $data);
});
}
})->download('xls');
}
/**
* @param $request
*
* @return array
*/
2015-11-12 21:36:28 +01:00
private function getData($request)
{
$account = Auth::user()->account;
$data = [
'account' => $account,
'title' => 'Invoice Ninja v' . NINJA_VERSION . ' - ' . $account->formatDateTime($account->getDateTime()),
2017-01-30 20:40:43 +01:00
'multiUser' => $account->users->count() > 1,
2015-11-12 21:36:28 +01:00
];
2016-05-22 19:01:37 +02:00
2016-07-05 15:09:52 +02:00
if ($request->input('include') === 'all' || $request->input('clients')) {
2015-11-12 21:36:28 +01:00
$data['clients'] = Client::scope()
2017-11-19 13:52:42 +01:00
->with('user', 'contacts', 'country', 'currency', 'shipping_country')
2016-02-25 10:25:07 +01:00
->withArchived()
2015-11-12 21:36:28 +01:00
->get();
2016-07-05 15:09:52 +02:00
}
2015-11-12 21:36:28 +01:00
2016-07-05 15:09:52 +02:00
if ($request->input('include') === 'all' || $request->input('contacts')) {
2015-11-12 21:36:28 +01:00
$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();
2016-07-05 15:09:52 +02:00
}
2015-11-12 21:36:28 +01:00
2016-07-05 15:09:52 +02:00
if ($request->input('include') === 'all' || $request->input('credits')) {
2015-11-12 21:36:28 +01:00
$data['credits'] = Credit::scope()
->with('user', 'client.contacts')
->get();
}
2016-05-22 19:01:37 +02:00
2016-07-05 15:09:52 +02:00
if ($request->input('include') === 'all' || $request->input('tasks')) {
2015-11-12 21:36:28 +01:00
$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
2016-07-05 15:09:52 +02:00
if ($request->input('include') === 'all' || $request->input('invoices')) {
2015-11-12 21:36:28 +01:00
$data['invoices'] = Invoice::scope()
2016-05-26 16:56:54 +02:00
->invoiceType(INVOICE_TYPE_STANDARD)
2017-08-23 10:26:00 +02:00
->with('user', 'client.contacts', 'invoice_status', 'invoice_items')
2016-02-25 10:25:07 +01:00
->withArchived()
2015-11-12 21:36:28 +01:00
->where('is_recurring', '=', false)
->get();
2016-07-05 15:09:52 +02:00
}
2016-05-22 19:01:37 +02:00
2016-07-05 15:09:52 +02:00
if ($request->input('include') === 'all' || $request->input('quotes')) {
2015-11-12 21:36:28 +01:00
$data['quotes'] = Invoice::scope()
2016-05-26 16:56:54 +02:00
->invoiceType(INVOICE_TYPE_QUOTE)
2017-08-23 10:26:00 +02:00
->with('user', 'client.contacts', 'invoice_status', 'invoice_items')
2016-02-25 10:25:07 +01:00
->withArchived()
2015-11-12 21:36:28 +01:00
->where('is_recurring', '=', false)
->get();
2016-07-05 15:09:52 +02:00
}
2016-02-25 10:25:07 +01:00
2016-07-05 15:09:52 +02:00
if ($request->input('include') === 'all' || $request->input('recurring')) {
2016-02-25 10:25:07 +01:00
$data['recurringInvoices'] = Invoice::scope()
2016-05-26 16:56:54 +02:00
->invoiceType(INVOICE_TYPE_STANDARD)
2017-08-23 10:26:00 +02:00
->with('user', 'client.contacts', 'invoice_status', 'frequency', 'invoice_items')
2016-02-25 10:25:07 +01:00
->withArchived()
->where('is_recurring', '=', true)
->get();
2015-11-12 21:36:28 +01:00
}
2016-05-22 19:01:37 +02:00
2016-07-05 15:09:52 +02:00
if ($request->input('include') === 'all' || $request->input('payments')) {
2015-11-12 21:36:28 +01:00
$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-12-05 19:35:01 +01:00
if ($request->input('include') === 'all' || $request->input('expenses')) {
$data['expenses'] = Expense::scope()
->with('user', 'vendor.vendor_contacts', 'client.contacts', 'expense_category')
->withArchived()
->get();
}
2016-12-15 14:28:24 +01:00
if ($request->input('include') === 'all' || $request->input('products')) {
$data['products'] = Product::scope()
->withArchived()
->get();
}
2016-07-05 15:09:52 +02:00
if ($request->input('include') === 'all' || $request->input('vendors')) {
$data['vendors'] = 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();
2016-07-05 15:09:52 +02:00
}
2016-01-06 15:23:58 +01:00
2016-07-05 15:09:52 +02:00
if ($request->input('include') === 'all' || $request->input('vendor_contacts')) {
2016-01-06 15:23:58 +01:00
$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
2015-11-12 21:36:28 +01:00
return $data;
}
2016-05-22 19:01:37 +02:00
}