<?php namespace myretail\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'=>'retail'],];}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){$conn=SyncConfig::getConnection('MyConnection');$type_con='retail';$query="
            SELECT
                a.tipe_tabel,
                a.id_record,
                a.keterangan
            FROM sync_to_jurnal a
            WHERE a.id_header = $idHeaderLog AND a.type_con = '$type_con' AND a.status_sinkron != 'success'";$result=mysqli_query($conn,$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');$type_con='retail';$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,'customer_success'=>0,'customer_total'=>0,'supplier_success'=>0,'supplier_total'=>0,'warehouse_success'=>0,'warehouse_total'=>0,'sales_success'=>0,'sales_total'=>0,'payment_success'=>0,'payment_total'=>0,'salesreturn_success'=>0,'salesreturn_total'=>0,];if($count>0){foreach($result as $value){switch($value['tipe_tabel']){case 'inv_item':$data['product_total']+=$value['total'];if($value['status_sinkron']=='success'){$data['product_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 'inv_warehouse':$data['warehouse_total']+=$value['total'];if($value['status_sinkron']=='success'){$data['warehouse_success']+=$value['total'];}break;case 'rtl_fc_trans_header':$data['sales_total']+=$value['total'];if($value['status_sinkron']=='success'){$data['sales_success']+=$value['total'];}break;case 'rtl_fc_payment':$data['payment_total']+=$value['total'];if($value['status_sinkron']=='success'){$data['payment_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='retail';$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;}}