12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace App\Models;
- use Carbon\Carbon;
- class Order extends BaseModel
- {
- public static function createOrderNumber()
- {
- $times = 10;
- $str = substr(Carbon::now()->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]);
- }
- }
|