123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace app\common\validate;
- use think\Db;
- use think\Validate;
- class MateApply extends Validate{
- protected $rule = [
- 'type' => 'require|in:1,2',
- 'name' => 'checkName',
- 'phone' => 'checkPhone',
- 'goods' => 'checkGoods'
- ];
- protected $message = [
- 'type.require' => '参数错误',
- 'type.in' => '参数错误',
- ];
- protected $scene = [
- ];
- protected function checkName($value,$rule,$data=[]){
- if($data['type'] == 1){
- if(!$data['name']){
- return '未填写入库人';
- }
- }else{
- if(!$data['name']){
- return '未填写出库人';
- }
- }
- return true;
- }
- protected function checkPhone($value,$rule,$data=[]){
- if(!$data['phone']){
- return '未填写联系电话';
- }
- return true;
- }
- protected function checkGoods($value,$rule,$data=[]){
- if(!isset($data['goods'])||empty($data['goods'])){
- return '未选择物品';
- }
- if($data['type'] == 1){ // 入库单,检查商品是否存在,物品数量大于0,金额大于等于0
- foreach ($data['goods'] as $k=>$v){
- if((int)$v['nums'] <= 0){
- return '物品数量必须大于0';
- }
- if((int)$v['price'] < 0){
- return '物品单价不能小于0';
- }
- $info = Db::name('mate_goods')
- ->where('org_id',$data['org_id'])
- ->where('id',$k)
- ->where('enable',1)
- ->where('del',0)
- ->find();
- if(!$info){
- return '选择的物品不存在';
- }
- }
- }else{ // 出库单
- foreach ($data['goods'] as $k=>$v){
- $nums = (int)$v['nums'];
- if($nums <= 0){
- return '物品数量必须大于0';
- }
- $info = Db::name('mate_goods')
- ->where('org_id',$data['org_id'])
- ->where('id',$k)
- ->where('enable',1)
- ->where('del',0)
- ->find();
- if(!$info){
- return '选择的物品不存在';
- }
- if($info['nums'] < $nums){
- return '物品数量大于物品剩余数量';
- }
- }
- }
- return true;
- }
- }
|