public static function getAcquire($beginDate, $endDate)
 {
     //customer acquisition per day
     $ll = new LL_Account();
     $arrw = $ll->getWhere("(macc_acquire_date BETWEEN '{$beginDate}' AND '{$endDate}')", "macc_acquire_date");
     //        pr($arrw);
     //        echo count($arrw);
     $data = array();
     //for begin date sampai end date day by day
     $start = strtotime($beginDate);
     $finish = strtotime($endDate);
     while ($start <= $finish) {
         $label = date("d", $start);
         $start = strtotime('+1 day', $start);
         $xLabels[] = $label;
         $data[$label] = 0;
     }
     //        pr($xLabels);
     //manage data
     foreach ($arrw as $w) {
         $curdate = date("d", strtotime($w->macc_acquire_date));
         $data[$curdate]++;
     }
     //        pr($data);
     $arr = array_values($data);
     //        pr($arr);
     $c = new Charting();
     $c->label = "User Acquisition";
     $c->data = $arr;
     $c->color = "#AAAAAA";
     $arrData2[] = $c;
     Charting::chartJSLine("300px", $xLabels, $arrData2, "false", 0, 0, "User Acquisition", "info", 0);
     return $arrw;
 }
 static function kerjakan($psn)
 {
     //cek apakah ada acc_id, kalau ada..forget filter
     $cols = "macc_id,macc_first_name,macc_last_name";
     $acc = $psn->camp_acc_id;
     if ($acc != "" || $psn->camp_account_file != "") {
         $arrAcc = array();
         if ($acc != "") {
             $exp = explode(",", $psn->camp_acc_id);
             foreach ($exp as $ac) {
                 $arrAcc[] = trim(rtrim($ac));
             }
             //from acc get device ID
             //                self::sendUsingAccountArray($arrAcc, $psn);
         }
         if ($psn->camp_account_file != "") {
             $row = 1;
             $if = new InputFileModel();
             //                $arrAcc = array();
             if (($handle = fopen($if->upload_location . $psn->camp_account_file, "r")) !== FALSE) {
                 $csv = fread($handle, filesize($if->upload_location . $psn->camp_account_file));
                 $arrsem = explode(",", $csv);
                 foreach ($arrsem as $acs) {
                     $arrAcc[] = trim(rtrim($acs));
                 }
                 //                    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                 //
                 //                        $arrAcc[] = trim(rtrim($data[0]));
                 //
                 //                    }
                 fclose($handle);
             }
             //                pr($arrAcc);
             //from acc get device ID
             //                self::sendUsingAccountArray($arrAcc, $psn);
             //                die();
         }
         self::sendUsingAccountArray($arrAcc, $psn);
     } else {
         //bukan acc, brati filter aktif
         //cek contradictives constraints
         $arrwhere = array();
         //gender check
         $query_gender = "";
         if ($psn->camp_filter_gender != '0') {
             if ($psn->camp_filter_gender == '1') {
                 $query_gender = "macc_gender = 'Female'";
             } else {
                 $query_gender = "macc_gender = 'Male'";
             }
             $arrwhere[] = $query_gender;
         }
         //tier check
         $query_tier = "";
         if ($psn->camp_filter_tier != '0') {
             if ($psn->camp_filter_tier == '1') {
                 //stampcard
                 $query_tier = "(macc_member_type = 'STC')";
             } elseif ($psn->camp_filter_tier == '2') {
                 //club
                 $query_tier = "macc_member_tier = 'LYB Member'";
             } elseif ($psn->camp_filter_tier == '3') {
                 //fan
                 $query_tier = "macc_member_tier = 'Fan'";
             } else {
                 //all lyb
                 //                    $query_tier = "(macc_member_tier = 'LYB Member' OR macc_member_tier = 'Fan')";
                 $query_tier = "(macc_member_tier = 'LYB Member' OR macc_member_tier = 'Fan')";
             }
             $arrwhere[] = $query_tier;
         }
         //city check
         $query_city = "";
         if ($psn->camp_filter_city != 'all') {
             $query_city = "macc_address_city = '{$psn->camp_filter_city}' ";
             $arrwhere[] = $query_city;
         }
         //birthday cek
         //camp_filter_bday
         $query_bday = "";
         if ($psn->camp_filter_bday != '0') {
             if ($psn->camp_filter_bday == '1') {
                 //has birthday today
                 $query_bday = "macc_dob = CURDATE()";
             } else {
                 //today not his/her bday
                 $query_bday = "macc_dob != CURDATE()";
             }
             $arrwhere[] = $query_bday;
         }
         //camp age cek
         //using inputfilter..
         $query_age = "";
         if ($psn->camp_filter_age != "") {
             $exp = explode("__", $psn->camp_filter_age);
             //                $cols = "macc_id,TIMESTAMPDIFF(YEAR,macc_dob,CURDATE()) AS age";
             if ($exp[0] == "=") {
                 $query_age = " TIMESTAMPDIFF(YEAR,macc_dob,CURDATE()) = " . $exp[1];
             }
             if ($exp[0] == "!=") {
                 $query_age = " TIMESTAMPDIFF(YEAR,macc_dob,CURDATE()) != " . $exp[1];
             }
             if ($exp[0] == "<") {
                 $query_age = " TIMESTAMPDIFF(YEAR,macc_dob,CURDATE()) < " . $exp[1];
             }
             if ($exp[0] == ">") {
                 $query_age = " TIMESTAMPDIFF(YEAR,macc_dob,CURDATE()) > " . $exp[1];
             }
             if ($exp[0] == "<=") {
                 $query_age = " TIMESTAMPDIFF(YEAR,macc_dob,CURDATE()) <= " . $exp[1];
             }
             if ($exp[0] == ">=") {
                 $query_age = " TIMESTAMPDIFF(YEAR,macc_dob,CURDATE()) >= " . $exp[1];
             }
             $arrwhere[] = $query_age;
         }
         //now calculate the query
         $acc = new LL_Account();
         $arrAcc = array();
         //            pr($arrwhere);
         //            echo $cols;
         if (count($arrwhere) > 0) {
             $where = implode(" AND ", $arrwhere);
             echo $where;
             $arrAcc = $acc->getWhere($where, $cols);
         } else {
             $arrAcc = $acc->getAll();
         }
         $arrSend = array();
         //masukan ke 1 array
         foreach ($arrAcc as $cc) {
             $arrSend[] = $cc->macc_id;
         }
         //from acc get device ID
         self::sendUsingAccountArray($arrSend, $psn);
     }
 }
    function demographic()
    {
        $bulan = addslashes($_GET['m']);
        if ($bulan == "") {
            $bulan = date("F Y");
            $days_ago = date('Y-m-01');
            // hard-coded '01' for first day
            $days_now = date('Y-m-t');
        } else {
            $bulan = urldecode($bulan);
            $days_ago = date('Y-m-01', strtotime($bulan));
            // hard-coded '01' for first day
            $days_now = date('Y-m-t', strtotime($bulan));
        }
        $numberDays = cal_days_in_month(CAL_GREGORIAN, date('n', strtotime($bulan)), date('Y', strtotime($bulan)));
        $days_ago = date('Y-m-01', strtotime($bulan));
        // hard-coded '01' for first day
        $days_now = date('Y-m-01', strtotime('+1 month', strtotime($bulan)));
        $t = time();
        //male female
        $ll = new LL_Account();
        global $db;
        $query3 = "Select (Select count(*) from {$ll->table_name} where macc_gender='Male' AND (macc_acquire_date BETWEEN '{$days_ago}' AND '{$days_now}')) AS Male,\n(Select count(*) from {$ll->table_name} where macc_gender='Female' AND (macc_acquire_date BETWEEN '{$days_ago}' AND '{$days_now}')) AS Female ";
        $gender = $db->query($query3, 1);
        //        pr($gender);
        //        macc_lyb_status
        $query4 = "Select (Select count(*) from {$ll->table_name} where macc_lyb_status='LYB Club' AND (macc_acquire_date BETWEEN '{$days_ago}' AND '{$days_now}')) AS Club,\n(Select count(*) from {$ll->table_name} where macc_lyb_status='The Body Shop Friend' AND (macc_acquire_date BETWEEN '{$days_ago}' AND '{$days_now}')) AS Stampcard,\n(Select count(*) from {$ll->table_name} where macc_lyb_status='LYB Fan' AND (macc_acquire_date BETWEEN '{$days_ago}' AND '{$days_now}')) AS Fan ";
        $lyb = $db->query($query4, 1);
        //        pr($lyb);
        //macc_address_city
        //        $query5="SELECT DISTINCT macc_address_city FROM {$ll->table_name} ";
        //
        //        $cities = $db->query($query5,2);
        ////        pr($cities);
        //
        //        foreach($cities as $number=>$city){
        //            if($city->macc_address_city!="") {
        //                $city->macc_address_city = str_replace(".","",$city->macc_address_city);
        //                $city->macc_address_city = str_replace(",","",$city->macc_address_city);
        //                $text[] = "(Select count(*) as count from {$ll->table_name} where macc_address_city='{$city->macc_address_city}' AND (macc_acquire_date BETWEEN '$days_ago' AND '$days_now')) AS " . str_replace("/", "", str_replace(" ", "", str_replace("-", "", $city->macc_address_city)));
        //            }
        ////            if($number>10)break;
        //        }
        ////        pr($text);
        //        $imp = "Select ". implode(",",$text)." ORDER BY count DESC";
        global $db;
        $q = "SELECT macc_address_city,COUNT(*) as count FROM {$ll->table_name} GROUP BY macc_address_city ORDER BY count DESC LIMIT 0,20";
        $arrSl = $db->query($q, 2);
        //        pr($arrSl);
        //        echo $imp;
        //        $nrcities = $db->query($imp,2);
        //        pr($nrcities);
        ?>
<div class="row">
    <div class="col-md-12">
        <h1>
            Demographic Dashboard
            <small><?php 
        echo $bulan;
        ?>
</small>
        </h1>
        <ol class="breadcrumb">
            <li>
                <?php 
        echo Lang::t('Select Timeframe');
        ?>
            </li>
            <li class="active">
                <?php 
        $start = new DateTime('11 months ago');
        // So you don't skip February if today is day the 29th, 30th, or 31st
        $start->modify('first day of this month');
        $end = new DateTime();
        $interval = new DateInterval('P1M');
        $period = new DatePeriod($start, $interval, $end);
        ?>
                <select id="apptimeselector_<?php 
        echo $t;
        ?>
">
                    <?php 
        foreach ($period as $dt) {
            ?>
                        <option value="<?php 
            echo urlencode($dt->format('F Y'));
            ?>
" <?php 
            if ($dt->format('F Y') == $bulan) {
                echo "selected";
            }
            ?>
>
                            <?php 
            echo $dt->format('F Y') . "<br>";
            ?>
                        </option>
                    <?php 
        }
        ?>

                </select>
                <script>
                    $("#apptimeselector_<?php 
        echo $t;
        ?>
").change(function(){
                        var slc = $("#apptimeselector_<?php 
        echo $t;
        ?>
").val();
                        openLw("Demographic","<?php 
        echo _SPPATH;
        ?>
BIWebDemographic/demographic?m="+slc,"fade");
                    });
                </script>
            </li>
        </ol>
    </div>
</div>

        <?php 
        $arrUserswPic = $ll->getWhere("macc_foto != '' AND (macc_acquire_date BETWEEN '{$days_ago}' AND '{$days_now}') ORDER BY macc_acquire_date DESC LIMIT 0,8");
        $arrUsers = $ll->getOrderBy(" macc_acquire_date DESC LIMIT 0,8");
        ?>
        <div class="row">
            <!--<div class="col-md-6">

                <div class="box box-danger">
                    <div class="box-header with-border">
                        <h3 class="box-title">Latest Members</h3>

                        <div class="box-tools pull-right">
                            <span class="label label-danger">8 New Members</span>
                            <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
                            </button>
                            <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i>
                            </button>
                        </div>
                    </div>

                    <div class="box-body no-padding">
                        <ul class="users-list clearfix">
                            <?php 
        foreach ($arrUsers as $user) {
            if ($user->macc_foto == "") {
                $url = _SPPATH . "images/noimage.jpg";
            } else {
                $url = "http://43.231.128.129/" . $user->macc_foto;
            }
            ?>
                            <li>
                                <img src="<?php 
            echo $url;
            ?>
" alt="<?php 
            echo $user->macc_first_name . " " . $user->macc_last_name;
            ?>
">
                                <a class="users-list-name" href="#"><?php 
            echo $user->macc_first_name . " " . $user->macc_last_name;
            ?>
</a>
                                <span class="users-list-date"><?php 
            echo ago(strtotime($user->macc_acquire_date));
            ?>
</span>
                            </li>
                            <?php 
        }
        ?>

                        </ul>

                    </div>

                    <div class="box-footer text-center">
                        <a href="javascript::" class="uppercase">View All Users</a>
                    </div>

                </div>

            </div>
            -->
            <div class="col-md-6">
                <!-- USERS LIST -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">Latest Customers</h3>

                        <!--<div class="box-tools pull-right">
                            <span class="label label-danger">8 New Members</span>
                            <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
                            </button>
                            <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i>
                            </button>
                        </div>-->
                    </div>
                    <!-- /.box-header -->
                    <div class="box-body no-padding">
                        <ul class="users-list clearfix">
                            <?php 
        foreach ($arrUserswPic as $user) {
            ?>
                                <li style="height: 160px;">
                                    <img src="<?php 
            echo _LOKASI;
            echo $user->macc_foto;
            ?>
" alt="User Image">
                                    <span class="users-list-name" ><?php 
            echo $user->macc_first_name . " " . $user->macc_last_name;
            ?>
</span>
                                    <span class="users-list-date"><?php 
            echo ago(strtotime($user->macc_acquire_date));
            ?>
</span>
                                </li>
                            <?php 
        }
        ?>

                        </ul>
                        <!-- /.users-list -->
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer text-center">
                        <a href="javascript:openLw('LL_Account', '<?php 
        echo _SPPATH;
        ?>
LLAccWeb/LL_Account', 'fade'); activkanMenuKiri('LL_Account');" class="uppercase">View All Customers</a>
                    </div>
                    <!-- /.box-footer -->
                </div>
                <!--/.box -->
            </div>

            <?php 
        $arrColor = array("#00a65a", "#00c0ef");
        $c = new Charting();
        $c->color = array_pop($arrColor);
        $c->label = "Male";
        $c->value = $gender->Male;
        $arrData[] = $c;
        $c = new Charting();
        $c->color = array_pop($arrColor);
        $c->label = "Female";
        $c->value = $gender->Female;
        $arrData[] = $c;
        ?>
<div class="col-md-3"><?php 
        Charting::morrisDonut("287px", $arrData, 1, "Gender", "default");
        ?>

            </div>
            <?php 
        $arrColor = array("#4da65a", "#7bcd03", "#53ddde");
        $arrData = array();
        $c = new Charting();
        //            $c->color = array_pop($arrColor);
        $c->color = "#e90057";
        $c->label = "Fan";
        $c->value = $lyb->Fan;
        $arrData[] = $c;
        $c = new Charting();
        //            $c->color = array_pop($arrColor);
        $c->color = "#cd6949";
        $c->label = "Club";
        $c->value = $lyb->Club;
        $arrData[] = $c;
        $c = new Charting();
        //            $c->color = array_pop($arrColor);
        $c->color = "#f4bb1b";
        $c->label = "Stampcard";
        $c->value = $lyb->Stampcard;
        $arrData[] = $c;
        ?>
<div class="col-md-3"><?php 
        Charting::morrisDonut("287px", $arrData, 1, "Member Type", "default");
        ?>

            </div>

            <?php 
        $arrData = array();
        $arrlegend = array();
        foreach ($arrSl as $attr => $value) {
            if ($value->macc_address_city == "") {
                $value->macc_address_city = "Unknown";
            }
            $c = new Charting();
            $c->color = "#" . $this->random_color();
            $c->label = str_replace(" ", "_", $value->macc_address_city);
            $c->value = $value->count;
            $arrData[] = $c;
            //                $arrlegend[$attr] = $value;
        }
        //            asort($arrlegend);
        //pr($arrlegend);
        //            $arrlegend = array_reverse($arrlegend,1);
        //            $arrData = array();
        //            foreach($arrSl as $attr=>$value){
        //                $c = new Charting();
        //                $c->color = "#".$this->random_color();
        //
        //                $c->label = str_replace(" ","_",$value->macc_address_city);
        //                $c->value = $value->count;
        //
        ////                $c->label = ucwords(strtolower(str_replace("_"," ",$attr)));
        ////                $c->value = $value;
        //                $arrData[] = $c;
        //                $arrColors[$attr] = $c->color;
        ////                $arrlegend[$attr] = $value;
        //            }
        ?>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="box box-default">
                    <div class="box-header with-border">
                        <h3 class="box-title">Locations</h3>

                        <div class="box-tools pull-right">
                            <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
                            </button>
                            <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
                        </div>
                    </div>
                    <div class="box-body chart-responsive">

                <div class="col-md-6">
                <?php 
        Charting::morrisDonut("300px", $arrData, 0, "Locations", "default", 0);
        ?>
                </div>
                <div class="col-md-6">
                    <?php 
        ?>
                    <ul class="nav nav-pills nav-stacked">
                        <?php 
        foreach ($arrSl as $attr => $value) {
            if ($value->macc_address_city == "") {
                $value->macc_address_city = "Unknown";
            }
            ?>
                        <li style="line-height: 30px;">
                            <div style="margin-top:5px; float: left; width: 20px; height: 20px; background-color: <?php 
            echo $arrColors[$attr];
            ?>
;"></div>
                            &nbsp; <?php 
            echo ucwords(str_replace("_", " ", strtolower($value->macc_address_city)));
            ?>
                                <span class="pull-right "> <?php 
            echo $value->count;
            ?>
</span></li>

                        <?php 
        }
        ?>
                    </ul>
                </div>

                    </div>
                    <!-- /.box-body -->
                </div>
            </div>
        </div>


        <style>
            ul.legend li{
                list-style: none;
                line-height: 30px;
            }
            ul.legend li div{
                float: left;
                margin-top: 10px;
                margin-right: 15px;
            }
            .legend-item{
                float: left;
                margin: 10px;
                line-height: 30px;
                margin-right: 5px;
            }
            .legend-item div{
                float: left;
                margin-top: 10px;
                margin-right: 5px;
            }
            .breadcrumb {
                padding: 8px 15px;
                margin-bottom: 20px;
                list-style: none;
                background-color: #f5f5f5;
                border-radius: 4px;
            }
            .breadcrumb {
                float: right;
                background: transparent;
                margin-top: 0;
                margin-bottom: 0;
                font-size: 12px;
                padding: 7px 5px;
                position: absolute;
                top: 15px;
                right: 10px;
                border-radius: 2px;
            }
            .users-list-name{
                white-space: nowrap;
            }
        </style>
<?php 
    }
 public function getByFB()
 {
     if (Efiwebsetting::getData('checkOAuth') == 'yes') {
         IMBAuth::checkOAuth();
     }
     $fb_id = addslashes($_POST['macc_fb_id']);
     $json = array();
     //$fb_id = addslashes($_POST['macc_fb_id']);
     if ($fb_id == "") {
         $json['status_code'] = 0;
         $json['status_message'] = "Invalid FB ID";
         echo json_encode($json);
         die;
     }
     //update di lokal saja
     $ll = new LL_Account();
     $arrLL = $ll->getWhere("macc_fb_id = '{$fb_id}' LIMIT 0,1");
     //kalau sudah terdaftar
     if (count($arrLL) > 0) {
         $ll = $arrLL[0];
         //ambil dari ll dengan macc_id ini untuk update status
         $VRO = VRCustModel::findByID($ll->macc_id);
         $json = VRCustMapper::kerjakan($VRO);
         echo json_encode($json);
         die;
     } else {
         $json['status_code'] = 0;
         $json['status_message'] = "User with this FB ID not Found";
     }
     echo json_encode($json);
     die;
 }