Order.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 workpoints()
  32. {
  33. return $this->hasOne(WorkPoint::class, 'id','work_point_id');
  34. }
  35. public function devices()
  36. {
  37. return $this->belongsToMany('App\Models\Device', 'order_devices', 'order_id', 'device_id')->withPivot('name', 'quantity', 'price', 'start_date', 'end_date', 'id');
  38. }
  39. public function innerDevices()
  40. {
  41. return $this->belongsToMany('App\Models\InnerDevice', 'order_devices', 'order_id', 'inner_device_id')->withPivot('id', 'start_date', 'end_date');
  42. }
  43. public function getOrderStatus(Project $project, $user, $is_draft)
  44. {
  45. if($is_draft) {
  46. return Option::get('orders', 'status', 'checking');
  47. }
  48. if($project->isTopLevel($user)) {
  49. return Option::get('orders', 'status', 'pass');
  50. }
  51. return Option::get('orders', 'status', 'checking');
  52. }
  53. public function getStatusName()
  54. {
  55. $option = Option::find($this['status']);
  56. if(!$option) return '';
  57. if($option['key'] == 'checking') return $option['name'];
  58. else if(in_array($option['key'], ['checked', 'reject'])) {
  59. $project_role = ProjectRole::find($this['last_project_role_id']);
  60. return $project_role ? $project_role->name . ' - ' . $option['name'] : $option['name'];
  61. } else if($option['key'] == 'pass') {
  62. if($this['is_change'] == 1) return '管理员 - 已修订';
  63. return '已完成';
  64. }
  65. return $option ? $option['name'] : '';
  66. }
  67. public function updateMoney()
  68. {
  69. $total = 0;
  70. $devices = OrderDevice::where('order_id', '=', $this['id'])->get();
  71. foreach($devices as $device) {
  72. $total = $total + ($device['price'] * (int)$device['quantity']);
  73. }
  74. $this->update(['money' => $total]);
  75. }
  76. public function formatOrder($item)
  77. {
  78. if($item->type == 1) {
  79. $item->devices = OrderDevice::where('order_id', $item->id)->get();
  80. foreach($item->devices as $device) {
  81. $device->device = Device::find($device->device_id);
  82. $device->device_name = DeviceName::find($device->device_name_id);
  83. $device->spec = Spec::find($device->spec_id);
  84. $device->rent_type = RentType::find($device->rent_type_id);
  85. }
  86. } else if($item->type == 2) {
  87. $item->devices = $item->innerDevices;
  88. }
  89. $item->user_name = $item->user ? $item->user->name : '';
  90. $item->work_point_name = $item->workPoint ? $item->workPoint->name : '';
  91. $option = Option::find($item->status);
  92. $item->status = $item->getStatusName();
  93. $item->status_key = $option ? $option['key'] : '';
  94. $item->color = $option ? $option['color'] : '';
  95. $item->date_time = substr($item->created_at, 0, 16);
  96. }
  97. public function backInnerDevices($devices)
  98. {
  99. $back_devices = [];
  100. $back_device_ids = [];
  101. foreach ($devices as $device) {
  102. if(isset($device['checked']) && $device['checked']) {
  103. array_push($back_devices, $device);
  104. array_push($back_device_ids, $device['id']);
  105. }
  106. }
  107. $new_order = null;
  108. if(count($back_devices) < count($devices)) {
  109. $status = Option::get('orders', 'status', 'pass', 'id');
  110. $last_role = ProjectRole::getLastRole($this);
  111. $level = $last_role ? $last_role['level'] : '';
  112. $new_order = Order::create([
  113. 'work_point_id' => $this['work_point_id'],
  114. 'remark' => $this['remark'],
  115. 'money' => $this['money'],
  116. 'is_draft' => $this['is_draft'],
  117. 'status' => $status,
  118. 'order_number' => self::createOrderNumber(),
  119. 'project_id' => $this['project_id'],
  120. 'user_id' => $this['user_id'],
  121. 'project_role_id' => $this['project_role_id'],
  122. 'level' => $level,
  123. 'last_project_role_id' => $this['last_project_role_id'],
  124. 'last_user_id' => $this['last_user_id'],
  125. 'is_change' => $this['is_change'],
  126. 'type' => $this['type']
  127. ]);
  128. OrderDevice::where('order_id', $this['id'])->whereIn('inner_device_id', $back_device_ids)->update(['order_id' => $new_order->id, 'end_date' => Carbon::now()->toDateString()]);
  129. }
  130. $free_id = Option::get('inner_devices', 'status', 'free');
  131. InnerDevice::whereIn('id', $back_device_ids)->update([
  132. 'status' => $free_id,
  133. 'start_date' => null,
  134. 'end_date' => null,
  135. 'work_point_id' => ''
  136. ]);
  137. return $new_order;
  138. }
  139. public function updateOrderDevices($devices)
  140. {
  141. OrderDevice::where('order_id', '=', $this['id'])->delete();
  142. if($this['type'] == 1) {
  143. $total = 0;
  144. foreach($devices as $device) {
  145. $price = $this->transMoney($device['price']);
  146. $data = $this->getNameSpecRent($device);
  147. OrderDevice::create(array_merge([
  148. 'order_id' => $this['id'],
  149. 'project_id' => $this['project_id'],
  150. 'quantity' => $device['quantity'],
  151. 'price' => $price,
  152. 'start_date' => $device['start_date'],
  153. 'end_date' => $device['end_date']
  154. ], $data));
  155. $total = $total + ($price * (int)$device['quantity']);
  156. }
  157. $this->update(['money' => $total]);
  158. } else {
  159. $using_id = Option::get('inner_devices', 'status', 'using');
  160. foreach($devices as $device) {
  161. OrderDevice::create([
  162. 'name' => $device['name'],
  163. 'order_id' => $this['id'],
  164. 'project_id' => $this['project_id'],
  165. 'user_id' => $this['user_id'],
  166. 'inner_device_id' => $device['id'],
  167. 'start_date' => $device['start_date'],
  168. 'end_date' => $device['end_date']
  169. ]);
  170. if($device['id']) {
  171. InnerDevice::find($device['id'])->update([
  172. 'project_id' => $this['project_id'],
  173. 'status' => $using_id,
  174. 'start_date' => $device['start_date'],
  175. 'end_date' => $device['end_date'],
  176. 'work_point_id' => $this['work_point_id']
  177. ]);
  178. }
  179. }
  180. }
  181. }
  182. public function getNameSpecRent($data)
  183. {
  184. $device = Device::find($data['type_id']);
  185. $rent_type = RentType::firstOrCreate([
  186. 'name' => $data['rent']
  187. ]);
  188. if(!$device) return [
  189. 'device_id' => '',
  190. 'device_name_id' => '',
  191. 'spec_id' => '',
  192. 'rent_type_id' => $rent_type->id
  193. ];
  194. $device_name = DeviceName::firstOrCreate([
  195. 'device_id' => $device->id,
  196. 'name' => $data['name']
  197. ]);
  198. $spec = Spec::firstOrCreate([
  199. 'device_id' => $device->id,
  200. 'device_name_id' => $device_name->id,
  201. 'name' => $data['spec']
  202. ]);
  203. return [
  204. 'device_id' => $device->id,
  205. 'device_name_id' => $device_name->id,
  206. 'spec_id' => $spec->id,
  207. 'rent_type_id' => $rent_type->id
  208. ];
  209. }
  210. /**
  211. * @param Request $request
  212. * @return int(1改变2不改变)
  213. */
  214. public function checkIsChange(Request $request)
  215. {
  216. if($this['type'] == 1) return 2;
  217. $devices = $request->input('devices');
  218. }
  219. public function order_devices()
  220. {
  221. return $this->hasMany('App\Models\OrderDevice', 'order_id');
  222. }
  223. }