Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logic error in sales controller and partial backend validation #48

Merged
merged 2 commits into from
Jun 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions app/Http/Controllers/DetailSaleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Models\DetailSale;
use App\Models\Sale;
use App\Models\Medicine;
use Illuminate\View\View;


class DetailSaleController extends Controller
{
public function store(Request $request)
public function store(Request $request): JsonResponse
{
$saleId = $request->input('sale_id');
$is_success = $request->input('is_success');
Expand All @@ -20,6 +22,10 @@ public function store(Request $request)
$discounts = $request->input('discount');
$subtotals = $request->input('subtotal');

// Var to make sure database is correct
$totalPrice = 0;
$totalDiscount = 0;

// Check stock
$allStockOk = true;
$stockMessage = [];
Expand All @@ -35,6 +41,11 @@ public function store(Request $request)
}

if(!$allStockOk){
$sale = Sale::find($saleId);
$sale->is_success = 0;
$sale->change = 0;
$sale->cash = 0;
$sale->save();
return response()->json(['message' => $stockMessage,'status' => 'error']);
}

Expand All @@ -47,17 +58,48 @@ public function store(Request $request)
$detailSale->price = $prices[$i];
$detailSale->discount = $discounts[$i];
$detailSale->subtotal = $subtotals[$i];
$totalPrice += $subtotals[$i];
$totalDiscount += $discounts[$i];

$detailSale->save();
if ($is_success) {
$meds = Medicine::find($detailSale->medicine_id);
}

// Update total price, discount, cash, change
if($is_success){
$isWeird = false;
$sale = Sale::find($saleId);

if($sale->total != $totalPrice){
$sale->total = $totalPrice;
$isWeird = true;
}

if($sale->discount != $totalDiscount){
$sale->discount = $totalDiscount;
$isWeird = true;
}

if($sale->cash < $totalPrice) {
$sale->is_success = 0;
$sale->change = 0;
$sale->cash = 0;
$sale->save();
return response()->json(['message' => 'HAYO NGECIT']);
}
}

// Decrease medicine if it's 100% sure
if ($is_success) {
for($i = 0; $i < count($medicineIds); $i++){
$meds = Medicine::find($medicineIds[$i]);
$meds->decrement('quantity', $quantities[$i]);
}
}

return response()->json(['message' => 'Transaction details added successfully!'. ($is_success? ' (PAID) ':' (CANCELLED) ')]);
}

public function show(Sale $sale, Request $request)
public function show(Sale $sale, Request $request): View
{
$search = $request->input('search');

Expand Down