<?php namespace myretail\models;use Yii;use yii\base\Model;use myretail\models\SyncConfig;class ImportPayment extends Model{public $tipe_tabel="rtl_fc_payment";public function kirimData($data,$access_token,$endpoint){$base=SyncConfig::$base;if($data==0){$method="GET";$post="";$header=array("apikey: $access_token");}else{$method="POST";$post=json_encode($data);$header=array("apikey: $access_token","Content-Type: application/json");}$curl=curl_init();curl_setopt_array($curl,array(CURLOPT_URL=>"https://$base.jurnal.id/core/api/v1/$endpoint",CURLOPT_RETURNTRANSFER=>true,CURLOPT_CUSTOMREQUEST=>$method,CURLOPT_POSTFIELDS=>$post,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,$lastheader,$jenis_sinkron,$access_token,$result){$conn=SyncConfig::getConnection();$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['receive_payments']);$i++){$status=$data['receive_payments'][$i]['receive_payment']['status'];$id_jurnal=0;if($status==201){$id=$data['receive_payments'][$i]['receive_payment']['content']['custom_id'];$id_jurnal=$data['receive_payments'][$i]['receive_payment']['content']['id'];$status_sinkron="success";$ket="";$berhasil++;}elseif($status==409&&isset($data['receive_payments'][$i]['receive_payment']['content']['custom_id'])){$id=$data['receive_payments'][$i]['receive_payment']['body']['receive_payment']['custom_id'];$res=ImportPayment::kirimData(0,$access_token,"receive_payments/$id");$response=json_decode($res,TRUE);$id_jurnal=$response['receive_payment']['id'];$status_sinkron="success";$ket="";$berhasil++;}else{$status_sinkron="failed";$id=$data['receive_payments'][$i]['receive_payment']['body']['receive_payment']['custom_id'];$ket=json_encode($data['receive_payments'][$i]['receive_payment']['content']);$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')");}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,$access_token,$start,$end,$id_header){$type_con=SyncConfig::$type_con;$conn=SyncConfig::getConnection('MyConnection');$connMyRetail=SyncConfig::getConnection('MyRetail');$dbNameMyConnection=SyncConfig::getDbNameMyConnection();$base=SyncConfig::$base;$last_key=0;if($start!='0000-00-00')$filterStart=" AND a.fdate >= '$start'";else $filterStart="";if($end!='0000-00-00')$filterEnd=" AND a.fdate <= '$end'";else $filterEnd="";$stmt=mysqli_query($conn,"SELECT branch_code FROM sync_config WHERE type_con = '$type_con'");$row=mysqli_fetch_assoc($stmt);$bcode=$row['branch_code'];if($start=="0000-00-00"&&$end=="0000-00-00"){$stmt=mysqli_query($conn,"SELECT fpkey AS id_record FROM sync_data WHERE tipe_tabel = '$tipe_tabel' AND type_con = '$type_con' ORDER BY fpkey DESC LIMIT 1");}else{$stmt=mysqli_query($conn,"SELECT id_record FROM sync_to_jurnal WHERE tipe_tabel = '$tipe_tabel' AND type_con = '$type_con' AND id_header = '$id_header' ORDER BY id_record DESC LIMIT 1");}$row=mysqli_fetch_assoc($stmt);if($row>1){$last_key=$row['id_record'];}$stmt=mysqli_query($connMyRetail,"SELECT ftranskey FROM rtl_fc_payment a WHERE fpkey > '$last_key' $filterStart $filterEnd AND fpayment_type != 99 AND fpayment_type != 101 AND fpayment_type != 100 ORDER BY fpkey LIMIT 1");$row=mysqli_fetch_assoc($stmt);if($row>0){$transkey=$row['ftranskey'];}else{$transkey=0;}$selisih=0;$result=array();if($status=="import"){$stmt=mysqli_query($connMyRetail,"SELECT a.fpkey, e.fname customer, a.ftranskey, b.fcode, a.fbill_amount, a.fpayment, a.fpayment_type, a.fchange, a.fdate, c.fname payment, d.nama_akun, fpembulatan, e.fdeleted
            FROM rtl_fc_payment a
            JOIN rtl_fc_trans_header b ON a.ftranskey = b.fpkey
            JOIN solis_payment_type c ON a.fpayment_type = c.fpkey
            JOIN $dbNameMyConnection.sync_account_to_jurnal d ON a.fpayment_type = d.payment_fpkey
            JOIN bp_customer e ON b.fcustcode = e.fcode
            WHERE a.ftranskey = '$transkey' AND a.fpayment_type != 100 AND type_con = '$type_con'");}else{$stmt=mysqli_query($connMyRetail,"SELECT a.fpkey, f.fname customer, a.ftranskey, b.fcode, a.fbill_amount, a.fpayment, a.fpayment_type, a.fchange, a.fdate, c.fname payment, d.nama_akun, fpembulatan, f.fdeleted
            FROM rtl_fc_payment a
            JOIN rtl_fc_trans_header b ON a.ftranskey = b.fpkey
            JOIN solis_payment_type c ON a.fpayment_type = c.fpkey
            JOIN $dbNameMyConnection.sync_account_to_jurnal d ON a.fpayment_type = d.payment_fpkey
            JOIN $dbNameMyConnection.sync_data e ON a.fpkey = e.fpkey
            JOIN bp_customer f ON b.fcustcode = f.fcode
            WHERE tipe_tabel = '$tipe_tabel' AND is_sync = '0' AND e.type_con = '$type_con' AND d.type_con = '$type_con'");}$i=0;while($row=mysqli_fetch_assoc($stmt)){$payment=$row['fpayment'];if($row['fdeleted']==1){$del="(Deleted)";}else{$del="";}$num=mysqli_num_rows($stmt)-1;$invoice=$bcode.'-'.$row['fcode'];if($status=='import'&&$num==$i){$curl=curl_init();curl_setopt_array($curl,array(CURLOPT_URL=>"https://$base.jurnal.id/core/api/v1/sales_invoices/$invoice",CURLOPT_RETURNTRANSFER=>true,CURLOPT_CUSTOMREQUEST=>"GET",CURLOPT_HTTPHEADER=>array("apikey: $access_token")));$response=curl_exec($curl);curl_close($curl);$data=json_decode($response,TRUE);if(isset($data['sales_invoice']['id'])){$gt_jurnal=str_replace(".","",$data['sales_invoice']['original_amount_currency_format']);$gt_jurnal=str_replace(",",".",$gt_jurnal);$gt_jurnal=str_replace("Rp","",$gt_jurnal);$gt_jurnal=str_replace(" ","",$gt_jurnal);$selisih=$row['fbill_amount'] - $gt_jurnal;}else{$selisih=0;}}else{$selisih=0;}if($row['fpayment_type']==97&&$payment>$row['fbill_amount']){$payment=$gt_jurnal;}$jurnal_entry=abs($selisih);$result['receive_payments'][$i]['receive_payment']['custom_id']=$row['fpkey'];$result['receive_payments'][$i]['receive_payment']['transaction_date']=substr($row['fdate'],0,10);$result['receive_payments'][$i]['receive_payment']['person_name']=$row['customer'].$del;$result['receive_payments'][$i]['receive_payment']['payment_method_name']=$row['payment'];$result['receive_payments'][$i]['receive_payment']['deposit_to_name']=$row['nama_akun'];$result['receive_payments'][$i]['receive_payment']['records_attributes'][0]['transaction_no']=$invoice;$result['receive_payments'][$i]['receive_payment']['records_attributes'][0]['amount']=$payment-$row['fchange']-$selisih;$result['lastheader']=0;$result['jurnal']=$jurnal_entry;$result['pembulatan']=$row['fpembulatan'];$result['method']=$row['payment'];$i++;}return $result;}public function kirimJurnalEntry($jurnal_entry,$result,$access_token,$account){$type_con=SyncConfig::$type_con;$conn=SyncConfig::getConnection();$jEntry=array();$stmt=mysqli_query($conn,"SELECT asset_account_name, income_account_name FROM sync_config WHERE type_con = '$type_con'");$row=mysqli_fetch_assoc($stmt);$jEntry['journal_entry']['transaction_date']=$result['receive_payments'][0]['receive_payment']['transaction_date'];$jEntry['journal_entry']['transaction_no']=$result['receive_payments'][0]['receive_payment']['records_attributes'][0]['transaction_no'];if($jurnal_entry<0.5){$debit=$row['income_account_name'];$kredit=$account;}else{$debit=$account;$kredit=$row['income_account_name'];}$jEntry['journal_entry']['transaction_account_lines_attributes'][0]['account_name']=$debit;$jEntry['journal_entry']['transaction_account_lines_attributes'][0]['debit']=abs($jurnal_entry);$jEntry['journal_entry']['transaction_account_lines_attributes'][1]['account_name']=$kredit;$jEntry['journal_entry']['transaction_account_lines_attributes'][1]['credit']=abs($jurnal_entry);$jurnal=self::kirimData($jEntry,$access_token,'journal_entries');}}