<?php
include_once('../system/lib/config.php');

$sys = new System();

// 変数宣言部
$rtn			= array(PARAM_RESULT => array());
$xml			= NO_STRING;
$cardHolderId	= $sys -> getDataPost(PARAM_CARD_HOLDER_ID);
$startDate		= $sys -> getDataPost(PARAM_FROM);
$endDate		= $sys -> getDataPost(PARAM_TO);

		try {

// 			if($cardHolderId == NO_STRING) {
// 				throw new Exception(NO_STRING);
// 			}

			$xml =	'<ViewStatement><Cardholderid>' . $cardHolderId . '</Cardholderid><StartDate>' . $startDate . '</StartDate><EndDate>' . $endDate . '</EndDate><ViewStyle>Y</ViewStyle>ss</ViewStatement>';
// 			$xml =	'<ViewStatement><Cardholderid>400000542107</Cardholderid><StartDate>2015-07-01</StartDate><EndDate>2015-07-31</EndDate><ViewStyle>Y</ViewStyle>ss</ViewStatement>';

			// 送信データの生成
			$post = array('Username'			=> VAR_CARD_API_USER_NAME
							, 'Password'		=> VAR_CARD_API_PASSWORD
							, 'APISigniture'	=> 'ViewStatement'
							, 'MessageID'		=> $sys -> getSequenceNo(CARD_MESSAGE_ID)
							, 'Data'			=> $xml
			);

			// ポスト送信
			$url = VAR_CARD_API_URL;

			$client = new SoapClient($url);
			$result = $client -> Process($post);

			$result = simplexml_load_string($result -> ProcessResult);
			$result = get_object_vars($result);

			// エラーがないか確認する
			if($sys -> getColumnData($result, 'ErrorCode') == '0000') {	// 取得成功

				$result = $sys -> getColumnData($result, 'ViewStatement');

				if(is_object($result)) {

					$result = get_object_vars($result);
					$result = $sys -> getColumnData($result, 'cardholderstatementdetails');

					if(is_object($result)) {

						$result = get_object_vars($result);
						$result = $sys -> getColumnData($result, 'cardpan');

						if(is_object($result)) {

							$result = get_object_vars($result);
							$result = $sys -> getColumnData($result, 'cardaccount');
							// データが存在する場合
							if($sys -> isLoopData($result)) {

								// データの数だけループ
								foreach($result as $row) {

									if(is_object($row)) {
										$transaction = get_object_vars($row -> transactionlist -> transaction);
// print_r($transaction);
										$rtn[PARAM_RESULT][] = array(PARAM_DATE				=> $transaction['date']
																	, 'description'			=> $transaction['description']
																	, PARAM_AMOUNT			=> $transaction['amount']
																	, PARAM_FEE				=> $transaction['fee']
																	, PARAM_BALANCE			=> $transaction['availablebalance']
																	, 'AuthNum'				=> $transaction['AuthNum']
																	, 'rspcode'				=> $transaction['rspcode']
																	, 'termcountry'			=> $transaction['termcountry']
																	, 'termnamelocation'	=> $transaction['termnamelocation']
																	, 'issuerfee'			=> $transaction['issuerfee']
																);
									}
								}
							}

						}
					}
				}

			} else {													// 取得失敗
			}

		} catch(Exception $e) {
		}

		echo json_encode($rtn);
?>
