Inventory.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\StockConfig;
  4. use app\api\service\auth\ApiAuthService;
  5. use app\common\model\ProductConfig;
  6. use think\exception\ValidateException;
  7. use think\facade\Db;
  8. class Inventory extends Base
  9. {
  10. //品种包装箱/出入库列表
  11. public function stockList(StockConfig $stockConfig)
  12. {
  13. $type_id = $this->request->post('type_id', 'variety_name');
  14. if(!in_array($type_id, [$stockConfig::VarietyName, $stockConfig::PackingBox, $stockConfig::Material])) $this->error(__('参数错误'));
  15. $list = $stockConfig::where('type_id', $type_id)->field('id,title,field_name')->select();
  16. $this->success('ok', $list);
  17. }
  18. //添加出入库
  19. public function stock(ApiAuthService $authService)
  20. {
  21. $data = $this->request->post();
  22. $result = false;
  23. Db::startTrans();
  24. try {
  25. $where['user_id'] = $this->userinfo['id'];
  26. //join
  27. $data['variety'] = json_encode($data['variety'], JSON_UNESCAPED_UNICODE);
  28. $result = $userEnterLog::create($data);
  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(ApiAuthService $authService)
  43. {
  44. $data = $this->request->post();
  45. $result = false;
  46. Db::startTrans();
  47. try {
  48. validate(ShopValidate::class)->scene('add')->check($data);
  49. $data['user_id'] = $this->userinfo['id'];
  50. $data['variety'] = json_encode($data['variety'], JSON_UNESCAPED_UNICODE);
  51. $result = $userEnterLog::create($data);
  52. Db::commit();
  53. }catch (ValidateException $e) {
  54. return $this->error($e->getError());
  55. } catch (\Exception $e) {
  56. Db::rollback();
  57. $this->error($e->getMessage());
  58. }
  59. if ($result === false) {
  60. $this->error(__('没有新增任何数据'));
  61. }
  62. $this->success();
  63. }
  64. }