git -C '/home/opc/rocketa.git' show 13d79e5 -- app/Console/Commands/GetResultDatasTest.phpcommit 13d79e5dee41987780133ced01b3e3cc2349b1c9
Author: Satoshi Ujihara <satoshi_ujihara@fivegate.jp>
Date: Wed Nov 5 13:17:46 2025 +0900
テストセンター機能実装
diff --git a/app/Console/Commands/GetResultDatasTest.php b/app/Console/Commands/GetResultDatasTest.php
new file mode 100644
index 0000000..146aa5f
--- /dev/null
+++ b/app/Console/Commands/GetResultDatasTest.php
@@ -0,0 +1,1900 @@
+<?php
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+
+use Carbon\Carbon;
+use Log;
+use Validator;
+use App\Http\Controllers\Controller;
+use GuzzleHttp\Client;
+use App\Http\Controllers\Traits\Common;
+use App\Http\Controllers\Traits\HistoryUpsert;
+use App\Http\Controllers\Traits\Budget;
+use Illuminate\Support\Facades\Mail;
+use App\Mail\budgetOverDecision;
+use App\Mail\budgetOverDecisionMedia;
+use Symfony\Component\HttpFoundation\IpUtils;
+
+use App\Models\Api\ApiAdDatas;
+use App\Models\Api\ApiResultRecordsTest;
+use App\Models\Api\ApiClickRecordsTest;
+use App\Models\La\AdDatas;
+use App\Models\La\AdJoinMediaDatas;
+use App\Models\La\AdRewardDatas;
+use App\Models\La\ClientDatas;
+use App\Models\La\ClientIpList;
+use App\Models\La\MediaDatas;
+use App\Models\La\ResultDatasTest;
+use App\Models\La\ResultErrorDatasTest;
+use App\Models\La\AdRewardProductDatas;
+use App\Models\La\AdRewardGroupDatas;
+use App\Models\La\AdRewardStageDatas;
+use App\Models\La\ResultApplicationDatas;
+use App\Models\La\MediaNoticeFailedDatasTest;
+use App\Models\La\ReceivingPeriodTargetDatasTest;
+
+
+class GetResultDatasTest extends Command
+{
+ use Common;
+ use Budget;
+ use HistoryUpsert;
+
+ protected $signature = 'get_result_data_tests {insert_id}';
+
+ protected $description = '成果反映ロジック(テスト)';
+
+ /**
+ * Create a new controller instance.
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * 成果データのエラーチェック、パートナーへ成果通信
+ *
+ * error種別
+ * 1:パラメーターエラー
+ * 2:sid不一致
+ * 3:広告不一致
+ * 4:クライアントチェックエラー
+ * 5:送信元チェックエラー
+ * 6:パートナーチェックエラー
+ * 7:未連携パートナー
+ * 8:リピートチェックエラー
+ * 9:報酬額設定ミス
+ * 10:通信エラー
+ * 11:退会識別パラメータ重複エラー
+ * 12:App通信 イベント不一致
+ * 13:IPアドレス重複リピートチェックエラー
+ * 14:成果重複エラー
+ * 15:ステータス停止エラー
+ * 16:期間外承認エラー
+ */
+ public function handle()
+ {
+ $insert_id = $this->argument('insert_id');
+ if(!$insert_id)exit;
+ // lambdaのresult_datasから未繁栄のデータを取得
+ $result_records = ApiResultRecordsTest::select()
+ ->where('migration_status', 0)
+ ->where('id', $insert_id)
+ ->get();
+
+ foreach ($result_records as $result_record) {
+ // 変数初期値
+ $insert_result_data = [
+ 'status' => 0,
+ 'ad_id' => $result_record['ad_id'],
+ 'client_id' => $result_record['client_id'],
+ 'media_id' => 0,
+ 'media_uid' => null,
+ 'sid' => $result_record['sid'],
+ 'net_reward' => 0,
+ 'gross_reward' => 0,
+ 'client_uid' => $result_record['uid'],
+ 'client_uid2' => $result_record['uid2'],
+ 'client_product_code' => $result_record['product_code'],
+ 'client_amount' => $result_record['amount'],
+ 'client_sales_count' => $result_record['sales_count'],
+ 'client_stage' => $result_record['stage'],
+ 'client_unsubscribe_value' => null,
+ 'banner_id' => null,
+ 'user_ip' => null,
+ 'user_agent' => null,
+ 'user_referer' => null,
+ 'client_ip' => $result_record['ip'],
+ 'client_domain' => $result_record['domain'],
+ 'client_parameters' => $result_record['parameters'],
+ 'click_at' => null,
+ 'fixed_at' => null,
+ 'created_ym' => date('Ym'),
+ ];
+ $now_date_time = date('Y-m-d H:i:s');
+ $now_months = date('Y-m-01');
+ $notice_at = $result_record['created_at'];
+ $error = 0;
+ $error_etc_data = [];
+ $sid_created_ym = '';
+ parse_str($result_record['parameters'], $parameters);
+ $application_data = false;
+ $media_other_parameters = [];
+ $receiving_period_target_data = false;
+ $insert_receiving_data = [];
+
+ // http対応
+ if (isset($parameters['original_ip'])) {
+ $not_ssl_validate = ClientIpList::select()
+ ->where('not_ssl', 1)
+ ->where('client_id', $result_record['client_id'])
+ ->where('ip', $parameters['original_ip'])
+ ->first();
+
+ if ($not_ssl_validate || $parameters['original_ip'] == '118.238.4.197') {
+ $result_record['ip'] = $parameters['original_ip'];
+ } else {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 5);
+ continue;
+ }
+ }
+
+ // デジマース対応(仮にデジマースがhttps対応した場合削除する)
+ // if ($result_record['ip'] == '118.238.4.197' && $result_record['client_id'] == 1) {
+ // $result_record['ip'] = '218.216.135.93';
+ // }
+
+ // 必須パラメータチェック
+ if (!$result_record['sid'] || !$result_record['ad_id'] || !$result_record['client_id']) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+
+ }
+
+ // sid フォーマットチェック
+ if ($result_record['sid']) {
+ if (mb_strlen($result_record['sid']) != 30) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+
+ }
+
+ if (preg_match("/^[a-z0-9]+$/", $result_record['sid'])) {
+ $sid_created_ym = substr($result_record['sid'], 16, 6);
+
+ } else {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+
+ }
+ }
+
+ // stage と product_code が同時に投げられてきた場合エラー
+ if ($result_record['stage'] && $result_record['product_code']) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ }
+
+ // sidからクリックデータ取得
+ $click_record = ApiClickRecordsTest::select(
+ 'id',
+ 'media_id',
+ 'uid',
+ 'banner_id',
+ 'ip',
+ 'user_agent',
+ 'referer',
+ 'net_price',
+ 'net_price_unit',
+ 'gross_price',
+ 'gross_price_unit',
+ 'other_parameters',
+ 'created_at'
+ )
+ ->where('created_ym', $sid_created_ym)
+ ->where('sid', $result_record['sid'])
+ ->where('master_ad_id', $result_record['ad_id'])
+ ->where('client_id', $result_record['client_id'])
+ ->first();
+
+ if ($click_record) {
+ $insert_result_data['media_id'] = $click_record['media_id'];
+ $insert_result_data['media_uid'] = $click_record['uid'];
+ $insert_result_data['banner_id'] = $click_record['banner_id'];
+ $insert_result_data['user_ip'] = $click_record['ip'];
+ $insert_result_data['user_agent'] = $click_record['user_agent'];
+ $insert_result_data['user_referer'] = $click_record['referer'];
+ $insert_result_data['click_at'] = $click_record['created_at'];
+
+ if ($click_record['other_parameters']) {
+ parse_str($click_record['other_parameters'], $media_other_parameters);
+ }
+
+ } else {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 2);
+ continue;
+
+ }
+
+ // 案件データ取得
+ $ad_data = AdDatas::select(
+ 'id',
+ 'name',
+ 'url',
+ 'result_method',
+ 'result_domain',
+ 'appsflyer_event_title',
+ 'adjust_token',
+ 'airbridge_event_value',
+ 'tyrads_event',
+ 'ayet_studios_install',
+ 'ayet_studios_event',
+ 'torox_install',
+ 'torox_event',
+ 'appricot_ads_event',
+ 'singular_event',
+ 'app_approval_wait',
+ 'receiving_period',
+ 'receiving_period_decision',
+ 'repeat_criteria',
+ 'repeat_unlock_date',
+ 'repeat_ip_check',
+ 'result_type',
+ 'unsubscribe_key',
+ 'status',
+ 'valid_start_date',
+ 'valid_end_date',
+ 'absolute_result'
+ )
+ ->where('id', $result_record['ad_id'])
+ ->where('client_id', $result_record['client_id'])
+ ->first();
+
+ if ($ad_data) {
+ if (
+ $ad_data['result_type'] == 2 ||
+ ($ad_data['status'] == 1 && $ad_data['valid_start_date'] <= $now_date_time && $ad_data['valid_end_date'] > $now_date_time) ||
+ $ad_data['absolute_result'] == 1 ||
+ (($ad_data['result_method'] == 3 || $ad_data['result_method'] == 4 || $ad_data['result_method'] == 6 || $ad_data['result_method'] == 7 || $ad_data['result_method'] == 8 || $ad_data['result_method'] == 9 || $ad_data['result_method'] == 10) && $ad_data['app_approval_wait'] == 1)
+ ) {
+ $result_data = ResultDatasTest::select()
+ ->where('ad_id', $result_record['ad_id'])
+ ->where('sid', $result_record['sid'])
+ ->orderBy('id', 'DESC')
+ ->first();
+
+ // タグ成果(ソケット)の時は複数回飛んでこないのでエラー
+ if ($ad_data['result_method'] == 2) {
+ if ($result_data) {
+ $this->insertResultErrorDatas($result_record, 14);
+ continue;
+ }
+ }
+
+ if ($ad_data['result_type'] == 2 && $result_record['sender'] != 3) {
+ if (!isset($parameters['status']) || $parameters['status'] == '') {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+
+ } elseif ($parameters['status'] != 0 && $parameters['status'] != 1 && $parameters['status'] != 2) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+
+ }
+
+ if ($result_data) {
+ // 承認からは動かせない
+ if ($result_data->status == 1) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ }
+
+ // 否認から承認待ちに変更は不可
+ if ($result_data->status == 2 && $parameters['status'] == 0) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ }
+
+ // ステータスの更新なし
+ if ($result_data->status == $parameters['status']) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ }
+ }
+
+ $insert_result_data['status'] = $parameters['status'];
+
+ // if ($result_record['client_id'] == 25 || $result_record['client_id'] == 87) {
+ // if ($parameters['status'] == 2) {
+ // $insert_result_data['status'] = 1;
+ // } elseif ($parameters['status'] == 3) {
+ // $insert_result_data['status'] = 2;
+ // } else {
+ // $insert_result_data['status'] = 0;
+ // }
+ // } else {
+ // $insert_result_data['status'] = $parameters['status'];
+ // }
+ //client依存処理削除↑
+ $insert_result_data['status'] = $parameters['status'];
+
+ } else {
+ if ($result_record['sender'] == 3) {
+ $insert_result_data['status'] = 0;
+ } else {
+ $insert_result_data['status'] = $ad_data['result_type'];
+ }
+ }
+
+ // 期間外の承認が来た場合は新たにエラーに落とす処理の追加
+ // 将来的にシステム化するが、今は急場しのぎでベタで対応
+ // if ($result_record['ad_id'] == 2041 || $result_record['ad_id'] == 2040 || $result_record['ad_id'] == 2039 || $result_record['ad_id'] == 2017) {
+ // if ($notice_at >= '2025-02-13 00:00:00') {
+ // if ($insert_result_data['status'] == 1) {
+ // // result_error_datas登録
+ // $this->insertResultErrorDatas($result_record, 16);
+ // continue;
+ // }
+ // }
+ // }
+
+ if ($insert_result_data['status'] == 1 || $insert_result_data['status'] == 2) {
+ $insert_result_data['fixed_at'] = $result_record['created_at'];
+ }
+
+ if ($ad_data['unsubscribe_key']) {
+ // parse_str($result_record['parameters'], $parameters);
+
+ if ($parameters[$ad_data['unsubscribe_key']]) {
+ $unsubscribe_value_check = ResultDatasTest::select()
+ ->where('ad_id', $result_record['ad_id'])
+ ->where('client_id', $result_record['client_id'])
+ ->where('client_unsubscribe_value', $parameters[$ad_data['unsubscribe_key']])
+ ->first();
+
+ if (!$unsubscribe_value_check) {
+ $insert_result_data['client_unsubscribe_value'] = $parameters[$ad_data['unsubscribe_key']];
+ } else {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 11);
+ continue;
+
+ }
+ } else {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+
+ }
+ }
+ } else {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 15);
+ continue;
+ }
+ } else {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 3);
+ continue;
+
+ }
+
+ // クライアントチェック
+ $client_datas = ClientDatas::select(['id'])
+ ->where('id', $result_record['client_id'])
+ ->get();
+
+ if (!$client_datas) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 4);
+ continue;
+ }
+
+ // 送信元チェック
+ $sender_validate = 0;
+ if ($result_record['sender'] == 1) {
+ // AppsFlyerのipを参照する
+ if ($ad_data['result_method'] == 3) {
+ if (in_array($result_record['ip'], \Config::get('apps_flyer_ip.list'))) {
+ $sender_validate = 1;
+ }
+
+ // リピートはやらないとのこと、この時点で重複データはエラー
+ if ($result_data && $result_data->status == 1) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ }
+
+ // イベントチェック
+ if (isset($ad_data['appsflyer_event_title']) && $ad_data['appsflyer_event_title'] != '') {
+
+ // インストール時
+ if (!isset($parameters['event']) || $parameters['event'] == '') {
+ // result_error_datas登録
+ if ($ad_data['app_approval_wait']) {
+ // 何かしらの成果データが有った場合は承認待ちデータはエラー
+ if ($result_data) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+
+ } else {
+ $insert_result_data['status'] = 0;
+ $insert_result_data['fixed_at'] = null;
+ }
+
+ } else {
+ $this->updateApiResultRecords($result_record->id, 98);
+ continue;
+
+ }
+
+ } elseif ($ad_data['appsflyer_event_title'] != $parameters['event']) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+
+ }
+ } else {
+ if (isset($parameters['event']) && $parameters['event'] != '') {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+
+ }
+ }
+
+ // Adjustのipを参照する
+ } elseif ($ad_data['result_method'] == 4) {
+ // if (in_array($result_record['ip'], \Config::get('adjust_ip.list'))) {
+ if (IpUtils::checkIp($result_record['ip'], \Config::get('adjust_ip.list'))) {
+ $sender_validate = 1;
+ }
+
+ // リピートはやらないとのこと、この時点で重複データはエラー
+ if ($result_data && $result_data->status == 1) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ }
+
+ if (isset($parameters['adjust_id']) && $parameters['adjust_id'] != '') {
+ $insert_result_data['client_uid'] = $parameters['adjust_id'];
+ $result_record['uid'] = $parameters['adjust_id'];
+
+ }
+
+ if (isset($parameters['device_id']) && $parameters['device_id'] != '') {
+ $insert_result_data['client_uid2'] = $parameters['device_id'];
+ $result_record['uid2'] = $parameters['device_id'];
+
+ }
+
+ // イベントチェック
+ if (isset($ad_data['adjust_token']) && $ad_data['adjust_token'] != '') {
+
+ // インストール時
+ if (!isset($parameters['adjust_token']) || $parameters['adjust_token'] == '') {
+ // result_error_datas登録
+ if ($ad_data['app_approval_wait']) {
+ // 何かしらの成果データが有った場合は承認待ちデータはエラー
+ if ($result_data) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+
+ } else {
+ $insert_result_data['status'] = 0;
+ $insert_result_data['fixed_at'] = null;
+ }
+
+ } else {
+ $this->updateApiResultRecords($result_record->id, 98);
+ continue;
+
+ }
+
+ } elseif ($ad_data['adjust_token'] != $parameters['adjust_token']) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+
+ }
+ } else {
+ if (isset($parameters['adjust_token']) && $parameters['adjust_token'] != '') {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+
+ }
+ }
+
+ // Airbridgeのipを参照する
+ } elseif ($ad_data['result_method'] == 5) {
+ // test環境でMMPのIPチェックはしない
+ // if (in_array($result_record['ip'], $this->getAirbridgeIps())) {
+ $sender_validate = 1;
+ // }
+ // イベントチェック
+ if (!isset($parameters['event_name'])) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+
+ } elseif ($ad_data['airbridge_event_value'] != $parameters['event_name']) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+ }
+ } elseif ($ad_data['result_method'] == 6) {
+ if (in_array($result_record['ip'], \Config::get('tyrads_ip.list'))) {
+ $sender_validate = 1;
+ }
+
+ // リピートはやらないとのこと、この時点で重複データはエラー
+ if ($result_data && $result_data->status == 1) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ }
+
+ // eventは何かしら入ってくるとのこと
+ if (!isset($parameters['event']) || $parameters['event'] == '') {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ } else {
+ // 一番後ろに半角スペースが入っている事象があったため
+ $parameters['event'] = trim($parameters['event']);
+ }
+
+ // イベントチェック
+ if ($ad_data['app_approval_wait']) {
+ if (isset($ad_data['tyrads_event']) && $ad_data['tyrads_event'] != '') {
+ if ($parameters['event'] == 'Install') {
+ $insert_result_data['status'] = 0;
+ $insert_result_data['fixed_at'] = null;
+
+ } elseif ($ad_data['tyrads_event'] == $parameters['event']) {
+ $insert_result_data['status'] = 1;
+
+ } else {
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+
+ }
+ } else {
+ if ($parameters['event'] == 'Install') {
+ $insert_result_data['status'] = 1;
+
+ } else {
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+
+ }
+ }
+ } else {
+ if (isset($ad_data['tyrads_event']) && $ad_data['tyrads_event'] != '') {
+ if ($ad_data['tyrads_event'] == $parameters['event']) {
+ $insert_result_data['status'] = 1;
+ $insert_result_data['fixed_at'] = null;
+
+ } else {
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+
+ }
+ } else {
+ if ($parameters['event'] == 'Install') {
+ $insert_result_data['status'] = 1;
+ $insert_result_data['fixed_at'] = null;
+
+ } else {
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+
+ }
+ }
+ }
+ } elseif ($ad_data['result_method'] == 7) {
+ if (in_array($result_record['ip'], \Config::get('ayet_studios_ip.list'))) {
+ $sender_validate = 1;
+ }
+
+ // リピートはやらないとのこと、この時点で重複データはエラー
+ if ($result_data && $result_data->status == 1) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ }
+
+ // eventは何かしら入ってくるとのこと
+ if (!isset($parameters['event']) || $parameters['event'] == '') {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ }
+
+ // イベントチェック
+ /* 個別処理
+ * 使わなくなったら削除
+ * 削除後は INSTALLATION_TRACKED を書き換えるのを忘れない事
+ */
+ $ayet_studios_default_event = 'INSTALLATION_TRACKED';
+
+ if ($result_record['ad_id'] == 2315) {
+ $ayet_studios_default_event = 'Tutorial_complete';
+ }
+
+ if (isset($ad_data['ayet_studios_install']) && $ad_data['ayet_studios_install'] != '') {
+ $ayet_studios_default_event = $ad_data['ayet_studios_install'];
+ }
+
+ if (isset($ad_data['ayet_studios_event']) && $ad_data['ayet_studios_event'] != '') {
+ // インストール時
+ if ($parameters['event'] == $ayet_studios_default_event) {
+ // result_error_datas登録
+ if ($ad_data['app_approval_wait']) {
+ // 何かしらの成果データが有った場合は承認待ちデータはエラー
+ if ($result_data) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+
+ } else {
+ $insert_result_data['status'] = 0;
+ $insert_result_data['fixed_at'] = null;
+ }
+ } else {
+ $this->updateApiResultRecords($result_record->id, 98);
+ continue;
+
+ }
+
+ } elseif ($ad_data['ayet_studios_event'] != $parameters['event']) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+
+ }
+ } else {
+ if ($parameters['event'] != $ayet_studios_default_event) {
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+
+ }
+ }
+
+ } elseif ($ad_data['result_method'] == 8) {
+ if (in_array($result_record['ip'], \Config::get('torox_ip.list'))) {
+ $sender_validate = 1;
+ }
+
+ // リピートはやらないとのこと、この時点で重複データはエラー
+ if ($result_data && $result_data->status == 1) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ }
+
+ // eventは何かしら入ってくるとのこと
+ if (!isset($parameters['event']) || $parameters['event'] == '') {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ }
+
+ // イベントチェック
+ if($ad_data['torox_event'] == $parameters['event']) {
+ $insert_result_data['status'] = 1;
+ } elseif ($ad_data['app_approval_wait']) {
+ if ($ad_data['torox_install'] == $parameters['event']) {
+ $insert_result_data['status'] = 0;
+ $insert_result_data['fixed_at'] = null;
+ } else {
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+ }
+ } else {
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+ }
+
+ } elseif ($ad_data['result_method'] == 9) {
+ if (in_array($result_record['ip'], \Config::get('appricot_ads_ip.list'))) {
+ $sender_validate = 1;
+ }
+
+ // リピートはやらないとのこと、この時点で重複データはエラー
+ if ($result_data && $result_data->status == 1) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ }
+
+ // eventは何かしら入ってくるとのこと
+ if (!isset($parameters['event']) || $parameters['event'] == '') {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ }
+
+ // イベントチェック
+ if (isset($ad_data['appricot_ads_event']) && $ad_data['appricot_ads_event'] != '') {
+ // インストール時
+ if ($parameters['event'] == '1') {
+ // result_error_datas登録
+ if ($ad_data['app_approval_wait']) {
+ // 何かしらの成果データが有った場合は承認待ちデータはエラー
+ if ($result_data) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+
+ } else {
+ $insert_result_data['status'] = 0;
+ $insert_result_data['fixed_at'] = null;
+ }
+ } else {
+ $this->updateApiResultRecords($result_record->id, 98);
+ continue;
+
+ }
+
+ } elseif ($ad_data['appricot_ads_event'] != $parameters['event']) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+
+ }
+ } else {
+ if (isset($parameters['event']) && $parameters['event'] != '1') {
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+
+ }
+ }
+
+ } elseif ($ad_data['result_method'] == 10) {
+ if (IpUtils::checkIp($result_record['ip'], \Config::get('singular_ip.list'))) {
+ $sender_validate = 1;
+ }
+
+ // リピートはやらないとのこと、この時点で重複データはエラー
+ if ($result_data && $result_data->status == 1) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+ }
+
+ // イベントチェック
+ if (isset($ad_data['singular_event']) && $ad_data['singular_event'] != '') {
+ // インストール時
+ if (!isset($parameters['event']) || !$parameters['event']) {
+ if ($ad_data['app_approval_wait']) {
+ // 何かしらの成果データが有った場合は承認待ちデータはエラー
+ if ($result_data) {
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+
+ } else {
+ $insert_result_data['status'] = 0;
+ $insert_result_data['fixed_at'] = null;
+ }
+ } else {
+ $this->updateApiResultRecords($result_record->id, 98);
+ continue;
+ }
+ } elseif ($ad_data['singular_event'] != $parameters['event']) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+ }
+ } else {
+ if (isset($parameters['event']) && $parameters['event']) {
+ $this->insertResultErrorDatas($result_record, 12);
+ continue;
+ }
+ }
+
+ } else {
+ $client_ip_array = [];
+ $client_ips = ClientIpList::select('ip')
+ ->where('client_id', $result_record['client_id'])
+ ->get();
+
+ if ($client_ips) {
+ foreach ($client_ips as $client_ip) {
+ $client_ip_array[] = $client_ip['ip'];
+ }
+
+ if (IpUtils::checkIp($result_record['ip'], $client_ip_array)) {
+ $sender_validate = 1;
+ }
+ }
+ }
+
+ } elseif ($result_record['sender'] == 2 && $result_record['domain']) {
+ if ($ad_data['result_domain'] == $result_record['domain']) {
+ $sender_validate = 1;
+ }
+
+ // 手動成果処理
+ } elseif ($result_record['sender'] == 3 && isset($parameters['app_id']) && $parameters['app_id']) {
+ $application_data = ResultApplicationDatas::find($parameters['app_id']);
+
+ if ($application_data) {
+ if ($application_data['sid'] == $result_record['sid']) {
+ if ($application_data['click_id'] == $click_record['id']) {
+ $sender_validate = 1;
+
+ $insert_result_data['status'] = $application_data['status'];
+ $insert_result_data['client_amount'] = $application_data['amount'];
+ $insert_result_data['memo'] = $application_data['memo'];
+
+ if ($application_data['click_at']) {
+ $insert_result_data['click_at'] = $application_data['click_at'];
+ }
+
+ if ($application_data['notice_at']) {
+ $notice_at = $application_data['notice_at'];
+
+ if ($application_data['status'] == 1 || $application_data['status'] == 2) {
+ $insert_result_data['fixed_at'] = $application_data['notice_at'];
+
+ }
+ }
+
+ $result_record['amount'] = $application_data['amount'];
+
+ }
+ }
+ }
+ }
+
+ // ad_id:1935 の個別処理
+ // 掲載が終了になり次第削除
+ // if ($result_record['ad_id'] == 1935) {
+ // if (isset($result_record['amount']) && $result_record['amount'] > 0) {
+ // $result_record['amount'] = (int)ceil($result_record['amount'] / (1 + (\Config::get('const.tax') / 100)));
+ // $insert_result_data['client_amount'] = $result_record['amount'];
+ // }
+ // }
+ if ($sender_validate == 0) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 5);
+ continue;
+ }
+
+ // パートナーチェック
+ $media_data = MediaDatas::select(['api_url_test'])
+ ->where('id', $click_record['media_id'])
+ ->first();
+ if (!$media_data) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 6);
+ continue;
+
+ }
+
+ $ad_join_media_data = AdJoinMediaDatas::select(['id'])
+ ->where('status', 1)
+ ->where('media_id', $click_record['media_id'])
+ ->where('ad_id', $result_record['ad_id'])
+ ->first();
+ if (!$ad_join_media_data) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 7);
+ continue;
+
+ }
+
+ // 案件利用済み、リピートチェック
+ if ($ad_data['result_type'] != 2 || !$result_data) {
+ // クライアントuid重複チェック
+ $repeat_validate = 0;
+ $target_months = '';
+
+ if ($result_record['uid']) {
+ if ($ad_data['result_method'] == 3 || $ad_data['result_method'] == 4 || $ad_data['result_method'] == 6 || $ad_data['result_method'] == 7 || $ad_data['result_method'] == 8 || $ad_data['result_method'] == 9 || $ad_data['result_method'] == 10) {
+ $repeat_check_result_data = ResultDatasTest::select('created_at')
+ ->where('ad_id', $result_record['ad_id'])
+ ->where('client_uid', $result_record['uid'])
+ ->where('status', 1)
+ ->orderBy('id', 'DESC')
+ ->first();
+
+ } else {
+ $repeat_check_result_data = ResultDatasTest::select('created_at')
+ ->where('ad_id', $result_record['ad_id'])
+ ->where('client_uid', $result_record['uid'])
+ ->where(function($query) {
+ $query->where('status', 1)
+ ->orWhere('status', 0);
+ })->orderBy('id', 'DESC')
+ ->first();
+
+ }
+
+ if ($repeat_check_result_data) {
+ if ($ad_data['repeat_criteria'] == 2) { // 制限なし
+ $repeat_validate = 1;
+
+ } elseif ($ad_data['repeat_criteria'] == 3) { // 同月内NG
+ $target_month = date('Y-m-01', strtotime($repeat_check_result_data['created_at']));
+ if ($target_month != $now_months) {
+ $repeat_validate = 1;
+ }
+
+ } elseif ($ad_data['repeat_criteria'] == 4) { //
+ $target_months = date('Y-m-01', strtotime(date('Y-m-01', strtotime($repeat_check_result_data['created_at'])).' +1 months'));
+ if ($target_months < $now_months) {
+ $repeat_validate = 1;
+ }
+
+ } elseif ($ad_data['repeat_criteria'] == 5) { // 3ヶ月NG
+ $target_months = date('Y-m-01', strtotime(date('Y-m-01', strtotime($repeat_check_result_data['created_at'])).' +2 months'));
+ if ($target_months < $now_months) {
+ $repeat_validate = 1;
+ }
+
+ } elseif ($ad_data['repeat_criteria'] == 6) { // 手動追加
+ if ($ad_data['repeat_unlock_date'] >= $repeat_check_result_data['created_at']) {
+ $repeat_validate = 1;
+ }
+ }
+ } else {
+ $repeat_validate = 1;
+ }
+
+ if ($repeat_validate == 0) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 8);
+ continue;
+ }
+ }
+
+ // ユーザー IPアドレス重複チェック
+ $repeat_validate = 0;
+ $target_months = '';
+
+ if ($ad_data['repeat_ip_check'] == 1) {
+ $repeat_check_result_data = ResultDatasTest::select('created_at')
+ ->where('ad_id', $result_record['ad_id'])
+ ->where(function($query) {
+ $query->where('status', 1)
+ ->orWhere('status', 0);
+ })
+ ->where('user_ip', $click_record['ip'])
+ ->orderBy('id', 'DESC')
+ ->first();
+
+ if ($repeat_check_result_data) {
+ if ($ad_data['repeat_criteria'] == 2) { // 制限なし
+ $repeat_validate = 1;
+
+ } elseif ($ad_data['repeat_criteria'] == 3) { // 同月内NG
+ $target_month = date('Y-m-01', strtotime($repeat_check_result_data['created_at']));
+ if ($target_month != $now_months) {
+ $repeat_validate = 1;
+ }
+
+ } elseif ($ad_data['repeat_criteria'] == 4) { // 2ヶ月NG
+ $target_months = date('Y-m-01', strtotime(date('Y-m-01', strtotime($repeat_check_result_data['created_at'])).' +1 months'));
+ if ($target_months < $now_months) {
+ $repeat_validate = 1;
+ }
+
+ } elseif ($ad_data['repeat_criteria'] == 5) { // 3ヶ月NG
+ $target_months = date('Y-m-01', strtotime(date('Y-m-01', strtotime($repeat_check_result_data['created_at'])).' +2 months'));
+ if ($target_months < $now_months) {
+ $repeat_validate = 1;
+ }
+
+ } elseif ($ad_data['repeat_criteria'] == 6) { // 手動追加
+ if ($ad_data['repeat_unlock_date'] >= $repeat_check_result_data['created_at']) {
+ $repeat_validate = 1;
+ }
+ }
+ } else {
+ $repeat_validate = 1;
+ }
+
+ if ($repeat_validate == 0) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 13);
+ continue;
+ }
+ }
+ }
+
+ // 報酬額取得
+ $ad_reward_data = [];
+
+ // 手動成果は強制的に入力値
+ if ($result_record['sender'] == 3) {
+ // インカムへ投げるときは必ず net_price = gross_price にする
+ if ($click_record['media_id'] == 1) {
+ $ad_reward_data = [
+ 'net_price' => $application_data['gross_price'],
+ 'net_price_unit' => $application_data['gross_price_unit'],
+ 'gross_price' => $application_data['gross_price'],
+ 'gross_price_unit' => $application_data['gross_price_unit']
+ ];
+ } else {
+ $ad_reward_data = [
+ 'net_price' => $application_data['net_price'],
+ 'net_price_unit' => $application_data['net_price_unit'],
+ 'gross_price' => $application_data['gross_price'],
+ 'gross_price_unit' => $application_data['gross_price_unit']
+ ];
+ }
+
+ } elseif ($result_record['product_code']) {
+ $media_id = $click_record['media_id'];
+ $query = AdRewardProductDatas::query();
+ $query->select(
+ 'ad_reward_datas.net_price',
+ 'ad_reward_datas.net_price_unit',
+ 'ad_reward_datas.gross_price',
+ 'ad_reward_datas.gross_price_unit',
+ 'ad_reward_datas.media_id',
+ 'ad_reward_datas.group_id'
+ )
+ ->join('ad_reward_datas', 'ad_reward_product_datas.id', '=', 'ad_reward_datas.product_id')
+ ->where('ad_reward_datas.type', 1)
+ ->where('ad_reward_product_datas.status', 1)
+ ->where('ad_reward_product_datas.ad_id', $result_record['ad_id'])
+ ->where('ad_reward_product_datas.code', $result_record['product_code'])
+ ->where('ad_reward_product_datas.valid_start_date', '<=', date('Y-m-d H:i:s'))
+ ->where('ad_reward_product_datas.valid_end_date', '>=', date('Y-m-d H:i:s'));
+ $query = $query->where(function($query) use($media_id) {
+ $query->where('ad_reward_datas.media_id', $media_id)
+ ->orWhereNull('ad_reward_datas.media_id');
+ });
+ $target_product_datas = $query->orderBy('ad_reward_datas.group_id', 'DESC')
+ ->get();
+
+ if ($target_product_datas && count($target_product_datas) > 0) {
+ foreach ($target_product_datas as $target_product_data) {
+ if ($target_product_data->group_id != NULL) {
+ $target_group_datas = AdRewardGroupDatas::select('id')
+ ->where('id', $target_product_data->group_id)
+ ->where('status', 1)
+ ->where('ad_id', $result_record['ad_id'])
+ ->where('valid_start_date', '<=', date('Y-m-d H:i:s'))
+ ->where('valid_end_date', '>=', date('Y-m-d H:i:s'))
+ ->first();
+
+ if ($target_group_datas) {
+ $ad_reward_data = [
+ 'net_price' => $target_product_data['net_price'],
+ 'net_price_unit' => $target_product_data['net_price_unit'],
+ 'gross_price' => $target_product_data['gross_price'],
+ 'gross_price_unit' => $target_product_data['gross_price_unit']
+ ];
+
+ break;
+ }
+
+ } else {
+ $ad_reward_data = [
+ 'net_price' => $target_product_data['net_price'],
+ 'net_price_unit' => $target_product_data['net_price_unit'],
+ 'gross_price' => $target_product_data['gross_price'],
+ 'gross_price_unit' => $target_product_data['gross_price_unit']
+ ];
+ }
+ }
+
+ if (!$ad_reward_data) {
+ // 通常成果
+ $target_reward_datas = AdRewardDatas::select(
+ 'net_price',
+ 'net_price_unit',
+ 'gross_price',
+ 'gross_price_unit',
+ 'stage_id',
+ 'group_id'
+ )
+ ->where('type', 1)
+ ->where('ad_id', $result_record['ad_id'])
+ ->where('stage_id', NULL)
+ ->where('product_id', NULL)
+ ->where(function ($query) use($click_record) {
+ $query->where('media_id', $click_record['media_id'])
+ ->orWhere('media_id', NULL);
+ })
+ ->orderBy('group_id', 'DESC')
+ ->get();
+
+ if ($target_reward_datas) {
+ foreach ($target_reward_datas as $target_reward_data) {
+ if ($target_reward_data->group_id != NULL) {
+
+ $target_group_datas = AdRewardGroupDatas::select('id')
+ ->where('id', $target_reward_data->group_id)
+ ->where('status', 1)
+ ->where('ad_id', $result_record['ad_id'])
+ ->where('valid_start_date', '<=', date('Y-m-d H:i:s'))
+ ->where('valid_end_date', '>=', date('Y-m-d H:i:s'))
+ ->first();
+
+ if ($target_group_datas) {
+ $ad_reward_data = [
+ 'net_price' => $target_reward_data['net_price'],
+ 'net_price_unit' => $target_reward_data['net_price_unit'],
+ 'gross_price' => $target_reward_data['gross_price'],
+ 'gross_price_unit' => $target_reward_data['gross_price_unit']
+ ];
+
+ break;
+ }
+ } else {
+ $ad_reward_data = [
+ 'net_price' => $target_reward_data['net_price'],
+ 'net_price_unit' => $target_reward_data['net_price_unit'],
+ 'gross_price' => $target_reward_data['gross_price'],
+ 'gross_price_unit' => $target_reward_data['gross_price_unit']
+ ];
+
+ }
+ }
+ }
+ }
+ } else {
+ // 通常成果
+ $target_reward_datas = AdRewardDatas::select(
+ 'net_price',
+ 'net_price_unit',
+ 'gross_price',
+ 'gross_price_unit',
+ 'stage_id',
+ 'group_id'
+ )
+ ->where('type', 1)
+ ->where('ad_id', $result_record['ad_id'])
+ ->where('stage_id', NULL)
+ ->where('product_id', NULL)
+ ->where(function ($query) use($click_record) {
+ $query->where('media_id', $click_record['media_id'])
+ ->orWhere('media_id', NULL);
+ })
+ ->orderBy('group_id', 'DESC')
+ ->get();
+
+ if ($target_reward_datas) {
+ foreach ($target_reward_datas as $target_reward_data) {
+ if ($target_reward_data->group_id != NULL) {
+
+ $target_group_datas = AdRewardGroupDatas::select('id')
+ ->where('id', $target_reward_data->group_id)
+ ->where('status', 1)
+ ->where('ad_id', $result_record['ad_id'])
+ ->where('valid_start_date', '<=', date('Y-m-d H:i:s'))
+ ->where('valid_end_date', '>=', date('Y-m-d H:i:s'))
+ ->first();
+
+ if ($target_group_datas) {
+ $ad_reward_data = [
+ 'net_price' => $target_reward_data['net_price'],
+ 'net_price_unit' => $target_reward_data['net_price_unit'],
+ 'gross_price' => $target_reward_data['gross_price'],
+ 'gross_price_unit' => $target_reward_data['gross_price_unit']
+ ];
+
+ break;
+ }
+ } else {
+ $ad_reward_data = [
+ 'net_price' => $target_reward_data['net_price'],
+ 'net_price_unit' => $target_reward_data['net_price_unit'],
+ 'gross_price' => $target_reward_data['gross_price'],
+ 'gross_price_unit' => $target_reward_data['gross_price_unit']
+ ];
+
+ }
+ }
+ }
+ }
+ } elseif ($result_record['stage']) {
+ $target_reward_datas = AdRewardStageDatas::select(
+ 'ad_reward_datas.net_price',
+ 'ad_reward_datas.net_price_unit',
+ 'ad_reward_datas.gross_price',
+ 'ad_reward_datas.gross_price_unit',
+ 'ad_reward_datas.group_id'
+ )
+ ->join('ad_reward_datas', 'ad_reward_stage_datas.id', '=', 'ad_reward_datas.stage_id')
+ ->where('ad_reward_datas.type', 1)
+ ->where('ad_reward_stage_datas.status', 1)
+ ->where('ad_reward_stage_datas.ad_id', $result_record['ad_id'])
+ ->where('ad_reward_stage_datas.id', $result_record['stage'])
+ ->where('ad_reward_stage_datas.valid_start_date', '<=', date('Y-m-d H:i:s'))
+ ->where('ad_reward_stage_datas.valid_end_date', '>=', date('Y-m-d H:i:s'))
+ ->where(function ($query) use($click_record) {
+ $query->where('ad_reward_datas.media_id', $click_record['media_id'])
+ ->orWhere('ad_reward_datas.media_id', NULL);
+ })
+ ->orderBy('ad_reward_datas.group_id', 'DESC')
+ ->get();
+
+ if ($target_reward_datas) {
+ foreach ($target_reward_datas as $target_reward_data) {
+ if ($target_reward_data->group_id != NULL) {
+
+ $target_group_datas = AdRewardGroupDatas::select('id')
+ ->where('id', $target_reward_data->group_id)
+ ->where('status', 1)
+ ->where('ad_id', $result_record['ad_id'])
+ ->where('valid_start_date', '<=', date('Y-m-d H:i:s'))
+ ->where('valid_end_date', '>=', date('Y-m-d H:i:s'))
+ ->first();
+
+ if ($target_group_datas) {
+ $ad_reward_data = [
+ 'net_price' => $target_reward_data['net_price'],
+ 'net_price_unit' => $target_reward_data['net_price_unit'],
+ 'gross_price' => $target_reward_data['gross_price'],
+ 'gross_price_unit' => $target_reward_data['gross_price_unit']
+ ];
+
+ break;
+ }
+ } else {
+ $ad_reward_data = [
+ 'net_price' => $target_reward_data['net_price'],
+ 'net_price_unit' => $target_reward_data['net_price_unit'],
+ 'gross_price' => $target_reward_data['gross_price'],
+ 'gross_price_unit' => $target_reward_data['gross_price_unit']
+ ];
+
+ }
+ }
+ }
+
+ } else {
+ // 通常成果
+ $target_reward_datas = AdRewardDatas::select(
+ 'net_price',
+ 'net_price_unit',
+ 'gross_price',
+ 'gross_price_unit',
+ 'stage_id',
+ 'group_id'
+ )
+ ->where('type', 1)
+ ->where('ad_id', $result_record['ad_id'])
+ ->where('stage_id', NULL)
+ ->where('product_id', NULL)
+ ->where(function ($query) use($click_record) {
+ $query->where('media_id', $click_record['media_id'])
+ ->orWhere('media_id', NULL);
+ })
+ ->orderBy('group_id', 'DESC')
+ ->get();
+
+ if ($target_reward_datas) {
+ foreach ($target_reward_datas as $target_reward_data) {
+ if ($target_reward_data->group_id != NULL) {
+
+ $target_group_datas = AdRewardGroupDatas::select('id')
+ ->where('id', $target_reward_data->group_id)
+ ->where('status', 1)
+ ->where('ad_id', $result_record['ad_id'])
+ ->where('valid_start_date', '<=', date('Y-m-d H:i:s'))
+ ->where('valid_end_date', '>=', date('Y-m-d H:i:s'))
+ ->first();
+
+ if ($target_group_datas) {
+ $ad_reward_data = [
+ 'net_price' => $target_reward_data['net_price'],
+ 'net_price_unit' => $target_reward_data['net_price_unit'],
+ 'gross_price' => $target_reward_data['gross_price'],
+ 'gross_price_unit' => $target_reward_data['gross_price_unit']
+ ];
+
+ break;
+ }
+ } else {
+ $ad_reward_data = [
+ 'net_price' => $target_reward_data['net_price'],
+ 'net_price_unit' => $target_reward_data['net_price_unit'],
+ 'gross_price' => $target_reward_data['gross_price'],
+ 'gross_price_unit' => $target_reward_data['gross_price_unit']
+ ];
+
+ }
+ }
+ }
+ }
+
+ if ($ad_data['result_type'] == 2 && $result_data) {
+ $insert_result_data['net_reward'] = $result_data->net_reward;
+ $insert_result_data['gross_reward'] = $result_data->gross_reward;
+
+ } elseif (($ad_data['result_method'] == 3 || $ad_data['result_method'] == 4 || $ad_data['result_method'] == 6 || $ad_data['result_method'] == 7 || $ad_data['result_method'] == 8 || $ad_data['result_method'] == 9 || $ad_data['result_method'] == 10) && $ad_data['app_approval_wait'] == 1 && $result_data) { // 長くなってしまうのであえて上記の条件と別にしました
+ $insert_result_data['net_reward'] = $result_data->net_reward;
+ $insert_result_data['gross_reward'] = $result_data->gross_reward;
+
+ } elseif ($ad_reward_data) {
+ if ($ad_reward_data['net_price_unit'] == 2 || $ad_reward_data['gross_price_unit'] == 2) {
+ if (!$result_record['amount']) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 1);
+ continue;
+
+ }
+ }
+
+ if ($ad_reward_data['net_price_unit'] == 1) {
+ $insert_result_data['net_reward'] = $ad_reward_data['net_price'];
+
+ } elseif ($ad_reward_data['net_price_unit'] == 2) {
+ $insert_result_data['net_reward'] = floor(($result_record['amount'] / 100) * $ad_reward_data['net_price']);
+
+ }
+
+ if ($ad_reward_data['gross_price_unit'] == 1) {
+ $insert_result_data['gross_reward'] = $ad_reward_data['gross_price'];
+
+ } elseif ($ad_reward_data['gross_price_unit'] == 2) {
+ $insert_result_data['gross_reward'] = floor(($result_record['amount'] / 100) * $ad_reward_data['gross_price']);
+
+ }
+
+ // パートナーがインカムだった時はnet_rewardをgross_rewardと同じ値に書き換え
+ // if ($insert_result_data['media_id'] == 1) {
+ // $insert_result_data['net_reward'] = $insert_result_data['gross_reward'];
+ // }
+ } else {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 9);
+ continue;
+
+ }
+
+ // 成果受け取り期間設定のデータチェック
+ if ($result_data && $result_data->status == 2) {
+ $receiving_period_target_data = ReceivingPeriodTargetDatas::select()
+ ->where('result_datas_id', $result_data['id'])
+ ->first();
+
+ if ($receiving_period_target_data && $receiving_period_target_data->receiving_period_decision == 3) {
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 16);
+ continue;
+ }
+ }
+
+ \DB::beginTransaction();
+ try {
+ // rocket-a-laのresult_datasに登録
+ if (($ad_data['result_type'] == 2 || $ad_data['result_method'] == 3 || $ad_data['result_method'] == 4 || $ad_data['result_method'] == 6 || $ad_data['result_method'] == 7 || $ad_data['result_method'] == 8 || $ad_data['result_method'] == 9 || $ad_data['result_method'] == 10) && $result_data) {
+ // 変わる可能性のある部分のみアップデート
+ $target_result_data = ResultDatasTest::find($result_data->id);
+ $target_result_data->status = $insert_result_data['status'];
+ $target_result_data->net_reward = $insert_result_data['net_reward'];
+ $target_result_data->gross_reward = $insert_result_data['gross_reward'];
+ $target_result_data->client_amount = $insert_result_data['client_amount'];
+ if ($insert_result_data['status'] == 1 || $insert_result_data['status'] == 2) {
+ $target_result_data->fixed_at = $now_date_time;
+ }
+ $target_result_data->save();
+
+ $new_result_data = ResultDatasTest::find($result_data->id);
+ } else {
+ $new_result_data = ResultDatasTest::create($insert_result_data);
+
+ if ($new_result_data) {
+ $new_result_data = ResultDatasTest::find($new_result_data->id);
+ }
+ }
+
+ if (!$new_result_data) {
+ throw new \Exception('Error:'.$result_record['id']);
+ }
+
+ // 成果受け取り期間が設定されている広告の処理
+ if ($ad_data->receiving_period != 0 && $insert_result_data['status'] == 0 && !$receiving_period_target_data) {
+ $insert_receiving_data = [
+ 'result_datas_id' => $new_result_data->id,
+ 'result_records_id' => $result_record->id,
+ 'ad_id' => $ad_data->id,
+ 'sid' => $result_record->sid,
+ 'receiving_period' => $ad_data->receiving_period,
+ 'receiving_period_decision' => $ad_data->receiving_period_decision,
+ 'is_resolved' => 0
+ ];
+
+ $new_receiving_data = ReceivingPeriodTargetDatasTest::create($insert_receiving_data);
+ }
+ // データ移行済みフラグ更新
+ $target_result_record = $this->updateApiResultRecords($result_record['id'], $insert_result_data['status']);
+
+ if (!$target_result_record) {
+ throw new \Exception('Error:'.$result_record['id']);
+ }
+
+ \DB::commit();
+ } catch (\Exception $e) {
+
+ \Log::error($e);
+ echo $e->getMessage()."\n";
+
+ \DB::rollBack();
+
+ // result_error_datas登録
+ $this->insertResultErrorDatas($result_record, 10);
+ continue;
+
+ }
+
+ // 成果返却
+ if ($application_data && !$application_data['media_notice']) {
+ continue;
+ }
+
+ $query = [];
+ // $url = explode('?', $media_data['api_url']);
+ // $path = $url[0];
+ // if (isset($url[1])) {
+ // parse_str($url[1], $query);
+ // }
+
+ // パートナー独自パラメータ追加
+ if ($media_other_parameters) {
+ foreach ($media_other_parameters as $media_other_key => $media_other_value) {
+ $query[$media_other_key] = $media_other_value;
+ }
+ }
+ /*
+ // GMO個別処理
+ if ($click_record['media_id'] == 3) {
+ $query['u1'] = $insert_result_data['sid'];
+ $query['amount'] = $insert_result_data['client_amount'];
+ $query['status'] = $insert_result_data['status'];
+ $query['s'] = $insert_result_data['media_uid'];
+ $query['eaid'] = $insert_result_data['ad_id'];
+ $query['pieces'] = $insert_result_data['client_sales_count'];
+
+ // ちょびリッチ個別処理
+ } elseif ($click_record['media_id'] == 8) {
+ $query['ad_id'] = $insert_result_data['ad_id'];
+ $query['media_uid'] = $insert_result_data['media_uid'];
+ $query['sid'] = $insert_result_data['sid'];
+ $query['amount'] = $insert_result_data['client_amount'];
+ $query['reward'] = $insert_result_data['net_reward'];
+ $query['count'] = $insert_result_data['client_sales_count'];
+ $query['status'] = $insert_result_data['status'];
+ $query['date'] = $notice_at;
+ $query['stage'] = $insert_result_data['client_stage'];
+
+ $query['action_date'] = $notice_at;
+ if ($result_data) {
+ $query['action_date'] = $result_data->created_at;
+ } elseif ($new_result_data) {
+ $query['action_date'] = $new_result_data->created_at;
+ }
+
+ // SKYFLAG個別処理
+ } elseif ($click_record['media_id'] == 16) {
+ $query['ad_id'] = $insert_result_data['ad_id'];
+ $query['_xuid'] = $insert_result_data['media_uid'];
+ $query['_buid'] = $insert_result_data['sid'];
+ $query['amount'] = $insert_result_data['client_amount'];
+ $query['reward'] = $insert_result_data['net_reward'];
+ $query['count'] = $insert_result_data['client_sales_count'];
+ $query['_status'] = $insert_result_data['status'];
+ $query['date'] = $notice_at;
+ $query['event_frmsdk'] = $insert_result_data['client_stage'];
+
+ // オズビジョン個別処理
+ } elseif ($click_record['media_id'] == 20) {
+ $query['ad_id'] = $insert_result_data['ad_id'];
+ $query['media_uid'] = $insert_result_data['media_uid'];
+ $query['oid'] = $insert_result_data['sid'];
+ $query['amount'] = $insert_result_data['client_amount'];
+ $query['reward'] = $insert_result_data['net_reward'];
+ $query['count'] = $insert_result_data['client_sales_count'];
+ $query['status'] = $insert_result_data['status'];
+ $query['date'] = $notice_at;
+ $query['stage'] = $insert_result_data['client_stage'];
+ $query['cap'] = 'c01k06czpmzm4ysjmdzc28ckvdr_phkzww5p9npb';
+
+ // ギミックス個別処理
+ } elseif ($click_record['media_id'] == 30) {
+ $query['ad_id'] = $insert_result_data['ad_id'];
+ $query['media_uid'] = $insert_result_data['media_uid'];
+ $query['sid'] = $insert_result_data['sid'];
+ $query['amount'] = $insert_result_data['client_amount'];
+ $query['reward'] = $insert_result_data['net_reward'];
+ $query['count'] = $insert_result_data['client_sales_count'];
+ $query['date'] = $notice_at;
+ $query['stage'] = $insert_result_data['client_stage'];
+
+ if ($insert_result_data['status'] == 1) {
+ $query['status'] = 2;
+
+ } elseif ($insert_result_data['status'] == 2) {
+ $query['status'] = 3;
+
+ } else {
+ $query['status'] = 1;
+
+ }
+
+ // affitown個別処理
+ } elseif ($click_record['media_id'] == 35) {
+ $query['ad_id'] = $insert_result_data['ad_id'];
+ $query['af'] = $insert_result_data['media_uid'];
+ $query['uid'] = $insert_result_data['sid'];
+ $query['amount'] = $insert_result_data['client_amount'];
+ $query['reward'] = $insert_result_data['net_reward'];
+ $query['count'] = $insert_result_data['client_sales_count'];
+ $query['date'] = $notice_at;
+ $query['stage'] = $insert_result_data['client_stage'];
+
+ if ($insert_result_data['status'] == 1) {
+ $query['status'] = 2;
+
+ } elseif ($insert_result_data['status'] == 2) {
+ $query['status'] = 3;
+
+ } else {
+ $query['status'] = 1;
+
+ }
+
+ // CyberAgent個別処理
+ } elseif ($click_record['media_id'] == 38) {
+ $query['uid'] = $insert_result_data['sid'];
+ $query['pid'] = 1;
+ $query['adid'] = $insert_result_data['ad_id'];
+ $query['amount'] = $insert_result_data['client_amount'];
+
+ $cyber_agent_hash = \Config::get('media_param.cyber_agent_hash_key');
+ $hased_ky = hash('sha256', $query['adid'] . ':' . $query['cid'] . ':' . $query['af'] . ':' . $cyber_agent_hash);
+ $query['ky'] = $hased_ky;
+
+ if ($result_data) {
+ $path = 'https://ad.mobadme.jp/ac/action_admit.php';
+ if ($insert_result_data['status'] == 1) {
+ $query['deny'] = 0;
+ } else {
+ $query['deny'] = 1;
+ }
+ }
+
+ // ヴィーナスウォーカー個別処理
+ } elseif ($click_record['media_id'] == 48) {
+ $query['ad_id'] = $insert_result_data['ad_id'];
+ $query['s'] = $insert_result_data['media_uid'];
+ $query['oid'] = $insert_result_data['sid'];
+ $query['m'] = $insert_result_data['client_amount'];
+ $query['reward'] = $insert_result_data['net_reward'];
+ $query['count'] = $insert_result_data['client_sales_count'];
+ $query['status'] = $insert_result_data['status'];
+ $query['date'] = $notice_at;
+ $query['stage'] = $insert_result_data['client_stage'];
+
+ // 株式会社zinRai個別処理
+ } elseif ($click_record['media_id'] == 55) {
+ $query['ad_id'] = $insert_result_data['ad_id'];
+ $query['cid'] = $insert_result_data['media_uid'];
+ $query['args'] = $insert_result_data['sid'];
+ $query['price'] = $insert_result_data['client_amount'];
+ $query['did'] = $insert_result_data['sid'];
+ $query['reward'] = $insert_result_data['net_reward'];
+ $query['state'] = $insert_result_data['status'];
+ $query['date'] = $notice_at;
+
+ // 株式会社DIGITALIO(ECナビ)個別処理
+ } elseif ($click_record['media_id'] == 56) {
+ $query['ad_id'] = $insert_result_data['ad_id'];
+ $query['media_uid'] = $insert_result_data['media_uid'];
+ $query['sid'] = $insert_result_data['sid'];
+ $query['sid2'] = $insert_result_data['sid'];
+ $query['amount'] = $insert_result_data['net_reward'];
+
+ if ($insert_result_data['status'] == 1) {
+ $query['status'] = 2;
+
+ } elseif ($insert_result_data['status'] == 2) {
+ $query['status'] = 3;
+
+ } else {
+ $query['status'] = 1;
+
+ }
+
+ // AD.TRACK(アドトラック)個別処理
+ } elseif ($click_record['media_id'] == 61) {
+ $query['ad_id'] = $insert_result_data['ad_id'];
+ $query['_xuid'] = $insert_result_data['media_uid'];
+ $query['_buid'] = $insert_result_data['sid'];
+ $query['price'] = $insert_result_data['client_amount'];
+ $query['reward'] = $insert_result_data['net_reward'];
+ $query['count'] = $insert_result_data['client_sales_count'];
+ $query['status'] = $insert_result_data['status'];
+ $query['date'] = $notice_at;
+ $query['stage'] = $insert_result_data['client_stage'];
+
+ } else {
+ $query['ad_id'] = $insert_result_data['ad_id'];
+ $query['media_uid'] = $insert_result_data['media_uid'];
+ $query['sid'] = $insert_result_data['sid'];
+ $query['amount'] = $insert_result_data['client_amount'];
+ $query['reward'] = $insert_result_data['net_reward'];
+ $query['count'] = $insert_result_data['client_sales_count'];
+ $query['status'] = $insert_result_data['status'];
+ $query['date'] = $notice_at;
+ $query['stage'] = $insert_result_data['client_stage'];
+
+ }
+ */
+ //上のelseの部分
+ $query['ad_id'] = $insert_result_data['ad_id'];
+ $query['media_uid'] = $insert_result_data['media_uid'];
+ $query['sid'] = $insert_result_data['sid'];
+ $query['amount'] = $insert_result_data['client_amount'];
+ $query['reward'] = $insert_result_data['net_reward'];
+ $query['count'] = $insert_result_data['client_sales_count'];
+ $query['status'] = $insert_result_data['status'];
+ $query['date'] = $notice_at;
+ $query['stage'] = $insert_result_data['client_stage'];
+
+ $replaced_url = preg_replace_callback('/\{(\w+)\}/', function ($matches) use ($query) {
+ $key = $matches[1];
+ return isset($query[$key]) ? $query[$key] : ''; // 未設定は空文字
+ }, $media_data['api_url_test']); //test環境限定
+
+
+ $url = explode('?', $replaced_url);
+ $path = $url[0];
+ if (isset($url[1])) {
+ parse_str($url[1], $send_query);
+ }
+
+ try {
+ $client = new Client();
+ $response = $client->get($path, [
+ 'query' => $send_query,
+ 'http_errors' => false,
+ ]);
+ $response_result = $response->getStatusCode();
+
+ if ($response_result == 200) {
+ $target_data = ResultDatasTest::findOrFail($new_result_data['id']);
+ $target_data->media_achievement = 1;
+ $target_data->save();
+ } else {
+ throw new \Exception('Error:'.$new_result_data['id'].' ResponseStatus:'.$response_result);
+ }
+ } catch (\Exception $e) {
+ $response_status = substr($e->getMessage(), -3);
+
+ if (!$this->isIntMatch($response_status)) {
+ $response_status = Null;
+ }
+
+ // $failed_url = $path.'?';
+ // foreach ($query as $key => $param) {
+ // if (!$param || $param == '') {
+ // continue;
+ // }
+
+ // $failed_url .= $key.'='.$param.'&';
+ // }
+ // $failed_url = substr($failed_url, 0, -1);
+
+ MediaNoticeFailedDatasTest::create([
+ 'result_datas_id' => $new_result_data->id,
+ 'ad_id' => $new_result_data->ad_id,
+ 'client_id' => $new_result_data->client_id,
+ 'media_id' => $new_result_data->media_id,
+ 'media_uid' => $new_result_data->media_uid,
+ 'sid' => $new_result_data->sid,
+ 'url' => $replaced_url,
+ 'response_status' => $response_status
+ ]);
+
+ $target_new_result_data = ResultDatasTest::find($new_result_data->id);
+ $target_new_result_data->media_achievement = 0;
+ $target_new_result_data->save();
+
+ \Log::error($e);
+ echo $e->getMessage()."\n";
+ }
+
+ /*本番環境と違いテストなので予算はなしで。
+ // 予算チェック
+ $budget_over = 0;
+ $budget_fix_ad_data = $this->getBudgetRelationDataByAdId($result_record['ad_id']);
+ if ($budget_fix_ad_data) {
+ if (!$budget_fix_ad_data->budget_start_date || ($budget_fix_ad_data->budget_start_date && $now_date_time >= $budget_fix_ad_data->budget_start_date)) {
+ // 期間
+ if ($budget_fix_ad_data->budget_end_date) {
+ if ($now_date_time >= $budget_fix_ad_data->budget_end_date) {
+ // 完全終了
+ $budget_over = 1;
+ }
+ }
+
+ // 全体
+ if ($budget_fix_ad_data->budget && !$budget_over) {
+ $result = $this->getBudgetSumResult($budget_fix_ad_data->id, $budget_fix_ad_data->result_type, $budget_fix_ad_data->budget_unit, $budget_fix_ad_data->budget_start_date, 1);
+
+ if ($budget_fix_ad_data->budget <= (int)$result) {
+ $budget_over = 1;
+ }
+ }
+
+ // 月別
+ if ($budget_fix_ad_data->budget_monthly_limit && !$budget_over) {
+ $result = $this->getBudgetSumResult($budget_fix_ad_data->id, $budget_fix_ad_data->result_type, $budget_fix_ad_data->budget_monthly_limit_unit, $budget_fix_ad_data->budget_start_date, 2);
+
+ if ($budget_fix_ad_data->budget_monthly_limit <= (int)$result) {
+ $budget_over = 2;
+ }
+ }
+
+ // 日別
+ if ($budget_fix_ad_data->budget_daily_limit && !$budget_over) {
+ $result = $this->getBudgetSumResult($budget_fix_ad_data->id, $budget_fix_ad_data->result_type, $budget_fix_ad_data->budget_daily_limit_unit, $budget_fix_ad_data->budget_start_date, 3);
+
+ if ($budget_fix_ad_data->budget_daily_limit <= (int)$result) {
+ $budget_over = 3;
+ }
+ }
+ }
+ }
+
+ if ($budget_over) {
+ if ($budget_over == 1) { // 全体
+ AdDatas::where('id', $result_record['ad_id'])
+ ->update([
+ 'status' => 0,
+ 'budget' => null,
+ 'budget_unit' => 1,
+ 'budget_daily_limit' => null,
+ 'budget_daily_limit_unit' => 1,
+ 'budget_start_date' => null,
+ 'budget_end_date' => null,
+ ]);
+
+ $update_history_title = '予算超過';
+
+ Mail::to(['ujihara@aixinc.io', 'satoshi_ujihara@fivegate.jp'])
+ ->send(new budgetOverDecision($ad_data));
+
+ // 予算終了時 アラートメールを送信するパートナー
+ $alert_send_media_datas = $this->getBudgetAlertMailSendMedias($ad_data->id);
+ if ($alert_send_media_datas && count($alert_send_media_datas) > 0) {
+ foreach ($alert_send_media_datas as $alert_send_media_data) {
+ Mail::to([$alert_send_media_data->mail_address])
+ ->send(new budgetOverDecisionMedia($ad_data, $alert_send_media_data->media_id, 1));
+ }
+ }
+
+ } elseif ($budget_over == 2) { // 月別
+ AdDatas::where('id', $result_record['ad_id'])
+ ->update([
+ 'status' => 0
+ ]);
+
+ $update_history_title = 'マンスリー予算超過';
+
+ Mail::to(['ujihara@aixinc.io', 'satoshi_ujihara@fivegate.jp'])
+ ->send(new budgetOverDecision($ad_data, 2));
+
+ // 予算終了時 アラートメールを送信するパートナー
+ $alert_send_media_datas = $this->getBudgetAlertMailSendMedias($ad_data->id);
+ if ($alert_send_media_datas && count($alert_send_media_datas) > 0) {
+ foreach ($alert_send_media_datas as $alert_send_media_data) {
+ Mail::to([$alert_send_media_data->mail_address])
+ ->send(new budgetOverDecisionMedia($ad_data, $alert_send_media_data->media_id, 2));
+ }
+ }
+ } elseif ($budget_over == 3) { // 日別
+ AdDatas::where('id', $result_record['ad_id'])
+ ->update([
+ 'status' => 0
+ ]);
+
+ $update_history_title = 'デイリー予算超過';
+
+ Mail::to(['ujihara@aixinc.io', 'satoshi_ujihara@fivegate.jp'])
+ ->send(new budgetOverDecision($ad_data, 3));
+
+ // 予算終了時 アラートメールを送信するパートナー
+ $alert_send_media_datas = $this->getBudgetAlertMailSendMedias($ad_data->id);
+ if ($alert_send_media_datas && count($alert_send_media_datas) > 0) {
+ foreach ($alert_send_media_datas as $alert_send_media_data) {
+ Mail::to([$alert_send_media_data->mail_address])
+ ->send(new budgetOverDecisionMedia($ad_data, $alert_send_media_data->media_id, 3));
+ }
+ }
+ }
+
+ ApiAdDatas::where('master_ad_id', $result_record['ad_id'])
+ ->update([
+ 'status' => 0
+ ]);
+
+ // 更新履歴に書き込む
+ $this->adDatasUpdateHistory($result_record['ad_id'], $update_history_title);
+ }
+ */
+ }
+ }
+
+ // 反映済フラグを更新する
+ public function updateApiResultRecords ($id, $status)
+ {
+ $target_data = ApiResultRecordsTest::findOrFail($id);
+ $target_data->status = $status;
+ $target_data->migration_status = 1;
+ $target_data->save();
+
+ return $target_data;
+ }
+
+ // エラーテーブルに記録
+ public function insertResultErrorDatas($data, $error)
+ {
+
+ // echo "<pre>";
+ // debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
+ // echo "</pre>";
+ // exit;
+
+ $sid_created_ym = substr($data['sid'], 16, 6);
+
+ $click_data = ApiClickRecordsTest::select(
+ 'media_id',
+ 'banner_id',
+ 'uid'
+ )
+ ->where('created_ym', $sid_created_ym)
+ ->where('sid', $data['sid'])
+ ->first();
+
+ $error_data = [
+ 'result_records_id' => $data['id'],
+ 'error' => $error,
+ 'ip' => $data['ip'],
+ 'sid' => $data['sid'],
+ 'ad_id' => $data['ad_id'],
+ 'client_id' => $data['client_id'],
+ 'uid' => $data['uid'],
+ 'uid2' => $data['uid2'],
+ 'product_code' => $data['product_code'],
+ 'amount' => $data['amount'],
+ 'sales_count' => $data['sales_count'],
+ 'stage' => $data['stage'],
+ 'domain' => $data['domain'],
+ 'sender' => $data['sender'],
+ 'media_id' => $click_data['media_id'],
+ 'media_uid' => $click_data['uid'],
+ 'banner_id' => $click_data['banner_id'],
+ 'parameters' => $data['parameters'],
+ 'created_ym' => date('Ym')
+ ];
+
+ $new_result_data = ResultErrorDatasTest::create($error_data);
+
+ $this->updateApiResultRecords($data['id'], 99);
+ }
+
+ // Airbridge 成果返却IPアドレス
+ private function getAirbridgeIps()
+ {
+ return [
+ '58.81.70.242', //fg
+ '18.177.210.124',
+ '18.182.81.54',
+ '3.113.116.246',
+ '52.196.176.123',
+ '54.150.6.139',
+ '61.74.194.15',
+ '52.199.189.116',
+ ];
+ }
+}