Inventory.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 = $this->request->post('type/s', '');
  46. $type_id = $this->request->post('type_id/s', '');
  47. $spec_id = $this->request->post('spec/s', '');
  48. if(!empty($type)) $where['a.type'] = $type;
  49. if(!empty($type_id)) $where['a.type_id'] = $type_id;
  50. if(!empty($spec_id)) $where['b.title'] = ['like', '%'.$spec_id.'%'];
  51. $list = $stockLog::alias('a')
  52. ->leftjoin('stock_config b', 'a.variety_id = b.id')
  53. ->where('a.user_id', $this->userinfo['id'])
  54. ->where($where)
  55. ->whereTime('a.createtime', '-2 days')
  56. ->field('a.*,b.title')
  57. ->select();
  58. $this->success('ok', $list);
  59. }
  60. }