예제 #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);
 }