Order.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 '已完成';
  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, 'end_date' => Carbon::now()->toDateString()]);
  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. public function updateOrderDevices($devices)
  136. {
  137. OrderDevice::where('order_id', '=', $this['id'])->delete();
  138. if($this['type'] == 1) {
  139. $total = 0;
  140. foreach($devices as $device) {
  141. $price = $this->transMoney($device['price']);
  142. $data = $this->getNameSpecRent($device);
  143. OrderDevice::create(array_merge([
  144. 'order_id' => $this['id'],
  145. 'project_id' => $this['project_id'],
  146. 'quantity' => $device['quantity'],
  147. 'price' => $price,
  148. 'start_date' => $device['start_date'],
  149. 'end_date' => $device['end_date']
  150. ], $data));
  151. $total = $total + ($price * (int)$device['quantity']);
  152. }
  153. $this->update(['money' => $total]);
  154. } else {
  155. foreach($devices as $device) {
  156. OrderDevice::create([
  157. 'name' => $device['name'],
  158. 'order_id' => $this['id'],
  159. 'device_id' => $device['id'],
  160. 'start_date' => $device['start_date'],
  161. 'end_date' => $device['end_date']
  162. ]);
  163. if($device['id']) {
  164. InnerDevice::find($device['id'])->update([
  165. 'start_date' => $device['start_date'],
  166. 'end_date' => $device['end_date'],
  167. 'work_point_id' => $this['work_point_id']
  168. ]);
  169. }
  170. }
  171. }
  172. }
  173. public function getNameSpecRent($data)
  174. {
  175. $device = Device::find($data['type_id']);
  176. $rent_type = RentType::firstOrCreate([
  177. 'name' => $data['rent']
  178. ]);
  179. if(!$device) return [
  180. 'device_id' => '',
  181. 'device_name_id' => '',
  182. 'spec_id' => '',
  183. 'rent_type_id' => $rent_type->id
  184. ];
  185. $device_name = DeviceName::firstOrCreate([
  186. 'device_id' => $device->id,
  187. 'name' => $data['name']
  188. ]);
  189. $spec = Spec::firstOrCreate([
  190. 'device_id' => $device->id,
  191. 'device_name_id' => $device_name->id,
  192. 'name' => $data['spec']
  193. ]);
  194. return [
  195. 'device_id' => $device->id,
  196. 'device_name_id' => $device_name->id,
  197. 'spec_id' => $spec->id,
  198. 'rent_type_id' => $rent_type->id
  199. ];
  200. }
  201. /**
  202. * @param Request $request
  203. * @return int(1改变2不改变)
  204. */
  205. public function checkIsChange(Request $request)
  206. {
  207. if($this['type'] == 1) return 2;
  208. $devices = $request->input('devices');
  209. }
  210. public function order_devices()
  211. {
  212. return $this->hasMany('App\Models\OrderDevice', 'order_id');
  213. }
  214. }