SHOW:
|
|
- or go back to the newest paste.
1 | if (!empty($Transaksi)) { | |
2 | $produkDibeli = []; | |
3 | ||
4 | // variabel for difference promo price with main price | |
5 | $restPromo = 0; | |
6 | $grandRestPromo = 0; | |
7 | foreach ($request->input('produk') as $pesanan) { | |
8 | $idProduk = $pesanan['id']; | |
9 | $jumlahProduk = $pesanan['jumlah']; | |
10 | ||
11 | if ($jumlahProduk > 0) { | |
12 | if ($metode_pembayaran == 'tunai') { | |
13 | // update stok qty total header | |
14 | $produk = ProdukMitra::with('detail_satuan')->find($idProduk); | |
15 | $produk->stok = $produk->stok - $jumlahProduk; | |
16 | $produk->save(); | |
17 | ||
18 | // update stok qty in detail with fifo method | |
19 | // check stok detail base on date | |
20 | $detailProdukMitra = DetailProdukMitra::where('produk_mitra_id', $idProduk)->orderBy('created_at', 'asc')->get(); | |
21 | ||
22 | // set variable counter jmlBeli | |
23 | $jmlBeliTemp = 0; | |
24 | $jmlBeliTemp = $jumlahProduk; | |
25 | foreach ($detailProdukMitra as $k => $v) { | |
26 | if ($jmlBeliTemp > 0 ) { | |
27 | // check atribute qty_akhir > 0 | |
28 | if ($v->qty_akhir > 0) { | |
29 | // product mitra single row selected by date | |
30 | $produkMitraSingle = DetailProdukMitra::find($v->_id); | |
31 | ||
32 | // check atribute qty_akhir <= jumlahBeli | |
33 | if ($jmlBeliTemp <= $v->qty_akhir) { | |
34 | $res = $v->qty_akhir - $jmlBeliTemp; | |
35 | $produkMitraSingle->qty_akhir = $res; | |
36 | $produkMitraSingle->save(); | |
37 | ||
38 | // difference price promo and main price to wallet | |
39 | if ($produk->harga_promo > 0) { | |
40 | if ($produk->harga_promo < $v->harga_awal) { | |
41 | // find produk with promo | |
42 | $ProdukPromo = ProdukMitra::find($v->produk_mitra_id); | |
43 | $restPromo = ($v->harga_awal - $produk->harga_promo) * $jmlBeliTemp; | |
44 | $grandRestPromo += (int) $restPromo; | |
45 | ||
46 | // for email notification promo price | |
47 | $listProdukPromo[] = [ | |
48 | 'name' => $ProdukPromo->name, | |
49 | 'jumlah' => (int) $jmlBeliTemp, | |
50 | 'harga_jual' => (int) $v->harga_awal, | |
51 | 'harga_promo' => (int) $ProdukPromo->harga_promo, | |
52 | 'subNominal' => (int) $restPromo, | |
53 | ]; | |
54 | $TransaksiPromo = $listProdukPromo; | |
55 | } | |
56 | - | else { |
56 | + | // problem jual 4, 1 dan 3 |
57 | - | $listProdukPromo = null; |
57 | + | // else { |
58 | // $listProdukPromo = null; | |
59 | // } | |
60 | } | |
61 | else { | |
62 | $TransaksiPromo = null; | |
63 | } | |
64 | ||
65 | // reduce temp jmlBeli | |
66 | $jmlBeliTemp = $jmlBeliTemp - $jmlBeliTemp; | |
67 | } | |
68 | else { | |
69 | $res = $jmlBeliTemp - $v->qty_akhir; | |
70 | ||
71 | // update stok qty akhir detail | |
72 | $produkMitraSingle->qty_akhir = 0; | |
73 | $produkMitraSingle->save(); | |
74 | ||
75 | if ($jmlBeliTemp > $v->qty_akhir){ | |
76 | $jmlBeliTemp = $v->qty_akhir; | |
77 | } | |
78 | ||
79 | // difference price promo and main price to wallet | |
80 | if ($produk->harga_promo > 0) { | |
81 | if ($produk->harga_promo < $v->harga_awal) { | |
82 | // find produk with promo | |
83 | $ProdukPromo = ProdukMitra::find($v->produk_mitra_id); | |
84 | $restPromo = ($v->harga_awal - $produk->harga_promo) * $jmlBeliTemp; | |
85 | $grandRestPromo += (int) $restPromo; | |
86 | ||
87 | // for email notification promo price | |
88 | $listProdukPromo[] = [ | |
89 | 'name' => $ProdukPromo->name, | |
90 | 'jumlah' => (int) $jmlBeliTemp, | |
91 | 'harga_jual' => (int) $v->harga_awal, | |
92 | 'harga_promo' => (int) $ProdukPromo->harga_promo, | |
93 | 'subNominal' => (int) $restPromo, | |
94 | ]; | |
95 | $TransaksiPromo = $listProdukPromo; | |
96 | - | else { |
96 | + | |
97 | - | $TransaksiPromo = null; |
97 | + | // else { |
98 | // $TransaksiPromo = null; | |
99 | // } | |
100 | } | |
101 | else { | |
102 | // $TransaksiPromo = $listProdukPromo ? $listProdukPromo : null; | |
103 | } | |
104 | ||
105 | // reduce temp jmlBeli | |
106 | $jmlBeliTemp = $res; | |
107 | } | |
108 | } | |
109 | } | |
110 | else { | |
111 | // $TransaksiPromo = $listProdukPromo ? $listProdukPromo : null; | |
112 | } | |
113 | } | |
114 | $TransaksiPromo = $listProdukPromo ? $listProdukPromo : null; | |
115 | } | |
116 | ||
117 | $dataProduk = [ | |
118 | 'name' => $produk->name, | |
119 | 'jumlah' => $jumlahProduk, | |
120 | 'harga' => $produk->harga_promo > 0 ? $produk->harga_promo : $produk->harga_jual, | |
121 | 'berat_produk' => $produk->berat_kemasan, | |
122 | 'satuan' => $produk->detail_satuan->name, | |
123 | ]; | |
124 | $produkDibeli[] = $dataProduk; | |
125 | ||
126 | // insert to detail transaction | |
127 | $detail_produk = [ | |
128 | 'transaksi_id' => $Transaksi->_id, | |
129 | 'produk_id' => $idProduk, | |
130 | ]; | |
131 | DetailTransaksi::create(array_merge($detail_produk, $dataProduk)); | |
132 | } | |
133 | ||
134 | } | |
135 | ||
136 | if ($metode_pembayaran == 'tunai') { | |
137 | if ($TransaksiPromo != null) { | |
138 | // difference promo price with main price | |
139 | $userMitra = User::find($Transaksi->mitra_id); | |
140 | if ($userMitra) { | |
141 | $userMitra->uang = (int) $userMitra->uang + $grandRestPromo; | |
142 | $userMitra->save(); | |
143 | ||
144 | // notif email difference price promo and main price to wallet | |
145 | Mail::send(new \App\Mail\SendNotifications(User::find($Transaksi->mitra_id), 'restPricePromo', $TransaksiPromo)); | |
146 | ||
147 | // input to finance transcation(transaksi_keuangan) | |
148 | $deviationPrice = TransaksiKeuangan::create([ | |
149 | 'user_id' => $Transaksi->mitra_id, | |
150 | 'transaksi_id' => $Transaksi->id, | |
151 | 'tanggal_transaksi' => date('Y-m-d'), | |
152 | 'tipe' => "OUT", | |
153 | 'nominal' => $grandRestPromo, | |
154 | 'saldo_akhir' => $userMitra->uang, | |
155 | 'tag' => "SELISIH PROMO", | |
156 | 'keterangan' => "Selisih Harga Promo dan Harga Pusat", | |
157 | 'status' => "DONE", | |
158 | ]); | |
159 | } | |
160 | } | |
161 | } | |
162 | ||
163 | $Transaksi->produk = $produkDibeli; | |
164 | ||
165 | //notifikasi pembayaran | |
166 | Mail::send(new \App\Mail\SendNotifications(Auth::User(), 'order_konsumen', $Transaksi)); | |
167 | ||
168 | // kirim notif ke cfo | |
169 | $notif = WebConfig::where('name', 'notifCMO')->first(); | |
170 | $notif->value = 1; | |
171 | $notif->save(); | |
172 | ||
173 | return response()->json([ | |
174 | 'success' => true, | |
175 | 'messages' => 'Add Transaksi Success !', | |
176 | 'trxcode' => "USR001", //user belanja via pos | |
177 | 'data' => $Transaksi, | |
178 | ], 201); | |
179 | } else { | |
180 | return response()->json([ | |
181 | 'success' => false, | |
182 | 'messages' => 'Can\'t Insert Transaksi!', | |
183 | 'data' => null, | |
184 | ], 200); | |
185 | } |