<?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){$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++;}else{$status_sinkron="failed";$ket=json_encode($data['error_full_messages']);$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' AND tipe_proses = 'new'");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,$start,$end){$type_con=SyncConfig::$type_con;$conn=SyncConfig::getConnection('MyConnection');$connMyRetail=SyncConfig::getConnection('MyRetail');$dbNameMyConnection=SyncConfig::getDbNameMyConnection();$last_key=0;if($start!='0000-00-00'){$tambahan="AND a.fdate >= '$start' AND a.fdate <= '$end'";}else{$tambahan="";}$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'];}$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.fpkey > '$last_key' $tambahan ORDER BY a.fpkey ASC LIMIT 1");}else{$stmt=mysqli_query($connMyRetail,"SELECT DISTINCT a.fpkey, a.fcode, a.fvoiddt, a.fvoidreason, id_header
            FROM rtl_fc_trans_header a
            JOIN rtl_fc_trans_detail b ON a.fpkey = b.ftranskey
            JOIN $dbNameMyConnection.sync_to_jurnal c ON a.fpkey = c.id_record
            WHERE status_sinkron = 'proses' AND tipe_proses = 'new' AND tipe_tabel = '$tipe_tabel' LIMIT 1");}$row=mysqli_fetch_assoc($stmt);if($row>0){if(isset($row['id_header'])){$lastheader=$row['id_header'];}else{$lastheader=0;}$result['sales_return']['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, a.fqty
            FROM rtl_fc_trans_detail a
            JOIN inv_item b ON a.fitemcode = b.fcode
            WHERE a.ftranskey = '$id_header'");$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;}}