Skip to content

Commit

Permalink
Merge pull request #48 from alfikiafan/afif
Browse files Browse the repository at this point in the history
Fix logic error in sales and partial validation
  • Loading branch information
alscheift authored Jun 17, 2023
2 parents ac69df9 + a144c3a commit 3175d75
Showing 1 changed file with 46 additions and 4 deletions.
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

0 comments on commit 3175d75

Please sign in to comment.