mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 04:37:34 -05:00 
			
		
		
		
	
		
			
				
	
	
		
			160 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			160 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?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;
 | 
						|
 | 
						|
class ImportExportController extends BaseController
 | 
						|
{
 | 
						|
    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());
 | 
						|
 | 
						|
        $account = Auth::user()->account;
 | 
						|
        $account->loadAllData();
 | 
						|
 | 
						|
        $resource = new Item($account, new AccountTransformer);
 | 
						|
        $data = $manager->createData($resource)->toArray();
 | 
						|
 | 
						|
        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;
 | 
						|
                }
 | 
						|
                $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
 | 
						|
        ];
 | 
						|
        
 | 
						|
        if ($request->input(ENTITY_CLIENT)) {
 | 
						|
            $data['clients'] = Client::scope()
 | 
						|
                ->with('user', 'contacts', 'country')
 | 
						|
                ->withTrashed()
 | 
						|
                ->where('is_deleted', '=', false)
 | 
						|
                ->get();
 | 
						|
 | 
						|
            $data['contacts'] = Contact::scope()
 | 
						|
                ->with('user', 'client.contacts')
 | 
						|
                ->withTrashed()
 | 
						|
                ->get();
 | 
						|
 | 
						|
            $data['credits'] = Credit::scope()
 | 
						|
                ->with('user', 'client.contacts')
 | 
						|
                ->get();
 | 
						|
        }
 | 
						|
        
 | 
						|
        if ($request->input(ENTITY_TASK)) {
 | 
						|
            $data['tasks'] = Task::scope()
 | 
						|
                ->with('user', 'client.contacts')
 | 
						|
                ->withTrashed()
 | 
						|
                ->where('is_deleted', '=', false)
 | 
						|
                ->get();
 | 
						|
        }
 | 
						|
        
 | 
						|
        if ($request->input(ENTITY_INVOICE)) {
 | 
						|
            $data['invoices'] = Invoice::scope()
 | 
						|
                ->with('user', 'client.contacts', 'invoice_status')
 | 
						|
                ->withTrashed()
 | 
						|
                ->where('is_deleted', '=', false)
 | 
						|
                ->where('is_quote', '=', false)
 | 
						|
                ->where('is_recurring', '=', false)
 | 
						|
                ->get();
 | 
						|
        
 | 
						|
            $data['quotes'] = Invoice::scope()
 | 
						|
                ->with('user', 'client.contacts', 'invoice_status')
 | 
						|
                ->withTrashed()
 | 
						|
                ->where('is_deleted', '=', false)
 | 
						|
                ->where('is_quote', '=', true)
 | 
						|
                ->where('is_recurring', '=', false)
 | 
						|
                ->get();
 | 
						|
        }
 | 
						|
        
 | 
						|
        if ($request->input(ENTITY_PAYMENT)) {
 | 
						|
            $data['payments'] = Payment::scope()
 | 
						|
                ->withTrashed()
 | 
						|
                ->where('is_deleted', '=', false)
 | 
						|
                ->with('user', 'client.contacts', 'payment_type', 'invoice', 'account_gateway.gateway')
 | 
						|
                ->get();
 | 
						|
        }
 | 
						|
 | 
						|
        return $data;
 | 
						|
    }
 | 
						|
} |