Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (!empty($Transaksi)) {
- $produkDibeli = [];
- // variabel for difference promo price with main price
- $restPromo = 0;
- $grandRestPromo = 0;
- foreach ($request->input('produk') as $pesanan) {
- $idProduk = $pesanan['id'];
- $jumlahProduk = $pesanan['jumlah'];
- if ($jumlahProduk > 0) {
- if ($metode_pembayaran == 'tunai') {
- // update stok qty total header
- $produk = ProdukMitra::with('detail_satuan')->find($idProduk);
- $produk->stok = $produk->stok - $jumlahProduk;
- $produk->save();
- // update stok qty in detail with fifo method
- // check stok detail base on date
- $detailProdukMitra = DetailProdukMitra::where('produk_mitra_id', $idProduk)->orderBy('created_at', 'asc')->get();
- // set variable counter jmlBeli
- $jmlBeliTemp = 0;
- $jmlBeliTemp = $jumlahProduk;
- foreach ($detailProdukMitra as $k => $v) {
- if ($jmlBeliTemp > 0 ) {
- // check atribute qty_akhir > 0
- if ($v->qty_akhir > 0) {
- // product mitra single row selected by date
- $produkMitraSingle = DetailProdukMitra::find($v->_id);
- // check atribute qty_akhir <= jumlahBeli
- if ($jmlBeliTemp <= $v->qty_akhir) {
- $res = $v->qty_akhir - $jmlBeliTemp;
- $produkMitraSingle->qty_akhir = $res;
- $produkMitraSingle->save();
- // difference price promo and main price to wallet
- if ($produk->harga_promo > 0) {
- if ($produk->harga_promo < $v->harga_awal) {
- // find produk with promo
- $ProdukPromo = ProdukMitra::find($v->produk_mitra_id);
- $restPromo = ($v->harga_awal - $produk->harga_promo) * $jmlBeliTemp;
- $grandRestPromo += (int) $restPromo;
- // for email notification promo price
- $listProdukPromo[] = [
- 'name' => $ProdukPromo->name,
- 'jumlah' => (int) $jmlBeliTemp,
- 'harga_jual' => (int) $v->harga_awal,
- 'harga_promo' => (int) $ProdukPromo->harga_promo,
- 'subNominal' => (int) $restPromo,
- ];
- $TransaksiPromo = $listProdukPromo;
- }
- // problem jual 4, 1 dan 3
- // else {
- // $listProdukPromo = null;
- // }
- }
- else {
- $TransaksiPromo = null;
- }
- // reduce temp jmlBeli
- $jmlBeliTemp = $jmlBeliTemp - $jmlBeliTemp;
- }
- else {
- $res = $jmlBeliTemp - $v->qty_akhir;
- // update stok qty akhir detail
- $produkMitraSingle->qty_akhir = 0;
- $produkMitraSingle->save();
- if ($jmlBeliTemp > $v->qty_akhir){
- $jmlBeliTemp = $v->qty_akhir;
- }
- // difference price promo and main price to wallet
- if ($produk->harga_promo > 0) {
- if ($produk->harga_promo < $v->harga_awal) {
- // find produk with promo
- $ProdukPromo = ProdukMitra::find($v->produk_mitra_id);
- $restPromo = ($v->harga_awal - $produk->harga_promo) * $jmlBeliTemp;
- $grandRestPromo += (int) $restPromo;
- // for email notification promo price
- $listProdukPromo[] = [
- 'name' => $ProdukPromo->name,
- 'jumlah' => (int) $jmlBeliTemp,
- 'harga_jual' => (int) $v->harga_awal,
- 'harga_promo' => (int) $ProdukPromo->harga_promo,
- 'subNominal' => (int) $restPromo,
- ];
- $TransaksiPromo = $listProdukPromo;
- }
- // else {
- // $TransaksiPromo = null;
- // }
- }
- else {
- // $TransaksiPromo = $listProdukPromo ? $listProdukPromo : null;
- }
- // reduce temp jmlBeli
- $jmlBeliTemp = $res;
- }
- }
- }
- else {
- // $TransaksiPromo = $listProdukPromo ? $listProdukPromo : null;
- }
- }
- $TransaksiPromo = $listProdukPromo ? $listProdukPromo : null;
- }
- $dataProduk = [
- 'name' => $produk->name,
- 'jumlah' => $jumlahProduk,
- 'harga' => $produk->harga_promo > 0 ? $produk->harga_promo : $produk->harga_jual,
- 'berat_produk' => $produk->berat_kemasan,
- 'satuan' => $produk->detail_satuan->name,
- ];
- $produkDibeli[] = $dataProduk;
- // insert to detail transaction
- $detail_produk = [
- 'transaksi_id' => $Transaksi->_id,
- 'produk_id' => $idProduk,
- ];
- DetailTransaksi::create(array_merge($detail_produk, $dataProduk));
- }
- }
- if ($metode_pembayaran == 'tunai') {
- if ($TransaksiPromo != null) {
- // difference promo price with main price
- $userMitra = User::find($Transaksi->mitra_id);
- if ($userMitra) {
- $userMitra->uang = (int) $userMitra->uang + $grandRestPromo;
- $userMitra->save();
- // notif email difference price promo and main price to wallet
- Mail::send(new \App\Mail\SendNotifications(User::find($Transaksi->mitra_id), 'restPricePromo', $TransaksiPromo));
- // input to finance transcation(transaksi_keuangan)
- $deviationPrice = TransaksiKeuangan::create([
- 'user_id' => $Transaksi->mitra_id,
- 'transaksi_id' => $Transaksi->id,
- 'tanggal_transaksi' => date('Y-m-d'),
- 'tipe' => "OUT",
- 'nominal' => $grandRestPromo,
- 'saldo_akhir' => $userMitra->uang,
- 'tag' => "SELISIH PROMO",
- 'keterangan' => "Selisih Harga Promo dan Harga Pusat",
- 'status' => "DONE",
- ]);
- }
- }
- }
- $Transaksi->produk = $produkDibeli;
- //notifikasi pembayaran
- Mail::send(new \App\Mail\SendNotifications(Auth::User(), 'order_konsumen', $Transaksi));
- // kirim notif ke cfo
- $notif = WebConfig::where('name', 'notifCMO')->first();
- $notif->value = 1;
- $notif->save();
- return response()->json([
- 'success' => true,
- 'messages' => 'Add Transaksi Success !',
- 'trxcode' => "USR001", //user belanja via pos
- 'data' => $Transaksi,
- ], 201);
- } else {
- return response()->json([
- 'success' => false,
- 'messages' => 'Can\'t Insert Transaksi!',
- 'data' => null,
- ], 200);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement