update export lelong to api
This commit is contained in:
@@ -1069,44 +1069,79 @@ class LelongController extends Controller
|
|||||||
|
|
||||||
public function exportLelong(Request $request)
|
public function exportLelong(Request $request)
|
||||||
{
|
{
|
||||||
$currentDate = Carbon::now();
|
$currentDate = Carbon::now()->toDateString();
|
||||||
$currentDate = $currentDate->toDateString();
|
|
||||||
|
|
||||||
$cawangan = Cawangan::on('mysql7')->where('kodcaw', $request->kodcaw)->first();
|
// --- Step 0: Dapatkan Cawangan ---
|
||||||
|
$cawangan = Cawangan::on('mysql7')
|
||||||
|
->where('kodcaw', $request->kodcaw)
|
||||||
|
->first();
|
||||||
|
|
||||||
$batchlel = BatchLel::on($cawangan['mysql'])
|
if (!$cawangan) {
|
||||||
->select('batchno')
|
Log::error("Export Lelong gagal: cawangan tidak ditemui", ['kodcaw' => $request->kodcaw]);
|
||||||
->orderBy('batchno', 'desc')
|
return response()->json(['error' => 'Cawangan tidak ditemui'], 404);
|
||||||
->first();
|
}
|
||||||
|
|
||||||
$newBatchNo = $batchlel->batchno + 1;
|
Log::info("Mula Export Lelong", [
|
||||||
|
'kodcaw' => $cawangan->kodcaw,
|
||||||
$newBatchLel = BatchLel::on($cawangan['mysql'])->create([
|
'nama_cawangan' => $cawangan->details_caw,
|
||||||
'kodcaw' => $request->kodcaw,
|
|
||||||
'batchno' => $newBatchNo,
|
|
||||||
'tarikh' => $request->tarikh,
|
|
||||||
'tarikh2' => $request->tarikh,
|
|
||||||
't_daftar' => $currentDate,
|
|
||||||
'masadari' => $request->masadari,
|
|
||||||
'masahingga' => $request->masahingga
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$lelong = Lelong::on($cawangan['mysql'])
|
// --- Step 1: Batch (Cegah duplicate) ---
|
||||||
->select('sirilel')
|
$batchlel = BatchLel::on($cawangan['mysql'])
|
||||||
->orderBy('batchno', 'desc')
|
->where('kodcaw', $request->kodcaw)
|
||||||
->first();
|
->whereDate('tarikh', $request->tarikh)
|
||||||
|
->where('masadari', $request->masadari)
|
||||||
|
->where('masahingga', $request->masahingga)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if (!$batchlel) {
|
||||||
|
$lastBatchNo = BatchLel::on($cawangan['mysql'])->max('batchno') ?? 0;
|
||||||
|
$newBatchNo = $lastBatchNo + 1;
|
||||||
|
|
||||||
|
$batchlel = BatchLel::on($cawangan['mysql'])->create([
|
||||||
|
'kodcaw' => $request->kodcaw,
|
||||||
|
'batchno' => $newBatchNo,
|
||||||
|
'tarikh' => $request->tarikh,
|
||||||
|
'tarikh2' => $request->tarikh,
|
||||||
|
't_daftar' => $currentDate,
|
||||||
|
'masadari' => $request->masadari,
|
||||||
|
'masahingga' => $request->masahingga
|
||||||
|
]);
|
||||||
|
|
||||||
|
Log::info("Batch baru dicipta", ['batchno' => $newBatchNo]);
|
||||||
|
} else {
|
||||||
|
$newBatchNo = $batchlel->batchno;
|
||||||
|
Log::info("Batch sedia ada digunakan", ['batchno' => $newBatchNo]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Step 2: Siri Lelong Terakhir ---
|
||||||
|
$lastSiriLelong = Lelong::on($cawangan['mysql'])->max('sirilel') ?? 0;
|
||||||
|
$lelong_arr = [];
|
||||||
|
|
||||||
|
// --- Step 3: Cegah duplicate Lelong ---
|
||||||
|
$existingNorujukan = Lelong::on($cawangan['mysql'])
|
||||||
|
->whereIn('norujukan', collect($request->lelongbid)->pluck('norujukan'))
|
||||||
|
->pluck('norujukan')
|
||||||
|
->toArray();
|
||||||
|
|
||||||
foreach ($request->lelongbid as $index) {
|
foreach ($request->lelongbid as $index) {
|
||||||
|
if (in_array($index['norujukan'], $existingNorujukan)) {
|
||||||
|
Log::warning("Skip Lelong duplicate", ['norujukan' => $index['norujukan']]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$no_pembeli = Pembeli::on($cawangan['mysql'])->where('nokp', '=', $index['user_details']['nokp'])->select('nopembeli')->first();
|
// --- Step 4: Semak / cipta pembeli ---
|
||||||
if ($no_pembeli) {
|
$no_pembeli = Pembeli::on($cawangan['mysql'])
|
||||||
$no_pembeli_data = $no_pembeli->nopembeli;
|
->where('nokp', $index['user_details']['nokp'])
|
||||||
} else {
|
->value('nopembeli');
|
||||||
$last_no_pembeli = Pembeli::on($cawangan['mysql'])->select('nopembeli')->orderBy('nopembeli', 'desc')->first();
|
|
||||||
$no_pembeli_data = $last_no_pembeli->nopembeli + 1;
|
|
||||||
|
|
||||||
$newPembeli = Pembeli::on($cawangan['mysql'])->create([
|
if (!$no_pembeli) {
|
||||||
'nopembeli' => $no_pembeli_data,
|
$last_no_pembeli = Pembeli::on($cawangan['mysql'])
|
||||||
|
->max('nopembeli') ?? 0;
|
||||||
|
$no_pembeli = $last_no_pembeli + 1;
|
||||||
|
|
||||||
|
Pembeli::on($cawangan['mysql'])->create([
|
||||||
|
'nopembeli' => $no_pembeli,
|
||||||
'nokp' => $index['user_details']['nokp'],
|
'nokp' => $index['user_details']['nokp'],
|
||||||
'nama' => $index['user_details']['name'],
|
'nama' => $index['user_details']['name'],
|
||||||
'alamat1' => $index['user_details']['alamat'],
|
'alamat1' => $index['user_details']['alamat'],
|
||||||
@@ -1114,105 +1149,147 @@ class LelongController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$update_gadai = Gadai::on($cawangan['mysql'])->where('norujukan','=',$index['norujukan'])->update(array(
|
// --- Step 5: Update gadai ---
|
||||||
'sttebus' => "L",
|
Gadai::on($cawangan['mysql'])
|
||||||
't_update' => $request->tarikh
|
->where('norujukan', $index['norujukan'])
|
||||||
));
|
->update([
|
||||||
|
'sttebus' => "L",
|
||||||
|
't_update' => $request->tarikh
|
||||||
|
]);
|
||||||
|
|
||||||
$lelong_to_be_inserted = new Lelong();
|
// --- Step 6: Simpan data Lelong ---
|
||||||
$lelong_to_be_inserted->batchno = $newBatchNo;
|
$lelong_arr[] = [
|
||||||
$lelong_to_be_inserted->sirilel = $lelong->sirilel + 1;
|
'batchno' => $newBatchNo,
|
||||||
$lelong_to_be_inserted->kodcaw = $index['kodcaw'];
|
'sirilel' => ++$lastSiriLelong,
|
||||||
$lelong_to_be_inserted->norujukan = $index['norujukan'];
|
'kodcaw' => $index['kodcaw'],
|
||||||
$lelong_to_be_inserted->t_lelong = $request->tarikh;
|
'norujukan' => $index['norujukan'],
|
||||||
$lelong_to_be_inserted->hargaasas = $index['hargaasas'];
|
't_lelong' => $request->tarikh,
|
||||||
$lelong_to_be_inserted->hargajual = $index['hargajual'];
|
'hargaasas' => $index['hargaasas'],
|
||||||
$lelong_to_be_inserted->glcode = "1000/010";
|
'hargajual' => $index['hargajual'],
|
||||||
$lelong_to_be_inserted->t_jual = $index['t_jual'];
|
'glcode' => "1000/010",
|
||||||
$lelong_to_be_inserted->nopembeli = $no_pembeli_data;
|
't_jual' => $index['t_jual'],
|
||||||
$lelong_to_be_inserted->jbg = $index['jbg'];
|
'nopembeli' => $no_pembeli,
|
||||||
$lelong_to_be_inserted->kadarupah = $index['kadarupah'];
|
'jbg' => $index['jbg'],
|
||||||
$lelong_to_be_inserted->upahsimpan = $index['upahsimpan'];
|
'kadarupah' => $index['kadarupah'],
|
||||||
$lelong_to_be_inserted->ansuran = $index['ansuran'];
|
'upahsimpan' => $index['upahsimpan'],
|
||||||
$lelong_to_be_inserted->nilaimarhun = $index['nilaimarhun'];
|
'ansuran' => $index['ansuran'],
|
||||||
$lelong_to_be_inserted->nilaimarhuns = $index['nmarhuns'];
|
'nilaimarhun' => $index['nilaimarhun'],
|
||||||
$lelong_to_be_inserted->pinjaman = $index['pinjaman'];
|
'nilaimarhuns' => $index['nmarhuns'],
|
||||||
$lelong_to_be_inserted->bakiupah = $index['bakiupah'];
|
'pinjaman' => $index['pinjaman'],
|
||||||
$lelong_to_be_inserted->cajlelong = $index['cajlelong'];
|
'bakiupah' => $index['bakiupah'],
|
||||||
$lelong_to_be_inserted->baki = $index['baki'];
|
'cajlelong' => $index['cajlelong'],
|
||||||
$lelong_to_be_inserted->teller = $index['teller'];
|
'baki' => $index['baki'],
|
||||||
$lelong_to_be_inserted->prkod = "01";
|
'teller' => $index['teller'],
|
||||||
$lelong_to_be_inserted->kodgl = "1000/013";
|
'prkod' => "01",
|
||||||
$lelong_to_be_inserted->berat = $index['berat'];
|
'kodgl' => "1000/013",
|
||||||
$lelong_to_be_inserted->marhun = $index['marhun'];
|
'berat' => $index['berat'],
|
||||||
$lelong_to_be_inserted->fasa = $index['fasa'];
|
'marhun' => $index['marhun'],
|
||||||
$lelong_to_be_inserted->ujrah = $index['ujrah'];
|
'fasa' => $index['fasa'],
|
||||||
$lelong_to_be_inserted->onepercent = $index['onepercent'];
|
'ujrah' => $index['ujrah'],
|
||||||
$lelong_to_be_inserted->tagujrah = $index['tagujrah'];
|
'onepercent' => $index['onepercent'],
|
||||||
|
'tagujrah' => $index['tagujrah'],
|
||||||
$lelong_arr[] = $lelong_to_be_inserted->attributesToArray();
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
Lelong::on($cawangan['mysql'])->insert($lelong_arr);
|
if ($lelong_arr) {
|
||||||
|
Lelong::on($cawangan['mysql'])->insert($lelong_arr);
|
||||||
foreach ($request->lelongbidmarhun as $data) {
|
Log::info("Berjaya simpan lelong baru", ['total' => count($lelong_arr)]);
|
||||||
$lelmarhun_to_be_inserted = new LelMarhun();
|
|
||||||
$lelmarhun_to_be_inserted->kodcaw = $request->kodcaw;
|
|
||||||
$lelmarhun_to_be_inserted->norujukan = $data['norujukan'];
|
|
||||||
$lelmarhun_to_be_inserted->bil = $data['bil'];
|
|
||||||
$lelmarhun_to_be_inserted->marhun = $data['marhun'];
|
|
||||||
$lelmarhun_to_be_inserted->nota = $data['nota'];
|
|
||||||
$lelmarhun_to_be_inserted->karat = $data['karat'];
|
|
||||||
$lelmarhun_to_be_inserted->berat = $data['berat'];
|
|
||||||
$lelmarhun_to_be_inserted->harga = $data['harga'];
|
|
||||||
$lelmarhun_to_be_inserted->n_marhun = $data['n_marhun'];
|
|
||||||
$lelmarhun_to_be_inserted->hargaasas = $data['hargaasas'];
|
|
||||||
$lelmarhun_to_be_inserted->batch_no = $newBatchNo;
|
|
||||||
// $lelmarhun_to_be_inserted->beratasal = $;
|
|
||||||
|
|
||||||
$lelmarhun_arr[] = $lelmarhun_to_be_inserted->attributesToArray();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LelMarhun::on($cawangan['mysql'])->insert($lelmarhun_arr);
|
// --- Step 7: LELMARHUN (cegah duplicate) ---
|
||||||
|
if ($request->lelongbidmarhun) {
|
||||||
|
$existingMarhun = LelMarhun::on($cawangan['mysql'])
|
||||||
|
->whereIn('norujukan', collect($request->lelongbidmarhun)->pluck('norujukan'))
|
||||||
|
->pluck('norujukan')
|
||||||
|
->toArray();
|
||||||
|
|
||||||
if ($request->tunai != null) {
|
$lelmarhun_arr = collect($request->lelongbidmarhun)
|
||||||
$tunai = Tunai::on($cawangan['mysql'])->create([
|
->reject(fn($data) => in_array($data['norujukan'], $existingMarhun))
|
||||||
'tarikh' => $request->tarikh,
|
->map(function ($data) use ($request, $newBatchNo) {
|
||||||
'kodcaw' => $request->kodcaw,
|
return [
|
||||||
'kodlaluan' => "LELONG",
|
'kodcaw' => $request->kodcaw,
|
||||||
'masuk' => $request->tunai,
|
'norujukan' => $data['norujukan'],
|
||||||
'baki' => $request->tunai,
|
'bil' => $data['bil'],
|
||||||
]);
|
'marhun' => $data['marhun'],
|
||||||
|
'nota' => $data['nota'],
|
||||||
|
'karat' => $data['karat'],
|
||||||
|
'berat' => $data['berat'],
|
||||||
|
'harga' => $data['harga'],
|
||||||
|
'n_marhun' => $data['n_marhun'],
|
||||||
|
'hargaasas' => $data['hargaasas'],
|
||||||
|
'batch_no' => $newBatchNo
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
if ($lelmarhun_arr) {
|
||||||
|
LelMarhun::on($cawangan['mysql'])->insert($lelmarhun_arr);
|
||||||
|
Log::info("LelMarhun baru disimpan", ['total' => count($lelmarhun_arr)]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($request->gldetail != null) {
|
// --- Step 8: GL dan Tunai (check duplicate descpt) ---
|
||||||
$gldetail = GlDetail::on($cawangan['mysql'])->create([
|
if ($request->tunai > 0) {
|
||||||
'kodcaw' => $request->kodcaw,
|
$existsTunai = Tunai::on($cawangan['mysql'])
|
||||||
'tarikh' => $request->tarikh,
|
->where('kodcaw', $request->kodcaw)
|
||||||
'dtacct' => "1000/010",
|
->where('tarikh', $request->tarikh)
|
||||||
'ktacct' => "1000/020",
|
->where('kodlaluan', 'LELONG')
|
||||||
'descpt' => "SERAHAN LELONG ONLINE, " . $request->tarikh,
|
->exists();
|
||||||
'amaun' => $request->gldetail,
|
|
||||||
'teller' => "LELONG ONLINE",
|
if (!$existsTunai) {
|
||||||
'serahan' => $request->gldetail,
|
Tunai::on($cawangan['mysql'])->create([
|
||||||
]);
|
'tarikh' => $request->tarikh,
|
||||||
|
'kodcaw' => $request->kodcaw,
|
||||||
|
'kodlaluan' => "LELONG",
|
||||||
|
'masuk' => $request->tunai,
|
||||||
|
'baki' => $request->tunai,
|
||||||
|
]);
|
||||||
|
Log::info("Rekod Tunai lelong dimasukkan");
|
||||||
|
} else {
|
||||||
|
Log::warning("Skip Tunai duplicate");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($request->gldetailcdm != null) {
|
$glDescs = [
|
||||||
GlDetail::on($cawangan['mysql'])->create([
|
'SERAHAN LELONG ONLINE' => $request->gldetail,
|
||||||
'kodcaw' => $request->kodcaw,
|
'SERAHAN LELONG CDM' => $request->gldetailcdm,
|
||||||
'tarikh' => $request->tarikh,
|
];
|
||||||
'dtacct' => "1000/010",
|
|
||||||
'ktacct' => "1000/020",
|
foreach ($glDescs as $desc => $amount) {
|
||||||
'descpt' => "SERAHAN LELONG CDM, " . $request->tarikh,
|
if ($amount > 0) {
|
||||||
'amaun' => $request->gldetailcdm,
|
$existsGl = GlDetail::on($cawangan['mysql'])
|
||||||
'teller' => "LELONG ONLINE",
|
->where('kodcaw', $request->kodcaw)
|
||||||
'serahan' => $request->gldetailcdm,
|
->where('tarikh', $request->tarikh)
|
||||||
]);
|
->where('descpt', 'like', "%{$desc}%")
|
||||||
|
->exists();
|
||||||
|
|
||||||
|
if (!$existsGl) {
|
||||||
|
GlDetail::on($cawangan['mysql'])->create([
|
||||||
|
'kodcaw' => $request->kodcaw,
|
||||||
|
'tarikh' => $request->tarikh,
|
||||||
|
'dtacct' => "1000/010",
|
||||||
|
'ktacct' => "1000/020",
|
||||||
|
'descpt' => "{$desc}, {$request->tarikh}",
|
||||||
|
'amaun' => $amount,
|
||||||
|
'teller' => "LELONG ONLINE",
|
||||||
|
'serahan' => $amount,
|
||||||
|
]);
|
||||||
|
Log::info("Rekod GL dimasukkan", ['desc' => $desc]);
|
||||||
|
} else {
|
||||||
|
Log::warning("Skip GL duplicate", ['desc' => $desc]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json("success");
|
// --- Step 9: Log Akhir ---
|
||||||
|
Log::info('Berjaya Export Cawangan:', [
|
||||||
|
'kodcaw' => $cawangan->kodcaw,
|
||||||
|
'nama_cawangan' => $cawangan->details_caw,
|
||||||
|
'batch_baru' => $newBatchNo,
|
||||||
|
'total_lelong' => count($lelong_arr),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json(['status' => 'success']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRingkasanHarian(Request $request){
|
public function getRingkasanHarian(Request $request){
|
||||||
|
|||||||
Reference in New Issue
Block a user