Order.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Illuminate\Support\Facades\Request;
  5. class Order extends BaseModel
  6. {
  7. public static function createOrderNumber()
  8. {
  9. $times = 10;
  10. $str = substr(Carbon::now()->format('Ymdhis'), 2);
  11. do {
  12. $str = $str . mt_rand(10000, 99999);
  13. $res = self::where('order_number', $str)->first();
  14. if(!$res) break;
  15. $times = $times - 1;
  16. } while($times);
  17. return $str;
  18. }
  19. public function user()
  20. {
  21. return $this->belongsTo('App\Models\User', 'user_id');
  22. }
  23. public function project()
  24. {
  25. return $this->belongsTo('App\Models\Project', 'project_id');
  26. }
  27. public function workPoint()
  28. {
  29. return $this->belongsTo('App\Models\WorkPoint', 'work_point_id');
  30. }
  31. public function devices()
  32. {
  33. return $this->belongsToMany('App\Models\Device', 'order_devices', 'order_id', 'device_id')->withPivot('name', 'quantity', 'price', 'start_date', 'end_date', 'id');
  34. }
  35. public function innerDevices()
  36. {
  37. return $this->belongsToMany('App\Models\InnerDevice', 'order_devices', 'order_id', 'inner_device_id')->withPivot('id', 'start_date', 'end_date');
  38. }
  39. public function getOrderStatus(Project $project, $user, $is_draft)
  40. {
  41. if($is_draft) {
  42. return Option::get('orders', 'status', 'checking');
  43. }
  44. if($project->isTopLevel($user)) {
  45. return Option::get('orders', 'status', 'pass');
  46. }
  47. return Option::get('orders', 'status', 'checking');
  48. }
  49. public function getStatusName()
  50. {
  51. $option = Option::find($this['status']);
  52. if(!$option) return '';
  53. if($option['key'] == 'checking') return $option['name'];
  54. else if(in_array($option['key'], ['checked', 'reject'])) {
  55. $project_role = ProjectRole::find($this['last_project_role_id']);
  56. return $project_role ? $project_role->name . ' - ' . $option['name'] : $option['name'];
  57. } else if($option['key'] == 'pass') {
  58. if($this['is_change'] == 1) return '管理员 - 已修订';
  59. return $this['type'] == 1 ? '提交人 - 已确定' : '已完成';
  60. }
  61. return $option ? $option['name'] : '';
  62. }
  63. public function updateMoney()
  64. {
  65. $total = 0;
  66. $devices = OrderDevice::where('order_id', '=', $this['id'])->get();
  67. foreach($devices as $device) {
  68. $total = $total + ($device['price'] * (int)$device['quantity']);
  69. }
  70. $this->update(['money' => $total]);
  71. }
  72. public function formatOrder($item)
  73. {
  74. if($item->type == 1) {
  75. $item->devices = OrderDevice::where('order_id', $item->id)->get();
  76. foreach($item->devices as $device) {
  77. $device->device = Device::find($device->device_id);
  78. $device->device_name = DeviceName::find($device->device_name_id);
  79. $device->spec = Spec::find($device->spec_id);
  80. $device->rent_type = RentType::find($device->rent_type_id);
  81. }
  82. } else if($item->type == 2) {
  83. $item->devices = $item->innerDevices;
  84. }
  85. $item->user_name = $item->user ? $item->user->name : '';
  86. $item->work_point_name = $item->workPoint ? $item->workPoint->name : '';
  87. $option = Option::find($item->status);
  88. $item->status = $item->getStatusName();
  89. $item->status_key = $option ? $option['key'] : '';
  90. $item->color = $option ? $option['color'] : '';
  91. $item->date_time = substr($item->created_at, 0, 16);
  92. }
  93. public function backInnerDevices($devices)
  94. {
  95. $back_devices = [];
  96. $back_device_ids = [];
  97. foreach ($devices as $device) {
  98. if(isset($device['checked']) && $device['checked']) {
  99. array_push($back_devices, $device);
  100. array_push($back_device_ids, $device['id']);
  101. }
  102. }
  103. $new_order = null;
  104. if(count($back_devices) < count($devices)) {
  105. $status = Option::get('orders', 'status', 'pass', 'id');
  106. $last_role = ProjectRole::getLastRole($this);
  107. $level = $last_role ? $last_role['level'] : '';
  108. $new_order = Order::create([
  109. 'work_point_id' => $this['work_point_id'],
  110. 'remark' => $this['remark'],
  111. 'money' => $this['money'],
  112. 'is_draft' => $this['is_draft'],
  113. 'status' => $status,
  114. 'order_number' => self::createOrderNumber(),
  115. 'project_id' => $this['project_id'],
  116. 'user_id' => $this['user_id'],
  117. 'project_role_id' => $this['project_role_id'],
  118. 'level' => $level,
  119. 'last_project_role_id' => $this['last_project_role_id'],
  120. 'last_user_id' => $this['last_user_id'],
  121. 'is_change' => $this['is_change'],
  122. 'type' => $this['type']
  123. ]);
  124. OrderDevice::where('order_id', $this['id'])->whereIn('inner_device_id', $back_device_ids)->update(['order_id' => $new_order->id]);
  125. }
  126. $free_id = Option::get('inner_devices', 'status', 'free');
  127. InnerDevice::whereIn('id', $back_device_ids)->update([
  128. 'status' => $free_id,
  129. 'start_date' => null,
  130. 'end_date' => null,
  131. 'work_point_id' => ''
  132. ]);
  133. return $new_order;
  134. }
  135. /**
  136. * @param Request $request
  137. * @return int(1改变2不改变)
  138. */
  139. public function checkIsChange(Request $request)
  140. {
  141. if($this['type'] == 1) return 2;
  142. $devices = $request->input('devices');
  143. }
  144. public function order_devices()
  145. {
  146. return $this->hasMany('App\Models\OrderDevice', 'order_id');
  147. }
  148. }