<?php namespace myresto\models;use Yii;class SyncToJurnalHeader extends \yii\db\ActiveRecord{public static function tableName(){return 'sync_to_jurnal_header';}public function rules(){return [ [['jenis_sinkron','jenis_data','type_con'],'string'],[['date_created'],'safe'],[['type_con'],'default','value'=>'resto'],];}public function attributeLabels(){return [ 'id_header'=>'Id Header','jenis_sinkron'=>'Jenis Sinkron','jenis_data'=>'Jenis Data','date_created'=>'Date Created',];}public static function getStatusSync($idHeaderLog){$connMyResto=SyncConfig::getConnection('MyResto');$dbNameMyConnection=SyncConfig::getDbNameMyConnection();$type_con='resto';$query="
            SELECT
                a.tipe_tabel,
                a.id_record,
                CASE
                    WHEN a.tipe_tabel = 'rst_menu' THEN product.fname
                    WHEN a.tipe_tabel = 'bp_customer' THEN customer.fname
                    WHEN a.tipe_tabel = 'bp_supplier' THEN supplier.fname
                    WHEN a.tipe_tabel = 'rst_menu_notes2' THEN modifier.fcode
                    ELSE product.fname
                END AS fname,
                a.keterangan
            FROM $dbNameMyConnection.sync_to_jurnal a
            LEFT JOIN rst_menu product ON a.id_record = product.fpkey
            LEFT JOIN bp_customer customer ON a.id_record = customer.fpkey
            LEFT JOIN bp_supplier supplier ON a.id_record = supplier.fpkey
            LEFT JOIN rst_menu_notes2 modifier ON a.id_record = modifier.fpkey
            WHERE a.id_header = $idHeaderLog AND a.type_con = '$type_con' AND a.status_sinkron != 'success'";$result=mysqli_query($connMyResto,$query);$result=mysqli_fetch_all($result,MYSQLI_ASSOC);$count=count($result);if($count>0)$status='Fail';else $status='Success';return $status;}public static function getDataSync($idHeaderLog){$conn=SyncConfig::getConnection('MyConnection');$dbNameMyConnection=SyncConfig::getDbNameMyConnection();$type_con='resto';$query="
            SELECT
                COUNT(id) as total,
                tipe_tabel,
                status_sinkron
            FROM sync_to_jurnal
            WHERE id_header = $idHeaderLog AND type_con = '$type_con'
            GROUP BY tipe_tabel, status_sinkron";$result=mysqli_query($conn,$query);$result=mysqli_fetch_all($result,MYSQLI_ASSOC);$count=count($result);$data=[ 'product_success'=>0,'product_total'=>0,'modifier_success'=>0,'modifier_total'=>0,'customer_success'=>0,'customer_total'=>0,'supplier_success'=>0,'supplier_total'=>0,'sales_success'=>0,'sales_total'=>0,'payment_success'=>0,'payment_total'=>0,'deposit_success'=>0,'deposit_total'=>0,'salesreturn_success'=>0,'salesreturn_total'=>0,];if($count>0){foreach($result as $value){switch($value['tipe_tabel']){case 'rst_menu':$data['product_total']+=$value['total'];if($value['status_sinkron']=='success'){$data['product_success']+=$value['total'];}break;case 'rst_menu_notes2':$data['modifier_total']+=$value['total'];if($value['status_sinkron']=='success'){$data['modifier_success']+=$value['total'];}break;case 'bp_customer':$data['customer_total']+=$value['total'];if($value['status_sinkron']=='success'){$data['customer_success']+=$value['total'];}break;case 'bp_supplier':$data['supplier_total']+=$value['total'];if($value['status_sinkron']=='success'){$data['supplier_success']+=$value['total'];}break;case 'rst_fc_trans_header':$data['sales_total']+=$value['total'];if($value['status_sinkron']=='success'){$data['sales_success']+=$value['total'];}break;case 'rst_fc_payment':$data['payment_total']+=$value['total'];if($value['status_sinkron']=='success'){$data['payment_success']+=$value['total'];}break;case 'bp_customer_deposit':$data['deposit_total']+=$value['total'];if($value['status_sinkron']=='success'){$data['deposit_success']+=$value['total'];}break;case 'sales_return':$data['salesreturn_total']+=$value['total'];if($value['status_sinkron']=='success'){$data['salesreturn_success']+=$value['total'];}break;default:break;}}}return $data;}public static function getErrorData($dateStart='',$dateEnd=''){date_default_timezone_set("Asia/Jakarta");if(empty($dateStart))$dateStart=date('Y-m-d',strtotime('-6 days'));if(empty($dateEnd))$dateEnd=date('Y-m-d');$conn=SyncConfig::getConnection('MyConnection');$type_con='resto';$query="
            SELECT
                DATE(h.date_created) AS `date`,
                h.jenis_data AS `type`,
                COUNT(d.id) AS `count`
            FROM sync_to_jurnal_header h
            LEFT JOIN sync_to_jurnal d ON h.id_header = d.id_header
            WHERE h.type_con = '$type_con' AND d.status_sinkron != 'success' AND DATE(h.date_created) >= '$dateStart' AND DATE(h.date_created) <= '$dateEnd'
            GROUP BY DATE(h.date_created), h.jenis_data";$result=mysqli_query($conn,$query);$result=mysqli_fetch_all($result,MYSQLI_ASSOC);$count=count($result);$total=[ 'master'=>0,'transaksi'=>0,'total'=>0 ];if($count>0){foreach($result as $key=>$value){$total['total']+=$value['count'];if($value['type']=='master')$total['master']+=$value['count'];else $total['transaksi']+=$value['count'];}}$query="
            SELECT
                DATE(h.date_created) AS `date`,
                COUNT(d.id) AS `count`
            FROM sync_to_jurnal_header h
            LEFT JOIN sync_to_jurnal d ON h.id_header = d.id_header
            WHERE h.type_con = '$type_con' AND d.status_sinkron != 'success' AND DATE(h.date_created) >= '$dateStart' AND DATE(h.date_created) <= '$dateEnd'
            GROUP BY DATE(h.date_created)";$resultChart=mysqli_query($conn,$query);$resultChart=mysqli_fetch_all($resultChart,MYSQLI_ASSOC);$temp=array_column($resultChart,'date');$dates=[];foreach($temp as $key=>$value){$dates[]=date('d M Y',strtotime($value));}$return=[ 'result'=>$result,'resultChart'=>[ 'labels'=>$dates,'data'=>array_column($resultChart,'count')],'total'=>$total ];return $return;}}