<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Siak extends CI_Model
{

    function __construct() 
    {
        parent::__construct();
    }

    public function data_invoice_item($itm_acc, $itm_desc, $itm_qty, $itm_price, $itm_disc, $itm_tax)
    {
      $data = array(
        'acc'=>$itm_acc,
        'desc'=>$itm_desc,
        'qty'=>$itm_qty,
        'price'=>str_replace(",", "", $itm_price),
        'disc'=>$itm_disc,
        'tax'=>$itm_tax,
        'id_sinkron'=>'0',
        'added_by'=>$this->session->userdata('userid'),
          'date_added'=>date('Y-m-d h:i:s'),
          'id_sinkron'=>'0'
      );
      return $data;

    }

    public function insert_receipt_item($id_receipt_item_root, $acc, $desc, $qty, $price, $tax, $id_cust, $id_supp, $id_inv, $id_purchase, $id_asset, $dari, $added_by, $date_added, $proses_untuk)
    {
      $data2=array(
        'id_receipt'=>$id_receipt_item_root,
        'acc'=>$acc,
        'desc'=>$desc,
        'qty'=>$qty,
        'price'=>$price,
        'tax'=>$tax!=''?$tax:'0',
        'id_cust'=>$id_cust,
        'id_supp'=>$id_supp!=''?$id_supp:'0',
        'id_inv'=>$id_inv!=''?$id_inv:'0',
        'id_purchase'=>$id_purchase!=''?$id_purchase:'0',
        'id_asset'=>$id_asset!=''?$id_asset:'0',
        'dari'=>$dari,
        'added_by'=>$added_by,
        'date_added'=>$date_added,
        'id_sinkron'=>'0'
      );

      if($proses_untuk == 'insert' || $proses_untuk == 'update'){
        $this->M_general->insert_db('siak_tr_receipt_item', $data2);
      }else if($proses_untuk == 'kas_bon_insert' || $proses_untuk == 'kas_bon_update'){
        $this->M_general->insert_db('siak_tr_kas_bon_item', $data2);
      }

      //echo $this->db->last_query(); 
    }
   
    
    public function new_invoice_registrasi($id_cust,$id_cabang,$tgl_approve, $tipe_pembayaran,$thn_ajaran,$biaya, $id_acc)
    {



        //CREATE INVOICE 
        $proses_untuk = 'insert';
        $n=1;
        $itm_price[0] = $biaya;
        $itm_qty[0] = '1';

        
        $cek_sms = $this->M_general->get_value('permohonan_daftar_mhs','id_cust',$id_cust,'id_sms');
        $itm_acc[0]  = $this->M_general->get_value3('siak_re_account_chart','tipe','Pendaftaran','id_sms',$cek_sms,'id_cabang',$id_cabang,'id');
        //echo $this->db->last_query();
        if($itm_acc[0] == ''){
          //jika akun tidak dapet masukin ke suspend
          $itm_acc[0] = '57'; 
        }
       

        $itm_desc[0] = 'Pendaftaran Uang Registrasi (Daftar Online)';
        $itm_disc = '0';
        $itm_tax = '0';
        $tgl = $tgl_approve;
        $id = $this->M_general->get_ai('siak_tr_invoice');
        $id_invs = $id;
        $no_inv= 'INV'.sprintf("%03d",$id_invs).'/AUTO-REG1/'.$this->M_general->roman(date("n")).'/'.date("Y");
        $no_po = "";
        $cust = $id_cust;
        $addr = $this->M_general->get_value('permohonan_daftar_mhs','id_cust',$id_cust,'alamat_rumah');
        $desc = 'Uang Registrasi Pendaftaran Online Mediterranean Bali (Daftar Online)';
        $untuk = '0';
        $note = 'Pembayaran ini otomatis ditrigger oleh sistem pendaftaran online, hubungi admin IT jika terjadi kesalahan inputan pada nama akun / kesalahan lainnya';
        $invoice_id = $id;

        $data=array(
          'tgl'=>$tgl,
          'no_inv'=>$no_inv,
          'no_po'=>$no_po,
          'id_cust'=>$cust,
          'addr'=>$addr,
          'desc'=>$desc,
          'untuk'=>$untuk,
          'note'=>$note,
          'added_by'=>$this->session->userdata('userid'),
          'date_added'=>date('Y-m-d h:i:s'),
          'id_thn_ajaran'=>$thn_ajaran,
          'id_sinkron'=>'0'
        );

        if($proses_untuk == 'insert')
        {
          $arr_insert  = array('id'=>$id);
          $data_insert = array_merge($data, $arr_insert);

          $this->db->trans_start();
          if($this->M_general->insert_db('siak_tr_invoice', $data_insert)==true)
          { //insert items
            $n = count($itm_acc);
            for($i=0; $i<$n; $i++)
            {
              if($itm_price[$i]!='' && $itm_qty[$i]!='' && $itm_price[$i]!='0' ){

                $data2 = $this->data_invoice_item(
                  $itm_acc[$i], 
                  $itm_desc[$i], 
                  $itm_qty[$i], 
                  str_replace(",", "", $itm_price[$i]), 
                  $itm_disc[$i], 
                  $itm_tax[$i]
                );

                $arr_insert = array(
                    'id_inv'=>$id,
                    'added_by'=>$this->session->userdata('userid'),
                    'date_added'=>date('Y-m-d h:i:s'),
                    'id_sinkron'=>'0'
                );

                $data_insert = array_merge($data2, $arr_insert);
                $this->M_general->insert_db('siak_tr_inv_item', $data_insert);


              }
            }
          }
        }
        if($this->db->trans_complete()){  }
        else { echo "Terjadi kesalahan saat pembuatan invoice pendaftaran";}
        


        //CREATE RECEIVE INVOICE

        $dari = 'receive money';

        /*
        if($tipe_pembayaran == 'Cash'){
          $acc = $this->M_general->get_value2('siak_tr_cash_account','id_cabang',$id_cabang,'custom_trigger','daftar_online_reg1','id');
        }else if($tipe_pembayaran == 'Transfer'){
          if($id_cabang == '1'){
            $acc = '58';
          }else if($id_cabang == '2'){
            $acc = '55';
          }else if($id_cabang == '3'){
            $acc = '50';
          }else if($id_cabang == '4'){
            $acc = '47';
          }else{
            $acc = '';
          }
        }else{
          $acc = '';
        }
        */
        $acc = $this->M_general->get_value2('siak_tr_cash_account','id_cabang',$id_cabang,'custom_trigger','daftar_online_reg1','id');



        if($acc != '')
        {
         
          $payer= $this->M_general->get_value('permohonan_daftar_mhs','id_cust',$id_cust,'nama_lengkap');
          $desc= "Transaksi pembayaran uang biaya pendaftaran (Daftar Online)";
          $itm_cust[0] = $id_cust;
          $itm_acc[0] = '22';
          $itm_desc[0] = $desc;
          $itm_qty[0] = '1';
          $itm_price[0] = $biaya;
          $itm_tax[0] = '0';
          $cust_id=$itm_cust;
          $itm_supp[0]='';
          $itm_inv[0]=$invoice_id;
          $itm_purch[0]='';
          $itm_asset[0]='';
         

          if($dari == 'receive money')
            $rec = 1;
          else if($dari == 'spend money')
            $rec = 0;


          if($proses_untuk == 'insert' || $proses_untuk == 'kas_bon_insert')
          {
            $id = $this->M_general->get_ai('siak_tr_receipt'); 
            $id_invs = $id;
            $ref= 'RM'.sprintf("%03d",$id_invs).'/AUTO-REG2/'.$this->M_general->roman(date("n")).'/'.date("Y");  
            
            $cek_pettycash = $this->M_general->get_row_custom('siak_tr_cash_account','id',$acc)->view_neraca;//echo $this->db->last_query();
            $data=array(
              'id'=>$id,
              'tgl'=>$tgl,
              'ref'=>$ref,
              'acc_id'=>$acc,
              'payer'=>$payer,
              'desc'=>$desc,
              'notes'=>$note,
              'receive'=>$rec,
              'added_by'=>$this->session->userdata('userid'),
              'date_added'=>date('Y-m-d h:i:s'),
              'is_pettycash'=>$cek_pettycash==1?"1":"0",
              'id_thn_ajaran'=>$thn_ajaran,
              'id_sinkron'=>'0'
            );
            
            $this->db->trans_start();

            if($proses_untuk == 'insert'){
              $query_insert = $this->M_general->insert_db('siak_tr_receipt', $data);
            }

            if($query_insert==true)
            { //insert items
              $n = count($itm_acc);
              for($i=0; $i<$n; $i++)
              {
                if($itm_price[$i]!='' && $itm_qty[$i]!='' && $itm_price[$i]!='0' ){

                  $cust_id="";
                  if($itm_cust[$i] != '' && $itm_cust[$i] != null){
                    $cust_id = $itm_cust[$i];
                  }
                  else if($id_sdm[$i] != '' && $id_sdm[$i] != null ){
                    $cust_id = $id_sdm[$i];
                  }
                  
                  if($itm_acc[$i] == '22' && $cust_id == ''){
                    $itm_acc[$i] = '57'; //konfert jadi suspense klo transaksi piutang gak lengkap
                  }
                  else if($itm_acc[$i] == '5' && $itm_supp[$i] == ''){
                    $itm_acc[$i] = '57'; //konfert jadi suspense klo transaksi utang gak lengkap
                  }
                  else if($itm_acc[$i] == '24' && $itm_asset[$i] == '0'){
                    $itm_acc[$i] = '57'; //konfert jadi suspense klo transaksi asset gak lengkap
                  }
                  else if($itm_acc[$i] == '32' && $cust_id == ''){
                    $itm_acc[$i] = '57'; //konfert jadi suspense klo transaksi payslip employee gak lengkap
                  }


                  
                  $this->insert_receipt_item(
                    $id_receipt_item_root=$id,
                    $acc=$itm_acc[$i],
                    $desc=$itm_desc[$i],
                    $qty=$itm_qty[$i],
                    $price=str_replace(",", "", $itm_price[$i]),
                    $tax=$itm_tax[$i],
                    $id_cust=$cust_id,
                    $id_supp=isset($itm_supp[$i])?$itm_supp[$i]:'',
                    $id_inv=isset($itm_inv[$i])?$itm_inv[$i]:'',
                    $id_purchase=isset($itm_purch[$i])?$itm_purch[$i]:'',
                    $id_asset=isset($itm_asset[$i])?$itm_asset[$i]:'',
                    $dari=$dari,
                    $added_by=$this->session->userdata('userid'),
                    $date_added=date('Y-m-d h:i:s'),
                    $proses_untuk=$proses_untuk
                  );

                }
              }
            }
          }

          if($this->db->trans_complete()){
           
          }
          else { 
            $this->session->set_flashdata('error_sinkron_siak_invoice_receive',true);
            //redirect($_SERVER['HTTP_REFERER'],'refresh'); 
          }

        }

        if($acc == ''){
          $this->session->set_flashdata('error_sinkron_siak_receive',true);
          //redirect($_SERVER['HTTP_REFERER'],'refresh'); 
        }
    }



    public function new_invoice_daftar_ulang($id_cust,$id_cabang,$nominal,$tgl_approve,$thn_ajaran)
    {

        //CREATE INVOICE 
        $proses_untuk = 'insert';
        $n=1;
        $itm_price[0] =  $this->M_general->get_value('siak_re_data','id','13','value');
        $itm_qty[0] = '1';

        
        $cek_sms = $this->M_general->get_value('permohonan_daftar_mhs','id_cust',$id_cust,'id_sms_terpilih');
        $itm_acc[0]  = $this->M_general->get_value3('siak_re_account_chart','tipe','Pendidikan','id_sms',$cek_sms,'id_cabang',$id_cabang,'id');

        //echo $this->db->last_query();
        if($itm_acc[0] == ''){
          //jika akun tidak dapet masukin ke suspend
          $itm_acc[0] = '57'; 
        }
       

        $itm_desc[0] = 'Daftar Ulang (Daftar Online)';
        $itm_disc = '0';
        $itm_tax = '0';
        $tgl = $tgl_approve;
        $id = $this->M_general->get_ai('siak_tr_invoice');
        $id_invs = $id;
        $no_inv= 'INV'.sprintf("%03d",$id_invs).'/AUTO-REG2/'.$this->M_general->roman(date("n")).'/'.date("Y");
        $no_po = "";
        $cust = $id_cust;
        $addr = $this->M_general->get_value('permohonan_daftar_mhs','id_cust',$id_cust,'alamat_rumah');
        $desc = 'Uang Daftar Ulang Pendaftaran Online Mediterranean Bali (Daftar Online)';
        $untuk = '0';
        $note = 'Pembayaran ini otomatis ditrigger oleh sistem pendaftaran online, hubungi admin IT jika terjadi kesalahan inputan pada nama akun / kesalahan lainnya';
        $invoice_id = $id;

        $data=array(
          'tgl'=>$tgl,
          'no_inv'=>$no_inv,
          'no_po'=>$no_po,
          'id_cust'=>$cust,
          'addr'=>$addr,
          'desc'=>$desc,
          'untuk'=>$untuk,
          'note'=>$note,
          'added_by'=>$this->session->userdata('userid'),
          'date_added'=>date('Y-m-d h:i:s'),
          'id_thn_ajaran'=>$thn_ajaran,
          'id_sinkron'=>'0'
        );

        if($proses_untuk == 'insert')
        {
          $arr_insert  = array('id'=>$id);
          $data_insert = array_merge($data, $arr_insert);

          $this->db->trans_start();
          if($this->M_general->insert_db('siak_tr_invoice', $data_insert)==true)
          { //insert items
            $n = count($itm_acc);
            for($i=0; $i<$n; $i++)
            {
              if($itm_price[$i]!='' && $itm_qty[$i]!='' && $itm_price[$i]!='0' ){

                $data2 = $this->data_invoice_item(
                  $itm_acc[$i], 
                  $itm_desc[$i], 
                  $itm_qty[$i], 
                  str_replace(",", "", $itm_price[$i]), 
                  $itm_disc[$i], 
                  $itm_tax[$i]
                );

                $arr_insert = array(
                  'id_inv'=>$id,
                    'added_by'=>$this->session->userdata('userid'),
                    'date_added'=>date('Y-m-d h:i:s'),
                    'id_sinkron'=>'0'
                );

                $data_insert = array_merge($data2, $arr_insert);
                $this->M_general->insert_db('siak_tr_inv_item', $data_insert);
                

              }
            }
          }
        }
        if($this->db->trans_complete()){  }
        else { 
         
          echo "Terjadi kesalahan saat pembuatan invoice pendaftaran ";}
        


        //CREATE RECEIVE INVOICE

        $dari = 'receive money';
        $acc = $this->M_general->get_value2('siak_tr_cash_account','id_cabang',$id_cabang,'custom_trigger','daftar_online_reg2','id');

        if($acc != '')
        {

          $payer= $this->M_general->get_value('permohonan_daftar_mhs','id_cust',$id_cust,'nama_lengkap');
          $desc= "Transaksi pembayaran uang biaya daftar ulang (Daftar Online)";
          $itm_cust[0] = $id_cust;
          $itm_acc[0] = '22';
          $itm_desc[0] = $desc;
          $itm_qty[0] = '1';
          $itm_price[0] = $nominal;
          $itm_tax[0] = '0';
          $cust_id=$itm_cust;
          $itm_supp[0]='';
          $itm_inv[0]=$invoice_id;
          $itm_purch[0]='';
          $itm_asset[0]='';
         

          if($dari == 'receive money')
            $rec = 1;
          else if($dari == 'spend money')
            $rec = 0;


          if($proses_untuk == 'insert' || $proses_untuk == 'kas_bon_insert')
          {
            $id = $this->M_general->get_ai('siak_tr_receipt'); 
            $id_invs = $id;
            $ref= 'RM'.sprintf("%03d",$id_invs).'/AUTO-REG2/'.$this->M_general->roman(date("n")).'/'.date("Y");  
            
            $cek_pettycash = $this->M_general->get_row_custom('siak_tr_cash_account','id',$acc)->view_neraca; 
            $data=array(
              'id'=>$id,
              'tgl'=>$tgl,
              'ref'=>$ref,
              'acc_id'=>$acc,
              'payer'=>$payer,
              'desc'=>$desc,
              'notes'=>$note,
              'receive'=>$rec,
              'added_by'=>$this->session->userdata('userid'),
              'date_added'=>date('Y-m-d h:i:s'),
              'is_pettycash'=>$cek_pettycash==1?"1":"0",
              'id_thn_ajaran'=>$thn_ajaran,
              'id_sinkron'=>'0'
            );
            
            $this->db->trans_start();

            if($proses_untuk == 'insert'){
              $query_insert = $this->M_general->insert_db('siak_tr_receipt', $data);
            }

            if($query_insert==true)
            { //insert items
              $n = count($itm_acc);
              for($i=0; $i<$n; $i++)
              {
                if($itm_price[$i]!='' && $itm_qty[$i]!='' && $itm_price[$i]!='0' ){

                  $cust_id="";
                  if($itm_cust[$i] != '' && $itm_cust[$i] != null){
                    $cust_id = $itm_cust[$i];
                  }
                  else if($id_sdm[$i] != '' && $id_sdm[$i] != null ){
                    $cust_id = $id_sdm[$i];
                  }
                  
                  if($itm_acc[$i] == '22' && $cust_id == ''){
                    $itm_acc[$i] = '57'; //konfert jadi suspense klo transaksi piutang gak lengkap
                  }
                  else if($itm_acc[$i] == '5' && $itm_supp[$i] == ''){
                    $itm_acc[$i] = '57'; //konfert jadi suspense klo transaksi utang gak lengkap
                  }
                  else if($itm_acc[$i] == '24' && $itm_asset[$i] == '0'){
                    $itm_acc[$i] = '57'; //konfert jadi suspense klo transaksi asset gak lengkap
                  }
                  else if($itm_acc[$i] == '32' && $cust_id == ''){
                    $itm_acc[$i] = '57'; //konfert jadi suspense klo transaksi payslip employee gak lengkap
                  }


                  
                  $this->insert_receipt_item(
                    $id_receipt_item_root=$id,
                    $acc=$itm_acc[$i],
                    $desc=$itm_desc[$i],
                    $qty=$itm_qty[$i],
                    $price=str_replace(",", "", $itm_price[$i]),
                    $tax=$itm_tax[$i],
                    $id_cust=$cust_id,
                    $id_supp=isset($itm_supp[$i])?$itm_supp[$i]:'',
                    $id_inv=isset($itm_inv[$i])?$itm_inv[$i]:'',
                    $id_purchase=isset($itm_purch[$i])?$itm_purch[$i]:'',
                    $id_asset=isset($itm_asset[$i])?$itm_asset[$i]:'',
                    $dari=$dari,
                    $added_by=$this->session->userdata('userid'),
                    $date_added=date('Y-m-d h:i:s'),
                    $proses_untuk=$proses_untuk
                  );

                }
              }
            }

          }

         if($this->db->trans_complete()){
          
          }
          else { 
            $this->session->set_flashdata('error_sinkron_siak_invoice_receive',true);
            //redirect($_SERVER['HTTP_REFERER'],'refresh'); 
            echo "terjadi kesalahan invoice receipt";
          }



        }

         if($acc == ''){
          $this->session->set_flashdata('error_sinkron_siak_receive',true);
          echo "terjadi kesalahan receipt";
          //redirect($_SERVER['HTTP_REFERER'],'refresh'); 
        }
            
    }

    public function get_value_penarikan($id_sdm, $thn_ajaran=null, $tgl_awal=null, $tgl_akhir=null)
    {
      $this->db->where('id_sdm',$id_sdm);
        $this->db->select('sum(jumlah) as jum');
        $this->db->from('cc_reward_take');
        if($thn_ajaran != null){
            $this->db->like('date_added',$thn_ajaran);
        }
        if($tgl_awal!=null){ $this->db->where('tgl_penarikan >=',$tgl_awal." 00:00:00"); }
        if($tgl_akhir!=null){ $this->db->where('tgl_penarikan <=',$tgl_akhir." 23:59:59"); }
        $this->db->where('deleted','0');
        return $this->db->get()->row()->jum;
    }

    public function account_payable_cc($id_cust, $dari, $pisah_penarikan=null, $thn_ajaran=null, $tgl_awal=null, $tgl_akhir=null)
    {

      $penarikan = $this->get_value_penarikan($id_cust,$thn_ajaran,$tgl_awal,$tgl_akhir);

        if($dari == 'cc_mhs'){
          $filter = 'point';
          $filter_cabang = 'point_cabang';
        }
        if($dari == 'cc_ins'){
          $filter = 'point_instruktur';
          $filter_cabang = 'point_instruktur_cabang';
        }
        if($dari == 'cc_dim'){
          $filter = 'point_dim';
          $filter_cabang = 'point_dim_cabang';
        }
        if($dari == 'cc_mst'){
          $filter = 'point_mst';
          $filter_cabang = 'point_mst_cabang';
        }


      
        $this->db->select('mhs.*');
        $this->db->from('cc_generated_code cc');
        //$this->db->join('permohonan_daftar_dim dim','cc.id_sdm = dim.id_cust');
        $this->db->join('permohonan_daftar_mhs mhs','cc.kupon_code = mhs.kupon_code');
        if($thn_ajaran != null){
          $this->db->like('pendaftaran_untuk_tahun_ajaran',$thn_ajaran);
        }
        $this->db->where('id_sdm',$id_cust);
        $this->db->where('mhs.deleted','0');
        $this->db->where('cc.deleted','0');
        $q = $this->db->get();
      

      

        $ttl_point = 0; 
        $i=1;
        foreach ($q->result() as $key ) {
          $double = 0;

          $cek_thn = explode(" ", $key->pendaftaran_untuk_tahun_ajaran);
                $thn_ajaran_cmaba = $cek_thn[0];
                if($thn_ajaran_cmaba >= '2020'){
                    $thn_ajaran_fillter = '2020';
                }else{
                    $thn_ajaran_fillter = null;
                }


          if($key->status_complete == '99'){ 
            $double = 1;
                    if($key->id_cabang == 1){
                        $point = $this->M_general->get_value2('cc_status_complete','status','TERDAFTAR','id_thn_ajaran',$thn_ajaran_fillter, $filter);
                    }else{
                        $point = $this->M_general->get_value2('cc_status_complete','status','TERDAFTAR','id_thn_ajaran',$thn_ajaran_fillter, $filter_cabang);
                    }
          }else if($key->status_complete == '4'){
            $double = 1;
                    if($key->id_cabang == 1){
                        $point = $this->M_general->get_value2('cc_status_complete','status','DATA COMPLETE','id_thn_ajaran',$thn_ajaran_fillter, $filter);
                    }else{
                        $point = $this->M_general->get_value2('cc_status_complete','status','DATA COMPLETE','id_thn_ajaran',$thn_ajaran_fillter, $filter_cabang);
                    }
          }else if($key->bukti_transfer2!=''||$key->bukti_transfer2!=null){ 
            $double = 1;
                    if($key->id_cabang == 1){
                      $point = $this->M_general->get_value2('cc_status_complete','status','DAFTAR ULANG COMPLETE','id_thn_ajaran',$thn_ajaran_fillter, $filter);
                    }else{
                        $point = $this->M_general->get_value2('cc_status_complete','status','DAFTAR ULANG COMPLETE','id_thn_ajaran',$thn_ajaran_fillter, $filter_cabang);
                    }
          }else if($key->status_complete =='2' && ($key->bukti_transfer!=''||$key->bukti_transfer!=null)){ 
            if($key->id_cabang == 1){
                        $point = $this->M_general->get_value2('cc_status_complete','status','TERFERIFIKASI TES SELEKSI','id_thn_ajaran',$thn_ajaran_fillter, $filter);
                    }else{
                        $point = $this->M_general->get_value2('cc_status_complete','status','TERFERIFIKASI TES SELEKSI','id_thn_ajaran',$thn_ajaran_fillter, $filter_cabang);
                    }
                    if($thn_ajaran_cmaba >= '2020'){$point = 0;}
          }else if($key->status_complete =='1' && ($key->bukti_transfer!=''||$key->bukti_transfer!=null)){ 
            if($key->id_cabang == 1){
                        $point = $this->M_general->get_value2('cc_status_complete','status','REGISTRASI COMPLETE','id_thn_ajaran',$thn_ajaran_fillter, $filter);
                    }else{
                        $point = $this->M_general->get_value2('cc_status_complete','status','REGISTRASI COMPLETE','id_thn_ajaran',$thn_ajaran_fillter, $filter_cabang);
                    }
                    if($thn_ajaran_cmaba >= '2020'){$point = 0;}
          }else if($key->status_complete =='3' && ($key->bukti_transfer!=''||$key->bukti_transfer!=null)){ 
            if($key->id_cabang == 1){
                        $point = $this->M_general->get_value2('cc_status_complete','status','MENUNGGU PEM. DFTR ULG.','id_thn_ajaran',$thn_ajaran_fillter, $filter);
                    }else{
                        $point = $this->M_general->get_value2('cc_status_complete','status','MENUNGGU PEM. DFTR ULG.','id_thn_ajaran',$thn_ajaran_fillter, $filter_cabang);
                    }
                    if($thn_ajaran_cmaba >= '2020'){$point = 0;}
          }else{ 
            if($key->id_cabang == 1){
                        $point = $this->M_general->get_value2('cc_status_complete','status','MENUNGGU PEM. REG.','id_thn_ajaran',$thn_ajaran_fillter, $filter);
                    }else{
                        $point = $this->M_general->get_value2('cc_status_complete','status','MENUNGGU PEM. REG.','id_thn_ajaran',$thn_ajaran_fillter, $filter_cabang);
                    }
          }


          
                if($double == 1){ 
                  if($key->id_cabang == 1){
                      $point_pendaftaran = $this->M_general->get_value2('cc_status_complete','status','REGISTRASI COMPLETE','id_thn_ajaran', $thn_ajaran_fillter,$filter);
                  }else{
                      $point_pendaftaran = $this->M_general->get_value2('cc_status_complete','status','REGISTRASI COMPLETE','id_thn_ajaran', $thn_ajaran_fillter,$filter_cabang);
                  }
                }

                $ttl_point = $ttl_point + $point; $i++;

        }

        if($pisah_penarikan == true){
          $data['ttl_point'] = $ttl_point;
          $data['ttl_penarikan'] = $penarikan;
          $data['ttl_sisa_utang'] = $ttl_point-$penarikan;
          return $data;
        }else{
          return $ttl_point-$penarikan;
        }


      

    
    }

    public function new_jurnal_entry($id_cust, $id_cabang, $tgl_approve, $thn_ajaran, $biaya, $tipe)
    {
        $id = $this->M_general->get_ai('siak_tr_journal_entry');
        $id_invs =  $id;
        $nm_pd = $this->M_general->get_value('permohonan_daftar_mhs','id_cust',$id_cust,'nama_lengkap');
        $nm_cabang = $this->M_general->get_value('permohonan_daftar_cabang','id_permohonan_daftar_cabang',$id_cabang,'nama_cabang');

        $desc = "Jurnal Perbaikan $tipe ".$nm_pd;
        $note = "Detail Nama dan Informasi Lainnya dari Calon Mahasiswa\nNama: $nm_pd\nCabang: $nm_cabang\nUUID: $id_cust\n\nJurnal perbaikan pendaftaran ini otomatis ditrigger oleh sistem pendaftaran online, hubungi admin IT jika terjadi kesalahan inputan pada nama akun / kesalahan lainnya.";
        $ref= 'JU'.sprintf("%03d",$id_invs).'/AUTO/'.$this->M_general->roman(date("n")).'/'.date("Y");
        $data=array(
            'date'=>$tgl_approve,
            'no_inv'=>$ref,
            'naration'=>$desc,
            'notes'=>$note,
            'id_cabang'=>$id_cabang,
            'deleted'=>'0',
            'added_by'=>$this->session->userdata('userid'),
            'date_added'=>date('Y-m-d h:i:s'),
            'id_thn_ajaran'=>$thn_ajaran,
            'id_sinkron'=>'0'
        );
        $this->db->trans_start();

        if($this->M_general->insert_db('siak_tr_journal_entry', $data)==true)
        { //insert items

            $cek_sms = $this->M_general->get_value('permohonan_daftar_mhs','id_cust',$id_cust,'id_sms');
            $acc1 = $this->M_general->get_value3('siak_re_account_chart','tipe',$tipe,'id_sms',$cek_sms,'id_cabang',$id_cabang,'id');
            if($acc1  == ''){
              $acc1 = '57'; 
            }

            //akun utang mahasiswa tahun ajaran baru 
            if($id_cabang == '1'){
              $acc2 = '249';
            }else if($id_cabang == '2'){
              $acc2 = '601';
            }else if($id_cabang == '3'){
              $acc2 = '602';
            }else if($id_cabang == '4'){
              $acc2 = '603';
            }else{
              $acc2 = '57'; 
            }

            
            $data_item = array(
                'id_journal_entry'=>$id,
                'acc'=>$acc1,
                'desc'=>$desc,
                'debit'=>$biaya,
                'credit'=>'0',
                'id_cust'=>'',
                'id_supp'=>'0',
                'id_inv'=>'0',
                'id_purchase'=>'0',
                'id_asset'=>'0',
                'tax'=>'0',
                'added_by'=>$this->session->userdata('userid'),
                'id_sinkron'=>'0',
                'date_added'=>date('Y-m-d h:i:s')
            );
            $this->M_general->insert_db('siak_tr_journal_entry_item', $data_item);
            
            $data_item = array(
                'id_journal_entry'=>$id,
                'acc'=>$acc2,
                'desc'=>$desc,
                'debit'=>'0',
                'credit'=>$biaya,
                'id_cust'=>'',
                'id_supp'=>'0',
                'id_inv'=>'0',
                'id_purchase'=>'0',
                'id_asset'=>'0',
                'tax'=>'0',
                'added_by'=>$this->session->userdata('userid'),
                'id_sinkron'=>'0',
                'date_added'=>date('Y-m-d h:i:s')
            );
            $this->M_general->insert_db('siak_tr_journal_entry_item', $data_item);
            

        }
        $this->db->trans_complete();

    }

    public function new_transfer($id_cust,$id_cabang,$tgl_approve2,$thn_ajaran,$nominal_pembayaran,$tipe)
    {
        $nm_pd = $this->M_general->get_value('permohonan_daftar_mhs','id_cust',$id_cust,'nama_lengkap');
        $nm_cabang = $this->M_general->get_value('permohonan_daftar_cabang','id_permohonan_daftar_cabang',$id_cabang,'nama_cabang');

        if($tipe =='Pendaftaran'){
          $tipe_p = 'Pendaftaran';
        }else if($tipe == 'Pendidikan'){
          $tipe_p = 'Daftar Ulang';
        }
        
       

        $desc = "Transfer $tipe_p Nama: $nm_pd,Cabang: $nm_cabang,UUID: $id_cust (Auto Transaction)";
        $id = $this->M_general->get_ai('siak_tr_cash_transfer');
        $id_invs = $id;
        $ref= 'TRF'.sprintf("%03d",$id_invs).'/AUTO/'.$this->M_general->roman(date("n")).'/'.date("Y");


        if($tipe == 'Pendaftaran'){
            $acc = $this->M_general->get_value2('siak_tr_cash_account','id_cabang',$id_cabang,'custom_trigger','daftar_online_reg1','id');
        }else if($tipe == 'Pendidikan'){
            $acc = $this->M_general->get_value2('siak_tr_cash_account','id_cabang',$id_cabang,'custom_trigger','daftar_online_reg2','id');
        }
        
        
        //bank 2.daftar online 
        $acc_ex = $acc;
        //bank asli cabang
        if($id_cabang == '1'){
          $acc_in = '58';
        }else if($id_cabang == '2'){
          $acc_in = '55';
        }else if($id_cabang == '3'){
          $acc_in = '50';
        }else if($id_cabang == '4'){
          $acc_in = '47';
        }else{
          $acc_in = '0';
        }

        
        $data=array(
            'tgl'=>$tgl_approve2,
            'ref'=>$ref,
            'acc_ex'=>$acc_ex,
            'acc_in'=>$acc_in,
            'ammount'=>str_replace(",", "", $nominal_pembayaran),
            'desc'=>$desc,
            'added_by'=>$this->session->userdata('userid'),
            'date_added'=>date('Y-m-d h:i:s'),
            'id_thn_ajaran'=>$thn_ajaran,
            'id_sinkron'=>'0'
        );
        $this->db->trans_start();
            $this->M_general->insert_db('siak_tr_cash_transfer', $data);
        $this->db->trans_complete();
    }



}
