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')); // $product = $this->productModel->findById($params['id']); // if($request->isAjax()) { // $category = $this->categoryModel->findById($params['category_id']); // if(!$category) // return $this->fail(lang('Category does not exist')); // $res = $this->productModel->where('id',$params['id'])->update([ // 'bar_code' => $params['bar_code'], // 'name' => $params['name'] ?? null, // 'image' => $params['image'] ?? null, // 'category_id' => $params['category_id'] ?? null, // 'category_name' => $category->name, // 'is_serve' => (int)$params['is_serve'], // 'purchase_price'=> $params['purchase_price'], // 'selling_price' => $params['selling_price'] // ]); // return $this->ok($res); // } // View::assign([ // 'product' => $product, // 'all_category' => recursion($this->categoryModel->findAll(),0), // ]); 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(); } }