<?php

CLASS aros extends RasioProject
{
  protected $set;
  protected $enableCk = false;
  protected $report;
  protected $baseTitle;

  public function __construct($config)
  {
    $this->getConfig($config);
    $this->validation();
    $this->set();
  }

  protected function set()
  {
    $this->report       = new Report;
    $this->set->em      = $this->em;
    $this->set->kode    = 'ACC-006';
    $this->set->date    = date("d/m/Y H:i");
    $this->set->modul     = 'LAPORAN REKAP OUTSTANDING PIUTANG USAHA';
    $this->set->form    = $this->setForm();
    $this->set->display   = $this->setReport();
    $this->set->filter    = $this->report->setFilter($_GET, $this->setForm());
    $this->set->query     = $this->setQuery();
    $this->set->notNull   = array("date1", "date2", "paper");
    $this->set->button    = array("View", "Cetak", "Pdf", "Excel");
    $this->set->groupDis  = array();
    $this->set->groupBy   = "";
    $this->set->hitung    = array("debet", "credit", "saldo");
    $this->set->rowPage   = 60;
    $this->baseTitle      = $this->set->modul;
    return $this->set;
  }
  
  protected function defValue()
  {
    $lap = new Lap;
    $res->cabang  = $lap->getMinMax($this->set, "cabang", "kd_cabang");
    $res->account = $lap->getMinMax($this->set, "acc", "kdac");
    return $res; 
  }
  
  protected function setForm()
  {
    $this->defVal = $this->defValue();
    
    // $form[] = array('row' =>  0,  'col' => 0,   "label" => array("name" => "Profit Center",  "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'cabang1', "size" => 5, 'value' => $this->defVal->cabang->min, 'after' => 'load("cabang", "false", "cabang")')));
    // $form[] = array('row' =>  0,  'col' => 170, "label" => array("name" => "s/d",   "width" => 30)  ,   "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'cabang2', "size" => 5, 'value' => $this->defVal->cabang->max, 'after' => 'load("cabang", "false", "cabang")')));
    $form[] = array('row' =>  25,  'col' => 0,   "label" => array("name" => "Periode S/d",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'date',  'attr' => array("name" => 'date1', "size" => 10, 'class' => 'tanggal', 'value' => date("d/m/Y"))));
  
    $form[] = array('row' =>  50,  'col' => 0,   "label" => array("name" => "Paper",   "width" => 100),  "input" => array("type" => 'text', 'mode' => 'normal',  'attr' => array("name" => 'paper', "size" => 2, 'value' => 'P', 'after' => 'select()', "select" => 'paper')));

    return $form;
  }
  
  protected function setReport()
  {
    $res[] = array('id' => 'nm_cabang',	'nama' => 'PC',  		'type' => 'text',   'width' => 50,  'size' => 10);
    $res[] = array('id' => 'unit',		'nama' => 'No. Unit',   'type' => 'text', 	'width' => 100,  'size' => 10);
    $res[] = array('id' => 'kd_sub',	'nama' => 'Kd. Cust',  	'type' => 'text',   'width' => 100,  'size' => 20);
    $res[] = array('id' => 'nm_sub',	'nama' => 'Nm. Cust', 	'type' => 'text',   'width' => 180,  'size' => 25);
    $res[] = array('id' => 'nota_jt',	'nama' => 'Jatuh Tempo', 'type' => 'date',   'width' => 80,  'size' => 25);
    $res[] = array('id' => 'debet',		'nama' => 'Nilai',   	'type' => 'numeric', 'width' => 130,  'size' => 15, 'null' => true);
    $res[] = array('id' => 'credit',	'nama' => 'Bayar',   	'type' => 'numeric', 'width' => 130,  'size' => 15, 'null' => true);
    $res[] = array('id' => 'saldo',		'nama' => 'Outstanding','type' => 'numeric', 'width' => 130,  'size' => 15, 'null' => true);
    $res[] = array('id' => 'ket',		'nama' => 'Keterangan', 'type' => 'text',   'width' => 200,  'size' => 60);
    
    return $res;
  }
  
  public function bayar()
  {
    $sql = $this->em->addSelect("a.nota, sum(credit) as credit");
    $sql = $this->em->getFrom("acd", 'a');
    $sql = $this->em->getWhere("a.nota is not null");
    $sql = $this->em->andWhere("a.nota != ''");
    $sql = $this->em->andWhere("a.credit > 0");
    $sql = $this->em->andWhere("(a.kd_cabang = 'GR1' or a.kd_cabang = 'GR2')");
    if(!empty($filter->date1->value)){
      $sql = $this->em->andWhere("a.tgl <= '".date('Y-m-d 23:59:59', strtotime($filter->date1->value))."'");
    }
    $sql = $this->em->getGroupBy("a.nota");
	return $sql;
  }
  
  public function setQuery()
  {
    $filter = $this->set->filter;
	$bayar = $this->bayar($filter);
	
    $sql = $this->em->addSelect("
									a.nota, a.unit, a.nm_cabang, a.kd_sub, a.nm_sub, a.ket, a.kdac
									, a.debet, b.credit, (a.debet - COALESCE(b.credit, 0)) as saldo
									, to_char(a.nota_jt, 'dd/mm/yyyy') as nota_jt
								");
    $sql = $this->em->getFrom("acd", 'a');
    $sql = $this->em->addLeftJoin("($bayar)", 'b', array("a.nota = b.nota"));
    $sql = $this->em->getWhere("a.nota is not null");
    $sql = $this->em->andWhere("a.nota != ''");
    $sql = $this->em->andWhere("a.debet > 0");
    $sql = $this->em->andWhere("(a.debet - COALESCE(b.credit, 0)) > 0");
    $sql = $this->em->andWhere("(a.kd_cabang = 'GR1' or a.kd_cabang = 'GR2')");
    if(!empty($filter->date1->value)){
      $sql = $this->em->andWhere("a.tgl <= '".date('Y-m-d 23:59:59', strtotime($filter->date1->value))."'");
    }
    $sql = $this->em->getMultiSort("a.nota_jt asc, a.unit asc");

    return $sql;
  }
  
  public function Index()
  {
    $this->data = $this->set;
    $this->view = "default/report";
  }
  
  public function View()
  {
    $this->data = $this->report->getDef($this->set);
    $this->layout = "report";
  }
}