StockWarning.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\admin\controller\shopro\goods;
  3. use app\admin\controller\shopro\Common;
  4. use think\Db;
  5. use app\admin\model\shopro\goods\SkuPrice as SkuPriceModel;
  6. use app\admin\model\shopro\goods\StockWarning as StockWarningModel;
  7. use addons\shopro\traits\StockWarning as StockWarningTrait;
  8. /**
  9. * 库存预警
  10. */
  11. class StockWarning extends Common
  12. {
  13. use StockWarningTrait;
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->model = new StockWarningModel;
  18. }
  19. /**
  20. * 库存预警列表
  21. *
  22. * @return \think\Response
  23. */
  24. public function index()
  25. {
  26. if (!$this->request->isAjax()) {
  27. return $this->view->fetch();
  28. }
  29. $skuPriceTableName = (new SkuPriceModel())->getQuery()->getTable();
  30. $stockWarnings = $this->model->sheepFilter()->alias('g')->with(['goods' => function ($query) {
  31. $query->removeOption('soft_delete');
  32. }])
  33. ->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
  34. ->field('g.*,sp.stock')
  35. ->paginate($this->request->param('list_rows', 10));
  36. $warning_total = $this->model->sheepFilter(false, function ($filters) {
  37. $filters['stock_type'] = 'no_enough';
  38. return $filters;
  39. })->alias('g')
  40. ->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
  41. ->field('g.*,sp.stock')
  42. ->count();
  43. $over_total = $this->model->sheepFilter(false, function ($filters) {
  44. $filters['stock_type'] = 'over';
  45. return $filters;
  46. })->alias('g')
  47. ->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
  48. ->field('g.*,sp.stock')
  49. ->count();
  50. $result = [
  51. 'rows' => $stockWarnings,
  52. 'warning_total' => $warning_total,
  53. 'over_total' => $over_total,
  54. ];
  55. $this->success('获取成功', null, $result);
  56. }
  57. /**
  58. * 补货
  59. *
  60. * @param [type] $ids
  61. * @param [type] $stock
  62. * @return void
  63. */
  64. public function addStock ($id) {
  65. if ($this->request->isAjax()) {
  66. $params = $this->request->only(['stock']);
  67. $this->svalidate($params, ".add");
  68. $stockWarning = $this->model->with(['sku_price'])->where('id', $id)->find();
  69. if (!$stockWarning) {
  70. $this->error(__('No Results were found'));
  71. }
  72. if (!$stockWarning->sku_price) {
  73. $this->error('库存规格不存在');
  74. }
  75. Db::transaction(function () use ($stockWarning, $params) {
  76. // 补货
  77. $this->addStockToSkuPrice($stockWarning->sku_price, $params['stock'], 'stock_warning');
  78. });
  79. $this->success('补货成功');
  80. }
  81. return $this->view->fetch();
  82. }
  83. public function recyclebin()
  84. {
  85. if ($this->request->isAjax()) {
  86. $skuPriceTableName = (new SkuPriceModel())->getQuery()->getTable();
  87. $stockWarnings = $this->model->onlyTrashed()->sheepFilter()->alias('g')->with(['goods' => function ($query) {
  88. $query->removeOption('soft_delete');
  89. }])
  90. ->join($skuPriceTableName . ' sp', 'g.goods_sku_price_id = sp.id', 'left')
  91. ->field('g.*,sp.stock')
  92. ->paginate($this->request->param('list_rows', 10));
  93. $this->success('获取成功', null, $stockWarnings);
  94. }
  95. return $this->view->fetch();
  96. }
  97. }