Inventory.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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')->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 index(StockLog $stockLog)
  58. {
  59. //分组统计品种
  60. $data= $stockLog->where('user_id', $this->userinfo['id'])
  61. ->field('type,count(id) as count')
  62. ->group('type')
  63. ->select();
  64. $this->success('ok', $data);
  65. }
  66. }