MateCheck.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. class MateCheck extends Base
  6. {
  7. protected $createTime = 'create_time';
  8. protected $updateTime = 'update_time';
  9. public $table = 'mate_check';
  10. protected $validateName = 'MateCheck';
  11. public function updates($userId){
  12. $data = request()->post();
  13. $data['org_id'] = cur_org_id();
  14. $result = validate($this->validateName)->check($data,[],'');
  15. if(true !== $result){
  16. $this->error = validate($this->validateName)->getError();
  17. return false;
  18. }
  19. $curTime = date('Y-m-d H:i:s');
  20. Db::startTrans();
  21. try{
  22. $applyId = Db::name('mate_check')->insertGetId([
  23. 'org_id' => $data['org_id'],
  24. 'create_time' => $curTime,
  25. 'user_id' => $userId,
  26. 'remark' => $data['remark'],
  27. 'status' => 0
  28. ]);
  29. if(!$applyId){
  30. \exception('操作失败');
  31. }
  32. $arr = [];
  33. foreach ($data['goods'] as $k=>$v){
  34. $info = Db::name('mate_goods')->where('id',$v)->find();
  35. $arr[] = [
  36. 'check_id' => $applyId,
  37. 'goods_id' => $v,
  38. 'nums' => $info['nums'],
  39. 'price' => $info['price'],
  40. 'status' => 0
  41. ];
  42. }
  43. $ret = Db::name('mate_check_goods')->insertAll($arr);
  44. if($ret != count($arr)){
  45. \exception('操作失败');
  46. }
  47. Db::commit();
  48. }catch (Exception $e){
  49. Db::rollback();
  50. $this->error = $e->getMessage();
  51. return false;
  52. }
  53. return true;
  54. }
  55. public function checkGoods($userId){
  56. $id = input('id/d',0);
  57. $checknums = input('check_nums/d',0);
  58. $remark = input('remark','','trim');
  59. if($checknums < 0){
  60. $this->error = '实盘数量不能小于0';
  61. return false;
  62. }
  63. $info = Db::name('mate_check_goods')->where('id',$id)->where('del',0)->find();
  64. if(!$info){
  65. $this->error = '记录不存在';
  66. return false;
  67. }
  68. if($info['status'] != 0){
  69. $this->error = '该记录已处理';
  70. return false;
  71. }
  72. $status = 2;
  73. if($checknums > $info['nums']){
  74. $status = 3;
  75. }else if($checknums < $info['nums']){
  76. $status = 1;
  77. }
  78. $ret = Db::name('mate_check_goods')->where('id',$id)->update([
  79. 'user_id' => $userId,
  80. 'status' => $status,
  81. 'check_time' => date('Y-m-d H:i:s'),
  82. 'check_nums' => $checknums,
  83. 'remark' => $remark
  84. ]);
  85. if(!$ret){
  86. $this->error = '记录不存在';
  87. return false;
  88. }
  89. return true;
  90. }
  91. public function getInfo($id){
  92. $info = $this
  93. ->where('id',$id)
  94. ->find()
  95. ->toArray();
  96. if(!$info){
  97. return false;
  98. }
  99. $info['userName'] = Db::name('user')->where('id',$info['user_id'])->value('real_name');
  100. $goods = Db::name('mate_check_goods')
  101. ->alias('a')
  102. ->join('mate_goods c','c.id=a.goods_id')
  103. ->where('a.check_id',$info['id'])
  104. ->where('a.del',0)
  105. ->field('a.*,c.title,c.unit,c.brand,c.spec')
  106. ->select();
  107. $goods = $goods?$goods:[];
  108. $tnums = 0;
  109. $tchecknums = 0;
  110. foreach ($goods as $k=>$v){
  111. $tnums += $v['nums'];
  112. $tchecknums += $v['check_nums'];
  113. }
  114. $info['tnums'] = $tnums;
  115. $info['tchecknums'] = $tchecknums;
  116. $info['goods'] = $goods;
  117. return $info;
  118. }
  119. }