ExportController::index()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 19

Duplication

Lines 27
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 27
loc 27
ccs 0
cts 22
cp 0
rs 8.8571
cc 1
eloc 19
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Inventory;
6
use Auth;
7
use Excel;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\DB;
10
use PDF;
11
12 View Code Duplication
class ExportController extends Controller
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
{
14
    /**
15
     * Create a new controller instance.
16
     *
17
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
18
     */
19
    public function __construct()
20
    {
21
        $this->middleware('auth');
22
    }
23
24
    /**
25
     * Show the form for creating a new resource.
26
     *
27
     * @return \Illuminate\Http\Response
28
     */
29
    public function index()
30
    {
31
        date_default_timezone_set('Europe/Madrid');
32
        $format = 'Y/m/d';
33
        $now = date($format);
34
        $to = date($format, strtotime('+15 days'));
35
        $constraints = [
36
          'from' => $now,
37
          'to'   => $to,
38
        ];
39
40
        $inventories = DB::table('inventories')
41
        ->leftJoin('brand', 'inventories.brand_id', '=', 'brand.id')
42
        ->leftJoin('material_type', 'inventories.material_type_id', '=', 'material_type.id')
43
        ->leftJoin('brand_model', 'inventories.model_id', '=', 'brand_model.id')
44
        ->leftJoin('moneySource', 'inventories.moneySourceId', '=', 'moneySource.id')
45
        ->leftJoin('location', 'inventories.location_id', '=', 'location.id')
46
        ->leftJoin('provider', 'inventories.provider_id', '=', 'provider.id')
47
        ->select('inventories.*', 'material_type.name as material_type_name', 'material_type.id as material_type_id', 'brand.name as brand_name', 'brand.id as brand_id', 'brand_model.name as brand_model_name', 'brand_model.id as model_id', 'location.name as location_name', 'location.id as location_id', 'moneySource.name as moneySource_name', 'moneySource.id as moneySourceId',
48
        'provider.name as provider_name', 'provider.id as provider_id')
49
        ->paginate();
50
51
        return view('/export/index', ['inventories' =>$inventories, 'searchingVals' => $constraints]);
52
53
        //$inventories = $this->getItemsInventory($constraints);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
54
        //return view('/export/index', ['inventories' =>$inventories, 'searchingVals' => $constraints]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
55
    }
56
57
     /**
58
      * Store a newly created resource in storage.
59
      *
60
      * @param  \Illuminate\Http\Request  $request
61
      *
62
      * @return \Illuminate\Http\Response
63
      */
64
     public function exportExcel(Request $request)
65
     {
66
         $this->prepareExportingData($request)->export('csv');
67
         redirect()->intended('mnt-export');
68
     }
69
70
    public function exportPDF(Request $request)
71
    {
72
        $constraints = [
73
           'from' => $request['from'],
74
           'to'   => $request['to'],
75
       ];
76
        $inventories = $this->getExportingData($constraints);
77
        $pdf = PDF::loadView('export/exportpdf', ['inventories' => $inventories, 'searchingVals' => $constraints])->setPaper('a4', 'landscape');
78
79
        return $pdf->download('Exportat_desde_'.$request['from'].'_a_'.$request['to'].'.pdf');
80
    }
81
82
    private function prepareExportingData($request)
83
    {
84
        $inventories = $this->getExportingData(['from'=> $request['from'], 'to' => $request['to']]);
85
86
        return Excel::create('Exportat_desde_'.$request['from'].'_a_'.$request['to'], function ($excel) use ($inventories, $request) {
87
            // Set the title
88
       $excel->setTitle('Inventari from '.$request['from'].' to '.$request['to']);
89
90
       // Call them separately
91
       $excel->setDescription('Llista');
92
            $excel->sheet('Items', function ($sheet) use ($inventories) {
93
                $sheet->fromArray($inventories);
94
            });
95
        });
96
    }
97
98
    public function search(Request $request)
99
    {
100
        $constraints = [
101
           'from' => $request['from'],
102
           'to'   => $request['to'],
103
       ];
104
        $inventories = $this->getItemsInventory($constraints);
105
106
        return view('export/index', ['inventories' => $inventories, 'searchingVals' => $constraints]);
107
    }
108
109
    private function getItemsInventory($constraints)
110
    {
111
        $inventories = Inventory::where('date_entrance', '>=', $constraints['from'])
112
                       ->where('date_entrance', '<=', $constraints['to'])
113
                       ->get();
114
115
        return $inventories;
116
    }
117
118
    private function getExportingData($constraints)
119
    {
120
        return DB::table('inventories')
121
       ->leftJoin('brand', 'inventories.brand_id', '=', 'brand.id')
122
       ->leftJoin('material_type', 'inventories.material_type_id', '=', 'material_type.id')
123
       ->leftJoin('brand_model', 'inventories.model_id', '=', 'brand_model.id')
124
       ->leftJoin('moneySource', 'inventories.moneySourceId', '=', 'moneySource.id')
125
       ->leftJoin('location', 'inventories.location_id', '=', 'location.id')
126
       ->leftJoin('provider', 'inventories.provider_id', '=', 'provider.id')
127
       ->select('inventories.name as name', 'inventories.description as description', 'material_type.name as material_type_name', 'brand.name as brand_name', 'brand_model.name as brand_model_name',
128
       'location.name as location_name', 'moneySource.name as moneySource_name', 'provider.name as provider_name', 'inventories.quantity as quantity', 'inventories.price as price', 'inventories.date_entrance as date_entrance', 'inventories.last_update as last_update')
129
130
       ->get()
131
       ->map(function ($item, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
132
           return (array) $item;
133
       })
134
       ->all();
135
    }
136
}
137