123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2021/5/25
- * Time: 15:31
- */
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- class CustomerInvoicesComplement extends Model
- {
- protected $table = 'customer_invoices_complement';
- public static function statisticalData($month)
- {
- //处理日期
- $startDate = $month;
- $endDate = date('Y-m-01', strtotime($month.' +1 month'));
- //查询数据
- $dataList = CustomerInvoicesComplement::where('invoiced_time', '>=', $startDate)
- ->where('invoiced_time', '<', $endDate)->where('enable', 1)
- ->where('status', 4)->where('is_invoiced', 1)->get()->toArray();
- //查询结算单信息
- $statementsIdList = array_column($dataList, 'statements_id');
- $mcnStatementsList = StatementsComplement::whereIn('id', $statementsIdList)->where('type', 2)
- ->where('enable', 1)->get()->toArray();
- $mcnIdList = array_column($mcnStatementsList, 'id');
- $mcnDataList = array_column($mcnStatementsList, null, 'id');
- //循环处理
- $insertData = [];
- foreach($dataList as $value) {
- $isMcn = false;
- $mcnData = [];
- if(in_array($value['statements_id'], $mcnIdList)) {
- $isMcn = true;
- $mcnData = $mcnDataList[$value['statements_id']];
- }
- $statementsDetail = StatementsComplementDetail::where('statements_id', $value['statements_id'])
- ->where('enable', 1)->get();
- foreach($statementsDetail as $v) {
- $item['company'] = $value['company'];
- $item['customer_name'] = $value['customer_name'];
- $item['month'] = $v->order_month.'_01';
- $item['salesman'] = $v->saleman;
- $item['project_name'] = $v->project_name;
- $item['media_name'] = $v->media_name;
- $item['launch_platform'] = !empty($v->platform_name) ? $v->platform_name : null;
- $item['change_month'] = $value['invoiced_time'] ? date('Y-m-01', strtotime($value['invoiced_time'])) : null;
- # 开票数据只汇总开票金额,订单金额导出会造成数据异常
- $item['amount'] = null;
- $item['final_amount'] = null;
- if($isMcn) {
- $item['handler'] = null;
- $item['mcn_handler'] = $v->handler;
- $item['mcn_name'] = $mcnData['party_a_name'];
- $item['mcn_invoice_amount'] = $v->invoice_amount;
- $item['mcn_invoice_date'] = $value['invoiced_time'] ? date('Y-m-d', strtotime($value['invoiced_time'])) : null;
- $item['invoice_num'] = null;
- $item['invoice_amount'] = null;
- $item['billing_date'] = null;
- $item['first_invoice_amount'] = null;
- $item['first_invoice_date'] = null;
- $item['second_invoice_amount'] = null;
- $item['second_invoice_date'] = null;
- } else {
- $item['handler'] = $v->handler;
- $item['mcn_handler'] = null;
- $item['mcn_name'] = null;
- $item['mcn_invoice_amount'] = null;
- $item['mcn_invoice_date'] = null;
- $item['invoice_num'] = $v->invoice_num;
- $item['invoice_amount'] = $v->invoice_amount;
- $item['billing_date'] = $value['invoiced_time'] ? date('Y-m-d', strtotime($value['invoiced_time'])) : null;
- if(1 == $v->invoice_num) {
- $item['first_invoice_amount'] = $v->invoice_amount;
- $item['first_invoice_date'] = $value['invoiced_time'] ? date('Y-m-d', strtotime($value['invoiced_time'])) : null;
- $item['second_invoice_amount'] = null;
- $item['second_invoice_date'] = null;
- } else if(2 == $v->invoice_num) {
- $item['first_invoice_amount'] = null;
- $item['first_invoice_date'] = null;
- $item['second_invoice_amount'] = $v->invoice_amount;
- $item['second_invoice_date'] = $value['invoiced_time'] ? date('Y-m-d', strtotime($value['invoiced_time'])) : null;
- }
- }
- $item['back_type'] = 4;
- $insertData[] = $item;
- }
- }
- //插入
- OrderBackUp::insert($insertData);
- return ;
- }
- }
|