Inventory.php 2.7 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,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. $limit = $this->request->post('limit/d', 15); //条数
  49. $time = $this->request->post('create_time/s'); //日期
  50. if(!empty($type)) $where[] = ['a.type', '=', $type];
  51. if(!empty($type_id)) $where[] = ['a.type_id', '=',$type_id];
  52. if(!empty($spec_id)) $where[] = ['a.variety_id', '=',$spec_id];
  53. if(!empty($time)){
  54. $arr = explode(',', $time);
  55. $where[] = ['a.createtime', '>=', strtotime($arr[0])];
  56. $where[] = ['a.createtime', '<=', strtotime($arr[1])];
  57. }
  58. $list = $stockLog::alias('a')
  59. ->leftjoin('stock_config b', 'a.variety_id = b.id')
  60. ->where('a.user_id', $this->userinfo['id'])
  61. ->where($where)
  62. ->field('a.*,b.title')
  63. ->order('a.id desc')
  64. ->paginate($limit);
  65. $this->success('ok', $list);
  66. }
  67. }