SystemStoreProductStockLog.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\admin\model\system;
  3. use app\admin\model\store\StoreProductAttrValue;
  4. use app\admin\model\ump\StoreBargain;
  5. use app\admin\model\ump\StoreCombination;
  6. use app\admin\model\ump\StoreFree;
  7. use app\admin\model\ump\StoreLimit;
  8. use app\admin\model\ump\StoreSeckill;
  9. use app\models\store\StoreProduct;
  10. use crmeb\traits\ModelTrait;
  11. use crmeb\basic\BaseModel;
  12. use think\facade\Db;
  13. /**
  14. * 店铺仓库明细 model
  15. * Class User
  16. * @package app\admin\model\user
  17. */
  18. class SystemStoreProductStockLog extends BaseModel
  19. {
  20. /**
  21. * 数据表主键
  22. * @var string
  23. */
  24. protected $pk = 'id';
  25. /**
  26. * 模型名称
  27. * @var string
  28. */
  29. protected $name = 'system_store_product_stock_log';
  30. use ModelTrait;
  31. //修改库存减少库存记录
  32. public static function expend($store_id, $product_id, $unique, $type, $link_id, $number, $adder_id, $mark = '', $status = 1, $price = 0)
  33. {
  34. $info = StoreProductAttrValue::where(['unique' => $unique])->find();
  35. if (!$info) {
  36. return self::setErrorInfo('找不到对应商品规格');
  37. }
  38. if ($info['type'] > 0) {
  39. $unique = StoreProductAttrValue::where('product_id', $product_id)->where('suk', $info['suk'])->where('type', 0)->value('unique');
  40. }
  41. if (!$unique) {
  42. return self::setErrorInfo('找不到对应商品规格');
  43. }
  44. $stock = SystemStoreProductStock::where(['product_id' => $product_id, 'unique' => $unique, 'store_id' => $store_id])->find();
  45. if (!$stock) {
  46. $stock = SystemStoreProductStock::create(['product_id' => $product_id, 'unique' => $unique, 'store_id' => $store_id, 'add_time' => time(), 'stock' => 999, 'in_use' => 1, 'price' => $price]);
  47. }
  48. if (!$stock['in_use']) return self::setErrorInfo('所选门店未上架' . StoreProduct::get($product_id)['store_name'] . '[' . $info['suk'] . ']');
  49. if ($number > 0) {
  50. $balance = $stock['stock'] - $number;
  51. if ($balance < 0) {
  52. return self::setErrorInfo('所选门店' . StoreProduct::get($product_id)['store_name'] . '[' . $info['suk'] . ']库存不足');
  53. }
  54. SystemStoreProductStock::where('id', $stock['id'])->dec('stock', $number)->update(['price' => $price]);
  55. $pm = 0;
  56. $add_time = time();
  57. $stock_id = $stock['id'];
  58. return self::create(compact('stock_id', 'link_id', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'add_time', 'adder_id'));
  59. } else if ($price != $stock['price']) {
  60. return SystemStoreProductStock::where('id', $stock['id'])->update(['price' => $price]);
  61. } else {
  62. return true;
  63. }
  64. }
  65. //修改库存增加库存记录
  66. public static function income($store_id, $product_id, $unique, $type, $link_id, $number, $adder_id, $mark = '', $status = 1, $price = 0)
  67. {
  68. $info = StoreProductAttrValue::where(['unique' => $unique])->find();
  69. if (!$info) {
  70. return self::setErrorInfo('找不到对应商品规格');
  71. }
  72. if ($info['type'] > 0) {
  73. $unique = StoreProductAttrValue::where('product_id', $product_id)->where('suk', $info['suk'])->where('type', 0)->value('unique');
  74. }
  75. if (!$unique) {
  76. return self::setErrorInfo('找不到对应商品规格');
  77. }
  78. $stock = SystemStoreProductStock::where(['product_id' => $product_id, 'unique' => $unique, 'store_id' => $store_id])->find();
  79. if (!$stock) {
  80. $stock = SystemStoreProductStock::create(['product_id' => $product_id, 'unique' => $unique, 'store_id' => $store_id, 'add_time' => time(), 'stock' => 0, 'in_use' => 1, 'price' => $price]);
  81. }
  82. if ($number > 0) {
  83. $balance = $stock['stock'] + $number;
  84. SystemStoreProductStock::where('id', $stock['id'])->inc('stock', $number)->update(['in_use' => 1, 'price' => $price]);
  85. $pm = 1;
  86. $add_time = time();
  87. $stock_id = $stock['id'];
  88. return self::create(compact('stock_id', 'link_id', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'add_time', 'adder_id'));
  89. } else if ($price != $stock['price']) {
  90. return SystemStoreProductStock::where('id', $stock['id'])->update(['price' => $price]);
  91. } else {
  92. return true;
  93. }
  94. }
  95. /**
  96. * @param $where
  97. * @param $store_id
  98. * @return array
  99. */
  100. public static function getList($where, $store_id)
  101. {
  102. $model = self::where('stock_id', $store_id);
  103. $count = $model->count();
  104. $data = $model->order('add_time', 'desc')->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($item) {
  105. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  106. $item['adder'] = SystemAdmin::where('id', $item['adder_id'])->value('real_name') ?: '--';
  107. });
  108. return compact('count', 'data');
  109. }
  110. }