CustomerController.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\CustomersExport;
  4. use App\Http\Controllers\Controller;
  5. use App\Http\Requests\Customer\StoreCustomerRequest;
  6. use App\Http\Requests\Customer\UpdateCustomerRequest;
  7. use App\Models\Customer;
  8. use App\Models\Transaction;
  9. use App\Services\TransactionService;
  10. class CustomerController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Inertia\Response
  16. */
  17. public function index()
  18. {
  19. return inertia('customer/Index', [
  20. 'filters' => request()->all('search'),
  21. 'customers' => Customer::filter(request()->only('search'))
  22. ->latest()
  23. ->paginate(10)
  24. ->withQueryString()
  25. ->through(fn($customer) => [
  26. 'id' => $customer->id,
  27. 'customer_number' => $customer->customer_number,
  28. 'name' => $customer->name,
  29. 'phone' => $customer->phone,
  30. 'gender' => $customer->gender_id,
  31. ]),
  32. ]);
  33. }
  34. /**
  35. * Show the form for creating a new resource.
  36. *
  37. * @return \Inertia\Response
  38. */
  39. public function create()
  40. {
  41. return inertia('customer/Create', [
  42. 'customer_number' => 'CS' . now()->format('YmdHis'),
  43. 'genders' => [
  44. ['label' => 'Perempuan', 'value' => 1],
  45. ['label' => 'Laki-laki', 'value' => 2],
  46. ],
  47. ]);
  48. }
  49. /**
  50. * Store a newly created resource in storage.
  51. *
  52. * @param \Illuminate\Http\Request $request
  53. * @return \Illuminate\Http\Response
  54. */
  55. public function store(StoreCustomerRequest $request)
  56. {
  57. Customer::create($request->validated());
  58. if ($request->transaction_number) {
  59. return back()->with('success', __('messages.success.store.customer'));
  60. } else {
  61. return back()->with('success', __('messages.success.store.customer'));
  62. }
  63. }
  64. /**
  65. * Display the specified resource.
  66. *
  67. * @param int $id
  68. * @return \Inertia\Response
  69. */
  70. public function show($id)
  71. {
  72. //
  73. }
  74. /**
  75. * Show the form for editing the specified resource.
  76. *
  77. * @param Customer $customer
  78. * @return \Inertia\Response
  79. */
  80. public function edit(Customer $customer)
  81. {
  82. return inertia('customer/Edit', [
  83. 'customer' => [
  84. 'id' => $customer->id,
  85. 'customer_number' => $customer->customer_number,
  86. 'name' => $customer->name,
  87. 'phone' => $customer->phone,
  88. 'gender_id' => (int) $customer->getRawOriginal('gender_id'),
  89. 'relation' => $customer->transaction()->exists(),
  90. ],
  91. 'genders' => [
  92. ['label' => 'Perempuan', 'value' => 1],
  93. ['label' => 'Laki-laki', 'value' => 2],
  94. ],
  95. 'transactions' => [
  96. 'details' => $customer->transaction()
  97. ->latest()
  98. ->paginate(10)
  99. ->withQueryString()
  100. ->through(fn($transaction) => [
  101. 'id' => $transaction->id,
  102. 'transactionNumber' => $transaction->transaction_number,
  103. 'createdAt' => $transaction->created_at,
  104. 'customer' => [
  105. 'number' => $customer->customer_number,
  106. 'name' => $customer->name,
  107. 'phone' => $customer->phone,
  108. ],
  109. 'price' => $transaction->totalPriceAsFullString(),
  110. 'outlet' => $transaction->outlet->name,
  111. 'transactionStatusName' => $transaction->transactionStatus->name,
  112. 'transactionStatusId' => $transaction->transactionStatus->id,
  113. ]),
  114. 'totalTransaction' => $customer->transaction->count(),
  115. 'totalValue' => (new TransactionService)->totalPriceGroupAsString($customer->fresh()->transaction),
  116. 'totalDiscountGiven' => (new TransactionService)->totalDiscountGivenGroupAsString($customer->fresh()->transaction),
  117. ],
  118. ]);
  119. }
  120. /**
  121. * Update the specified resource in storage.
  122. *
  123. * @param \Illuminate\Http\Request $request
  124. * @param Customer $customer
  125. * @return \Illuminate\Http\Response
  126. */
  127. public function update(UpdateCustomerRequest $request, Customer $customer)
  128. {
  129. $customer->update($request->validated());
  130. return back()->with('success', __('messages.success.update.customer'));
  131. }
  132. /**
  133. * Remove the specified resource from storage.
  134. *
  135. * @param Customer $customer
  136. * @return \Illuminate\Http\Response
  137. */
  138. public function destroy(Customer $customer)
  139. {
  140. $customer->delete();
  141. return to_route('customers.index')->with('success', __('messages.success.destroy.customer'));
  142. }
  143. /**
  144. * Export to excel
  145. */
  146. public function exportExcel()
  147. {
  148. return new CustomersExport(request());
  149. }
  150. }