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

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

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

$listutil = null;

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

//コンボボックス作成
$listutil = null;
$listutil = new getListUtil();
$assign_var["listCukbn"] = $listutil->getlistAfm90("P");
$assign_var["listCusei"] = $cbo_cusei;
$assign_var["listCuspflg"] = $cbo_cuspflg;
$assign_var["listCuosflg"] = $cbo_cuosflg;
$assign_var["listStaxcls"] = $cbo_staxcls;
$assign_var["listCutax"] = $cbo_cutax;
$assign_var["listCustdc"] = $cbo_custdc;
$assign_var["listCutaxc"] = $cbo_cutaxc;
$assign_var["listCustdc1"] = $cbo_custdc1;
$assign_var["listCutaxc1"] = $cbo_cutaxc1;

$assign_var["listUpchke"] = $cbo_upchke;
$assign_var["listCusacls"] = $cbo_cusacls;
$assign_var["listCucat"] = $cbo_cucat;
$assign_var["listSamtchk"] = $cbo_samtchk;
$assign_var["listCuhcls"] = $cbo_cuhcls;
$assign_var["listCucpcls"] = $cbo_cucpcls;
$assign_var["listAsflg"] = $cbo_asflg;
$assign_var["listCutaxdiv"] = $cbo_cutaxdiv;
$assign_var["listCutaxmxtrt"] = $cbo_cutaxmxtrt;
$assign_var["listCutaxdiff"] = $cbo_cutaxdiff;

$assign_var["listCudenp1"] = $listutil->getlistAfm90("9");
$assign_var["listSalpcls"] = $cbo_salpcls;
$assign_var["listCstpcls"] = $cbo_cstpcls;
$assign_var["listDmzkflg"] = $cbo_dmzkflg;
$assign_var["listDtcor"] = $cbo_dtcor;
$assign_var["listDyflg"] = $cbo_dyflg;
$assign_var["listCudpcls"] = $cbo_cudpcls;
$assign_var["listDhfflg"] = $cbo_dhfflg;
$assign_var["listDgsiflg"] = $cbo_dgsiflg;
$assign_var["listSpnoflg"] = $cbo_spnoflg;
$assign_var["listStcorflg"] = $cbo_stcorflg;

$assign_var["listCudenp2"] = $listutil->getlistAfm90("9");
$assign_var["listSalpcls2"] = $cbo_salpcls2;
$assign_var["listCstpcls2"] = $cbo_cstpcls2;
$assign_var["listDmzkflg2"] = $cbo_dmzkflg2;
$assign_var["listDtcor2"] = $cbo_dtcor2;
$assign_var["listDyflg2"] = $cbo_dyflg2;
$assign_var["listCudpcls2"] = $cbo_cudpcls2;
$assign_var["listDhfflg2"] = $cbo_dhfflg2;
$assign_var["listDgsiflg2"] = $cbo_dgsiflg2;
$assign_var["listSpnoflg2"] = $cbo_spnoflg2;
$assign_var["listStcorflg2"] = $cbo_stcorflg2;

$assign_var["listCuflcls"] = $cbo_cuflcls;
$assign_var["listJcls"] = $cbo_jcls;
$assign_var["listCucuc"] = $cbo_cucuc;

$assign_var["listCuptax"] = $cbo_cuptax;
$assign_var["listCupcls"] = $cbo_cupcls;
$assign_var["listCupycls"] = $cbo_cupycls;

//popup用コンボボックス
$assign_var["listShflg"] = $cbo_shflg;
$assign_var["listSpcat"] = $cbo_spcat;
$assign_var["listSplnk"] = $cbo_splnk;


//POPUP
$assign_var["hasPopCucd"]="1";
$assign_var["hasPopChgcd"]="1";
$assign_var["hasPopScls90"]="1";

$assign_var["hasPopAf03m"] = "1";

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

  // sqlの条件を設定する
  $logger->info("header_cucd",$header["cucd"]);
  $whereParams["cucd"] = $header["cucd"];
  // SQL実行
  $sql = file_get_contents("./sql/search.sql");
  $logger->info("whereParams",$whereParams);
  $result = $dbutil->select($sql, $whereParams);

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

  //店舗数のカウント
  $result3 = cntAfm032($dbutil,$header);
  $logger->info("result3",$result3);

  //店舗分類取得
  $whereParams = array();
  $whereParams["cucd"] = $header["cucd"];
  $sql = file_get_contents("./sql/cntSpcat.sql");
  $result4 = $dbutil->select($sql, $whereParams);
  if($result4 != null) {
    $assign_var["dataList2"] = $result4;
  }


  if ($result == null) {
    // 1:新規, 2:修正, 3:削除
    $header["smode"] = '1';
    $assign_var["isNoData"] = true;
    $dataList = Array(
      "staxcls" => "0",
      "cutaxc" => "0",
      "upchke" => "2",
      "vdlmt" => "1",
      "samtchk" => "0",
      "pcflg" => "0",
      "cupmn" => "0",
      "salpcls" => "0",
      "cstpcls" => "0",
      "dmzkflg" => "0",
      "dtcor" => "0",
      "dyflg" => "0",
      "dhfflg" => "0",
      "dgsiflg" => "0",
      "spnoflg" => "1",
      "stcorflg" => "1",
      "salpcls2" => "0",
      "cstpcls2" => "0",
      "dmzkflg2" => "0",
      "dtcor2" => "0",
      "dyflg2" => "0",
      "dhfflg2" => "0",
      "dgsiflg2" => "0",
      "spnoflg2" => "1",
      "stcorflg2" => "1",
      "cusacls" => "1",
      "curtcls" => "1",
      "cuhcls" => "0",
      "cusei" => "1",
      "cuspflg" => "0",
      "cuosflg" => "0",
      "cucat" => "1",
      "cucpcls" => "0",
      "aslmt" => "1",
      "asflg" => "0",
      "undkbn" => "0",
      "cuptax" => "0",
      "cuflcls" => null,
      "cuflamt" => null,
      "afm032cnt" => '0'
    );
    $assign_var["data"] = $dataList;
  } else {
    $dataList = $result[0];
    $dataList["cdat"] = $result2[0]["cdat"];
    $dataList["bal"] = $result2[0]["bal"];
    $dataList["afm032cnt"] = $result3["afm032cnt"];

    // 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("=== af904x_search_end ===");
?>
