format('Ymdhis'), 2); do { $str = $str . mt_rand(10000, 99999); $res = self::where('order_number', $str)->first(); if(!$res) break; $times = $times - 1; } while($times); return $str; } public function user() { return $this->belongsTo('App\Models\User', 'user_id'); } public function project() { return $this->belongsTo('App\Models\Project', 'project_id'); } public function workPoint() { return $this->belongsTo('App\Models\WorkPoint', 'work_point_id'); } public function devices() { return $this->belongsToMany('App\Models\Device', 'order_devices', 'order_id', 'device_id')->withPivot('name', 'quantity', 'price', 'start_date', 'end_date', 'id'); } public function innerDevices() { return $this->belongsToMany('App\Models\InnerDevice', 'order_devices', 'order_id', 'inner_device_id')->withPivot('id', 'start_date', 'end_date'); } public function getOrderStatus(Project $project, $user, $is_draft) { if($is_draft) { return Option::get('orders', 'status', 'checking'); } if($project->isTopLevel($user)) { return Option::get('orders', 'status', 'pass'); } return Option::get('orders', 'status', 'checking'); } public function getStatusName() { $option = Option::find($this['status']); if(!$option) return ''; if($option['key'] == 'checking') return $option['name']; else if(in_array($option['key'], ['checked', 'reject'])) { $project_role = ProjectRole::find($this['last_project_role_id']); return $project_role ? $project_role->name . ' - ' . $option['name'] : $option['name']; } else if($option['key'] == 'pass') { return '提交人 - 已确定'; } return $option ? $option['name'] : ''; } public function updateMoney() { $total = 0; $devices = OrderDevice::where('order_id', '=', $this['id'])->get(); foreach($devices as $device) { $total = $total + ($device['price'] * (int)$device['quantity']); } $this->update(['money' => $total]); } public function formatOrder($item) { if($item->type == 1) { $item->devices = OrderDevice::where('order_id', $item->id)->get(); foreach($item->devices as $device) { $device->device = Device::find($device->device_id); $device->device_name = DeviceName::find($device->device_name_id); $device->spec = Spec::find($device->spec_id); $device->rent_type = RentType::find($device->rent_type_id); } } else if($item->type == 2) { $item->devices = $item->innerDevices; } $item->user_name = $item->user ? $item->user->name : ''; $item->work_point_name = $item->workPoint ? $item->workPoint->name : ''; $option = Option::find($item->status); $item->status = $item->getStatusName(); $item->status_key = $option ? $option['key'] : ''; $item->color = $option ? $option['color'] : ''; $item->date_time = substr($item->created_at, 0, 16); } }