get('name', ''); $order = $request->get('order', 'stock_desc'); return view('goods/index', ['name' => $name, 'order' => $order]); } //添加商品 public function add(Request $request) { $method = $request->method(); if($method == 'POST') { $name = $request->post('name'); $price = $request->post('price'); $remark = $request->post('remark'); $stock = $request->post('stock'); $stock_small = $request->post('stock_small'); $sales = $request->post('sales'); $sales_small = $request->post('sales_small'); $split_to_small = $request->post('split_to_small'); $old = Goods::getByName($name); if(!$old->isEmpty()){ return json(['code' => 1, 'msg' => '已存在此商品']); } $goodsModel = new Goods; $goodsModel->save([ 'name' => $name, 'price' => $price, 'stock' => $stock, 'stock_small' => $stock_small, 'sales' => $sales, 'sales_small' => $sales_small, 'split_to_small' => $split_to_small, 'remark' => $remark ]); return json(['code' => 0, 'msg' => 'ok']); } return view('goods/add', []); } //编辑商品 public function edit(Request $request) { $method = $request->method(); if($method == 'POST') { $name = $request->post('name'); $price = $request->post('price'); $remark = $request->post('remark'); $stock = $request->post('stock'); $stock_small = $request->post('stock_small'); $sales = $request->post('sales'); $sales_small = $request->post('sales_small'); $split_to_small = $request->post('split_to_small'); $id = $request->post('id'); // return json([ // 'id' => $id, // 'name' => $name, // 'price' => $price, // 'stock' => $stock, // 'stock_small' => $stock_small, // 'sales' => $sales, // 'sales_small' => $sales_small, // 'split_to_small' => $split_to_small, // 'remark' => $remark // ]); try{ Goods::updateByID($id, [ 'name' => $name, 'price' => $price, 'stock' => $stock, 'stock_small' => $stock_small, 'sales' => $sales, 'sales_small' => $sales_small, 'split_to_small' => $split_to_small, 'remark' => $remark ]); return json(['code' => 0, 'msg' => 'ok']); }catch(\Exception $e){ return json(['code' => 1, 'msg' => $e->getMessage()]); } } $id = $request->get('id'); $goods = Goods::getByID($id); return view('goods/edit', ['goods' => $goods]); } //商品入库日志 public function receipt(Request $request) { return view('goods/receipt', []); } public function addReceipt(Request $request) { $method = $request->method(); if($method == 'POST') { Db::startTrans(); try { $goodsID = $request->post('goods_id'); $boughtAt = $request->post('bought_at'); $num = $request->post('num'); $totalPrice = $request->post('total_price'); $remark = $request->post('remark'); if($goodsID <= 0){ return json(['code' => 1, 'msg' => '请选择商品']); } if($totalPrice <= 0){ return json(['code' => 2, 'msg' => '进货总价必须大于0']); } if($boughtAt == ''){ return json(['code' => 3, 'msg' => '请选择进货日期']); } $goods = Goods::getByID($goodsID); if($goods->isEmpty()){ return json(['code' => 4, 'msg' => '商品不存在']); } $splitToSmall = $goods['split_to_small'] ?: 1; $costPrice = bcdiv($totalPrice, $num, 2); //计算大包单价 $smallCostPrice = bcdiv($costPrice, $splitToSmall, 2); //计算小包单价 GoodsReceipt::create([ 'goods_id' => $goodsID, 'num' => $num, 'total_price' => $totalPrice, 'cost_price' => $costPrice, 'small_cost_price' => $smallCostPrice, 'bought_at' => $boughtAt, 'split_to_small' => $splitToSmall, 'created_at' => date('Y-m-d H:i:s'), 'remark' => $remark ]); Goods::updateByID($goodsID, ['stock' => $goods['stock'] + $num]); Db::commit(); return json(['code' => 0, 'msg' => 'ok']); } catch (\Exception $e) { Db::rollback(); return json(['code' => 5, 'msg' => $e->getMessage()]); } } $goodsList = Goods::getPageList('', 100); return view('goods/addReceipt', ['goodsList' => $goodsList]); } //商品销售日志 public function sale(Request $request) { $goodsID = $request->get('id'); $goods = Goods::getByID($goodsID); $goodsList = Goods::getPageList('', 100); return view('goods/sale', ['goods' => $goods, 'goodsID' => $goodsID, 'goodsList' => $goodsList]); } public function addSale(Request $request) { $method = $request->method(); if($method == 'POST') { Db::startTrans(); try { $goodsID = $request->post('goods_id'); $sold_at = $request->post('sold_at'); $num = $request->post('num', 0); $num_small = $request->post('num_small', 0); $totalPrice = $request->post('total_price'); $remark = $request->post('remark'); if($goodsID <= 0){ return json(['code' => 1, 'msg' => '请选择商品']); } if($totalPrice <= 0){ return json(['code' => 2, 'msg' => '销售总价必须大于0']); } if($sold_at == ''){ return json(['code' => 3, 'msg' => '请选择销售时间']); } if (empty($num) && empty($num_small)) { return json(['code' => 4, 'msg' => '大包销量和小包销量不能同时为空']); } if(empty($num)){ $num = 0; } if(empty($num_small)){ $num_small = 0; } $goods = Goods::getByID($goodsID); if($goods->isEmpty()){ return json(['code' => 5, 'msg' => '商品不存在']); } // 小包库存不足时,没有自动拆分大包的操作 if ($goods->stock < $num || $goods->stock_small < $num_small) { return json(['code' => 6, 'msg' => '商品库存不足']); } SaleLog::create([ 'goods_id' => $goodsID, 'num' => $num, 'num_small' => $num_small, 'total_price' => $totalPrice, 'sold_at' => $sold_at, 'remark' => $remark ]); Goods::updateByID($goodsID, [ 'sales' => $goods->sales + $num, 'sales_small' => $goods->sales_small + $num_small, 'stock' => $goods->stock - $num, 'stock_small' => $goods->stock_small - $num_small ]); Db::commit(); return json(['code' => 0, 'msg' => 'ok']); } catch (\Exception $e) { Db::rollback(); return json(['code' => 7, 'msg' => $e->getMessage()]); } } $goodsList = Goods::getPageList('', 100); return view('goods/addSale', ['goodsList' => $goodsList]); } //获取商品列表 public function apiGetList(Request $request) { $name = $request->get('name', ''); $order = $request->get('order', 'stock_desc'); $pageSize = $request->get('size', 20); $goodsList = Goods::getPageList($name, $pageSize, str_replace('_', ' ', $order)); return json(['code' => 0, 'msg' => 'ok', 'goodsList' => $goodsList]); } //获取进货列表 public function apiGetReceiptList(Request $request) { $pageSize = $request->get('size', 20); $receiptList = GoodsReceipt::getPageList($pageSize); return json(['code' => 0, 'msg' => 'ok', 'receiptList' => $receiptList]); } public function apiGetSaleList(Request $request) { $pageSize = $request->get('size', 20); $saleList = SaleLog::getPageList($pageSize); return json(['code' => 0, 'msg' => 'ok', 'saleList' => $saleList]); } public function apiDelReceipt(Request $request) { $method = $request->method(); if($method == 'POST') { $id = $request->post('id'); $goodsReceipt = GoodsReceipt::getByID($id); if($goodsReceipt->isEmpty()){ return json(['code' => 1, 'msg' => '进货记录不存在']); } Db::startTrans(); try { $goodsID = $goodsReceipt['goods_id']; $num = $goodsReceipt['num']; $goods = Goods::getByID($goodsID); if($goods->isEmpty()){ return json(['code' => 2, 'msg' => '商品不存在']); } GoodsReceipt::destroy($id); Goods::updateByID($goodsID, ['stock' => $goods['stock'] - $num]); Db::commit(); return json(['code' => 0, 'msg' => 'ok']); } catch (\Exception $e) { Db::rollback(); return json(['code' => 3, 'msg' => $e->getMessage()]); } } return json(['code' => 4, 'msg' => '请求方式错误']); } }