Inventory.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\StockConfig;
  4. use app\api\service\StockService;
  5. use app\common\model\StockLog;
  6. use think\exception\ValidateException;
  7. use app\api\validate\Stock as StockValidate;
  8. use think\facade\Db;
  9. class Inventory extends Base
  10. {
  11. //品种包装箱/出入库列表
  12. public function stockList(StockConfig $stockConfig)
  13. {
  14. $type_id = $this->request->post('type_id', 'variety_name');
  15. if(!in_array($type_id, [$stockConfig::VarietyName, $stockConfig::PackingBox, $stockConfig::Material])) $this->error(__('参数错误'));
  16. $list = $stockConfig::where('type_id', $type_id)->field('id,title,field_name,unit')->select();
  17. $this->success('ok', $list);
  18. }
  19. //添加出入库
  20. public function stock(StockLog $stockLog, StockService $stockService)
  21. {
  22. $data = $this->request->post();
  23. $result = false;
  24. Db::startTrans();
  25. try {
  26. validate(StockValidate::class)->scene('add')->check($data);
  27. $resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
  28. $result = $stockLog->saveAll($resData);
  29. Db::commit();
  30. }catch (ValidateException $e) {
  31. return $this->error($e->getError());
  32. } catch (\Exception $e) {
  33. Db::rollback();
  34. $this->error($e->getMessage());
  35. }
  36. if ($result === false) {
  37. $this->error(__('没有新增任何数据'));
  38. }
  39. $this->success();
  40. }
  41. //出入库记录
  42. public function stocklog(StockLog $stockLog)
  43. {
  44. $where = [];
  45. $type_id = $this->request->post('type_id/s', '');
  46. if(!empty($type_id)) $where['a.type_id'] = $type_id;
  47. $list = $stockLog::alias('a')
  48. ->leftjoin('stock_config b', 'a.variety_id = b.id')
  49. ->where('a.user_id', $this->userinfo['id'])
  50. ->where($where)
  51. ->whereTime('a.createtime', '-2 days')
  52. ->field('a.*,b.title')
  53. ->select();
  54. $this->success('ok', $list);
  55. }
  56. //编辑出入库
  57. public function stockedit(StockLog $stockLog)
  58. {
  59. $data = $this->request->post();
  60. $result = false;
  61. Db::startTrans();
  62. try {
  63. validate(StockValidate::class)->scene('add')->check($data);
  64. $resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
  65. $result = $stockLog->saveAll($resData);
  66. Db::commit();
  67. }catch (ValidateException $e) {
  68. return $this->error($e->getError());
  69. } catch (\Exception $e) {
  70. Db::rollback();
  71. $this->error($e->getMessage());
  72. }
  73. if ($result === false) {
  74. $this->error(__('没有新增任何数据'));
  75. }
  76. $this->success();
  77. }
  78. //删除出入库
  79. public function stockdel(StockLog $stockLog)
  80. {
  81. $data = $this->request->post();
  82. $result = false;
  83. Db::startTrans();
  84. try {
  85. validate(StockValidate::class)->scene('add')->check($data);
  86. $resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
  87. $result = $stockLog->saveAll($resData);
  88. Db::commit();
  89. }catch (ValidateException $e) {
  90. return $this->error($e->getError());
  91. } catch (\Exception $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. }
  95. if ($result === false) {
  96. $this->error(__('没有新增任何数据'));
  97. }
  98. $this->success();
  99. }
  100. }