SystemStoreProductStockLog.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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)
  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' => 0, 'in_use' => 0]);
  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();
  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 {
  60. return true;
  61. }
  62. }
  63. //修改库存增加库存记录
  64. public static function income($store_id, $product_id, $unique, $type, $link_id, $number, $adder_id, $mark = '', $status = 1)
  65. {
  66. $info = StoreProductAttrValue::where(['unique' => $unique])->find();
  67. if (!$info) {
  68. return self::setErrorInfo('找不到对应商品规格');
  69. }
  70. if ($info['type'] > 0) {
  71. $unique = StoreProductAttrValue::where('product_id', $product_id)->where('suk', $info['suk'])->where('type', 0)->value('unique');
  72. }
  73. if (!$unique) {
  74. return self::setErrorInfo('找不到对应商品规格');
  75. }
  76. $stock = SystemStoreProductStock::where(['product_id' => $product_id, 'unique' => $unique, 'store_id' => $store_id])->find();
  77. if (!$stock) {
  78. $stock = SystemStoreProductStock::create(['product_id' => $product_id, 'unique' => $unique, 'store_id' => $store_id, 'add_time' => time(), 'stock' => 0, 'in_use' => 1]);
  79. }
  80. if ($number > 0) {
  81. $balance = $stock['stock'] + $number;
  82. SystemStoreProductStock::where('id', $stock['id'])->inc('stock', $number)->update();
  83. $pm = 1;
  84. $add_time = time();
  85. $stock_id = $stock['id'];
  86. return self::create(compact('stock_id', 'link_id', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'add_time', 'adder_id'));
  87. } else {
  88. return true;
  89. }
  90. }
  91. /**
  92. * @param $where
  93. * @param $store_id
  94. * @return array
  95. */
  96. public static function getList($where, $store_id)
  97. {
  98. $model = self::where('stock_id', $store_id);
  99. $count = $model->count();
  100. $data = $model->order('add_time', 'desc')->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($item) {
  101. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  102. $item['adder'] = SystemAdmin::where('id', $item['adder_id'])->value('real_name') ?: '--';
  103. });
  104. return compact('count', 'data');
  105. }
  106. }