input('quantity', null); if (\Cookie::has('card')) { $cards = json_decode(\Cookie::get('card'), true); $qs = json_decode(\Cookie::get('q'), true); if (in_array($product->id, $cards)) { $found = false; foreach ($cards as $i => $card) { if ($card == $product->id && $qs[$i] == $quantity) { $found = true; break; } } if ($found) { $msg = "Product removed from card"; unset($cards[$i]); unset($qs[$i]); } else { $cards[] = $product->id; $qs[] = $quantity; $msg = "Product added to card"; } } else { $cards[] = $product->id; $qs[] = $quantity; $msg = "Product added to card"; } $count = count($cards); \Cookie::queue('card', json_encode($cards), 2000); \Cookie::queue('q', json_encode($qs), 2000); } else { $count = 1; $msg = "Product added to card"; \Cookie::queue('card', "[$product->id]", 2000); \Cookie::queue('q', "[$quantity]", 2000); $qs = [$quantity]; $cards = [$product->id]; } if ($count > 0 && auth('customer')->check()) { $customer = auth('customer')->user(); $customer->card = json_encode(['cards' => $cards, 'quantities' => $qs]); $customer->save(); } if (\request()->ajax()) { return success(['count' => $count], $msg); } else { return redirect()->back()->with(['message' => $msg]); } } public function index() { $area = 'card'; $title = __("Shopping card"); $subtitle = ''; return view('client.default-list', compact('area', 'title', 'subtitle')); } public function check(Request $request) { $request->validate([ 'product_id' => ['required', 'array'], 'count' => ['required', 'array'], 'address_id' => ['required', 'exists:addresses,id'], 'desc' => ['nullable', 'string'] ]); $total = 0; // return $request->all(); $inv = new Invoice(); $inv->customer_id = auth('customer')->user()->id; $inv->count = array_sum($request->count); $inv->address_id = $request->address_id; $inv->desc = $request->desc; if ($request->has('transport_id')) { $request->transport_id = $request->input('transport_id'); $t = Transport::find($request->input('transport_id')); $inv->transport_price = $t->price; $total += $t->price; } if ($request->has('discount_id')) { $request->discount_id = $request->input('discount_id'); } $inv->save(); foreach ($request->product_id as $i => $product) { $order = new Order(); $order->product_id = $product; $order->invoice_id = $inv->id; $order->count = $request->count[$i]; if ($request->quantity_id[$i] != '') { $order->quantity_id = $request->quantity_id[$i]; $q = Quantity::find($request->quantity_id[$i]); $order->price_total = $q->price * $request->count[$i]; $order->data = $q->data; } else { $p = Product::find($request->product_id[$i]); $order->price_total = $p->price * $request->count[$i]; } $total += $order->price_total; $order->save(); } $inv->total_price = $total; $inv->save(); return [$inv,$inv->orders]; } public static function clear() { \Cookie::expire('card'); \Cookie::expire('q'); return true; } public function clearing() { self::clear(); return __("Card cleared"); } public function discount($code) { $discount = Discount::where('code', trim($code))->where(function ($query) { $query->where('expire', '>=', date('Y-m-d')) ->orWhereNull('expire'); })->first(); if ($discount == null) { return [ 'OK' => false, 'err' => __("Discount code isn't valid."), ]; } else { if ($discount->type == 'PERCENT') { $human = $discount->title . '( ' . $discount->amount . '%' . ' )'; } else { $human = '- ' . $discount->title . '( ' . $discount->amount . config('app.currency.symbol') . ' )'; } return [ 'OK' => true, 'msg' => __("Discount code is valid."), 'data' => $discount, 'human' => $human, ]; } } }