<?php namespace myretail\models;use Yii;use yii\base\Model;use myretail\models\SyncConfig;class SalesReturn extends Model{public $tipe_tabel="sales_return";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/sales_returns",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,$idHeaderLog,$lastheader,$jenis_sinkron,$date){$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['sales_return']['id'])){$id_jurnal=$data['sales_return']['id'];$status_sinkron="success";$ket="";$berhasil++;}elseif(isset($data['custom_id'])){$status_sinkron="success";$ket="";$stmt=mysqli_query($conn,"SELECT id_jurnal FROM sync_to_jurnal WHERE id_record = '$id' AND tipe_tabel = '$tipe_tabel' AND id_jurnal != 0 LIMIT 1");$row=mysqli_fetch_assoc($stmt);$id_jurnal=$row['id_jurnal'];$berhasil++;}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 = '$date' 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', '$date', '$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', '$date', '$tipe_tabel', '$id', '$status_sinkron', '$ket', '$id_jurnal', 'new', '$type_con')");return $berhasil;}public function ambilData($status,$tipe_tabel,$start,$end,$id_header){$type_con=SyncConfig::$type_con;$conn=SyncConfig::getConnection('MyConnection');$connMyRetail=SyncConfig::getConnection('MyRetail');$dbNameMyConnection=SyncConfig::getDbNameMyConnection();$last_update='0000-00-00 00:00:00';if($start!='0000-00-00')$filterStart=" AND a.fdate >= '$start'";else $filterStart="";if($end!='0000-00-00')$filterEnd=" AND a.fdate <= '$end'";else $filterEnd="";if($start=="0000-00-00"&&$end=="0000-00-00"){$stmt=mysqli_query($conn,"SELECT last_update FROM sync_data WHERE tipe_tabel = '$tipe_tabel' AND type_con = '$type_con' ORDER BY last_update DESC LIMIT 1");}else{$stmt=mysqli_query($conn,"SELECT last_update FROM sync_to_jurnal WHERE tipe_tabel = '$tipe_tabel' AND type_con = '$type_con' AND id_header = '$id_header' ORDER BY last_update DESC LIMIT 1");}$row=mysqli_fetch_assoc($stmt);if($row>1){$last_update=$row['last_update'];}$stmt=mysqli_query($conn,"SELECT branch_code FROM sync_config WHERE type_con = '$type_con'");$row=mysqli_fetch_assoc($stmt);$bcode=$row['branch_code'];$result=array();$x=array();if($status=="import"){$stmt=mysqli_query($connMyRetail,"SELECT DISTINCT a.fpkey, a.fcode, a.fvoiddt, a.fvoidreason
            FROM rtl_fc_trans_header a
            JOIN rtl_fc_trans_detail b ON a.fpkey = b.ftranskey
            WHERE a.fvoidby != 0 AND a.fvoiddt > '$last_update' $filterStart $filterEnd ORDER BY a.fvoiddt ASC LIMIT 1");}else{$stmt=mysqli_query($connMyRetail,"SELECT DISTINCT a.fpkey, a.fcode, a.fvoiddt, a.fvoidreason
            FROM rtl_fc_trans_header a
            JOIN rtl_fc_trans_detail b ON a.fpkey = b.ftranskey
            JOIN $dbNameMyConnection.sync_data c ON a.fpkey = c.fpkey
            WHERE tipe_tabel = '$tipe_tabel' AND is_sync = '0' AND type_con = '$type_con' LIMIT 1");}$lastheader=0;$row=mysqli_fetch_assoc($stmt);if($row>0){$result['sales_return']['custom_id']=$row['fpkey'];$result['sales_return']['lastheader']=$lastheader;$result['sales_return']['transaction_date']=$row['fvoiddt'];$result['sales_return']['invoice_no']=$bcode.'-'.$row['fcode'];$result['sales_return']['message']=$row['fvoidreason'];$id_header=$row['fpkey'];$stmt2=mysqli_query($connMyRetail,"SELECT b.fname, SUM(a.fqty) AS fqty
            FROM rtl_fc_trans_detail a
            JOIN inv_item b ON a.fitemcode = b.fcode
            WHERE a.ftranskey = '$id_header' AND a.fcancelby = 0 GROUP BY b.fname");$j=0;$num=mysqli_num_rows($stmt2);while($row2=mysqli_fetch_assoc($stmt2)){$result['sales_return']['transaction_return_lines_attributes'][$j]['product_name']=$row2['fname'];$result['sales_return']['transaction_return_lines_attributes'][$j]['return_quantity']=$row2['fqty'];$j++;}}return $result;}}