Order.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 getOrderStatus(Project $project, $user, $is_draft)
  35. {
  36. if($is_draft) {
  37. return Option::get('orders', 'status', 'checking');
  38. }
  39. if($project->isTopLevel($user)) {
  40. return Option::get('orders', 'status', 'pass');
  41. }
  42. return Option::get('orders', 'status', 'checking');
  43. }
  44. public function getStatusName()
  45. {
  46. $option = Option::find($this['status']);
  47. if(!$option) return '';
  48. if($option['key'] == 'checking') return $option['name'];
  49. else if(in_array($option['key'], ['checked', 'reject'])) {
  50. $project_role = ProjectRole::find($this['last_project_role_id']);
  51. return $project_role ? $project_role->name . ' - ' . $option['name'] : $option['name'];
  52. } else if($option['key'] == 'pass') {
  53. return '提交人 - 已确定';
  54. }
  55. return '';
  56. }
  57. public function updateMoney()
  58. {
  59. $total = 0;
  60. $devices = OrderDevice::where('order_id', '=', $this['id'])->get();
  61. foreach($devices as $device) {
  62. $total = $total + ($device['price'] * (int)$device['quantity']);
  63. }
  64. $this->update(['money' => $total]);
  65. }
  66. }