<?php namespace myresto\models;use Yii;use yii\base\Model;use myresto\models\SyncConfig;class ImportTransaction extends Model{public $tipe_tabel="rst_fc_trans_header";public function kirimData($data,$access_token,$endpoint,$method){$base=SyncConfig::$base;$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=>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');$connMyResto=SyncConfig::getConnection('MyResto');$type_con=SyncConfig::$type_con;$berhasil=0;$tgl=$result['sales_invoice']['date'];$data=json_decode($data,TRUE);$id_jurnal=0;if(isset($data['sales_invoice'])){$id=$data['sales_invoice']['custom_id'];$id_jurnal=$data['sales_invoice']['id'];$status_sinkron="success";$ket="";$berhasil++;}elseif(isset($data['custom_id'])||isset($data['transaction_no'])){$id=$result['sales_invoice']['custom_id'];$id_jurnal=$data['id'];ImportTransaction::kirimData($result,$access_token,"sales_invoices/$id_jurnal","PATCH");$status_sinkron="success";$ket="";$berhasil++;}else{$status_sinkron="failed";$id=$id=$result['sales_invoice']['custom_id'];$ket=json_encode($data['error_full_messages']);$ket=addslashes($ket);$cari=array('a','b','c','d','e','i','k','l','n','o','p','r','s','t','u','w','-','_',' ','A','B','C','D','E','I','K','L','N','O','P','R','S','T','U','W');foreach($data['error_full_messages'] as $key){if(preg_match("/Person name person not exist/i",$key)){$stmt=mysqli_query($connMyResto,"SELECT fcustkey FROM rst_fc_trans_header WHERE fpkey = '$id'");$row=mysqli_fetch_assoc($stmt);$fpkey=$row['fcustkey'];$tabel='bp_customer';}elseif(preg_match("/product di transaction lines attributes untuk baris/i",$key)||preg_match("/product in transaction lines attributes on row/i",$key)){$index=str_replace($cari,"",$key);$index-=1;$detail=array();$stmt=mysqli_query($connMyResto,"SELECT b.fname, a.fprice, a.fnotespricesubtotal, a.fnotes, a.fqty, a.fdisctype, a.fdiscvalue, a.fdiscount, fcomboheader, fnotesglobal, b.fdeleted, fmenukey
                FROM rst_fc_trans_detail a
                JOIN rst_menu b ON a.fmenukey = b.fpkey
                WHERE a.ftranskey = '$id' AND fiscomboheader != 1 AND a.fcancelby = 0");$num_prod=mysqli_num_rows($stmt);$num_prod-=1;while($row=mysqli_fetch_assoc($stmt)){array_push($detail,$row['fmenukey']);}$stmt=mysqli_query($connMyResto,"SELECT a.fnoteskey, c.fnotes, SUM(a.fqty) AS jml
                FROM rst_fc_trans_detail_modifier a
                JOIN rst_fc_trans_detail b ON a.ftranskeyd = b.fpkey
                JOIN rst_menu_notes2 c ON a.fnoteskey = c.fpkey
                WHERE a.fpkey > 0 AND ftype = '4' AND ftranskeyh = '$id' AND fcancelby = 0 GROUP BY a.fnotes");while($row=mysqli_fetch_assoc($stmt)){array_push($detail,$row['fnoteskey']);}$fpkey=$detail[$index];if($index>$num_prod){$tabel="rst_menu_notes2";}else{$tabel="rst_menu";}}mysqli_query($conn,"UPDATE sync_data SET is_sync = '0' WHERE fpkey = '$fpkey' AND tipe_tabel = '$tabel' AND type_con = '$type_con'");}}$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,$start,$end,$id_header){$type_con=SyncConfig::$type_con;$conn=SyncConfig::getConnection('MyConnection');$connMyResto=SyncConfig::getConnection('MyResto');$dbNameMyConnection=SyncConfig::getDbNameMyConnection();$last_key=0;$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'];}$stmt2=mysqli_query($conn,"SELECT tax_name, branch_code, branch_name FROM sync_config WHERE type_con = '$type_con'");$row2=mysqli_fetch_assoc($stmt2);$tax_awal=$row2['tax_name'];$bname=$row2['branch_name'];$bcode=$row2['branch_code'];$result=array();$x=array();if($status=="import"){$stmt=mysqli_query($connMyResto,"SELECT DISTINCT a.fpkey, a.fcode, a.fdate, a.fpos_discType, a.fpos_discValue, a.fpos_discount, a.fiscompliment, b.fname, fco_date, fco_time, a.fpembulatan, b.fdeleted
            FROM rst_fc_trans_header a
            LEFT JOIN bp_customer b ON a.fcustkey = b.fpkey
            JOIN rst_fc_trans_detail c ON a.fpkey = c.ftranskey
            WHERE concat(fco_date, ' ', fco_time) > '$last_update' AND a.fcancelby = 0 $filterStart $filterEnd ORDER BY concat(fco_date, ' ', fco_time) ASC LIMIT 1");}else{$stmt=mysqli_query($connMyResto,"SELECT DISTINCT a.fpkey, a.fcode, a.fdate, a.fpos_discType, a.fpos_discValue, a.fpos_discount, a.fiscompliment, b.fname, fco_date, fco_time, a.fpembulatan, b.fdeleted
            FROM rst_fc_trans_header a
            LEFT JOIN bp_customer b ON a.fcustkey = b.fpkey
            JOIN rst_fc_trans_detail c ON a.fpkey = c.ftranskey
            JOIN $dbNameMyConnection.sync_data d ON a.fpkey = d.fpkey
            WHERE tipe_tabel = '$tipe_tabel' AND is_sync = '0' AND type_con = '$type_con' LIMIT 1");}if($row=mysqli_fetch_assoc($stmt)){$price=0;if(isset($row['id_header'])){$lastheader=$row['id_header'];}else{$lastheader=0;}if($row['fpos_discType']==1){$disc_name="value";$disc_value=$row['fpos_discount'];}else{$disc_name="Percent";$disc_value=$row['fpos_discValue'];}if($row['fdeleted']==1){$del="(Deleted)";}else{$del="";}$result['sales_invoice']['custom_id']=$row['fpkey'];$result['sales_invoice']['transaction_date']=$row['fdate'];$result['sales_invoice']['lastheader']=$lastheader;$result['sales_invoice']['date']=$row['fco_date']." ".$row['fco_time'];$result['sales_invoice']['due_date']=$row['fdate'];$result['sales_invoice']['person_name']=$row['fname'].$del;$result['sales_invoice']['term_name']="Cash On Delivery";$result['sales_invoice']['transaction_no']=$bcode.'-'.$row['fcode'];$result['sales_invoice']['tags'][0]=$bname;$result['sales_invoice']['discount_type_name']=$disc_name;$result['sales_invoice']['discount_unit']=$disc_value;$id_header=$row['fpkey'];$stmt2=mysqli_query($connMyResto,"SELECT b.fname, a.fprice, a.fnotespricesubtotal, a.fnotes, SUM(a.fqty) AS fqty, a.fdisctype, a.fdiscvalue, a.fdiscount, fcomboheader, fnotesglobal, b.fdeleted, a.fnontax
                FROM rst_fc_trans_detail a
                JOIN rst_menu b ON a.fmenukey = b.fpkey
                WHERE a.ftranskey = '$id_header' AND fiscomboheader != 1 AND a.fcancelby = 0 GROUP BY b.fname");$j=0;while($row2=mysqli_fetch_assoc($stmt2)){if($row2['fdisctype']==1){$discount=$row2['fdiscvalue'];}elseif($row2['fdisctype']==2){$discount=($row2['fdiscount']/$row2['fprice'])*100;}else{$discount=0;}if($row2['fnontax']==1){$tax="";}else{$tax=$tax_awal;}if($row2['fdeleted']==1){$del="(Deleted)";}else{$del="";}if($row['fiscompliment']==0){$price=$row2['fprice']+$row2['fnotespricesubtotal'];}$result['sales_invoice']['transaction_lines_attributes'][$j]['quantity']=$row2['fqty'];$result['sales_invoice']['transaction_lines_attributes'][$j]['product_name']=$row2['fname'].$del;$result['sales_invoice']['transaction_lines_attributes'][$j]['rate']=$price;$result['sales_invoice']['transaction_lines_attributes'][$j]['discount']=$discount;$result['sales_invoice']['transaction_lines_attributes'][$j]['line_tax_name']=$tax;if($row2['fcomboheader']!=0){$x=$row2['fcomboheader'];$ssmt=mysqli_query($connMyResto,"SELECT a.fname FROM rst_menu a JOIN rst_fc_trans_detail b ON a.fpkey = b.fmenukey WHERE b.fpkey = '$x'");$r=mysqli_fetch_assoc($ssmt);$result['sales_invoices'][$i]['sales_invoice']['transaction_lines_attributes'][$j]['description']=$r['fname'];}$j++;}$stmt2=mysqli_query($connMyResto,"SELECT c.fnotes, SUM(a.fqty) AS jml
                FROM rst_fc_trans_detail_modifier a
                JOIN rst_fc_trans_detail b ON a.ftranskeyd = b.fpkey
                JOIN rst_menu_notes2 c ON a.fnoteskey = c.fpkey
                WHERE a.fpkey > 0 AND ftype = '4' AND ftranskeyh = '$id_header' AND fcancelby = 0 GROUP BY a.fnotes");while($row2=mysqli_fetch_assoc($stmt2)){$result['sales_invoice']['transaction_lines_attributes'][$j]['quantity']=$row2['jml'];$result['sales_invoice']['transaction_lines_attributes'][$j]['product_name']=$row2['fnotes'];$result['sales_invoice']['transaction_lines_attributes'][$j]['rate']=0;$result['sales_invoice']['transaction_lines_attributes'][$j]['discount']=0;$result['sales_invoice']['transaction_lines_attributes'][$j]['line_tax_name']=$tax;$j++;}}return $result;}}