新版订单消耗系统

OrderEditRecords.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. namespace App\Models;
  3. use App\Services\OplogService;
  4. use App\Services\StatementsService;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Services\OrderService;
  8. use App\Models\Users;
  9. class OrderEditRecords extends Model
  10. {
  11. protected $table = 'order_edit_records';
  12. public $timestamps = false;
  13. public static function add($order_id, $before = [], $after = [], $op_reason = null, $content = null, $is_batch = 0)
  14. {
  15. DB::beginTransaction();
  16. try{
  17. $log = new OrderEditRecords();
  18. $log->order_id = $order_id;
  19. $log->month = $before['month'];
  20. $log->op_reason = $op_reason;
  21. $log->before_data = json_encode($before);
  22. $log->after_data = json_encode($after);
  23. $log->op_user_id = \Auth::id();
  24. $log->is_batch = $is_batch;
  25. $log->is_show = 1;
  26. if(1 == $is_batch) {
  27. $log->data_type = 1;
  28. }
  29. $user = Users::select('name', 'role_id', 'real_name')->where('id', $log->op_user_id)->first();
  30. $role_name = Roles::where('id', $user->role_id)->value('name');
  31. $log->op_user_name = $user->real_name;
  32. $log->op_user_role = $role_name;
  33. #名词
  34. $columns = config('columns_note.PROJECT');
  35. if($content) {
  36. $log->content = $content;
  37. } else {
  38. $log->content = '';
  39. if( !empty($after) && !empty($before) ){
  40. foreach( $after as $k=>$v ){
  41. if( $v != $before[$k] ){
  42. $k_desc = isset($columns[$k]) ? $columns[$k] : $k;
  43. $log->content .= $k_desc.'由'.$before[$k].'改为'. $v ."; ";
  44. }
  45. }
  46. }
  47. }
  48. $res = $log->save();
  49. if($res && $is_batch == 0){
  50. Order::where('id', $order_id)->update(['verify_status' =>1]); //同步更新订单表审核状态
  51. }
  52. if($is_batch == 1) {
  53. #批量修改 记录修改详情
  54. OrderEditRecordsDetail::addData($after, $log->id);
  55. }
  56. //短信提醒媒介审核
  57. StatementsService::auditReminder(15, '订单修改申请');
  58. DB::commit();
  59. } catch (Exception $e) {
  60. DB::rollBack();
  61. return false;
  62. }
  63. return true;
  64. }
  65. public static function addNew($orderId, $before = [], $after = [], $opReason = null, $type, $content = null, $isBatch = 0, $opUserId = null)
  66. {
  67. DB::beginTransaction();
  68. try{
  69. $log = new OrderEditRecords();
  70. $log->order_id = $orderId;
  71. $log->month = $before['month'];
  72. $log->op_reason = $opReason;
  73. $log->before_data = json_encode($before);
  74. $log->after_data = json_encode($after);
  75. $log->op_user_id = !empty($opUserId) ? $opUserId : \Auth::id();
  76. $log->is_batch = $isBatch;
  77. $log->data_type = $type;
  78. $log->is_show = 1;
  79. $user = Users::select('name', 'role_id', 'real_name')->where('id', $log->op_user_id)->first();
  80. $role_name = Roles::where('id', $user->role_id)->value('name');
  81. $log->op_user_name = $user->real_name;
  82. $log->op_user_role = $role_name;
  83. #名词
  84. $columns = config('columns_note.PROJECT');
  85. if($content) {
  86. $log->content = $content;
  87. } else {
  88. $log->content = '';
  89. if( !empty($after) && !empty($before) ){
  90. foreach( $after as $k=>$v ){
  91. if( $v != $before[$k] ){
  92. $k_desc = isset($columns[$k]) ? $columns[$k] : $k;
  93. $log->content .= $k_desc.'由'.$before[$k].'改为'. $v ."; ";
  94. }
  95. }
  96. }
  97. }
  98. $res = $log->save();
  99. if($res && $isBatch == 0){
  100. if (1 == $type) {
  101. OrderIncome::where('order_id', $orderId)->update(['verify_status' =>1]); //同步更新订单表审核状态
  102. if(empty($opUserId)){
  103. StatementsService::auditReminder(15, '订单收入修改申请');
  104. }
  105. } else {
  106. OrderCost::where('order_id', $orderId)->update(['cost_verify_status' => 1]);
  107. OrderMcn::where('order_id', $orderId)->update(['verify_status' =>1]);
  108. if(empty($opUserId)){
  109. StatementsService::auditReminder(13, '订单成本修改申请');
  110. }
  111. }
  112. }
  113. if($isBatch == 1) {
  114. #批量修改 记录修改详情
  115. OrderEditRecordsDetail::addData($after, $log->id);
  116. }
  117. //短信提醒商务审核
  118. // StatementsService::auditReminder(15, '订单修改申请');
  119. DB::commit();
  120. } catch (Exception $e) {
  121. DB::rollBack();
  122. return 500;
  123. }
  124. return true;
  125. }
  126. public static function importAdd($orderId, $before = [], $after = [], $opReason = null, $type, $content = null, $uniqueId)
  127. {
  128. \DB::beginTransaction();
  129. $isBatch = 0;
  130. try{
  131. $log = new OrderEditRecords();
  132. $log->order_id = $orderId;
  133. $log->month = $before['month'];
  134. $log->op_reason = $opReason;
  135. $log->before_data = json_encode($before, JSON_UNESCAPED_UNICODE);
  136. $log->after_data = json_encode($after, JSON_UNESCAPED_UNICODE);
  137. $log->op_user_id = \Auth::id();
  138. $log->is_batch = $isBatch;
  139. $log->data_type = $type;
  140. $log->is_show = 1;
  141. $log->unique_id = $uniqueId;
  142. $user = Users::query()
  143. ->select('name', 'role_id', 'real_name')
  144. ->where('id', $log->op_user_id)
  145. ->first();
  146. $role_name = Roles::query()
  147. ->where('id', $user->role_id)
  148. ->value('name');
  149. $log->op_user_name = $user->real_name;
  150. $log->op_user_role = $role_name;
  151. #名词
  152. $columns = config('columns_note.PROJECT');
  153. if($content) {
  154. $log->content = $content;
  155. } else {
  156. $log->content = '';
  157. if( !empty($after) && !empty($before) ){
  158. foreach( $after as $k=>$v ){
  159. if( $v != $before[$k] ){
  160. $k_desc = isset($columns[$k]) ? $columns[$k] : $k;
  161. $log->content .= $k_desc.'由'.$before[$k].'改为'. $v ."; ";
  162. }
  163. }
  164. }
  165. }
  166. $res = $log->save();
  167. if($res && $isBatch == 0){
  168. if (1 == $type) {
  169. OrderIncome::query()
  170. ->where('order_id', $orderId)
  171. ->update(['verify_status' =>1]); //同步更新订单表审核状态
  172. // StatementsService::auditReminder(15, '订单收入修改申请');
  173. } else {
  174. OrderCost::query()
  175. ->where('order_id', $orderId)
  176. ->update(['cost_verify_status' => 1]);
  177. OrderMcn::query()
  178. ->where('order_id', $orderId)
  179. ->update(['verify_status' =>1]);
  180. // StatementsService::auditReminder(13, '订单成本修改申请');
  181. }
  182. }
  183. if($isBatch == 1) {
  184. #批量修改 记录修改详情
  185. OrderEditRecordsDetail::addData($after, $log->id);
  186. }
  187. //短信提醒商务审核
  188. StatementsService::auditReminder(15, '订单修改申请');
  189. \DB::commit();
  190. } catch (\Exception $e) {
  191. \DB::rollBack();
  192. return $e->getMessage();
  193. }
  194. return true;
  195. }
  196. /**
  197. * 状态机判断
  198. */
  199. private static function verifyStatus($status, $old_status)
  200. {
  201. $ok_status = [];
  202. switch ($old_status) {
  203. case 1:
  204. $ok_status = [2, 3, 0];
  205. break;
  206. case 2:
  207. $ok_status = [4, 5, 0];
  208. break;
  209. default:
  210. break;
  211. }
  212. if( in_array($status, $ok_status) ){
  213. return true;
  214. }
  215. return false;
  216. }
  217. /**
  218. * 审核订单
  219. */
  220. public static function verifyOrder($id, $status, $verify_note = null)
  221. {
  222. DB::beginTransaction();
  223. try{
  224. $log = OrderEditRecords::where('id', $id)->first();
  225. if( !self::verifyStatus($status, $log->status) ){
  226. return false; //异常
  227. }
  228. $op_user_id = \Auth::id();
  229. $user = Users::select('name', 'role_id')->where('id', $op_user_id)->first();
  230. $op_user_name = $user->name;
  231. if( in_array($status, [2, 3]) ){
  232. #媒介审核,商务审核
  233. $log->status = $status;
  234. $log->verify_user_id = $op_user_id;
  235. $log->verify_user_name = $op_user_name;
  236. $log->verify_note = $verify_note;
  237. $log->verify_at = date('Y-m-d H:i:s');
  238. } elseif( in_array($status, [4, 5]) ) {
  239. #财务审核
  240. $log->status = $status;
  241. $log->final_verify_user_id = $op_user_id;
  242. $log->final_verify_user_name = $op_user_name;
  243. $log->final_verify_note = $verify_note;
  244. $log->final_verify_at = date('Y-m-d H:i:s');
  245. } elseif( $status == 0){
  246. //撤销审核
  247. $log->status = $status;
  248. }else {
  249. return false; //异常
  250. }
  251. $res = $log->save();
  252. if($res){
  253. $order_id = $log->order_id;
  254. #$update = ['verify_status' =>$status];
  255. if($log->is_batch == 0) {
  256. #终审通过,字段变更
  257. if (1 == $log->data_type) {
  258. OrderIncome::where('order_id', $order_id)->update(['verify_status' => $status]);
  259. } else {
  260. OrderMcn::where('order_id', $order_id)->update(['verify_status' => $status]);
  261. OrderCost::where('order_id', $order_id)->update(['cost_verify_status' => $status]);
  262. }
  263. #同步修改字段变更明细记录表
  264. OrderColumnRecords::addData($log->toArray());
  265. $orderInfo = Order::where('id', $order_id)->first();
  266. if($status == 4){
  267. $params = json_decode($log->after_data, true);
  268. if($orderInfo->status < 4) {
  269. if(isset($params['mcn_final_amount'])) {
  270. $params['mcn_rebate'] = $params['mcn_final_amount'];
  271. $params['original_mcn_rebate'] = $params['mcn_final_amount'];
  272. }
  273. if(isset($params['final_amount'])) {
  274. $params['origin_amount'] = $params['final_amount'];
  275. }
  276. if(isset($params['customer_name'])) {
  277. $params['origin_customer_name'] = $params['customer_name'];
  278. }
  279. }
  280. $change_columns = OplogService::changeColumns(json_decode(json_encode($orderInfo), 1), $params);
  281. # 如果修改了mcn负责人,则自动带入mcn负责人id
  282. if ( isset($params['mcn_user']) && in_array('mcn_user', $change_columns) ) {
  283. $saler_info = Users::select('id')->where('real_name', $params['mcn_user'])->first();
  284. $params['mcn_user_id'] = isset($saler_info->id) ? $saler_info->id : null;
  285. }
  286. # 如果修改了mcn经手人,则自动带入mcn经手人id
  287. if ( isset($params['mcn_handler']) && in_array('mcn_handler', $change_columns) ) {
  288. $saler_info = Users::select('id')->where('real_name', $params['mcn_handler'])->first();
  289. $params['mcn_handler_id'] = isset($saler_info->id) ? $saler_info->id : null;
  290. }
  291. Order::pushSyncTask($orderInfo->task_id, $orderInfo->month, 2, $params);
  292. }
  293. } else {
  294. #终审通过,字段变更
  295. $params = OrderEditRecordsDetail::getAllOrder($log->id);
  296. $order_ids = array_column($params, 'order_id');
  297. OrderIncome::whereIn('order_id', $order_ids)->update(['verify_status' => $status]); //同步更新订单表审核状态
  298. if($status == 4){
  299. foreach($params as $item) {
  300. $orderInfo = OrderIncome::where('order_id',$item['order_id'])->first();
  301. $updateData = [
  302. 'final_amount' => $item['final_amount'],
  303. 'actual_consumption_after' => $item['actual_consumption_after'],
  304. 'top_fee_after' => $item['top_fee_after'],
  305. 'link_fee_after' => $item['link_fee_after'],
  306. 'flow_boost_back_point_after' => $item['flow_boost_back_point_after']
  307. ];
  308. if($orderInfo->status < 4) {
  309. $updateData['origin_amount'] = $item['final_amount'];
  310. }
  311. Order::pushSyncTask($orderInfo->task_id, $orderInfo->month, 2, $updateData);
  312. }
  313. OrderColumnRecords::addBatchData($params, $log->toArray());
  314. }
  315. }
  316. if (2 == $status) {
  317. //一审通过之后短信通知财务
  318. StatementsService::auditReminder(10, '订单修改申请');
  319. }
  320. }
  321. DB::commit();
  322. } catch (Exception $e) {
  323. DB::rollBack();
  324. return false;
  325. }
  326. return true;
  327. }
  328. /*
  329. * 批量修改撤销审核
  330. */
  331. public static function delVerifyOrder($id)
  332. {
  333. $status = 0;
  334. DB::beginTransaction();
  335. try{
  336. $log = OrderEditRecords::where('id', $id)->first();
  337. if( !self::verifyStatus($status, $log->status) ){
  338. return false; //异常
  339. }
  340. //撤销审核
  341. $log->status = $status;
  342. $res = $log->save();
  343. $update = ['verify_status' =>$status];
  344. $params = OrderEditRecordsDetail::getAllOrder($id);
  345. $order_ids = array_column($params, 'order_id');
  346. OrderEditRecordsDetail::where('edit_id', $id)->update(['enable' => 0]);
  347. OrderIncome::whereIn('order_id', $order_ids)->update($update); //同步更新订单表审核状态
  348. DB::commit();
  349. } catch (Exception $e) {
  350. DB::rollBack();
  351. return false;
  352. }
  353. return true;
  354. }
  355. public static function addRecord($order_id, $before = [], $after = [], $op_reason = null)
  356. {
  357. // $log = new OrderEditRecords();
  358. // $before = json_decode(json_encode($before), 1);
  359. $log['order_id'] = $order_id;
  360. $log['month'] = $before['month'];
  361. $log['op_reason'] = $op_reason;
  362. $log['before_data'] = json_encode($before);
  363. $log['after_data'] = json_encode($after);
  364. $log['op_user_id'] = \Auth::id();
  365. $log['is_batch'] = 0;
  366. $log['is_show'] = 0;
  367. $log['data_type'] = 0;
  368. $log['final_verify_at'] = date('Y-m-d H:i:s');
  369. $log['status'] = 4;
  370. $user = Users::select('name', 'role_id', 'real_name')->where('id', $log['op_user_id'])->first();
  371. $role_name = Roles::where('id', $user->role_id)->value('name');
  372. $log['op_user_name'] = $user->real_name;
  373. $log['op_user_role'] = $role_name;
  374. #名词
  375. $columns = config('columns_note.PROJECT');
  376. $log['content'] = '';
  377. if( !empty($after) && !empty($before) ){
  378. foreach( $after as $k=>$v ){
  379. if( $v != $before[$k] ){
  380. $k_desc = isset($columns[$k]) ? $columns[$k] : $k;
  381. $log['content'] .= $k_desc.'由'.$before[$k].'改为'. $v ."; ";
  382. }
  383. }
  384. }
  385. if(self::insert($log)) {
  386. return true;
  387. } else {
  388. return false;
  389. }
  390. }
  391. }