| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace app\admin\controller;
- use app\BaseController;
- use app\common\model\ProductModel;
- use app\common\model\StoreModel;
- use app\common\model\StoreProductModel;
- use think\App;
- use think\facade\View;
- use think\Request;
- class Stock extends BaseController
- {
- private $storeModel;
- private $storeProductModel;
- private $productModel;
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->storeModel = new StoreModel();
- $this->storeProductModel = new StoreProductModel();
- $this->productModel = new ProductModel();
- }
- /**
- * @param Request $request
- * @return \think\response\View
- * @throws \think\db\exception\DbException
- */
- public function index(Request $request) {
- $params = $request->param();
- $format_params = [
- 'store_id' => format_string($params['store_id'] ?? null),
- 'sort' => format_string($params['sort'] ?? null)
- ];
- $list = $this->storeProductModel->findByPaginate($format_params);
- View::assign([
- 'list' => $list,
- 'params' => $format_params,
- 'stores' => $this->storeModel->findAllStore(),
- ]);
- return view();
- }
- public function add(Request $request) {
- $params = $request->param();
- $store_id = format_string($params['store_id'] ?? null);
- if($request->isAjax()) {
- $store = $this->storeModel->findById($store_id);
- $product = $this->productModel->findById(format_string($params['product_id'] ?? null));
- if (!$store || !$product)
- return $this->fail(lang('Store|Product does not exist'));
- $relation = $this->storeProductModel->doesItExist($store->id,$product->id);
- if($relation)
- return $this->fail(lang('Fail to add. Data duplication'));
- $res = $this->storeProductModel->save([
- 'store_id' => $store->id,
- 'product_id' => $product->id,
- 'product_bar_code' => $product->bar_code,
- 'product_name' => $product->name,
- 'now_stock' => $params['now_stock'],
- ]);
- return $this->ok($res);
- }
- View::assign([
- 'store_id' => $store_id,
- 'stores' => $this->storeModel->findAllStore(),
- 'products' => $this->productModel->findProducts()
- ]);
- return view();
- }
- public function edit(Request $request) {
- $params = $request->param();
- if(!isset($params['id']))
- return $this->fail(lang('ID not exist'));
- $relation = $this->storeProductModel->findById($params['id']);
- if($request->isAjax()) {
- $exist = $this->storeProductModel->doesItExist($params['store_id'],$params['product_id']);
- if(!is_null($exist) && $relation->id != $exist->id)
- return $this->fail(lang(('Data duplication')));
- $store = $this->storeModel->findById($params['store_id']);
- $product = $this->productModel->findById(format_string($params['product_id'] ?? null));
- if (!$store || !$product)
- return $this->fail(lang('Store|Product does not exist'));
- $res = $this->storeProductModel->where('id',$params['id'])->update([
- 'store_id' => $store->id,
- 'product_id' => $product->id,
- 'product_bar_code' => $product->bar_code,
- 'product_name' => $product->name,
- 'now_stock' => $params['now_stock'],
- 'update_time' => time()
- ]);
- return $this->ok($res);
- }
- View::assign([
- 'relation' => $relation,
- 'stores' => $this->storeModel->findAllStore(),
- 'products' => $this->productModel->findProducts()
- ]);
- return view();
- }
- public function delete(Request $request) {
- $params = $request->param();
- if(!isset($params['ids']))
- return $this->fail(lang("Please select the data you want to delete"));
- $this->storeProductModel->deleteByIds(explode(',',$params['ids']));
- return $this->ok(true);
- }
- public function test() {
- return view();
- }
- }
|