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

require APPPATH.'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;

class Ceisa_tpb extends MY_Controller {
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model('ceisa_tpb_mdl','mdl');
		$this->title = 'Ceisa TPB Integration';
		$this->title2 = 'Pemasukan RM';
	}
	
	function index()
    {       
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $sheet->setCellValue('A1', 'Hello World !');
        
        $writer = new Xlsx($spreadsheet);
 
        $filename = 'name-of-the-generated-file.xlsx';
 
        $writer->save($filename); // will create and save the file in the root of the project
 
    }

	public function bc23()
	{		
		$data = array(
			'title' 	=> 	$this->title,
			'content' 	=> 	'ceisa_tpb/bc23_frm',
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view('ceisa_tpb/_cjs');
		
	}
	
	function bc23_process(){
		if (isset($_REQUEST['simpan'])){
			//echo $_FILES["h"]["type"]."<br>";
			//echo $_FILES["h"]["name"];
			if (($_FILES["h"]["type"] == "application/vnd.ms-excel")){
				if ($_FILES["h"]["error"] < 1){
					//Jenis BC 23
					$DokKdBc=1;

					$inputFileType = IOFactory::identify($_FILES['h']['tmp_name']);
					$objReader = IOFactory::createReader($inputFileType);
					$spreadsheet = $objReader->load($_FILES['h']['tmp_name']);

					//Sheet Header
					$sheet = 0;
					$sheet = $spreadsheet->setActiveSheetIndex($sheet);

					//dapatkan jml baris sheet2
					$totRowExist = $sheet->getHighestRow();
					// echo $totRowExist;

					for ($row = 2; $row <= $totRowExist; $row++):
						$FCAR = $sheet->getCell('A' . $row)->getValue();

						$CAR = substr($FCAR, -6);
						$TgAju=substr($FCAR, 12, 4)."-".substr($FCAR, 16, 2)."-".substr($FCAR, 18, 2);
										
						$KdKpbcAsal = $sheet->getCell('B' . $row)->getValue();
						// $KdJnsTpbAsal = $sheet->getCell('BQ' . $row)->getValue();
						$KdJnsTpbTuj = $sheet->getCell('CI' . $row)->getValue();
						$KdJnsTpbAsal = $KdJnsTpbTuj;

						$KdJnsBarang = $sheet->getCell('BL' . $row)->getValue();
						if ($KdJnsBarang == ''){
							$KdJnsBarang = 1;
						}
						$KdTp = $sheet->getCell('CH' . $row)->getValue();
						if ($KdTp == ''){
							$KdTp = 3;
						}
						$NoDaf = $sheet->getCell('CZ' . $row)->getValue();
						//Tanggal Daftar
						$dMY = $sheet->getCell('DN' . $row)->getValue();
						$TgDaf = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgDaf = $date->format('Y-m-d');	
							}
						}

						//Tanggal TTD
						$dMY = $sheet->getCell('DT' . $row)->getValue();
						$TgTT = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgTT = $date->format('Y-m-d');	
							}
						}

						$KdKpBongkar = $sheet->getCell('BZ' . $row)->getValue();
						$KdKpbcTuj = $sheet->getCell('BS' . $row)->getValue();
						$KdKpPengawas='050500';
						$NpwpPrshn = $sheet->getCell('AI' . $row)->getValue();
						$NmPengusaha = $sheet->getCell('CR' . $row)->getValue();
						$NipPengusaha = $sheet->getCell('AM' . $row)->getValue();
						$NmPejabat='';
						$NipPejabat='';
						$ref_id='';
						$NmTuj='';
						$NmPpjk='';
						$KdTimbun='';
						$KdKemas = '';
						$JmlKemas = $sheet->getCell('AO' . $row)->getValue();
						$MerekKemas = '';
						$BRUTO=$sheet->getCell('T' . $row)->getValue();
						$NETTO=$sheet->getCell('CT' . $row)->getValue();
						$KdVal=$sheet->getCell('CK' . $row)->getValue();
						$CIF=$sheet->getCell('U' . $row)->getValue();
						$CIF_RUPIAH=$sheet->getCell('V' . $row)->getValue();
						$Total='';
						$TotalH='';
						$tgl=$TgDaf." ".date('H:i:s');
						$usr = 'SYSTEM';

						// $ID_HEADER=$r['ID'];
						$NAMA_PENGIRIM = $sheet->getCell('CP' . $row)->getValue();
						$NAMA_PEMASOK = $sheet->getCell('D' . $row)->getValue();		
						$NAMA_PENERIMA_BARANG = $sheet->getCell('CN' . $row)->getValue();
						$ALAMAT_PENGIRIM = $sheet->getCell('K' . $row)->getValue();				
						$ALAMAT_PEMASOK = $sheet->getCell('H' . $row)->getValue();
						$ALAMAT_PENERIMA_BARANG = $sheet->getCell('J' . $row)->getValue(); 
						$HARGA_PENYERAHAN = $sheet->getCell('AD' . $row)->getValue();
						$HrgSerah=$HARGA_PENYERAHAN;

						$q="SELECT MAX(matin_id) AS auto_no FROM mat_inchdr_sub LIMIT 1";
						$rs = $this->db->query($q)->result_array();
						
						$autono=($rs)?$rs[0]['auto_no']+1:1;
						$ref_id=$autono;

						$NAMA_PEMASOK = $NAMA_PEMASOK;

						$wh_Npwp = "";
						if ($NpwpPrshn != ""){
							$wh_Npwp = " OR NpwpPrshn = '$NpwpPrshn'";
						}

						$q="SELECT KdPrshn FROM mst_perusahaan WHERE NmPrshn='$NAMA_PEMASOK' $wh_Npwp ";
						$rsp = $this->db->query($q)->result_array();
						
						if ($rsp){
							$NmTuj=$rsp[0]['KdPrshn'];
						} 


						//MANIPULASI DATA UNTUK INCOMING HEADER					
						$currency=$KdVal;
						$supplier=$NmTuj;
						$matin_no = 'IMP'.$CAR;
						//Hapus Transaksi Incoming
						$q="DELETE FROM mat_inchdr_sub WHERE KdJnsDok='$DokKdBc' 
							AND matin_no = '$matin_no'
							AND matin_date = '$TgAju'
							AND NoDaf='$NoDaf' 
							AND TgDaf='$TgDaf'
							AND mat_type='1'";
						// die($q);
						$this->db->query($q);				

						$q = "INSERT INTO mat_inchdr_sub (
						matin_id,matin_type,matin_no,matin_date,mat_type,
						tot_weight,notes,KdJnsDok,supplier,currency,kurs,NoDaf,TgDaf,kd_cmpy
						) VALUES (
						'$autono','0','$matin_no','$TgAju','1',
						'$BRUTO','','$DokKdBc','$supplier','$currency','$NDPBM','$NoDaf','$TgDaf','9221'
						)";
						// echo $q."<br>";
						$this->db->query($q);


						//MANIPULASI DATA UNTUK LIST BARANG (TABEL barang)
						$sheetBar = $spreadsheet->setActiveSheetIndexByName('Barang');
						//dapatkan jml baris sheet
						$totRowExistBar = $sheetBar->getHighestRow();
						for ($rBar = 2; $rBar <= $totRowExistBar; $rBar++):
							$FCARBar = $sheetBar->getCell('A' . $rBar)->getValue();

							$no_urut_barang = $sheetBar->getCell('B' . $rBar)->getValue();
							$HE = $sheetBar->getCell('AK' . $rBar)->getValue();
							$KdBarang = $sheetBar->getCell('U' . $rBar)->getValue();
							$UrBarang= $sheetBar->getCell('AQ' . $rBar)->getValue();
							$Spek = $sheetBar->getCell('AM' . $rBar)->getValue();
							$Negara = $sheetBar->getCell('AA' . $rBar)->getValue();
							$BM2 = 0;
							$BMT = 0;
							$Cukai2 = 0;
							$PPN2 = 0;
							$PPnBM2 = 0;
							$PPh2 = 0;
							$qty = $sheetBar->getCell('Q' . $rBar)->getValue();
							$price = $sheetBar->getCell('K' . $rBar)->getValue();
							$JnsSatuan = $sheetBar->getCell('AB' . $rBar)->getValue();
							$NETTO2 = $sheetBar->getCell('AG' . $rBar)->getValue();
							$CIF2 = $sheetBar->getCell('D' . $rBar)->getValue();
							$HrgSerah2 = $sheetBar->getCell('L' . $rBar)->getValue();

							if ($FCARBar == $FCAR):
								//TAMBAH DETAIL
								$q = "INSERT INTO mat_incdet_sub (
										matin_id,child_no,mat_id,weight,qty,price
										) VALUES (
										'$autono','$no_urut_barang','$KdBarang','$NETTO2','$qty','$price'
										)";
								$this->db->query($q);
							endif;
						endfor;
						echo "$CAR $TgAju Berhasil<br>";
					endfor;
				} else {
					echo "Return Code: " . $_FILES["h"]["error"] . "<br />";
				}
			} else {
				}
		//akhir if post simpan
		}
	}

	public function bc262()
	{		
		$data = array(
			'title' 	=> 	$this->title,
			'content' 	=> 	'ceisa_tpb/bc262_frm',
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view('ceisa_tpb/_cjs');
		
	}
	
	function bc262_process(){
		if (isset($_REQUEST['simpan'])){
			//echo $_FILES["h"]["type"]."<br>";
			//echo $_FILES["h"]["name"];
			if (($_FILES["h"]["type"] == "application/vnd.ms-excel")){
				if ($_FILES["h"]["error"] < 1){
					//Jenis BC 262
					$DokKdBc=5;

					$inputFileType = IOFactory::identify($_FILES['h']['tmp_name']);
					$objReader = IOFactory::createReader($inputFileType);
					$spreadsheet = $objReader->load($_FILES['h']['tmp_name']);

					//Sheet Header
					$sheet = 0;
					$sheet = $spreadsheet->setActiveSheetIndex($sheet);

					//dapatkan jml baris sheet2
					$totRowExist = $sheet->getHighestRow();
					// echo $totRowExist;

					for ($row = 2; $row <= $totRowExist; $row++):
						$FCAR = $sheet->getCell('A' . $row)->getValue();

						$CAR = substr($FCAR, -6);
						$TgAju=substr($FCAR, 12, 4)."-".substr($FCAR, 16, 2)."-".substr($FCAR, 18, 2);
										
						$KdKpbcAsal = $sheet->getCell('B' . $row)->getValue();
						// $KdJnsTpbAsal = $sheet->getCell('BQ' . $row)->getValue();
						$KdJnsTpbTuj = $sheet->getCell('CI' . $row)->getValue();
						$KdJnsTpbAsal = $KdJnsTpbTuj;

						$KdJnsBarang = $sheet->getCell('BL' . $row)->getValue();
						if ($KdJnsBarang == ''){
							$KdJnsBarang = 1;
						}
						$KdTp = $sheet->getCell('CH' . $row)->getValue();
						if ($KdTp == ''){
							$KdTp = 3;
						}
						$NoDaf = $sheet->getCell('CZ' . $row)->getValue();
						//Tanggal Daftar
						$dMY = $sheet->getCell('DN' . $row)->getValue();
						$TgDaf = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgDaf = $date->format('Y-m-d');	
							}
						}

						//Tanggal TTD
						$dMY = $sheet->getCell('DT' . $row)->getValue();
						$TgTT = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgTT = $date->format('Y-m-d');	
							}
						}

						$KdKpBongkar = $sheet->getCell('BZ' . $row)->getValue();
						$KdKpbcTuj = $sheet->getCell('BS' . $row)->getValue();
						$KdKpPengawas='050500';
						$NpwpPrshn = $sheet->getCell('AI' . $row)->getValue();
						$NmPengusaha = $sheet->getCell('CR' . $row)->getValue();
						$NipPengusaha = $sheet->getCell('AM' . $row)->getValue();
						$NmPejabat='';
						$NipPejabat='';
						$ref_id='';
						$NmTuj='';
						$NmPpjk='';
						$KdTimbun='';
						$KdKemas = '';
						$JmlKemas = $sheet->getCell('AO' . $row)->getValue();
						$MerekKemas = '';
						$BRUTO=$sheet->getCell('T' . $row)->getValue();
						$NETTO=$sheet->getCell('CT' . $row)->getValue();
						$KdVal=$sheet->getCell('CK' . $row)->getValue();
						$CIF=$sheet->getCell('U' . $row)->getValue();
						$CIF_RUPIAH=$sheet->getCell('V' . $row)->getValue();
						$Total='';
						$TotalH='';
						$tgl=$TgDaf." ".date('H:i:s');
						$usr = 'SYSTEM';

						// $ID_HEADER=$r['ID'];
						$NAMA_PENGIRIM = $sheet->getCell('CP' . $row)->getValue();
						$NAMA_PEMASOK = $sheet->getCell('D' . $row)->getValue();		
						$NAMA_PENERIMA_BARANG = $sheet->getCell('CN' . $row)->getValue();
						$ALAMAT_PENGIRIM = $sheet->getCell('K' . $row)->getValue();				
						$ALAMAT_PEMASOK = $sheet->getCell('H' . $row)->getValue();
						$ALAMAT_PENERIMA_BARANG = $sheet->getCell('J' . $row)->getValue(); 
						$HARGA_PENYERAHAN = $sheet->getCell('AD' . $row)->getValue();
						$HrgSerah=$HARGA_PENYERAHAN;

						$q="SELECT MAX(matin_id) AS auto_no FROM mat_inchdr_sub LIMIT 1";
						$rs = $this->db->query($q)->result_array();
						
						$autono=($rs)?$rs[0]['auto_no']+1:1;
						$ref_id=$autono;

						$NAMA_PEMASOK = $NAMA_PEMASOK;

						$wh_Npwp = "";
						if ($NpwpPrshn != ""){
							$wh_Npwp = " OR NpwpPrshn = '$NpwpPrshn'";
						}

						$q="SELECT KdPrshn FROM mst_perusahaan WHERE NmPrshn='$NAMA_PEMASOK' $wh_Npwp ";
						$rsp = $this->db->query($q)->result_array();
						
						if ($rsp){
							$NmTuj=$rsp[0]['KdPrshn'];
						} 


						//MANIPULASI DATA UNTUK INCOMING HEADER					
						$currency=$KdVal;
						$supplier=$NmTuj;
						$matin_no = 'IMP'.$CAR;
						//Hapus Transaksi Incoming
						$q="DELETE FROM mat_inchdr_sub WHERE KdJnsDok='$DokKdBc' 
							AND matin_no = '$matin_no'
							AND matin_date = '$TgAju'
							AND NoDaf='$NoDaf' 
							AND TgDaf='$TgDaf'
							AND mat_type='1'";
						// die($q);
						$this->db->query($q);				

						$q = "INSERT INTO mat_inchdr_sub (
						matin_id,matin_type,matin_no,matin_date,mat_type,
						tot_weight,notes,KdJnsDok,supplier,currency,kurs,NoDaf,TgDaf,kd_cmpy
						) VALUES (
						'$autono','0','$matin_no','$TgAju','1',
						'$BRUTO','','$DokKdBc','$supplier','$currency','$NDPBM','$NoDaf','$TgDaf','9221'
						)";
						// echo $q."<br>";
						$this->db->query($q);


						//MANIPULASI DATA UNTUK LIST BARANG (TABEL barang)
						$sheetBar = $spreadsheet->setActiveSheetIndexByName('Barang');
						//dapatkan jml baris sheet
						$totRowExistBar = $sheetBar->getHighestRow();
						for ($rBar = 2; $rBar <= $totRowExistBar; $rBar++):
							$FCARBar = $sheetBar->getCell('A' . $rBar)->getValue();

							$no_urut_barang = $sheetBar->getCell('B' . $rBar)->getValue();
							$HE = $sheetBar->getCell('AK' . $rBar)->getValue();
							$KdBarang = $sheetBar->getCell('U' . $rBar)->getValue();
							$UrBarang= $sheetBar->getCell('AQ' . $rBar)->getValue();
							$Spek = $sheetBar->getCell('AM' . $rBar)->getValue();
							$Negara = $sheetBar->getCell('AA' . $rBar)->getValue();
							$BM2 = 0;
							$BMT = 0;
							$Cukai2 = 0;
							$PPN2 = 0;
							$PPnBM2 = 0;
							$PPh2 = 0;
							$qty = $sheetBar->getCell('Q' . $rBar)->getValue();
							$price = $sheetBar->getCell('K' . $rBar)->getValue();
							$JnsSatuan = $sheetBar->getCell('AB' . $rBar)->getValue();
							$NETTO2 = $sheetBar->getCell('AG' . $rBar)->getValue();
							$CIF2 = $sheetBar->getCell('D' . $rBar)->getValue();
							$HrgSerah2 = $sheetBar->getCell('L' . $rBar)->getValue();

							if ($FCARBar == $FCAR):
								//TAMBAH DETAIL
								$q = "INSERT INTO mat_incdet_sub (
										matin_id,child_no,mat_id,weight,qty,price
										) VALUES (
										'$autono','$no_urut_barang','$KdBarang','$NETTO2','$qty','$price'
										)";
								$this->db->query($q);
							endif;
						endfor;
						echo "$CAR $TgAju Berhasil<br>";
					endfor;
				} else {
					echo "Return Code: " . $_FILES["h"]["error"] . "<br />";
				}
			} else {
				}
		//akhir if post simpan
		}
	}

	public function bc27i()
	{		
		$data = array(
			'title' 	=> 	$this->title,
			'content' 	=> 	'ceisa_tpb/bc27i_frm',
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view('ceisa_tpb/_cjs');
		
	}
	
	function bc27i_process(){
		if (isset($_REQUEST['simpan'])){
			//echo $_FILES["h"]["type"]."<br>";
			//echo $_FILES["h"]["name"];
			if (($_FILES["h"]["type"] == "application/vnd.ms-excel")){
				if ($_FILES["h"]["error"] < 1){
					//Jenis BC 27 In
					$DokKdBc=6;

					$inputFileType = IOFactory::identify($_FILES['h']['tmp_name']);
					$objReader = IOFactory::createReader($inputFileType);
					$spreadsheet = $objReader->load($_FILES['h']['tmp_name']);

					//Sheet Header
					$sheet = 0;
					$sheet = $spreadsheet->setActiveSheetIndex($sheet);

					//dapatkan jml baris sheet2
					$totRowExist = $sheet->getHighestRow();
					// echo $totRowExist;

					for ($row = 2; $row <= $totRowExist; $row++):
						$FCAR = $sheet->getCell('A' . $row)->getValue();

						$CAR = substr($FCAR, -6);
						$TgAju=substr($FCAR, 12, 4)."-".substr($FCAR, 16, 2)."-".substr($FCAR, 18, 2);
										
						$KdKpbcAsal = $sheet->getCell('B' . $row)->getValue();
						// $KdJnsTpbAsal = $sheet->getCell('BQ' . $row)->getValue();
						$KdJnsTpbTuj = $sheet->getCell('CI' . $row)->getValue();
						$KdJnsTpbAsal = $KdJnsTpbTuj;

						$KdJnsBarang = $sheet->getCell('BL' . $row)->getValue();
						if ($KdJnsBarang == ''){
							$KdJnsBarang = 1;
						}
						$KdTp = $sheet->getCell('CH' . $row)->getValue();
						if ($KdTp == ''){
							$KdTp = 3;
						}
						$NoDaf = $sheet->getCell('CZ' . $row)->getValue();
						//Tanggal Daftar
						$dMY = $sheet->getCell('DN' . $row)->getValue();
						$TgDaf = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgDaf = $date->format('Y-m-d');	
							}
						}

						//Tanggal TTD
						$dMY = $sheet->getCell('DT' . $row)->getValue();
						$TgTT = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgTT = $date->format('Y-m-d');	
							}
						}

						$KdKpBongkar = $sheet->getCell('BZ' . $row)->getValue();
						$KdKpbcTuj = $sheet->getCell('BS' . $row)->getValue();
						$KdKpPengawas='050500';
						$NpwpPrshn = $sheet->getCell('AI' . $row)->getValue();
						$NmPengusaha = $sheet->getCell('CR' . $row)->getValue();
						$NipPengusaha = $sheet->getCell('AM' . $row)->getValue();
						$NmPejabat='';
						$NipPejabat='';
						$ref_id='';
						$NmTuj='';
						$NmPpjk='';
						$KdTimbun='';
						$KdKemas = '';
						$JmlKemas = $sheet->getCell('AO' . $row)->getValue();
						$MerekKemas = '';
						$BRUTO=$sheet->getCell('T' . $row)->getValue();
						$NETTO=$sheet->getCell('CT' . $row)->getValue();
						$KdVal=$sheet->getCell('CK' . $row)->getValue();
						$CIF=$sheet->getCell('U' . $row)->getValue();
						$CIF_RUPIAH=$sheet->getCell('V' . $row)->getValue();
						$Total='';
						$TotalH='';
						$tgl=$TgDaf." ".date('H:i:s');
						$usr = 'SYSTEM';

						// $ID_HEADER=$r['ID'];
						$NAMA_PENGIRIM = $sheet->getCell('CP' . $row)->getValue();
						$NAMA_PEMASOK = $sheet->getCell('D' . $row)->getValue();		
						$NAMA_PENERIMA_BARANG = $sheet->getCell('CN' . $row)->getValue();
						$ALAMAT_PENGIRIM = $sheet->getCell('K' . $row)->getValue();				
						$ALAMAT_PEMASOK = $sheet->getCell('H' . $row)->getValue();
						$ALAMAT_PENERIMA_BARANG = $sheet->getCell('J' . $row)->getValue(); 
						$HARGA_PENYERAHAN = $sheet->getCell('AD' . $row)->getValue();
						$HrgSerah=$HARGA_PENYERAHAN;

						$q="SELECT MAX(matin_id) AS auto_no FROM mat_inchdr_sub LIMIT 1";
						$rs = $this->db->query($q)->result_array();
						
						$autono=($rs)?$rs[0]['auto_no']+1:1;
						$ref_id=$autono;

						$NAMA_PEMASOK = $NAMA_PEMASOK;

						$wh_Npwp = "";
						if ($NpwpPrshn != ""){
							$wh_Npwp = " OR NpwpPrshn = '$NpwpPrshn'";
						}

						$q="SELECT KdPrshn FROM mst_perusahaan WHERE NmPrshn='$NAMA_PEMASOK' $wh_Npwp ";
						$rsp = $this->db->query($q)->result_array();
						
						if ($rsp){
							$NmTuj=$rsp[0]['KdPrshn'];
						} 


						//MANIPULASI DATA UNTUK INCOMING HEADER					
						$currency=$KdVal;
						$supplier=$NmTuj;
						$matin_no = 'IMP'.$CAR;
						//Hapus Transaksi Incoming
						$q="DELETE FROM mat_inchdr_sub WHERE KdJnsDok='$DokKdBc' 
							AND matin_no = '$matin_no'
							AND matin_date = '$TgAju'
							AND NoDaf='$NoDaf' 
							AND TgDaf='$TgDaf'
							AND mat_type='1'";
						// die($q);
						$this->db->query($q);				

						$q = "INSERT INTO mat_inchdr_sub (
						matin_id,matin_type,matin_no,matin_date,mat_type,
						tot_weight,notes,KdJnsDok,supplier,currency,kurs,NoDaf,TgDaf,kd_cmpy
						) VALUES (
						'$autono','0','$matin_no','$TgAju','1',
						'$BRUTO','','$DokKdBc','$supplier','$currency','$NDPBM','$NoDaf','$TgDaf','9221'
						)";
						// echo $q."<br>";
						$this->db->query($q);


						//MANIPULASI DATA UNTUK LIST BARANG (TABEL barang)
						$sheetBar = $spreadsheet->setActiveSheetIndexByName('Barang');
						//dapatkan jml baris sheet
						$totRowExistBar = $sheetBar->getHighestRow();
						for ($rBar = 2; $rBar <= $totRowExistBar; $rBar++):
							$FCARBar = $sheetBar->getCell('A' . $rBar)->getValue();

							$no_urut_barang = $sheetBar->getCell('B' . $rBar)->getValue();
							$HE = $sheetBar->getCell('AK' . $rBar)->getValue();
							$KdBarang = $sheetBar->getCell('U' . $rBar)->getValue();
							$UrBarang= $sheetBar->getCell('AQ' . $rBar)->getValue();
							$Spek = $sheetBar->getCell('AM' . $rBar)->getValue();
							$Negara = $sheetBar->getCell('AA' . $rBar)->getValue();
							$BM2 = 0;
							$BMT = 0;
							$Cukai2 = 0;
							$PPN2 = 0;
							$PPnBM2 = 0;
							$PPh2 = 0;
							$qty = $sheetBar->getCell('Q' . $rBar)->getValue();
							$price = $sheetBar->getCell('K' . $rBar)->getValue();
							$JnsSatuan = $sheetBar->getCell('AB' . $rBar)->getValue();
							$NETTO2 = $sheetBar->getCell('AG' . $rBar)->getValue();
							$CIF2 = $sheetBar->getCell('D' . $rBar)->getValue();
							$HrgSerah2 = $sheetBar->getCell('L' . $rBar)->getValue();

							if ($FCARBar == $FCAR):
								//TAMBAH DETAIL
								$q = "INSERT INTO mat_incdet_sub (
										matin_id,child_no,mat_id,weight,qty,price
										) VALUES (
										'$autono','$no_urut_barang','$KdBarang','$NETTO2','$qty','$price'
										)";
								$this->db->query($q);
							endif;
						endfor;
						echo "$CAR $TgAju Berhasil<br>";
					endfor;
				} else {
					echo "Return Code: " . $_FILES["h"]["error"] . "<br />";
				}
			} else {
				}
		//akhir if post simpan
		}
	}

	public function bc40()
	{		
		$data = array(
			'title' 	=> 	$this->title,
			'content' 	=> 	'ceisa_tpb/bc40_frm',
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view('ceisa_tpb/_cjs');
		
	}
	
	function bc40_process(){
		if (isset($_REQUEST['simpan'])){
			//echo $_FILES["h"]["type"]."<br>";
			//echo $_FILES["h"]["name"];
			if (($_FILES["h"]["type"] == "application/vnd.ms-excel")){
				if ($_FILES["h"]["error"] < 1){
					//Jenis BC 40
					$DokKdBc=8;

					$inputFileType = IOFactory::identify($_FILES['h']['tmp_name']);
					$objReader = IOFactory::createReader($inputFileType);
					$spreadsheet = $objReader->load($_FILES['h']['tmp_name']);

					//Sheet Header
					$sheet = 0;
					$sheet = $spreadsheet->setActiveSheetIndex($sheet);

					//dapatkan jml baris sheet2
					$totRowExist = $sheet->getHighestRow();
					// echo $totRowExist;

					for ($row = 2; $row <= $totRowExist; $row++):
						$FCAR = $sheet->getCell('A' . $row)->getValue();

						$CAR = substr($FCAR, -6);
						$TgAju=substr($FCAR, 12, 4)."-".substr($FCAR, 16, 2)."-".substr($FCAR, 18, 2);
										
						$KdKpbcAsal = $sheet->getCell('B' . $row)->getValue();
						// $KdJnsTpbAsal = $sheet->getCell('BQ' . $row)->getValue();
						$KdJnsTpbTuj = $sheet->getCell('CI' . $row)->getValue();
						$KdJnsTpbAsal = $KdJnsTpbTuj;

						$KdJnsBarang = $sheet->getCell('BL' . $row)->getValue();
						if ($KdJnsBarang == ''){
							$KdJnsBarang = 1;
						}
						$KdTp = $sheet->getCell('CH' . $row)->getValue();
						if ($KdTp == ''){
							$KdTp = 3;
						}
						$NoDaf = $sheet->getCell('CZ' . $row)->getValue();
						//Tanggal Daftar
						$dMY = $sheet->getCell('DN' . $row)->getValue();
						$TgDaf = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgDaf = $date->format('Y-m-d');	
							}
						}

						//Tanggal TTD
						$dMY = $sheet->getCell('DT' . $row)->getValue();
						$TgTT = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgTT = $date->format('Y-m-d');	
							}
						}

						$KdKpBongkar = $sheet->getCell('BZ' . $row)->getValue();
						$KdKpbcTuj = $sheet->getCell('BS' . $row)->getValue();
						$KdKpPengawas='050500';
						$NpwpPrshn = $sheet->getCell('AI' . $row)->getValue();
						$NmPengusaha = $sheet->getCell('CR' . $row)->getValue();
						$NipPengusaha = $sheet->getCell('AM' . $row)->getValue();
						$NmPejabat='';
						$NipPejabat='';
						$ref_id='';
						$NmTuj='';
						$NmPpjk='';
						$KdTimbun='';
						$KdKemas = '';
						$JmlKemas = $sheet->getCell('AO' . $row)->getValue();
						$MerekKemas = '';
						$BRUTO=$sheet->getCell('T' . $row)->getValue();
						$NETTO=$sheet->getCell('CT' . $row)->getValue();
						$KdVal=$sheet->getCell('CK' . $row)->getValue();
						$CIF=$sheet->getCell('U' . $row)->getValue();
						$CIF_RUPIAH=$sheet->getCell('V' . $row)->getValue();
						$Total='';
						$TotalH='';
						$tgl=$TgDaf." ".date('H:i:s');
						$usr = 'SYSTEM';

						// $ID_HEADER=$r['ID'];
						$NAMA_PENGIRIM = $sheet->getCell('CP' . $row)->getValue();
						$NAMA_PEMASOK = $sheet->getCell('D' . $row)->getValue();		
						$NAMA_PENERIMA_BARANG = $sheet->getCell('CN' . $row)->getValue();
						$ALAMAT_PENGIRIM = $sheet->getCell('K' . $row)->getValue();				
						$ALAMAT_PEMASOK = $sheet->getCell('H' . $row)->getValue();
						$ALAMAT_PENERIMA_BARANG = $sheet->getCell('J' . $row)->getValue(); 
						$HARGA_PENYERAHAN = $sheet->getCell('AD' . $row)->getValue();
						$HrgSerah=$HARGA_PENYERAHAN;

						$q="SELECT MAX(matin_id) AS auto_no FROM mat_inchdr_sub LIMIT 1";
						$rs = $this->db->query($q)->result_array();
						
						$autono=($rs)?$rs[0]['auto_no']+1:1;
						$ref_id=$autono;

						$NAMA_PEMASOK = $NAMA_PEMASOK;

						$wh_Npwp = "";
						if ($NpwpPrshn != ""){
							$wh_Npwp = " OR NpwpPrshn = '$NpwpPrshn'";
						}

						$q="SELECT KdPrshn FROM mst_perusahaan WHERE NmPrshn='$NAMA_PEMASOK' $wh_Npwp ";
						$rsp = $this->db->query($q)->result_array();
						
						if ($rsp){
							$NmTuj=$rsp[0]['KdPrshn'];
						} 


						//MANIPULASI DATA UNTUK INCOMING HEADER					
						$currency=$KdVal;
						$supplier=$NmTuj;
						$matin_no = 'IMP'.$CAR;
						//Hapus Transaksi Incoming
						$q="DELETE FROM mat_inchdr_sub WHERE KdJnsDok='$DokKdBc' 
							AND matin_no = '$matin_no'
							AND matin_date = '$TgAju'
							AND NoDaf='$NoDaf' 
							AND TgDaf='$TgDaf'
							AND mat_type='1'";
						// die($q);
						$this->db->query($q);				

						$q = "INSERT INTO mat_inchdr_sub (
						matin_id,matin_type,matin_no,matin_date,mat_type,
						tot_weight,notes,KdJnsDok,supplier,currency,kurs,NoDaf,TgDaf,kd_cmpy
						) VALUES (
						'$autono','0','$matin_no','$TgAju','1',
						'$BRUTO','','$DokKdBc','$supplier','$currency','$NDPBM','$NoDaf','$TgDaf','9221'
						)";
						// echo $q."<br>";
						$this->db->query($q);


						//MANIPULASI DATA UNTUK LIST BARANG (TABEL barang)
						$sheetBar = $spreadsheet->setActiveSheetIndexByName('Barang');
						//dapatkan jml baris sheet
						$totRowExistBar = $sheetBar->getHighestRow();
						for ($rBar = 2; $rBar <= $totRowExistBar; $rBar++):
							$FCARBar = $sheetBar->getCell('A' . $rBar)->getValue();

							$no_urut_barang = $sheetBar->getCell('B' . $rBar)->getValue();
							$HE = $sheetBar->getCell('AK' . $rBar)->getValue();
							$KdBarang = $sheetBar->getCell('U' . $rBar)->getValue();
							$UrBarang= $sheetBar->getCell('AQ' . $rBar)->getValue();
							$Spek = $sheetBar->getCell('AM' . $rBar)->getValue();
							$Negara = $sheetBar->getCell('AA' . $rBar)->getValue();
							$BM2 = 0;
							$BMT = 0;
							$Cukai2 = 0;
							$PPN2 = 0;
							$PPnBM2 = 0;
							$PPh2 = 0;
							$qty = $sheetBar->getCell('Q' . $rBar)->getValue();
							$price = $sheetBar->getCell('K' . $rBar)->getValue();
							$JnsSatuan = $sheetBar->getCell('AB' . $rBar)->getValue();
							$NETTO2 = $sheetBar->getCell('AG' . $rBar)->getValue();
							$CIF2 = $sheetBar->getCell('D' . $rBar)->getValue();
							$HrgSerah2 = $sheetBar->getCell('L' . $rBar)->getValue();

							if ($FCARBar == $FCAR):
								//TAMBAH DETAIL
								$q = "INSERT INTO mat_incdet_sub (
										matin_id,child_no,mat_id,weight,qty,price
										) VALUES (
										'$autono','$no_urut_barang','$KdBarang','$NETTO2','$qty','$price'
										)";
								$this->db->query($q);
							endif;
						endfor;
						echo "$CAR $TgAju Berhasil<br>";
					endfor;
				} else {
					echo "Return Code: " . $_FILES["h"]["error"] . "<br />";
				}
			} else {
				}
		//akhir if post simpan
		}
	}
	
	public function bc25()
	{		
		$data = array(
			'title' 	=> 	$this->title,
			'content' 	=> 	'ceisa_tpb/bc25_frm',
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view('ceisa_tpb/_cjs');
		
	}
	
	function bc25_process(){
		if (isset($_REQUEST['simpan'])){
			//echo $_FILES["h"]["type"]."<br>";
			//echo $_FILES["h"]["name"];
			if (($_FILES["h"]["type"] == "application/vnd.ms-excel")){
				if ($_FILES["h"]["error"] < 1){
					//Jenis BC 25
					$DokKdBc=3;

					$inputFileType = IOFactory::identify($_FILES['h']['tmp_name']);
					$objReader = IOFactory::createReader($inputFileType);
					$spreadsheet = $objReader->load($_FILES['h']['tmp_name']);

					//Sheet Header
					$sheet = 0;
					$sheet = $spreadsheet->setActiveSheetIndex($sheet);

					//dapatkan jml baris sheet2
					$totRowExist = $sheet->getHighestRow();
					// echo $totRowExist;

					for ($row = 2; $row <= $totRowExist; $row++):
						$FCAR = $sheet->getCell('A' . $row)->getValue();

						$CAR = substr($FCAR, -6);
						$TgAju=substr($FCAR, 12, 4)."-".substr($FCAR, 16, 2)."-".substr($FCAR, 18, 2);
										
						$KdKpbcAsal = $sheet->getCell('B' . $row)->getValue();
						// $KdJnsTpbAsal = $sheet->getCell('BQ' . $row)->getValue();
						$KdJnsTpbTuj = $sheet->getCell('CI' . $row)->getValue();
						$KdJnsTpbAsal = $KdJnsTpbTuj;

						$KdJnsBarang = $sheet->getCell('BL' . $row)->getValue();
						if ($KdJnsBarang == ''){
							$KdJnsBarang = 1;
						}
						$KdTp = $sheet->getCell('CH' . $row)->getValue();
						if ($KdTp == ''){
							$KdTp = 3;
						}
						$NoDaf = $sheet->getCell('CZ' . $row)->getValue();
						//Tanggal Daftar
						$dMY = $sheet->getCell('DN' . $row)->getValue();
						$TgDaf = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgDaf = $date->format('Y-m-d');	
							}
						}

						//Tanggal TTD
						$dMY = $sheet->getCell('DT' . $row)->getValue();
						$TgTT = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgTT = $date->format('Y-m-d');	
							}
						}

						$KdKpBongkar = $sheet->getCell('BZ' . $row)->getValue();
						$KdKpbcTuj = $sheet->getCell('BS' . $row)->getValue();
						$KdKpPengawas='050500';
						$NpwpPrshn = $sheet->getCell('AI' . $row)->getValue();
						$NmPengusaha = $sheet->getCell('CR' . $row)->getValue();
						$NipPengusaha = $sheet->getCell('AM' . $row)->getValue();
						$NmPejabat='';
						$NipPejabat='';
						$ref_id='';
						$NmTuj='';
						$NmPpjk='';
						$KdTimbun='';
						$KdKemas = '';
						$JmlKemas = $sheet->getCell('AO' . $row)->getValue();
						$MerekKemas = '';
						$BRUTO=$sheet->getCell('T' . $row)->getValue();
						$NETTO=$sheet->getCell('CT' . $row)->getValue();
						$KdVal=$sheet->getCell('CK' . $row)->getValue();
						$CIF=$sheet->getCell('U' . $row)->getValue();
						$CIF_RUPIAH=$sheet->getCell('V' . $row)->getValue();
						$Total='';
						$TotalH='';
						$tgl=$TgDaf." ".date('H:i:s');
						$usr = 'SYSTEM';

						// $ID_HEADER=$r['ID'];
						$NAMA_PENGIRIM = $sheet->getCell('CP' . $row)->getValue();
						$NAMA_PEMASOK = $sheet->getCell('D' . $row)->getValue();		
						$NAMA_PENERIMA_BARANG = $sheet->getCell('CN' . $row)->getValue();
						$ALAMAT_PENGIRIM = $sheet->getCell('K' . $row)->getValue();				
						$ALAMAT_PEMASOK = $sheet->getCell('H' . $row)->getValue();
						$ALAMAT_PENERIMA_BARANG = $sheet->getCell('J' . $row)->getValue(); 
						$HARGA_PENYERAHAN = $sheet->getCell('AD' . $row)->getValue();
						$HrgSerah=$HARGA_PENYERAHAN;

						$q="SELECT MAX(matout_id) AS auto_no FROM mat_outhdr_sub LIMIT 1";
						$rs = $this->db->query($q)->result_array();
						
						$autono=($rs)?$rs[0]['auto_no']+1:1;
						$ref_id=$autono;

						$NAMA_PEMASOK = $NAMA_PEMASOK;

						$wh_Npwp = "";
						if ($NpwpPrshn != ""){
							$wh_Npwp = " OR NpwpPrshn = '$NpwpPrshn'";
						}

						$q="SELECT KdPrshn FROM mst_perusahaan WHERE NmPrshn='$NAMA_PEMASOK' $wh_Npwp ";
						$rsp = $this->db->query($q)->result_array();
						
						if ($rsp){
							$NmTuj=$rsp[0]['KdPrshn'];
						} 


						//MANIPULASI DATA UNTUK INCOMING HEADER					
						$currency=$KdVal;
						$supplier=$NmTuj;
						$matin_no = 'IMP'.$CAR;
						//Hapus Transaksi Incoming
						$q="DELETE FROM mat_outhdr_sub WHERE KdJnsDok='$DokKdBc' 
							AND matin_no = '$matin_no'
							AND matin_date = '$TgAju'
							AND NoDaf='$NoDaf' 
							AND TgDaf='$TgDaf'
							AND mat_type='1'";
						// die($q);
						$this->db->query($q);				

						$q = "INSERT INTO mat_outhdr_sub (
						matout_id,matin_type,matin_no,matin_date,mat_type,
						tot_weight,notes,KdJnsDok,supplier,currency,kurs,NoDaf,TgDaf,kd_cmpy
						) VALUES (
						'$autono','0','$matin_no','$TgAju','1',
						'$BRUTO','','$DokKdBc','$supplier','$currency','$NDPBM','$NoDaf','$TgDaf','9221'
						)";
						// echo $q."<br>";
						$this->db->query($q);


						//MANIPULASI DATA UNTUK LIST BARANG (TABEL barang)
						$sheetBar = $spreadsheet->setActiveSheetIndexByName('Barang');
						//dapatkan jml baris sheet
						$totRowExistBar = $sheetBar->getHighestRow();
						for ($rBar = 2; $rBar <= $totRowExistBar; $rBar++):
							$FCARBar = $sheetBar->getCell('A' . $rBar)->getValue();

							$no_urut_barang = $sheetBar->getCell('B' . $rBar)->getValue();
							$HE = $sheetBar->getCell('AK' . $rBar)->getValue();
							$KdBarang = $sheetBar->getCell('U' . $rBar)->getValue();
							$UrBarang= $sheetBar->getCell('AQ' . $rBar)->getValue();
							$Spek = $sheetBar->getCell('AM' . $rBar)->getValue();
							$Negara = $sheetBar->getCell('AA' . $rBar)->getValue();
							$BM2 = 0;
							$BMT = 0;
							$Cukai2 = 0;
							$PPN2 = 0;
							$PPnBM2 = 0;
							$PPh2 = 0;
							$qty = $sheetBar->getCell('Q' . $rBar)->getValue();
							$price = $sheetBar->getCell('K' . $rBar)->getValue();
							$JnsSatuan = $sheetBar->getCell('AB' . $rBar)->getValue();
							$NETTO2 = $sheetBar->getCell('AG' . $rBar)->getValue();
							$CIF2 = $sheetBar->getCell('D' . $rBar)->getValue();
							$HrgSerah2 = $sheetBar->getCell('L' . $rBar)->getValue();

							if ($FCARBar == $FCAR):
								//TAMBAH DETAIL
								$q = "INSERT INTO mat_incdet_sub (
										matout_id,child_no,mat_id,weight,qty,price
										) VALUES (
										'$autono','$no_urut_barang','$KdBarang','$NETTO2','$qty','$price'
										)";
								$this->db->query($q);
							endif;
						endfor;
						echo "$CAR $TgAju Berhasil<br>";
					endfor;
				} else {
					echo "Return Code: " . $_FILES["h"]["error"] . "<br />";
				}
			} else {
				}
		//akhir if post simpan
		}
	}

	public function bc261()
	{		
		$data = array(
			'title' 	=> 	$this->title,
			'content' 	=> 	'ceisa_tpb/bc261_frm',
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view('ceisa_tpb/_cjs');
		
	}
	
	function bc261_process(){
		if (isset($_REQUEST['simpan'])){
			//echo $_FILES["h"]["type"]."<br>";
			//echo $_FILES["h"]["name"];
			if (($_FILES["h"]["type"] == "application/vnd.ms-excel")){
				if ($_FILES["h"]["error"] < 1){
					//Jenis BC 261
					$DokKdBc=4;

					$inputFileType = IOFactory::identify($_FILES['h']['tmp_name']);
					$objReader = IOFactory::createReader($inputFileType);
					$spreadsheet = $objReader->load($_FILES['h']['tmp_name']);

					//Sheet Header
					$sheet = 0;
					$sheet = $spreadsheet->setActiveSheetIndex($sheet);

					//dapatkan jml baris sheet2
					$totRowExist = $sheet->getHighestRow();
					// echo $totRowExist;

					for ($row = 2; $row <= $totRowExist; $row++):
						$FCAR = $sheet->getCell('A' . $row)->getValue();

						$CAR = substr($FCAR, -6);
						$TgAju=substr($FCAR, 12, 4)."-".substr($FCAR, 16, 2)."-".substr($FCAR, 18, 2);
										
						$KdKpbcAsal = $sheet->getCell('B' . $row)->getValue();
						// $KdJnsTpbAsal = $sheet->getCell('BQ' . $row)->getValue();
						$KdJnsTpbTuj = $sheet->getCell('CI' . $row)->getValue();
						$KdJnsTpbAsal = $KdJnsTpbTuj;

						$KdJnsBarang = $sheet->getCell('BL' . $row)->getValue();
						if ($KdJnsBarang == ''){
							$KdJnsBarang = 1;
						}
						$KdTp = $sheet->getCell('CH' . $row)->getValue();
						if ($KdTp == ''){
							$KdTp = 3;
						}
						$NoDaf = $sheet->getCell('CZ' . $row)->getValue();
						//Tanggal Daftar
						$dMY = $sheet->getCell('DN' . $row)->getValue();
						$TgDaf = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgDaf = $date->format('Y-m-d');	
							}
						}

						//Tanggal TTD
						$dMY = $sheet->getCell('DT' . $row)->getValue();
						$TgTT = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgTT = $date->format('Y-m-d');	
							}
						}

						$KdKpBongkar = $sheet->getCell('BZ' . $row)->getValue();
						$KdKpbcTuj = $sheet->getCell('BS' . $row)->getValue();
						$KdKpPengawas='050500';
						$NpwpPrshn = $sheet->getCell('AI' . $row)->getValue();
						$NmPengusaha = $sheet->getCell('CR' . $row)->getValue();
						$NipPengusaha = $sheet->getCell('AM' . $row)->getValue();
						$NmPejabat='';
						$NipPejabat='';
						$ref_id='';
						$NmTuj='';
						$NmPpjk='';
						$KdTimbun='';
						$KdKemas = '';
						$JmlKemas = $sheet->getCell('AO' . $row)->getValue();
						$MerekKemas = '';
						$BRUTO=$sheet->getCell('T' . $row)->getValue();
						$NETTO=$sheet->getCell('CT' . $row)->getValue();
						$KdVal=$sheet->getCell('CK' . $row)->getValue();
						$CIF=$sheet->getCell('U' . $row)->getValue();
						$CIF_RUPIAH=$sheet->getCell('V' . $row)->getValue();
						$Total='';
						$TotalH='';
						$tgl=$TgDaf." ".date('H:i:s');
						$usr = 'SYSTEM';

						// $ID_HEADER=$r['ID'];
						$NAMA_PENGIRIM = $sheet->getCell('CP' . $row)->getValue();
						$NAMA_PEMASOK = $sheet->getCell('D' . $row)->getValue();		
						$NAMA_PENERIMA_BARANG = $sheet->getCell('CN' . $row)->getValue();
						$ALAMAT_PENGIRIM = $sheet->getCell('K' . $row)->getValue();				
						$ALAMAT_PEMASOK = $sheet->getCell('H' . $row)->getValue();
						$ALAMAT_PENERIMA_BARANG = $sheet->getCell('J' . $row)->getValue(); 
						$HARGA_PENYERAHAN = $sheet->getCell('AD' . $row)->getValue();
						$HrgSerah=$HARGA_PENYERAHAN;

						$q="SELECT MAX(matout_id) AS auto_no FROM mat_outhdr_sub LIMIT 1";
						$rs = $this->db->query($q)->result_array();
						
						$autono=($rs)?$rs[0]['auto_no']+1:1;
						$ref_id=$autono;

						$NAMA_PEMASOK = $NAMA_PEMASOK;

						$wh_Npwp = "";
						if ($NpwpPrshn != ""){
							$wh_Npwp = " OR NpwpPrshn = '$NpwpPrshn'";
						}

						$q="SELECT KdPrshn FROM mst_perusahaan WHERE NmPrshn='$NAMA_PEMASOK' $wh_Npwp ";
						$rsp = $this->db->query($q)->result_array();
						
						if ($rsp){
							$NmTuj=$rsp[0]['KdPrshn'];
						} 


						//MANIPULASI DATA UNTUK INCOMING HEADER					
						$currency=$KdVal;
						$supplier=$NmTuj;
						$matin_no = 'IMP'.$CAR;
						//Hapus Transaksi Incoming
						$q="DELETE FROM mat_outhdr_sub WHERE KdJnsDok='$DokKdBc' 
							AND matin_no = '$matin_no'
							AND matin_date = '$TgAju'
							AND NoDaf='$NoDaf' 
							AND TgDaf='$TgDaf'
							AND mat_type='1'";
						// die($q);
						$this->db->query($q);				

						$q = "INSERT INTO mat_outhdr_sub (
						matout_id,matin_type,matin_no,matin_date,mat_type,
						tot_weight,notes,KdJnsDok,supplier,currency,kurs,NoDaf,TgDaf,kd_cmpy
						) VALUES (
						'$autono','0','$matin_no','$TgAju','1',
						'$BRUTO','','$DokKdBc','$supplier','$currency','$NDPBM','$NoDaf','$TgDaf','9221'
						)";
						// echo $q."<br>";
						$this->db->query($q);


						//MANIPULASI DATA UNTUK LIST BARANG (TABEL barang)
						$sheetBar = $spreadsheet->setActiveSheetIndexByName('Barang');
						//dapatkan jml baris sheet
						$totRowExistBar = $sheetBar->getHighestRow();
						for ($rBar = 2; $rBar <= $totRowExistBar; $rBar++):
							$FCARBar = $sheetBar->getCell('A' . $rBar)->getValue();

							$no_urut_barang = $sheetBar->getCell('B' . $rBar)->getValue();
							$HE = $sheetBar->getCell('AK' . $rBar)->getValue();
							$KdBarang = $sheetBar->getCell('U' . $rBar)->getValue();
							$UrBarang= $sheetBar->getCell('AQ' . $rBar)->getValue();
							$Spek = $sheetBar->getCell('AM' . $rBar)->getValue();
							$Negara = $sheetBar->getCell('AA' . $rBar)->getValue();
							$BM2 = 0;
							$BMT = 0;
							$Cukai2 = 0;
							$PPN2 = 0;
							$PPnBM2 = 0;
							$PPh2 = 0;
							$qty = $sheetBar->getCell('Q' . $rBar)->getValue();
							$price = $sheetBar->getCell('K' . $rBar)->getValue();
							$JnsSatuan = $sheetBar->getCell('AB' . $rBar)->getValue();
							$NETTO2 = $sheetBar->getCell('AG' . $rBar)->getValue();
							$CIF2 = $sheetBar->getCell('D' . $rBar)->getValue();
							$HrgSerah2 = $sheetBar->getCell('L' . $rBar)->getValue();

							if ($FCARBar == $FCAR):
								//TAMBAH DETAIL
								$q = "INSERT INTO mat_incdet_sub (
										matout_id,child_no,mat_id,weight,qty,price
										) VALUES (
										'$autono','$no_urut_barang','$KdBarang','$NETTO2','$qty','$price'
										)";
								$this->db->query($q);
							endif;
						endfor;
						echo "$CAR $TgAju Berhasil<br>";
					endfor;
				} else {
					echo "Return Code: " . $_FILES["h"]["error"] . "<br />";
				}
			} else {
				}
		//akhir if post simpan
		}
	}

	public function bc27o()
	{		
		$data = array(
			'title' 	=> 	$this->title,
			'content' 	=> 	'ceisa_tpb/bc27o_frm',
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view('ceisa_tpb/_cjs');
		
	}
	
	function bc27o_process(){
		if (isset($_REQUEST['simpan'])){
			//echo $_FILES["h"]["type"]."<br>";
			//echo $_FILES["h"]["name"];
			if (($_FILES["h"]["type"] == "application/vnd.ms-excel")){
				if ($_FILES["h"]["error"] < 1){
					//Jenis BC 270
					$DokKdBc=10;

					$inputFileType = IOFactory::identify($_FILES['h']['tmp_name']);
					$objReader = IOFactory::createReader($inputFileType);
					$spreadsheet = $objReader->load($_FILES['h']['tmp_name']);

					//Sheet Header
					$sheet = 0;
					$sheet = $spreadsheet->setActiveSheetIndex($sheet);

					//dapatkan jml baris sheet2
					$totRowExist = $sheet->getHighestRow();
					// echo $totRowExist;

					for ($row = 2; $row <= $totRowExist; $row++):
						$FCAR = $sheet->getCell('A' . $row)->getValue();

						$CAR = substr($FCAR, -6);
						$TgAju=substr($FCAR, 12, 4)."-".substr($FCAR, 16, 2)."-".substr($FCAR, 18, 2);
										
						$KdKpbcAsal = $sheet->getCell('B' . $row)->getValue();
						// $KdJnsTpbAsal = $sheet->getCell('BQ' . $row)->getValue();
						$KdJnsTpbTuj = $sheet->getCell('CI' . $row)->getValue();
						$KdJnsTpbAsal = $KdJnsTpbTuj;

						$KdJnsBarang = $sheet->getCell('BL' . $row)->getValue();
						if ($KdJnsBarang == ''){
							$KdJnsBarang = 1;
						}
						$KdTp = $sheet->getCell('CH' . $row)->getValue();
						if ($KdTp == ''){
							$KdTp = 3;
						}
						$NoDaf = $sheet->getCell('CZ' . $row)->getValue();
						//Tanggal Daftar
						$dMY = $sheet->getCell('DN' . $row)->getValue();
						$TgDaf = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgDaf = $date->format('Y-m-d');	
							}
						}

						//Tanggal TTD
						$dMY = $sheet->getCell('DT' . $row)->getValue();
						$TgTT = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgTT = $date->format('Y-m-d');	
							}
						}

						$KdKpBongkar = $sheet->getCell('BZ' . $row)->getValue();
						$KdKpbcTuj = $sheet->getCell('BS' . $row)->getValue();
						$KdKpPengawas='050500';
						$NpwpPrshn = $sheet->getCell('AI' . $row)->getValue();
						$NmPengusaha = $sheet->getCell('CR' . $row)->getValue();
						$NipPengusaha = $sheet->getCell('AM' . $row)->getValue();
						$NmPejabat='';
						$NipPejabat='';
						$ref_id='';
						$NmTuj='';
						$NmPpjk='';
						$KdTimbun='';
						$KdKemas = '';
						$JmlKemas = $sheet->getCell('AO' . $row)->getValue();
						$MerekKemas = '';
						$BRUTO=$sheet->getCell('T' . $row)->getValue();
						$NETTO=$sheet->getCell('CT' . $row)->getValue();
						$KdVal=$sheet->getCell('CK' . $row)->getValue();
						$CIF=$sheet->getCell('U' . $row)->getValue();
						$CIF_RUPIAH=$sheet->getCell('V' . $row)->getValue();
						$Total='';
						$TotalH='';
						$tgl=$TgDaf." ".date('H:i:s');
						$usr = 'SYSTEM';

						// $ID_HEADER=$r['ID'];
						$NAMA_PENGIRIM = $sheet->getCell('CP' . $row)->getValue();
						$NAMA_PEMASOK = $sheet->getCell('D' . $row)->getValue();		
						$NAMA_PENERIMA_BARANG = $sheet->getCell('CN' . $row)->getValue();
						$ALAMAT_PENGIRIM = $sheet->getCell('K' . $row)->getValue();				
						$ALAMAT_PEMASOK = $sheet->getCell('H' . $row)->getValue();
						$ALAMAT_PENERIMA_BARANG = $sheet->getCell('J' . $row)->getValue(); 
						$HARGA_PENYERAHAN = $sheet->getCell('AD' . $row)->getValue();
						$HrgSerah=$HARGA_PENYERAHAN;

						$q="SELECT MAX(matout_id) AS auto_no FROM mat_outhdr_sub LIMIT 1";
						$rs = $this->db->query($q)->result_array();
						
						$autono=($rs)?$rs[0]['auto_no']+1:1;
						$ref_id=$autono;

						$NAMA_PEMASOK = $NAMA_PEMASOK;

						$wh_Npwp = "";
						if ($NpwpPrshn != ""){
							$wh_Npwp = " OR NpwpPrshn = '$NpwpPrshn'";
						}

						$q="SELECT KdPrshn FROM mst_perusahaan WHERE NmPrshn='$NAMA_PEMASOK' $wh_Npwp ";
						$rsp = $this->db->query($q)->result_array();
						
						if ($rsp){
							$NmTuj=$rsp[0]['KdPrshn'];
						} 


						//MANIPULASI DATA UNTUK INCOMING HEADER					
						$currency=$KdVal;
						$supplier=$NmTuj;
						$matin_no = 'IMP'.$CAR;
						//Hapus Transaksi Incoming
						$q="DELETE FROM mat_outhdr_sub WHERE KdJnsDok='$DokKdBc' 
							AND matin_no = '$matin_no'
							AND matin_date = '$TgAju'
							AND NoDaf='$NoDaf' 
							AND TgDaf='$TgDaf'
							AND mat_type='1'";
						// die($q);
						$this->db->query($q);				

						$q = "INSERT INTO mat_outhdr_sub (
						matout_id,matin_type,matin_no,matin_date,mat_type,
						tot_weight,notes,KdJnsDok,supplier,currency,kurs,NoDaf,TgDaf,kd_cmpy
						) VALUES (
						'$autono','0','$matin_no','$TgAju','1',
						'$BRUTO','','$DokKdBc','$supplier','$currency','$NDPBM','$NoDaf','$TgDaf','9221'
						)";
						// echo $q."<br>";
						$this->db->query($q);


						//MANIPULASI DATA UNTUK LIST BARANG (TABEL barang)
						$sheetBar = $spreadsheet->setActiveSheetIndexByName('Barang');
						//dapatkan jml baris sheet
						$totRowExistBar = $sheetBar->getHighestRow();
						for ($rBar = 2; $rBar <= $totRowExistBar; $rBar++):
							$FCARBar = $sheetBar->getCell('A' . $rBar)->getValue();

							$no_urut_barang = $sheetBar->getCell('B' . $rBar)->getValue();
							$HE = $sheetBar->getCell('AK' . $rBar)->getValue();
							$KdBarang = $sheetBar->getCell('U' . $rBar)->getValue();
							$UrBarang= $sheetBar->getCell('AQ' . $rBar)->getValue();
							$Spek = $sheetBar->getCell('AM' . $rBar)->getValue();
							$Negara = $sheetBar->getCell('AA' . $rBar)->getValue();
							$BM2 = 0;
							$BMT = 0;
							$Cukai2 = 0;
							$PPN2 = 0;
							$PPnBM2 = 0;
							$PPh2 = 0;
							$qty = $sheetBar->getCell('Q' . $rBar)->getValue();
							$price = $sheetBar->getCell('K' . $rBar)->getValue();
							$JnsSatuan = $sheetBar->getCell('AB' . $rBar)->getValue();
							$NETTO2 = $sheetBar->getCell('AG' . $rBar)->getValue();
							$CIF2 = $sheetBar->getCell('D' . $rBar)->getValue();
							$HrgSerah2 = $sheetBar->getCell('L' . $rBar)->getValue();

							if ($FCARBar == $FCAR):
								//TAMBAH DETAIL
								$q = "INSERT INTO mat_incdet_sub (
										matout_id,child_no,mat_id,weight,qty,price
										) VALUES (
										'$autono','$no_urut_barang','$KdBarang','$NETTO2','$qty','$price'
										)";
								$this->db->query($q);
							endif;
						endfor;
						echo "$CAR $TgAju Berhasil<br>";
					endfor;
				} else {
					echo "Return Code: " . $_FILES["h"]["error"] . "<br />";
				}
			} else {
				}
		//akhir if post simpan
		}
	}

	public function bc30()
	{		
		$data = array(
			'title' 	=> 	$this->title,
			'content' 	=> 	'ceisa_tpb/bc30_frm',
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view('ceisa_tpb/_cjs');
		
	}
	
	function bc30_process(){
		if (isset($_REQUEST['simpan'])){
			//echo $_FILES["h"]["type"]."<br>";
			//echo $_FILES["h"]["name"];
			if (($_FILES["h"]["type"] == "application/vnd.ms-excel")){
				if ($_FILES["h"]["error"] < 1){
					//Jenis BC 30
					$DokKdBc=7;

					$inputFileType = IOFactory::identify($_FILES['h']['tmp_name']);
					$objReader = IOFactory::createReader($inputFileType);
					$spreadsheet = $objReader->load($_FILES['h']['tmp_name']);

					//Sheet Header
					$sheet = 0;
					$sheet = $spreadsheet->setActiveSheetIndex($sheet);

					//dapatkan jml baris sheet2
					$totRowExist = $sheet->getHighestRow();
					// echo $totRowExist;

					for ($row = 2; $row <= $totRowExist; $row++):
						$FCAR = $sheet->getCell('A' . $row)->getValue();

						$CAR = substr($FCAR, -6);
						$TgAju=substr($FCAR, 12, 4)."-".substr($FCAR, 16, 2)."-".substr($FCAR, 18, 2);
										
						$KdKpbcAsal = $sheet->getCell('B' . $row)->getValue();
						// $KdJnsTpbAsal = $sheet->getCell('BQ' . $row)->getValue();
						$KdJnsTpbTuj = $sheet->getCell('CI' . $row)->getValue();
						$KdJnsTpbAsal = $KdJnsTpbTuj;

						$KdJnsBarang = $sheet->getCell('BL' . $row)->getValue();
						if ($KdJnsBarang == ''){
							$KdJnsBarang = 1;
						}
						$KdTp = $sheet->getCell('CH' . $row)->getValue();
						if ($KdTp == ''){
							$KdTp = 3;
						}
						$NoDaf = $sheet->getCell('CZ' . $row)->getValue();
						//Tanggal Daftar
						$dMY = $sheet->getCell('DN' . $row)->getValue();
						$TgDaf = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgDaf = $date->format('Y-m-d');	
							}
						}

						//Tanggal TTD
						$dMY = $sheet->getCell('DT' . $row)->getValue();
						$TgTT = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgTT = $date->format('Y-m-d');	
							}
						}

						$KdKpBongkar = $sheet->getCell('BZ' . $row)->getValue();
						$KdKpbcTuj = $sheet->getCell('BS' . $row)->getValue();
						$KdKpPengawas='050500';
						$NpwpPrshn = $sheet->getCell('AI' . $row)->getValue();
						$NmPengusaha = $sheet->getCell('CR' . $row)->getValue();
						$NipPengusaha = $sheet->getCell('AM' . $row)->getValue();
						$NmPejabat='';
						$NipPejabat='';
						$ref_id='';
						$NmTuj='';
						$NmPpjk='';
						$KdTimbun='';
						$KdKemas = '';
						$JmlKemas = $sheet->getCell('AO' . $row)->getValue();
						$MerekKemas = '';
						$BRUTO=$sheet->getCell('T' . $row)->getValue();
						$NETTO=$sheet->getCell('CT' . $row)->getValue();
						$KdVal=$sheet->getCell('CK' . $row)->getValue();
						$CIF=$sheet->getCell('U' . $row)->getValue();
						$CIF_RUPIAH=$sheet->getCell('V' . $row)->getValue();
						$Total='';
						$TotalH='';
						$tgl=$TgDaf." ".date('H:i:s');
						$usr = 'SYSTEM';

						// $ID_HEADER=$r['ID'];
						$NAMA_PENGIRIM = $sheet->getCell('CP' . $row)->getValue();
						$NAMA_PEMASOK = $sheet->getCell('D' . $row)->getValue();		
						$NAMA_PENERIMA_BARANG = $sheet->getCell('CN' . $row)->getValue();
						$ALAMAT_PENGIRIM = $sheet->getCell('K' . $row)->getValue();				
						$ALAMAT_PEMASOK = $sheet->getCell('H' . $row)->getValue();
						$ALAMAT_PENERIMA_BARANG = $sheet->getCell('J' . $row)->getValue(); 
						$HARGA_PENYERAHAN = $sheet->getCell('AD' . $row)->getValue();
						$HrgSerah=$HARGA_PENYERAHAN;

						$q="SELECT MAX(matout_id) AS auto_no FROM mat_outhdr_sub LIMIT 1";
						$rs = $this->db->query($q)->result_array();
						
						$autono=($rs)?$rs[0]['auto_no']+1:1;
						$ref_id=$autono;

						$NAMA_PEMASOK = $NAMA_PEMASOK;

						$wh_Npwp = "";
						if ($NpwpPrshn != ""){
							$wh_Npwp = " OR NpwpPrshn = '$NpwpPrshn'";
						}

						$q="SELECT KdPrshn FROM mst_perusahaan WHERE NmPrshn='$NAMA_PEMASOK' $wh_Npwp ";
						$rsp = $this->db->query($q)->result_array();
						
						if ($rsp){
							$NmTuj=$rsp[0]['KdPrshn'];
						} 


						//MANIPULASI DATA UNTUK INCOMING HEADER					
						$currency=$KdVal;
						$supplier=$NmTuj;
						$matin_no = 'IMP'.$CAR;
						//Hapus Transaksi Incoming
						$q="DELETE FROM mat_outhdr_sub WHERE KdJnsDok='$DokKdBc' 
							AND matin_no = '$matin_no'
							AND matin_date = '$TgAju'
							AND NoDaf='$NoDaf' 
							AND TgDaf='$TgDaf'
							AND mat_type='1'";
						// die($q);
						$this->db->query($q);				

						$q = "INSERT INTO mat_outhdr_sub (
						matout_id,matin_type,matin_no,matin_date,mat_type,
						tot_weight,notes,KdJnsDok,supplier,currency,kurs,NoDaf,TgDaf,kd_cmpy
						) VALUES (
						'$autono','0','$matin_no','$TgAju','0',
						'$BRUTO','','$DokKdBc','$supplier','$currency','$NDPBM','$NoDaf','$TgDaf','9221'
						)";
						// echo $q."<br>";
						$this->db->query($q);


						//MANIPULASI DATA UNTUK LIST BARANG (TABEL barang)
						$sheetBar = $spreadsheet->setActiveSheetIndexByName('Barang');
						//dapatkan jml baris sheet
						$totRowExistBar = $sheetBar->getHighestRow();
						for ($rBar = 2; $rBar <= $totRowExistBar; $rBar++):
							$FCARBar = $sheetBar->getCell('A' . $rBar)->getValue();

							$no_urut_barang = $sheetBar->getCell('B' . $rBar)->getValue();
							$HE = $sheetBar->getCell('AK' . $rBar)->getValue();
							$KdBarang = $sheetBar->getCell('U' . $rBar)->getValue();
							$UrBarang= $sheetBar->getCell('AQ' . $rBar)->getValue();
							$Spek = $sheetBar->getCell('AM' . $rBar)->getValue();
							$Negara = $sheetBar->getCell('AA' . $rBar)->getValue();
							$BM2 = 0;
							$BMT = 0;
							$Cukai2 = 0;
							$PPN2 = 0;
							$PPnBM2 = 0;
							$PPh2 = 0;
							$qty = $sheetBar->getCell('Q' . $rBar)->getValue();
							$price = $sheetBar->getCell('K' . $rBar)->getValue();
							$JnsSatuan = $sheetBar->getCell('AB' . $rBar)->getValue();
							$NETTO2 = $sheetBar->getCell('AG' . $rBar)->getValue();
							$CIF2 = $sheetBar->getCell('D' . $rBar)->getValue();
							$HrgSerah2 = $sheetBar->getCell('L' . $rBar)->getValue();

							if ($FCARBar == $FCAR):
								//TAMBAH DETAIL
								$q = "INSERT INTO mat_incdet_sub (
										matout_id,child_no,mat_id,weight,qty,price
										) VALUES (
										'$autono','$no_urut_barang','$KdBarang','$NETTO2','$qty','$price'
										)";
								$this->db->query($q);
							endif;
						endfor;
						echo "$CAR $TgAju Berhasil<br>";
					endfor;
				} else {
					echo "Return Code: " . $_FILES["h"]["error"] . "<br />";
				}
			} else {
				}
		//akhir if post simpan
		}
	}

	public function bc41()
	{		
		$data = array(
			'title' 	=> 	$this->title,
			'content' 	=> 	'ceisa_tpb/bc41_frm',
			);		
		$this->load('tpl',$data['content'], $data);
		$this->load->view('ceisa_tpb/_cjs');
		
	}
	
	function bc41_process(){
		if (isset($_REQUEST['simpan'])){
			//echo $_FILES["h"]["type"]."<br>";
			//echo $_FILES["h"]["name"];
			if (($_FILES["h"]["type"] == "application/vnd.ms-excel")){
				if ($_FILES["h"]["error"] < 1){
					//Jenis BC 41
					$DokKdBc=9;

					$inputFileType = IOFactory::identify($_FILES['h']['tmp_name']);
					$objReader = IOFactory::createReader($inputFileType);
					$spreadsheet = $objReader->load($_FILES['h']['tmp_name']);

					//Sheet Header
					$sheet = 0;
					$sheet = $spreadsheet->setActiveSheetIndex($sheet);

					//dapatkan jml baris sheet2
					$totRowExist = $sheet->getHighestRow();
					// echo $totRowExist;

					for ($row = 2; $row <= $totRowExist; $row++):
						$FCAR = $sheet->getCell('A' . $row)->getValue();

						$CAR = substr($FCAR, -6);
						$TgAju=substr($FCAR, 12, 4)."-".substr($FCAR, 16, 2)."-".substr($FCAR, 18, 2);
										
						$KdKpbcAsal = $sheet->getCell('B' . $row)->getValue();
						// $KdJnsTpbAsal = $sheet->getCell('BQ' . $row)->getValue();
						$KdJnsTpbTuj = $sheet->getCell('CI' . $row)->getValue();
						$KdJnsTpbAsal = $KdJnsTpbTuj;

						$KdJnsBarang = $sheet->getCell('BL' . $row)->getValue();
						if ($KdJnsBarang == ''){
							$KdJnsBarang = 1;
						}
						$KdTp = $sheet->getCell('CH' . $row)->getValue();
						if ($KdTp == ''){
							$KdTp = 3;
						}
						$NoDaf = $sheet->getCell('CZ' . $row)->getValue();
						//Tanggal Daftar
						$dMY = $sheet->getCell('DN' . $row)->getValue();
						$TgDaf = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgDaf = $date->format('Y-m-d');	
							}
						}

						//Tanggal TTD
						$dMY = $sheet->getCell('DT' . $row)->getValue();
						$TgTT = "";
						if ($dMY != ""){
							$date = DateTime::createFromFormat('d M Y', $dMY);
							if ($date){
								$TgTT = $date->format('Y-m-d');	
							}
						}

						$KdKpBongkar = $sheet->getCell('BZ' . $row)->getValue();
						$KdKpbcTuj = $sheet->getCell('BS' . $row)->getValue();
						$KdKpPengawas='050500';
						$NpwpPrshn = $sheet->getCell('AI' . $row)->getValue();
						$NmPengusaha = $sheet->getCell('CR' . $row)->getValue();
						$NipPengusaha = $sheet->getCell('AM' . $row)->getValue();
						$NmPejabat='';
						$NipPejabat='';
						$ref_id='';
						$NmTuj='';
						$NmPpjk='';
						$KdTimbun='';
						$KdKemas = '';
						$JmlKemas = $sheet->getCell('AO' . $row)->getValue();
						$MerekKemas = '';
						$BRUTO=$sheet->getCell('T' . $row)->getValue();
						$NETTO=$sheet->getCell('CT' . $row)->getValue();
						$KdVal=$sheet->getCell('CK' . $row)->getValue();
						$CIF=$sheet->getCell('U' . $row)->getValue();
						$CIF_RUPIAH=$sheet->getCell('V' . $row)->getValue();
						$Total='';
						$TotalH='';
						$tgl=$TgDaf." ".date('H:i:s');
						$usr = 'SYSTEM';

						// $ID_HEADER=$r['ID'];
						$NAMA_PENGIRIM = $sheet->getCell('CP' . $row)->getValue();
						$NAMA_PEMASOK = $sheet->getCell('D' . $row)->getValue();		
						$NAMA_PENERIMA_BARANG = $sheet->getCell('CN' . $row)->getValue();
						$ALAMAT_PENGIRIM = $sheet->getCell('K' . $row)->getValue();				
						$ALAMAT_PEMASOK = $sheet->getCell('H' . $row)->getValue();
						$ALAMAT_PENERIMA_BARANG = $sheet->getCell('J' . $row)->getValue(); 
						$HARGA_PENYERAHAN = $sheet->getCell('AD' . $row)->getValue();
						$HrgSerah=$HARGA_PENYERAHAN;

						$q="SELECT MAX(matout_id) AS auto_no FROM mat_outhdr_sub LIMIT 1";
						$rs = $this->db->query($q)->result_array();
						
						$autono=($rs)?$rs[0]['auto_no']+1:1;
						$ref_id=$autono;

						$NAMA_PEMASOK = $NAMA_PEMASOK;

						$wh_Npwp = "";
						if ($NpwpPrshn != ""){
							$wh_Npwp = " OR NpwpPrshn = '$NpwpPrshn'";
						}

						$q="SELECT KdPrshn FROM mst_perusahaan WHERE NmPrshn='$NAMA_PEMASOK' $wh_Npwp ";
						$rsp = $this->db->query($q)->result_array();
						
						if ($rsp){
							$NmTuj=$rsp[0]['KdPrshn'];
						} 


						//MANIPULASI DATA UNTUK INCOMING HEADER					
						$currency=$KdVal;
						$supplier=$NmTuj;
						$matin_no = 'IMP'.$CAR;
						//Hapus Transaksi Incoming
						$q="DELETE FROM mat_outhdr_sub WHERE KdJnsDok='$DokKdBc' 
							AND matin_no = '$matin_no'
							AND matin_date = '$TgAju'
							AND NoDaf='$NoDaf' 
							AND TgDaf='$TgDaf'
							AND mat_type='1'";
						// die($q);
						$this->db->query($q);				

						$q = "INSERT INTO mat_outhdr_sub (
						matout_id,matin_type,matin_no,matin_date,mat_type,
						tot_weight,notes,KdJnsDok,supplier,currency,kurs,NoDaf,TgDaf,kd_cmpy
						) VALUES (
						'$autono','0','$matin_no','$TgAju','1',
						'$BRUTO','','$DokKdBc','$supplier','$currency','$NDPBM','$NoDaf','$TgDaf','9221'
						)";
						// echo $q."<br>";
						$this->db->query($q);


						//MANIPULASI DATA UNTUK LIST BARANG (TABEL barang)
						$sheetBar = $spreadsheet->setActiveSheetIndexByName('Barang');
						//dapatkan jml baris sheet
						$totRowExistBar = $sheetBar->getHighestRow();
						for ($rBar = 2; $rBar <= $totRowExistBar; $rBar++):
							$FCARBar = $sheetBar->getCell('A' . $rBar)->getValue();

							$no_urut_barang = $sheetBar->getCell('B' . $rBar)->getValue();
							$HE = $sheetBar->getCell('AK' . $rBar)->getValue();
							$KdBarang = $sheetBar->getCell('U' . $rBar)->getValue();
							$UrBarang= $sheetBar->getCell('AQ' . $rBar)->getValue();
							$Spek = $sheetBar->getCell('AM' . $rBar)->getValue();
							$Negara = $sheetBar->getCell('AA' . $rBar)->getValue();
							$BM2 = 0;
							$BMT = 0;
							$Cukai2 = 0;
							$PPN2 = 0;
							$PPnBM2 = 0;
							$PPh2 = 0;
							$qty = $sheetBar->getCell('Q' . $rBar)->getValue();
							$price = $sheetBar->getCell('K' . $rBar)->getValue();
							$JnsSatuan = $sheetBar->getCell('AB' . $rBar)->getValue();
							$NETTO2 = $sheetBar->getCell('AG' . $rBar)->getValue();
							$CIF2 = $sheetBar->getCell('D' . $rBar)->getValue();
							$HrgSerah2 = $sheetBar->getCell('L' . $rBar)->getValue();

							if ($FCARBar == $FCAR):
								//TAMBAH DETAIL
								$q = "INSERT INTO mat_incdet_sub (
										matout_id,child_no,mat_id,weight,qty,price
										) VALUES (
										'$autono','$no_urut_barang','$KdBarang','$NETTO2','$qty','$price'
										)";
								$this->db->query($q);
							endif;
						endfor;
						echo "$CAR $TgAju Berhasil<br>";
					endfor;
				} else {
					echo "Return Code: " . $_FILES["h"]["error"] . "<br />";
				}
			} else {
				}
		//akhir if post simpan
		}
	}
	
}