Goods.php 12 KB

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