OfficeReceive.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. class OfficeReceive extends Model
  7. {
  8. public function advancedStartOfficeReceive($id,$orgId,$userId,$formJson,$extra){
  9. $formJson = json_decode($formJson,true);
  10. $data = [
  11. 'org_id' => $orgId,
  12. 'user_id' => $userId,
  13. 'apply_id' => $id,
  14. 'dep_id' => $extra['depId'],
  15. 'create_time' => date('Y-m-d H:i:s'),
  16. 'status' => 0,
  17. ];
  18. $items = [];
  19. foreach ($formJson as $k=>$v){
  20. if($v['componentName'] == 'ddofficefield'){
  21. foreach ($v['components'] as $key=>$val){
  22. switch ($val['idx']){
  23. case '0':
  24. $items = json_decode($val['values'],true);
  25. break;
  26. }
  27. }
  28. break;
  29. }
  30. }
  31. $amount = 0;
  32. foreach ($items as $k=>$v){
  33. $amount += $v['snums']*$v['price'];
  34. }
  35. $data['amount'] = $amount;
  36. $receiveId = Db::name('office_receive')->insertGetId($data);
  37. // 减商品库存
  38. $sitems = [];
  39. foreach ($items as $k=>$v){
  40. $sitems[] = [
  41. 'receive_id' => $receiveId,
  42. 'items_id' => $v['id'],
  43. 'nums' => $v['snums'],
  44. 'price' => $v['price']
  45. ];
  46. $ret = Db::name('office_items')->where('id',$v['id'])->setDec('last_nums',$v['snums']);
  47. if(!$ret){
  48. return false;
  49. }
  50. }
  51. $count = Db::name('office_receive_items')->insertAll($sitems);
  52. if($count != count($sitems)){
  53. return false;
  54. }
  55. return true;
  56. }
  57. public function advancedEndOfficeReceive($id,$orgId,$userId,$formJson){
  58. $data = [
  59. 'update_time' => date('Y-m-d H:i:s'),
  60. 'status' => 1
  61. ];
  62. $ret = Db::name('office_receive')->where('apply_id',$id)->update($data);
  63. return $ret?true:false;
  64. }
  65. public function advancedDisagreeOfficeReceive($id){
  66. $data = [
  67. 'update_time' => date('Y-m-d H:i:s'),
  68. 'status' => 2
  69. ];
  70. $ret = Db::name('office_receive')->where('apply_id',$id)->update($data);
  71. if(!$ret){
  72. return false;
  73. }
  74. // 拒绝返还库存
  75. $info = Db::name('office_receive')->where('apply_id',$id)->find();
  76. $items = Db::name('office_receive_items')->where('receive_id',$info['id'])->select();
  77. foreach ($items as $k=>$v){
  78. $ret = Db::name('office_items')->where('id',$v['items_id'])->setInc('last_nums',$v['nums']);
  79. if(!$ret){
  80. return false;
  81. }
  82. }
  83. return true;
  84. }
  85. public function lists($page,$size,$title,$items,$startTime,$endTime,$orgId){
  86. $map[] = ['or.org_id','=',$orgId];
  87. $map[] = ['or.status','=',1];
  88. if($title){
  89. $map[] = ['ui.name','like','%'.$title.'%'];
  90. }
  91. if($items){
  92. $receive_ids = Db::name('office_receive_items')
  93. ->alias('ori')
  94. ->join('office_items oi','oi.id = ori.items_id')
  95. ->where('oi.title','like','%'.$items.'%')
  96. ->where('oi.org_id',$orgId)
  97. ->column('ori.receive_id');
  98. $receive_ids = $receive_ids?array_unique($receive_ids):[];
  99. if($receive_ids){
  100. $map[] = ['or.id','in',$receive_ids];
  101. }
  102. }
  103. if($startTime && $endTime){
  104. $map[] = ['or.create_time','>=',$startTime.' 00:00:00'];
  105. $map[] = ['or.create_time','<=',$endTime.' 23:59:59'];
  106. }
  107. $lists = Db::name('office_receive')
  108. ->alias('or')
  109. ->join('user_info ui','ui.user_id = or.user_id')
  110. ->field('or.*,ui.name as user_name')
  111. ->where($map)
  112. ->page($page,$size)
  113. ->order('or.id desc')
  114. ->select();
  115. $lists = $lists?$lists:[];
  116. foreach ($lists as $k=>$v){
  117. $lists[$k]['dep_name'] = Db::name('dep')->where('id',$v['dep_id'])->value('name');
  118. }
  119. $total = Db::name('office_receive')
  120. ->alias('or')
  121. ->join('user_info ui','ui.user_id = or.user_id')
  122. ->where($map)->count();
  123. $data = [
  124. 'total' => $total,
  125. 'list' => $lists
  126. ];
  127. return $data;
  128. }
  129. }