<?php namespace myresto\models;use Yii;use yii\base\Model;use myresto\models\SyncConfig;class ImportCustomer extends Model{public static $tipe_tabel="bp_customer";public function kirimData($data,$access_token){$base=SyncConfig::$base;$curl=curl_init();curl_setopt_array($curl,array(CURLOPT_URL=>"https://$base.jurnal.id/core/api/v1/customers",CURLOPT_RETURNTRANSFER=>true,CURLOPT_CUSTOMREQUEST=>"POST",CURLOPT_POSTFIELDS=>json_encode($data),CURLOPT_HTTPHEADER=>array("apikey: $access_token","Content-Type: application/json")));$response=curl_exec($curl);curl_close($curl);return $response;}public function simpanStatus($data,$tipe_tabel,$idHeaderLog,$jenis_sinkron,$result){$conn=SyncConfig::getConnection('MyConnection');$type_con=SyncConfig::$type_con;$tgl=date("Y-m-d H:i:s");$berhasil=0;$data=json_decode($data,TRUE);$lastheader=$result['customer']['lastheader'];$id_jurnal=0;$id=$result['customer']['id'];if(isset($data['customer'])){$id_jurnal=$data['customer']['id'];$status_sinkron="success";$ket="";}else{if(preg_match("/(Deleted)/i",$result['customer']['display_name'])){$status_sinkron="success";$ket="";}elseif($data['id']!=NULL){$id_jurnal=$data['id'];$status_sinkron="success";$ket="";}else{$status_sinkron="failed";$ket=json_encode($data['error_full_messages']);$ket=addslashes($ket);}}$stmt=mysqli_query($conn,"SELECT * FROM sync_data WHERE tipe_tabel = '$tipe_tabel' AND fpkey = '$id' AND type_con = '$type_con'");if(mysqli_num_rows($stmt)>0){mysqli_query($conn,"UPDATE sync_data SET id_jurnal = '$id_jurnal', is_sync = '1', last_update = '$tgl' WHERE fpkey = '$id' AND tipe_tabel = '$tipe_tabel' AND type_con = '$type_con'");}else{mysqli_query($conn,"INSERT INTO sync_data (fpkey, id_jurnal, tipe_tabel, is_sync, last_update, type_con) VALUES ('$id', '$id_jurnal', '$tipe_tabel', '1', '$tgl', '$type_con')");}if($jenis_sinkron=='manual'&&$status_sinkron!='success'){mysqli_query($conn,"INSERT INTO sync_to_jurnal (id_header, last_update, tipe_tabel, id_record, status_sinkron, keterangan, id_jurnal, tipe_proses, type_con) VALUES ('$idHeaderLog', '$tgl', '$tipe_tabel', '$id', '$status_sinkron', '$ket', '$id_jurnal', 'new', '$type_con')");}$berhasil++;return $berhasil;}public function ambilData($status,$tipe_tabel,$id_header){$conn=SyncConfig::getConnection('MyConnection');$type_con=SyncConfig::$type_con;$connMyResto=SyncConfig::getConnection('MyResto');$dbNameMyConnection=SyncConfig::getDbNameMyConnection();$last_key=0;$stmt=mysqli_query($conn,"SELECT fpkey FROM sync_data WHERE tipe_tabel = '$tipe_tabel' ORDER BY fpkey DESC LIMIT 1");$row=mysqli_fetch_assoc($stmt);if($row>0){$last_key=$row['fpkey'];}$result=array();if($status=="import"){$stmt=mysqli_query($connMyResto,"SELECT * FROM bp_customer WHERE fpkey > $last_key LIMIT 1");}else{$stmt=mysqli_query($connMyResto,"SELECT * FROM bp_customer a
                JOIN $dbNameMyConnection.sync_data b ON a.fpkey = b.fpkey
                WHERE tipe_tabel = '$tipe_tabel' AND is_sync = '0' AND type_con = '$type_con' LIMIT 1");}$lastheader=0;if($row=mysqli_fetch_assoc($stmt)){if($row['fdeleted']==1){$del="(Deleted)";}else{$del="";}$result['customer']['id']=$row['fpkey'];$result['customer']['lastheader']=$lastheader;$result['customer']['display_name']=$row['fname'].$del;$result['customer']['address']=$row['faddress'];$result['customer']['phone']=$row['fphone'];$result['customer']['mobile']=$row['fcphone'];$result['customer']['email']=$row['femail'];$result['customer']['fax']=$row['ffax'];}return $result;}}