OrderObserver.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Observers;
  11. use App\Repositories\Enums\Base\PayStatusEnum;
  12. use App\Repositories\Enums\Car\BillDisburseEnum;
  13. use App\Repositories\Enums\Car\BillTypeEnum;
  14. use App\Repositories\Enums\Car\OrderDrivingStatusEnum;
  15. use App\Repositories\Enums\Car\OrderPayTypeEnum;
  16. use App\Repositories\Enums\Check\StatusEnum;
  17. use App\Repositories\Enums\ModelStatusEnum;
  18. use App\Repositories\Enums\RoleEnum;
  19. use App\Repositories\Models\Base\Admin;
  20. use App\Repositories\Models\Base\Setting;
  21. use App\Repositories\Models\Car\Bill;
  22. use App\Repositories\Models\Car\Grade;
  23. use App\Repositories\Models\Car\Order;
  24. use App\Repositories\Models\Car\OrderLog;
  25. use App\Repositories\Models\Car\Shop;
  26. use Carbon\Carbon;
  27. use Illuminate\Support\Facades\DB;
  28. class OrderObserver
  29. {
  30. public function saved(Order $order)
  31. {
  32. $settings = Setting::byCodesGetSettings([
  33. 'shop_manage_bonus',
  34. 'salesman_all_bonus',
  35. 'time_salesman_all_bonus',
  36. 'salesman_0_bonus',
  37. 'time_salesman_0_bonus',
  38. 'coach_car_2_bonus',
  39. 'coach_car_3_bonus',
  40. ]);
  41. //分成
  42. if ($order->isDirty('driving_status')) {
  43. $now = Carbon::now()->toDateTimeString();
  44. switch ((int)$order->driving_status) {
  45. case OrderDrivingStatusEnum::WAIT_HEALTH:
  46. //新学员分成
  47. //2.店长老师
  48. $shop_admin_id = Shop::query()->where('id', $order->shop_id)->value('shop_admin_id');
  49. if ($shop_admin_id) {
  50. if ($order->pay_type == OrderPayTypeEnum::PAY_ALL) {
  51. Bill::query()->create([
  52. 'user_id' => $order->user_id,
  53. 'order_id' => $order->id,
  54. 'shop_id' => $order->shop_id,
  55. 'admin_id' => $shop_admin_id,
  56. // 'check_status' => StatusEnum::SUCCESS,
  57. 'check_status' => StatusEnum::WAIT,
  58. 'check_admin_id' => 0,
  59. // 'check_time' => $now,
  60. 'type_id' => BillTypeEnum::BONUS_SHOP,
  61. 'disburse' => BillDisburseEnum::OUT,
  62. 'money' => $settings['shop_manage_bonus'],
  63. 'is_give' => 0,
  64. 'is_lock' => 0,
  65. ]);
  66. } else {
  67. Bill::query()->create([
  68. 'user_id' => $order->user_id,
  69. 'order_id' => $order->id,
  70. 'shop_id' => $order->shop_id,
  71. 'admin_id' => $shop_admin_id,
  72. 'check_status' => StatusEnum::STAGE,
  73. 'check_admin_id' => 0,
  74. 'check_time' => $now,
  75. 'type_id' => BillTypeEnum::BONUS_SHOP,
  76. 'disburse' => BillDisburseEnum::OUT,
  77. 'money' => $settings['shop_manage_bonus'],
  78. 'is_give' => 0,
  79. 'is_lock' => 1,
  80. ]);
  81. }
  82. }
  83. //1.招生老师
  84. if ($order->salesman_admin_id) {
  85. $salesman_admin = Admin::query()->where('id', $order->salesman_admin_id)->first();
  86. if ($salesman_admin) {
  87. switch ($order->pay_type) {
  88. case OrderPayTypeEnum::PAY_ALL:
  89. if (!$salesman_admin->hasRole(RoleEnum::BUSINESS_TEMP)) {
  90. Bill::query()->create([
  91. 'user_id' => $order->user_id,
  92. 'order_id' => $order->id,
  93. 'shop_id' => $order->shop_id,
  94. 'admin_id' => $order->salesman_admin_id,
  95. // 'check_status' => StatusEnum::SUCCESS,
  96. 'check_status' => StatusEnum::WAIT,
  97. 'check_admin_id' => 0,
  98. // 'check_time' => $now,
  99. 'type_id' => BillTypeEnum::BONUS_BUSINESS,
  100. 'disburse' => BillDisburseEnum::OUT,
  101. 'money' => $settings['salesman_all_bonus'],
  102. ]);
  103. } else {
  104. Bill::query()->create([
  105. 'user_id' => $order->user_id,
  106. 'order_id' => $order->id,
  107. 'shop_id' => $order->shop_id,
  108. 'admin_id' => $order->salesman_admin_id,
  109. // 'check_status' => StatusEnum::SUCCESS,
  110. 'check_status' => StatusEnum::WAIT,
  111. 'check_admin_id' => 0,
  112. // 'check_time' => $now,
  113. 'type_id' => BillTypeEnum::BONUS_BUSINESS,
  114. 'disburse' => BillDisburseEnum::OUT,
  115. 'money' => $settings['time_salesman_all_bonus'],
  116. 'is_give' => 0,
  117. ]);
  118. }
  119. break;
  120. case OrderPayTypeEnum::PAY_AGING:
  121. if (!$salesman_admin->hasRole(RoleEnum::BUSINESS_TEMP)) {
  122. $salesman_0_bonus_arr = str2arr($settings['salesman_0_bonus']);
  123. if (isset($salesman_0_bonus_arr[0]) && $salesman_0_bonus_arr[0]) {
  124. Bill::query()->create([
  125. 'user_id' => $order->user_id,
  126. 'order_id' => $order->id,
  127. 'shop_id' => $order->shop_id,
  128. 'admin_id' => $order->salesman_admin_id,
  129. // 'check_status' => StatusEnum::SUCCESS,
  130. 'check_status' => StatusEnum::WAIT,
  131. 'check_admin_id' => 0,
  132. // 'check_time' => $now,
  133. 'type_id' => BillTypeEnum::BONUS_BUSINESS,
  134. 'disburse' => BillDisburseEnum::OUT,
  135. 'money' => $salesman_0_bonus_arr[0],
  136. 'is_give' => 0,
  137. ]);
  138. }
  139. if (isset($salesman_0_bonus_arr[1]) && $salesman_0_bonus_arr[1]) {
  140. Bill::query()->create([
  141. 'user_id' => $order->user_id,
  142. 'order_id' => $order->id,
  143. 'shop_id' => $order->shop_id,
  144. 'admin_id' => $order->salesman_admin_id,
  145. 'check_status' => StatusEnum::STAGE,
  146. 'check_admin_id' => 0,
  147. 'check_time' => $now,
  148. 'type_id' => BillTypeEnum::BONUS_BUSINESS,
  149. 'disburse' => BillDisburseEnum::OUT,
  150. 'is_lock' => 1,
  151. 'money' => $salesman_0_bonus_arr[1],
  152. 'is_give' => 0,
  153. ]);
  154. }
  155. } else {
  156. $time_salesman_0_bonus_arr = str2arr($settings['time_salesman_0_bonus']);
  157. if (isset($time_salesman_0_bonus_arr[0]) && $time_salesman_0_bonus_arr[0]) {
  158. Bill::query()->create([
  159. 'user_id' => $order->user_id,
  160. 'order_id' => $order->id,
  161. 'shop_id' => $order->shop_id,
  162. 'admin_id' => $order->salesman_admin_id,
  163. // 'check_status' => StatusEnum::SUCCESS,
  164. 'check_status' => StatusEnum::WAIT,
  165. 'check_admin_id' => 0,
  166. // 'check_time' => $now,
  167. 'type_id' => BillTypeEnum::BONUS_BUSINESS,
  168. 'disburse' => BillDisburseEnum::OUT,
  169. 'money' => $time_salesman_0_bonus_arr[0],
  170. 'is_give' => 0,
  171. ]);
  172. }
  173. if (isset($time_salesman_0_bonus_arr[1]) && $time_salesman_0_bonus_arr[1]) {
  174. Bill::query()->create([
  175. 'user_id' => $order->user_id,
  176. 'order_id' => $order->id,
  177. 'shop_id' => $order->shop_id,
  178. 'admin_id' => $order->salesman_admin_id,
  179. 'check_status' => StatusEnum::STAGE,
  180. 'check_admin_id' => 0,
  181. 'check_time' => $now,
  182. 'type_id' => BillTypeEnum::BONUS_BUSINESS,
  183. 'disburse' => BillDisburseEnum::OUT,
  184. 'is_lock' => 1,
  185. 'money' => $time_salesman_0_bonus_arr[1],
  186. 'is_give' => 0,
  187. ]);
  188. }
  189. }
  190. break;
  191. }
  192. }
  193. }
  194. break;
  195. case OrderDrivingStatusEnum::STUDY_KM_1_FAIL:
  196. DB::table('car_orders')->where('id', $order->id)->update([
  197. 'ke_1_nums' => $order->ke_1_nums + 1
  198. ]);
  199. break;
  200. case OrderDrivingStatusEnum::STUDY_KM_2_FAIL:
  201. DB::table('car_orders')->where('id', $order->id)->update([
  202. 'ke_2_nums' => $order->ke_2_nums + 1
  203. ]);
  204. break;
  205. case OrderDrivingStatusEnum::STUDY_KM_3_FAIL:
  206. DB::table('car_orders')->where('id', $order->id)->update([
  207. 'ke_3_nums' => $order->ke_3_nums + 1
  208. ]);
  209. break;
  210. case OrderDrivingStatusEnum::STUDY_KM_3:
  211. //科二提成
  212. $nums = OrderLog::query()->where('order_id', $order->id)->where('driving_status', OrderDrivingStatusEnum::STUDY_KM_2_FAIL)->where('status', ModelStatusEnum::OK)->count();
  213. if ($order->ke_2_nums != $nums) {
  214. DB::table('car_orders')->where('id', $order->id)->update([
  215. 'ke_2_nums' => $nums
  216. ]);
  217. }
  218. $moneys = str2arr($settings['coach_car_2_bonus']);
  219. $money = 0;
  220. if (isset($moneys[$nums])) {
  221. $money = $moneys[$nums];
  222. }
  223. Bill::query()->create([
  224. 'user_id' => $order->user_id,
  225. 'order_id' => $order->id,
  226. 'shop_id' => $order->shop_id,
  227. 'admin_id' => $order->subject_2_admin_id,
  228. // 'check_status' => StatusEnum::SUCCESS,
  229. 'check_status' => StatusEnum::WAIT,
  230. 'check_admin_id' => 0,
  231. // 'check_time' => $now,
  232. 'type_id' => BillTypeEnum::BONUS_COACH_2,
  233. 'disburse' => BillDisburseEnum::OUT,
  234. 'money' => $money,
  235. 'is_give' => 0,
  236. ]);
  237. break;
  238. case OrderDrivingStatusEnum::STUDY_KM_4:
  239. //科三提成
  240. $nums = OrderLog::query()->where('order_id', $order->id)->where('driving_status', OrderDrivingStatusEnum::STUDY_KM_3_FAIL)->where('status', ModelStatusEnum::OK)->count();
  241. if ($order->ke_3_nums != $nums) {
  242. DB::table('car_orders')->where('id', $order->id)->update([
  243. 'ke_3_nums' => $nums
  244. ]);
  245. }
  246. $moneys = str2arr($settings['coach_car_3_bonus']);
  247. $money = 0;
  248. if (isset($moneys[$nums])) {
  249. $money = $moneys[$nums];
  250. }
  251. Bill::query()->create([
  252. 'user_id' => $order->user_id,
  253. 'order_id' => $order->id,
  254. 'shop_id' => $order->shop_id,
  255. 'admin_id' => $order->subject_3_admin_id,
  256. // 'check_status' => StatusEnum::SUCCESS,
  257. 'check_status' => StatusEnum::WAIT,
  258. 'check_admin_id' => 0,
  259. // 'check_time' => $now,
  260. 'type_id' => BillTypeEnum::BONUS_COACH_3,
  261. 'disburse' => BillDisburseEnum::OUT,
  262. 'money' => $money,
  263. 'is_give' => 0,
  264. ]);
  265. break;
  266. }
  267. }
  268. }
  269. }