Stockin.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. namespace app\admin\controller\stock;
  3. use app\common\controller\Backend;
  4. use DateTime;
  5. /**
  6. * 商品入库
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Stockin extends Backend
  11. {
  12. /**
  13. * StockStockin模型对象
  14. * @var \app\admin\model\stock\Stockin
  15. */
  16. protected $noNeedRight = ['getlistByDocnum', 'countData','info'];
  17. protected $model = null;
  18. protected $listmodel = null;
  19. /**
  20. * 快速搜索时执行查找的字段
  21. */
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = model('\app\admin\model\stock\Stockin');
  26. $this->listmodel = model('\app\admin\model\stock\Stockinlist');
  27. $result = collection(model('\app\admin\model\stock\Supplier')->select())->toArray();
  28. $supplier = ['' => '==请选择=='];
  29. foreach ($result as $k => $v) {
  30. $supplier[$v['id']] = $v['name'];
  31. };
  32. $this->view->assign("supplier", $supplier);
  33. }
  34. /**
  35. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  36. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  37. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  38. */
  39. /**
  40. * 查看
  41. */
  42. public function index()
  43. {
  44. //设置过滤方法
  45. $this->request->filter(['strip_tags']);
  46. if ($this->request->isAjax()) {
  47. //如果发送的来源是Selectpage,则转发到Selectpage
  48. if ($this->request->request('keyField')) {
  49. return $this->selectpage();
  50. }
  51. $this->relationSearch = true;
  52. $this->searchFields = 'docnum,supplier.name';
  53. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  54. $total = $this->model
  55. ->with("supplier")
  56. ->where($where)
  57. ->order($sort, $order)
  58. ->count();
  59. $list = $this->model
  60. ->with("supplier")
  61. ->where($where)
  62. ->order($sort, $order)
  63. ->limit($offset, $limit)
  64. ->select();
  65. $list = collection($list)->toArray();
  66. $result = array("total" => $total, "rows" => $list);
  67. return json($result);
  68. }
  69. return $this->view->fetch();
  70. }
  71. /**
  72. * 入库时序表
  73. */
  74. public function getlist()
  75. {
  76. //设置过滤方法
  77. $this->request->filter(['strip_tags']);
  78. if ($this->request->isAjax()) {
  79. //如果发送的来源是Selectpage,则转发到Selectpage
  80. if ($this->request->request('keyField')) {
  81. return $this->selectpage();
  82. }
  83. $this->relationSearch = false;
  84. $this->searchFields = 'docnum,goods.goodsname,goods.productmodel';
  85. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  86. $total = $this->listmodel
  87. ->with(['goods', 'stockin'])
  88. ->where('stockin.audittime', 'NOT NULL')
  89. ->where($where)
  90. ->order($sort, $order)
  91. ->count();
  92. $list = $this->listmodel
  93. ->with(['goods', 'stockin'])
  94. ->where('stockin.audittime', 'NOT NULL')
  95. ->where($where)
  96. ->order($sort, $order)
  97. ->limit($offset, $limit)
  98. ->select();
  99. foreach ($list as $k => $v) {
  100. $v->stockin->supplier;
  101. }
  102. $list = collection($list)->toArray();
  103. $result = array("total" => $total, "rows" => $list);
  104. return json($result);
  105. }
  106. return $this->view->fetch();
  107. }
  108. /**
  109. * 入库添加
  110. */
  111. public function add()
  112. {
  113. if ($this->request->isPost()) {
  114. $params = $this->request->post("row/a");
  115. $main = \GuzzleHttp\json_decode($params["main"], true);
  116. $goods = \GuzzleHttp\json_decode($params["goodslist"], true);
  117. if ($main && $goods) {
  118. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  119. $params[$this->dataLimitField] = $this->auth->id;
  120. }
  121. try {
  122. $main["docnum"] = $this->model->createdocnum();
  123. $main['inboundtime'] = strtotime($main['inboundtime']);
  124. $num = count($goods);
  125. $stockinTotalNum = 0;
  126. $totalamount = 0.00;
  127. $result = \app\admin\model\stock\Stockin::create($main);
  128. for ($i = 0; $i < $num; $i++) {
  129. $goods[$i]['stock_stockin_id'] = $result->id;
  130. $goods[$i]['amount'] = bcmul($goods[$i]['stockinnums'], $goods[$i]['inboundprice'], 2);
  131. $totalamount = bcadd($totalamount, $goods[$i]['amount'], 2);
  132. $stockinTotalNum += $goods[$i]['stockinnums'];
  133. }
  134. $result->totalamount = $totalamount;
  135. $result->stockintotalnums = $stockinTotalNum;
  136. $result->save();
  137. $stockinlist = new \app\admin\model\stock\Stockinlist;
  138. $stockinlist->allowField(true)->saveAll($goods);
  139. if ($result !== false) {
  140. $this->success();
  141. } else {
  142. $this->error($this->model->getError());
  143. }
  144. } catch (\think\exception\PDOException $e) {
  145. $this->error($e->getMessage());
  146. }
  147. }
  148. $this->error(__('Parameter %s can not be empty', ''));
  149. }
  150. return $this->view->fetch();
  151. }
  152. /**
  153. * 审核
  154. */
  155. public function audit()
  156. {
  157. if ($this->request->isPost()) {
  158. $params = $this->request->post("row/a");
  159. $row = $this->model->get($params["id"]);
  160. if ($row) {
  161. try {
  162. if ($row->audittime) {
  163. $this->error(__('单据已审核,不可重复审核', ''));
  164. }
  165. $row->audittime = time();
  166. $stockcurlist = [];
  167. $stockinlist = new \app\admin\model\stock\Stockinlist;
  168. $goodslist = $stockinlist->where("stock_stockin_id", $row->id)->select();
  169. foreach ($goodslist as $value) {
  170. $stockcur = ['stock_stockin_id' => $row->id, 'stock_stockin_list_id' => $value->id, 'stock_stockin_docnum' => $row->docnum, 'curnums' => $value->stockinnums, 'stock_goods_id' => $value->stock_goods_id];
  171. $stockcurlist[] = $stockcur;
  172. }
  173. $row->save();
  174. model('\app\admin\model\stock\Stockcur')->allowField(true)->saveAll($stockcurlist);
  175. $this->success();
  176. } catch (\think\exception\PDOException $e) {
  177. $this->error($e->getMessage());
  178. }
  179. }
  180. $this->error(__('Parameter %s can not be empty', ''));
  181. }
  182. }
  183. /**
  184. * 反审核
  185. */
  186. public function unaudit()
  187. {
  188. if ($this->request->isPost()) {
  189. $params = $this->request->post("row/a");
  190. if ($params) {
  191. try {
  192. $row = $this->model->get($params["id"]);
  193. $stockinlist = new \app\admin\model\stock\Stockinlist;
  194. $goodslist = $stockinlist->where("stock_stockin_id", $row->id)->select();
  195. if ($goodslist) {
  196. if (!$row->audittime) {
  197. $this->error(__('单据已反审核,不可重复反审核!', ''));
  198. };
  199. foreach ($goodslist as $k => $v) {
  200. if ($v['charged']) {
  201. $this->error(__('存在已出库商品,禁止反审核', ''));
  202. }
  203. };
  204. $row->audittime = null; //更新审核标记位
  205. $row->save();
  206. model('\app\admin\model\stock\Stockcur')->where('stock_stockin_id', $row->id)->delete();
  207. $this->success();
  208. } else {
  209. $this->error($this->model->getError());
  210. }
  211. } catch (\think\exception\PDOException $e) {
  212. $this->error($e->getMessage());
  213. }
  214. }
  215. $this->error(__('Parameter %s can not be empty', ''));
  216. }
  217. }
  218. /**
  219. * 结算
  220. */
  221. public function settle()
  222. {
  223. if ($this->request->isPost()) {
  224. $params = $this->request->post("row/a");
  225. $row = $this->model->get($params["id"]);
  226. if ($row) {
  227. try {
  228. if ($row->settletime) {
  229. $this->error(__('单据已结算,不可重复结算', ''));
  230. }
  231. $row->settletime = time();
  232. $row->save();
  233. $this->success();
  234. } catch (\think\exception\PDOException $e) {
  235. $this->error($e->getMessage());
  236. }
  237. }
  238. $this->error(__('Parameter %s can not be empty', ''));
  239. }
  240. }
  241. /**
  242. * 反结算
  243. */
  244. public function unsettle()
  245. {
  246. if ($this->request->isPost()) {
  247. $params = $this->request->post("row/a");
  248. $row = $this->model->get($params["id"]);
  249. if ($row) {
  250. try {
  251. if (!$row->settletime) {
  252. $this->error(__('单据尚未结算,操作失败', ''));
  253. }
  254. $row->settletime = null;
  255. $row->save();
  256. $this->success();
  257. } catch (\think\exception\PDOException $e) {
  258. $this->error($e->getMessage());
  259. }
  260. }
  261. $this->error(__('Parameter %s can not be empty', ''));
  262. }
  263. }
  264. /**
  265. * 编辑
  266. */
  267. public function edit($ids = null)
  268. {
  269. $row = $this->model->get($ids);
  270. if (!$row) {
  271. $this->error(__('No Results were found'));
  272. }
  273. $adminIds = $this->getDataLimitAdminIds();
  274. if (is_array($adminIds)) {
  275. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  276. $this->error(__('You have no permission'));
  277. }
  278. }
  279. if ($row['audittime']) {
  280. return ($this->error(__('该入库记录已审核,不能编辑!')));
  281. }
  282. $stockinlist = new \app\admin\model\stock\Stockinlist;
  283. $list = collection($stockinlist->where('stock_stockin_id', $row->id)->select())->toArray();
  284. foreach ($list as $k => $v) {
  285. if ($v['charged']) {
  286. return ($this->error(__('该入库记录存在已出库商品,不能编辑!')));
  287. }
  288. }
  289. if ($this->request->isPost()) {
  290. $params = $this->request->post("row/a");
  291. $main = \GuzzleHttp\json_decode($params["main"], true);
  292. $goods = \GuzzleHttp\json_decode($params["goodslist"], true);
  293. $delist = \GuzzleHttp\json_decode($params["deletedgoodslist"], true);
  294. if ($main && $goods) {
  295. try {
  296. $main["inboundtime"] = strtotime($main["inboundtime"]);
  297. $num = count($goods);
  298. $stockintotalnums = 0;
  299. $totalamount = 0.00;
  300. for ($i = 0; $i < $num; $i++) {
  301. $goods[$i]['stock_stockin_id'] = $row->id;
  302. $goods[$i]['amount'] = bcmul($goods[$i]['stockinnums'], $goods[$i]['inboundprice'], 2);
  303. $totalamount = bcadd($totalamount, $goods[$i]['amount'], 2);
  304. $stockintotalnums += $goods[$i]['stockinnums'];
  305. }
  306. $main["totalamount"] = $totalamount;
  307. $main["stockintotalnums"] = $stockintotalnums;
  308. unset($main["supplier"]);
  309. $row->save($main);
  310. $result = $stockinlist->allowField(true)->saveAll($goods);
  311. $stockinlist->destroy($delist);
  312. if ($result !== false) {
  313. $this->success();
  314. } else {
  315. $this->error($this->model->getError());
  316. }
  317. } catch (\think\exception\PDOException $e) {
  318. $this->error($e->getMessage());
  319. }
  320. }
  321. $this->error(__('Parameter %s can not be empty', ''));
  322. }
  323. $this->view->assign("row", $row);
  324. return $this->view->fetch();
  325. }
  326. /**
  327. * 查看
  328. */
  329. public function look($ids = null)
  330. {
  331. $row = $this->model->get($ids);
  332. if (!$row) {
  333. $this->error(__('No Results were found'));
  334. }
  335. $operatetype = $this->request->get("operatetype");
  336. if ($operatetype == "audit") {
  337. if ($row->settletime !== null) {
  338. $this->error("该入库单已结算,请先进行反结算!");
  339. }
  340. } else if ($operatetype == "settle") {
  341. if ($row->audittime == null) {
  342. $this->error("该入库单未审核,请先进行审核!");
  343. }
  344. }
  345. $this->view->assign("row", $row);
  346. return $this->view->fetch();
  347. }
  348. /**
  349. * 删除
  350. */
  351. public function del($ids = "")
  352. {
  353. if ($ids) {
  354. $row = $this->model->get($ids);
  355. if (!$row) {
  356. $this->error(__('No Results were found'));
  357. }
  358. $stockinlist = new \app\admin\model\stock\Stockinlist;
  359. $list = collection($stockinlist->where('stock_stockin_id', $row->id)->select())->toArray();
  360. if ($row['audittime']) {
  361. return ($this->error(__('该入库记录已审核,不能删除!')));
  362. }
  363. foreach ($list as $k => $v) {
  364. if ($v['charged']) {
  365. return ($this->error(__('该入库记录存在已出库商品,不能删除!')));
  366. }
  367. }
  368. $result = $stockinlist->where('stock_stockin_id', $row->id)->delete();
  369. $result = $row->delete();
  370. if ($result) {
  371. $this->success();
  372. } else {
  373. $this->error(__('No rows were deleted'));
  374. }
  375. }
  376. $this->error(__('Parameter %s can not be empty', 'ids'));
  377. }
  378. /**
  379. * 查看页,根据单据编号返回商品信息
  380. */
  381. public function info($id = null)
  382. {
  383. if ($this->request->isPost()) {
  384. if ($id) {
  385. try {
  386. $row = $this->model->get($id);
  387. $row->supplier;
  388. $goodslist = \think\Db::view('stock_stockin_list', '*')
  389. ->view('stock_goods', 'id as stock_goods_id,volnum,goodsname,productmodel,measureunit', 'stock_stockin_list.stock_goods_id=stock_goods.id', 'left')
  390. ->where('stock_stockin_list.stock_stockin_id', $id)->select();
  391. return json(["row" => $row, "goodslist" => $goodslist]);
  392. } catch (\think\exception\PDOException $e) {
  393. $this->error($e->getMessage());
  394. }
  395. }
  396. $this->error(__('Parameter %s can not be empty', ''));
  397. }
  398. $this->error('只接受POST请求', '');
  399. }
  400. /**
  401. * 控制台出入库统计
  402. */
  403. public function countData()
  404. {
  405. //当前是否为关联查询
  406. $this->relationSearch = true;
  407. if ($this->request->isAjax()) {
  408. $months = array();
  409. for ($i = 0; $i <= 12; $i++) {
  410. $months[] = date("Y-m", strtotime(date('y-m-01') . '-' . $i . ' months'));
  411. }
  412. $months=array_reverse($months);
  413. $inlist = $this->listmodel
  414. ->with('stockin')
  415. ->field('FROM_UNIXTIME(stockin.createtime,"%Y-%m") month,sum(amount) amount')
  416. ->where('stockin.audittime', 'not null')
  417. ->group('FROM_UNIXTIME(stockin.createtime,"%Y-%m")')
  418. ->select();
  419. $outlistmodel = model('\app\admin\model\stock\Stockoutlist');
  420. $outlist = $outlistmodel
  421. ->with('stockout')
  422. ->field('FROM_UNIXTIME(stockout.createtime,"%Y-%m") month,sum(amount) amount')
  423. ->where('stockout.audittime', 'not null')
  424. ->group('FROM_UNIXTIME(stockout.createtime,"%Y-%m")')
  425. ->select();
  426. $indata = [];
  427. $outdata = [];
  428. foreach ($months as $k => $v) {
  429. $indata[$k] = 0;
  430. $outdata[$k] = 0;
  431. foreach ($inlist as $ink => $inv) {
  432. if ($inv->month == $v) {
  433. $indata[$k] = intval($inv->amount);
  434. }
  435. }
  436. foreach ($outlist as $outk => $outv) {
  437. if ($outv->month == $v) {
  438. $outdata[$k] = intval($outv->amount);
  439. }
  440. }
  441. }
  442. // print_r($indata);die;
  443. return json(["month"=>$months,"indata" => $indata, "outdata" => $outdata]);
  444. }
  445. }
  446. /**
  447. * 打印数据(LODOP)
  448. */
  449. public function printer()
  450. {
  451. return $this->view->fetch();
  452. }
  453. }