fix: recalculate gadai totals after marhun edits #21
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Marhun;
|
||||
use App\Gadai;
|
||||
use App\Jemas;
|
||||
use App\Cawangan;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -160,7 +161,10 @@ class MarhunController extends Controller
|
||||
|
||||
$cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
|
||||
|
||||
$old_data = Marhun::on($cawangan['mysql'])->where([['recno','=',$request->recno],['norujukan','=',$norujukan]])->first();
|
||||
// Lock the parent first so concurrent edits to this SAG are serialized.
|
||||
(new Marhun)->setConnection($cawangan['mysql'])->getConnection()->transaction(function () use ($cawangan, $norujukan, $request, $harga, $nilai_marhun, $kodcaw, $current) {
|
||||
Gadai::on($cawangan['mysql'])->where('norujukan', $norujukan)->lockForUpdate()->firstOrFail();
|
||||
$old_data = Marhun::on($cawangan['mysql'])->where([['recno','=',$request->recno],['norujukan','=',$norujukan]])->firstOrFail();
|
||||
|
||||
$marhunToUpdate = [
|
||||
'karat' => $request->karat,
|
||||
@@ -181,6 +185,16 @@ class MarhunController extends Controller
|
||||
|
||||
$jemas = Marhun::on($cawangan['mysql'])->where([['recno','=',$request->recno],['norujukan','=',$norujukan]])->update($marhunToUpdate);
|
||||
|
||||
// Recalculate from every item belonging to this SAG, after the edit.
|
||||
$totals = Marhun::on($cawangan['mysql'])->where('norujukan', $norujukan)
|
||||
->selectRaw('COALESCE(SUM(berat), 0) AS total_berat, COALESCE(SUM(n_marhun), 0) AS total_nilai, COALESCE(SUM(bil), 0) AS total_kuantiti')
|
||||
->first();
|
||||
Gadai::on($cawangan['mysql'])->where('norujukan', $norujukan)->update([
|
||||
'berat' => $totals->total_berat,
|
||||
'nilaimarhun' => $totals->total_nilai,
|
||||
'kuantiti' => $totals->total_kuantiti,
|
||||
]);
|
||||
|
||||
$new_data = Marhun::on($cawangan['mysql'])->where([['recno','=',$request->recno],['norujukan','=',$norujukan]])->first();
|
||||
|
||||
$audit = new Audit();
|
||||
@@ -193,6 +207,7 @@ class MarhunController extends Controller
|
||||
$audit->created_at = $current;
|
||||
$audit->updated_at = $current;
|
||||
$audit->save();
|
||||
});
|
||||
|
||||
return response('Edit Successfully', 200);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user