<?php
//共通
require_once '../../base/common.php';
require_once '../../util/combo.php';

$logger = new logger(basename(__FILE__));
$logger->info(" ===af9032x_search_start=== ");

$response["status"] = "OK";

$listutil = null;

// 再ページ読込用
$header = Array();
foreach($_POST as $key => $val) {
  $header[$key] = $val;
}
$logger->info("header", $header);

if($header["cunm"] == null) {
  $getCunm = $valutil->getCunm($header["cucd"], $header["cuscd"], '0','0');
  $header["cunm"] = $getCunm[0]["cunm"];
}

//コンボボックス作成
$listutil = null;
$listutil = new getListUtil();
$assign_var["listShflg"] = $cbo_shflg;
$assign_var["listCuspkf"] = $cbo_cuspkf;
$assign_var["listCuclcls"] = $cbo_cuclcls;
$assign_var["listCupcls"] = $cbo_cupcls032;
$assign_var["listCupycls"] = $cbo_cupycls; 
$assign_var["listSpcat"] = $cbo_spcat; 
$assign_var["listSplnk"] = $cbo_splnk; 

//POPUP
$assign_var["hasPopChgcd"]="1";
$assign_var["hasPopCucd"] = "1";
$assign_var["hasPopCuscd"] = "1";
$assign_var["hasPopKtktcd"]="1";
$assign_var["hasPopCshcd"] = "1";
$assign_var["hasPopScls90"]="1";
$assign_var["hasPopAf9032x1"]="1";
$assign_var["hasPopAf032m"] = "1";

try {
  $assign_var["noeventClassHeader"] = "noevent";
  $assign_var["noeventClassBody"]   = "";

  // sqlの条件を設定する
  $logger->info("header_cucd",$header["cucd"]);
  $logger->info("header_cuscd",$header["cuscd"]);
  $whereParams["cucd"] = $header["cucd"];
  $whereParams["cuscd"] = $header["cuscd"];

  // SQL実行
  $sql = file_get_contents("./sql/search.sql");
  $logger->info("whereParams",$whereParams);
  $result = $dbutil->select($sql, $whereParams);

  //前回請求締日、前回残高を取得する
  //前回残高の取得式は入金入力参考?
  $whereParams = array();
  $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $whereParams["cucd"] = $header["cucd"];
  $sql = file_get_contents("./sql/searchAus75.sql");
  $result2 = $dbutil->select($sql, $whereParams);
  $logger->info('searchAus75_sql', $sql);


  if ($result == null) {
    // 1:新規, 2:修正, 3:削除
    $header["smode"] = '1';
    $assign_var["isNoData"] = true;
    $dataList = Array(
      "spprio" => "0"
    );
    $assign_var["data"] = $dataList;
  } else {
    $dataList = $result[0];
    $dataList["cdat"] = $result2[0]["cdat"];
    $dataList["bal"] = $result2[0]["bal"];
    
    if ($result[0]["cuclcls"] == "0" ) {
      $dataList["cdat"] = null;
      $dataList["bal"] = null;
    }else{
      $dataList["cdat"] = $result2[0]["cdat"];
      $dataList["bal"] = $result2[0]["bal"];
    }
    
    // 1:新規, 2:修正
    $header["smode"] = '2';
    $assign_var["isNoData"] = false;
    $assign_var["data"] = $dataList;
  }

  $logger->info('head', $header);
  $assign_var["header"] = $header;

} catch(userException $ue) {
  $dbutil->rollback();
  $assign_var["hasError"] = true;
  $assign_var["errorMsg"] = $ue->getMessage();
} catch(Exception $e) {
  $dbutil->rollback();

  $assign_var["hasError"] = true;
  $assign_var["errorMsg"] = $e->getMessage();

}

//$logger->info("smarty", $assign_var);

//Smartyへアサイン
$smarty->assign($assign_var);

$smarty->display("index");

$logger->info("=== af9032x_search_end ===");
?>
