<?php

class Kode
{
  public $repGaring = ';;';
  public $repGabung = ':::';

  public function setPrefik($num)
  {
    $jumlah = $num + 1;

    if(strlen($jumlah) == 1){
      $jumlah = '0000'.$jumlah;
    }elseif(strlen($jumlah) == 2){
      $jumlah = '000'.$jumlah;
    }elseif(strlen($jumlah) == 3){
      $jumlah = '00'.$jumlah;
    }elseif(strlen($jumlah) == 4){
      $jumlah = '0'.$jumlah;
    }
    
    return $jumlah;
  }

  public function getFlag()
  {
    $data = array(
            'A',
            'B',
          );
    
    return $data;
  }

  public function getWb()
  {
    $data = array(
            'W' => 'Wrapper',
            'B' => 'Binder',
          );
    
    return $data;
  }
  
  public function setAsalPromosi($kode)
  {
    $data = $this->getAsalPromosi();
    return $data[$kode]; 
  }
  
  public function getSamaTengah($var, $a)
  {
    $len = strlen($var);
    $sis = 40 - $len;
    $sis = (int)$sis/2;
  
    $l= '';
    $r= '';
    for($i = 1; $i <= $sis; $i++){
      $l.= "$a";
      $r.= "$a";
    }
    
    $n = $l.$var.$r;
    
    return $n;
  }
  
  public function getSama($no, $jum, $a)
  {
    $jum = $jum - strlen($no);
    $n = $no;
    for($i = 1; $i <= $jum; $i++){
      $n.= "$a";
    }
    
    return $n;
  }
  
  public function getLeft($no, $jum, $a)
  {
    $jum = $jum - strlen($no);
    $n = $no;
    for($i = 1; $i <= $jum; $i++){
      $n.= "$a";
    }
    
    return $n;
  }
  
  public function getRight($no, $jum, $a)
  {
    $jum = $jum - strlen($no);
    $n = '';
    for($i = 1; $i <= $jum; $i++){
      $n.= "$a";
    }
    $n.= $no;
    
    return $n;
  }
  
  public function getNomor($no, $jum)
  {
    $jum = $jum - strlen($no);
    $n = '';
    for($i = 1; $i <= $jum; $i++){
      $n.= '0';
    }
    $n.= $no;
    
    return $n;
  }
  
  public function getPpn()
  {
    $ppn = array(
          '0' => 'NON PAJAK',
          '1' => 'PAJAK',
          '3' => 'CASH',
        );
    return $ppn;
  }
  
  public function setPpn($ppn)
  {
    $dataPpn = $this->getPpn();
    return $dataPpn[$ppn];
  }
  
  public function getAbs($nominal, $ex)
  {
    if($nominal < 0){
      $nominal = str_ireplace('-', '', $nominal);
      return $nominal = '('.number_format($nominal, $ex).')';
    }else{
      return number_format($nominal, 0);
    }
  }
  
  public function getSex()
  {
    $data = array(
          'L' => 'Laki-laki',
          'P' => 'Perempuan',
        );
    return $data;
  }
  
  public function getEnd()
  {
    $data = array(
          strtoupper(''),
          strtoupper('end'),
        );
    return $data;
  }
  
  public function setStatus($key)
  {
    if(!empty($key)){
      return 'E';
    }
    return '';
  }
  
  public function getStatus()
  {
    $data = array(
          strtoupper('aktif'),
          strtoupper('tidak aktif'),
        );
    return $data;
  }
  
  
  public function getKelas()
  {
    $data = array(
          strtoupper('pt'),
          strtoupper('cv'),
          strtoupper('ud'),
          strtoupper('dll'),
        );
    return $data;
  }
  
  public function getJenis()
  {
    $data = array(
          2 => strtoupper('supplier'),
          3 => strtoupper('customer'),
        );
    return $data;
  }
  
  public function setJenis($k)
  {
    $res = $this->getJenis();
    return $res[$k];
  }
  
  public function getBuruh()
  {
    $data = array(
          strtoupper('ya'),
          strtoupper('tidak'),
        );
    return $data;
  }
  
  public function getJabatan()
  {
    $data = array(
          '1' => strtoupper('admin'),
          // '2' => strtoupper('supplier'),
          // '3' => strtoupper('customer'),
          // '4' => strtoupper('contact person'),
          // '5' => strtoupper('sales'),
          // '6' => strtoupper('kasir'),
          // '7' => strtoupper('pelabuhan'),
        );
    return $data;
  }
  
  public function setJabatan($k)
  {
    $data = $this->getJabatan();
    return $data[$k];
  }
  
  public function getDk()
  {
    $data = array(
          'D' => strtoupper('debet'),
          'K' => strtoupper('credit'),
        );
    return $data;
  }
  
  public function getMu()
  {
    $data = array(
          'IDR' => strtoupper('IDR'),
        );
    return $data;
  }
  
  public function getKelAcc()
  {
    $data = array(
          1 => strtoupper('Aktiva'),
          2 => strtoupper('Hutang'),
          3 => strtoupper('Modal'),
          4 => strtoupper('Pendapatan'),
          5 => strtoupper('Biaya'),
          6 => strtoupper('Pendapatan di luar usaha'),
          7 => strtoupper('Biaya di luar usaha'),
          8 => strtoupper('Lain-lain'),
        );
    return $data;
  }
  
  public function setKelAcc($id)
  {
    $data = $this->getKelAcc();
    return $data[$id];
  }
  
  public function getJenisAcc()
  {
    $data = array(
          1 => strtoupper('NORMAL'),
          2 => strtoupper('BANK'),
          3 => strtoupper('KAS'),
        );
    return $data;
  }
  
  public function setJenisAcc($id)
  {
    $data = $this->getJenisAcc();
    return $data[$id];
  }
  
  public function getDep()
  {
    $data = array(
          1 => strtoupper('Umum'),
          2 => strtoupper('Accounting'),
          3 => strtoupper('Admin'),
          4 => strtoupper('HRD'),
          5 => strtoupper('EDP'),
          6 => strtoupper('penjualan'),
        );
    return $data;
  }
  
  public function setDep($id = '')
  {
    $res = $this->getDep();
    return $res[$id];
  }
  
  public function ConfAbjNum()
  {
    $res[] = 'A';
    $res[] = 'B';
    $res[] = 'C';
    $res[] = 'D';
    $res[] = 'E';
    $res[] = 'F';
    $res[] = 'G';
    $res[] = 'H';
    $res[] = 'I';
    $res[] = 'J';
    $res[] = 'K';
    $res[] = 'L';
    $res[] = 'M';
    $res[] = 'N';
    $res[] = 'O';
    $res[] = 'P';
    $res[] = 'Q';
    $res[] = 'R';
    $res[] = 'S';
    $res[] = 'T';
    $res[] = 'U';
    $res[] = 'V';
    $res[] = 'W';
    $res[] = 'X';
    $res[] = 'Y';
    $res[] = 'Z';
    return $res;
  }
  
  public function genBarCode($kode, $param)
  {
    $ex = explode('-', $kode);
    $kode = '';
    foreach($param as $key => $row){
      $kode.= $this->$row($ex[$key]);
    }
    return $kode;
  }
  
  protected function prefik($i)
  {
    if($i == 'INV'){
      return 'A';
    }
  }

  protected function getCodCabang()
  {
    $res[] = 'ASB';
    $res[] = 'BSB';
    $res[] = 'CSB';
    $res[] = 'ATS';
    $res[] = 'BTS';
    $res[] = 'CTS';
    return $res;
  }
  
  public function getJenSub()
  {
    $data = array(
          1 => strtoupper('supplier'),
          2 => strtoupper('customer'),
        );
    return $data;
  }
  
  public function getJenQty()
  {
    $data = array(
          1 => strtoupper('In'),
          2 => strtoupper('out'),
        );
    return $data;
  }
  
  protected function cabang($i)
  {
    $conf = $this->ConfAbjNum();
    foreach($this->getCodCabang() as $key => $row){
      if($row == $i){
        return $conf[$key];
      }
    }
  }
  
  protected function periode($i)
  {
    $no = 1;
    for($n = 201301; $n <= 301311; $n++){
      if($i == $n){
        return $this->getNomor($no, 3);
      }
      $no++;
    }
  
  }
  
  protected function seri($i)
  {
    return $i;
  }
  
  public function getKota($kota)
  {
    $kota = str_ireplace('KOTA', '', $kota);
    $kota = str_ireplace('KABUPATEN', '', $kota);
    $kota = str_ireplace('KAB', '', $kota);
    $kota = str_ireplace('KAB.', '', $kota);
    return trim($kota);
  }
  
  public function getModel()
  {
    $res = array(
            1 => strtoupper('Normal'),
            2 => strtoupper('Keterangan'),
          );
    return $res;
  }
  
  public function setKodeLeft($id)
  {
    $data = $this->getKodeLeft();
    return $data[$id];
  }
  
  public function getKodeLeft()
  {
    $res = array(
            'L' => strtoupper('left'),
            'R' => strtoupper('right'),
          );
    return $res;
  }
  
  public function setDark($id)
  {
    $data = $this->getDark();
    return $data[$id];
  }
  
  public function getDark()
  {
    $res = array(
            'D' => strtoupper('Dark'),
            'L' => strtoupper('Light'),
            'N' => strtoupper('normal'),
            'O' => strtoupper('left over'),
          );
    return $res;
  }
  
  public function getShift()
  {
    $res = array(
            '1',
            '2',
          );
    return $res;
  }
  
  public function getSide()
  {
    $res = array(
            'S'  => strtoupper('side'),
            'C' => strtoupper('comb'),
          );
    return $res;
  }
  
  public function setSide($key)
  {
    $side = $this->getSide();
    return $side[$key];
  }
  
  public function setSideRet($key)
  {
    foreach($this->getSide() as $k => $r){
      if($key == $r){
        return $k;
      }
    }
    return '';
  }
  
  public function getTypeWrn()
  {
    $res = array(
            strtoupper('20.0 POLY'),
            strtoupper('32.0 CM'),
          );
    return $res;
  }
  
  public function setModel($id)
  {
    $data = $this->getModel();
    return $data[$id];
  }
  
  public function convNumeric($res)
  {
    
    if($_POST){
      $res = str_ireplace('.', '', $res);
      $res = str_ireplace(',', '.', $res);
    }else{
      $res = str_ireplace(',', '', $res);
      // $res = str_ireplace('.', '', $res);
      // $res = str_ireplace('*', '.', $res);
    }

    $res = trim($res);
    
    if(empty($res)){
      $res = 0;
    }
    
    $temp = (int)str_ireplace('.', '', $res);
    $temp = trim($temp);
    if(empty($temp)){
      $res = 0;
    }
    
    return number_format($res, 2, '.', '');
  }
  
  public function accNumeric($res, $dec)
  {
    $awal = '';
    $akhir = '';
    
    if($res < 0){
      $awal = '(';
      $akhir = ')';
    }
    
    $res = abs($res);
    return $awal.number_format($res, (int)$dec, '.', ',').$akhir;
  }
  
  public function accInteger($res)
  {
    $awal = '';
    $akhir = '';
    
    if($res < 0){
      $awal = '(';
      $akhir = ')';
    }
    
    $res = abs($res);
    return $awal.number_format($res, 0, '', '').$akhir;
  }
  
  public function text_right($res)
  {
    $awal = '';
    $akhir = '';
    
    if($res < 0){
      $awal = '(';
      $akhir = ')';
    }
    
    $res = abs($res);
    return $awal.number_format($res, 0, '', '').$akhir;
  }
  
  public function concatFormat()
  {
	return "CONCAT(replace(tobacco, ' ', ''), replace(dienumber, ' ', ''), replace(wb, ' ', ''), replace(client, ' ', ''), replace(merk, ' ', ''))";
  }
  
  public function selFormat($post, $i)
  {
		$format = '';
		foreach($this->str_format() as $key =>$row){
			$key = $key.$i;
			$format.= trim($post[$key]);
		}
		return $format;
  }
  
  public function str_format()
  {
	return array(
				'tobacco' 	=> 3,
				'dienumber' => 7,
				'wb' 		=> 1,
				'client' 	=> 3,
				'merk' 		=> 3,
				);
  }
  
  public function format($row, $i = '')
  {
	$tob = "tobacco".$i;
	$die = "dienumber".$i;
	$wb  = "wb".$i;
	$cli = "client".$i;
	$merk = "merk".$i;
  
    $res = '';
    $res.= $this->getLeft($row->$tob, 3, '&nbsp;');
    $res.= $this->getLeft($row->$die, 7, '&nbsp;');
    $res.= $this->getLeft($row->$wb, 1, '&nbsp;');
    $res.= $this->getLeft($row->$cli, 3, '&nbsp;');
    $res.= $this->getLeft($row->$merk, 3, '&nbsp;');
    return $res;
  }
  
  public function genFormat($row)
  {
    $res = '';
    $res.= $this->getLeft($row->tobacco, 3, ' ');
    $res.= $this->getLeft($row->dienumber, 7, ' ');
    $res.= $this->getLeft($row->wb, 1, ' ');
    $res.= $this->getLeft($row->client, 3, ' ');
    $res.= $this->getLeft($row->merk, 3, ' ');
    return $res;
  }
  
  public function formatNonSpasi($row)
  {
		return str_ireplace(" ", "", trim($row->tobacco.$row->dienumber.$row->wb.$row->client.$row->merk));
  }
  
  public function getAbjad(){
    return array("1" => 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
  }
 public function getControl()
  {
    $data = array(
          1 => 'Printed ',
          2 => 'Deleted ',
          3 => 'Edited  ',
          4 => 'Replaced',
          5 => 'Closed  ',
          6 => 'Opened  ',
          7 => 'Moved   ',
          8 => 'Taked   ',
          9 => 'Received',
         10 => 'Collect ',
         11 => 'Payment ',
         12 => 'Pending ',
        );
    return $data;
  }
  
  public function getPk($data)
  {
	$pk = "";
	if($data != null){
		foreach($data as $row){
			if(!empty($pk)){
				$pk.= ", ";
			}
			$pk.= $row;
		}
	}
	return $pk;
  }
  
  public function getConcat($data)
  {
	$pk = "";
	foreach($data as $row => $key){
		if(!empty($pk)){
			$pk.= ", ";
		}
		$pk.= $row;
	}
	return $pk;
  }
	
	public function setControl($id)
	{
		$control = $this->getControl();
		return $control[$id];
	}
	
	public function decAscii($kode)
	{
		$a = $kode / 10;
		$a = (int)$a;
		$a = $a+55;
		$a = chr($a);
		$a = $a.substr($kode, 2, 1);
		return $a;
	}
	
	public function ascii($kode, $jum)
	{
		$kode = (int)$kode;
		if(strlen($kode) == 2 and $kode != -1){
			$abj = substr($kode, 0, 1);
			$nil = (int)$abj;
			
			if($nil == '0'){
			
				$a = substr($kode, 0, 1);
				$a = ord($a);
				$a = ($a-55) * 10;

				$b = substr($kode, 1, 1);
				$c = $a + $b;
				$c = $c + $jum;
				
				return $this->decAscii($c);
			}else{
				$res = $kode + $jum;
				return $this->getRight($res, 2, 0);
			}
			
		}else{
			if(!empty($jum)){
				$res = $kode + $jum;
				return $this->getRight($res, 2, 0);
			}else{
				return '';
			}
		}
	}
	
	public function setAbj($kode, $jum)
	{
		if(!empty($kode)){
			$abj = $this->getAbjad();
			foreach($abj as $k => $i){
				if(strtoupper($i) == strtoupper($kode)){
					$nom = $k + $jum;
					return $abj[$nom];
				}
			}
			
		}else{
			return 'A';
		}
	}
	
	public function setAttrDetail($row, $baris)
	{
		unset($row['attr']);
		unset($row['type']);
		unset($row['width']);
		unset($row['label']);
		unset($row['class']);
		$res = '';
		foreach($row as $k => $r){
			$res.= ' '.$k.' = "'.str_ireplace('"', "&quot;", $r).'"';
		}
		return $res;
	}
	
	public function totBertingkat($row)
	{
		$bruto = $row->qty * $row->harga;
		$nett  = $bruto;
		
		$disRp = $row->diskon_2 + $row->diskon_3;
		$nett = $nett - $disRp;
		
		$dis1 = ($nett * $row->diskon_4) / 100;
		$nett = $nett - $dis1;
		
		$dis2 = ($nett * $row->diskon_5) / 100;
		$nett = $nett - $dis2;
		
		$dis3 = ($nett * $row->diskon_6) / 100;
		$nett = $nett - $dis3;
		
		$tot = $nett - $disRp;
		$nDis = ($dis1 + $dis2 + $dis3 + $disRp);
		
		
		$res['bruto'] 	= $bruto;
		$res['tot'] 	= $tot;
		$res['dis'] 	= $nDis;
		$res['disrp1'] 	= $disRp;
		$res['disrp2'] 	= 0;
		$res['dis1'] 	= $dis1;
		$res['dis2'] 	= $dis2;
		$res['dis3'] 	= $dis3;
		$res['dis4'] 	= $dis4;
		$res['dis5'] 	= $dis5;
		
		return $res;
	}
	
	public function totBertingkatPosting($row)
	{
		$tot = $this->totBertingkat($row);
		
		if($row->diskg > 0){
			$nDis = ($tot['tot'] * $row->diskg) / 100;
			$tot['tot'] = $tot['tot'] - $nDis;
			$tot['dis'] = $tot['dis'] + $nDis;
		}
		
		$tot['tot'] = round($tot['tot']);
		$tot['dis'] = round($tot['dis']);

		$row->ppn = strtoupper($row->ppn);
		
		$inv = substr($row->kd_inv, 0, 1);
		$dpp = $tot['tot'];
		$ppn = 0;
		if($inv == 'Z'){
		}else{
			if($row->ppn == 'I'){
				$dpp = round($tot['tot'] / 1.1);
				$ppn = $tot['tot'] - $dpp;
			}
			if($row->ppn == 'E'){
				$ppn = (int)(($tot['tot'] * 10) / 100);
			}
		}
		
		$tot['dpp'] = $dpp;
		$tot['ppn'] = $ppn;
		
		return $tot;
	}
	
	public function getting_browser(){
		$user_agent = $_SERVER['HTTP_USER_AGENT'];
		$browser        	=   "Unknown Browser";
		$daftar_browser  	=   array(
								'/msie/i'       =>  'Internet Explorer',
								'/firefox/i'    =>  'Firefox',
								'/safari/i'     =>  'Safari',
								'/chrome/i'     =>  'Chrome',
								'/opera/i'      =>  'Opera',
								'/netscape/i'   =>  'Netscape',
								'/maxthon/i'    =>  'Maxthon',
								'/konqueror/i'  =>  'Konqueror',
								'/mobile/i'     =>  'Handheld Browser'
							);

		foreach ($daftar_browser as $regex => $value) { 
			if (preg_match($regex, $user_agent)) {
				$browser    =   $value;
			}
		}
		return strtoupper($browser);
	}
	
	public function getLastAutoNota($em, $post)
	{
		$this->em = $em;
		$noasli = explode(".", $post['nojnl']);
		$noasli = $noasli[0];
			
		$sql = $this->em->addSelect("kdacf");
		$sql = $this->em->getFrom("umd", 'a');
		$sql = $this->em->getWhere("kdacf like '$noasli%'");
		$sql = $this->em->getMultiSort("kdacf desc");
		$sql = $this->em->getLimit(0, 1);
		$eks = $this->em->queryEks($sql);
		$res = $this->em->fetchObject($eks);
		if(!empty($res->kdacf)){
			$ex = explode("-", $res->kdacf);
			$jm = count($ex)-1;
			return (int)$ex[$jm] + 1;
		}else{
			return 1;
		}
	}
	
}