<?php namespace myresto\models;use Yii;use yii\base\Model;use myresto\models\SyncConfig;class ImportProduct extends Model{public static $tipe_tabel="rst_menu";public function kirimData($data,$access_token,$method){$base=SyncConfig::$base;if($method=="PATCH"){$id="/".$data['product']['custom_id'];}else{$id="";}$curl=curl_init();curl_setopt_array($curl,array(CURLOPT_URL=>"https://$base.jurnal.id/core/api/v1/products".$id,CURLOPT_RETURNTRANSFER=>true,CURLOPT_CUSTOMREQUEST=>$method,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,$access_token){$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['product']['lastheader'];$id_jurnal=0;if(isset($data['product'])){$id=$data['product']['custom_id'];$id_jurnal=$data['product']['id'];$status_sinkron="success";$ket="";}elseif(isset($data['custom_id'])||(isset($data['name'])&&isset($data['product_code']))){ImportProduct::kirimData($result,$access_token,"PATCH");$id=$result['product']['custom_id'];$id_jurnal=$data['id'];$status_sinkron="success";$ket="";}else{$id=$result['product']['custom_id'];if(preg_match("/(Deleted)/i",$result['product']['name'])){$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){$type_con=SyncConfig::$type_con;$conn=SyncConfig::getConnection('MyConnection');$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 a.fpkey, a.fcode, fname, a.fdeleted, fprice, fdesc, b.fcategory category, c.fcategory sub_category , fcreateddt
            FROM rst_menu a
            LEFT JOIN rst_menu_category b ON a.fcategory = b.fpkey
            LEFT JOIN rst_menu_sub_category c ON a.fcategory2 = c.fpkey
            WHERE a.fpkey > $last_key AND fcombo_attribute != 1 LIMIT 1");}else{$stmt=mysqli_query($connMyResto,"SELECT a.fpkey, a.fcode, fname, a.fdeleted, fprice, fdesc, b.fcategory category, c.fcategory sub_category, fcreateddt
            FROM rst_menu a
            LEFT JOIN rst_menu_category b ON a.fcategory = b.fpkey
            LEFT JOIN rst_menu_sub_category c ON a.fcategory2 = c.fpkey
            JOIN $dbNameMyConnection.sync_data d ON a.fpkey = d.fpkey
            WHERE d.tipe_tabel = '$tipe_tabel' AND is_sync = '0' AND type_con = '$type_con' LIMIT 1");}$lastheader=0;if($row=mysqli_fetch_assoc($stmt)){$del="";if($row['fdeleted']==1){$del="(Deleted)";}$result['product']['custom_id']=$row['fpkey'];$result['product']['lastheader']=$lastheader;$result['product']['product_code']=$row['fcode'];$result['product']['name']=$row['fname'].$del;$result['product']['sell_price_per_unit']=$row['fprice'];$result['product']['taxable_sell']=true;$result['product']['description']=$row['fdesc'];$result['product']['is_sold']=true;$result['product']['track_inventory']="false";$result['product']['product_category_names'][0]=$row['category'];$result['product']['product_category_names'][1]=$row['sub_category'];}return $result;}}