$unique])->find(); if (!$info) { return self::setErrorInfo('找不到对应商品规格'); } if ($info['type'] > 0) { $unique = StoreProductAttrValue::where('product_id', $product_id)->where('suk', $info['suk'])->where('type', 0)->value('unique'); } if (!$unique) { return self::setErrorInfo('找不到对应商品规格'); } $stock = SystemStoreProductStock::where(['product_id' => $product_id, 'unique' => $unique, 'store_id' => $store_id])->find(); if (!$stock) { $stock = SystemStoreProductStock::create(['product_id' => $product_id, 'unique' => $unique, 'store_id' => $store_id, 'add_time' => time(), 'stock' => 999, 'in_use' => 1, 'price' => $price]); } if (!$stock['in_use']) return self::setErrorInfo('所选门店未上架' . StoreProduct::get($product_id)['store_name'] . '[' . $info['suk'] . ']'); if ($number > 0) { $balance = $stock['stock'] - $number; if ($balance < 0) { return self::setErrorInfo('所选门店' . StoreProduct::get($product_id)['store_name'] . '[' . $info['suk'] . ']库存不足'); } SystemStoreProductStock::where('id', $stock['id'])->dec('stock', $number)->update(['price' => $price]); $pm = 0; $add_time = time(); $stock_id = $stock['id']; return self::create(compact('stock_id', 'link_id', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'add_time', 'adder_id')); } else if ($price != $stock['price']) { return SystemStoreProductStock::where('id', $stock['id'])->update(['price' => $price]); } else { return true; } } //修改库存增加库存记录 public static function income($store_id, $product_id, $unique, $type, $link_id, $number, $adder_id, $mark = '', $status = 1, $price = 0) { $info = StoreProductAttrValue::where(['unique' => $unique])->find(); if (!$info) { return self::setErrorInfo('找不到对应商品规格'); } if ($info['type'] > 0) { $unique = StoreProductAttrValue::where('product_id', $product_id)->where('suk', $info['suk'])->where('type', 0)->value('unique'); } if (!$unique) { return self::setErrorInfo('找不到对应商品规格'); } $stock = SystemStoreProductStock::where(['product_id' => $product_id, 'unique' => $unique, 'store_id' => $store_id])->find(); if (!$stock) { $stock = SystemStoreProductStock::create(['product_id' => $product_id, 'unique' => $unique, 'store_id' => $store_id, 'add_time' => time(), 'stock' => 0, 'in_use' => 1, 'price' => $price]); } if ($number > 0) { $balance = $stock['stock'] + $number; SystemStoreProductStock::where('id', $stock['id'])->inc('stock', $number)->update(['in_use' => 1, 'price' => $price]); $pm = 1; $add_time = time(); $stock_id = $stock['id']; return self::create(compact('stock_id', 'link_id', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'add_time', 'adder_id')); } else if ($price != $stock['price']) { return SystemStoreProductStock::where('id', $stock['id'])->update(['price' => $price]); } else { return true; } } /** * @param $where * @param $store_id * @return array */ public static function getList($where, $store_id) { $model = self::where('stock_id', $store_id); $count = $model->count(); $data = $model->order('add_time', 'desc')->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($item) { $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']); $item['adder'] = SystemAdmin::where('id', $item['adder_id'])->value('real_name') ?: '--'; }); return compact('count', 'data'); } }