Example #1
0
 public function postBooth()
 {
     // base infos
     $token = Input::get('token', '');
     $u_id = Input::get('u_id', '');
     // booth type
     $boothType = Input::get('type');
     // product category
     $productCate = Input::get('prod_cate');
     // booth title
     $boothTitle = Input::get('title');
     // booth position
     $boothLng = Input::get('lng');
     $boothLat = Input::get('lat');
     // product source
     $productSource = Input::get('prod_source');
     // customer group
     $cusomerGroup = Input::get('cust_group');
     // promo strategy
     $promoStratege = Input::get('promo_strategy');
     // with fund
     $withFund = Input::get('fund', 0);
     $booth_cate = Input::get('cate', 0);
     // profit ratio
     $profitRate = Input::get('profit');
     // loan amount
     $loan = Input::get('loan');
     // how to drow loan
     $loanSchema = Input::get('loan_schema', '');
     DB::beginTransaction();
     try {
         $user = User::chkUserByToken($token, $u_id);
         $user->load(['school']);
         $booth = Booth::where('u_id', '=', $u_id)->first();
         if (empty($booth)) {
             $booth = new Booth();
         } else {
             if ($booth->b_status == 1) {
                 throw new Exception("您已经申请过店铺了", 7001);
             }
         }
         $school = $user->school;
         $booth->s_id = $school->t_id;
         $booth->c_id = $school->t_city;
         $booth->pv_id = $school->t_province;
         $booth->u_id = $u_id;
         $booth->b_title = $boothTitle;
         $booth->b_desc = '';
         $booth->latitude = $boothLat;
         $booth->longitude = $boothLng;
         $booth->b_product_source = $productSource;
         $booth->b_product_category = $productCate;
         $booth->b_customer_group = $cusomerGroup;
         $booth->b_promo_strategy = $promoStratege;
         $booth->b_with_fund = $withFund;
         $booth->b_type = $boothType;
         $booth->b_cate = $booth_cate;
         $b_id = $booth->register();
         if ($withFund == 1) {
             $fund = Fund::where('b_id', '=', $booth->b_id)->first();
             if (empty($fund)) {
                 $fund = new Fund();
             } else {
                 if ($fund->t_status > 2) {
                     throw new Exception("基金已经发放", 1);
                 }
             }
             $fund->u_id = $u_id;
             $fund->t_apply_money = $loan;
             $fund->b_id = $b_id;
             $fund->t_profit_rate = $profitRate;
             $f_id = $fund->apply();
             $schema = 0;
             $allotedAmount = 0;
             $loanSchema = json_decode($loanSchema, true);
             if (!is_array($loanSchema)) {
                 throw new Exception("请传入正确的提款计划", 7001);
             }
             // clear all exists schema
             DB::table('repayments')->where('f_id', '=', $f_id)->delete();
             foreach ($loanSchema as $key => $percentage) {
                 $percentage = $percentage / 100;
                 $schema++;
                 if ($schema == count($loanSchema)) {
                     $amount = $loan - $allotedAmount;
                 } else {
                     $amount = $loan * $percentage;
                     $allotedAmount += $amount;
                 }
                 $repayment = new Repayment();
                 $repayment->f_id = $f_id;
                 $repayment->f_re_money = $amount;
                 $repayment->f_schema = $schema;
                 $repayment->f_percentage = $percentage * 100;
                 $repayment->apply();
             }
         } else {
             // if without fund, no need to censor
             $booth->b_status = 1;
             $booth->save();
         }
         $re = Tools::reTrue('申请成功');
         DB::commit();
     } catch (Exception $e) {
         $re = Tools::reFalse($e->getCode(), '申请失败:' . $e->getMessage());
         DB::rollback();
     }
     return Response::json($re);
 }
 public function retriveLoan($id)
 {
     DB::beginTransaction();
     $now = new DateTime();
     try {
         $current_loan = Repayment::find($id);
         if (empty($current_loan)) {
             throw new Exception("没有找到请求的放款", 6001);
         }
         $fund = Fund::find($current_loan->f_id);
         if (empty($fund)) {
             throw new Exception("没有找到相关的基金", 6001);
         }
         $fund->load('loans');
         $current_income = $fund->getCurrentPeriodIncome();
         $current_loan->f_income = $current_income;
         $profit = $current_income - $current_loan->f_re_money;
         if ($profit > 0) {
             $wallet = UsersWalletBalances::find($fund->u_id);
             if (empty($wallet)) {
                 throw new Exception("没有获取到用户钱包", 6001);
             }
             $wallet->putIn($profit);
             $current_loan->f_status = 4;
             $current_loan->f_money = $current_loan->f_re_money;
         } elseif ($profit == 0) {
             $current_loan->f_status = 3;
             $current_loan->f_money = $current_loan->f_re_money;
         } elseif ($profit < 0) {
             $current_loan->f_status = 2;
             $current_loan->f_money = $current_loan->f_income;
         }
         $current_loan->repaied_at = $now->format('Y-m-d H:i:s');
         $current_loan->save();
         $fund->load('loans');
         $all_repaied = true;
         $total_loan = 0;
         $total_repay = 0;
         foreach ($fund->loans as $key => $loan) {
             if ($loan->f_status < 3) {
                 $all_repaied = false;
             }
             $total_loan += $loan->f_re_money;
             $total_repay += $loan->f_money;
         }
         if ($all_repaied) {
             $total_profit = $total_repay - $total_loan;
             if ($profit > 0) {
                 $qnck_profit = $total_loan * (100 + $fund->t_profit_rate) / 500;
                 $wallet = UsersWalletBalances::find($fund->u_id);
                 if (empty($wallet)) {
                     throw new Exception("没有获取到用户钱包", 6001);
                 }
                 $wallet->getOut($qnck_profit);
             }
             $fund->t_is_close = 1;
             $fund->closed_at = $now->format('Y-m-d H:i:s');
         } else {
             $fund->t_is_close = 0;
         }
         $msg = new MessageDispatcher($fund->u_id);
         $msg->fireTextToUser('您基金的第' . $current_loan->f_schema . '次已结账, 金额:' . $current_income);
         $fund->save();
         $re = Tools::reTrue('回收放款成功');
         DB::commit();
     } catch (Exception $e) {
         $re = Tools::reFalse($e->getCode(), '回收放款失败:' . $e->getMessage());
         DB::rollback();
     }
     return Response::json($re);
 }