Code Duplication    Length = 125-125 lines in 2 locations

src/Http/Controllers/ExportController.php 1 location

@@ 12-136 (lines=125) @@
9
use Illuminate\Support\Facades\DB;
10
use PDF;
11
12
class ExportController extends Controller
13
{
14
    /**
15
     * Create a new controller instance.
16
     *
17
     * @return void
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);
54
        //return view('/export/index', ['inventories' =>$inventories, 'searchingVals' => $constraints]);
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) {
132
           return (array) $item;
133
       })
134
       ->all();
135
    }
136
}
137

src/Models/ExportController.php 1 location

@@ 12-136 (lines=125) @@
9
use Illuminate\Support\Facades\DB;
10
use PDF;
11
12
class ExportController extends Controller
13
{
14
    /**
15
     * Create a new controller instance.
16
     *
17
     * @return void
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);
54
        //return view('/export/index', ['inventories' =>$inventories, 'searchingVals' => $constraints]);
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) {
132
           return (array) $item;
133
       })
134
       ->all();
135
    }
136
}
137