Order.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. }