0
0

MateApply.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\common\validate;
  3. use think\Db;
  4. use think\Validate;
  5. class MateApply extends Validate{
  6. protected $rule = [
  7. 'type' => 'require|in:1,2',
  8. 'name' => 'checkName',
  9. 'phone' => 'checkPhone',
  10. 'goods' => 'checkGoods'
  11. ];
  12. protected $message = [
  13. 'type.require' => '参数错误',
  14. 'type.in' => '参数错误',
  15. ];
  16. protected $scene = [
  17. ];
  18. protected function checkName($value,$rule,$data=[]){
  19. if($data['type'] == 1){
  20. if(!$data['name']){
  21. return '未填写入库人';
  22. }
  23. }else{
  24. if(!$data['name']){
  25. return '未填写出库人';
  26. }
  27. }
  28. return true;
  29. }
  30. protected function checkPhone($value,$rule,$data=[]){
  31. if(!$data['phone']){
  32. return '未填写联系电话';
  33. }
  34. return true;
  35. }
  36. protected function checkGoods($value,$rule,$data=[]){
  37. if(!isset($data['goods'])||empty($data['goods'])){
  38. return '未选择物品';
  39. }
  40. if($data['type'] == 1){ // 入库单,检查商品是否存在,物品数量大于0,金额大于等于0
  41. foreach ($data['goods'] as $k=>$v){
  42. if((int)$v['nums'] <= 0){
  43. return '物品数量必须大于0';
  44. }
  45. if((int)$v['price'] < 0){
  46. return '物品单价不能小于0';
  47. }
  48. $info = Db::name('mate_goods')
  49. ->where('org_id',$data['org_id'])
  50. ->where('id',$k)
  51. ->where('enable',1)
  52. ->where('del',0)
  53. ->find();
  54. if(!$info){
  55. return '选择的物品不存在';
  56. }
  57. }
  58. }else{ // 出库单
  59. foreach ($data['goods'] as $k=>$v){
  60. $nums = (int)$v['nums'];
  61. if($nums <= 0){
  62. return '物品数量必须大于0';
  63. }
  64. $info = Db::name('mate_goods')
  65. ->where('org_id',$data['org_id'])
  66. ->where('id',$k)
  67. ->where('enable',1)
  68. ->where('del',0)
  69. ->find();
  70. if(!$info){
  71. return '选择的物品不存在';
  72. }
  73. if($info['nums'] < $nums){
  74. return '物品数量大于物品剩余数量';
  75. }
  76. }
  77. }
  78. return true;
  79. }
  80. }