<?php
/**
 * 管理员管理
 * $Id: account_edit_input.php,v 1.1 2015/10/08 11:18:46 wanggb Exp $
 * @author wanggb
 * @package manager.public_html
 */

// 底层包含
require_once("manager_include.inc");

// 登录检查
require_once("check_login.inc");

// 权限检查
if (!checkAuthority("ADMIN,MANAGER")) {
	// エラー表示
	$layout_pages = array();
	$layout_pages["footer"] = "footer.inc";
	$layout_pages["top"] = "menu.inc";
	$layout_pages["middle"] = "error.inc";
	$message = "权限不足，请联系系统管理员。";
	require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/topmenutopsub_layout.inc");
	exit;
}

// 参数取得
$id = ParamUtil::getRequestString("id");
$status_from = ParamUtil::getRequestString("status_from");
$status_to = ParamUtil::getRequestString("status_to");
$comment = ParamUtil::getRequestString("comment", "");
$comment = date("Y-m-d") . ":" . $comment;

$user_use_coupon_dat = UserUseCouponDat::getById($id);
$user_mst = UserMst::getById($user_use_coupon_dat->user_id);

$coupon_status_obj_from = CouponStatus::getByName($status_from);
$coupon_status_obj_to = CouponStatus::getByName($status_to);

if ($user_use_coupon_dat == null || $user_mst == null || $coupon_status_obj_from == null || $coupon_status_obj_to == null) {
	// エラー表示
	$layout_pages = array();
	$layout_pages["footer"] = "footer.inc";
	$layout_pages["top"] = "menu.inc";
	$layout_pages["middle"] = "error.inc";
	require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/topmenutopsub_layout.inc");
	exit;
}

//状态已经改变的话出错！！
if($user_use_coupon_dat->status != $status_from) {
	// エラー表示
	$layout_pages = array();
	$layout_pages["footer"] = "footer.inc";
	$layout_pages["top"] = "menu.inc";
	$layout_pages["middle"] = "error.inc";
	require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/topmenutopsub_layout.inc");
	exit;
}

if($status_from=="APPLY" && $status_to=="BACK") {
	//退回操作
	$user_use_coupon_dat->status = "BACK";
	$user_use_coupon_dat->comment = $user_use_coupon_dat->comment . "\n" . $comment;
	$user_use_coupon_dat->delete_flg = true;
	$user_use_coupon_dat->save();
	//对应的记录添加1条
	$param = array();
	$param["user_id"] = $user_mst->id;
	$param["coupon_id"] = $user_use_coupon_dat->coupon_id;
	$param["coupon_count"] = 0;
	$param["delete_flg"] = false;
	$user_coupon_list = UserCouponDat::getList($param, "id", "desc", 0, 1);
	if(!empty($user_coupon_list)) {
		$user_coupon_dat = $user_coupon_list[0];
		$user_coupon_dat->coupon_count = 1;
		$user_coupon_dat->save();
	}
}

if($status_from=="APPLY" && $status_to=="CASHING") {
	//申请退款操作
	$user_use_coupon_dat->status = "CASHING";
	$user_use_coupon_dat->comment = $user_use_coupon_dat->comment . "\n" . $comment;
	$user_use_coupon_dat->save();
}

if($status_from=="CASHING" && $status_to=="SUCCESS") {
	//申请退款操作
	$user_use_coupon_dat->status = "SUCCESS";
	$user_use_coupon_dat->comment = $user_use_coupon_dat->comment . "\n" . $comment;
	$user_use_coupon_dat->save();
}

// 页面表示
header("Location:user_use_coupon_list.php?status=".$status_from);
exit;