Order.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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->project_name = Project::where('id',$item->project_id)->value('name');
  90. $item->user_name = $item->user ? $item->user->name : '';
  91. $item->work_point_name = $item->workPoint ? $item->workPoint->name : '';
  92. $option = Option::find($item->status);
  93. $item->status = $item->getStatusName();
  94. $item->status_key = $option ? $option['key'] : '';
  95. $item->color = $option ? $option['color'] : '';
  96. $item->date_time = substr($item->created_at, 0, 16);
  97. }
  98. public function backInnerDevices($devices)
  99. {
  100. $back_devices = [];
  101. $back_device_ids = [];
  102. foreach ($devices as $device) {
  103. if(isset($device['checked']) && $device['checked']) {
  104. array_push($back_devices, $device);
  105. array_push($back_device_ids, $device['id']);
  106. }
  107. }
  108. $new_order = null;
  109. if(count($back_devices) < count($devices)) {
  110. $status = Option::get('orders', 'status', 'pass', 'id');
  111. $last_role = ProjectRole::getLastRole($this);
  112. $level = $last_role ? $last_role['level'] : '';
  113. $new_order = Order::create([
  114. 'work_point_id' => $this['work_point_id'],
  115. 'remark' => $this['remark'],
  116. 'money' => $this['money'],
  117. 'is_draft' => $this['is_draft'],
  118. 'status' => $status,
  119. 'order_number' => self::createOrderNumber(),
  120. 'project_id' => $this['project_id'],
  121. 'user_id' => $this['user_id'],
  122. 'project_role_id' => $this['project_role_id'],
  123. 'level' => $level,
  124. 'last_project_role_id' => $this['last_project_role_id'],
  125. 'last_user_id' => $this['last_user_id'],
  126. 'is_change' => $this['is_change'],
  127. 'type' => $this['type']
  128. ]);
  129. OrderDevice::where('order_id', $this['id'])->whereIn('inner_device_id', $back_device_ids)->update(['order_id' => $new_order->id, 'end_date' => Carbon::now()->toDateString()]);
  130. }
  131. $free_id = Option::get('inner_devices', 'status', 'free');
  132. InnerDevice::whereIn('id', $back_device_ids)->update([
  133. 'status' => $free_id,
  134. 'start_date' => null,
  135. 'end_date' => null,
  136. 'work_point_id' => ''
  137. ]);
  138. return $new_order;
  139. }
  140. public function updateOrderDevices($devices)
  141. {
  142. OrderDevice::where('order_id', '=', $this['id'])->delete();
  143. if($this['type'] == 1) {
  144. $total = 0;
  145. foreach($devices as $device) {
  146. $price = $this->transMoney($device['price']);
  147. $data = $this->getNameSpecRent($device);
  148. OrderDevice::create(array_merge([
  149. 'order_id' => $this['id'],
  150. 'project_id' => $this['project_id'],
  151. 'quantity' => $device['quantity'],
  152. 'price' => $price,
  153. 'start_date' => $device['start_date'],
  154. 'end_date' => $device['end_date']
  155. ], $data));
  156. $total = $total + ($price * (int)$device['quantity']);
  157. }
  158. $this->update(['money' => $total]);
  159. } else {
  160. $using_id = Option::get('inner_devices', 'status', 'using');
  161. foreach($devices as $device) {
  162. OrderDevice::create([
  163. 'name' => $device['name'],
  164. 'order_id' => $this['id'],
  165. 'project_id' => $this['project_id'],
  166. 'user_id' => $this['user_id'],
  167. 'inner_device_id' => $device['id'],
  168. 'start_date' => $device['start_date'],
  169. 'end_date' => $device['end_date']
  170. ]);
  171. if($device['id']) {
  172. InnerDevice::find($device['id'])->update([
  173. 'project_id' => $this['project_id'],
  174. 'status' => $using_id,
  175. 'start_date' => $device['start_date'],
  176. 'end_date' => $device['end_date'],
  177. 'work_point_id' => $this['work_point_id']
  178. ]);
  179. }
  180. }
  181. }
  182. }
  183. public function getNameSpecRent($data)
  184. {
  185. $device = Device::find($data['type_id']);
  186. $rent_type = RentType::firstOrCreate([
  187. 'name' => $data['rent']
  188. ]);
  189. if(!$device) return [
  190. 'device_id' => '',
  191. 'device_name_id' => '',
  192. 'spec_id' => '',
  193. 'rent_type_id' => $rent_type->id
  194. ];
  195. $device_name = DeviceName::firstOrCreate([
  196. 'device_id' => $device->id,
  197. 'name' => $data['name']
  198. ]);
  199. $spec = Spec::firstOrCreate([
  200. 'device_id' => $device->id,
  201. 'device_name_id' => $device_name->id,
  202. 'name' => $data['spec']
  203. ]);
  204. return [
  205. 'device_id' => $device->id,
  206. 'device_name_id' => $device_name->id,
  207. 'spec_id' => $spec->id,
  208. 'rent_type_id' => $rent_type->id
  209. ];
  210. }
  211. /**
  212. * @param Request $request
  213. * @return int(1改变2不改变)
  214. */
  215. public function checkIsChange(Request $request)
  216. {
  217. if($this['type'] == 1) return 2;
  218. $devices = $request->input('devices');
  219. }
  220. public function order_devices()
  221. {
  222. return $this->hasMany('App\Models\OrderDevice', 'order_id');
  223. }
  224. }