Stock.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\common\model\ProductModel;
  5. use app\common\model\StoreModel;
  6. use app\common\model\StoreProductModel;
  7. use think\App;
  8. use think\facade\View;
  9. use think\Request;
  10. class Stock extends BaseController
  11. {
  12. private $storeModel;
  13. private $storeProductModel;
  14. private $productModel;
  15. public function __construct(App $app)
  16. {
  17. parent::__construct($app);
  18. $this->storeModel = new StoreModel();
  19. $this->storeProductModel = new StoreProductModel();
  20. $this->productModel = new ProductModel();
  21. }
  22. /**
  23. * @param Request $request
  24. * @return \think\response\View
  25. * @throws \think\db\exception\DbException
  26. */
  27. public function index(Request $request) {
  28. $params = $request->param();
  29. $format_params = [
  30. 'store_id' => format_string($params['store_id'] ?? null),
  31. 'sort' => format_string($params['sort'] ?? null)
  32. ];
  33. $list = $this->storeProductModel->findByPaginate($format_params);
  34. View::assign([
  35. 'list' => $list,
  36. 'params' => $format_params,
  37. 'stores' => $this->storeModel->findAllStore(),
  38. ]);
  39. return view();
  40. }
  41. public function add(Request $request) {
  42. $params = $request->param();
  43. $store_id = format_string($params['store_id'] ?? null);
  44. if($request->isAjax()) {
  45. $store = $this->storeModel->findById($store_id);
  46. $product = $this->productModel->findById(format_string($params['product_id'] ?? null));
  47. if (!$store || !$product)
  48. return $this->fail(lang('Store|Product does not exist'));
  49. $relation = $this->storeProductModel->doesItExist($store->id,$product->id);
  50. if($relation)
  51. return $this->fail(lang('Fail to add. Data duplication'));
  52. $res = $this->storeProductModel->save([
  53. 'store_id' => $store->id,
  54. 'product_id' => $product->id,
  55. 'product_bar_code' => $product->bar_code,
  56. 'product_name' => $product->name,
  57. 'now_stock' => $params['now_stock'],
  58. ]);
  59. return $this->ok($res);
  60. }
  61. View::assign([
  62. 'store_id' => $store_id,
  63. 'stores' => $this->storeModel->findAllStore(),
  64. 'products' => $this->productModel->findProducts()
  65. ]);
  66. return view();
  67. }
  68. public function edit(Request $request) {
  69. $params = $request->param();
  70. if(!isset($params['id']))
  71. return $this->fail(lang('ID not exist'));
  72. $relation = $this->storeProductModel->findById($params['id']);
  73. if($request->isAjax()) {
  74. $exist = $this->storeProductModel->doesItExist($params['store_id'],$params['product_id']);
  75. if(!is_null($exist) && $relation->id != $exist->id)
  76. return $this->fail(lang(('Data duplication')));
  77. $store = $this->storeModel->findById($params['store_id']);
  78. $product = $this->productModel->findById(format_string($params['product_id'] ?? null));
  79. if (!$store || !$product)
  80. return $this->fail(lang('Store|Product does not exist'));
  81. $res = $this->storeProductModel->where('id',$params['id'])->update([
  82. 'store_id' => $store->id,
  83. 'product_id' => $product->id,
  84. 'product_bar_code' => $product->bar_code,
  85. 'product_name' => $product->name,
  86. 'now_stock' => $params['now_stock'],
  87. 'update_time' => time()
  88. ]);
  89. return $this->ok($res);
  90. }
  91. View::assign([
  92. 'relation' => $relation,
  93. 'stores' => $this->storeModel->findAllStore(),
  94. 'products' => $this->productModel->findProducts()
  95. ]);
  96. return view();
  97. }
  98. public function delete(Request $request) {
  99. $params = $request->param();
  100. if(!isset($params['ids']))
  101. return $this->fail(lang("Please select the data you want to delete"));
  102. $this->storeProductModel->deleteByIds(explode(',',$params['ids']));
  103. return $this->ok(true);
  104. }
  105. public function test() {
  106. return view();
  107. }
  108. }