Goods.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace app\admin\controller\stock;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. /**
  6. * 商品库
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Goods extends Backend {
  11. /**
  12. * StockGoods模型对象
  13. * @var \app\admin\model\stock\Goods
  14. */
  15. protected $noNeedRight = ['detail', 'madevolnum', 'getlist','getgoodsbybarcodeorid'];
  16. protected $model = null;
  17. /**
  18. * 快速搜索时执行查找的字段
  19. */
  20. protected $relationSearch = true;
  21. protected $searchFields = 'volnum,goodsname';
  22. public function _initialize() {
  23. parent::_initialize();
  24. $this->model = model('\app\admin\model\stock\Goods');
  25. $goodscategorylist = collection(model('\app\admin\model\stock\Goodscategory')->select())->toArray();
  26. Tree::instance()->init($goodscategorylist);
  27. $goodscategorylist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
  28. $categorylist = array('' => '==请选择==');
  29. $this->categoryList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
  30. foreach ($goodscategorylist as $k => $v) {
  31. $categorylist[$v['id']] = $v['name'];
  32. }
  33. $measureunit = model('\app\admin\model\stock\Datadict')->getDatadic('JiLiangDW');
  34. $this->view->assign('goodscategorylist', $categorylist);
  35. $this->view->assign('measureunit', $measureunit);
  36. }
  37. /**
  38. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  39. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  40. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  41. */
  42. /**
  43. * 查看
  44. */
  45. public function index() {
  46. //设置过滤方法
  47. $this->request->filter(['strip_tags']);
  48. if ($this->request->isAjax()) {
  49. //如果发送的来源是Selectpage,则转发到Selectpage
  50. if ($this->request->request('keyField')) {
  51. return $this->selectpage();
  52. }
  53. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  54. $categoryids = $this->request->request("categoryids");
  55. $query = [];
  56. if ($categoryids !== "") {
  57. $query["goods.goodscategoryid"] = ["in", $categoryids];
  58. }
  59. $total = $this->model
  60. ->with('goodscategory')
  61. ->where($where)
  62. ->where($query)
  63. ->order($sort, $order)
  64. ->count();
  65. $list = $this->model
  66. ->with('goodscategory')
  67. ->where($where)
  68. ->where($query)
  69. ->order($sort, $order)
  70. ->limit($offset, $limit)
  71. ->select();
  72. $list = collection($list)->toArray();
  73. $result = array("total" => $total, "rows" => $list);
  74. return json($result);
  75. }
  76. return $this->view->fetch();
  77. }
  78. /**
  79. * 获取所有商品列表
  80. */
  81. public function getlist() {
  82. //设置过滤方法
  83. if ($this->request->isAjax()) {
  84. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  85. $searchvalue = $this->request->request("searchvalue");
  86. $query = [];
  87. if ($searchvalue) {
  88. $query["volnum|goodsname|spell"] = ["LIKE", '%' . $searchvalue . '%'];
  89. }
  90. $total = $this->model
  91. ->with('goodscategory')
  92. ->where($where)
  93. ->where($query)
  94. ->order($sort, $order)
  95. ->count();
  96. $list = $this->model
  97. ->with('goodscategory')
  98. ->where($where)
  99. ->where($query)
  100. ->order($sort, $order)
  101. ->limit($offset, $limit)
  102. ->select();
  103. $list = collection($list)->toArray();
  104. $result = array("total" => $total, "rows" => $list);
  105. return json($result);
  106. }
  107. }
  108. /**
  109. * 添加
  110. */
  111. public function add() {
  112. if ($this->request->isPost()) {
  113. $params = $this->request->post("row/a");
  114. if ($params) {
  115. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  116. $params[$this->dataLimitField] = $this->auth->id;
  117. }
  118. $map = [
  119. 'goodsname' => $params['goodsname'],
  120. 'productmodel' => $params['productmodel'],
  121. ];
  122. $list = $this->model->where($map)->count();
  123. if ($list == 0) {
  124. try {
  125. //是否采用模型验证
  126. if ($this->modelValidate) {
  127. $name = basename(str_replace('\\', '/', get_class($this->model)));
  128. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  129. $this->model->validate($validate);
  130. }
  131. $result = $this->model->allowField(true)->save($params);
  132. if ($result !== false) {
  133. $this->success();
  134. } else {
  135. $this->error($this->model->getError());
  136. }
  137. } catch (\think\exception\PDOException $e) {
  138. $this->error($e->getMessage());
  139. }
  140. } else {
  141. $this->error(__('商品已存在', ''));
  142. }
  143. }
  144. $this->error(__('Parameter %s can not be empty', ''));
  145. }
  146. return $this->view->fetch();
  147. }
  148. /**
  149. * 删除
  150. */
  151. public function del($ids = "") {
  152. if ($ids) {
  153. $pk = $this->model->getPk();
  154. $adminIds = $this->getDataLimitAdminIds();
  155. if (is_array($adminIds)) {
  156. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  157. }
  158. $list = $this->model->where($pk, 'in', $ids)->select();
  159. $count = 0;
  160. foreach ($list as $k => $v) {
  161. $goodinfo = model('\app\admin\model\stock\Stockinlist')->where('stock_goods_id', '=', $v->id)->limit(1)->select();
  162. if ($goodinfo) {
  163. $this->error('该商品存在入库信息,不能删除!');
  164. } else {
  165. $count += $v->delete();
  166. }
  167. }
  168. if ($count) {
  169. $this->success();
  170. } else {
  171. $this->error(__('No rows were deleted'));
  172. }
  173. }
  174. $this->error(__('Parameter %s can not be empty', 'ids'));
  175. }
  176. /**
  177. * 出入明细
  178. */
  179. public function detail($ids = null) {
  180. if ($this->request->isAjax()) {
  181. $ids = $this->request->request('ids');
  182. //如果发送的来源是Selectpage,则转发到Selectpage
  183. if ($this->request->request('keyField')) {
  184. return $this->selectpage();
  185. }
  186. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  187. $subQuery = \think\Db::field('\'入库\' AS leixing, `stock_goods_id`,`inboundtime` AS riqi,stockinnums,\'\' as stockoutnums,amount AS stockinamount,\'\' as stockoutamount')
  188. ->name('stock_stockin_list')
  189. ->alias('list')
  190. ->join('stock_stockin stockin','stockin.id=list.stock_stockin_id')
  191. ->union(function ($query) {
  192. $query->field('\'出库\' AS leixing,`stock_goods_id`,`outboundtime` AS riqi,\'\' as stockinnums,stockoutnums,\'\' as stockinamount,amount AS stockoutamount')
  193. ->name('stock_stockout_list')
  194. ->alias('list')
  195. ->join('stock_stockout stockout','stockout.id=list.stock_stockout_id');
  196. })
  197. ->buildSql();
  198. $sum = \think\Db::table($subQuery . ' a')
  199. ->field('COUNT(*) AS total, SUM(a.stockinnums) AS totalstockinnums,SUM(a.stockinamount) AS totalstockinamount,SUM(a.stockoutnums) AS totalstockoutnums,SUM(a.stockoutamount) AS totalstockoutamount')
  200. ->where('a.stock_goods_id', $ids)->select();
  201. $total = $sum[0]['total'];
  202. $list = \think\Db::view([$subQuery => 'a'], '*')
  203. ->view('stock_goods goods', '*', 'a.stock_goods_id=goods.id', 'left')
  204. ->where('goods.id', $ids)->where($where)->order($sort, $order)->limit($offset, $limit)->select();
  205. $list = collection($list)->toArray();
  206. if (count($list) > 0) {
  207. $list[0]["sum"] = $sum[0];
  208. }
  209. $result = array("total" => $total, "rows" => $list);
  210. return json($result);
  211. }
  212. $this->view->assign("ids", $ids);
  213. return $this->view->fetch();
  214. }
  215. /**
  216. * 根据商品类别生成商品代码
  217. */
  218. public function madevolnum() {
  219. if ($this->request->isPost()) {
  220. $goodscategoryid = $this->request->post('goodscategoryid');
  221. if ($goodscategoryid) {
  222. try {
  223. //生产商品代码
  224. $aVolnum = $this->model->where('goodscategoryid', $goodscategoryid)->field('volnum')->order('volnum desc')->limit(1)->select();
  225. $volnum = '';
  226. if (count($aVolnum) > 0) {
  227. $volnumArr = explode('.', $aVolnum[0]['volnum']);
  228. $volnumArr[count($volnumArr) - 1] = sprintf("%03d", $volnumArr[count($volnumArr) - 1] + 1);
  229. $volnum = implode('.', $volnumArr);
  230. } else {
  231. $bVolnum = model('\app\admin\model\stock\Goodscategory')->where('id', $goodscategoryid)->column('value');
  232. if (count($bVolnum) > 0) {
  233. $volnum = ($bVolnum[0] . '.001');
  234. } else {
  235. $volnum = "001";
  236. }
  237. }
  238. //查询商品类别名称
  239. $result['volnum'] = $volnum;
  240. $result['goodscategory'] = $goodscategoryid;
  241. $this->success("获取成功!", "", $result);
  242. } catch (\think\exception\PDOException $e) {
  243. $this->error($e->getMessage());
  244. }
  245. }
  246. $this->error(__('Parameter %s can not be empty', ''));
  247. }
  248. $this->error('只接受POST请求', '');
  249. }
  250. public function getgoodsbybarcodeorid() {
  251. if ($this->request->isPost()) {
  252. $barcode = $this->request->post('barcode');
  253. $id = $this->request->post('id');
  254. if ($barcode) {
  255. $goods = $this->model->where("barcode",$barcode)->find();
  256. if ($goods) {
  257. // $goods["curstocknum"]=$goods->curstocknum;
  258. $this->success("查询成功!", "", $goods);
  259. } else {
  260. $this->success("没有找到商品!", "", $goods);
  261. }
  262. }else if($id) {
  263. $goods = $this->model->get($id);
  264. if ($goods) {
  265. // $goods["curstocknum"]=$goods->curstocknum;
  266. $this->success("查询成功!", "", $goods);
  267. } else {
  268. $this->success("没有找到商品!", "", $goods);
  269. }
  270. }
  271. $this->error(__('参数不能为空!', ''));
  272. }
  273. $this->error('只接受POST请求', '');
  274. }
  275. /**
  276. * 导入
  277. */
  278. public function import() {
  279. return parent::import();
  280. }
  281. }