public function saveProduct($stock) { $now = new DateTime(); $this->baseValidate(); $this->active_at = $now->format('Y-m-d H:i:s'); $this->save(); $quantity = ProductQuantity::where('p_id', '=', $this->p_id)->first(); if (empty($quantity)) { $quantity = new ProductQuantity(); $quantity->p_id = $this->p_id; $quantity->b_id = $this->b_id; $quantity->u_id = $this->u_id; $quantity->q_total = $stock; $quantity->addQuantity(); } $freez = $quantity->q_cart + $quantity->q_sold; if ($freez > $stock) { throw new Exception("库存数量不能小于:" . $freez, 1); } $quantity->q_total = $stock; $quantity->save(); return $this->p_id; }
public function delFlea($id) { $token = Input::get('token', ''); $u_id = Input::get('u_id', 0); DB::beginTransaction(); try { $user = User::chkUserByToken($token, $u_id); $product = Product::find($id); if (empty($product) || $product->u_id != $u_id) { throw new Exception("没有找到请求的产品", 2001); } $quantity = ProductQuantity::where('p_id', '=', $id)->first(); if (empty($quantity)) { throw new Exception("获取库存信息失败", 2001); } $product->delete(); $quantity->delete(); $re = Tools::reTrue('删除成功'); DB::commit(); } catch (Exception $e) { $re = Tools::reFalse($e->getCode(), '删除失败:' . $e->getMessage()); DB::rollback(); } return Response::json($re); }
public function postProduct() { $token = Input::get('token', ''); $u_id = Input::get('u_id', 0); $b_id = Input::get('b_id', ''); $prodName = Input::get('prod_name', ''); $prodDesc = Input::get('content', ''); $prodBrief = Input::get('prod_brief', ''); $prodCost = Input::get('prod_cost', 0); $prodPriceOri = Input::get('prod_price', 0); $prodDiscount = Input::get('prod_discount', 100); $prodStock = Input::get('prod_stock', 0); $publish = Input::get('publish', 1); $product_cate = Input::get('cate'); if (!$product_cate) { $product_cate = 7; } $prodDesc = urldecode($prodDesc); $promoDesc = Input::get('promo', ''); $promoRange = Input::get('promo_range', 1); $imgToken = Input::get('img_token', ''); try { $user = User::chkUserByToken($token, $u_id); if ($prodDiscount > 0) { $prodPrice = $prodPriceOri * $prodDiscount / 100; } else { $prodPrice = $prodPriceOri; } $product = new Product(); $product->b_id = $b_id; $product->p_title = $prodName; $product->u_id = $u_id; $product->p_cost = $prodCost; $product->p_price_origin = $prodPriceOri; $product->p_price = $prodPrice; $product->p_discount = $prodDiscount; $product->p_desc = $prodDesc; $product->p_brief = $prodBrief; $product->p_status = $publish == 1 ? 1 : 2; $product->p_cate = $product_cate; $product->p_type = 1; $p_id = $product->addProduct(); $quantity = new ProductQuantity(); $quantity->p_id = $p_id; $quantity->b_id = $b_id; $quantity->u_id = $u_id; $quantity->q_total = $prodStock; $quantity->addQuantity(); if ($promoDesc) { $user->load('school'); $promo = new PromotionInfo(); $promo->p_id = $p_id; $promo->p_content = $prodName; $promo->c_id = $user->school->t_city; $promo->s_id = $user->school->t_id; $promo->b_id = $b_id; $promo->p_status = 1; $promo->p_range = $promoRange; $promo->addPromo(); } if ($imgToken) { $imgObj = new Img('product', $imgToken); $imgs = $imgObj->getSavedImg($p_id, '', true); $product->p_imgs = implode(',', $imgs); $product->save(); } $re = Tools::reTrue('添加产品成功'); } catch (Exception $e) { $re = Tools::reFalse($e->getCode(), '添加产品失败:' . $e->getMessage()); } return Response::json($re); }