224 lines
6.5 KiB
PHP
224 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Spatie\Permission\Models\Role;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use App\Models\User;
|
|
use PDF;
|
|
use App\Models\Cawangan;
|
|
use App\Models\JenisEmas;
|
|
use App\Models\JenisKarat;
|
|
|
|
use App\Models\Lelong;
|
|
use App\Models\CawanganLelong;
|
|
use App\Models\LelongBid;
|
|
use App\Models\LelongBidMarhun;
|
|
use App\Models\LelKarat;
|
|
use App\Models\Cart;
|
|
use Carbon\Carbon;
|
|
use App\Models\FileMarhun;
|
|
|
|
|
|
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)
|
|
{
|
|
$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();
|
|
|
|
$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);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|