新版订单消耗系统

CustomerInvoicesComplement.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2021/5/25
  6. * Time: 15:31
  7. */
  8. namespace App\Models;
  9. use Illuminate\Database\Eloquent\Model;
  10. class CustomerInvoicesComplement extends Model
  11. {
  12. protected $table = 'customer_invoices_complement';
  13. public static function statisticalData($month)
  14. {
  15. //处理日期
  16. $startDate = $month;
  17. $endDate = date('Y-m-01', strtotime($month.' +1 month'));
  18. //查询数据
  19. $dataList = CustomerInvoicesComplement::where('invoiced_time', '>=', $startDate)
  20. ->where('invoiced_time', '<', $endDate)->where('enable', 1)
  21. ->where('status', 4)->where('is_invoiced', 1)->get()->toArray();
  22. //查询结算单信息
  23. $statementsIdList = array_column($dataList, 'statements_id');
  24. $mcnStatementsList = StatementsComplement::whereIn('id', $statementsIdList)->where('type', 2)
  25. ->where('enable', 1)->get()->toArray();
  26. $mcnIdList = array_column($mcnStatementsList, 'id');
  27. $mcnDataList = array_column($mcnStatementsList, null, 'id');
  28. //循环处理
  29. $insertData = [];
  30. foreach($dataList as $value) {
  31. $isMcn = false;
  32. $mcnData = [];
  33. if(in_array($value['statements_id'], $mcnIdList)) {
  34. $isMcn = true;
  35. $mcnData = $mcnDataList[$value['statements_id']];
  36. }
  37. $statementsDetail = StatementsComplementDetail::where('statements_id', $value['statements_id'])
  38. ->where('enable', 1)->get();
  39. foreach($statementsDetail as $v) {
  40. $item['company'] = $value['company'];
  41. $item['customer_name'] = $value['customer_name'];
  42. $item['month'] = $v->order_month.'_01';
  43. $item['salesman'] = $v->saleman;
  44. $item['project_name'] = $v->project_name;
  45. $item['media_name'] = $v->media_name;
  46. $item['launch_platform'] = !empty($v->platform_name) ? $v->platform_name : null;
  47. $item['change_month'] = $value['invoiced_time'] ? date('Y-m-01', strtotime($value['invoiced_time'])) : null;
  48. # 开票数据只汇总开票金额,订单金额导出会造成数据异常
  49. $item['amount'] = null;
  50. $item['final_amount'] = null;
  51. if($isMcn) {
  52. $item['handler'] = null;
  53. $item['mcn_handler'] = $v->handler;
  54. $item['mcn_name'] = $mcnData['party_a_name'];
  55. $item['mcn_invoice_amount'] = $v->invoice_amount;
  56. $item['mcn_invoice_date'] = $value['invoiced_time'] ? date('Y-m-d', strtotime($value['invoiced_time'])) : null;
  57. $item['invoice_num'] = null;
  58. $item['invoice_amount'] = null;
  59. $item['billing_date'] = null;
  60. $item['first_invoice_amount'] = null;
  61. $item['first_invoice_date'] = null;
  62. $item['second_invoice_amount'] = null;
  63. $item['second_invoice_date'] = null;
  64. } else {
  65. $item['handler'] = $v->handler;
  66. $item['mcn_handler'] = null;
  67. $item['mcn_name'] = null;
  68. $item['mcn_invoice_amount'] = null;
  69. $item['mcn_invoice_date'] = null;
  70. $item['invoice_num'] = $v->invoice_num;
  71. $item['invoice_amount'] = $v->invoice_amount;
  72. $item['billing_date'] = $value['invoiced_time'] ? date('Y-m-d', strtotime($value['invoiced_time'])) : null;
  73. if(1 == $v->invoice_num) {
  74. $item['first_invoice_amount'] = $v->invoice_amount;
  75. $item['first_invoice_date'] = $value['invoiced_time'] ? date('Y-m-d', strtotime($value['invoiced_time'])) : null;
  76. $item['second_invoice_amount'] = null;
  77. $item['second_invoice_date'] = null;
  78. } else if(2 == $v->invoice_num) {
  79. $item['first_invoice_amount'] = null;
  80. $item['first_invoice_date'] = null;
  81. $item['second_invoice_amount'] = $v->invoice_amount;
  82. $item['second_invoice_date'] = $value['invoiced_time'] ? date('Y-m-d', strtotime($value['invoiced_time'])) : null;
  83. }
  84. }
  85. $item['back_type'] = 4;
  86. $insertData[] = $item;
  87. }
  88. }
  89. //插入
  90. OrderBackUp::insert($insertData);
  91. return ;
  92. }
  93. }