Ejemplo n.º 1
0
 public function checkoutCrowdFunding()
 {
     if ($this->c_type != 2) {
         return true;
     }
     // push msg to seller
     $booth = Booth::find($this->b_id);
     $product = CrowdFundingProduct::find($this->p_id);
     $product->confirmProduct($this->c_quantity);
     $funding = CrowdFunding::find($product->cf_id);
     $funding->c_amount += $this->c_amount;
     $funding->save();
     $msg = new MessageDispatcher($booth->u_id);
     $msg->fireCateToUser('您的众筹' . $funding->c_title . '已有人认购', 1, $funding->cf_id);
     return true;
 }
Ejemplo n.º 2
0
 public function fakeCrowdFundingPurches($id)
 {
     set_time_limit(0);
     $this->login();
     $bottom = Input::get('bottom', '');
     $top = Input::get('top', '');
     $p_id = Input::get('p_id', '');
     try {
         if (!$p_id || !$top || !$bottom) {
             throw new Exception("需要关键数据", 1);
         }
         $funding = CrowdFunding::find($id);
         $funding->load(['eventItem']);
         $product = CrowdFundingProduct::find($p_id);
         $quantity = 1;
         $users = User::where('u_mobile', '>=', $bottom)->where('u_mobile', '<=', $top)->get();
         foreach ($users as $key => $user) {
             $u_id = $user->u_id;
             // sku need to be calulated before cart generated
             $product->loadProduct($quantity);
             // add cart
             $cart = new Cart();
             $cart->p_id = $p_id;
             $cart->p_name = $product->p_title;
             $cart->u_id = $u_id;
             $cart->b_id = $product->b_id;
             $cart->created_at = Tools::getNow();
             $cart->c_quantity = $quantity;
             $cart->c_price = $product->p_price;
             $cart->c_amount = $product->p_price * $quantity;
             $cart->c_discount = 100;
             $cart->c_price_origin = $product->p_price;
             $cart->c_amount_origin = $product->p_price * $quantity;
             $cart->c_status = 2;
             $cart->c_type = 2;
             $re = $cart->save();
             if (!$re) {
                 throw new Exception("提交库存失败", 7006);
             }
             $shipping_address = 'Fake Purches';
             $shipping_name = $user->u_name;
             $shipping_phone = $user->u_mobile;
             $date_obj = new DateTime($funding->eventItem->e_start_at);
             $delivery_time_obj = $date_obj->modify('+' . ($funding->c_time + $funding->c_yield_time) . 'days');
             // add order
             $order_group_no = Order::generateOrderGroupNo($u_id);
             $rnd_str = rand(10, 99);
             $order_no = $order_group_no . $cart->b_id . $rnd_str;
             $order = new Order();
             $order->u_id = $u_id;
             $order->b_id = $cart->b_id;
             $order->o_amount_origin = $cart->c_amount_origin;
             $order->o_amount = $cart->c_amount;
             $order->o_shipping_fee = $funding->c_shipping_fee;
             $order->o_shipping_name = $shipping_name;
             $order->o_shipping_phone = $shipping_phone;
             $order->o_shipping_address = $shipping_address;
             $order->o_delivery_time = $delivery_time_obj->format('Y-m-d H:i:s');
             $order->o_shipping = $funding->c_shipping;
             $order->o_comment = 'Fake Order';
             $order->o_number = $order_no;
             $order->o_group_number = $order_group_no;
             $o_id = $order->addOrder();
             Cart::bindOrder([$order->o_id => [$cart->c_id]]);
             $cart->checkout();
             $order->o_status = 2;
             $order->o_shipping_status = 10;
             $order->paied_at = Tools::getNow();
             $order->save();
         }
     } catch (Exception $e) {
         echo $e->getMessage();
     }
     echo "done";
 }
 public function postOrder($id)
 {
     $token = Input::get('token', '');
     $u_id = Input::get('u_id', 0);
     $p_id = Input::get('product', 0);
     $quantity = Input::get('quantity', 0);
     $shipping_name = Input::get('shipping_name', '');
     $shipping_phone = Input::get('shipping_phone', '');
     $shipping_address = Input::get('shipping_address', '');
     $comment = Input::get('comment', '');
     DB::beginTransaction();
     try {
         $funding = CrowdFunding::find($id);
         $funding->load(['eventItem']);
         $date_end = new DateTime($funding->eventItem->e_end_at);
         $date_start = new DateTime($funding->eventItem->e_start_at);
         $now = new DateTime();
         if ($now > $date_end) {
             throw new Exception("抱歉, 众筹已经结束", 2001);
         }
         if ($now < $date_start) {
             throw new Exception("抱歉, 众筹还未开始, 请耐心等待", 2001);
         }
         if ($funding->c_status != 4) {
             throw new Exception("抱歉, 无效的众筹状态", 2001);
         }
         if ($funding->u_id == $u_id) {
             throw new Exception("您不能认筹自己发起的众筹", 2001);
         }
         $validator = Validator::make(['收货人电话' => (string) $shipping_phone, '收货人姓名' => $shipping_name, '收获地址' => $shipping_address, '产品' => $p_id, '数量' => $quantity], ['收货人电话' => 'required|numeric|digits:11', '收货人姓名' => 'required', '收获地址' => 'required', '产品' => 'required|numeric', '数量' => 'required|numeric']);
         if ($validator->fails()) {
             $msg = $validator->messages();
             throw new Exception($msg->first(), 7001);
         }
         $user = User::chkUserByToken($token, $u_id);
         if (!$user->u_mobile && ($id = 30)) {
             throw new Exception("此众筹需要绑定联系电话,请到[我的-编辑资料]里绑定后进行支持", 2001);
         }
         if ($funding->c_local_only) {
             $funding_owner = User::find($funding->u_id);
             if ($funding_owner->u_school_id != $user->u_school_id) {
                 throw new Exception("该众筹仅限于同校参与", 2001);
             }
         }
         $product = CrowdFundingProduct::find($p_id);
         if ($product->p_price == 0) {
             if ($quantity != 1) {
                 throw new Exception("此类众筹只能认筹一份", 1);
             }
             // check if user has bought
             $chk = Cart::where('u_id', '=', $u_id)->where('c_type', '=', 2)->where('p_id', '=', $p_id)->where('c_status', '>', 0)->first();
             if (!empty($chk)) {
                 throw new Exception("此类众筹每人限认筹一次", 7001);
             }
         }
         // sku need to be calulated before cart generated
         $product->loadProduct($quantity);
         // add cart
         $cart = new Cart();
         $cart->p_id = $p_id;
         $cart->p_name = $product->p_title;
         $cart->u_id = $u_id;
         $cart->b_id = $product->b_id;
         $cart->created_at = Tools::getNow();
         $cart->c_quantity = $quantity;
         $cart->c_price = $product->p_price;
         $cart->c_amount = $product->p_price * $quantity;
         $cart->c_discount = 100;
         $cart->c_price_origin = $product->p_price;
         $cart->c_amount_origin = $product->p_price * $quantity;
         $cart->c_status = 2;
         $cart->c_type = 2;
         $re = $cart->save();
         if (!$re) {
             throw new Exception("提交库存失败", 7006);
         }
         if (!$funding->c_shipping) {
             $shipping_address = '';
         }
         $shipping_name = $shipping_name ? $shipping_name : $user->u_name;
         $shipping_phone = $shipping_phone ? $shipping_phone : $user->u_mobile;
         $date_obj = new DateTime($funding->eventItem->e_start_at);
         $delivery_time_obj = $date_obj->modify('+' . ($funding->c_time + $funding->c_yield_time) . 'days');
         // add order
         $order_group_no = Order::generateOrderGroupNo($u_id);
         $rnd_str = rand(10, 99);
         $order_no = $order_group_no . $cart->b_id . $rnd_str;
         $order = new Order();
         $order->u_id = $u_id;
         $order->b_id = $cart->b_id;
         $order->o_amount_origin = $cart->c_amount_origin;
         $order->o_amount = $cart->c_amount;
         $order->o_shipping_fee = $funding->c_shipping_fee;
         $order->o_shipping_name = $shipping_name;
         $order->o_shipping_phone = $shipping_phone;
         $order->o_shipping_address = $shipping_address;
         $order->o_delivery_time = $delivery_time_obj->format('Y-m-d H:i:s');
         $order->o_shipping = $funding->c_shipping;
         $order->o_comment = $comment;
         $order->o_number = $order_no;
         $order->o_group_number = $order_group_no;
         $o_id = $order->addOrder();
         Cart::bindOrder([$order->o_id => [$cart->c_id]]);
         // change order to finish if price = 0
         if ($order->o_amount == 0) {
             $cart->checkout();
             $order->o_status = Order::$STATUS_FINISHED;
             $order->o_shipping_status = Order::$SHIPPING_STATUS_FINISHED;
             $order->paied_at = Tools::getNow();
             $order->save();
         }
         $data = ['order_no' => $order_group_no];
         $re = Tools::reTrue('提交订单成功', $data);
         DB::commit();
     } catch (Exception $e) {
         $re = Tools::reFalse($e->getCode(), '提交订单失败:' . $e->getMessage());
         DB::rollback();
     }
     return Response::json($re);
 }
 public function putCrowdFunding($id)
 {
     $token = Input::get('token', '');
     $u_id = Input::get('u_id', 0);
     $title = Input::get('title', '');
     $cate = Input::get('cate', 1);
     $amount = Input::get('amount', 0);
     $time = Input::get('time', 0);
     $yield_time = Input::get('yield_time', 0);
     $shipping = Input::get('shipping', 0);
     $shipping_fee = Input::get('shipping_fee', 0);
     $brief = Input::get('brief', '');
     $yield_desc = Input::get('yield_desc', '');
     $content = Input::get('content', '');
     $open_file = Input::get('open_file', 0);
     $local_only = Input::get('local_only', 0);
     $is_schedule = Input::get('is_schedule', 0);
     $active_at = Input::get('active_at');
     if (empty($active_at)) {
         $active_at = Tools::getNow();
     }
     $mobile = Input::get('mobile', '');
     $price = Input::get('price', 0);
     $quantity = Input::get('quantity', 0);
     $is_limit = Input::get('is_limit', 0);
     $img_token = Input::get('img_token', '');
     $img_token_2 = Input::get('img_token_2', '');
     $apartment_no = Input::get('apartment_no', '');
     $content = urldecode($content);
     $modified_img = Input::get('modified_img', '');
     $modified_img_index = Input::get('modified_img_index', '');
     if ($modified_img) {
         $modified_img = explode(',', $modified_img);
     }
     try {
         $user = User::chkUserByToken($token, $u_id);
         if ($cate == 8 && $user->u_type != 2) {
             throw new Exception("错误的发布类型", 2001);
         }
         $crowd_funding = CrowdFunding::find($id);
         if (empty($crowd_funding) || $crowd_funding->u_id != $u_id) {
             throw new Exception("无法获取到请求的众筹", 2001);
         }
         $funding_product = CrowdFundingProduct::where('cf_id', '=', $crowd_funding->cf_id)->first();
         if (empty($funding_product)) {
             throw new Exception("库存信息丢失", 2001);
         }
         if (Cart::getCartTypeCount(Cart::$TYPE_CROWD_FUNDING, $funding_product->p_id)) {
             throw new Exception("已有人购买", 2001);
         }
         if ($apartment_no) {
             $tmp_base = TmpUserProfileBase::find($user->u_id);
             $tmp_base->u_apartment_no = $apartment_no;
             $tmp_base->save();
         }
         $event = EventItem::find($crowd_funding->e_id);
         // put funding
         $event->e_title = $title;
         $event->e_brief = $brief;
         $event->e_start_at = $active_at;
         $date_obj = new DateTime($active_at);
         $date_obj->modify('+' . $time . ' days');
         $event->e_end_at = $date_obj->format('Y-m-d H:i:s');
         $crowd_funding->c_local_only = $local_only;
         $crowd_funding->c_yield_desc = $yield_desc;
         $crowd_funding->c_content = $content;
         $crowd_funding->c_yield_time = $yield_time;
         $crowd_funding->u_mobile = $mobile;
         $crowd_funding->c_is_schedule = $is_schedule;
         $crowd_funding->c_time = $time;
         $crowd_funding->c_shipping = $shipping;
         $crowd_funding->c_shipping_fee = $shipping_fee;
         $crowd_funding->c_target_amount = $amount;
         $crowd_funding->c_cate = $cate;
         $crowd_funding->c_open_file = $open_file;
         if ($amount <= 2000) {
             $crowd_funding->c_status = 4;
         } else {
             $crowd_funding->c_status = 1;
         }
         // if the user is an official user, set funding type to offical
         if ($user->u_type == 2) {
             $crowd_funding->c_cate = 8;
         }
         if (is_numeric($modified_img_index)) {
             $imgObj = new Img('crowd_funding', $img_token_2);
             $new_paths = [];
             if (!empty($modified_img)) {
                 foreach ($modified_img as $old_path) {
                     $new_path = $imgObj->reindexImg($id, $modified_img_index, $old_path);
                     $new_paths[] = $new_path;
                     $modified_img_index++;
                 }
                 $new_paths = Img::attachHost($new_paths);
                 $crowd_funding->c_imgs = implode(',', $new_paths);
             }
         }
         if ($img_token_2) {
             $imgObj = new Img('crowd_funding', $img_token_2);
             $imgs = $imgObj->getSavedImg($crowd_funding->cf_id, $crowd_funding->c_imgs, true);
             if (!empty($modified_img)) {
                 foreach ($modified_img as $del) {
                     if (array_key_exists($del, $imgs)) {
                         unset($imgs[$del]);
                     }
                 }
             }
             $crowd_funding->c_imgs = implode(',', $imgs);
         }
         if ($img_token) {
             $img_obj = new Img('event', $img_token);
             $cover_img = $img_obj->replace($event->e_id, 'cover_img');
             $event->cover_img = $cover_img;
         }
         $crowd_funding->save();
         $event->save();
         // put funding product
         $funding_product = CrowdFundingProduct::where('cf_id', '=', $crowd_funding->cf_id)->first();
         if (empty($funding_product)) {
             throw new Exception("库存信息丢失", 2001);
         }
         $funding_product->p_price = $price;
         $funding_product->p_target_quantity = $quantity;
         if ($is_limit) {
             $funding_product->p_max_quantity = $quantity;
         } else {
             $funding_product->p_max_quantity = 0;
         }
         $funding_product->save();
         $re = Tools::reTrue('编辑众筹成功');
     } catch (Exception $e) {
         $re = Tools::reFalse($e->getCode(), '编辑众筹失败:' . $e->getMessage());
     }
     return Response::json($re);
 }
Ejemplo n.º 5
0
 public function cloneCrowdFunding()
 {
     $this->load(['eventItem', 'product']);
     $event = new EventItem();
     $event->o_id = $this->eventItem->o_id;
     $event->e_title = $this->eventItem->e_title;
     $event->cover_img = $this->eventItem->cover_img;
     $event->e_brief = $this->eventItem->e_brief;
     $event->url = $this->eventItem->url;
     $event->e_range = $this->eventItem->e_range;
     $event->e_start_at = $this->eventItem->e_start_at;
     $event->e_end_at = $this->eventItem->e_end_at;
     $event->created_at = $this->eventItem->created_at;
     $event->e_status = $this->eventItem->e_status;
     $event->save();
     $funding = new CrowdFunding();
     $funding->u_id = $this->u_id;
     $funding->b_id = $this->b_id;
     $funding->c_status = $this->c_status;
     $funding->c_yield_desc = $this->c_yield_desc;
     $funding->c_content = $this->c_content;
     $funding->c_imgs = $this->c_imgs;
     $funding->c_yield_time = $this->c_yield_time;
     $funding->c_time = $this->c_time;
     $funding->c_shipping = $this->c_shipping;
     $funding->c_shipping_fee = $this->c_shipping_fee;
     $funding->c_target_amount = $this->c_target_amount;
     $funding->c_cate = $this->c_cate;
     $funding->created_at = $this->created_at;
     $funding->c_open_file = $this->c_open_file;
     $funding->c_praise_count = $this->c_praise_count;
     $funding->c_remark = $this->c_remark;
     $funding->c_amount = $this->c_amount;
     $funding->u_mobile = $this->u_mobile;
     $funding->c_local_only = $this->c_local_only;
     $funding->e_id = $event->e_id;
     $funding->save();
     $product = new CrowdFundingProduct();
     $product->cf_id = $funding->cf_id;
     $product->u_id = $this->product->u_id;
     $product->b_id = $this->product->b_id;
     $product->p_imgs = $this->product->p_imgs;
     $product->p_title = $this->product->p_title;
     $product->p_desc = $this->product->p_desc;
     $product->p_price = $this->product->p_price;
     $product->p_status = $this->product->p_status;
     $product->p_max_quantity = $this->product->p_max_quantity;
     $product->p_target_quantity = $this->product->p_target_quantity;
     $product->p_sort = $this->product->p_sort;
     $product->created_at = $this->product->created_at;
     $product->p_sold_quantity = $this->product->p_sold_quantity;
     $product->p_cart_quantity = $this->product->p_cart_quantity;
     $product->save();
     return $event;
 }
 public function postFunding()
 {
     $u_id = Tools::getOfficialUserId();
     $b_id = Tools::getOfficialBoothId();
     $title = Input::get('title', '');
     $amount = Input::get('amount', 0);
     $time = Input::get('time', 0);
     $yield_time = Input::get('yield_time', 0);
     $shipping = Input::get('shipping', 0);
     $shipping_fee = Input::get('shipping_fee', 0);
     $brief = Input::get('brief', '');
     $yield_desc = Input::get('yield_desc', '');
     $content = Input::get('content', '');
     $open_file = Input::get('open_file', 0);
     $active_at = Input::get('active_at');
     $local_only = Input::get('local_only', 0);
     $range = Input::get('range', 1);
     $cities = Input::get('cities', 0);
     $schools = Input::get('schools', 0);
     if (empty($active_at)) {
         $active_at = Tools::getNow();
     }
     $mobile = Input::get('mobile', '');
     $price = Input::get('price', 0);
     $quantity = Input::get('quantity', 0);
     $is_limit = Input::get('is_limit', 0);
     $img_token = Input::get('img_token', '');
     $content = urldecode($content);
     DB::beginTransaction();
     try {
         $user = User::find($u_id);
         $user->load('profileBase', 'school');
         $booth = Booth::find($b_id);
         // add event
         $event = new EventItem();
         $event->e_title = $title;
         $event->e_brief = $brief;
         $event->e_range = $range;
         $event->e_start_at = $active_at;
         $date_obj = new DateTime($active_at);
         $date_obj->modify('+' . $time . ' days');
         $event->e_end_at = $date_obj->format('Y-m-d H:i:s');
         $event->addEvent();
         $e_id = $event->e_id;
         // add funding
         $crowd_funding = new CrowdFunding();
         $crowd_funding->u_id = $u_id;
         $crowd_funding->b_id = $booth->b_id;
         $crowd_funding->c_yield_desc = $yield_desc;
         $crowd_funding->c_content = $content;
         $crowd_funding->c_yield_time = $yield_time;
         $crowd_funding->u_mobile = $mobile;
         $crowd_funding->c_time = $time;
         $crowd_funding->c_shipping = $shipping;
         $crowd_funding->c_shipping_fee = $shipping_fee;
         $crowd_funding->c_target_amount = $amount;
         $crowd_funding->c_amount = 0.0;
         $crowd_funding->c_local_only = $local_only;
         $crowd_funding->c_praise_count = 0;
         $crowd_funding->c_remark = '';
         $crowd_funding->c_open_file = $open_file;
         $crowd_funding->c_status = 4;
         $crowd_funding->c_cate = 8;
         $crowd_funding->e_id = $e_id;
         $crowd_funding->addCrowdFunding();
         if ($img_token) {
             $imgObj = new Img('crowd_funding', $img_token);
             $crowd_funding->c_imgs = $imgObj->getSavedImg($crowd_funding->cf_id);
             $crowd_funding->save();
             $imgObj = new Img('event', $img_token);
             $event->cover_img = $imgObj->getSavedImg($event->e_id);
             $event->save();
         }
         // add funding product
         $funding_product = new CrowdFundingProduct();
         $funding_product->cf_id = $crowd_funding->cf_id;
         $funding_product->u_id = $u_id;
         $funding_product->b_id = $booth->b_id;
         $funding_product->p_title = $title;
         $funding_product->p_desc = '';
         $funding_product->p_price = $price;
         $funding_product->p_target_quantity = $quantity;
         $funding_product->p_sort = 0;
         if ($is_limit) {
             $funding_product->p_max_quantity = $quantity;
         } else {
             $funding_product->p_max_quantity = 0;
         }
         $funding_product->addProduct();
         if ($range == 1) {
             $event_range = new EventRange(['c_id' => 0, 'p_id' => 0, 's_id' => 0]);
             $event->ranges()->save($event_range);
         }
         if ($cities && $range == 2) {
             $city_sets = explode(',', $cities);
             foreach ($city_sets as $key => $set) {
                 $array = explode('|', $set);
                 $event_range = new EventRange(['c_id' => $array[0], 'p_id' => $array[1]]);
                 if ($key) {
                     $new_event = $crowd_funding->cloneCrowdFunding();
                 } else {
                     $new_event = $event;
                 }
                 $new_event->ranges()->save($event_range);
             }
         }
         if ($schools && $range == 3) {
             $schools = explode(',', $schools);
             foreach ($schools as $key => $school) {
                 $event_range = new EventRange(['s_id' => $school]);
                 if ($key) {
                     $new_event = $crowd_funding->cloneCrowdFunding();
                 } else {
                     $new_event = $event;
                 }
                 $new_event->ranges()->save($event_range);
             }
         }
         $re = Tools::reTrue('添加众筹成功');
         DB::commit();
     } catch (Exception $e) {
         $re = Tools::reFalse($e->getCode(), '添加众筹失败:' . $e->getMessage());
         DB::rollback();
     }
     return Response::json($re);
 }