| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 | <?phpnamespace App\Service;use App\Model\BookingList;use App\Model\BookingListInfo;use App\Model\Employee;use App\Model\SalesOrder;use Illuminate\Support\Facades\DB;class BookingListService extends Service{    public function customerEdit($data,$user){        list($status,$msg) = $this->customerRule($data,$user, false);        if(!$status) return [$status,$msg];        try {            DB::beginTransaction();            $model = BookingList::where('id',$data['id'])->first();            $model->type = $data['type'];            $model->mark = $data['mark'] ?? '';            $model->amount = $data['amount'] ?? 0;            $model->save();            $time = time();            BookingListInfo::where('del_time',0)                ->where('booking_list_id',$data['id'])                ->update('del_time',$time);            if(! empty($data['file'])){                $insert = [];                foreach ($data['file'] as $value){                    $insert[] = [                        'booking_list_id' => $model->id,                        'file' => $value['url'],                        'type' => BookingListInfo::type_one,                        'name' => $value['name'],                        'crt_time' => $time,                    ];                }                BookingListInfo::insert($insert);            }            DB::commit();        }catch (\Exception $exception){            DB::rollBack();            return [false,$exception->getMessage()];        }        return [true,''];    }    public function customerAdd($data,$user){        list($status,$msg) = $this->customerRule($data,$user);        if(!$status) return [$status,$msg];        try {            DB::beginTransaction();            $model = new BookingList();            $model->data_id = $data['data_id'];            $model->data_type = $data['data_type'] ?? 0;            $model->type = $data['type'] ?? 0;            $model->amount = $data['amount'] ?? 0;            $model->mark = $data['mark'] ?? '';            $model->crt_id = $user['id'];            $model->save();            if(! empty($data['file'])){                $insert = [];                foreach ($data['file'] as $value){                    $insert[] = [                        'booking_list_id' => $model->id,                        'file' => $value['url'],                        'type' => BookingListInfo::type_one,                        'name' => $value['name'],                        'crt_time' => time(),                    ];                }                BookingListInfo::insert($insert);            }            DB::commit();        }catch (\Exception $exception){            DB::rollBack();            return [false,$exception->getMessage()];        }        return [true,''];    }    public function customerDel($data){        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];        $booking = BookingList::where('del_time',0)->where('id',$data['id'])->first();        if(empty($booking)) return [false,'记录不存在或已被删除'];        $booking = $booking->toArray();        if($booking['state'] != BookingList::STATE_ZERO) return [false,'请确认记录状态,删除失败'];        try {            DB::beginTransaction();            $time = time();            BookingList::where('id',$data['id'])->update([                'del_time'=> $time            ]);            BookingListInfo::where('del_time',0)                ->where('booking_list_id',$data['id'])                ->update('del_time',$time);            DB::commit();        }catch (\Exception $exception){            DB::rollBack();            return [false,$exception->getMessage()];        }        return [true,''];    }    public function customerDetail($data){        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];        $customer = BookingList::where('del_time',0)            ->where('id',$data['id'])            ->first();        if(empty($customer)) return [false,'记录不存在或已被删除'];        $customer = $customer->toArray();        $customer['state_title'] = BookingList::$name[$customer['state']] ?? "";        $customer['type'] = BookingList::$model_type[$customer['type']] ?? "";        $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');        $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';        $file = BookingListInfo::where('del_time',0)            ->where('booking_list_id',$data['id'])            ->get()->toArray();        $customer['file'] = [];        foreach ($file as $value){            if($value['type'] == BookingListInfo::type_one){                $tmp = [                    'url' => $value['file'],                    'name' => $value['name'],                ];                $customer['file'][] = $tmp;            }        }        return [true, $customer];    }    public function customerList($data,$user){        $model = BookingList::where('del_time',0)            ->select('id','data_id','data_type','type','amount','crt_id','crt_time','state','mark')            ->orderby('id', 'asc');        if(! empty($data['data_id'])) $model->where('data_id', $data['data_id']);        if(! empty($data['data_type'])) $model->where('data_type',$data['data_type']);        $list = $this->limit($model,'',$data);        $list = $this->fillData($list);        return [true, $list];    }    public function customerRule(&$data, $user, $is_add = true){        if(empty($data['data_id'])) return [false,'数据ID不能为空'];        if(empty($data['data_type'])) return [false,'数据类型不能为空'];        if(empty($data['type'])) return [false,'金额类型不能为空'];        if(empty($data['amount'])) return [false,'金额不能为空'];        $res = $this->checkNumber($data['amount']);        if(! $res) return [false, '金额请输入不超过两位小数并且大于0的数值'];        if($data['data_type'] == BookingList::data_type_one){            $bool = SalesOrder::where('id',$data['data_id'])->where('state',SalesOrder::State2_zero)->exists();            if($bool) return [false,'合同还未指派,添加金额记录失败'];        }        if($is_add){        }else{            if(empty($data['id'])) return [false,'ID不能为空'];            $booking = BookingList::where('del_time',0)->where('id',$data['id'])->first();            if(empty($booking)) return [false,'记录不存在或已被删除'];            $booking = $booking->toArray();            if($booking['state'] != BookingList::STATE_ZERO) return [false,'请确认记录状态,编辑失败'];        }        return [true, ''];    }    public function fillData($data){        if(empty($data['data'])) return $data;        $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))            ->pluck('emp_name','id')            ->toArray();        foreach ($data['data'] as $key => $value){            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';            $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';            $data['data'][$key]['state_title'] = BookingList::$name[$value['state']] ?? '';        }        return $data;    }    public function customerConfirm($data){        if(empty($data['id'])) return [false,'记录ID不能为空'];        $customer = BookingList::where('del_time',0)            ->where('id',$data['id'])            ->first();        if(empty($customer)) return [false,'记录不存在或已被删除'];        $customer = $customer->toArray();        if($customer['state'] != BookingList::STATE_ZERO) return [false,'请确认记录状态,确认金额失败'];        BookingList::where('id',$data['id'])->update(['state' => BookingList::STATE_ONE]);        return [true,''];    }    public function getAllAmount($data_id = [], $data_type = 0){        if(empty($data_id) || empty($data_type)) return [];        $booking = BookingList::where('del_time',0)            ->where('state',BookingList::STATE_ONE)            ->whereIn('data_id',$data_id)            ->where('data_type',$data_type)            ->select('data_id','amount');        $return = [];        foreach ($booking as $value){            if(isset($return[$value['data_id']][$value['data_type']])){                $return[$value['data_id']][$value['data_type']] += $value['amount'];            }else{                $return[$value['data_id']][$value['data_type']] = $value['amount'];            }        }        return $return;    }}
 |