123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace app\admin\model\system;
- use app\admin\model\store\StoreProductAttrValue;
- use app\admin\model\ump\StoreBargain;
- use app\admin\model\ump\StoreCombination;
- use app\admin\model\ump\StoreFree;
- use app\admin\model\ump\StoreLimit;
- use app\admin\model\ump\StoreSeckill;
- use app\models\store\StoreProduct;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- use think\facade\Db;
- /**
- * 店铺仓库明细 model
- * Class User
- * @package app\admin\model\user
- */
- class SystemStoreProductStockLog extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'system_store_product_stock_log';
- use ModelTrait;
- //修改库存减少库存记录
- public static function expend($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' => 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');
- }
- }
|