public static function changeCodeToPercent($code)
 {
     $detail = DiscountCode::select(["percent"])->where("code", $code)->first();
     if (!is_null($detail)) {
         return $detail->percent;
     } else {
         return false;
     }
 }
 public function useCode(Request $request)
 {
     if ($request->ajax()) {
         $code = $request->get("code");
         $percent = DiscountCode::changeCodeToPercent($code);
         $cart = new CartController();
         $products = $cart->getProduct();
         $total = $cart->subTotalPrice($products);
         $total = $total * ($percent / 100);
         $total = number_format($total);
         Session::put("codeDiscount", $code);
         return $total;
     } else {
         return redirect()->route("home");
     }
 }
 public function totalPrice($subTotal, $payment_method, $discount_code)
 {
     $total = $subTotal;
     if (!is_null($discount_code)) {
         $percent = DiscountCode::changeCodeToPercent($discount_code);
         $total *= 1 - $percent / 100;
     }
     if (!is_null($payment_method)) {
     }
     return $total;
 }
 public function Finish(Request $request)
 {
     if (Auth::check() && Session::has("cart")) {
         $payment_method = (int) $request->get("method");
         $cart = new CartController();
         $products = $cart->getProduct();
         $subTotal = $cart->subTotalPrice($products);
         $discounted = 0;
         //phan tram dc giam
         if (Session::has("codeDiscount")) {
             $code = Session::get("codeDiscount");
             $discounted = DiscountCode::changeCodeToPercent($code);
             $total = $cart->totalPrice($subTotal, null, $code);
         } else {
             $total = $cart->totalPrice($subTotal, null, null);
         }
         $customerID = Auth::user()->id;
         $customerInfoID = Auth::user()->default_info_id;
         /* Status :
          * 1: done
          * 2:chua thanh toan
          * 3: moi
          */
         $bill = Bill::create(["total" => $total, "customer_id" => $customerID, "customer_info_id" => $customerInfoID, "status" => 3, "payment_method" => $payment_method, "discounted" => $discounted]);
         $billID = $bill->id;
         foreach ($products as $item) {
             $price = $item->price * (100 - $item->percent) / 100;
             //don gia
             billDetail::create(["bill_id" => $billID, "products_id" => $item->id, "price" => $price, "amount" => $item->so_luong]);
             $count = Products::select(["count"])->where("id", $item->id)->first();
             // so luong da~ bán
             $count->count += $item->so_luong;
             Products::where("id", $item->id)->update(["count" => $count->count]);
             //update so luong
         }
         /*Xoa discound code*/
         if (Session::has("codeDiscount")) {
             $code = Session::get("codeDiscount");
             DiscountCode::deleteCode($code);
             Session::forget("codeDiscount");
         }
         /* Xoá cart */
         Session::forget("cart");
         /*Send mail*/
         $this->sendMail($billID);
         /**********/
         return redirect()->route("thanhtoan.thongtin.hoadon", $billID);
     } else {
         return redirect()->route("home");
     }
 }