MateGoodsUse.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class MateGoodsUse extends Base
  5. {
  6. // 添加消耗使用记录,不添加事务,使用逻辑中的事务
  7. public function save1($type,$busId,$userId,$orgId,$goods,$remark=''){ //[{"goodsId":1,"nums","price":0}]
  8. if(!in_array($type,[1,2,3,4])){
  9. $this->error = '类型错误';
  10. return false;
  11. }
  12. try{
  13. foreach ($goods as $k=>$v){
  14. if($type == 1||$type == 2){ // 使用小包中的耗材,检查库存的量是否充足,并减去相应库存
  15. $ginfo = Db::name('mate_user_goods')
  16. ->where('user_id',$userId)
  17. ->where('org_id',$orgId)
  18. ->where('goods_id',$v['goodsId'])
  19. ->find();
  20. if(!$ginfo){
  21. \exception('物品不存在');
  22. }
  23. if($ginfo['nums'] < $v['nums']){
  24. \exception('库存不足');
  25. }
  26. $mug = Db::name('mate_user_goods')
  27. ->where('id',$ginfo['id'])
  28. ->setDec('nums',$v['nums']);
  29. if(!$mug){
  30. \exception('操作失败');
  31. }
  32. $data = [
  33. 'org_id' => $orgId,
  34. 'user_id' => $userId,
  35. 'bus_id' => $busId,
  36. 'goods_id' => $v['goodsId'],
  37. 'type' => $type,
  38. 'nums' => $v['nums'],
  39. 'price' => $ginfo['price'],
  40. 'total_price' => round($ginfo['price']*$v['nums'],2),
  41. 'remark' => $remark,
  42. 'create_time' => date('Y-m-d H:i:s')
  43. ];
  44. $addUse = Db::name('mate_goods_use')->insert($data);
  45. if(!$addUse){
  46. \exception('操作失败');
  47. }
  48. }else{ // 领取消耗/损坏消耗
  49. $data = [
  50. 'org_id' => $orgId,
  51. 'user_id' => $userId,
  52. 'bus_id' => $busId,
  53. 'type' => $type,
  54. 'nums' => $v['nums'],
  55. 'price' => $v['price'],
  56. 'total_price' => round($v['price']*$v['nums'],2),
  57. 'remark' => $remark,
  58. 'create_time' => date('Y-m-d H:i:s')
  59. ];
  60. $add = Db::name('mate_goods_use')->insert($data);
  61. if(!$add){
  62. \exception('操作失败');
  63. }
  64. }
  65. }
  66. }catch (\Exception $e){
  67. $this->error = $e->getMessage();
  68. return false;
  69. }
  70. return true;
  71. }
  72. // 根据类型和业务id获取使用耗材记录
  73. public function lists($type,$busId,$orgId){
  74. $lists = Db::name('mate_goods_use')
  75. ->field('goods_id,nums as total,price as money')
  76. ->where('type',$type)
  77. ->where('bus_id',$busId)
  78. ->where('org_id',$orgId)
  79. ->select();
  80. foreach ($lists as $k=>$v){
  81. $lists[$k]['title'] = Db::name('mate_goods')->where('id',$v['goods_id'])->value('title');
  82. }
  83. return $lists;
  84. }
  85. }