<?php namespace myretail\models;use Yii;use yii\base\Model;use myretail\models\SyncConfig;class UpdateProduct extends Model{public $tipe_tabel="inv_item";public function kirimData($data,$access_token,$id){$base=SyncConfig::$base;$curl=curl_init();curl_setopt_array($curl,array(CURLOPT_URL=>"https://$base.jurnal.id/core/api/v1/products/$id",CURLOPT_RETURNTRANSFER=>true,CURLOPT_CUSTOMREQUEST=>"PATCH",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){$type_con=SyncConfig::$type_con;$conn=SyncConfig::getConnection();$tgl=date("Y-m-d H:i:s");$berhasil=0;$data=json_decode($data,TRUE);$id_jurnal=0;if(isset($data['product']['id'])){$id_jurnal=$data['product']['id'];$status_sinkron="success";$ket="";$berhasil++;}else{$status_sinkron="failed";$ket=json_encode($data['error_full_messages']);$ket=addslashes($ket);}$stmt=mysqli_query($conn,"SELECT tipe_tabel, id_record FROM sync_to_jurnal WHERE tipe_tabel = '$tipe_tabel' AND id_record = '$id_lokal' AND tipe_proses = 'update' AND type_con = '$type_con'");if(mysqli_fetch_assoc($stmt)<1){$query="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_lokal', '$status_sinkron', '$ket', '$id_jurnal', 'update', '$type_con')";mysqli_query($conn,$query);}else{mysqli_query($conn,"UPDATE sync_to_jurnal SET status_sinkron = '$status_sinkron', keterangan = '$ket', id_jurnal = '$id_jurnal' WHERE id_record = '$id_lokal' AND type_con = '$type_con' AND tipe_tabel = '$tipe_tabel' AND tipe_proses = 'update'");}return $berhasil;}public function ambilData($tipe_tabel){$type_con=SyncConfig::$type_con;$conn=SyncConfig::getConnection('MyConnection');$connMyRetail=SyncConfig::getConnection('MyRetail');$dbNameMyConnection=SyncConfig::getDbNameMyConnection();$stmt=mysqli_query($conn,"SELECT last_update FROM sync_to_jurnal WHERE type_con = '$type_con' ORDER BY last_update DESC LIMIT 1");$row=mysqli_fetch_assoc($stmt);$last_update=$row['last_update'];$stmt=mysqli_query($conn,"SELECT inventory_account_number FROM sync_config WHERE type_con = '$type_con'");$row=mysqli_fetch_assoc($stmt);$inv=$row['inventory_account_number'];$result=array();$stmt=mysqli_query($connMyRetail,"SELECT a.fpkey, a.fcode, fname, fpur_price, fsalesprice, funit, fdesc, b.fcategory category, c.fcategory sub_category, a.fnonstock, id_jurnal
            FROM inv_item a
            LEFT JOIN inv_item_category b ON a.fcategorykey = b.fpkey
            LEFT JOIN inv_item_sub_category c ON a.fcategorykey2 = c.fpkey
            LEFT JOIN inv_item_unit e ON a.fstock_unitkey = e.fpkey
            JOIN $dbNameMyConnection.sync_to_jurnal d ON a.fpkey = d.id_record
            WHERE tipe_tabel = '$tipe_tabel' AND fmodifieddt > '$last_update' ORDER BY fmodifieddt ASC LIMIT 1");$row=mysqli_fetch_assoc($stmt);if($row>0){if($row['fnonstock']==0){$stok="true";}else{$stok="false";}$result['product']['id']=$row['fpkey'];$result['product']['id_jurnal']=$row['id_jurnal'];$result['product']['product_code']=$row['fcode'];$result['product']['name']=$row['fname'];$result['product']['unit_name']=$row['funit'];$result['product']['buy_price_per_unit']=$row['fpur_price'];$result['product']['sell_price_per_unit']=$row['fsalesprice'];$result['product']['taxable_sell']=true;$result['product']['description']=$row['fdesc'];$result['product']['is_sold']=true;$result['product']['is_bought']=true;$result['product']['track_inventory']=$stok;$result['product']['inventory_asset_account_number']=$inv;$result['product']['product_category_names'][0]=$row['category'];$result['product']['product_category_names'][1]=$row['sub_category'];}return $result;}}