Merge pull request 'fix: recalculate gadai totals after marhun edits' (#21) from fix/marhun-edit-gadai-totals into main
Reviewed-on: #21
This commit was merged in pull request #21.
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Marhun;
|
use App\Marhun;
|
||||||
|
use App\Gadai;
|
||||||
use App\Jemas;
|
use App\Jemas;
|
||||||
use App\Cawangan;
|
use App\Cawangan;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@@ -160,39 +161,53 @@ class MarhunController extends Controller
|
|||||||
|
|
||||||
$cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
|
$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 = [
|
$marhunToUpdate = [
|
||||||
'karat' => $request->karat,
|
'karat' => $request->karat,
|
||||||
'marhun' => $request->marhun,
|
'marhun' => $request->marhun,
|
||||||
'nota' => $request->nota,
|
'nota' => $request->nota,
|
||||||
'tag_permata' => $request->tag_permata,
|
'tag_permata' => $request->tag_permata,
|
||||||
'berat_batu_permata' => $request->berat_batu_permata,
|
'berat_batu_permata' => $request->berat_batu_permata,
|
||||||
'berat' => $request->berat,
|
'berat' => $request->berat,
|
||||||
'harga' => $harga['harga'],
|
'harga' => $harga['harga'],
|
||||||
'n_marhun' => $nilai_marhun,
|
'n_marhun' => $nilai_marhun,
|
||||||
'bil' => $request->kuantiti,
|
'bil' => $request->kuantiti,
|
||||||
];
|
];
|
||||||
|
|
||||||
// Set value berat_batu_permata to null if value passed is null
|
// Set value berat_batu_permata to null if value passed is null
|
||||||
if($marhunToUpdate['berat_batu_permata'] == '') {
|
if($marhunToUpdate['berat_batu_permata'] == '') {
|
||||||
$marhunToUpdate['berat_batu_permata'] = null;
|
$marhunToUpdate['berat_batu_permata'] = null;
|
||||||
};
|
};
|
||||||
|
|
||||||
$jemas = Marhun::on($cawangan['mysql'])->where([['recno','=',$request->recno],['norujukan','=',$norujukan]])->update($marhunToUpdate);
|
$jemas = Marhun::on($cawangan['mysql'])->where([['recno','=',$request->recno],['norujukan','=',$norujukan]])->update($marhunToUpdate);
|
||||||
|
|
||||||
$new_data = Marhun::on($cawangan['mysql'])->where([['recno','=',$request->recno],['norujukan','=',$norujukan]])->first();
|
// 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,
|
||||||
|
]);
|
||||||
|
|
||||||
$audit = new Audit();
|
$new_data = Marhun::on($cawangan['mysql'])->where([['recno','=',$request->recno],['norujukan','=',$norujukan]])->first();
|
||||||
$audit->users = $request->teller;
|
|
||||||
$audit->actions = 'Ubah Marhun';
|
$audit = new Audit();
|
||||||
$audit->norujukan = $norujukan;
|
$audit->users = $request->teller;
|
||||||
$audit->old_data = $old_data;
|
$audit->actions = 'Ubah Marhun';
|
||||||
$audit->new_data = $new_data;
|
$audit->norujukan = $norujukan;
|
||||||
$audit->kodcaw = $kodcaw;
|
$audit->old_data = $old_data;
|
||||||
$audit->created_at = $current;
|
$audit->new_data = $new_data;
|
||||||
$audit->updated_at = $current;
|
$audit->kodcaw = $kodcaw;
|
||||||
$audit->save();
|
$audit->created_at = $current;
|
||||||
|
$audit->updated_at = $current;
|
||||||
|
$audit->save();
|
||||||
|
});
|
||||||
|
|
||||||
return response('Edit Successfully', 200);
|
return response('Edit Successfully', 200);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user