Browse Source

fix: styling excel and color progressbar

Muhammad Iqbal Afandi 3 years ago
parent
commit
8e1ba8ba0b

+ 50
- 38
app/Exports/MutationExport.php View File

@@ -3,53 +3,65 @@
3 3
 namespace App\Exports;
4 4
 
5 5
 use App\Models\Mutation;
6
+use Illuminate\Contracts\Support\Responsable;
7
+use Illuminate\Contracts\View\View;
6 8
 use Illuminate\Http\Request;
7
-use Maatwebsite\Excel\Concerns\FromCollection;
9
+use Maatwebsite\Excel\Concerns\Exportable;
10
+use Maatwebsite\Excel\Concerns\FromView;
8 11
 use Maatwebsite\Excel\Concerns\ShouldAutoSize;
12
+use Maatwebsite\Excel\Concerns\WithStyles;
13
+use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
9 14
 
10
-class MutationExport implements ShouldAutoSize, FromCollection
15
+class MutationExport implements ShouldAutoSize, Responsable, FromView, WithStyles
11 16
 {
17
+    use Exportable;
18
+
19
+    private $fileName = 'mutation-report.xls';
20
+
12 21
     public function __construct(private Request $request)
13 22
     {}
14 23
 
15
-    public function collection()
24
+    public function view(): View
16 25
     {
17 26
         $mutations = Mutation::filter($this->request->only('startDate', 'endDate', 'outlet'))->get();
18
-
19
-        return $mutations;
27
+        return view('excel.mutation-report', compact('mutations'));
20 28
     }
21 29
 
22
-    // public function view(): View
23
-    // {
24
-    //     $startDate = $this->request->startDate;
25
-    //     $endDate = $this->request->endDate;
26
-    //     $mutations = Mutation::filter($startDate, $endDate)->get();
27
-
28
-    //     return inertia('excel.dashboard.payment-report.index', [
29
-    //         'mutations' => $mutations,
30
-    //         'startDate' => Carbon::parse($startDate)->format('d/m/Y'),
31
-    //         'endDate' => Carbon::parse($endDate)->subDay()->format('d/m/Y'),
32
-    //     ]);
33
-    // }
34
-
35
-    // public function styles(Worksheet $sheet)
36
-    // {
37
-    //     $lastRow = $sheet->getHighestDataRow();
38
-    //     $lastSecondRow = $lastRow - 1;
39
-
40
-    //     $sheet->getRowDimension($lastSecondRow)->setRowHeight(16);
41
-
42
-    //     return [
43
-    //         1 => [
44
-    //             'font' => ['bold' => true, 'size' => 16],
45
-    //             'alignment' => ['vertical' => 'center', 'horizontal' => 'center'],
46
-    //         ],
47
-    //         3 => ['font' => ['bold' => true]],
48
-    //         $lastSecondRow => ['font' => ['bold' => true, 'size' => 12]],
49
-    //         $lastRow => [
50
-    //             'font' => ['bold' => true, 'size' => 12],
51
-    //             'alignment' => ['horizontal' => 'left'],
52
-    //         ],
53
-    //     ];
54
-    // }
30
+    public function styles(Worksheet $sheet)
31
+    {
32
+        $lastRow = $sheet->getHighestDataRow();
33
+        $lastSecondRow = $lastRow - 1;
34
+        $lastThirdRow = $lastSecondRow - 1;
35
+        $lastFourRow = $lastThirdRow - 1;
36
+
37
+        return [
38
+            1 => [
39
+                'font' => ['bold' => true, 'size' => 12],
40
+                'alignment' => ['vertical' => 'center', 'horizontal' => 'center'],
41
+            ],
42
+            2 => [
43
+                'font' => ['bold' => true, 'size' => 12],
44
+                'alignment' => ['vertical' => 'center', 'horizontal' => 'center'],
45
+            ],
46
+            4 => [
47
+                'font' => ['bold' => true],
48
+            ],
49
+            $lastRow => [
50
+                'font' => ['bold' => true, 'size' => 12],
51
+                'alignment' => ['horizontal' => 'left'],
52
+            ],
53
+            $lastSecondRow => [
54
+                'font' => ['bold' => true, 'size' => 12],
55
+                'alignment' => ['horizontal' => 'left'],
56
+            ],
57
+            $lastThirdRow => [
58
+                'font' => ['bold' => true, 'size' => 12],
59
+                'alignment' => ['horizontal' => 'left'],
60
+            ],
61
+            $lastFourRow => [
62
+                'font' => ['bold' => true, 'size' => 12],
63
+                'alignment' => ['horizontal' => 'left'],
64
+            ],
65
+        ];
66
+    }
55 67
 }

+ 1
- 3
app/Http/Controllers/ReportMutationController.php View File

@@ -6,8 +6,6 @@ use App\Exports\MutationExport;
6 6
 use App\Http\Controllers\Controller;
7 7
 use App\Models\Mutation;
8 8
 use App\Models\Outlet;
9
-use Illuminate\Http\Request;
10
-use Maatwebsite\Excel\Facades\Excel;
11 9
 
12 10
 class ReportMutationController extends Controller
13 11
 {
@@ -45,6 +43,6 @@ class ReportMutationController extends Controller
45 43
      */
46 44
     public function exportExcel()
47 45
     {
48
-        return Excel::download(new MutationExport(request()), 'report-mutation.xls');
46
+        return new MutationExport(request());
49 47
     }
50 48
 }

+ 1
- 1
app/Models/Expense.php View File

@@ -32,7 +32,7 @@ class Expense extends Model
32 32
     public function amount(): Attribute
33 33
     {
34 34
         return Attribute::make(
35
-            get:fn($value) => '- ' . $this->setRupiahFormat($value, 2, true)
35
+            get:fn($value) => '- ' . $this->setRupiahFormat($value, 0, true)
36 36
         );
37 37
     }
38 38
 

+ 53
- 0
app/Models/Helpers/HasMutation.php View File

@@ -0,0 +1,53 @@
1
+<?php
2
+
3
+namespace App\Models\Helpers;
4
+
5
+use Illuminate\Database\Eloquent\Collection;
6
+
7
+trait HasMutation
8
+{
9
+    use CurrencyFormat;
10
+
11
+    protected static function totalIncome(Collection $collections)
12
+    {
13
+        foreach ($collections->chunk(100) as $chunk) {
14
+            return $chunk->sum(function ($collect) {
15
+                if ($collect->getRawOriginal('type') == 1) {
16
+                    return $collect->getRawOriginal('amount');
17
+                }
18
+            });
19
+        }
20
+    }
21
+
22
+    protected static function totalExpense(Collection $collections)
23
+    {
24
+        foreach ($collections->chunk(100) as $chunk) {
25
+            return $chunk->sum(function ($collect) {
26
+                if ($collect->getRawOriginal('type') == 2) {
27
+                    return $collect->getRawOriginal('amount');
28
+                }
29
+            });
30
+        }
31
+    }
32
+
33
+    protected static function totalIncomeAsString(Collection $collections)
34
+    {
35
+        return parent::setRupiahFormat(self::totalIncome($collections), 0, true);
36
+
37
+    }
38
+
39
+    protected static function totalExpenseAsString(Collection $collections)
40
+    {
41
+        return '- ' . parent::setRupiahFormat(self::totalExpense($collections), 0, true);
42
+    }
43
+
44
+    protected static function totalAmount(Collection $collections)
45
+    {
46
+        $amount = self::totalIncome($collections) - self::totalExpense($collections);
47
+        if ($amount < 0) {
48
+            return '- ' . parent::setRupiahFormat(abs($amount), 0, true);
49
+        } else {
50
+            return parent::setRupiahFormat($amount, 0, true);
51
+        }
52
+    }
53
+}

+ 1
- 1
app/Models/Laundry.php View File

@@ -34,7 +34,7 @@ class Laundry extends Model
34 34
     protected function price(): Attribute
35 35
     {
36 36
         return Attribute::make(
37
-            get:fn($value) => $this->setRupiahFormat($value, 2, true)
37
+            get:fn($value) => $this->setRupiahFormat($value, 0, true)
38 38
         );
39 39
     }
40 40
 

+ 4
- 3
app/Models/Mutation.php View File

@@ -3,6 +3,7 @@
3 3
 namespace App\Models;
4 4
 
5 5
 use App\Models\Helpers\CurrencyFormat;
6
+use App\Models\Helpers\HasMutation;
6 7
 use Carbon\Carbon;
7 8
 use Illuminate\Database\Eloquent\Casts\Attribute;
8 9
 use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -10,7 +11,7 @@ use Illuminate\Database\Eloquent\Model;
10 11
 
11 12
 class Mutation extends Model
12 13
 {
13
-    use HasFactory, CurrencyFormat;
14
+    use HasFactory, CurrencyFormat, HasMutation;
14 15
 
15 16
     protected $fillable = [
16 17
         'type',
@@ -30,8 +31,8 @@ class Mutation extends Model
30 31
     public function amount(): Attribute
31 32
     {
32 33
         return Attribute::make(
33
-            get:fn($value) => $this->transaction_id ? $this->setRupiahFormat($value, 2, true)
34
-            : '- ' . $this->setRupiahFormat($value, 2, true)
34
+            get:fn($value) => $this->transaction_id ? $this->setRupiahFormat($value, 0, true)
35
+            : '- ' . $this->setRupiahFormat($value, 0, true)
35 36
         );
36 37
     }
37 38
 

+ 1
- 1
app/Models/Product.php View File

@@ -34,7 +34,7 @@ class Product extends Model
34 34
     protected function price(): Attribute
35 35
     {
36 36
         return Attribute::make(
37
-            get:fn($value) => $this->setRupiahFormat($value, 2, true)
37
+            get:fn($value) => $this->setRupiahFormat($value, 0, true)
38 38
         );
39 39
     }
40 40
 

+ 1
- 1
app/Models/Transaction.php View File

@@ -107,7 +107,7 @@ class Transaction extends Model
107 107
 
108 108
     public function totalPriceAsFullString()
109 109
     {
110
-        return $this->setRupiahFormat($this->totalPrice(), 2, true);
110
+        return $this->setRupiahFormat($this->totalPrice(), 0, true);
111 111
     }
112 112
 
113 113
     public function subTotal()

+ 2
- 2
app/Models/TransactionDetail.php View File

@@ -26,7 +26,7 @@ class TransactionDetail extends Model
26 26
     protected function price(): Attribute
27 27
     {
28 28
         return Attribute::make(
29
-            get:fn($value) => $this->setRupiahFormat($value, 2, true)
29
+            get:fn($value) => $this->setRupiahFormat($value, 0, true)
30 30
         );
31 31
     }
32 32
 
@@ -67,6 +67,6 @@ class TransactionDetail extends Model
67 67
 
68 68
     public function totalPriceAsFullString()
69 69
     {
70
-        return $this->setRupiahFormat($this->totalPrice(), 2, true);
70
+        return $this->setRupiahFormat($this->totalPrice(), 0, true);
71 71
     }
72 72
 }

+ 83
- 0
public/js/resources_js_pages_excel_mutation_vue.js View File

@@ -0,0 +1,83 @@
1
+"use strict";
2
+(self["webpackChunk"] = self["webpackChunk"] || []).push([["resources_js_pages_excel_mutation_vue"],{
3
+
4
+/***/ "./node_modules/babel-loader/lib/index.js??clonedRuleSet-5.use[0]!./node_modules/vue-loader/dist/templateLoader.js??ruleSet[1].rules[2]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./resources/js/pages/excel/mutation.vue?vue&type=template&id=b8a5a7ac":
5
+/*!*******************************************************************************************************************************************************************************************************************************************************************************!*\
6
+  !*** ./node_modules/babel-loader/lib/index.js??clonedRuleSet-5.use[0]!./node_modules/vue-loader/dist/templateLoader.js??ruleSet[1].rules[2]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./resources/js/pages/excel/mutation.vue?vue&type=template&id=b8a5a7ac ***!
7
+  \*******************************************************************************************************************************************************************************************************************************************************************************/
8
+/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
9
+
10
+__webpack_require__.r(__webpack_exports__);
11
+/* harmony export */ __webpack_require__.d(__webpack_exports__, {
12
+/* harmony export */   "render": () => (/* binding */ render)
13
+/* harmony export */ });
14
+/* harmony import */ var vue__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! vue */ "./node_modules/vue/dist/vue.esm-bundler.js");
15
+
16
+function render(_ctx, _cache) {
17
+  return (0,vue__WEBPACK_IMPORTED_MODULE_0__.openBlock)(), (0,vue__WEBPACK_IMPORTED_MODULE_0__.createElementBlock)("h1", null, "asdf");
18
+}
19
+
20
+/***/ }),
21
+
22
+/***/ "./node_modules/vue-loader/dist/exportHelper.js":
23
+/*!******************************************************!*\
24
+  !*** ./node_modules/vue-loader/dist/exportHelper.js ***!
25
+  \******************************************************/
26
+/***/ ((__unused_webpack_module, exports) => {
27
+
28
+
29
+Object.defineProperty(exports, "__esModule", ({ value: true }));
30
+// runtime helper for setting properties on components
31
+// in a tree-shakable way
32
+exports["default"] = (sfc, props) => {
33
+    const target = sfc.__vccOpts || sfc;
34
+    for (const [key, val] of props) {
35
+        target[key] = val;
36
+    }
37
+    return target;
38
+};
39
+
40
+
41
+/***/ }),
42
+
43
+/***/ "./resources/js/pages/excel/mutation.vue":
44
+/*!***********************************************!*\
45
+  !*** ./resources/js/pages/excel/mutation.vue ***!
46
+  \***********************************************/
47
+/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
48
+
49
+__webpack_require__.r(__webpack_exports__);
50
+/* harmony export */ __webpack_require__.d(__webpack_exports__, {
51
+/* harmony export */   "default": () => (__WEBPACK_DEFAULT_EXPORT__)
52
+/* harmony export */ });
53
+/* harmony import */ var _mutation_vue_vue_type_template_id_b8a5a7ac__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./mutation.vue?vue&type=template&id=b8a5a7ac */ "./resources/js/pages/excel/mutation.vue?vue&type=template&id=b8a5a7ac");
54
+/* harmony import */ var _home_dijitalcode_Projects_dev_node_modules_vue_loader_dist_exportHelper_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./node_modules/vue-loader/dist/exportHelper.js */ "./node_modules/vue-loader/dist/exportHelper.js");
55
+
56
+const script = {}
57
+
58
+;
59
+const __exports__ = /*#__PURE__*/(0,_home_dijitalcode_Projects_dev_node_modules_vue_loader_dist_exportHelper_js__WEBPACK_IMPORTED_MODULE_1__["default"])(script, [['render',_mutation_vue_vue_type_template_id_b8a5a7ac__WEBPACK_IMPORTED_MODULE_0__.render],['__file',"resources/js/pages/excel/mutation.vue"]])
60
+/* hot reload */
61
+if (false) {}
62
+
63
+
64
+/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (__exports__);
65
+
66
+/***/ }),
67
+
68
+/***/ "./resources/js/pages/excel/mutation.vue?vue&type=template&id=b8a5a7ac":
69
+/*!*****************************************************************************!*\
70
+  !*** ./resources/js/pages/excel/mutation.vue?vue&type=template&id=b8a5a7ac ***!
71
+  \*****************************************************************************/
72
+/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
73
+
74
+__webpack_require__.r(__webpack_exports__);
75
+/* harmony export */ __webpack_require__.d(__webpack_exports__, {
76
+/* harmony export */   "render": () => (/* reexport safe */ _node_modules_babel_loader_lib_index_js_clonedRuleSet_5_use_0_node_modules_vue_loader_dist_templateLoader_js_ruleSet_1_rules_2_node_modules_vue_loader_dist_index_js_ruleSet_0_use_0_mutation_vue_vue_type_template_id_b8a5a7ac__WEBPACK_IMPORTED_MODULE_0__.render)
77
+/* harmony export */ });
78
+/* harmony import */ var _node_modules_babel_loader_lib_index_js_clonedRuleSet_5_use_0_node_modules_vue_loader_dist_templateLoader_js_ruleSet_1_rules_2_node_modules_vue_loader_dist_index_js_ruleSet_0_use_0_mutation_vue_vue_type_template_id_b8a5a7ac__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! -!../../../../node_modules/babel-loader/lib/index.js??clonedRuleSet-5.use[0]!../../../../node_modules/vue-loader/dist/templateLoader.js??ruleSet[1].rules[2]!../../../../node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./mutation.vue?vue&type=template&id=b8a5a7ac */ "./node_modules/babel-loader/lib/index.js??clonedRuleSet-5.use[0]!./node_modules/vue-loader/dist/templateLoader.js??ruleSet[1].rules[2]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./resources/js/pages/excel/mutation.vue?vue&type=template&id=b8a5a7ac");
79
+
80
+
81
+/***/ })
82
+
83
+}]);

+ 1
- 1
public/js/vue.js View File

@@ -57712,7 +57712,7 @@ __webpack_require__.r(__webpack_exports__);
57712 57712
   }
57713 57713
 });
57714 57714
 _inertiajs_progress__WEBPACK_IMPORTED_MODULE_7__.InertiaProgress.init({
57715
-  color: '#eb4432'
57715
+  color: '#4F46E5'
57716 57716
 });
57717 57717
 })();
57718 57718
 

+ 1
- 1
resources/js/vue.js View File

@@ -60,5 +60,5 @@ createInertiaApp({
60 60
 })
61 61
 
62 62
 InertiaProgress.init({
63
-  color: '#eb4432',
63
+  color: '#4F46E5',
64 64
 })

+ 47
- 0
resources/views/excel/mutation-report.blade.php View File

@@ -0,0 +1,47 @@
1
+<table>
2
+    <thead>
3
+        <tr>
4
+            <th colspan="5">Laporan Mutasi</th>
5
+        </tr>
6
+        <tr>
7
+            <th colspan="5" rowspan="2">Periode {{ $mutations->first()->created_at }} -
8
+                {{ $mutations->last()->created_at }} </th>
9
+        </tr>
10
+        <tr></tr>
11
+        <tr>
12
+            <th>#</th>
13
+            <th>Tanggal</th>
14
+            <th>Tipe</th>
15
+            <th>Nilai</th>
16
+            <th>Outlet</th>
17
+        </tr>
18
+    </thead>
19
+    <tbody>
20
+        @foreach ($mutations->chunk(100) as $chunk)
21
+            @foreach ($chunk as $index => $mutation)
22
+                <tr>
23
+                    <td>{{ ++$index }}</td>
24
+                    <td>{{ $mutation->created_at }}</td>
25
+                    <td>{{ $mutation->type }}</td>
26
+                    <td>{{ $mutation->amount }}</td>
27
+                    <td>{{ $mutation->outlet->name }}</td>
28
+                </tr>
29
+            @endforeach
30
+        @endforeach
31
+        <tr>
32
+            <td colspan="3">Total</td>
33
+        </tr>
34
+        <tr>
35
+            <td colspan="3">Pendapatan</td>
36
+            <td>{{ App\Models\Mutation::totalIncomeAsString($mutations) }}</td>
37
+        </tr>
38
+        <tr>
39
+            <td colspan="3">Pengeluran</td>
40
+            <td>{{ App\Models\Mutation::totalExpenseAsString($mutations) }}</td>
41
+        </tr>
42
+        <tr>
43
+            <td colspan="3">Jumlah</td>
44
+            <td>{{ App\Models\Mutation::totalAmount($mutations) }}</td>
45
+        </tr>
46
+    </tbody>
47
+</table>

+ 1
- 1
webpack.mix.js View File

@@ -15,7 +15,7 @@ mix
15 15
   .js('resources/js/vue.js', 'public/js')
16 16
   .webpackConfig(require('./webpack.config'))
17 17
   .vue()
18
-  .browserSync('http://dev.test/')
18
+  .browserSync('http://bamslaundry.test/')
19 19
   .disableNotifications()
20 20
 
21 21
 if (mix.inProduction()) {

+ 3
- 3
yarn.lock View File

@@ -2032,9 +2032,9 @@ caniuse-api@^3.0.0:
2032 2032
     lodash.uniq "^4.5.0"
2033 2033
 
2034 2034
 caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001317:
2035
-  version "1.0.30001323"
2036
-  resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001323.tgz#a451ff80dec7033016843f532efda18f02eec011"
2037
-  integrity sha512-e4BF2RlCVELKx8+RmklSEIVub1TWrmdhvA5kEUueummz1XyySW0DVk+3x9HyhU9MuWTa2BhqLgEuEmUwASAdCA==
2035
+  version "1.0.30001324"
2036
+  resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001324.tgz#e17c3a8b34822b02d5d15639d570057550074884"
2037
+  integrity sha512-/eYp1J6zYh1alySQB4uzYFkLmxxI8tk0kxldbNHXp8+v+rdMKdUBNjRLz7T7fz6Iox+1lIdYpc7rq6ZcXfTukg==
2038 2038
 
2039 2039
 chalk@^1.1.3:
2040 2040
   version "1.1.3"