Stockcur.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\admin\controller\stock;
  3. use app\common\controller\Backend;
  4. /**
  5. * 商品库存
  6. *
  7. * @icon fa fa-circle-o
  8. */
  9. class Stockcur extends Backend {
  10. /**
  11. * StockStockcur模型对象
  12. * @var \app\admin\model\stock\Stockcur
  13. */
  14. protected $noNeedLogin = ['getGoodsStoknum', 'countData'];
  15. protected $model = null;
  16. public function _initialize() {
  17. parent::_initialize();
  18. $this->model = model('\app\admin\model\stock\Stockcur');
  19. }
  20. /**
  21. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  22. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  23. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  24. */
  25. /**
  26. * 查看
  27. */
  28. public function index() {
  29. //设置过滤方法
  30. $this->request->filter(['strip_tags']);
  31. if ($this->request->isAjax()) {
  32. //如果发送的来源是Selectpage,则转发到Selectpage
  33. if ($this->request->request('keyField')) {
  34. return $this->selectpage();
  35. }
  36. //当前是否为关联查询
  37. $this->relationSearch = true;
  38. $this->searchFields = 'stockgoods.volnum,stockgoods.goodsname';
  39. $categoryids = $this->request->request("categoryids");
  40. $query = [];
  41. if ($categoryids !== NULL) {
  42. $query["stockgoods.goodscategoryid"] = ["in", $categoryids];
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $total = $this->model->with('stockgoods')->field("sum(curnums) as totalcurnums")->where($where)->where($query)->group("stock_goods_id")->order($sort, $order)->count();
  46. $list = $this->model->with('stockgoods')->field("sum(curnums) as totalcurnums")->where($where)->where($query)->group("stock_goods_id")->order($sort, $order)->limit($offset, $limit)->select();
  47. $list = collection($list)->toArray();
  48. $result = array("total" => $total, "rows" => $list);
  49. return json($result);
  50. }
  51. return $this->view->fetch();
  52. }
  53. /**
  54. * 库存首页
  55. */
  56. public function mainpage() {
  57. $stockinsum=\app\admin\model\stock\Stockinlist::with('stockin')
  58. ->field('IFNULL(SUM(stockinnums),0) as totalnums,IFNULL(SUM(amount),0) as totalamount')
  59. ->where('stockin.audittime','NOT NULL')
  60. ->whereTime("stockin.audittime",'today')
  61. ->select();
  62. // echo "<pre>";
  63. // print_r($stockinsum);
  64. // echo "</pre>";
  65. // die;
  66. $stockoutsum=\app\admin\model\stock\Stockoutlist::with('stockout')
  67. ->field('IFNULL(SUM(stockoutnums),0) totalnums,IFNULL(SUM(amount),0) totalamount')
  68. ->where('stockout.audittime','NOT NULL')
  69. ->whereTime("stockout.audittime",'today')
  70. ->select();
  71. $stockcursum=\app\admin\model\stock\Stockcur::with('stockinlist')
  72. ->field('IFNULL(SUM(curnums),0) totalnums,IFNULL(SUM(stockinlist.inboundprice*curnums),0) totalamount')
  73. ->select();
  74. $stockin_toaudit=\app\admin\model\stock\Stockin::where('audittime','NULL')->count();
  75. $stockout_toaudit=\app\admin\model\stock\Stockout::where('audittime','NULL')->count();
  76. if ($this->request->isAjax()) {
  77. $stockinsum=\app\admin\model\stock\Stockinlist::field('sum(stockinnums) totalnums,sum(amount) totalamount')->where("createtime",'today')->select();
  78. $result = array("stockinsum" => $stockinsum);
  79. return json($result);
  80. }
  81. $this->view->assign("stockinsum", $stockinsum[0]);
  82. $this->view->assign("stockoutsum", $stockoutsum[0]);
  83. $this->view->assign("stockcursum", $stockcursum[0]);
  84. $this->view->assign("stockin_toaudit", $stockin_toaudit);
  85. $this->view->assign("stockout_toaudit", $stockout_toaudit);
  86. return $this->view->fetch();
  87. }
  88. /**
  89. * 编辑
  90. */
  91. public function edit($ids = null) {
  92. $row = $this->model->get($ids);
  93. if (!$row) {
  94. $this->error(__('No Results were found'));
  95. }
  96. $adminIds = $this->getDataLimitAdminIds();
  97. if (is_array($adminIds)) {
  98. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  99. $this->error(__('You have no permission'));
  100. }
  101. }
  102. $stockinmodel = model('StockStockin');
  103. if ($this->request->isPost()) {
  104. $params = $this->request->post("row/a");
  105. if ($params) {
  106. try {
  107. //是否采用模型验证
  108. if ($this->modelValidate) {
  109. $name = basename(str_replace('\\', '/', get_class($this->model)));
  110. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  111. $row->validate($validate);
  112. }
  113. $id = $params['stock_stockin_id'];
  114. $allocation = $params['allocation'];
  115. $result = $stockinmodel->where('id', $id)->update(['allocation' => $allocation]);
  116. if ($result !== false) {
  117. $this->success();
  118. } else {
  119. $this->error($row->getError());
  120. }
  121. } catch (\think\exception\PDOException $e) {
  122. $this->error($e->getMessage());
  123. }
  124. }
  125. $this->error(__('Parameter %s can not be empty', ''));
  126. }
  127. $this->view->assign("row", $row);
  128. return $this->view->fetch();
  129. }
  130. /**
  131. * 出库单,根据选定商品返回库存数
  132. */
  133. public function getGoodsStoknum($goodsid) {
  134. if ($this->request->isPost()) {
  135. $goodsid = $this->request->post('goodsid');
  136. if ($goodsid) {
  137. try {
  138. $curnum = $this->model->getGoodsStoknum($goodsid);
  139. return json($curnum);
  140. } catch (\think\exception\PDOException $e) {
  141. $this->error($e->getMessage());
  142. }
  143. }
  144. $this->error(__('Parameter %s can not be empty', ''));
  145. }
  146. $this->error('只接受POST请求', '');
  147. }
  148. /**
  149. * 库存首页统计
  150. */
  151. public function countData() {
  152. //当前是否为关联查询
  153. $this->relationSearch = true;
  154. if ($this->request->isAjax()) {
  155. $total = $this->model
  156. ->group('stock_goods_id')
  157. ->count();
  158. $list = $this->model
  159. ->with('StockGoods,Stockinlist')
  160. ->field('sum(curnums*inboundprice) totalitemamount,count(*) totalitem,sum(curnums) totalnum')
  161. ->group('goodscategoryid')
  162. ->select();
  163. $list = collection($list)->toArray();
  164. $result = array("total" => $total, "rows" => $list);
  165. return json($result);
  166. }
  167. }
  168. }