/** * @param array $data * * @throws \Exception */ public function create(array $data) { \DB::beginTransaction(); try { $data['status'] = 0; if (isset($data['coupon_code'])) { $coupon = $this->couponRepository->findByField('code', $data['coupon_code'])->first(); $data['coupon_id'] = $coupon->id; $coupon->used = 1; $coupon->save(); unset($data['coupon_code']); } $items = $data['items']; unset($data['items']); $order = $this->orderRepository->create($data); $total = 0; foreach ($items as $item) { $item['price'] = $this->productRepository->find($item['product_id'])->price; $order->items()->create($item); $total += $item['price'] * $item['quantity']; } $order->total = $total; if (isset($coupon)) { $order->total = $total - $coupon->value; } $order->save(); \DB::commit(); return $order; } catch (\Exception $e) { \DB::rollback(); throw $e; } }
/** * @param $id * * @return \Illuminate\Http\RedirectResponse */ public function destroy($id) { $this->couponRepository->delete($id); return redirect()->route('admin.coupons.index'); }