<?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){$base=SyncConfig::$base;$curl=curl_init();curl_setopt_array($curl,array(CURLOPT_URL=>"https://$base.jurnal.id/core/api/v1/sales_invoices/batch_create",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,$tgl_tran,$idHeaderLog,$lastheader,$jenis_sinkron){$conn=SyncConfig::getConnection('MyConnection');$type_con=SyncConfig::$type_con;$berhasil=0;$data=json_decode($data,TRUE);for($i=0;$i<count($data['sales_invoices']);$i++){$tgl=$tgl_tran[$i];$status=$data['sales_invoices'][$i]['sales_invoice']['status'];$id_jurnal=0;if($status==201){$id=$data['sales_invoices'][$i]['sales_invoice']['content']['custom_id'];$id_jurnal=$data['sales_invoices'][$i]['sales_invoice']['content']['id'];$status_sinkron="success";$ket="";$berhasil++;}elseif($status==409&&isset($data['sales_invoices'][$i]['sales_invoice']['content']['custom_id'])){$id=$data['sales_invoices'][$i]['sales_invoice']['body']['sales_invoice']['custom_id'];$status_sinkron="success";$ket="";$berhasil++;}else{$status_sinkron="failed";$id=$data['sales_invoices'][$i]['sales_invoice']['body']['sales_invoice']['custom_id'];$ket=json_encode($data['sales_invoices'][$i]['sales_invoice']['content']);$ket=addslashes($ket);}if($jenis_sinkron=='manual'){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')");}else{$stmt=mysqli_query($conn,"SELECT * FROM sync_to_jurnal WHERE id_header = '$idHeaderLog' AND id_record = '$id' AND tipe_tabel = '$tipe_tabel' AND type_con = '$type_con' AND tipe_proses = 'new'");if(mysqli_num_rows($stmt)>0){mysqli_query($conn,"UPDATE sync_to_jurnal SET status_sinkron = '$status_sinkron', id_jurnal = '$id_jurnal', keterangan = '$ket' WHERE id_record = '$id' AND tipe_tabel = '$tipe_tabel' AND id_header = '$idHeaderLog' AND type_con = '$type_con' AND tipe_proses = 'new'");}else{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');$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="";$stmt=mysqli_query($conn,"SELECT id_record, last_update FROM sync_to_jurnal WHERE tipe_tabel = '$tipe_tabel' AND type_con = '$type_con' ORDER BY last_update DESC LIMIT 1");$row=mysqli_fetch_assoc($stmt);if($row>0){$last_key=$row['id_record'];$last_update=$row['last_update'];}$stmt=mysqli_query($conn,"SELECT tax_name, branch_code, branch_name FROM sync_config WHERE type_con = '$type_con'");$row=mysqli_fetch_assoc($stmt);$tax=$row['tax_name'];$bname=$row['branch_name'];$bcode=$row['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, fpos_tax, 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, fpos_tax, a.fpembulatan, id_header, 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_to_jurnal d ON a.fpkey = d.id_record
            WHERE status_sinkron = 'proses' $filterStart $filterEnd AND tipe_proses = 'new' AND tipe_tabel = '$tipe_tabel' LIMIT 1");}$i=0;while($row=mysqli_fetch_assoc($stmt)){$price=0;if(isset($row['id_header'])){$lastheader=$row['id_header'];}else{$lastheader=0;}if($row['fpos_tax']==0){$tax="";}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_invoices'][$i]['sales_invoice']['custom_id']=$row['fpkey'];$result['sales_invoices'][$i]['sales_invoice']['transaction_date']=$row['fdate'];$result['sales_invoices'][$i]['sales_invoice']['lastheader']=$lastheader;$result['sales_invoices'][$i]['sales_invoice']['date']=$row['fco_date']." ".$row['fco_time'];$result['sales_invoices'][$i]['sales_invoice']['due_date']=$row['fdate'];$result['sales_invoices'][$i]['sales_invoice']['person_name']=$row['fname'].$del;$result['sales_invoices'][$i]['sales_invoice']['term_name']="Cash On Delivery";$result['sales_invoices'][$i]['sales_invoice']['transaction_no']=$bcode.'-'.$row['fcode'];$result['sales_invoices'][$i]['sales_invoice']['tags'][0]=$bname;$result['sales_invoices'][$i]['sales_invoice']['discount_type_name']=$disc_name;$result['sales_invoices'][$i]['sales_invoice']['discount_unit']=$disc_value;$id_header=$row['fpkey'];$stmt2=mysqli_query($connMyResto,"SELECT b.fname, a.fprice, a.fnotespricesubtotal, a.fnotes, a.fqty, a.fdisctype, a.fdiscvalue, a.fdiscount, fcomboheader, fnotesglobal, b.fdeleted
                FROM rst_fc_trans_detail a
                JOIN rst_menu b ON a.fmenukey = b.fpkey
                WHERE a.ftranskey = '$id_header' AND fiscomboheader != 1");$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['fdeleted']==1){$del="(Deleted)";}else{$del="";}if($row['fiscompliment']==0){$price=$row2['fprice']+$row2['fnotespricesubtotal'];}$result['sales_invoices'][$i]['sales_invoice']['transaction_lines_attributes'][$j]['quantity']=$row2['fqty'];$result['sales_invoices'][$i]['sales_invoice']['transaction_lines_attributes'][$j]['product_name']=$row2['fname'].$del;$result['sales_invoices'][$i]['sales_invoice']['transaction_lines_attributes'][$j]['rate']=$price;$result['sales_invoices'][$i]['sales_invoice']['transaction_lines_attributes'][$j]['discount']=$discount;$result['sales_invoices'][$i]['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 a.fnotes, SUM(a.fqty) AS jml
                FROM rst_fc_trans_detail_modifier a
                JOIN rst_fc_trans_detail b ON a.ftranskeyd = b.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_invoices'][$i]['sales_invoice']['transaction_lines_attributes'][$j]['quantity']=$row2['jml'];$result['sales_invoices'][$i]['sales_invoice']['transaction_lines_attributes'][$j]['product_name']=$row2['fnotes'];$result['sales_invoices'][$i]['sales_invoice']['transaction_lines_attributes'][$j]['rate']=0;$result['sales_invoices'][$i]['sales_invoice']['transaction_lines_attributes'][$j]['discount']=0;$result['sales_invoices'][$i]['sales_invoice']['transaction_lines_attributes'][$j]['line_tax_name']=$tax;$j++;}$i++;}return $result;}}