init
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Advertiser;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class AdvEtcManagerModel extends Model
|
||||
{
|
||||
public function getList($searchParams)
|
||||
{
|
||||
$whereConditions = [];
|
||||
|
||||
if (!empty($searchParams['sdate'])) {
|
||||
$searchSDate = (new \DateTime($searchParams['sdate']))->format('Y-m-d');
|
||||
$whereConditions[] = "el.reg_date >= '{$searchSDate} 00:00:00'";
|
||||
}
|
||||
if (!empty($searchParams['edate'])) {
|
||||
$searchEDate = (new \DateTime($searchParams['edate']))->format('Y-m-d');
|
||||
$whereConditions[] = "el.reg_date <= '{$searchEDate} 23:59:59'";
|
||||
}
|
||||
|
||||
$whereSQL = '';
|
||||
if (!empty($whereConditions)) {
|
||||
$whereSQL = 'WHERE ' . implode(' AND ', $whereConditions);
|
||||
}
|
||||
$includeMedia = ['카카오광고','카카오비즈폼','카카오플러스친구','토스','틱톡','당근마켓','타불라','유튜브']; //표시 할 광고주
|
||||
$excludeMedia = ['유튜브', '틱톡', '토스', '타불라']; //부가세 제외 광고주
|
||||
if($searchSDate != $searchEDate) {
|
||||
$groupBy = 'GROUP BY el.event_seq, el.site';
|
||||
} else {
|
||||
$groupBy = 'GROUP BY date, el.event_seq, el.site';
|
||||
}
|
||||
$sql = "
|
||||
SELECT
|
||||
evt.*,
|
||||
IFNULL(ROUND(db.goal, 2), 0) as goal,
|
||||
IFNULL(db.unitprice, 0) as unitprice,
|
||||
IFNULL(
|
||||
CASE WHEN evt.media_name IN ('" . implode("', '", $excludeMedia) . "')
|
||||
THEN db.total_price
|
||||
ELSE db.total_price / 1.1
|
||||
END, 0
|
||||
) as total_price,
|
||||
IFNULL(ROUND(
|
||||
CASE WHEN evt.media_name IN ('" . implode("', '", $excludeMedia) . "')
|
||||
THEN db.total_price / evt.db_unique
|
||||
ELSE db.total_price / 1.1 / evt.db_unique
|
||||
END, 0
|
||||
), 0) as now_unitprice,
|
||||
IFNULL(ROUND(
|
||||
(db.unitprice - (
|
||||
CASE WHEN evt.media_name IN ('" . implode("', '", $excludeMedia) . "')
|
||||
THEN db.total_price / evt.db_unique
|
||||
ELSE db.total_price / 1.1 / evt.db_unique
|
||||
END
|
||||
)) * evt.db_unique, 0
|
||||
), 0) as profit,
|
||||
IFNULL(ROUND(
|
||||
((db.unitprice - (
|
||||
CASE WHEN evt.media_name IN ('" . implode("', '", $excludeMedia) . "')
|
||||
THEN db.total_price / evt.db_unique
|
||||
ELSE db.total_price / 1.1 / evt.db_unique
|
||||
END
|
||||
)) * evt.db_unique) / (db.unitprice * evt.db_unique) * 100, 1
|
||||
), 0) as profit_rate,
|
||||
db.confirm,
|
||||
db.reg_date
|
||||
FROM
|
||||
(
|
||||
SELECT
|
||||
ea.name as advertiser_name,
|
||||
em.media as media_name,
|
||||
ei.seq as event_seq,
|
||||
el.site,
|
||||
ei.description,
|
||||
DATE(el.reg_date) as date,
|
||||
COUNT(el.seq) as db_total,
|
||||
SUM(CASE WHEN el.status=1 THEN 1 ELSE 0 END) as db_unique,
|
||||
SUM(CASE WHEN el.status<>1 THEN 1 ELSE 0 END) as db_invalid
|
||||
FROM event_leads as el
|
||||
LEFT JOIN event_information as ei ON el.event_seq = ei.seq
|
||||
LEFT JOIN event_advertiser as ea ON ei.advertiser = ea.seq
|
||||
LEFT JOIN event_media as em ON ei.media = em.seq
|
||||
$whereSQL
|
||||
$groupBy
|
||||
) as evt
|
||||
LEFT JOIN db_price as db ON evt.event_seq = db.event_seq AND evt.site = db.site AND evt.date = db.date
|
||||
WHERE evt.db_unique > 0 AND evt.media_name IN ('" . implode("', '", $includeMedia) . "')
|
||||
ORDER BY evt.date DESC, evt.advertiser_name, evt.event_seq, evt.site
|
||||
";
|
||||
// echo $sql; exit;
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
return $query->getResult();
|
||||
}
|
||||
|
||||
public function updateOrInsertEventData($data)
|
||||
{
|
||||
$builder = $this->db->table('db_price');
|
||||
$builder->where('date', $data['date']);
|
||||
$builder->where('event_seq', $data['event_seq']);
|
||||
$builder->where('site', $data['site']);
|
||||
$exists = $builder->countAllResults(false);
|
||||
if ($exists) {
|
||||
// 데이터가 존재하면 업데이트
|
||||
return $builder->update($data);
|
||||
} else {
|
||||
// 데이터가 존재하지 않으면 삽입
|
||||
return $builder->insert($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,568 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Advertiser;
|
||||
|
||||
use App\Libraries\Calc;
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class AdvFacebookManagerModel extends Model
|
||||
{
|
||||
public function getAccounts($data)
|
||||
{
|
||||
$builder = $this->db->table('z_facebook.fb_ad_insight_history A');
|
||||
$builder->select('
|
||||
G.id AS company_id,
|
||||
G.name AS company_name
|
||||
');
|
||||
$builder->join('z_facebook.fb_ad B', 'A.ad_id = B.ad_id');
|
||||
$builder->join('z_facebook.fb_adset C', 'B.adset_id = C.adset_id');
|
||||
$builder->join('z_facebook.fb_campaign D', 'C.campaign_id = D.campaign_id');
|
||||
$builder->join('z_facebook.fb_ad_account E', 'D.account_id = E.ad_account_id');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.ad_account_id = F.ad_account_id AND F.media = "facebook"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$builder->where('A.date >=', $data['sdate']);
|
||||
$builder->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['facebookCheck'])){
|
||||
switch ($data['type']) {
|
||||
case 'campaigns':
|
||||
$builder->whereIn('D.campaign_id', $data['facebookCheck']);
|
||||
break;
|
||||
case 'adsets':
|
||||
$builder->whereIn('C.adset_id', $data['facebookCheck']);
|
||||
break;
|
||||
case 'ads':
|
||||
$builder->whereIn('B.ad_id', $data['facebookCheck']);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getMediaAccounts($data)
|
||||
{
|
||||
$builder = $this->db->table('z_facebook.fb_ad_insight_history A');
|
||||
$builder->select('
|
||||
"facebook" AS media,
|
||||
E.name AS media_account_name,
|
||||
E.ad_account_id AS media_account_id,
|
||||
E.status AS status,
|
||||
"" AS isAdminStop,
|
||||
"" AS is_exposed,
|
||||
E.db_count AS db_count,
|
||||
SUM(A.db_count) AS db_sum,
|
||||
COUNT(DISTINCT A.date) AS date_count
|
||||
');
|
||||
$builder->join('z_facebook.fb_ad B', 'A.ad_id = B.ad_id');
|
||||
$builder->join('z_facebook.fb_adset C', 'B.adset_id = C.adset_id');
|
||||
$builder->join('z_facebook.fb_campaign D', 'C.campaign_id = D.campaign_id');
|
||||
$builder->join('z_facebook.fb_ad_account E', 'D.account_id = E.ad_account_id');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.ad_account_id = F.ad_account_id AND F.media = "facebook"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$builder->where('A.date >=', $data['sdate']);
|
||||
$builder->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['facebookCheck'])){
|
||||
switch ($data['type']) {
|
||||
case 'campaigns':
|
||||
$builder->whereIn('D.campaign_id', $data['facebookCheck']);
|
||||
break;
|
||||
case 'adsets':
|
||||
$builder->whereIn('C.adset_id', $data['facebookCheck']);
|
||||
break;
|
||||
case 'ads':
|
||||
$builder->whereIn('B.ad_id', $data['facebookCheck']);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
$builder->groupBy('E.ad_account_id');
|
||||
$builder->groupBy('G.id');
|
||||
// dd($builder->getCompiledSelect());
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getOnlyAdAccount($data)
|
||||
{
|
||||
$builder = $this->db->table('z_facebook.fb_ad_account');
|
||||
$builder->select('
|
||||
"facebook" AS media,
|
||||
name AS media_account_name,
|
||||
ad_account_id AS media_account_id,
|
||||
"" AS is_exposed,
|
||||
"" AS canManageClients,
|
||||
db_count AS db_count
|
||||
');
|
||||
$builder->where('status', 1);
|
||||
$builder->where('perm', 1);
|
||||
|
||||
if(!empty($data['stx'])){
|
||||
$builder->groupStart();
|
||||
$builder->like('name', $data['stx']);
|
||||
$builder->groupEnd();
|
||||
}
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getCampaigns($data)
|
||||
{
|
||||
$subQuery = $this->db->table('z_facebook.fb_ad_insight_history A');
|
||||
$subQuery->select('
|
||||
D.account_id as customerId,
|
||||
D.campaign_id AS id,
|
||||
D.campaign_name AS name,
|
||||
D.status AS status,
|
||||
D.budget AS budget,
|
||||
D.create_date AS create_date,
|
||||
A.date AS date,
|
||||
A.hour AS hour,
|
||||
SUM(A.impressions) AS impressions,
|
||||
SUM(A.inline_link_clicks) AS click,
|
||||
SUM(A.spend) AS spend,
|
||||
SUM(A.sales) as sales,
|
||||
SUM(A.db_count) as unique_total,
|
||||
SUM(A.margin) as margin,
|
||||
IFNULL(ROUND((A.margin/A.sales)*100, 1), 0) as margin_ratio');
|
||||
$subQuery->join('z_facebook.fb_ad B', 'A.ad_id = B.ad_id');
|
||||
$subQuery->join('z_facebook.fb_adset C', 'B.adset_id = C.adset_id');
|
||||
$subQuery->join('z_facebook.fb_campaign D', 'C.campaign_id = D.campaign_id');
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$subQuery->where('A.date >=', $data['sdate']);
|
||||
$subQuery->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
$subQuery->groupBy('D.campaign_id, A.date, A.hour');
|
||||
$builder = $this->db->newQuery()->fromSubquery($subQuery, 'sub');
|
||||
$builder->select('
|
||||
G.id AS company_id,
|
||||
G.name AS company_name,
|
||||
E.ad_account_id as customerId,
|
||||
"facebook" AS media,
|
||||
sub.id AS id,
|
||||
sub.name AS name,
|
||||
COUNT(DISTINCT memo.seq) AS memo_cnt,
|
||||
sub.status,
|
||||
sub.budget,
|
||||
0 AS bidamount,
|
||||
"" AS bidamount_type,
|
||||
"" AS biddingStrategyType,
|
||||
SUM(sub.impressions) AS impressions,
|
||||
SUM(sub.click) AS click,
|
||||
SUM(sub.spend) AS spend,
|
||||
SUM(sub.sales) AS sales,
|
||||
SUM(sub.unique_total) AS unique_total,
|
||||
SUM(sub.margin) AS margin,
|
||||
JSON_OBJECTAGG(CONCAT(sub.date, "_", sub.hour), JSON_OBJECT(
|
||||
"impressions", sub.impressions,
|
||||
"click", sub.click,
|
||||
"spend", sub.spend,
|
||||
"sales", sub.sales,
|
||||
"unique_total", sub.unique_total,
|
||||
"cpa", IFNULL(ROUND(sub.spend/sub.unique_total, 1), 0),
|
||||
"margin", sub.margin,
|
||||
"margin_ratio", IFNULL(ROUND((sub.margin/sub.sales)*100, 1), 0)
|
||||
)) AS metrics,
|
||||
sub.create_date');
|
||||
$builder->join("z_facebook.fb_ad_account AS E", 'sub.customerId = E.ad_account_id');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.ad_account_id = F.ad_account_id AND F.media = "facebook"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->join('zenith.advertisement_memo memo', 'sub.id = memo.id AND memo.media = "facebook" AND memo.type = "campaigns" AND is_done <> 1', 'left');
|
||||
|
||||
if(!empty($data['business'])){
|
||||
$builder->whereIn('E.business_id', explode("|",$data['business']));
|
||||
}
|
||||
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['account'])){
|
||||
$builder->whereIn('E.ad_account_id', explode("|",$data['account']));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['stx'])){
|
||||
$builder->groupStart();
|
||||
$builder->like('sub.name', $data['stx']);
|
||||
$builder->groupEnd();
|
||||
}
|
||||
|
||||
$builder->groupBy('F.company_id');
|
||||
$builder->groupBy('sub.id');
|
||||
$builder->orderBy('sub.create_date', 'desc');
|
||||
// echo $builder->getCompiledSelect(); exit;
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getAdsets($data)
|
||||
{
|
||||
$subQuery = $this->db->table('z_facebook.fb_ad_insight_history A');
|
||||
$subQuery->select('
|
||||
C.campaign_id as campaign_id,
|
||||
C.adset_id AS id,
|
||||
C.adset_name AS name,
|
||||
C.status AS status,
|
||||
C.budget AS budget,
|
||||
C.create_date AS create_date,
|
||||
A.date AS date,
|
||||
A.hour AS hour,
|
||||
SUM(A.impressions) AS impressions,
|
||||
SUM(A.inline_link_clicks) AS click,
|
||||
SUM(A.spend) AS spend,
|
||||
SUM(A.sales) as sales,
|
||||
SUM(A.db_count) as unique_total,
|
||||
SUM(A.margin) as margin');
|
||||
$subQuery->join('z_facebook.fb_ad B', 'A.ad_id = B.ad_id');
|
||||
$subQuery->join('z_facebook.fb_adset C', 'B.adset_id = C.adset_id');
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$subQuery->where('A.date >=', $data['sdate']);
|
||||
$subQuery->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
$subQuery->groupBy('C.adset_id, A.date, A.hour');
|
||||
|
||||
$builder = $this->db->newQuery()->fromSubquery($subQuery, 'sub');
|
||||
$builder->select('
|
||||
G.id AS company_id,
|
||||
G.name AS company_name,
|
||||
E.ad_account_id as customerId,
|
||||
D.campaign_id AS campaign_id,
|
||||
"facebook" AS media,
|
||||
sub.id AS id,
|
||||
sub.name AS name,
|
||||
COUNT(DISTINCT memo.seq) AS memo_cnt,
|
||||
sub.status AS status,
|
||||
sub.budget AS budget,
|
||||
"" AS biddingStrategyType,
|
||||
0 AS bidamount,
|
||||
0 AS campaign_bidamount,
|
||||
"" AS bidamount_type,
|
||||
SUM(sub.impressions) AS impressions,
|
||||
SUM(sub.click) AS click,
|
||||
SUM(sub.spend) AS spend,
|
||||
SUM(sub.sales) AS sales,
|
||||
SUM(sub.unique_total) AS unique_total,
|
||||
SUM(sub.margin) AS margin,
|
||||
JSON_OBJECTAGG(CONCAT(sub.date, "_", sub.hour), JSON_OBJECT(
|
||||
"impressions", sub.impressions,
|
||||
"click", sub.click,
|
||||
"spend", sub.spend,
|
||||
"sales", sub.sales,
|
||||
"unique_total", sub.unique_total,
|
||||
"cpa", IFNULL(ROUND(sub.spend/sub.unique_total, 1), 0),
|
||||
"margin", sub.margin,
|
||||
"margin_ratio", IFNULL(ROUND((sub.margin/sub.sales)*100, 1), 0)
|
||||
)) AS metrics,
|
||||
sub.create_date
|
||||
');
|
||||
$builder->join('z_facebook.fb_campaign D', 'sub.campaign_id = D.campaign_id');
|
||||
$builder->join('z_facebook.fb_ad_account E', 'D.account_id = E.ad_account_id');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.ad_account_id = F.ad_account_id AND F.media = "facebook"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->join('zenith.advertisement_memo memo', 'sub.id = memo.id AND memo.media = "facebook" AND memo.type = "adsets" AND is_done <> 1', 'left');
|
||||
|
||||
if(!empty($data['business'])){
|
||||
$builder->whereIn('E.business_id', explode("|",$data['business']));
|
||||
}
|
||||
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['account'])){
|
||||
$builder->whereIn('E.ad_account_id', explode("|",$data['account']));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['stx'])){
|
||||
$builder->groupStart();
|
||||
$builder->like('sub.name', $data['stx']);
|
||||
$builder->groupEnd();
|
||||
}
|
||||
|
||||
$builder->groupBy('F.company_id');
|
||||
$builder->groupBy('sub.id');
|
||||
$builder->orderBy('sub.create_date', 'desc');
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getAds($data)
|
||||
{
|
||||
$subQuery = $this->db->table('z_facebook.fb_ad_insight_history A');
|
||||
$subQuery->select('
|
||||
B.adset_id as adset_id,
|
||||
B.ad_id AS id,
|
||||
B.ad_name AS name,
|
||||
B.code AS code,
|
||||
B.status AS status,
|
||||
B.effective_status AS approval_status,
|
||||
C.thumbnail AS thumbnail,
|
||||
C.link AS landingUrl,
|
||||
B.create_date AS create_date,
|
||||
A.date AS date,
|
||||
A.hour AS hour,
|
||||
SUM(A.impressions) AS impressions,
|
||||
SUM(A.inline_link_clicks) AS click,
|
||||
SUM(A.spend) AS spend,
|
||||
SUM(A.sales) as sales,
|
||||
SUM(A.db_count) as unique_total,
|
||||
SUM(A.margin) as margin');
|
||||
$subQuery->join('z_facebook.fb_ad B', 'A.ad_id = B.ad_id');
|
||||
$subQuery->join('z_facebook.fb_adcreative C', 'A.ad_id = C.ad_id');
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$subQuery->where('A.date >=', $data['sdate']);
|
||||
$subQuery->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
$subQuery->groupBy('B.ad_id, A.date, A.hour');
|
||||
|
||||
$builder = $this->db->newQuery()->fromSubquery($subQuery, 'sub');
|
||||
$builder->select('
|
||||
G.id AS company_id,
|
||||
G.name AS company_name,
|
||||
E.ad_account_id as customerId,
|
||||
D.campaign_id AS campaign_id,
|
||||
C.adset_id AS adset_id,
|
||||
"facebook" AS media,
|
||||
sub.id AS id,
|
||||
sub.name AS name,
|
||||
COUNT(DISTINCT memo.seq) AS memo_cnt,
|
||||
sub.code AS code,
|
||||
sub.status AS status,
|
||||
sub.approval_status AS approval_status,
|
||||
"" AS policyTopic,
|
||||
sub.thumbnail AS thumbnail,
|
||||
"" AS assets,
|
||||
sub.landingUrl AS landingUrl,
|
||||
0 AS budget,
|
||||
0 AS bidamount,
|
||||
"" AS bidamount_type,
|
||||
SUM(sub.impressions) AS impressions,
|
||||
SUM(sub.click) AS click,
|
||||
SUM(sub.spend) AS spend,
|
||||
SUM(sub.sales) AS sales,
|
||||
SUM(sub.unique_total) AS unique_total,
|
||||
SUM(sub.margin) AS margin,
|
||||
JSON_OBJECTAGG(CONCAT(sub.date, "_", sub.hour), JSON_OBJECT(
|
||||
"impressions", sub.impressions,
|
||||
"click", sub.click,
|
||||
"spend", sub.spend,
|
||||
"sales", sub.sales,
|
||||
"unique_total", sub.unique_total,
|
||||
"cpa", IFNULL(ROUND(sub.spend/sub.unique_total, 1), 0),
|
||||
"margin", sub.margin,
|
||||
"margin_ratio", IFNULL(ROUND((sub.margin/sub.sales)*100, 1), 0)
|
||||
)) AS metrics,
|
||||
sub.create_date
|
||||
');
|
||||
$builder->join('z_facebook.fb_adset C', 'sub.adset_id = C.adset_id');
|
||||
$builder->join('z_facebook.fb_campaign D', 'C.campaign_id = D.campaign_id');
|
||||
$builder->join('z_facebook.fb_ad_account E', 'D.account_id = E.ad_account_id');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.ad_account_id = F.ad_account_id AND F.media = "facebook"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->join('zenith.advertisement_memo memo', 'sub.id = memo.id AND memo.media = "facebook" AND memo.type = "ads" AND is_done <> 1', 'left');
|
||||
|
||||
if(!empty($data['business'])){
|
||||
$builder->whereIn('E.business_id', explode("|",$data['business']));
|
||||
}
|
||||
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['account'])){
|
||||
$builder->whereIn('E.ad_account_id', explode("|",$data['account']));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['stx'])){
|
||||
$builder->groupStart();
|
||||
$builder->like('sub.name', $data['stx']);
|
||||
$builder->groupEnd();
|
||||
}
|
||||
|
||||
$builder->groupBy('F.company_id');
|
||||
$builder->groupBy('sub.id');
|
||||
$builder->orderBy('sub.create_date', 'desc');
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getReport($data)
|
||||
{
|
||||
$builder = $this->db->table('z_facebook.fb_ad_insight_history A');
|
||||
$builder->select('
|
||||
A.date,
|
||||
SUM(A.impressions) AS impressions,
|
||||
SUM(A.inline_link_clicks) AS click,
|
||||
(SUM(A.inline_link_clicks) / SUM(A.impressions)) * 100 AS click_ratio,
|
||||
(SUM(A.db_count) / SUM(A.inline_link_clicks)) * 100 AS conversion_ratio,
|
||||
SUM(A.spend) AS spend,
|
||||
SUM(A.db_count) AS unique_total,
|
||||
IFNULL(SUM(A.spend) / SUM(A.db_count), 0) AS unique_one_price,
|
||||
SUM(A.db_price) AS unit_price,
|
||||
SUM(A.sales) AS price,
|
||||
SUM(A.margin) AS profit,
|
||||
(SUM(A.db_price * A.db_count) - SUM(A.spend)) / SUM(A.db_price * A.db_count) * 100 AS per
|
||||
');
|
||||
$builder->join('z_facebook.fb_ad B', 'A.ad_id = B.ad_id');
|
||||
$builder->join('z_facebook.fb_adset C', 'B.adset_id = C.adset_id');
|
||||
$builder->join('z_facebook.fb_campaign D', 'C.campaign_id = D.campaign_id');
|
||||
$builder->join('z_facebook.fb_ad_account E', 'D.account_id = E.ad_account_id');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.ad_account_id = F.ad_account_id AND F.media = "facebook"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$builder->where('A.date >=', $data['sdate']);
|
||||
$builder->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
|
||||
if(!empty($data['facebookCheck'])){
|
||||
switch ($data['type']) {
|
||||
case 'campaigns':
|
||||
$builder->whereIn('D.campaign_id', $data['facebookCheck']);
|
||||
break;
|
||||
case 'adsets':
|
||||
$builder->whereIn('C.adset_id', $data['facebookCheck']);
|
||||
break;
|
||||
case 'ads':
|
||||
$builder->whereIn('B.ad_id', $data['facebookCheck']);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!empty($data['business'])){
|
||||
$builder->whereIn('E.business_id', explode("|",$data['business']));
|
||||
}
|
||||
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['account'])){
|
||||
$builder->whereIn('E.ad_account_id', explode("|",$data['account']));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
$builder->groupBy('A.date');
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getDisapproval()
|
||||
{
|
||||
$builder = $this->db->table('z_facebook.fb_ad_account acc');
|
||||
$builder->select('acc.ad_account_id');
|
||||
$builder->join('z_facebook.fb_campaign ac', 'acc.ad_account_id = ac.account_id', 'left');
|
||||
$builder->join('z_facebook.fb_adset ag', 'ac.campaign_id = ag.campaign_id', 'left');
|
||||
$builder->join('z_facebook.fb_ad ad', 'ag.adset_id = ag.adset_id', 'left');
|
||||
$builder->join('z_facebook.fb_adcreative as', 'ad.ad_id = as.ad_id', 'left');
|
||||
$builder->where('ad.effective_status', 'DISAPPROVED');
|
||||
$builder->where('acc.status', 1);
|
||||
$builder->where('ac.status', 'ACTIVE');
|
||||
$builder->where('ag.status', 'ACTIVE');
|
||||
$builder->where('ad.status', 'ACTIVE');
|
||||
// $builder->orderBy('ad.created_time', 'DESC');
|
||||
$builder->groupBy('acc.ad_account_id');
|
||||
$result = $builder->get()->getResultArray();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getCampaignById($ids)
|
||||
{
|
||||
$builder = $this->db->table('z_facebook.fb_campaign');
|
||||
$builder->select('"페이스북" as media, "캠페인" as type, campaign_id as id, campaign_name as name, status as status');
|
||||
$builder->whereIn('campaign_id', $ids);
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getAdgroupById($ids)
|
||||
{
|
||||
$builder = $this->db->table('z_facebook.fb_adset');
|
||||
$builder->select('"페이스북" as media, "광고그룹" as type, adset_id as id, adset_name as name, status as status');
|
||||
$builder->whereIn('adset_id', $ids);
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getAdById($ids)
|
||||
{
|
||||
$builder = $this->db->table('z_facebook.fb_ad');
|
||||
$builder->select('"페이스북" as media, "광고" as type, ad_id as id, ad_name as name, status as status');
|
||||
$builder->whereIn('ad_id', $ids);
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function updateCode($data) {
|
||||
$result = [];
|
||||
$this->db->transStart();
|
||||
$builder = $this->db->table('z_facebook.fb_ad');
|
||||
$builder->set('code', $data['code']);
|
||||
$builder->where('ad_id', $data['id']);
|
||||
$builder->update();
|
||||
$queryResult = $this->db->transComplete();
|
||||
if($queryResult){
|
||||
$result['code'] = $data['code'] ?? '';
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function updateDbCount($data) {
|
||||
$this->db->transStart();
|
||||
$builder = $this->db->table('z_facebook.fb_ad_account');
|
||||
$builder->set('db_count', (integer)$data['db_count']);
|
||||
$builder->where('ad_account_id', $data['id']);
|
||||
$builder->update();
|
||||
$result = $this->db->transComplete();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function setUpdatingByAds($campaignIds){
|
||||
$this->db->transStart();
|
||||
$builder = $this->db->table('z_facebook.fb_campaign');
|
||||
$builder->whereIn('campaign_id', $campaignIds);
|
||||
$builder->set('is_updating', 1);
|
||||
$builder->update();
|
||||
$result = $this->db->transComplete();
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,874 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Advertiser;
|
||||
|
||||
use App\Libraries\Calc;
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class AdvGoogleManagerModel extends Model
|
||||
{
|
||||
public function getManageAccounts() {
|
||||
$builder = $this->google->table('aw_ad_account');
|
||||
$builder->select('*');
|
||||
$builder->where('canManageClients', 1);
|
||||
$builder->where('status', 'ENABLED');
|
||||
//$builder->where('is_exposed', 1);
|
||||
$builder->orderBy('name', 'asc');
|
||||
$builder->orderBy('create_time', 'asc');
|
||||
|
||||
$result = $builder->get()->getResultArray();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getAccounts($data)
|
||||
{
|
||||
$builder = $this->db->table('z_adwords.aw_ad_report A');
|
||||
$builder->select('
|
||||
G.id AS company_id,
|
||||
G.name AS company_name
|
||||
');
|
||||
$builder->join('z_adwords.aw_ad B', 'A.ad_id = B.id');
|
||||
$builder->join('z_adwords.aw_adgroup C', 'B.adgroupId = C.id');
|
||||
$builder->join('z_adwords.aw_campaign D', 'C.campaignId = D.id');
|
||||
$builder->join('z_adwords.aw_ad_account E', 'D.customerId = E.customerId');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.customerId = F.ad_account_id AND F.media = "google"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->where('D.status !=', 'NODATA');
|
||||
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$builder->where('A.date >=', $data['sdate']);
|
||||
$builder->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['googleCheck'])){
|
||||
switch ($data['type']) {
|
||||
case 'campaigns':
|
||||
$builder->whereIn('D.id', $data['googleCheck']);
|
||||
break;
|
||||
case 'adsets':
|
||||
$builder->whereIn('C.id', $data['googleCheck']);
|
||||
break;
|
||||
case 'ads':
|
||||
$builder->whereIn('B.id', $data['googleCheck']);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getMediaAccounts($data)
|
||||
{
|
||||
$builder = $this->db->table('z_adwords.aw_ad_report A');
|
||||
$builder->select('
|
||||
"google" AS media,
|
||||
E.name AS media_account_name,
|
||||
E.customerId AS media_account_id,
|
||||
E.status AS status,
|
||||
"" AS isAdminStop,
|
||||
E.is_exposed AS is_exposed,
|
||||
E.db_count AS db_count,
|
||||
SUM(A.db_count) AS db_sum,
|
||||
COUNT(DISTINCT A.date) AS date_count
|
||||
');
|
||||
$builder->join('z_adwords.aw_ad B', 'A.ad_id = B.id');
|
||||
$builder->join('z_adwords.aw_adgroup C', 'B.adgroupId = C.id');
|
||||
$builder->join('z_adwords.aw_campaign D', 'C.campaignId = D.id');
|
||||
$builder->join('z_adwords.aw_ad_account E', 'D.customerId = E.customerId');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.customerId = F.ad_account_id AND F.media = "google"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->where('D.status !=', 'NODATA');
|
||||
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$builder->where('A.date >=', $data['sdate']);
|
||||
$builder->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['googleCheck'])){
|
||||
switch ($data['type']) {
|
||||
case 'campaigns':
|
||||
$builder->whereIn('D.id', $data['googleCheck']);
|
||||
break;
|
||||
case 'adsets':
|
||||
$builder->whereIn('C.id', $data['googleCheck']);
|
||||
break;
|
||||
case 'ads':
|
||||
$builder->whereIn('B.id', $data['googleCheck']);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
$builder->groupBy('E.customerId');
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getOnlyAdAccount($data)
|
||||
{
|
||||
$builder = $this->db->table('z_adwords.aw_ad_account');
|
||||
$builder->select('
|
||||
"google" AS media,
|
||||
name AS media_account_name,
|
||||
customerId AS media_account_id,
|
||||
is_exposed AS is_exposed,
|
||||
canManageClients AS canManageClients,
|
||||
db_count AS db_count
|
||||
');
|
||||
$builder->where('is_hidden', 0);
|
||||
if(!empty($data['stx'])){
|
||||
$builder->groupStart();
|
||||
$builder->like('name', $data['stx']);
|
||||
$builder->groupEnd();
|
||||
}
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getCampaigns($data)
|
||||
{
|
||||
$subQuery = $this->db->table('z_adwords.aw_ad_report A');
|
||||
$subQuery->select('
|
||||
D.customerId as customerId,
|
||||
D.id AS id,
|
||||
D.name AS name,
|
||||
D.status AS status,
|
||||
D.amount AS budget,
|
||||
D.cpaBidAmount AS bidamount,
|
||||
D.create_time AS create_date,
|
||||
C.biddingStrategyType AS biddingStrategyType,
|
||||
SUM(A.impressions) AS impressions,
|
||||
SUM(A.clicks) AS click,
|
||||
SUM(A.cost) AS spend,
|
||||
SUM(A.sales) AS sales,
|
||||
SUM(A.db_count) AS unique_total,
|
||||
SUM(A.margin) AS margin
|
||||
');
|
||||
$subQuery->join('z_adwords.aw_ad B', 'A.ad_id = B.id');
|
||||
$subQuery->join('z_adwords.aw_adgroup C', 'B.adgroupId = C.id');
|
||||
$subQuery->join('z_adwords.aw_campaign D', 'C.campaignId = D.id');
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$subQuery->where('A.date >=', $data['sdate']);
|
||||
$subQuery->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
$subQuery->groupBy('D.id');
|
||||
|
||||
$metricsSubQuery = $this->db->table('z_adwords.aw_adgroup_report_history A');
|
||||
$metricsSubQuery->select('
|
||||
D.id as campaignId,
|
||||
A.date,
|
||||
A.hour,
|
||||
SUM(A.impressions) AS impressions,
|
||||
SUM(A.clicks) AS clicks,
|
||||
SUM(A.cost) AS cost,
|
||||
SUM(A.sales) AS sales,
|
||||
SUM(A.db_count) AS db_count,
|
||||
SUM(A.margin) AS margin
|
||||
');
|
||||
$metricsSubQuery->join('z_adwords.aw_adgroup C', 'A.adgroup_id = C.id');
|
||||
$metricsSubQuery->join('z_adwords.aw_campaign D', 'C.campaignId = D.id');
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$metricsSubQuery->where('A.date >=', $data['sdate']);
|
||||
$metricsSubQuery->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
$metricsSubQuery->groupBy(['D.id', 'A.date', 'A.hour']);
|
||||
|
||||
$metricsSubQuerySql = $metricsSubQuery->getCompiledSelect();
|
||||
|
||||
$finalMetricsSubQuery = $this->db->table("($metricsSubQuerySql) AS metrics");
|
||||
$finalMetricsSubQuery->select('
|
||||
metrics.campaignId,
|
||||
JSON_OBJECTAGG(
|
||||
CONCAT(metrics.date, "_", metrics.hour),
|
||||
JSON_OBJECT(
|
||||
"impressions", metrics.impressions,
|
||||
"click", metrics.clicks,
|
||||
"spend", metrics.cost,
|
||||
"sales", metrics.sales,
|
||||
"unique_total", metrics.db_count,
|
||||
"cpa", IFNULL(ROUND(metrics.cost/metrics.db_count, 1), 0),
|
||||
"margin", metrics.margin,
|
||||
"margin_ratio", IFNULL(ROUND((metrics.margin/metrics.sales)*100, 1), 0)
|
||||
)
|
||||
) AS metrics
|
||||
');
|
||||
$finalMetricsSubQuery->groupBy('metrics.campaignId');
|
||||
|
||||
$finalMetricsSubQuerySql = $finalMetricsSubQuery->getCompiledSelect();
|
||||
|
||||
$builder = $this->db->newQuery()->fromSubquery($subQuery, 'sub');
|
||||
$builder->select('
|
||||
G.id AS company_id,
|
||||
G.name AS company_name,
|
||||
E.customerId as customerId,
|
||||
"google" AS media,
|
||||
sub.id AS id,
|
||||
sub.name AS name,
|
||||
COUNT(DISTINCT memo.seq) AS memo_cnt,
|
||||
sub.status,
|
||||
sub.budget,
|
||||
sub.bidamount,
|
||||
"" AS bidamount_type,
|
||||
sub.biddingStrategyType AS biddingStrategyType,
|
||||
sub.impressions,
|
||||
sub.click,
|
||||
sub.spend,
|
||||
sub.sales,
|
||||
sub.unique_total,
|
||||
sub.margin,
|
||||
metrics.metrics AS metrics,
|
||||
sub.create_date
|
||||
');
|
||||
$builder->join('z_adwords.aw_ad_account E', 'sub.customerId = E.customerId');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.customerId = F.ad_account_id AND F.media = "google"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->join('zenith.advertisement_memo memo', 'sub.id = memo.id AND memo.media = "google" AND memo.type = "campaigns" AND is_done <> 1', 'left');
|
||||
$builder->join("($finalMetricsSubQuerySql) metrics", 'metrics.campaignId = sub.id', 'left');
|
||||
$builder->where('sub.status !=', 'NODATA');
|
||||
|
||||
if(!empty($data['business'])){
|
||||
$builder->whereIn('E.manageCustomer', explode("|",$data['business']));
|
||||
}
|
||||
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['account'])){
|
||||
$builder->whereIn('E.customerId', explode("|",$data['account']));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['stx'])){
|
||||
$builder->groupStart();
|
||||
$builder->like('sub.name', $data['stx']);
|
||||
$builder->groupEnd();
|
||||
}
|
||||
|
||||
$builder->groupBy('F.company_id');
|
||||
$builder->groupBy('sub.id');
|
||||
$builder->orderBy('sub.name', 'asc');
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getAdsets($data)
|
||||
{
|
||||
$subQuery = $this->db->table('z_adwords.aw_ad_report A');
|
||||
$subQuery->select('
|
||||
C.campaignId as campaign_id,
|
||||
C.id AS id,
|
||||
C.name AS name,
|
||||
C.status AS status,
|
||||
C.biddingStrategyType AS biddingStrategyType,
|
||||
C.create_time AS create_date,
|
||||
CASE
|
||||
WHEN C.biddingStrategyType = "TARGET_CPA" THEN C.cpaBidAmount
|
||||
WHEN C.biddingStrategyType = "MANUAL_CPC" THEN C.cpcBidAmount
|
||||
ELSE 0
|
||||
END AS bidamount,
|
||||
CASE
|
||||
WHEN C.biddingStrategyType = "TARGET_CPA" THEN "cpa"
|
||||
WHEN C.biddingStrategyType = "MANUAL_CPC" THEN "cpc"
|
||||
ELSE ""
|
||||
END AS bidamount_type,
|
||||
SUM(A.impressions) AS impressions,
|
||||
SUM(A.clicks) AS click,
|
||||
SUM(A.cost) AS spend,
|
||||
SUM(A.sales) AS sales,
|
||||
SUM(A.db_count) AS unique_total,
|
||||
SUM(A.margin) AS margin
|
||||
', false);
|
||||
$subQuery->join('z_adwords.aw_ad B', 'A.ad_id = B.id');
|
||||
$subQuery->join('z_adwords.aw_adgroup C', 'B.adgroupId = C.id');
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$subQuery->where('A.date >=', $data['sdate']);
|
||||
$subQuery->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
$subQuery->groupBy('C.id');
|
||||
|
||||
$metricsSubQuery = $this->db->table('z_adwords.aw_adgroup_report_history A');
|
||||
$metricsSubQuery->select('
|
||||
C.id as id,
|
||||
A.date,
|
||||
A.hour,
|
||||
SUM(A.impressions) AS impressions,
|
||||
SUM(A.clicks) AS clicks,
|
||||
SUM(A.cost) AS cost,
|
||||
SUM(A.sales) AS sales,
|
||||
SUM(A.db_count) AS db_count,
|
||||
SUM(A.margin) AS margin
|
||||
');
|
||||
$metricsSubQuery->join('z_adwords.aw_adgroup C', 'A.adgroup_id = C.id');
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$metricsSubQuery->where('A.date >=', $data['sdate']);
|
||||
$metricsSubQuery->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
$metricsSubQuery->groupBy(['C.id', 'A.date', 'A.hour']);
|
||||
$metricsSubQuerySql = $metricsSubQuery->getCompiledSelect();
|
||||
|
||||
$finalMetricsSubQuery = $this->db->table("($metricsSubQuerySql) AS metrics");
|
||||
$finalMetricsSubQuery->select('
|
||||
metrics.id,
|
||||
JSON_OBJECTAGG(
|
||||
CONCAT(metrics.date, "_", metrics.hour),
|
||||
JSON_OBJECT(
|
||||
"impressions", metrics.impressions,
|
||||
"click", metrics.clicks,
|
||||
"spend", metrics.cost,
|
||||
"sales", metrics.sales,
|
||||
"unique_total", metrics.db_count,
|
||||
"cpa", IFNULL(ROUND(metrics.cost/metrics.db_count, 1), 0),
|
||||
"margin", metrics.margin,
|
||||
"margin_ratio", IFNULL(ROUND((metrics.margin/metrics.sales)*100, 1), 0)
|
||||
)
|
||||
) AS metrics
|
||||
');
|
||||
$finalMetricsSubQuery->groupBy('metrics.id');
|
||||
$finalMetricsSubQuerySql = $finalMetricsSubQuery->getCompiledSelect();
|
||||
|
||||
$builder = $this->db->newQuery()->fromSubquery($subQuery, 'sub');
|
||||
$builder->select('
|
||||
G.id AS company_id,
|
||||
G.name AS company_name,
|
||||
E.customerId as customerId,
|
||||
D.id AS campaign_id,
|
||||
"google" AS media,
|
||||
sub.id AS id,
|
||||
sub.name AS name,
|
||||
COUNT(DISTINCT memo.seq) AS memo_cnt,
|
||||
sub.status,
|
||||
0 AS budget,
|
||||
sub.biddingStrategyType AS biddingStrategyType,
|
||||
sub.bidamount AS bidamount,
|
||||
D.cpaBidAmount AS campaign_bidamount,
|
||||
sub.bidamount_type,
|
||||
sub.impressions,
|
||||
sub.click,
|
||||
sub.spend,
|
||||
sub.sales,
|
||||
sub.unique_total,
|
||||
sub.margin,
|
||||
metrics.metrics AS metrics,
|
||||
sub.create_date');
|
||||
$builder->join('z_adwords.aw_campaign D', 'sub.campaign_id = D.id');
|
||||
$builder->join('z_adwords.aw_ad_account E', 'D.customerId = E.customerId');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.customerId = F.ad_account_id AND F.media = "google"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->join('zenith.advertisement_memo memo', 'sub.id = memo.id AND memo.media = "google" AND memo.type = "adsets" AND is_done <> 1', 'left');
|
||||
$builder->join("($finalMetricsSubQuerySql) metrics", 'metrics.id = sub.id', 'left');
|
||||
$builder->where('D.status !=', 'NODATA');
|
||||
|
||||
if(!empty($data['business'])){
|
||||
$builder->whereIn('E.manageCustomer', explode("|",$data['business']));
|
||||
}
|
||||
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['account'])){
|
||||
$builder->whereIn('E.customerId', explode("|",$data['account']));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['stx'])){
|
||||
$builder->groupStart();
|
||||
$builder->like('sub.name', $data['stx']);
|
||||
$builder->groupEnd();
|
||||
}
|
||||
|
||||
$builder->groupBy('F.company_id');
|
||||
$builder->groupBy('sub.id');
|
||||
$builder->orderBy('sub.name', 'asc');
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getAds($data)
|
||||
{
|
||||
$subQuery = $this->db->table('z_adwords.aw_ad_report A');
|
||||
$subQuery->select('
|
||||
B.adgroupId as adgroupId,
|
||||
B.id AS id,
|
||||
B.name AS name,
|
||||
B.code AS code,
|
||||
B.status AS status,
|
||||
B.approvalStatus AS approval_status,
|
||||
B.policyTopic AS policyTopic,
|
||||
B.imageUrl AS thumbnail,
|
||||
B.assets AS assets,
|
||||
B.finalUrl AS landingUrl,
|
||||
B.create_time AS create_date,
|
||||
SUM(A.impressions) AS impressions,
|
||||
SUM(A.clicks) AS click,
|
||||
SUM(A.cost) AS spend,
|
||||
SUM(A.sales) AS sales,
|
||||
SUM(A.db_count) AS unique_total,
|
||||
SUM(A.margin) AS margin,
|
||||
');
|
||||
$subQuery->join('z_adwords.aw_ad B', 'A.ad_id = B.id');
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$subQuery->where('A.date >=', $data['sdate']);
|
||||
$subQuery->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
$subQuery->groupBy('B.id');
|
||||
|
||||
$metricsSubQuery = $this->db->table('z_adwords.aw_ad_report_history A');
|
||||
$metricsSubQuery->select('
|
||||
B.id as id,
|
||||
A.date,
|
||||
A.hour,
|
||||
SUM(A.impressions) AS impressions,
|
||||
SUM(A.clicks) AS clicks,
|
||||
SUM(A.cost) AS cost,
|
||||
SUM(A.sales) AS sales,
|
||||
SUM(A.db_count) AS db_count,
|
||||
SUM(A.margin) AS margin
|
||||
');
|
||||
$metricsSubQuery->join('z_adwords.aw_ad B', 'A.ad_id = B.id');
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$metricsSubQuery->where('A.date >=', $data['sdate']);
|
||||
$metricsSubQuery->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
$metricsSubQuery->groupBy(['B.id', 'A.date', 'A.hour']);
|
||||
$metricsSubQuerySql = $metricsSubQuery->getCompiledSelect();
|
||||
|
||||
$finalMetricsSubQuery = $this->db->table("($metricsSubQuerySql) AS metrics");
|
||||
$finalMetricsSubQuery->select('
|
||||
metrics.id,
|
||||
JSON_OBJECTAGG(
|
||||
CONCAT(metrics.date, "_", metrics.hour),
|
||||
JSON_OBJECT(
|
||||
"impressions", metrics.impressions,
|
||||
"click", metrics.clicks,
|
||||
"spend", metrics.cost,
|
||||
"sales", metrics.sales,
|
||||
"unique_total", metrics.db_count,
|
||||
"cpa", IFNULL(ROUND(metrics.cost/metrics.db_count, 1), 0),
|
||||
"margin", metrics.margin,
|
||||
"margin_ratio", IFNULL(ROUND((metrics.margin/metrics.sales)*100, 1), 0)
|
||||
)
|
||||
) AS metrics
|
||||
');
|
||||
$finalMetricsSubQuery->groupBy('metrics.id');
|
||||
$finalMetricsSubQuerySql = $finalMetricsSubQuery->getCompiledSelect();
|
||||
|
||||
$builder = $this->db->newQuery()->fromSubquery($subQuery, 'sub');
|
||||
$builder->select('
|
||||
G.id AS company_id,
|
||||
G.name AS company_name,
|
||||
E.customerId as customerId,
|
||||
D.id AS campaign_id,
|
||||
C.id AS adset_id,
|
||||
"google" AS media,
|
||||
sub.id AS id,
|
||||
sub.name AS name,
|
||||
COUNT(DISTINCT memo.seq) AS memo_cnt,
|
||||
sub.code AS code,
|
||||
sub.status AS status,
|
||||
sub.approval_status AS approval_status,
|
||||
sub.policyTopic AS policyTopic,
|
||||
sub.thumbnail AS thumbnail,
|
||||
sub.assets AS assets,
|
||||
sub.landingUrl AS landingUrl,
|
||||
0 AS budget,
|
||||
0 AS bidamount,
|
||||
"" AS bidamount_type,
|
||||
sub.impressions,
|
||||
sub.click,
|
||||
sub.spend,
|
||||
sub.sales,
|
||||
sub.unique_total,
|
||||
sub.margin,
|
||||
metrics.metrics AS metrics,
|
||||
sub.create_date');
|
||||
$builder->join('z_adwords.aw_adgroup C', 'sub.adgroupId = C.id');
|
||||
$builder->join('z_adwords.aw_campaign D', 'C.campaignId = D.id');
|
||||
$builder->join('z_adwords.aw_ad_account E', 'D.customerId = E.customerId');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.customerId = F.ad_account_id AND F.media = "google"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->join('zenith.advertisement_memo memo', 'sub.id = memo.id AND memo.media = "google" AND memo.type = "ads" AND is_done <> 1', 'left');
|
||||
$builder->join("($finalMetricsSubQuerySql) metrics", 'metrics.id = sub.id', 'left');
|
||||
$builder->where('D.status !=', 'NODATA');
|
||||
|
||||
if(!empty($data['business'])){
|
||||
$builder->whereIn('E.manageCustomer', explode("|",$data['business']));
|
||||
}
|
||||
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['account'])){
|
||||
$builder->whereIn('E.customerId', explode("|",$data['account']));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['stx'])){
|
||||
$builder->groupStart();
|
||||
$builder->like('sub.name', $data['stx']);
|
||||
$builder->groupEnd();
|
||||
}
|
||||
|
||||
$builder->groupBy('F.company_id');
|
||||
$builder->groupBy('sub.id');
|
||||
$builder->orderBy('sub.name', 'asc');
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getStatuses($param, $result, $dates)
|
||||
{
|
||||
foreach ($result as &$row) {
|
||||
/* $stat = $this->getStat($params, $row['id'], $dates[0], $dates[1]);
|
||||
$optimization_stat = $this->getOptimization($params, $row['id']);
|
||||
$optimization_budget = $this->getOptimization_budget($params, $row['id']);
|
||||
|
||||
$row['unique_total'] = $stat['unique_total']; //유효db
|
||||
$row['sales'] = $row['sales']; //매출액
|
||||
|
||||
// echo $stat['shortterm']."!<br/>";
|
||||
|
||||
// $row['margin'] = Calc::margin($row['sales'], $row['cost'], $stat['margin']); // 수익
|
||||
$row['margin'] = $stat['margin']; // 수익
|
||||
|
||||
if($params=="campaigns"){ // 캠페인단 ai
|
||||
$optimization_campaign = "OFF";
|
||||
$song_optimization_campaign = "OFF";
|
||||
$choi_optimization_campaign = "OFF";
|
||||
for($i=0;$i<sizeof($optimization_stat);$i++){
|
||||
if($optimization_stat[$i]['type'] =="901"){
|
||||
$optimization_campaign = "ON";
|
||||
}
|
||||
if($optimization_stat[$i]['type'] =="801" || $optimization_stat[$i]['type'] =="802" || $optimization_stat[$i]['type'] =="803"){
|
||||
$song_optimization_campaign = "Lv".substr($optimization_stat[$i]['type'],-1);
|
||||
}
|
||||
if($optimization_stat[$i]['type'] =="701"){
|
||||
$choi_optimization_campaign = "ON";
|
||||
}
|
||||
}
|
||||
$row['optimization_campaign'] = $optimization_campaign;
|
||||
$row['song_optimization_campaign'] = $song_optimization_campaign;
|
||||
$row['choi_optimization_campaign'] = $choi_optimization_campaign;
|
||||
}
|
||||
else if($params=="ads"){ // 광고 ai
|
||||
if($optimization_stat =="701"){
|
||||
$row['optimization_ad'] = "ON";
|
||||
}
|
||||
else {
|
||||
$row['optimization_ad'] = "OFF";
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
if($optimization_stat =="1"){
|
||||
$row['optimization'] = "ON"; //어른정파고
|
||||
$row['optimization_ch'] = "OFF"; // 어린이정파고
|
||||
}
|
||||
else if($optimization_stat =="2"){
|
||||
$row['optimization'] = "OFF"; //어른정파고
|
||||
$row['optimization_ch'] = "ON"; // 어린이정파고
|
||||
}
|
||||
else {
|
||||
$row['optimization'] = "OFF";
|
||||
$row['optimization_ch'] = "OFF";
|
||||
}
|
||||
}
|
||||
if($choi_optimization_campaign=="ON") $row['optimization_campaign_budget'] = $optimization_budget; // ai 예산 */
|
||||
|
||||
if($row['status'] == 'ENABLED'){
|
||||
$row['status'] = "ON";
|
||||
}else{
|
||||
$row['status'] = "OFF";
|
||||
}
|
||||
|
||||
$row['margin_ratio'] = Calc::margin_ratio($row['margin'], $row['sales']); // 수익률
|
||||
|
||||
|
||||
$row['cpc'] = Calc::cpc($row['spend'], $row['click']); // 클릭당단가 (1회 클릭당 비용)
|
||||
$row['ctr'] = Calc::ctr($row['click'], $row['impressions']); // 클릭율 (노출 대비 클릭한 비율)
|
||||
$row['cpa'] = Calc::cpa($row['unique_total'], $row['spend']); //DB단가(전환당 비용)
|
||||
$row['cvr'] = Calc::cvr($row['unique_total'], $row['click']); //전환율
|
||||
|
||||
switch (!empty($row['biddingStrategyType'])) {
|
||||
case 'TARGET_CPA' :
|
||||
$row['biddingStrategyType'] = '타겟 CPA';
|
||||
break;
|
||||
case 'TARGET_ROAS' :
|
||||
$row['biddingStrategyType'] = '타겟 광고 투자수익(ROAS)';
|
||||
break;
|
||||
case 'TARGET_SPEND' :
|
||||
$row['biddingStrategyType'] = '클릭수 최대화';
|
||||
break;
|
||||
case 'MAXIMIZE_CONVERSIONS' :
|
||||
$row['biddingStrategyType'] = '전환수 최대화';
|
||||
break;
|
||||
/* //값이 뭔지 모름ㅠㅠ
|
||||
case '' :
|
||||
$row['biddingStrategyType'] = '검색 결과 위치 타겟';
|
||||
break;
|
||||
case '' :
|
||||
$row['biddingStrategyType'] = '경쟁 광고보다 내 광고가 높은 순위에 게재되는 비율 타겟';
|
||||
break;
|
||||
case '' :
|
||||
$row['biddingStrategyType'] = '타겟 노출 점유율';
|
||||
break;
|
||||
*/
|
||||
case 'PAGE_ONE_PROMOTED' :
|
||||
$row['biddingStrategyType'] = '향상된 CPC 입찰기능';
|
||||
break;
|
||||
case 'MANUAL_CPM' :
|
||||
$row['biddingStrategyType'] = '수동 입찰 전략';
|
||||
break;
|
||||
case 'MANUAL_CPC' :
|
||||
$row['biddingStrategyType'] = '수동 CPC';
|
||||
break;
|
||||
case 'UNKNOWN' :
|
||||
$row['biddingStrategyType'] = '알수없음';
|
||||
break;
|
||||
default :
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
public function getReport($data)
|
||||
{
|
||||
$builder = $this->db->table('z_adwords.aw_ad_report A');
|
||||
$builder->select('
|
||||
A.date,
|
||||
SUM(A.impressions) AS impressions,
|
||||
SUM(A.clicks) AS click,
|
||||
(SUM(A.clicks) / SUM(A.impressions)) * 100 AS click_ratio,
|
||||
(SUM(A.db_count) / SUM(A.clicks)) * 100 AS conversion_ratio,
|
||||
SUM(A.cost) AS spend,
|
||||
SUM(A.db_count) AS unique_total,
|
||||
IFNULL(SUM(A.cost) / SUM(A.db_count), 0) AS unique_one_price,
|
||||
SUM(A.db_price) AS unit_price,
|
||||
SUM(A.sales) AS price,
|
||||
SUM(A.margin) AS profit,
|
||||
(SUM(A.db_price * A.db_count) - SUM(A.cost)) / SUM(A.db_price * A.db_count) * 100 AS per
|
||||
');
|
||||
$builder->join('z_adwords.aw_ad B', 'A.ad_id = B.id');
|
||||
$builder->join('z_adwords.aw_adgroup C', 'B.adgroupId = C.id');
|
||||
$builder->join('z_adwords.aw_campaign D', 'C.campaignId = D.id');
|
||||
$builder->join('z_adwords.aw_ad_account E', 'D.customerId = E.customerId');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.customerId = F.ad_account_id AND F.media = "google"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->where('D.status !=', 'NODATA');
|
||||
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$builder->where('A.date >=', $data['sdate']);
|
||||
$builder->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
|
||||
if(!empty($data['googleCheck'])){
|
||||
switch ($data['type']) {
|
||||
case 'campaigns':
|
||||
$builder->whereIn('D.id', $data['googleCheck']);
|
||||
break;
|
||||
case 'adsets':
|
||||
$builder->whereIn('C.id', $data['googleCheck']);
|
||||
break;
|
||||
case 'ads':
|
||||
$builder->whereIn('B.id', $data['googleCheck']);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!empty($data['business'])){
|
||||
$builder->whereIn('E.manageCustomer', explode("|",$data['business']));
|
||||
}
|
||||
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['account'])){
|
||||
$builder->whereIn('E.customerId', explode("|",$data['account']));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
$builder->groupBy('A.date');
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getDisapproval()
|
||||
{
|
||||
$builder = $this->db->table('z_adwords.aw_ad_account acc');
|
||||
$builder->select('
|
||||
acc.customerId,
|
||||
acc.name AS customer_name,
|
||||
ac.id AS campaign_id,
|
||||
ac.name AS campaign_name,
|
||||
ag.id AS adgroup_id,
|
||||
ag.name AS adgroup_name,
|
||||
ad.id,
|
||||
ad.name,
|
||||
ad.code,
|
||||
ass.url,
|
||||
ad.status,
|
||||
ad.policyTopic,
|
||||
ad.reviewStatus,
|
||||
ad.approvalStatus,
|
||||
ad.adType,
|
||||
ad.finalUrl,
|
||||
ad.create_time,
|
||||
ad.update_time');
|
||||
$builder->join('z_adwords.aw_campaign ac', 'ac.customerId = acc.customerId', 'left');
|
||||
$builder->join('z_adwords.aw_adgroup ag', 'ag.campaignId = ac.id', 'left');
|
||||
$builder->join('z_adwords.aw_ad ad', 'ad.adgroupId = ag.id', 'left');
|
||||
$builder->join('z_adwords.aw_asset ass', "SUBSTRING_INDEX(ad.assets, ',', 1) = ass.id", 'left');
|
||||
$builder->where("(ad.approvalStatus = 'DISAPPROVED' OR ad.approvalStatus = 'AREA_OF_INTEREST_ONLY' OR ad.approvalStatus = 'APPROVED_LIMITED' OR (ad.approvalStatus = 'APPROVED' AND ad.policyTopic LIKE '%HEALTH_IN_PERSONALIZED_ADS%')) AND acc.is_exposed = 1 AND acc.status = 'ENABLED' AND ac.status = 'ENABLED' AND ag.status = 'ENABLED' AND ad.status = 'ENABLED'");
|
||||
// $builder->orderBy('ad.create_time', 'DESC');
|
||||
$result = $builder->get()->getResultArray();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getCampaignById($ids)
|
||||
{
|
||||
$builder = $this->db->table('z_adwords.aw_campaign');
|
||||
$builder->select('"구글" as media, "캠페인" as type, id, name, status');
|
||||
$builder->whereIn('id', $ids);
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getAdgroupById($ids)
|
||||
{
|
||||
$builder = $this->db->table('z_adwords.aw_adgroup');
|
||||
$builder->select('"구글" as media, "광고그룹" as type, id, name, status');
|
||||
$builder->whereIn('id', $ids);
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getAdById($ids)
|
||||
{
|
||||
$builder = $this->db->table('z_adwords.aw_ad');
|
||||
$builder->select('"구글" as media, "광고" as type, id, name, status');
|
||||
$builder->whereIn('id', $ids);
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function updateCode($data) {
|
||||
$result = [];
|
||||
$this->db->transStart();
|
||||
$builder = $this->db->table('z_adwords.aw_ad');
|
||||
$builder->set('code', $data['code']);
|
||||
$builder->where('id', $data['id']);
|
||||
$builder->update();
|
||||
$queryResult = $this->db->transComplete();
|
||||
if($queryResult){
|
||||
$result['code'] = $data['code'] ?? '';
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getCustomerByCampaignId($campaignIds) {
|
||||
$builder = $this->db->table('z_adwords.aw_campaign A');
|
||||
$builder->select('A.id, B.customerId, B.manageCustomer');
|
||||
$builder->join('z_adwords.aw_ad_account B', 'A.customerId = B.customerId');
|
||||
$builder->whereIn('A.id', $campaignIds);
|
||||
$builder->groupBy('A.id');
|
||||
$result = $builder->get()->getResultArray();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function setUpdatingByAds($campaignIds){
|
||||
$this->db->transStart();
|
||||
$builder = $this->db->table('z_adwords.aw_campaign');
|
||||
$builder->whereIn('id', $campaignIds);
|
||||
$builder->set('is_updating', 1);
|
||||
$builder->update();
|
||||
$result = $this->db->transComplete();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function updateDbCount($data) {
|
||||
$this->db->transStart();
|
||||
$builder = $this->db->table('z_adwords.aw_ad_account');
|
||||
$builder->set('db_count', (integer)$data['db_count']);
|
||||
$builder->where('customerId', $data['id']);
|
||||
$builder->update();
|
||||
$result = $this->db->transComplete();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function updateExposed($data) {
|
||||
$this->db->transStart();
|
||||
$builder = $this->db->table('z_adwords.aw_ad_account');
|
||||
$builder->set('is_exposed', (integer)$data['is_exposed']);
|
||||
$builder->where('customerId', $data['id']);
|
||||
$builder->update();
|
||||
$result = $this->db->transComplete();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getCustomerById($id, $type) {
|
||||
if(empty($type)){return false;}
|
||||
$builder = $this->db->table('z_adwords.aw_ad_account A');
|
||||
$builder->select('A.customerId');
|
||||
$builder->join('z_adwords.aw_campaign B', 'A.customerId = B.customerId');
|
||||
$builder->join('z_adwords.aw_adgroup C', 'B.id = C.campaignId');
|
||||
$builder->join('z_adwords.aw_ad D', 'C.id = D.adgroupId');
|
||||
|
||||
if($type == 'campaign'){
|
||||
$builder->where('B.id', $id);
|
||||
}else if($type == 'adgroup'){
|
||||
$builder->where('C.id', $id);
|
||||
}else if($type == 'ad'){
|
||||
$builder->where('D.id', $id);
|
||||
}
|
||||
|
||||
$builder->groupBy('A.customerId');
|
||||
$result = $builder->get()->getRowArray();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getAsset($id) {
|
||||
$builder = $this->db->table('z_adwords.aw_asset');
|
||||
$builder->select('url');
|
||||
$builder->where('id', $id);
|
||||
$result = $builder->get()->getRowArray();
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,530 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Advertiser;
|
||||
|
||||
use App\Libraries\Calc;
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class AdvKakaoManagerModel extends Model
|
||||
{
|
||||
public function getAccounts($data)
|
||||
{
|
||||
$builder = $this->db->table('z_moment.mm_creative_report_basic A');
|
||||
$builder->select('
|
||||
G.id AS company_id,
|
||||
G.name AS company_name
|
||||
');
|
||||
$builder->join('z_moment.mm_creative B', 'A.id = B.id');
|
||||
$builder->join('z_moment.mm_adgroup C', 'B.adgroup_id = C.id');
|
||||
$builder->join('z_moment.mm_campaign D', 'C.campaign_id = D.id');
|
||||
$builder->join('z_moment.mm_ad_account E', 'D.ad_account_id = E.id');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.id = F.ad_account_id AND F.media = "kakao"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->where('A.cost IS NOT NULL AND A.imp !=', 0);
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$builder->where('A.date >=', $data['sdate']);
|
||||
$builder->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['kakaoCheck'])){
|
||||
switch ($data['type']) {
|
||||
case 'campaigns':
|
||||
$builder->whereIn('D.id', $data['kakaoCheck']);
|
||||
break;
|
||||
case 'adsets':
|
||||
$builder->whereIn('C.id', $data['kakaoCheck']);
|
||||
break;
|
||||
case 'ads':
|
||||
$builder->whereIn('B.id', $data['kakaoCheck']);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getMediaAccounts($data)
|
||||
{
|
||||
$builder = $this->db->table('z_moment.mm_creative_report_basic A');
|
||||
$builder->select('
|
||||
"kakao" AS media,
|
||||
E.name AS media_account_name,
|
||||
E.id AS media_account_id,
|
||||
E.config AS status,
|
||||
E.isAdminStop AS isAdminStop,
|
||||
"" AS is_exposed,
|
||||
"" AS db_count,
|
||||
"" AS db_sum,
|
||||
"" AS date_count
|
||||
');
|
||||
$builder->join('z_moment.mm_creative B', 'A.id = B.id');
|
||||
$builder->join('z_moment.mm_adgroup C', 'B.adgroup_id = C.id');
|
||||
$builder->join('z_moment.mm_campaign D', 'C.campaign_id = D.id');
|
||||
$builder->join('z_moment.mm_ad_account E', 'D.ad_account_id = E.id');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.id = F.ad_account_id AND F.media = "kakao"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->where('A.cost IS NOT NULL AND A.imp !=', 0);
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$builder->where('A.date >=', $data['sdate']);
|
||||
$builder->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['kakaoCheck'])){
|
||||
switch ($data['type']) {
|
||||
case 'campaigns':
|
||||
$builder->whereIn('D.id', $data['kakaoCheck']);
|
||||
break;
|
||||
case 'adsets':
|
||||
$builder->whereIn('C.id', $data['kakaoCheck']);
|
||||
break;
|
||||
case 'ads':
|
||||
$builder->whereIn('B.id', $data['kakaoCheck']);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
$builder->groupBy('E.id');
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getCampaigns($data)
|
||||
{
|
||||
$subQuery = $this->db->table('z_moment.mm_creative_report_basic A');
|
||||
$subQuery->select('
|
||||
D.ad_account_id as customerId,
|
||||
D.id AS id,
|
||||
D.name AS name,
|
||||
D.config AS status,
|
||||
D.dailyBudgetAmount AS budget,
|
||||
D.create_time AS create_date,
|
||||
A.date AS date,
|
||||
A.hour AS hour,
|
||||
SUM(A.imp) AS impressions,
|
||||
SUM(A.click) AS click,
|
||||
SUM(A.cost) AS spend,
|
||||
SUM(A.sales) AS sales,
|
||||
SUM(A.db_count) as unique_total,
|
||||
SUM(A.margin) as margin
|
||||
');
|
||||
$subQuery->join('z_moment.mm_creative B', 'A.id = B.id');
|
||||
$subQuery->join('z_moment.mm_adgroup C', 'B.adgroup_id = C.id');
|
||||
$subQuery->join('z_moment.mm_campaign D', 'C.campaign_id = D.id');
|
||||
$subQuery->where('A.imp !=', 0);
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$subQuery->where('A.date >=', $data['sdate']);
|
||||
$subQuery->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
$subQuery->groupBy('D.id, A.date, A.hour');
|
||||
|
||||
$builder = $this->db->newQuery()->fromSubquery($subQuery, 'sub');
|
||||
$builder->select('
|
||||
G.id AS company_id,
|
||||
G.name AS company_name,
|
||||
E.id as customerId,
|
||||
"kakao" AS media,
|
||||
sub.id AS id,
|
||||
sub.name AS name,
|
||||
COUNT(DISTINCT memo.seq) AS memo_cnt,
|
||||
sub.status AS status,
|
||||
sub.budget AS budget,
|
||||
0 AS bidamount,
|
||||
"" AS bidamount_type,
|
||||
"" AS biddingStrategyType,
|
||||
SUM(sub.impressions) AS impressions,
|
||||
SUM(sub.click) AS click,
|
||||
SUM(sub.spend) AS spend,
|
||||
SUM(sub.sales) AS sales,
|
||||
SUM(sub.unique_total) AS unique_total,
|
||||
SUM(sub.margin) AS margin,
|
||||
JSON_OBJECTAGG(CONCAT(sub.date, "_", sub.hour), JSON_OBJECT(
|
||||
"impressions", sub.impressions,
|
||||
"click", sub.click,
|
||||
"spend", sub.spend,
|
||||
"sales", sub.sales,
|
||||
"unique_total", sub.unique_total,
|
||||
"cpa", IFNULL(ROUND(sub.spend/sub.unique_total, 1), 0),
|
||||
"margin", sub.margin,
|
||||
"margin_ratio", IFNULL(ROUND((sub.margin/sub.sales)*100, 1), 0)
|
||||
)) AS metrics,
|
||||
sub.create_date
|
||||
');
|
||||
$builder->join('z_moment.mm_ad_account E', 'sub.customerId = E.id');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.id = F.ad_account_id AND F.media = "kakao"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->join('zenith.advertisement_memo memo', 'sub.id = memo.id AND memo.media = "kakao" AND memo.type = "campaigns" AND is_done <> 1', 'left');
|
||||
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['account'])){
|
||||
$builder->whereIn('E.id', explode("|",$data['account']));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['stx'])){
|
||||
$builder->groupStart();
|
||||
$builder->like('sub.name', $data['stx']);
|
||||
$builder->groupEnd();
|
||||
}
|
||||
|
||||
$builder->groupBy('F.company_id');
|
||||
$builder->groupBy('sub.id');
|
||||
$builder->orderBy('sub.name', 'asc');
|
||||
// echo $builder->getCompiledSelect(); exit;
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getAdsets($data)
|
||||
{
|
||||
$subQuery = $this->db->table('z_moment.mm_creative_report_basic A');
|
||||
$subQuery->select('
|
||||
C.campaign_id as campaign_id,
|
||||
C.id AS id,
|
||||
C.name AS name,
|
||||
C.config AS status,
|
||||
C.dailyBudgetAmount AS budget,
|
||||
C.bidAmount AS bidamount,
|
||||
C.create_time AS create_date,
|
||||
A.date AS date,
|
||||
A.hour AS hour,
|
||||
SUM(A.imp) AS impressions,
|
||||
SUM(A.click) AS click,
|
||||
SUM(A.cost) AS spend,
|
||||
SUM(A.sales) AS sales,
|
||||
SUM(A.db_count) as unique_total,
|
||||
SUM(A.margin) as margin');
|
||||
$subQuery->join('z_moment.mm_creative B', 'A.id = B.id');
|
||||
$subQuery->join('z_moment.mm_adgroup C', 'B.adgroup_id = C.id');
|
||||
$subQuery->where('A.cost IS NOT NULL AND A.imp !=', 0);
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$subQuery->where('A.date >=', $data['sdate']);
|
||||
$subQuery->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
$subQuery->groupBy('C.id, A.date, A.hour');
|
||||
|
||||
$builder = $this->db->newQuery()->fromSubquery($subQuery, 'sub');
|
||||
$builder->select('
|
||||
G.id AS company_id,
|
||||
G.name AS company_name,
|
||||
E.id as customerId,
|
||||
D.id AS campaign_id,
|
||||
"kakao" AS media,
|
||||
sub.id AS id,
|
||||
sub.name AS name,
|
||||
COUNT(DISTINCT memo.seq) AS memo_cnt,
|
||||
sub.status AS status,
|
||||
sub.budget AS budget,
|
||||
"" AS biddingStrategyType,
|
||||
sub.bidamount AS bidamount,
|
||||
0 AS campaign_bidamount,
|
||||
"" AS bidamount_type,
|
||||
SUM(sub.impressions) AS impressions,
|
||||
SUM(sub.click) AS click,
|
||||
SUM(sub.spend) AS spend,
|
||||
SUM(sub.sales) AS sales,
|
||||
SUM(sub.unique_total) AS unique_total,
|
||||
SUM(sub.margin) AS margin,
|
||||
JSON_OBJECTAGG(CONCAT(sub.date, "_", sub.hour), JSON_OBJECT(
|
||||
"impressions", sub.impressions,
|
||||
"click", sub.click,
|
||||
"spend", sub.spend,
|
||||
"sales", sub.sales,
|
||||
"unique_total", sub.unique_total,
|
||||
"cpa", IFNULL(ROUND(sub.spend/sub.unique_total, 1), 0),
|
||||
"margin", sub.margin,
|
||||
"margin_ratio", IFNULL(ROUND((sub.margin/sub.sales)*100, 1), 0)
|
||||
)) AS metrics,
|
||||
sub.create_date
|
||||
');
|
||||
$builder->join('z_moment.mm_campaign D', 'sub.campaign_id = D.id');
|
||||
$builder->join('z_moment.mm_ad_account E', 'D.ad_account_id = E.id');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.id = F.ad_account_id AND F.media = "kakao"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->join('zenith.advertisement_memo memo', 'sub.id = memo.id AND memo.media = "kakao" AND memo.type = "adsets" AND is_done <> 1', 'left');
|
||||
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['account'])){
|
||||
$builder->whereIn('E.id', explode("|",$data['account']));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['stx'])){
|
||||
$builder->groupStart();
|
||||
$builder->like('sub.name', $data['stx']);
|
||||
$builder->groupEnd();
|
||||
}
|
||||
|
||||
$builder->groupBy('F.company_id');
|
||||
$builder->groupBy('sub.id');
|
||||
$builder->orderBy('sub.name', 'asc');
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getAds($data)
|
||||
{
|
||||
$subQuery = $this->db->table('z_moment.mm_creative_report_basic A');
|
||||
$subQuery->select('
|
||||
B.adgroup_id as adgroup_id,
|
||||
B.id AS id,
|
||||
B.name AS name,
|
||||
B.code AS code,
|
||||
B.config AS status,
|
||||
B.reviewStatus AS approval_status,
|
||||
B.imageUrl AS thumbnail,
|
||||
B.landingUrl AS landingUrl,
|
||||
B.create_time AS create_date,
|
||||
A.date AS date,
|
||||
A.hour AS hour,
|
||||
SUM(A.imp) AS impressions,
|
||||
SUM(A.click) AS click,
|
||||
SUM(A.cost) AS spend,
|
||||
SUM(A.sales) AS sales,
|
||||
SUM(A.db_count) as unique_total,
|
||||
SUM(A.margin) as margin');
|
||||
$subQuery->join('z_moment.mm_creative B', 'A.id = B.id');
|
||||
$subQuery->where('A.imp !=', 0);
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$subQuery->where('A.date >=', $data['sdate']);
|
||||
$subQuery->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
$subQuery->groupBy('B.id, A.date, A.hour');
|
||||
|
||||
$builder = $this->db->newQuery()->fromSubquery($subQuery, 'sub');
|
||||
$builder->select('
|
||||
G.id AS company_id,
|
||||
G.name AS company_name,
|
||||
E.id as customerId,
|
||||
D.id AS campaign_id,
|
||||
C.id AS adset_id,
|
||||
"kakao" AS media,
|
||||
sub.id AS id,
|
||||
sub.name AS name,
|
||||
COUNT(DISTINCT memo.seq) AS memo_cnt,
|
||||
sub.code AS code,
|
||||
sub.status AS status,
|
||||
sub.approval_status AS approval_status,
|
||||
"" AS policyTopic,
|
||||
sub.thumbnail AS thumbnail,
|
||||
"" AS assets,
|
||||
sub.landingUrl AS landingUrl,
|
||||
0 AS budget,
|
||||
0 AS bidamount,
|
||||
"" AS bidamount_type,
|
||||
SUM(sub.impressions) AS impressions,
|
||||
SUM(sub.click) AS click,
|
||||
SUM(sub.spend) AS spend,
|
||||
SUM(sub.sales) AS sales,
|
||||
SUM(sub.unique_total) AS unique_total,
|
||||
SUM(sub.margin) AS margin,
|
||||
JSON_OBJECTAGG(CONCAT(sub.date, "_", sub.hour), JSON_OBJECT(
|
||||
"impressions", sub.impressions,
|
||||
"click", sub.click,
|
||||
"spend", sub.spend,
|
||||
"sales", sub.sales,
|
||||
"unique_total", sub.unique_total,
|
||||
"cpa", IFNULL(ROUND(sub.spend/sub.unique_total, 1), 0),
|
||||
"margin", sub.margin,
|
||||
"margin_ratio", IFNULL(ROUND((sub.margin/sub.sales)*100, 1), 0)
|
||||
)) AS metrics,
|
||||
sub.create_date
|
||||
');
|
||||
$builder->join('z_moment.mm_adgroup C', 'sub.adgroup_id = C.id');
|
||||
$builder->join('z_moment.mm_campaign D', 'C.campaign_id = D.id');
|
||||
$builder->join('z_moment.mm_ad_account E', 'D.ad_account_id = E.id');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.id = F.ad_account_id AND F.media = "kakao"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->join('zenith.advertisement_memo memo', 'sub.id = memo.id AND memo.media = "kakao" AND memo.type = "ads" AND is_done <> 1', 'left');
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['account'])){
|
||||
$builder->whereIn('E.id', explode("|",$data['account']));
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['stx'])){
|
||||
$builder->groupStart();
|
||||
$builder->like('sub.name', $data['stx']);
|
||||
$builder->groupEnd();
|
||||
}
|
||||
|
||||
$builder->groupBy('F.company_id');
|
||||
$builder->groupBy('sub.id');
|
||||
$builder->orderBy('sub.create_date', 'desc');
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getReport($data)
|
||||
{
|
||||
$builder = $this->db->table('z_moment.mm_creative_report_basic A');
|
||||
$builder->select('
|
||||
A.date,
|
||||
SUM(A.imp) AS impressions,
|
||||
SUM(A.click) AS click,
|
||||
(SUM(A.click) / SUM(A.imp)) * 100 AS click_ratio,
|
||||
(SUM(A.db_count) / SUM(A.click)) * 100 AS conversion_ratio,
|
||||
SUM(A.cost) AS spend,
|
||||
SUM(A.db_count) AS unique_total,
|
||||
IFNULL(SUM(A.cost) / SUM(A.db_count),0) AS unique_one_price,
|
||||
SUM(A.db_price) AS unit_price,
|
||||
SUM(A.sales) AS price,
|
||||
SUM(A.margin) AS profit,
|
||||
(SUM(A.db_price * A.db_count) - SUM(A.cost)) / SUM(A.db_price * A.db_count) * 100 AS per
|
||||
');
|
||||
$builder->join('z_moment.mm_creative B', 'A.id = B.id');
|
||||
$builder->join('z_moment.mm_adgroup C', 'B.adgroup_id = C.id');
|
||||
$builder->join('z_moment.mm_campaign D', 'C.campaign_id = D.id');
|
||||
$builder->join('z_moment.mm_ad_account E', 'D.ad_account_id = E.id');
|
||||
$builder->join('zenith.company_adaccounts F', 'E.id = F.ad_account_id AND F.media = "kakao"');
|
||||
$builder->join('zenith.companies G', 'F.company_id = G.id');
|
||||
$builder->where('A.cost IS NOT NULL AND A.imp !=', 0);
|
||||
if(!empty($data['sdate']) && !empty($data['edate'])){
|
||||
$builder->where('A.date >=', $data['sdate']);
|
||||
$builder->where('A.date <', date('Y-m-d', strtotime($data['edate'].' + 1day')));
|
||||
}
|
||||
|
||||
if(!empty($data['kakaoCheck'])){
|
||||
switch ($data['type']) {
|
||||
case 'campaigns':
|
||||
$builder->whereIn('D.id', $data['kakaoCheck']);
|
||||
break;
|
||||
case 'adsets':
|
||||
$builder->whereIn('C.id', $data['kakaoCheck']);
|
||||
break;
|
||||
case 'ads':
|
||||
$builder->whereIn('B.id', $data['kakaoCheck']);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!empty($data['resta'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['resta']));
|
||||
}
|
||||
|
||||
if(!empty($data['company'])){
|
||||
$builder->whereIn('G.id', explode("|",$data['company']));
|
||||
}
|
||||
|
||||
if(!empty($data['account'])){
|
||||
$builder->whereIn('E.id', explode("|",$data['account']));
|
||||
}
|
||||
|
||||
$builder->groupBy('A.date');
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getDisapproval()
|
||||
{
|
||||
$builder = $this->db->table('z_moment.mm_ad_account acc');
|
||||
$builder->select('acc.id AS account_id');
|
||||
$builder->join('z_moment.mm_campaign ac', 'ac.ad_account_id = acc.id', 'left');
|
||||
$builder->join('z_moment.mm_adgroup ag', 'ag.campaign_id = ac.id', 'left');
|
||||
$builder->join('z_moment.mm_creative ad', 'ad.adgroup_id = ag.id', 'left');
|
||||
$builder->where("(ad.reviewStatus = 'REJECTED' OR ad.reviewStatus = 'MODIFICATION_REJECTED')");
|
||||
$builder->where('ad.creativeStatus is NOT NULL', NULL);
|
||||
$builder->where('acc.config', 'ON');
|
||||
$builder->where('ac.config', 'ON');
|
||||
$builder->where('ag.config', 'ON');
|
||||
$builder->where('ad.config', 'ON');
|
||||
// $builder->orderBy('ad.create_time', 'DESC');
|
||||
$builder->groupBy('acc.id');
|
||||
$result = $builder->get()->getResultArray();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getCampaignById($ids)
|
||||
{
|
||||
$builder = $this->db->table('z_moment.mm_campaign');
|
||||
$builder->select('"카카오" as media, "캠페인" as type, id, name, config as status');
|
||||
$builder->whereIn('id', $ids);
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getAdgroupById($ids)
|
||||
{
|
||||
$builder = $this->db->table('z_moment.mm_adgroup');
|
||||
$builder->select('"카카오" as media, "광고그룹" as type, id, name, config as status');
|
||||
$builder->whereIn('id', $ids);
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function getAdById($ids)
|
||||
{
|
||||
$builder = $this->db->table('z_moment.aw_ad');
|
||||
$builder->select('"카카오" as media, "광고" as type, id, name, config as status');
|
||||
$builder->whereIn('id', $ids);
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function updateCode($data) {
|
||||
$result = [];
|
||||
$this->db->transStart();
|
||||
$builder = $this->db->table('z_moment.mm_creative');
|
||||
$builder->set('code', $data['code']);
|
||||
$builder->where('id', $data['id']);
|
||||
$builder->update();
|
||||
$queryResult = $this->db->transComplete();
|
||||
if($queryResult){
|
||||
$result['code'] = $data['code'] ?? '';
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getAccountByCampaignId($campaignIds) {
|
||||
$builder = $this->db->table('z_moment.mm_campaign A');
|
||||
$builder->select('id, ad_account_id');
|
||||
$builder->whereIn('id', $campaignIds);
|
||||
$builder->groupBy('id');
|
||||
$result = $builder->get()->getResultArray();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function setUpdatingByAds($campaignIds){
|
||||
$this->db->transStart();
|
||||
$builder = $this->db->table('z_moment.mm_campaign');
|
||||
$builder->whereIn('id', $campaignIds);
|
||||
$builder->set('is_updating', 1);
|
||||
$builder->update();
|
||||
$result = $this->db->transComplete();
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,581 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Advertiser;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class AdvManagerModel extends Model
|
||||
{
|
||||
protected $zenith, $facebook, $google, $kakao;
|
||||
public function __construct()
|
||||
{
|
||||
$this->zenith = \Config\Database::connect();
|
||||
$this->facebook = model(AdvFacebookManagerModel::class);
|
||||
$this->google = model(AdvGoogleManagerModel::class);
|
||||
$this->kakao = model(AdvKakaoManagerModel::class);
|
||||
}
|
||||
|
||||
public function getAccounts($data)
|
||||
{
|
||||
return $this->getQueryResults($data, 'getAccounts');
|
||||
}
|
||||
|
||||
public function getMediaAccounts($data)
|
||||
{
|
||||
return $this->getQueryResults($data, 'getMediaAccounts');
|
||||
}
|
||||
|
||||
public function getCampaigns($data)
|
||||
{
|
||||
return $this->getQueryResults($data, 'getCampaigns');
|
||||
}
|
||||
|
||||
public function getAdSets($data)
|
||||
{
|
||||
return $this->getQueryResults($data, 'getAdsets');
|
||||
}
|
||||
|
||||
public function getAds($data)
|
||||
{
|
||||
return $this->getQueryResults($data, 'getAds');
|
||||
}
|
||||
|
||||
public function getOnlyAdAccount($data)
|
||||
{
|
||||
$data['searchData']['stx'] = $data['stx'] ?? '';
|
||||
$data['searchData']['media'] = 'facebook|google';
|
||||
return $this->getQueryResults($data, 'getOnlyAdAccount');
|
||||
}
|
||||
|
||||
public function getReport($data)
|
||||
{
|
||||
if(!empty($data['check'])){
|
||||
$data = $this->setArgs($data);
|
||||
}
|
||||
|
||||
return $this->getQueryResults($data, 'getReport');
|
||||
}
|
||||
|
||||
private function setArgs($data)
|
||||
{
|
||||
if (!isset($data['searchData']['data'])) {
|
||||
// data 키가 없을 경우 기본값 설정 또는 예외 처리
|
||||
return $data;
|
||||
}
|
||||
$kakaoNumbers = array();
|
||||
$googleNumbers = array();
|
||||
$facebookNumbers = array();
|
||||
foreach($data['searchData']['data'][$data['searchData']['type']] as $value) {
|
||||
$parts = explode('_', $value);
|
||||
$media = array_shift($parts);
|
||||
$id = end($parts);
|
||||
|
||||
switch ($media) {
|
||||
case 'kakao': $kakaoNumbers[] = $id; break;
|
||||
case 'google': $googleNumbers[] = $id; break;
|
||||
case 'facebook': $facebookNumbers[] = $id; break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
$data['searchData']['facebookCheck'] = $facebookNumbers;
|
||||
$data['searchData']['googleCheck'] = $googleNumbers;
|
||||
$data['searchData']['kakaoCheck'] = $kakaoNumbers;
|
||||
unset($facebookNumbers, $googleNumbers, $kakaoNumbers, $data['searchData']['media']);
|
||||
$data['searchData']['media'] = [];
|
||||
|
||||
if(!empty($data['searchData']['facebookCheck'])){
|
||||
array_push($data['searchData']['media'], 'facebook');
|
||||
}
|
||||
|
||||
if(!empty($data['searchData']['googleCheck'])){
|
||||
array_push($data['searchData']['media'], 'google');
|
||||
}
|
||||
|
||||
if(!empty($data['searchData']['kakaoCheck'])){
|
||||
array_push($data['searchData']['media'], 'kakao');
|
||||
}
|
||||
|
||||
$data['searchData']['media'] = implode("|", $data['searchData']['media']);
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function getQueryResults($data, $type)
|
||||
{
|
||||
// $cache = \Config\Services::cache();
|
||||
// $cacheName = $type."_".md5(json_encode($data['searchData']));
|
||||
// $cacheData = $cache->get($cacheName);
|
||||
// if($cacheName && $cacheData) return $cacheData;
|
||||
|
||||
$builders = [];
|
||||
if(!isset($data['searchData'])) {
|
||||
$data['searchData'] = [
|
||||
'media' => 'facebook|google|kakao',
|
||||
'sdate' => date('Y-m-d'),
|
||||
'edate' => date('Y-m-d'),
|
||||
];
|
||||
}
|
||||
if(!isset($data['searchData']['media']) || empty($data['searchData']['media'])){
|
||||
$media = ['facebook', 'google', 'kakao'];
|
||||
}else{
|
||||
$media = explode("|", $data['searchData']['media']);
|
||||
}
|
||||
|
||||
if (in_array('facebook', $media)) {
|
||||
$facebookBuilder = $this->facebook->$type($data['searchData']);
|
||||
$builders[] = $facebookBuilder;
|
||||
}
|
||||
|
||||
if (in_array('google', $media)) {
|
||||
$googleBuilder = $this->google->$type($data['searchData']);
|
||||
$builders[] = $googleBuilder;
|
||||
}
|
||||
|
||||
if (in_array('kakao', $media)) {
|
||||
$kakaoBuilder = $this->kakao->$type($data['searchData']);
|
||||
$builders[] = $kakaoBuilder;
|
||||
}
|
||||
|
||||
$unionBuilder = null;
|
||||
foreach ($builders as $builder) {
|
||||
if ($unionBuilder) {
|
||||
$unionBuilder->union($builder);
|
||||
} else {
|
||||
$unionBuilder = $builder;
|
||||
}
|
||||
}
|
||||
|
||||
if ($unionBuilder) {
|
||||
$resultQuery = $this->zenith->newQuery()->fromSubquery($unionBuilder, 'adv');
|
||||
if($type == 'getAccounts'){
|
||||
$resultQuery->groupBy('adv.company_id');
|
||||
$resultQuery->orderBy('adv.company_name', 'asc');
|
||||
}
|
||||
|
||||
if($type == 'getMediaAccounts'){
|
||||
$resultQuery->groupBy('adv.media_account_id');
|
||||
$resultQuery->orderBy('adv.media_account_name', 'asc');
|
||||
}
|
||||
|
||||
if($type == 'getAdsets' || $type == 'getAds') {
|
||||
$ids = [];
|
||||
if($type == 'getAdsets' && (isset($data['searchData']['data']['campaigns']) && count($data['searchData']['data']['campaigns']))) {
|
||||
$ids = array_map(function($v) { $v=explode('_', $v); return (integer)array_pop($v); }, $data['searchData']['data']['campaigns']);
|
||||
$resultQuery->whereIn('campaign_id', $ids);
|
||||
} else if($type == 'getAds') {
|
||||
if(isset($data['searchData']['data']['campaigns']) && count($data['searchData']['data']['campaigns'])) {
|
||||
$ids = array_map(function($v) { $v=explode('_', $v); return (integer)array_pop($v); }, $data['searchData']['data']['campaigns']);
|
||||
$resultQuery->whereIn('campaign_id', $ids);
|
||||
}
|
||||
if(isset($data['searchData']['data']['adsets']) && count($data['searchData']['data']['adsets'])) {
|
||||
$ids = array_map(function($v) { $v=explode('_', $v); return (integer)array_pop($v); }, $data['searchData']['data']['adsets']);
|
||||
$resultQuery->whereIn('adset_id', $ids);
|
||||
}
|
||||
}
|
||||
}
|
||||
if($type == 'getCampaigns' || $type == 'getAdsets' || $type == 'getAds'){
|
||||
$resultQuery->groupBy('id');
|
||||
$resultQuery->orderBy('create_date', 'desc');
|
||||
}
|
||||
// echo $resultQuery->getCompiledSelect(); exit;
|
||||
$result = $resultQuery->get()->getResultArray();
|
||||
} else {
|
||||
$result = null;
|
||||
}
|
||||
// if(!$cacheData)
|
||||
// $cache->save($cacheName, $result, 300);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getAccountStat($data)
|
||||
{
|
||||
if (empty($data['media'])) {
|
||||
$data['media'] = 'facebook|google|kakao';
|
||||
}
|
||||
if (empty($data['sdate'])) {
|
||||
$data['sdate'] = date('Y-m-d');
|
||||
}
|
||||
if (empty($data['edate'])) {
|
||||
$data['edate'] = date('Y-m-d');
|
||||
}
|
||||
$media = explode("|", $data['media']);
|
||||
|
||||
$sdate = $data['sdate'];
|
||||
$edate = $data['edate'];
|
||||
|
||||
$queries = [];
|
||||
|
||||
if (in_array('google', $media)) {
|
||||
$queries[] = "
|
||||
SELECT
|
||||
e.customerId, e.name,
|
||||
ROUND(SUM(a.cost) / SUM(a.db_count)) AS cpa,
|
||||
SUM(a.db_count) AS db_count,
|
||||
SUM(a.cost) AS cost,
|
||||
SUM(a.margin) AS margin,
|
||||
ROUND(SUM(a.margin) / SUM(a.sales) * 100, 2) AS margin_ratio,
|
||||
SUM(a.sales) AS sales,
|
||||
'google' AS source,
|
||||
ROUND(SUM(a.margin) / SUM(SUM(a.margin)) OVER () * 100, 2) AS total_margin_ratio
|
||||
FROM z_adwords.aw_adgroup_report_history AS a
|
||||
LEFT JOIN z_adwords.aw_adgroup AS c ON a.adgroup_id = c.id
|
||||
LEFT JOIN z_adwords.aw_campaign AS d ON c.campaignId = d.id
|
||||
LEFT JOIN z_adwords.aw_ad_account AS e ON d.customerId = e.customerId
|
||||
WHERE a.date BETWEEN '{$sdate}' AND '{$edate}' AND e.is_hidden = 0 AND e.is_exposed = 1
|
||||
GROUP BY e.customerId, e.name
|
||||
";
|
||||
}
|
||||
|
||||
if (in_array('facebook', $media)) {
|
||||
$queries[] = "
|
||||
SELECT
|
||||
E.ad_account_id AS customerId,
|
||||
E.name,
|
||||
ROUND(SUM(A.spend) / SUM(A.db_count)) AS cpa,
|
||||
SUM(A.db_count) AS db_count,
|
||||
SUM(A.spend) AS cost,
|
||||
SUM(A.margin) AS margin,
|
||||
ROUND(SUM(A.margin) / SUM(A.sales) * 100, 2) AS margin_ratio,
|
||||
SUM(A.sales) AS sales,
|
||||
'facebook' AS source,
|
||||
ROUND(SUM(A.margin) / SUM(SUM(A.margin)) OVER () * 100, 2) AS total_margin_ratio
|
||||
FROM z_facebook.fb_ad_insight_history AS A
|
||||
LEFT JOIN z_facebook.fb_ad AS B ON A.ad_id = B.ad_id
|
||||
LEFT JOIN z_facebook.fb_adset AS C ON B.adset_id = C.adset_id
|
||||
LEFT JOIN z_facebook.fb_campaign AS D ON C.campaign_id = D.campaign_id
|
||||
LEFT JOIN z_facebook.fb_ad_account AS E ON D.account_id = E.ad_account_id
|
||||
WHERE A.date BETWEEN '{$sdate}' AND '{$edate}' AND E.perm = 1 AND E.status = 1
|
||||
GROUP BY E.ad_account_id, E.name
|
||||
";
|
||||
}
|
||||
|
||||
if (in_array('kakao', $media)) {
|
||||
$queries[] = "
|
||||
SELECT
|
||||
E.id AS customerId,
|
||||
E.name,
|
||||
ROUND(SUM(A.cost) / SUM(A.db_count)) AS cpa,
|
||||
SUM(A.db_count) AS db_count,
|
||||
SUM(A.cost) AS cost,
|
||||
SUM(A.margin) AS margin,
|
||||
ROUND(SUM(A.margin) / SUM(A.sales) * 100, 2) AS margin_ratio,
|
||||
SUM(A.sales) AS sales,
|
||||
'kakao' AS source,
|
||||
ROUND(SUM(A.margin) / SUM(SUM(A.margin)) OVER () * 100, 2) AS total_margin_ratio
|
||||
FROM z_moment.mm_creative_report_basic AS A
|
||||
LEFT JOIN z_moment.mm_creative AS B ON A.id = B.id
|
||||
LEFT JOIN z_moment.mm_adgroup AS C ON B.adgroup_id = C.id
|
||||
LEFT JOIN z_moment.mm_campaign AS D ON C.campaign_id = D.id
|
||||
LEFT JOIN z_moment.mm_ad_account AS E ON D.ad_account_id = E.id
|
||||
WHERE A.date BETWEEN '{$sdate}' AND '{$edate}' AND E.config = 'ON' AND E.is_update = 1
|
||||
GROUP BY E.id, E.name
|
||||
";
|
||||
}
|
||||
|
||||
$combinedSql = implode(" UNION ALL ", $queries);
|
||||
$finalQuery = "
|
||||
WITH combined_data AS (
|
||||
{$combinedSql}
|
||||
)
|
||||
SELECT
|
||||
source, customerId, name, cpa, db_count, cost, margin, margin_ratio, sales, total_margin_ratio,
|
||||
ROUND(margin / SUM(margin) OVER () * 100, 2) AS source_margin_ratio
|
||||
FROM combined_data
|
||||
ORDER BY name ASC
|
||||
";
|
||||
|
||||
$query = $this->zenith->query($finalQuery);
|
||||
return $query->getResultArray();
|
||||
}
|
||||
|
||||
public function getMemo($data = null)
|
||||
{
|
||||
$fbBuilder = $this->zenith->table('z_facebook.fb_ad_account AS faa');
|
||||
$fbBuilder->select('am.*, faa.name AS account_name, fc.campaign_name AS campaign_name, fas.adset_name AS adset_name, fa.ad_name AS ad_name');
|
||||
$fbBuilder->join('z_facebook.fb_campaign AS fc', 'faa.ad_account_id = fc.account_id');
|
||||
$fbBuilder->join('z_facebook.fb_adset AS fas', 'fc.campaign_id = fas.campaign_id');
|
||||
$fbBuilder->join('z_facebook.fb_ad AS fa', 'fas.adset_id = fa.adset_id');
|
||||
$fbBuilder->join('zenith.advertisement_memo AS am', "am.media = 'facebook' AND ((fa.ad_id = am.id AND am.type = 'ads') OR (fas.adset_id = am.id AND am.type = 'adsets') OR (fc.campaign_id = am.id AND am.type = 'campaigns'))");
|
||||
|
||||
$ggBuilder = $this->zenith->table('z_adwords.aw_ad_account AS aaa');
|
||||
$ggBuilder->select('am.*, aaa.name AS account_name, ac.name AS campaign_name, aag.name AS adset_name, aa.name AS ad_name');
|
||||
$ggBuilder->join('z_adwords.aw_campaign ac', 'aaa.customerId = ac.customerId');
|
||||
$ggBuilder->join('z_adwords.aw_adgroup aag', 'ac.id = aag.campaignId');
|
||||
$ggBuilder->join('z_adwords.aw_ad aa', 'aag.id = aa.adgroupId');
|
||||
$ggBuilder->join('zenith.advertisement_memo AS am', "am.media = 'google' AND ((aa.id = am.id AND am.type = 'ads') OR (aag.id = am.id AND am.type = 'adsets') OR (ac.id = am.id AND am.type = 'campaigns'))");
|
||||
|
||||
$kkBuilder = $this->zenith->table('z_moment.mm_ad_account AS maa');
|
||||
$kkBuilder->select('am.*, maa.name AS account_name, mc.name AS campaign_name, mag.name AS adset_name, mct.name AS ad_name');
|
||||
$kkBuilder->join('z_moment.mm_campaign mc', 'maa.id = mc.ad_account_id');
|
||||
$kkBuilder->join('z_moment.mm_adgroup mag', 'mc.id = mag.campaign_id');
|
||||
$kkBuilder->join('z_moment.mm_creative mct', 'mag.id = mct.adgroup_id');
|
||||
$kkBuilder->join('zenith.advertisement_memo AS am', "am.media = 'kakao' AND ((mct.id = am.id AND am.type = 'ads') OR (mag.id = am.id AND am.type = 'adsets') OR (mc.id = am.id AND am.type = 'campaigns'))");
|
||||
|
||||
$fbBuilder->union($ggBuilder)->union($kkBuilder);
|
||||
$resultQuery = $this->zenith->newQuery()->fromSubquery($fbBuilder, 'memo');
|
||||
if(!empty($data['id'])){
|
||||
$resultQuery->where('memo.id', $data['id']);
|
||||
$resultQuery->where('memo.type', $data['type']);
|
||||
} else {
|
||||
$resultQuery->where("(memo.datetime >= DATE_SUB(NOW(), INTERVAL 3 DAY) OR (memo.datetime <= DATE_SUB(NOW(), INTERVAL 3 DAY) AND memo.is_done = 0))");
|
||||
}
|
||||
$resultQuery->groupBy('memo.seq');
|
||||
$resultQuery->orderBy('memo.datetime', 'desc');
|
||||
$result = $resultQuery->get()->getResultArray();
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function addMemo($data)
|
||||
{
|
||||
$this->zenith->transStart();
|
||||
$builder = $this->zenith->table('advertisement_memo');
|
||||
$builder->insert($data);
|
||||
$result = $this->zenith->transComplete();
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function checkMemo($data)
|
||||
{
|
||||
$this->zenith->transStart();
|
||||
$builder = $this->zenith->table('advertisement_memo');
|
||||
$builder->set('is_done', $data['is_done']);
|
||||
$builder->set('done_nickname', $data['done_nickname']);
|
||||
$builder->where('seq', $data['seq']);
|
||||
$builder->update();
|
||||
$result = $this->zenith->transComplete();
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getAdvs($data)
|
||||
{
|
||||
$kakaoNumbers = array();
|
||||
$googleNumbers = array();
|
||||
$facebookNumbers = array();
|
||||
|
||||
foreach ($data['check'] as $value) {
|
||||
$parts = explode('_', $value);
|
||||
$id = $parts[1];
|
||||
|
||||
switch ($parts[0]) {
|
||||
case 'kakao':
|
||||
$kakaoNumbers[] = $id;
|
||||
break;
|
||||
case 'google':
|
||||
$googleNumbers[] = $id;
|
||||
break;
|
||||
case 'facebook':
|
||||
$facebookNumbers[] = $id;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$data['facebook'] = $facebookNumbers;
|
||||
$data['google'] = $googleNumbers;
|
||||
$data['kakao'] = $kakaoNumbers;
|
||||
|
||||
switch ($data['type']) {
|
||||
case 'campaigns':
|
||||
if(!empty($data['facebook'])){
|
||||
$facebookBuilder = $this->facebook->getCampaignById($data['facebook']);
|
||||
$builders[] = $facebookBuilder;
|
||||
}
|
||||
|
||||
if(!empty($data['google'])){
|
||||
$googleBuilder = $this->google->getCampaignById($data['google']);
|
||||
$builders[] = $googleBuilder;
|
||||
}
|
||||
|
||||
if(!empty($data['kakao'])){
|
||||
$kakaoBuilder = $this->kakao->getCampaignById($data['kakao']);
|
||||
$builders[] = $kakaoBuilder;
|
||||
}
|
||||
break;
|
||||
case 'adsets':
|
||||
if(!empty($data['facebook'])){
|
||||
$facebookBuilder = $this->facebook->getAdgroupById($data['facebook']);
|
||||
$builders[] = $facebookBuilder;
|
||||
}
|
||||
|
||||
if(!empty($data['google'])){
|
||||
$googleBuilder = $this->google->getAdgroupById($data['google']);
|
||||
$builders[] = $googleBuilder;
|
||||
}
|
||||
|
||||
if(!empty($data['kakao'])){
|
||||
$kakaoBuilder = $this->kakao->getAdgroupById($data['kakao']);
|
||||
$builders[] = $kakaoBuilder;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'ads':
|
||||
if(!empty($data['facebook'])){
|
||||
$facebookBuilder = $this->facebook->getAdById($data['facebook']);
|
||||
$builders[] = $facebookBuilder;
|
||||
}
|
||||
|
||||
if(!empty($data['google'])){
|
||||
$googleBuilder = $this->google->getAdById($data['google']);
|
||||
$builders[] = $googleBuilder;
|
||||
}
|
||||
|
||||
if(!empty($data['kakao'])){
|
||||
$kakaoBuilder = $this->kakao->getAdById($data['kakao']);
|
||||
$builders[] = $kakaoBuilder;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
$unionBuilder = null;
|
||||
foreach ($builders as $builder) {
|
||||
if ($unionBuilder) {
|
||||
$unionBuilder->union($builder);
|
||||
|
||||
} else {
|
||||
$unionBuilder = $builder;
|
||||
}
|
||||
}
|
||||
|
||||
$result = $unionBuilder->get()->getResultArray();
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getChangeLogs($id = null, $dates = null)
|
||||
{
|
||||
$builder = $this->zenith->table('adv_change_logs');
|
||||
$builder->select('adv_change_logs.*, IFNULL(users.nickname, adv_change_logs.nickname) as nickname');
|
||||
$builder->join('users', 'adv_change_logs.nickname = users.username', 'left');
|
||||
|
||||
if (!empty($id)) {
|
||||
$builder->where('adv_change_logs.id', $id);
|
||||
}
|
||||
|
||||
if (!empty($dates)) {
|
||||
$builder->where('adv_change_logs.datetime >=', date('Y-m-d', strtotime($dates['sdate'])));
|
||||
$builder->where('adv_change_logs.datetime <', date('Y-m-d', strtotime($dates['edate']. '+1 day')));
|
||||
}
|
||||
$builder->orderBy('adv_change_logs.datetime', 'DESC');
|
||||
|
||||
$changeLogs = $builder->get()->getResultArray();
|
||||
|
||||
foreach ($changeLogs as &$log) {
|
||||
$media = $log['media'];
|
||||
$type = $log['type'];
|
||||
$id = $log['id'];
|
||||
$name = '';
|
||||
$accountName = '';
|
||||
|
||||
switch ($media) {
|
||||
case 'facebook':
|
||||
switch ($type) {
|
||||
case 'campaigns':
|
||||
case 'campaign':
|
||||
$name = $this->zenith->table('z_facebook.fb_campaign')->select('campaign_name')->where('campaign_id', $id)->get()->getRow()->campaign_name;
|
||||
$accountName = $this->zenith->table('z_facebook.fb_campaign')
|
||||
->select('z_facebook.fb_ad_account.name')
|
||||
->join('z_facebook.fb_ad_account', 'z_facebook.fb_campaign.account_id = z_facebook.fb_ad_account.ad_account_id')
|
||||
->where('z_facebook.fb_campaign.campaign_id', $id)
|
||||
->get()->getRow()->name;
|
||||
break;
|
||||
case 'adsets':
|
||||
case 'adset':
|
||||
$name = $this->zenith->table('z_facebook.fb_adset')->select('adset_name')->where('adset_id', $id)->get()->getRow()->adset_name;
|
||||
$accountName = $this->zenith->table('z_facebook.fb_adset')
|
||||
->select('z_facebook.fb_ad_account.name')
|
||||
->join('z_facebook.fb_campaign', 'z_facebook.fb_adset.campaign_id = z_facebook.fb_campaign.campaign_id')
|
||||
->join('z_facebook.fb_ad_account', 'z_facebook.fb_campaign.account_id = z_facebook.fb_ad_account.ad_account_id')
|
||||
->where('z_facebook.fb_adset.adset_id', $id)
|
||||
->get()->getRow()->name;
|
||||
break;
|
||||
case 'ads':
|
||||
case 'ad':
|
||||
$name = $this->zenith->table('z_facebook.fb_ad')->select('ad_name')->where('ad_id', $id)->get()->getRow()->ad_name;
|
||||
$accountName = $this->zenith->table('z_facebook.fb_ad')
|
||||
->select('z_facebook.fb_ad_account.name')
|
||||
->join('z_facebook.fb_adset', 'z_facebook.fb_ad.adset_id = z_facebook.fb_adset.adset_id')
|
||||
->join('z_facebook.fb_campaign', 'z_facebook.fb_adset.campaign_id = z_facebook.fb_campaign.campaign_id')
|
||||
->join('z_facebook.fb_ad_account', 'z_facebook.fb_campaign.account_id = z_facebook.fb_ad_account.ad_account_id')
|
||||
->where('z_facebook.fb_ad.ad_id', $id)
|
||||
->get()->getRow()->name;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 'google':
|
||||
switch ($type) {
|
||||
case 'campaigns':
|
||||
case 'campaign':
|
||||
$name = $this->zenith->table('z_adwords.aw_campaign')->select('name')->where('id', $id)->get()->getRow()->name;
|
||||
$accountName = $this->zenith->table('z_adwords.aw_campaign')
|
||||
->select('z_adwords.aw_ad_account.name')
|
||||
->join('z_adwords.aw_ad_account', 'z_adwords.aw_campaign.customerId = z_adwords.aw_ad_account.customerId')
|
||||
->where('z_adwords.aw_campaign.id', $id)
|
||||
->get()->getRow()->name;
|
||||
break;
|
||||
case 'adsets':
|
||||
case 'adset':
|
||||
$name = $this->zenith->table('z_adwords.aw_adgroup')->select('name')->where('id', $id)->get()->getRow()->name;
|
||||
$accountName = $this->zenith->table('z_adwords.aw_adgroup')
|
||||
->select('z_adwords.aw_ad_account.name')
|
||||
->join('z_adwords.aw_campaign', 'z_adwords.aw_adgroup.campaignId = z_adwords.aw_campaign.id')
|
||||
->join('z_adwords.aw_ad_account', 'z_adwords.aw_campaign.customerId = z_adwords.aw_ad_account.customerId')
|
||||
->where('z_adwords.aw_adgroup.id', $id)
|
||||
->get()->getRow()->name;
|
||||
break;
|
||||
case 'ads':
|
||||
case 'ad':
|
||||
$name = $this->zenith->table('z_adwords.aw_ad')->select('name')->where('id', $id)->get()->getRow()->name;
|
||||
$accountName = $this->zenith->table('z_adwords.aw_ad')
|
||||
->select('z_adwords.aw_ad_account.name')
|
||||
->join('z_adwords.aw_adgroup', 'z_adwords.aw_ad.adgroupId = z_adwords.aw_adgroup.id')
|
||||
->join('z_adwords.aw_campaign', 'z_adwords.aw_adgroup.campaignId = z_adwords.aw_campaign.id')
|
||||
->join('z_adwords.aw_ad_account', 'z_adwords.aw_campaign.customerId = z_adwords.aw_ad_account.customerId')
|
||||
->where('z_adwords.aw_ad.id', $id)
|
||||
->get()->getRow()->name;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 'kakao':
|
||||
switch ($type) {
|
||||
case 'campaigns':
|
||||
case 'campaign':
|
||||
$name = $this->zenith->table('z_moment.mm_campaign')->select('name')->where('id', $id)->get()->getRow()->name;
|
||||
$accountName = $this->zenith->table('z_moment.mm_campaign')
|
||||
->select('z_moment.mm_ad_account.name')
|
||||
->join('z_moment.mm_ad_account', 'z_moment.mm_campaign.ad_account_id = z_moment.mm_ad_account.id')
|
||||
->where('z_moment.mm_campaign.id', $id)
|
||||
->get()->getRow()->name;
|
||||
break;
|
||||
case 'adsets':
|
||||
case 'adset':
|
||||
$name = $this->zenith->table('z_moment.mm_adgroup')->select('name')->where('id', $id)->get()->getRow()->name;
|
||||
$accountName = $this->zenith->table('z_moment.mm_adgroup')
|
||||
->select('z_moment.mm_ad_account.name')
|
||||
->join('z_moment.mm_campaign', 'z_moment.mm_adgroup.campaign_id = z_moment.mm_campaign.id')
|
||||
->join('z_moment.mm_ad_account', 'z_moment.mm_campaign.ad_account_id = z_moment.mm_ad_account.id')
|
||||
->where('z_moment.mm_adgroup.id', $id)
|
||||
->get()->getRow()->name;
|
||||
break;
|
||||
case 'ads':
|
||||
case 'ad':
|
||||
$name = $this->zenith->table('z_moment.mm_creative')->select('name')->where('id', $id)->get()->getRow()->name;
|
||||
$accountName = $this->zenith->table('z_moment.mm_creative')
|
||||
->select('z_moment.mm_ad_account.name')
|
||||
->join('z_moment.mm_adgroup', 'z_moment.mm_creative.adgroup_id = z_moment.mm_adgroup.id')
|
||||
->join('z_moment.mm_campaign', 'z_moment.mm_adgroup.campaign_id = z_moment.mm_campaign.id')
|
||||
->join('z_moment.mm_ad_account', 'z_moment.mm_campaign.ad_account_id = z_moment.mm_ad_account.id')
|
||||
->where('z_moment.mm_creative.id', $id)
|
||||
->get()->getRow()->name;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
$log['item_name'] = $name;
|
||||
$log['account_name'] = $accountName;
|
||||
}
|
||||
return $changeLogs;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Advertiser;
|
||||
|
||||
use App\Libraries\Calc;
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class AdvNaverManagerModel extends Model
|
||||
{
|
||||
protected $naver, $ro_naver;
|
||||
public function __construct()
|
||||
{
|
||||
$this->naver = \Config\Database::connect('naver');
|
||||
$this->ro_naver = \Config\Database::connect('ro_naver');
|
||||
}
|
||||
|
||||
public function getCampaigns($data)
|
||||
{
|
||||
$srch = $data['searchData'];
|
||||
$builder = $this->naver->table('gfa_ad_report_history');
|
||||
$builder->select('"네이버" AS media, CONCAT("naver_", campaign_id) AS id, campaign_name AS name,
|
||||
COUNT(adset_id) AS adgroups, COUNT(ad_id) AS ads, SUM(impression) AS impressions, SUM(click) AS click, SUM(sales) AS spend, SUM(db_sales) AS sales, SUM(db_count) as unique_total, 0 AS budget, 0 AS status');
|
||||
|
||||
if(!empty($data['dates']['sdate']) && !empty($data['dates']['edate'])){
|
||||
$builder->where('date >=', $data['dates']['sdate']);
|
||||
$builder->where('date <=', date('Y-m-d', strtotime($data['dates']['edate'].' + 1day')));
|
||||
}
|
||||
|
||||
if(!empty($data['accounts'])){
|
||||
$builder->whereIn('account_id', explode("|",$srch['accounts']));
|
||||
}
|
||||
|
||||
if(!empty($data['stx'])){
|
||||
$builder->groupStart();
|
||||
$builder->like('name', $data['stx']);
|
||||
$builder->groupEnd();
|
||||
}
|
||||
|
||||
$builder->groupBy('campaign_id');
|
||||
$builder->orderBy('create_time', 'desc');
|
||||
$builder->orderBy('name', 'asc');
|
||||
$result = $builder->get()->getResultArray();
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getAdsets($data)
|
||||
{
|
||||
$srch = $data['searchData'];
|
||||
$builder = $this->naver->table('gfa_ad_report_history');
|
||||
$builder->select('"네이버" AS media, CONCAT("naver_", adset_id) AS id, adset_name AS name,
|
||||
COUNT(ad_id) AS ads, SUM(impression) AS impressions,
|
||||
SUM(click) AS click, SUM(db_count) as unique_total, SUM(sales) AS spend, SUM(db_sales) AS sales, 0 AS budget, 0 AS status');
|
||||
|
||||
if(!empty($data['dates']['sdate']) && !empty($data['dates']['edate'])){
|
||||
$builder->where('date >=', $data['dates']['sdate']);
|
||||
$builder->where('date <=', date('Y-m-d', strtotime($data['dates']['edate'].' + 1day')));
|
||||
}
|
||||
|
||||
if(!empty($data['accounts'])){
|
||||
$builder->whereIn('account_id', explode("|",$srch['accounts']));
|
||||
}
|
||||
|
||||
if(!empty($data['stx'])){
|
||||
$builder->groupStart();
|
||||
$builder->like('name', $data['stx']);
|
||||
$builder->groupEnd();
|
||||
}
|
||||
|
||||
$builder->groupBy('adset_id');
|
||||
$builder->orderBy('create_time', 'desc');
|
||||
$builder->orderBy('adset_name', 'asc');
|
||||
$result = $builder->get()->getResultArray();
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getAds($data)
|
||||
{
|
||||
$srch = $data['searchData'];
|
||||
$builder = $this->naver->table('gfa_ad_report_history');
|
||||
$builder->select('"네이버" AS media, CONCAT("naver_", ad_id) AS id, ad_name AS name, SUM(impression) AS impressions, SUM(click) AS click, SUM(db_count) as unique_total, SUM(sales) AS spend, SUM(db_sales) AS sales, 0 AS budget, 0 AS status');
|
||||
|
||||
if(!empty($data['dates']['sdate']) && !empty($data['dates']['edate'])){
|
||||
$builder->where('date >=', $data['dates']['sdate']);
|
||||
$builder->where('date <=', date('Y-m-d', strtotime($data['dates']['edate'].' + 1day')));
|
||||
}
|
||||
|
||||
if(!empty($data['accounts'])){
|
||||
$builder->whereIn('account_id', explode("|",$srch['accounts']));
|
||||
}
|
||||
|
||||
if(!empty($data['stx'])){
|
||||
$builder->groupStart();
|
||||
$builder->like('name', $data['stx']);
|
||||
$builder->groupEnd();
|
||||
}
|
||||
|
||||
$builder->groupBy('ad_id');
|
||||
$builder->orderBy('create_time', 'desc');
|
||||
$builder->orderBy('ad_name', 'asc');
|
||||
$result = $builder->get()->getResultArray();
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getStatuses($param, $result, $dates)
|
||||
{
|
||||
foreach ($result as &$row) {
|
||||
|
||||
$row['cost_ori'] = $row['spend'];
|
||||
$row['spend'] = $row['spend']/1.1;
|
||||
$row['margin'] = $row['sales'] - $row['spend'];
|
||||
|
||||
$row['margin_ratio'] = Calc::margin_ratio($row['margin'], $row['sales']); // 수익률
|
||||
|
||||
$row['cpc'] = Calc::cpc($row['spend'], $row['click']); // 클릭당단가 (1회 클릭당 비용)
|
||||
$row['ctr'] = Calc::ctr($row['click'], $row['impressions']); // 클릭율 (노출 대비 클릭한 비율)
|
||||
$row['cpa'] = Calc::cpa($row['unique_total'], $row['spend']); //DB단가(전환당 비용)
|
||||
$row['cvr'] = Calc::cvr($row['unique_total'], $row['click']); //전환율
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getAccounts($data)
|
||||
{
|
||||
$builder = $this->naver->table('gfa_ad_account A');
|
||||
$builder->select('A.account_id AS id, A.name');
|
||||
$builder->join('gfa_ad_report_history B', 'A.account_id = B.account_id', 'left');
|
||||
|
||||
if(!empty($data['dates']['sdate']) && !empty($data['dates']['edate'])){
|
||||
$builder->where('B.date >=', $data['dates']['sdate']);
|
||||
$builder->where('B.date <', date('Y-m-d', strtotime($data['dates']['edate'].' + 1day')));
|
||||
}
|
||||
|
||||
$builder->where('A.name !=', '');
|
||||
$builder->groupBy('A.account_id');
|
||||
$builder->orderBy('A.name', 'asc');
|
||||
$result = $builder->get()->getResultArray();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getReport($data)
|
||||
{
|
||||
$builder = $this->naver->table('gfa_ad_report_history A');
|
||||
$builder->select('A.date, SUM(A.impression) AS impressions, SUM(A.click) AS click, (SUM(A.click) / SUM(A.impression)) * 100 AS click_ratio, (SUM(A.db_count) / SUM(A.click)) * 100 AS conversion_ratio, SUM(A.sales)/1.1 AS spend, FLOOR(SUM(A.sales)/1.1 * 0.85) AS spend_ratio, SUM(A.db_count) AS unique_total, IFNULL(SUM(A.sales)/1.1 / SUM(A.db_count),0) AS unique_one_price, SUM(A.db_price) AS unit_price, SUM(A.db_sales) AS price, SUM(A.margin) AS profit,
|
||||
(SUM(A.db_price * A.db_count) - SUM(A.sales)/1.1) / SUM(A.db_price * A.db_count) * 100 AS per');
|
||||
$builder->join('gfa_ad_account B', 'A.account_id = B.account_id', 'left');
|
||||
|
||||
if(!empty($data['dates']['sdate']) && !empty($data['dates']['edate'])){
|
||||
$builder->where('A.date >=', $data['dates']['sdate']);
|
||||
$builder->where('A.date <', date('Y-m-d', strtotime($data['dates']['edate'].' + 1day')));
|
||||
}
|
||||
|
||||
if(!empty($data['accounts'])){
|
||||
$builder->whereIn('B.account_id', explode("|",$data['accounts']));
|
||||
}
|
||||
|
||||
$builder->groupBy('A.date');
|
||||
$builder->orderBy('A.date', 'ASC');
|
||||
$result = $builder->get()->getResultArray();
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user