1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?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 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 '';
- }
- 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]);
- }
- }
|