新版订单消耗系统

ToolController.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2021/4/7
  6. * Time: 14:26
  7. */
  8. namespace App\Http\Controllers\Api;
  9. use App\Models\OrderMcn;
  10. use App\Models\CPAStatementsDetail;
  11. use App\Models\StatementsSeal;
  12. use App\Services\ToolService;
  13. use Illuminate\Http\Request;
  14. use App\Models\Order;
  15. use App\Models\OrderEditRecords;
  16. use App\Models\OrderIncome;
  17. use App\Models\Statements;
  18. use App\Models\GameStatementsDetail;
  19. use App\Models\OrderCost;
  20. use App\Models\PddStatementsDetail;
  21. use App\Models\McnStatementsDetail;
  22. use App\Models\CustomerInvoices;
  23. use App\Models\CustomerInvoiceOrders;
  24. class ToolController extends Controller
  25. {
  26. public function updateOrderData()
  27. {
  28. $taskIdList = [
  29. '6986088715269439495',
  30. '6982825563635318791',
  31. '6985387528924692487',
  32. '6983129980322447374',
  33. '6982823120826531876'
  34. ];
  35. $month = '2021-07-01';
  36. foreach($taskIdList as $taskId) {
  37. # 查询订单基础信息,为了将订单原始信息保存到修改记录表中
  38. $info = Order::where('month', $month)->where('enable', 1)
  39. ->where('task_id', $taskId)->first();
  40. # 订单id
  41. $id = $info->id;
  42. # 订单信息格式化
  43. $old = json_decode(json_encode($info), 1);
  44. # 填写邮件中申请的理由
  45. $op_reason = '邮件申请修改,申请理由:现由于消耗表后台7月星图抖音消耗有五单任务对应媒体名称有误,商务端已开票,无法直接修改后台,导致成本发票对应不上,故申请修改消耗表后台部分任务的媒体名称及对应成本公司。';
  46. # 填写要修改的字段以及值
  47. $after = ['cost_company_name' => '武汉巨量星图科技有限公司', 'media_name' => '星图1-短视频'];
  48. # 保存修改记录,默认自动审核通过
  49. #OrderEditRecords::addRecord($id, $old, $after, $op_reason);
  50. #Order::where('id', $id)->update($after);
  51. # 按照修改的字段分别更新不同表数据
  52. #OrderIncome::where('order_id', $id)->update($after);
  53. #OrderCost::where('order_id', $id)->update($after);
  54. #OrderMcn::where('order_id', $id)->update($after);
  55. }
  56. return self::returnValue(['修改完成']);
  57. }
  58. public function deleteStatements(Request $request)
  59. {
  60. $statementsId = $request->input('id');
  61. \DB::beginTransaction();
  62. try{
  63. # 查询结算单基础信息
  64. $statementsInfo = Statements::query()
  65. ->where('id', $statementsId)
  66. ->where('enable', 1)
  67. ->first();
  68. if(empty($statementsInfo)) {
  69. return self::returnValue(['error' => '结算单已删除,无需重复操作'], 1102);
  70. }
  71. # 查询结算单盖章信息
  72. $search['statements_id'] = $statementsId;
  73. $statementsSealInfo = StatementsSeal::getStatementsSealInfoBySearch($search, ['id', 'verify_status', 'seal_status', 'file_status']);
  74. if(!empty($statementsSealInfo)) {
  75. if(1 == $statementsSealInfo->seal_status || 1 == $statementsSealInfo->file_status) {
  76. return self::returnValue(['error' => '结算单已标记盖章或归档,请取消标记后操作'], 1102);
  77. }
  78. StatementsSeal::updateData([$statementsSealInfo->id], ['enable' => 0]);
  79. }
  80. # 用来判断是否需要更新发票状态字段
  81. $invoiceFlag = false;
  82. # 查询结算单对应的发票信息
  83. $customerInvoicesInfo = CustomerInvoices::query()
  84. ->where('statements_id', $statementsId)
  85. ->where('enable', 1)
  86. ->first();
  87. if(!empty($customerInvoicesInfo)) {
  88. $invoiceFlag = true;
  89. if($customerInvoicesInfo->is_invoiced == 1) {
  90. return self::returnValue(['error' => '已经标记开票,请取消标记后走系统发票作废流程'], 1102);
  91. }
  92. if($customerInvoicesInfo->status == 4) {
  93. return self::returnValue(['error' =>'发票已审核通过,请走系统发票作废流程'], 1102);
  94. }
  95. # 处理发票相关信息
  96. $invId = $customerInvoicesInfo->id;
  97. CustomerInvoices::query()
  98. ->where('id', $invId)
  99. ->update(['enable' => 0, 'is_show' => 0]);
  100. CustomerInvoiceOrders::query()
  101. ->where('inv_id', $invId)
  102. ->update(['enable' => 0, 'is_show' => 0]);
  103. }
  104. $statementsType = $statementsInfo->type;
  105. $isMcn = false;
  106. switch($statementsType) {
  107. # 拼多多
  108. case 6:
  109. $orderIdList = PddStatementsDetail::query()
  110. ->where('statements_id', $statementsId)
  111. ->get()
  112. ->toArray();
  113. PddStatementsDetail::query()
  114. ->where('statements_id', $statementsId)
  115. ->where('enable', 1)
  116. ->update(['enable' => 0]);
  117. break;
  118. case 5:
  119. $orderIdList = GameStatementsDetail::query()
  120. ->where('statements_id', $statementsId)
  121. ->get()
  122. ->toArray();
  123. GameStatementsDetail::query()
  124. ->where('statements_id', $statementsId)
  125. ->where('enable', 1)
  126. ->update(['enable' => 0]);
  127. break;
  128. case 4:
  129. $orderIdList = CPAStatementsDetail::query()
  130. ->where('statements_id', $statementsId)
  131. ->get()
  132. ->toArray();
  133. CPAStatementsDetail::query()
  134. ->where('statements_id', $statementsId)
  135. ->where('enable', 1)
  136. ->update(['enable' => 0]);
  137. break;
  138. case 2:
  139. $orderIdList = McnStatementsDetail::query()
  140. ->where('statements_id', $statementsId)
  141. ->get()
  142. ->toArray();
  143. $isMcn = true;
  144. McnStatementsDetail::query()
  145. ->where('statements_id', $statementsId)
  146. ->where('enable', 1)
  147. ->update(['enable' => 0]);
  148. break;
  149. default:
  150. return self::returnValue(['error' => '结算单类型不存在'], 1102);
  151. break;
  152. }
  153. $orderIdList = array_column($orderIdList, 'order_id');
  154. Statements::query()
  155. ->where('id', $statementsId)
  156. ->update(['enable' => 0]);
  157. $updateStatus = ['statements_status' => 0];
  158. if($invoiceFlag) {
  159. $updateStatus['invoice_status'] = 0;
  160. }
  161. if($isMcn) {
  162. OrderMcn::query()
  163. ->whereIn('order_id', $orderIdList)
  164. ->update($updateStatus);
  165. } else {
  166. OrderIncome::query()
  167. ->whereIn('order_id', $orderIdList)
  168. ->update($updateStatus);
  169. }
  170. \DB::commit();
  171. return self::returnValue(true, 0);
  172. } catch (\Exception $exception) {
  173. \DB::rollBack();
  174. return self::returnValue(false, 500, $exception->getFile() . '(' . $exception->getLine() . '):' . $exception->getMessage());
  175. }
  176. }
  177. public static function getEditRecords(Request $request)
  178. {
  179. $monthStr = $request->input('month_str');
  180. $monthList = explode(',', $monthStr);
  181. $editRecordList = OrderEditRecords::query()
  182. ->where('status', '>', 0)
  183. ->whereIn('month', $monthList)
  184. ->where('is_show', 1)
  185. ->get()
  186. ->toArray();
  187. $result = [];
  188. return self::returnValue($result);
  189. foreach($editRecordList as $value) {
  190. if(0 == $value['is_batch']) {
  191. $afterData = json_decode($value['after_data'], 1);
  192. foreach($afterData as $key=>$data) {
  193. $result[$key] = isset($result[$key]) ? $result[$key] + 1 : 1;
  194. }
  195. } else {
  196. $result['final_amount'] = isset($result['final_amount']) ? $result['final_amount'] + 1 : 1;
  197. }
  198. }
  199. return self::returnValue($result);
  200. }
  201. public static function preAuditRecordSave(Request $request)
  202. {
  203. $validator = \Validator::make($request->all(), [
  204. 'type' => 'required|in:1,2,3,4,5,6',
  205. 'data_id_list' => 'required|array',
  206. 'status' => 'required|int',
  207. 'content' => 'nullable|string',
  208. ]);
  209. if ($validator->fails()) {
  210. return self::returnValue($validator->getMessageBag(), 1102);
  211. }
  212. $type = $request->input('type');
  213. $dataIdList = $request->input('data_id_list');
  214. $status = $request->input('status');
  215. $content = $request->input('content');
  216. $errcode = 0;
  217. ToolService::preAuditRecordSave($type, $dataIdList, $status, $content, $errcode);
  218. return self::returnValue('', $errcode);
  219. }
  220. public static function getPreAuditRecord(Request $request)
  221. {
  222. $validator = \Validator::make($request->all(), [
  223. 'type' => 'required|in:1,2,3,4,5,6',
  224. 'data_id' => 'required|int',
  225. ]);
  226. if ($validator->fails()) {
  227. return self::returnValue($validator->getMessageBag(), 1102);
  228. }
  229. $type = $request->input('type');
  230. $dataId = $request->input('data_id');
  231. $data = ToolService::getPreAuditRecord($type, [$dataId]);
  232. $res = isset($data[0]) ? $data[0] : [];
  233. return self::returnValue($res);
  234. }
  235. # 根据回款日期获取订单月份
  236. public static function getMonthListByPaymentDate(Request $request) {
  237. }
  238. }