Advertisement
bngoding

controller

Jun 21st, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.22 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use App\Exports\OrderInvoice;
  7. use Carbon\Carbon;
  8. use App\Customer;
  9. use App\Product;
  10. use App\Order;
  11. use App\User;
  12. use Cookie;
  13. use DB;
  14. use PDF;
  15.  
  16. class OrderController extends Controller
  17. {
  18. public function addOrder()
  19. {
  20. $products = Product::orderBy('created_at', 'DESC')->get();
  21. return view('orders.add', compact('products'));
  22. }
  23. public function getProduct($id)
  24. {
  25. $products = Product::findOrFail($id);
  26. return response()->json($products, 200);
  27. }
  28.  
  29.  
  30. public function addToCart(Request $request)
  31. {
  32. $this->validate($request, [
  33. 'product_id' => 'required|exists:products,id',
  34. 'qty' => 'required|integer'
  35. ]);
  36.  
  37. $product = Product::findOrFail($request->product_id);
  38. $getCart = json_decode($request->cookie('cart'), true);
  39. if ($getCart) {
  40. if (array_key_exists($request->product_id, $getCart)) {
  41. $getCart[$request->product_id]['qty'] += $request->qty;
  42. return response()->json($getCart, 200)
  43. ->cookie('cart', json_encode($getCart), 120);
  44. }
  45. }
  46. $getCart[$request->product_id] = [
  47. 'code' => $product->code,
  48. 'name' => $product->name,
  49. 'price' => $product->price,
  50. //'qty' => $request->qty
  51. ];
  52. return response()->json($getCart, 200)
  53. ->cookie('cart', json_encode($getCart), 120);
  54. }
  55. public function getCart()
  56. {
  57. $cart = json_decode(request()->cookie('cart'), true);
  58. return response()->json($cart, 200);
  59. }
  60. public function removeCart($id)
  61. {
  62. $cart = json_decode(request()->cookie('cart'), true);
  63. unset($cart[$id]);
  64. return response()->json($cart, 200)->cookie('cart', json_encode($cart), 120);
  65. }
  66. public function checkout()
  67. {
  68.  
  69. return view('orders.checkout');
  70. }
  71. public function storeOrder(Request $request)
  72. {
  73. //untuk ngeliat log
  74. //var_dump("masuk bro");
  75. // die();
  76. // /dd("masuk pak eko");
  77.  
  78. $this->validate($request, [
  79. 'email' => 'required|email',
  80. 'name' => 'required|string|max:100',
  81. 'address' => 'required',
  82. 'phone' => 'required|numeric'
  83. ]);
  84. $cart = json_decode($request->cookie('cart'), true);
  85. $result = collect($cart)->map(function($value) {
  86. return [
  87. 'code' => $value['code'],
  88. 'name' => $value['name'],
  89. 'qty' => $value['qty'],
  90. 'price' => $value['price'],
  91. 'result' => $value['price'] * $value['qty']
  92. ];
  93. })->all();
  94. DB::beginTransaction();
  95. try {
  96. $customer = Customer::firstOrCreate([
  97. 'email' => $request->email
  98. ], [
  99. 'name' => $request->name,
  100. 'address' => $request->address,
  101. 'phone' => $request->phone
  102. ]);
  103. $order = Order::create([
  104. 'invoice' => $this->generateInvoice(),
  105. 'customer_id' => $customer->id,
  106. 'user_id' => auth()->user()->id,
  107. 'total' => array_sum(array_column($result, 'result'))
  108. ]);
  109. foreach ($result as $key => $row) {
  110. $order->order_detail()->create([
  111. 'product_id' => $key,
  112. 'qty' => $row['qty'],
  113. 'price' => $row['price']
  114. ]);
  115. }
  116. DB::commit();
  117. return response()->json([
  118. 'status' => 'success',
  119. 'message' => $order->invoice,
  120. ], 200)->cookie(Cookie::forget('cart'));
  121. } catch (\Exception $e) {
  122. DB::rollback();
  123. return response()->json([
  124. 'status' => 'failed',
  125. 'message' => $e->getMessage()
  126. ], 400);
  127. }
  128. }
  129. public function generateInvoice()
  130. {
  131. $order = Order::orderBy('created_at', 'DESC');
  132. if ($order->count() > 0) {
  133. $order = $order->first();
  134. $explode = explode('-', $order->invoice);
  135. $count = $explode[1] + 1;
  136. return 'INV-' . $count;
  137. }
  138. return 'INV-1';
  139. }
  140. public function index(Request $request)
  141. {
  142. $customers = Customer::orderBy('name', 'ASC')->get();
  143. $users = User::role('kasir')->orderBy('name', 'ASC')->get();
  144. $orders = Order::orderBy('created_at', 'DESC')->with('order_detail', 'customer');
  145. if (!empty($request->customer_id)) {
  146. $orders = $orders->where('customer_id', $request->customer_id);
  147. }
  148. if (!empty($request->user_id)) {
  149. $orders = $orders->where('user_id', $request->user_id);
  150. }
  151.  
  152. if (!empty($request->start_date) && !empty($request->end_date)) {
  153. $this->validate($request, [
  154. 'start_date' => 'nullable|date',
  155. 'end_date' => 'nullable|date'
  156. ]);
  157. $start_date = Carbon::parse($request->start_date)->format('Y-m-d') . ' 00:00:01';
  158. $end_date = Carbon::parse($request->end_date)->format('Y-m-d') . ' 23:59:59';
  159. $orders = $orders->whereBetween('created_at', [$start_date, $end_date])->get();
  160. } else {
  161. $orders = $orders->take(10)->skip(0)->get();
  162. }
  163. return view('orders.index', [
  164. 'orders' => $orders,
  165. 'sold' => $this->countItem($orders),
  166. 'total' => $this->countTotal($orders),
  167. 'total_customer' => $this->countCustomer($orders),
  168. 'customers' => $customers,
  169. 'users' => $users
  170. ]);
  171. }
  172. private function countCustomer($orders)
  173. {
  174. $customer = [];
  175. if ($orders->count() > 0) {
  176. foreach ($orders as $row) {
  177. $customer[] = $row->customer->email;
  178. }
  179. }
  180. return count(array_unique($customer));
  181. }
  182. private function countTotal($orders)
  183. {
  184. $total = 0;
  185. if ($orders->count() > 0) {
  186. $sub_total = $orders->pluck('total')->all();
  187. $total = array_sum($sub_total);
  188. }
  189. return $total;
  190. }
  191. private function countItem($order)
  192. {
  193. $data = 0;
  194. if ($order->count() > 0) {
  195. foreach ($order as $row) {
  196. $qty = $row->order_detail->pluck('qty')->all();
  197. $val = array_sum($qty);
  198. $data += $val;
  199. }
  200. }
  201. return $data;
  202. }
  203. public function invoicePdf($invoice)
  204. {
  205. $order = Order::where('invoice', $invoice)
  206. ->with('customer', 'order_detail', 'order_detail.product')->first();
  207. $pdf = PDF::setOptions(['dpi' => 150, 'defaultFont' => 'sans-serif'])
  208. ->loadView('orders.report.invoice', compact('order'));
  209. return $pdf->stream();
  210. }
  211. public function invoiceExcel($invoice)
  212. {
  213. return (new OrderInvoice($invoice))->download('invoice-' . $invoice . '.xlsx');
  214. }
  215.  
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement