230 lines
7.3 KiB
PHP
230 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Cart;
|
|
use App\Models\Cawangan;
|
|
use App\Models\FileMarhun;
|
|
use App\Models\LelongBid;
|
|
use App\Models\LelongBidMarhun;
|
|
use Illuminate\Http\Request;
|
|
|
|
class CartController extends Controller
|
|
{
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
|
|
/**
|
|
* Show the application dashboard.
|
|
*
|
|
* @return \Illuminate\Contracts\Support\Renderable
|
|
*/
|
|
public function getCart($userid)
|
|
{
|
|
$carts = Cart::where('user_id', '=', $userid)->get()->toArray();
|
|
|
|
foreach ($carts as $index => $cart) {
|
|
$lelongbid = LelongBid::where('id', $cart['lelongbid_id'])->first()->toArray();
|
|
$marhun = LelongBidMarhun::where('lelongbid_id', '=', $lelongbid['id'])->get()->toArray();
|
|
$cawangan = Cawangan::where('id', '=', $lelongbid['caw_id'])->first()->toArray();
|
|
|
|
$carts[$index]['datalelongbid'] = $lelongbid;
|
|
$carts[$index]['datalelongbid']['lelongbidmarhun'] = $marhun;
|
|
$carts[$index]['datalelongbid']['cawangan'] = $cawangan;
|
|
}
|
|
|
|
return response()->json([
|
|
'carts' => $carts,
|
|
]);
|
|
}
|
|
|
|
public function getCartByCawangan($userId)
|
|
{
|
|
$groupCawangan = Cart::join('lelongbid', 'carts.lelongbid_id', '=', 'lelongbid.id')
|
|
->join('cawangan', 'lelongbid.caw_id', '=', 'cawangan.id')
|
|
->where('user_id', '=', $userId)
|
|
->select('caw_id', 'caw_name')
|
|
->groupBy('caw_id')
|
|
->get()->toArray();
|
|
|
|
$master_total_surat = 0;
|
|
$master_total_bidaan = 0;
|
|
|
|
foreach ($groupCawangan as $index => $cawangan) {
|
|
|
|
$cart = Cart::join('lelongbid', 'carts.lelongbid_id', '=', 'lelongbid.id')
|
|
->where('user_id', $userId)
|
|
->where('caw_id', $cawangan)
|
|
->get()->toArray();
|
|
|
|
$total_hargabida = 0;
|
|
$total_berat = 0;
|
|
$total_surat = 0;
|
|
|
|
foreach ($cart as $index1 => $data) {
|
|
$fileMarhun = FileMarhun::where('lelongbid_id', $data['id'])->where('photo_type', 1)->get();
|
|
if ($fileMarhun->isNotEmpty()) {
|
|
$cart[$index1]['filemarhun'] = $fileMarhun;
|
|
}
|
|
|
|
$tambahan_bidaan = $data['hargabida'] - $data['hargaasas'];
|
|
$total_hargabida += $data['hargabida'];
|
|
$total_berat += $data['berat'];
|
|
$total_surat += 1;
|
|
$marhun = LelongBidMarhun::where('lelongbid_id', '=', $data['id'])->get()->toArray();
|
|
|
|
$cart[$index1]['lelongbidmarhun'] = $marhun;
|
|
$cart[$index1]['tambahanbidaan'] = number_format((float) $tambahan_bidaan, 2, '.', '');
|
|
}
|
|
$groupCawangan[$index]['total_surat'] = $total_surat;
|
|
$groupCawangan[$index]['total_berat'] = number_format((float) $total_berat, 2, '.', '');
|
|
$groupCawangan[$index]['total_bidaan'] = number_format((float) $total_hargabida, 2, '.', '');
|
|
|
|
$groupCawangan[$index]['lelongbid'] = $cart;
|
|
|
|
$master_total_surat += $total_surat;
|
|
$master_total_bidaan += $total_hargabida;
|
|
}
|
|
|
|
if ($groupCawangan) {
|
|
return response()->json([
|
|
'success' => true,
|
|
'cart' => $groupCawangan,
|
|
'master_total_surat' => $master_total_surat,
|
|
'master_total_bidaan' => number_format((float) $master_total_bidaan, 2, '.', ''),
|
|
], 201);
|
|
} else {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Empty Data',
|
|
], 201);
|
|
}
|
|
}
|
|
|
|
public function addToCart(Request $request)
|
|
{
|
|
$lelong_bid = LelongBid::find($request->lelongbid_id, ['id', 'hargaasas']);
|
|
|
|
$beza_harga = $request->hargabida - $lelong_bid->hargaasas;
|
|
|
|
if ($beza_harga < 1) {
|
|
return response()->json([
|
|
'success' => 'gagal',
|
|
'status' => 9,
|
|
'message' => 'Harga bida kurang daripada harga asas.',
|
|
]);
|
|
}
|
|
|
|
$check_exist = Cart::where('lelongbid_id', $request->lelongbid_id)->where('user_id', $request->user_id)->exists();
|
|
if ($check_exist == true) {
|
|
return response()->json([
|
|
'success' => 'gagal_tambah_ke_troli',
|
|
'status' => 8,
|
|
'message' => 'SAG telah berada di dalam troli!',
|
|
]);
|
|
}
|
|
|
|
$item = new Cart;
|
|
$item->user_id = $request->user_id;
|
|
$item->lelongbid_id = $request->lelongbid_id;
|
|
$item->hargabida = $request->hargabida;
|
|
|
|
if ($item->save()) {
|
|
return response()->json([
|
|
'success' => true,
|
|
'carts' => $item,
|
|
]);
|
|
} else {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Error! Cannot save.',
|
|
], 401);
|
|
}
|
|
}
|
|
|
|
public function removeFromCart(Request $request)
|
|
{
|
|
$item = Cart::where('id', $request->cart_id)->first();
|
|
if ($item->delete()) {
|
|
return response()->json([
|
|
'success' => true,
|
|
'carts' => $item,
|
|
]);
|
|
} else {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Error! Cannot save.',
|
|
], 401);
|
|
}
|
|
}
|
|
|
|
public function updateItem(Request $request)
|
|
{
|
|
$cart = Cart::where('user_id', $request->userId)
|
|
->where('lelongbid_id', $request->lelongBidId)
|
|
->first();
|
|
|
|
$lelong_bid = LelongBid::find($request->lelongBidId, ['id', 'hargaasas']);
|
|
|
|
$beza_harga = $request->hargabida - $lelong_bid->hargaasas;
|
|
|
|
if ($beza_harga < 1) {
|
|
return response()->json([
|
|
'success' => 'gagal',
|
|
'status' => 9,
|
|
'message' => 'Harga bida kurang daripada harga asas.',
|
|
]);
|
|
}
|
|
|
|
$cart->hargabida = $request->hargabida;
|
|
if ($cart->save()) {
|
|
return response()->json([
|
|
'success' => true,
|
|
'cart' => $cart,
|
|
]);
|
|
} else {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Error! Cannot save.',
|
|
], 401);
|
|
}
|
|
}
|
|
|
|
public function deleteCart($id)
|
|
{
|
|
$cart = Cart::where('id', '=', $id)->first();
|
|
if ($cart->delete()) {
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Delete Success',
|
|
]);
|
|
} else {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Error! Cannot save.',
|
|
], 401);
|
|
}
|
|
}
|
|
|
|
public function deleteItem(Request $request)
|
|
{
|
|
$cart = Cart::where('user_id', $request->userId)
|
|
->where('lelongbid_id', $request->lelongBidId)
|
|
->first();
|
|
if ($cart->delete()) {
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Delete Success',
|
|
]);
|
|
} else {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Error! Cannot save.',
|
|
], 401);
|
|
}
|
|
}
|
|
}
|