<?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/batch_create",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,$id_lokal,$idHeaderLog,$lastheader){$conn=SyncConfig::getConnection('MyConnection');$type_con=SyncConfig::$type_con;$tgl=date("Y-m-d H:i:s");$berhasil=0;$data=json_decode($data,TRUE);for($i=0;$i<count($data['customers']);$i++){$id=$id_lokal[$i];$status=$data['customers'][$i]['customer']['status'];$id_jurnal=0;if($status==201){$id_jurnal=$data['customers'][$i]['customer']['content']['id'];$status_sinkron="success";$ket="";$berhasil++;}else{$status_sinkron="failed";$ket=json_encode($data['customers'][$i]['customer']['content']);$ket=addslashes($ket);}mysqli_query($conn,"UPDATE sync_to_jurnal SET status_sinkron = 'failed', id_jurnal = '$id_jurnal' WHERE id_record = '$id' AND tipe_tabel = '$tipe_tabel' AND id_header = '$lastheader' AND type_con = '$type_con'");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')");}return $berhasil;}public function ambilData($status,$tipe_tabel){$conn=SyncConfig::getConnection('MyConnection');$type_con=SyncConfig::$type_con;$connMyResto=SyncConfig::getConnection('MyResto');$dbNameMyConnection=SyncConfig::getDbNameMyConnection();$last_key=0;$stmt=mysqli_query($conn,"SELECT id_record FROM sync_to_jurnal WHERE tipe_tabel = '$tipe_tabel' AND type_con = '$type_con' ORDER BY id_record DESC LIMIT 1");$row=mysqli_fetch_assoc($stmt);if($row>0){$last_key=$row['id_record'];}$result=array();if($status=="import"){$stmt=mysqli_query($connMyResto,"SELECT * FROM bp_customer WHERE fpkey > $last_key LIMIT 10");}else{$stmt=mysqli_query($connMyResto,"SELECT * FROM bp_customer a
                JOIN $dbNameMyConnection.sync_to_jurnal b ON a.fpkey = b.id_record
                WHERE status_sinkron = 'proses' AND tipe_proses = 'new' AND tipe_tabel = '$tipe_tabel' LIMIT 10");}$i=0;while($row=mysqli_fetch_assoc($stmt)){$result['customers'][$i]['customer']['id']=$row['fpkey'];$result['customers'][$i]['customer']['display_name']=$row['fname'];$result['customers'][$i]['customer']['address']=$row['faddress'];$result['customers'][$i]['customer']['phone']=$row['fphone'];$result['customers'][$i]['customer']['mobile']=$row['fcphone'];$result['customers'][$i]['customer']['email']=$row['femail'];$result['customers'][$i]['customer']['fax']=$row['ffax'];$i++;}return $result;}}