public function OpenTransaksi(Request $request) { \Debugbar::disable(); $data_order_detail = json_decode($request->get('data_order'), true); // Convert like data session $temp = []; foreach ($data_order_detail as $d) { $key = $d['id']; $temp[$key] = $d; } $data_order_detail = $temp; # Create Nota $setting = Setting::first(); // Get Last Order $tanggal = $request->get('tanggal'); $lastOrder = Order::where('tanggal', $tanggal)->get()->count(); $nota = $setting->init_kode . "-" . str_replace('-', '', date('dmY', strtotime($tanggal))) . ($lastOrder + 1); // Order $karyawan_id = $request->get('karyawan_id'); $order = $request->only(['tanggal']) + ['nota' => $nota, 'state' => 'On Going', 'karyawan_id' => $karyawan_id]; $order = \App\Order::create($order); if ($order) { // Order Place $places = explode(',', $request->get('places')); $places = \App\Place::whereIn('id', $places)->get(); $orderPlaces = []; foreach ($places as $place) { $placeType = $place->kategori_id; // For Redirect array_push($orderPlaces, ['order_id' => $order->id, 'place_id' => $place->id, 'harga' => $place->harga]); } \App\OrderPlace::insert($orderPlaces); // Order Detail & Order Detail Bahan $produks = Produk::with(['detail' => function ($query) { $query->join('bahans', 'produk_details.bahan_id', '=', 'bahans.id'); }])->whereIn('id', array_keys($data_order_detail))->get(); $orderDetailBahan = []; foreach ($produks as $produk) { $id = $produk->id; // Order Detail $orderDetail = ['order_id' => $order->id, 'produk_id' => $produk->id, 'hpp' => CountHpp($produk), 'harga_jual' => $data_order_detail[$id]['harga'], 'qty' => $data_order_detail[$id]['qty'], 'use_mark_up' => $produk->use_mark_up, 'mark_up' => $produk->mark_up, 'note' => ""]; //echo "<pre>", print_r($orderDetail), "</pre>"; $orderDetail = \App\OrderDetail::create($orderDetail); if ($produk->detail->count()) { // Order Detail Bahan foreach ($produk->detail as $pd) { array_push($orderDetailBahan, ['order_detail_id' => $orderDetail->id, 'bahan_id' => $pd->bahan_id, 'harga' => $pd->harga, 'qty' => $pd->qty, 'satuan' => $pd->satuan]); } } } \App\OrderDetailBahan::insert($orderDetailBahan); Artisan::call('bahan:count'); Artisan::call('produk:count'); return 1; } return 0; }
public function saveChangeOrder(Request $request, $id) { $validator = Validator::make($request->all(), ['places' => 'required'], ['places.required' => 'Tempat tidak boleh kosong.']); if ($validator->fails()) { return redirect()->back()->withErrors($validator)->withInput(); } // Order Place $inputPlaces = explode(',', $request->get('places')); $oldPlaces = array_column(OrderPlace::where('order_id', $id)->get()->toArray(), 'place_id'); # Compare For New $diff = array_diff($inputPlaces, $oldPlaces); if (count($diff)) { $places = Place::whereIn('id', $diff)->get(); $orderPlaces = []; foreach ($places as $place) { array_push($orderPlaces, ['order_id' => $id, 'place_id' => $place->id, 'harga' => $place->harga]); } OrderPlace::insert($orderPlaces); } # Compare For Delete $diff = array_diff($oldPlaces, $inputPlaces); if (count($diff)) { OrderPlace::where('order_id', $id)->whereIn('place_id', $diff)->delete(); } $data_order_detail = $request->session()->has('data_order') ? $request->session()->get('data_order') : []; $order = Order::with('place.place')->find($id); if (count($data_order_detail)) { // Order Detail $orderDetailOld = OrderDetail::where('order_id', $id)->whereIn('produk_id', array_keys($data_order_detail))->get(); # Update Order Detail foreach ($orderDetailOld as $odo) { $oldQty = $odo->qty; $updateQty = $oldQty + $data_order_detail[$odo->produk_id]['qty']; $updatePrice = $data_order_detail[$odo->produk_id]['harga']; OrderDetail::find($odo->id)->update(['qty' => $updateQty, 'harga_jual' => $updatePrice]); unset($data_order_detail[$odo->produk_id]); } if (count($data_order_detail)) { # New Order Detail $produks = Produk::with(['detail' => function ($query) { $query->join('bahans', 'produk_details.bahan_id', '=', 'bahans.id'); }])->whereIn('id', array_keys($data_order_detail))->get(); $orderDetailBahan = []; foreach ($produks as $produk) { $pId = $produk->id; // Order Detail $orderDetail = ['order_id' => $id, 'produk_id' => $produk->id, 'hpp' => CountHpp($produk), 'harga_jual' => $data_order_detail[$pId]['harga'], 'qty' => $data_order_detail[$pId]['qty'], 'use_mark_up' => $produk->use_mark_up, 'mark_up' => $produk->mark_up, 'note' => $data_order_detail[$pId]['note']]; //echo "<pre>", print_r($orderDetail), "</pre>"; $orderDetail = OrderDetail::create($orderDetail); if ($produk->detail->count()) { // Order Detail Bahan foreach ($produk->detail as $pd) { array_push($orderDetailBahan, ['order_detail_id' => $orderDetail->id, 'bahan_id' => $pd->bahan_id, 'harga' => $pd->harga, 'qty' => $pd->qty, 'satuan' => $pd->satuan]); } } } OrderDetailBahan::insert($orderDetailBahan); } Artisan::call('bahan:count'); Artisan::call('produk:count'); $request->session()->forget('data_order'); } $tglRedirect = $order->tanggal->format('Y-m-d'); $typeRedirect = $order->place[0]->place->kategori_id; return redirect('/order?tgl=' . $tglRedirect . '&type=' . $typeRedirect)->with('succcess', 'Sukses ubah data order.'); }
/** * Run the database seeds. * * @return void */ public function run() { with(new \App\Tag(['tag' => 'adventure']))->save(); // DB::enableQueryLog(); // for ($i = 1; $i <= 200; $i++) { $destination = \App\Destination::whereNotNull('parent_id')->orderByRaw("RAND()")->first(); $destination_id = $destination->id; // TOUR $duration = rand(3, 12); $tour = new Tour(); $tour->fill(['travel_agent_id' => rand(1, 6), 'name' => $duration . 'D/' . ($duration - 1) . 'N ' . $destination->name, 'ittinary' => 'Lorem ipsum Incididunt ut velit voluptate ullamco occaecat ut laboris ad nulla mollit eu Ut sed do dolor sit aliqua amet dolore dolore laboris occaecat ad nulla mollit minim officia voluptate ex et proident dolore cillum dolore do nostrud et exercitation amet commodo magna ex amet sit minim cillum Duis proident amet consequat amet proident fugiat culpa velit qui eu consequat irure nulla consequat sit laborum est deserunt dolore enim id aliquip nisi pariatur pariatur irure tempor in fugiat consectetur ut deserunt Duis tempor dolor dolore laboris ullamco ullamco amet Duis nostrud amet consequat consequat ea non nisi elit cupidatat esse dolor esse exercitation dolor incididunt voluptate dolore nostrud dolor reprehenderit in cupidatat tempor consectetur cupidatat sed dolor amet deserunt cillum occaecat nulla Duis enim consequat enim tempor adipisicing nisi cillum do sed cupidatat reprehenderit Ut.', 'summary' => 'Lorem ipsum Pariatur amet irure labore aute Duis ea irure proident adipisicing adipisicing velit elit in non incididunt ea fugiat exercitation ex enim dolore.', 'duration_day' => $duration, 'duration_night' => $duration - 1, 'published_at' => \Carbon\Carbon::now(), 'slug' => str_slug($duration . 'D/' . ($duration - 1) . 'N ' . $destination->name . ' ' . $i), 'tag_ids' => 1]); if (!$tour->save()) { dd($tour->getErrors()); } $is_fit = rand(0, 100) < 20; // TOUR DESTINATION $tour->destinations()->sync([$destination_id]); // PLACES $places = \App\Place::whereIn('destination_id', $tour->destinations->lists('id'))->limit($tour->duration_day)->orderByRaw('rand()')->get(); // $queries = DB::getQueryLog(); // dd(end($queries)); if ($places->count()) { $tour->places()->sync($places->lists('id')->toArray()); } // SCHEDULE if (!$is_fit) { $departure = \Carbon\Carbon::now()->startOfMonth()->addDay(rand(0, 240)); $original_price = $duration * rand(1, 10) * 300000; $tour->schedules()->saveMany([new \App\TourSchedule(['departure' => $departure, 'departure_until' => \Carbon\Carbon::parse($departure)->addDay(rand(30, 150)), 'currency' => 'IDR', 'original_price' => $original_price, 'discounted_price' => $original_price * (rand(0, 100) <= 20 ? rand(80, 100) / 100 : 1), 'views' => 0])]); } else { for ($j = 1; $j <= rand(3, 5); $j++) { $departure = \Carbon\Carbon::now()->startOfMonth()->addDay(rand(0, 240)); $original_price = $duration * rand(1, 10) * 300000; $tour->schedules()->saveMany([new \App\TourSchedule(['departure' => $departure, 'departure_until' => null, 'currency' => 'IDR', 'original_price' => $original_price, 'discounted_price' => $original_price * (rand(0, 100) <= 20 ? rand(80, 100) / 100 : 1), 'views' => 0])]); } } // OPTIONS $tour->options()->sync([1 => ['description' => rand(1, 5)], 3 => ['description' => 'Singapore Airlines'], 5 => ['description' => ''], 6 => ['description' => '']]); } }