public function deleteProduct($id)
{
$supply = \App\Supply::find($id);
$product = \App\Product::where('kode_barang', $supply->kode_barang)->first();
$awal_dos = $product->stok / $product->pack;
$awal_pack = $product->stok % $product->pack;
$product->stok = $product->stok - $supply->jumlah;
if($product->stok < 0){
Session::flash('error', 'Stok kurang dari 0');
return;
}
$supply->status = 'batal';
$akhir_dos = $product->stok/$product->pack;
$akhir_pack = $product->stok%$product->pack;
$product->save();
$supply->save();
$pasok = \App\Supply::where('kode_pasok',$supply->kode_pasok)->where('status', 'lunas')->get();
$panjang_lama = strlen($supply->kode_pasok);
if($panjang_lama < 15 && $pasok->count() == 0){
$batal_pasok = \App\UsedCode::find(substr($supply->kode_pasok,5));
$batal_pasok->status_pasok = 0;
$batal_pasok->save();
}
$aktivitas_update_stok = new AktivitasUpdateStok;
$aktivitas_update_stok->id_user = Auth::id();
$aktivitas_update_stok->nama_user = Auth::user()->nama;
$aktivitas_update_stok->id_barang = $product->id;
$aktivitas_update_stok->kode_barang = $product->kode_barang;
$aktivitas_update_stok->nama_barang = $product->nama_barang;
$aktivitas_update_stok->jumlah = $supply->jumlah;
$aktivitas_update_stok->master_kemasan = (strtolower($supply->jenis_kemasan) == 'pack') ? 'Pack' : $product->master_kemasan;
$aktivitas_update_stok->pack = $product->pack;
$aktivitas_update_stok->kegiatan = 'Edit_Pasok';
$aktivitas_update_stok->keterangan =
'Pengurangan stok ' . $supply->nama_barang . ' - Kode Pasok = ' . $supply->kode_pasok . ' Stok Awal ( ' . (int) $awal_dos . ' ' . $product->master_kemasan . ' ' . (int) $awal_pack . ' pack ) Stok Akhir ( ' . (int) $akhir_dos . ' ' . $product->master_kemasan . ' ' . (int) $akhir_pack . ' pack )';
$aktivitas_update_stok->save();
//=================== Laporan stok =====================//
\App\LaporanStok::where('kode_aktivitas', $supply->kode_pasok)
->where('kode_barang', $product->kode_barang)
->where('jenis_kemasan',strtolower($supply->jenis_kemasan))
->update(['status_aktivitas' => 'batal']);
//=======================================================//
$payable = Payable::where('kode_pasok', $supply->kode_pasok)
->latest('created_at')->first();
if(isset($payable)){
$payable->total_pasok = $payable->total_pasok - $supply->subtotal;
$payable->bayar = $payable->bayar - $supply->subtotal;
$payable->save();
}
Session::flash('success', 'Data Barang '.$product->nama_barang .' Berhasil dibatalkan!');
}
public function deleteSupply($id)
{
$supplies = \App\Supply::where('kode_pasok', $id)->where('status', 'lunas')->get();
foreach($supplies as $no => $supply){
$product = \App\Product::where('kode_barang', $supply->kode_barang)->first();
$product->stok = $product->stok - $supply->jumlah;
if($product->stok < 0){
Session::flash('error', 'Stok kurang dari 0');
return;
}
}
foreach ($supplies as $no => $supply) {
$product = \App\Product::where('kode_barang', $supply->kode_barang)->first();
$awal_dos = $product->stok / $product->pack;
$awal_pack = $product->stok % $product->pack;
$product->stok = $product->stok - $supply->jumlah;
$supply->status = 'batal';
$akhir_dos = $product->stok/$product->pack;
$akhir_pack = $product->stok%$product->pack;
$product->save();
$supply->save();
$aktivitas_update_stok = new AktivitasUpdateStok;
$aktivitas_update_stok->id_user = Auth::id();
$aktivitas_update_stok->nama_user = Auth::user()->nama;
$aktivitas_update_stok->id_barang = $product->id;
$aktivitas_update_stok->kode_barang = $product->kode_barang;
$aktivitas_update_stok->nama_barang = $product->nama_barang;
$aktivitas_update_stok->jumlah = $supply->jumlah;
$aktivitas_update_stok->master_kemasan = (strtolower($supply->jenis_kemasan) == 'pack') ? 'Pack' : $product->master_kemasan;
$aktivitas_update_stok->pack = $product->pack;
$aktivitas_update_stok->kegiatan = 'Edit_Pasok';
$aktivitas_update_stok->keterangan =
'Pengurangan stok - Kode Pasok = ' . $supply->kode_pasok . ' Stok Awal ( ' . (int) $awal_dos . ' ' . $product->master_kemasan . ' ' . (int) $awal_pack . ' pack ) Stok Akhir ( ' . (int) $akhir_dos . ' ' . $product->master_kemasan . ' ' . (int) $akhir_pack . ' pack )';
$aktivitas_update_stok->save();
//=================== Laporan stok =====================//
\App\LaporanStok::where('kode_aktivitas', $id)
->where('kode_barang', $supply->kode_barang)
->where('jenis_kemasan',strtolower($supply->jenis_kemasan))
->update(['status_aktivitas' => 'batal']);
//=======================================================//
}
$pasok = \App\Supply::where('kode_pasok',$id)->where('status', 'lunas')->get();
$panjang_lama = strlen($id);
if($panjang_lama < 15 && $pasok->count() == 0){
$batal_pasok = \App\UsedCode::find(substr($id,5));
$batal_pasok->status_pasok = 0;
$batal_pasok->save();
}
$payable = Payable::where('kode_pasok', $id)->get();
if(isset($payable)){
foreach ($payable as $value) {
$value->status = 'batal';
$value->save();
}
}
Session::flash('success', 'Data Barang '.$supply->kode_pasok .' Berhasil dibatalkan!');
}