123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- <?php
- namespace App\Models;
- use App\Services\OplogService;
- use App\Services\StatementsService;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- use App\Services\OrderService;
- use App\Models\Users;
- class OrderEditRecords extends Model
- {
- protected $table = 'order_edit_records';
- public $timestamps = false;
- public static function add($order_id, $before = [], $after = [], $op_reason = null, $content = null, $is_batch = 0)
- {
- DB::beginTransaction();
- try{
- $log = new OrderEditRecords();
- $log->order_id = $order_id;
- $log->month = $before['month'];
- $log->op_reason = $op_reason;
- $log->before_data = json_encode($before);
- $log->after_data = json_encode($after);
- $log->op_user_id = \Auth::id();
- $log->is_batch = $is_batch;
- $log->is_show = 1;
- if(1 == $is_batch) {
- $log->data_type = 1;
- }
-
- $user = Users::select('name', 'role_id', 'real_name')->where('id', $log->op_user_id)->first();
- $role_name = Roles::where('id', $user->role_id)->value('name');
- $log->op_user_name = $user->real_name;
- $log->op_user_role = $role_name;
- #名词
- $columns = config('columns_note.PROJECT');
- if($content) {
- $log->content = $content;
- } else {
- $log->content = '';
- if( !empty($after) && !empty($before) ){
- foreach( $after as $k=>$v ){
- if( $v != $before[$k] ){
- $k_desc = isset($columns[$k]) ? $columns[$k] : $k;
- $log->content .= $k_desc.'由'.$before[$k].'改为'. $v ."; ";
- }
- }
- }
- }
- $res = $log->save();
- if($res && $is_batch == 0){
- Order::where('id', $order_id)->update(['verify_status' =>1]); //同步更新订单表审核状态
- }
- if($is_batch == 1) {
- #批量修改 记录修改详情
- OrderEditRecordsDetail::addData($after, $log->id);
- }
- //短信提醒媒介审核
- StatementsService::auditReminder(15, '订单修改申请');
- DB::commit();
- } catch (Exception $e) {
- DB::rollBack();
- return false;
- }
- return true;
- }
- public static function addNew($orderId, $before = [], $after = [], $opReason = null, $type, $content = null, $isBatch = 0, $opUserId = null)
- {
- DB::beginTransaction();
- try{
- $log = new OrderEditRecords();
- $log->order_id = $orderId;
- $log->month = $before['month'];
- $log->op_reason = $opReason;
- $log->before_data = json_encode($before);
- $log->after_data = json_encode($after);
- $log->op_user_id = !empty($opUserId) ? $opUserId : \Auth::id();
- $log->is_batch = $isBatch;
- $log->data_type = $type;
- $log->is_show = 1;
- $user = Users::select('name', 'role_id', 'real_name')->where('id', $log->op_user_id)->first();
- $role_name = Roles::where('id', $user->role_id)->value('name');
- $log->op_user_name = $user->real_name;
- $log->op_user_role = $role_name;
- #名词
- $columns = config('columns_note.PROJECT');
- if($content) {
- $log->content = $content;
- } else {
- $log->content = '';
- if( !empty($after) && !empty($before) ){
- foreach( $after as $k=>$v ){
- if( $v != $before[$k] ){
- $k_desc = isset($columns[$k]) ? $columns[$k] : $k;
- $log->content .= $k_desc.'由'.$before[$k].'改为'. $v ."; ";
- }
- }
- }
- }
- $res = $log->save();
- if($res && $isBatch == 0){
- if (1 == $type) {
- OrderIncome::where('order_id', $orderId)->update(['verify_status' =>1]); //同步更新订单表审核状态
- if(empty($opUserId)){
- StatementsService::auditReminder(15, '订单收入修改申请');
- }
- } else {
- OrderCost::where('order_id', $orderId)->update(['cost_verify_status' => 1]);
- OrderMcn::where('order_id', $orderId)->update(['verify_status' =>1]);
- if(empty($opUserId)){
- StatementsService::auditReminder(13, '订单成本修改申请');
- }
- }
- }
- if($isBatch == 1) {
- #批量修改 记录修改详情
- OrderEditRecordsDetail::addData($after, $log->id);
- }
- //短信提醒商务审核
- // StatementsService::auditReminder(15, '订单修改申请');
- DB::commit();
- } catch (Exception $e) {
- DB::rollBack();
- return 500;
- }
- return true;
- }
- public static function importAdd($orderId, $before = [], $after = [], $opReason = null, $type, $content = null, $uniqueId)
- {
- \DB::beginTransaction();
- $isBatch = 0;
- try{
- $log = new OrderEditRecords();
- $log->order_id = $orderId;
- $log->month = $before['month'];
- $log->op_reason = $opReason;
- $log->before_data = json_encode($before, JSON_UNESCAPED_UNICODE);
- $log->after_data = json_encode($after, JSON_UNESCAPED_UNICODE);
- $log->op_user_id = \Auth::id();
- $log->is_batch = $isBatch;
- $log->data_type = $type;
- $log->is_show = 1;
- $log->unique_id = $uniqueId;
- $user = Users::query()
- ->select('name', 'role_id', 'real_name')
- ->where('id', $log->op_user_id)
- ->first();
- $role_name = Roles::query()
- ->where('id', $user->role_id)
- ->value('name');
- $log->op_user_name = $user->real_name;
- $log->op_user_role = $role_name;
- #名词
- $columns = config('columns_note.PROJECT');
- if($content) {
- $log->content = $content;
- } else {
- $log->content = '';
- if( !empty($after) && !empty($before) ){
- foreach( $after as $k=>$v ){
- if( $v != $before[$k] ){
- $k_desc = isset($columns[$k]) ? $columns[$k] : $k;
- $log->content .= $k_desc.'由'.$before[$k].'改为'. $v ."; ";
- }
- }
- }
- }
- $res = $log->save();
- if($res && $isBatch == 0){
- if (1 == $type) {
- OrderIncome::query()
- ->where('order_id', $orderId)
- ->update(['verify_status' =>1]); //同步更新订单表审核状态
- // StatementsService::auditReminder(15, '订单收入修改申请');
- } else {
- OrderCost::query()
- ->where('order_id', $orderId)
- ->update(['cost_verify_status' => 1]);
- OrderMcn::query()
- ->where('order_id', $orderId)
- ->update(['verify_status' =>1]);
- // StatementsService::auditReminder(13, '订单成本修改申请');
- }
- }
- if($isBatch == 1) {
- #批量修改 记录修改详情
- OrderEditRecordsDetail::addData($after, $log->id);
- }
- //短信提醒商务审核
- StatementsService::auditReminder(15, '订单修改申请');
- \DB::commit();
- } catch (\Exception $e) {
- \DB::rollBack();
- return $e->getMessage();
- }
- return true;
- }
- /**
- * 状态机判断
- */
- private static function verifyStatus($status, $old_status)
- {
- $ok_status = [];
- switch ($old_status) {
- case 1:
- $ok_status = [2, 3, 0];
- break;
- case 2:
- $ok_status = [4, 5, 0];
- break;
- default:
- break;
- }
- if( in_array($status, $ok_status) ){
- return true;
- }
- return false;
- }
- /**
- * 审核订单
- */
- public static function verifyOrder($id, $status, $verify_note = null)
- {
- DB::beginTransaction();
- try{
- $log = OrderEditRecords::where('id', $id)->first();
- if( !self::verifyStatus($status, $log->status) ){
- return false; //异常
- }
- $op_user_id = \Auth::id();
- $user = Users::select('name', 'role_id')->where('id', $op_user_id)->first();
- $op_user_name = $user->name;
- if( in_array($status, [2, 3]) ){
- #媒介审核,商务审核
- $log->status = $status;
- $log->verify_user_id = $op_user_id;
- $log->verify_user_name = $op_user_name;
- $log->verify_note = $verify_note;
- $log->verify_at = date('Y-m-d H:i:s');
- } elseif( in_array($status, [4, 5]) ) {
- #财务审核
- $log->status = $status;
- $log->final_verify_user_id = $op_user_id;
- $log->final_verify_user_name = $op_user_name;
- $log->final_verify_note = $verify_note;
- $log->final_verify_at = date('Y-m-d H:i:s');
- } elseif( $status == 0){
- //撤销审核
- $log->status = $status;
- }else {
- return false; //异常
- }
- $res = $log->save();
- if($res){
- $order_id = $log->order_id;
- #$update = ['verify_status' =>$status];
- if($log->is_batch == 0) {
- #终审通过,字段变更
- if (1 == $log->data_type) {
- OrderIncome::where('order_id', $order_id)->update(['verify_status' => $status]);
- } else {
- OrderMcn::where('order_id', $order_id)->update(['verify_status' => $status]);
- OrderCost::where('order_id', $order_id)->update(['cost_verify_status' => $status]);
- }
- #同步修改字段变更明细记录表
- OrderColumnRecords::addData($log->toArray());
- $orderInfo = Order::where('id', $order_id)->first();
- if($status == 4){
- $params = json_decode($log->after_data, true);
- if($orderInfo->status < 4) {
- if(isset($params['mcn_final_amount'])) {
- $params['mcn_rebate'] = $params['mcn_final_amount'];
- $params['original_mcn_rebate'] = $params['mcn_final_amount'];
- }
- if(isset($params['final_amount'])) {
- $params['origin_amount'] = $params['final_amount'];
- }
- if(isset($params['customer_name'])) {
- $params['origin_customer_name'] = $params['customer_name'];
- }
- }
- $change_columns = OplogService::changeColumns(json_decode(json_encode($orderInfo), 1), $params);
- # 如果修改了mcn负责人,则自动带入mcn负责人id
- if ( isset($params['mcn_user']) && in_array('mcn_user', $change_columns) ) {
- $saler_info = Users::select('id')->where('real_name', $params['mcn_user'])->first();
- $params['mcn_user_id'] = isset($saler_info->id) ? $saler_info->id : null;
- }
- # 如果修改了mcn经手人,则自动带入mcn经手人id
- if ( isset($params['mcn_handler']) && in_array('mcn_handler', $change_columns) ) {
- $saler_info = Users::select('id')->where('real_name', $params['mcn_handler'])->first();
- $params['mcn_handler_id'] = isset($saler_info->id) ? $saler_info->id : null;
- }
- Order::pushSyncTask($orderInfo->task_id, $orderInfo->month, 2, $params);
- }
- } else {
- #终审通过,字段变更
- $params = OrderEditRecordsDetail::getAllOrder($log->id);
- $order_ids = array_column($params, 'order_id');
- OrderIncome::whereIn('order_id', $order_ids)->update(['verify_status' => $status]); //同步更新订单表审核状态
- if($status == 4){
- foreach($params as $item) {
- $orderInfo = OrderIncome::where('order_id',$item['order_id'])->first();
- $updateData = [
- 'final_amount' => $item['final_amount'],
- 'actual_consumption_after' => $item['actual_consumption_after'],
- 'top_fee_after' => $item['top_fee_after'],
- 'link_fee_after' => $item['link_fee_after'],
- 'flow_boost_back_point_after' => $item['flow_boost_back_point_after']
- ];
- if($orderInfo->status < 4) {
- $updateData['origin_amount'] = $item['final_amount'];
- }
- Order::pushSyncTask($orderInfo->task_id, $orderInfo->month, 2, $updateData);
- }
- OrderColumnRecords::addBatchData($params, $log->toArray());
- }
-
- }
- if (2 == $status) {
- //一审通过之后短信通知财务
- StatementsService::auditReminder(10, '订单修改申请');
- }
- }
- DB::commit();
- } catch (Exception $e) {
- DB::rollBack();
- return false;
- }
- return true;
- }
- /*
- * 批量修改撤销审核
- */
- public static function delVerifyOrder($id)
- {
- $status = 0;
- DB::beginTransaction();
- try{
- $log = OrderEditRecords::where('id', $id)->first();
- if( !self::verifyStatus($status, $log->status) ){
- return false; //异常
- }
- //撤销审核
- $log->status = $status;
- $res = $log->save();
- $update = ['verify_status' =>$status];
- $params = OrderEditRecordsDetail::getAllOrder($id);
- $order_ids = array_column($params, 'order_id');
- OrderEditRecordsDetail::where('edit_id', $id)->update(['enable' => 0]);
- OrderIncome::whereIn('order_id', $order_ids)->update($update); //同步更新订单表审核状态
- DB::commit();
- } catch (Exception $e) {
- DB::rollBack();
- return false;
- }
- return true;
- }
- public static function addRecord($order_id, $before = [], $after = [], $op_reason = null)
- {
- // $log = new OrderEditRecords();
- // $before = json_decode(json_encode($before), 1);
- $log['order_id'] = $order_id;
- $log['month'] = $before['month'];
- $log['op_reason'] = $op_reason;
- $log['before_data'] = json_encode($before);
- $log['after_data'] = json_encode($after);
- $log['op_user_id'] = \Auth::id();
- $log['is_batch'] = 0;
- $log['is_show'] = 0;
- $log['data_type'] = 0;
- $log['final_verify_at'] = date('Y-m-d H:i:s');
- $log['status'] = 4;
- $user = Users::select('name', 'role_id', 'real_name')->where('id', $log['op_user_id'])->first();
- $role_name = Roles::where('id', $user->role_id)->value('name');
- $log['op_user_name'] = $user->real_name;
- $log['op_user_role'] = $role_name;
- #名词
- $columns = config('columns_note.PROJECT');
- $log['content'] = '';
- if( !empty($after) && !empty($before) ){
- foreach( $after as $k=>$v ){
- if( $v != $before[$k] ){
- $k_desc = isset($columns[$k]) ? $columns[$k] : $k;
- $log['content'] .= $k_desc.'由'.$before[$k].'改为'. $v ."; ";
- }
- }
- }
- if(self::insert($log)) {
- return true;
- } else {
- return false;
- }
- }
- }
|