app/Http/Controllers/CheckoutController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $id)
    {
        $donhang = donDatHang::findOrFail($id);
        switch ($request->trangthai) {
            case "Đã Hoàn Thành":
                if($request->thanhtoan != 1):
                    return back()
                        ->with('errors','Đơn chưa thanh toán');
                endif;
                $soluong = DB::table('chi_tiet_don_dat_hangs')
                    ->where('id_donhang', $donhang->madonhang)->sum('soluong');
                $ctdhs = DB::table('chi_tiet_don_dat_hangs')
                    ->join('phieu_bao_hanhs', 'phieu_bao_hanhs.id_chitietdonhang', '=', 'chi_tiet_don_dat_hangs.id')
                    ->select('phieu_bao_hanhs.*')
                    ->where('id_donhang', $donhang->madonhang)->count();
                if($ctdhs<$soluong):
                    return back()
                        ->with('errors','Có sản phẩm chưa lập phiếu bảo hành');
                endif;
                break;
            case "Đã Hủy":
                $ctdhs = DB::table('chi_tiet_don_dat_hangs')
                    ->join('phieu_bao_hanhs', 'phieu_bao_hanhs.id_chitietdonhang', '=', 'chi_tiet_don_dat_hangs.id')
                    ->select('phieu_bao_hanhs.*')
                    ->where('id_donhang', $donhang->madonhang)->count();
                if($ctdhs>0):
                    return back()
                        ->with('errors','Đơn không thể hủy do đã lập phiếu bảo hành');
                endif;
                $sanphams = DB::table('chi_tiet_don_dat_hangs')
                   ->where('id_donhang', $donhang->madonhang)->get();
                $result = json_decode($sanphams, true);
                if($result!=''):
                    foreach ($result as $item):
                        $sp = sanPham::findOrFail($item['id_sanpham']);
                        $sp->soluongton = $sp->soluongton + $item['soluong'];
                        $sp->save();
                    endforeach;
                endif;
                break;
            default:
                break;
        }
        $donhang->trangthai = $request->trangthai;
        $donhang->trangthai_thanhtoan = $request->thanhtoan;
        $donhang->tenkhachhang = $request->tenkhachhang?$request->tenkhachhang:$donhang->tenkhachhang;
        $donhang->sodienthoai = $request->sodienthoai?$request->sodienthoai:$donhang->sodienthoai;
        $donhang->email = $request->email?$request->email:$donhang->email;
        $donhang->diachi = $request->diachi?$request->diachi:$donhang->diachi;
        $donhang->save();
        return back()
            ->with('success','Đã lưu xuống cơ sở dữ liệu');
    }
public function update ( Request $ request , $ id ) { $ donhang = donDatHang :: findOrFail ( $ id ) ; switch ( $ request -> trangthai ) { case " Đã Hoàn Thành": if ( $ request -> thanhtoan != 1 ) : return back ( ) -> with ( 'errors' , 'Đơn chưa thanh toán'); endif ; $ soluong = DB :: table ( 'chi_tiet_don_dat_hangs' ) -> where ( 'id_donhang' , $ donhang -> madonhang ) -> sum ( 'soluong' ) ; $ ctdhs = DB :: table ( 'chi_tiet_don_dat_hangs' ) -> join ( 'phieu_bao_hanhs' , 'phieu_bao_hanhs.id_chitietdonhang' , '=' , 'chi_tiet_don_dat_hangs.id' ) -> select ( 'phieu_bao_hanhs.*' ) -> where ( 'id_donhang' , $ donhang -> madonhang ) -> count ( ) ; if ( $ ctdhs < $ soluong ) : return back ( ) -> with ( 'errors' , 'Có sản phẩm chưa lập phiếu bảo hành'); endif ; break ; case " Đã Hủy": $ ctdhs = DB :: table ( 'chi_tiet_don_dat_hangs' ) -> join ( 'phieu_bao_hanhs' , 'phieu_bao_hanhs.id_chitietdonhang' , '=' , 'chi_tiet_don_dat_hangs.id' ) -> select ( 'phieu_bao_hanhs.*' ) -> where ( 'id_donhang' , $ donhang -> madonhang ) -> count ( ) ; if ( $ ctdhs > 0 ) : return back ( ) -> with ( 'errors' , 'Đơn không thể hủy do đã lập phiếu bảo hành'); endif ; $ sanphams = DB :: table ( 'chi_tiet_don_dat_hangs' ) -> where ( 'id_donhang' , $ donhang -> madonhang ) -> get ( ) ; $ result = json_decode ( $ sanphams , true ) ; if ( $ result != '' ) : foreach ( $ result as $ item ) : $ sp = sanPham :: findOrFail ( $ item [ 'id_sanpham' ] ) ; $ sp -> soluongton = $ sp -> soluongton + $ item [ 'soluong' ] ; $ sp -> save ( ) ; endforeach ; endif ; break ; default : break ; } $ donhang -> trangthai = $ request -> trangthai ; $ donhang -> trangthai_thanhtoan = $ request -> thanhtoan ; $ donhang -> tenkhachhang = $ request -> tenkhachhang ? $ request -> tenkhachhang : $ donhang -> tenkhachhang ; $ donhang -> sodienthoai = $ request -> sodienthoai ? $ request -> sodienthoai : $ donhang -> sodienthoai ; $ donhang -> email = $ request -> email ? $ request -> email : $ donhang -> email ; $ donhang -> diachi = $ request -> diachi ? $ request -> diachi : $ donhang -> diachi ; $ donhang -> save ( ) ; return back ( ) -> with ( 'success' , 'Đã lưu xuống cơ sở dữ liệu'); }
Update the specified resource in storage.
Update the specified resource in storage .
//