Order.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. class Order extends BaseModel
  5. {
  6. public static function createOrderNumber()
  7. {
  8. $times = 10;
  9. $str = substr(Carbon::now()->format('Ymdhis'), 2);
  10. do {
  11. $str = $str . mt_rand(10000, 99999);
  12. $res = self::where('order_number', $str)->first();
  13. if(!$res) break;
  14. $times = $times - 1;
  15. } while($times);
  16. return $str;
  17. }
  18. public function user()
  19. {
  20. return $this->belongsTo('App\Models\User', 'user_id');
  21. }
  22. public function project()
  23. {
  24. return $this->belongsTo('App\Models\Project', 'project_id');
  25. }
  26. public function workPoint()
  27. {
  28. return $this->belongsTo('App\Models\WorkPoint', 'work_point_id');
  29. }
  30. public function devices()
  31. {
  32. return $this->belongsToMany('App\Models\Device', 'order_devices', 'order_id', 'device_id')->withPivot('name', 'quantity', 'price', 'start_date', 'end_date', 'id');
  33. }
  34. public function innerDevices()
  35. {
  36. return $this->belongsToMany('App\Models\InnerDevice', 'order_devices', 'order_id', 'inner_device_id')->withPivot('id', 'start_date', 'end_date');
  37. }
  38. public function getOrderStatus(Project $project, $user, $is_draft)
  39. {
  40. if($is_draft) {
  41. return Option::get('orders', 'status', 'checking');
  42. }
  43. if($project->isTopLevel($user)) {
  44. return Option::get('orders', 'status', 'pass');
  45. }
  46. return Option::get('orders', 'status', 'checking');
  47. }
  48. public function getStatusName()
  49. {
  50. $option = Option::find($this['status']);
  51. if(!$option) return '';
  52. if($option['key'] == 'checking') return $option['name'];
  53. else if(in_array($option['key'], ['checked', 'reject'])) {
  54. $project_role = ProjectRole::find($this['last_project_role_id']);
  55. return $project_role ? $project_role->name . ' - ' . $option['name'] : $option['name'];
  56. } else if($option['key'] == 'pass') {
  57. return '提交人 - 已确定';
  58. }
  59. return $option ? $option['name'] : '';
  60. }
  61. public function updateMoney()
  62. {
  63. $total = 0;
  64. $devices = OrderDevice::where('order_id', '=', $this['id'])->get();
  65. foreach($devices as $device) {
  66. $total = $total + ($device['price'] * (int)$device['quantity']);
  67. }
  68. $this->update(['money' => $total]);
  69. }
  70. public function formatOrder($item)
  71. {
  72. if($item->type == 1) {
  73. $item->devices = OrderDevice::where('order_id', $item->id)->get();
  74. foreach($item->devices as $device) {
  75. $device->device = Device::find($device->device_id);
  76. $device->device_name = DeviceName::find($device->device_name_id);
  77. $device->spec = Spec::find($device->spec_id);
  78. $device->rent_type = RentType::find($device->rent_type_id);
  79. }
  80. } else if($item->type == 2) {
  81. $item->devices = $item->innerDevices;
  82. }
  83. $item->user_name = $item->user ? $item->user->name : '';
  84. $item->work_point_name = $item->workPoint ? $item->workPoint->name : '';
  85. $option = Option::find($item->status);
  86. $item->status = $item->getStatusName();
  87. $item->status_key = $option ? $option['key'] : '';
  88. $item->color = $option ? $option['color'] : '';
  89. $item->date_time = substr($item->created_at, 0, 16);
  90. }
  91. }