public function approveApplication(Request $request)
 {
     $shop_card_application_id = $request->input('require_id');
     $application = ShopCardApplication::find($shop_card_application_id);
     $customer = Customer::find($application->customer_id);
     $card_type = $application->cardType;
     try {
         \DB::transaction(function () use($application, $customer, $card_type) {
             $customer_rows = \DB::table('customers')->where('id', $customer->id);
             $customer_rows->lockForUpdate();
             $customer_row = $customer_rows->first();
             if ($customer_row->beans_total < $card_type->beans_value * $application->amount) {
                 throw new NotEnoughBeansException();
             }
             $cards = \DB::table('shop_cards')->where('card_type_id', '=', $card_type->id)->whereNull('customer_id')->limit($application->amount);
             $cards->lockForUpdate();
             if ($cards->count() < $application->amount) {
                 throw new CardNotEnoughException();
             }
             $customer->minusBeansByHand($application->amount * $card_type->beans_value);
             $cards->update(['customer_id' => $customer->id, 'bought_at' => Carbon::now()]);
             $application->update(['authorized' => true]);
             return true;
         });
     } catch (CardNotEnoughException $e) {
         return '相应卡片不足,无法继续。';
     } catch (NotEnoughBeansException $e) {
         return '用户迈豆不足!';
     }
     return "操作成功!";
 }
Example #2
0
 public function index()
 {
     $result = [];
     $applications = ShopCardApplication::where('authorized', 0)->with('customer')->get();
     foreach ($applications as $application) {
         $result[] = ['require_id' => $application->id, 'id' => $application->customer_id, 'name' => $application->customer->nickname, 'phone' => $application->customer->phone, 'num' => $application->amount, 'beans_total' => $application->customer->beans_total];
     }
     return view('backend.order.gift-card')->with(['applications' => json_encode($result)]);
 }
Example #3
0
 public function askForCard(Request $request)
 {
     $customer = \Helper::getCustomerOrFail();
     $amount = $request->input('amount');
     $card_type_id = $request->input('card_type_id', 1);
     $card_type = CardType::find($card_type_id);
     if (ShopCardApplication::where('customer_id', '=', $customer->id)->where('authorized', '=', 0)->first()) {
         return '您已有申请正在处理中,不能重复申请。';
     }
     if ($customer->beans_total < $card_type->beans_value * $amount) {
         return '迈豆不足,不能申请兑换。';
     }
     ShopCardApplication::create(['customer_id' => $customer->id, 'amount' => $amount, 'authorized' => 0, 'card_type_id' => $card_type_id]);
     return '申请成功,待管理员审核!';
 }
Example #4
0
 /**
  * @return \App\Models\ShopCardApplication;
  * 返回shopcardapplication的卡券总数
  */
 public function getShopCardApplication()
 {
     try {
         $user = self::getSessionCachedUser();
         $customer = Customer::where('openid', $user['openid'])->firstOrFail();
         $shopCardApplication = ShopCardApplication::where('customer_id', $customer['id'])->get();
         if (empty($shopCardApplication)) {
             return 0;
         } else {
             return $shopCardApplication;
         }
     } catch (\Exception $e) {
         abort('404');
     }
 }