<?php
/**
 * @copyright ONE/DEPO Project
 * @license   PHP License 3.01 <http://www.php.net/license/3_01.txt>
 */


require_once 'Pager.php';

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * [[機能説明]]
 *
 * @package     [[package名]]
 * @author      Your Name <mail@example.com>
 * @access      public
 */
class Admin_Moneymanage_Paymentlist
{
    var $month;       //支払いの〆 1～12
    var $money;       //テーブル操作
    var $paymentlist; //出力用の変数

    var $title;
    var $pagename;
    
    // 入力パラメータ
    var $cnt;         // 総登録件数
    var $pageID;      // ページ番号
    
	var $Pager_perPage;
	var $Pager_delta;
	var $Pager_append;
	var $Pager_clearIfVoid;
	var $Pager_urlVar;
	var $Pager_useSessions;
	var $Pager_closeSession;
	var $Pager_mode;
	var $Pager_prevImg;
	var $Pager_nextImg;
	var $Pager_altPage;
	var $Pager_altPrev;
	var $Pager_altNext;
	var $Pager_altFirst;
	var $Pager_altLast;
	var $Pager_firstPagePre;
	var $Pager_firstPageText;
	var $Pager_firstPagePost;
	var $Pager_lastPagePre;
	var $Pager_lastPageText;
	var $Pager_lastPagePost;
	var $Pager_separator;
	var $Pager_spacesBeforeSeparator;
	var $Pager_spacesAfterSeparator;

	function getPager()
	{
        // Pager設定
        $pageparams = array(
        'totalItems'    => $this->cnt,
        'perPage'       => $this->Pager_perPage,       // １ページの行数
        'delta'         => $this->Pager_delta,       // 表示ページから、前後に表示するページ数
        'append'        => $this->Pager_append,
        'currentPage'   => $this->pageID,
        'clearIfVoid'   => $this->Pager_clearIfVoid,
        'urlVar'        => $this->Pager_urlVar,
        'useSessions'   => $this->Pager_useSessions,
        'closeSession'  => $this->Pager_closeSession,
        'mode'          => $this->Pager_mode,
        'prevImg'       => $this->Pager_prevImg,
        'nextImg'       => $this->Pager_nextImg,
        'altPage'       => $this->Pager_altPage,
        'altPrev'       => $this->Pager_altPrev,
        'altNext'       => $this->Pager_altNext,
        'altFirst'      => $this->Pager_altFirst,
        'altLast'       => $this->Pager_altLast,
        'firstPagePre'  => $this->Pager_firstPagePre,
        'firstPageText' => $this->Pager_firstPageText,
        'firstPagePost' => $this->Pager_firstPagePost,
        'lastPagePre'   => $this->Pager_lastPagePre,
        'lastPageText'  => $this->Pager_lastPageText,
        'lastPagePost'  => $this->Pager_lastPagePost,
        'separator'     => $this->Pager_separator,
        'spacesBeforeSeparator' => $this->Pager_spacesBeforeSeparator,
        'spacesAfterSeparator'  => $this->Pager_spacesAfterSeparator,
        );

        // Pager出力
        $pager = & Pager::factory($pageparams);
        $this->pagedata = $pager->getPageData();
        $link =	$pager->getLinks();
        return $link["all"];
	}

    /**
     * [[機能説明]]
     *
     * @access  public
     */
    function execute()
    {
        if(!isset($this->pageID)) $this->pageID = 1;
        
        $term = $this -> getSQLWhere();
        $list = $this -> money -> getPaymentList($term['termStart'], $term['termEnd']);
        
        //会員コードごとに支払額をまとめる
        $listByMemberCd = array();
        foreach($list as $item){
            $listByMemberCd[$item['membercd']]['sellername'] = $item['lastnm'].$item['firstnm'];
            if(!isset($listByMemberCd[$item["membercd"]]["sales"]))       $listByMemberCd[$item["membercd"]]["sales"]       = 0;
            if(!isset($listByMemberCd[$item["membercd"]]["commission"]))  $listByMemberCd[$item["membercd"]]["commission"]  = 0;
            if(!isset($listByMemberCd[$item["membercd"]]["refundprice"])) $listByMemberCd[$item["membercd"]]["refundprice"] = 0;
            
            if($item['typediv'] == 1 || $item['typediv'] == 2 || $item['typediv'] == 3){
                $listByMemberCd[$item['membercd']]['sales'] += $item['price'];
            } elseif($item['typediv'] == 4 || $item['typediv'] == 5 || $item['typediv'] == 10 || $item['typediv'] == 11) {
                $listByMemberCd[$item['membercd']]['commission'] -= $item['price'];
            } elseif($item['typediv'] == 7 || $item['typediv'] == 8){ // 
                $listByMemberCd[$item['membercd']]['refundprice'] -= $item['price'];
            } else {
                echo $item['typediv'];
            }
            
        }

        $this -> cnt = count($listByMemberCd);
        $sliceStart = ($this->Pager_perPage*($this->pageID-1)-1 < 0) ? 0 : $this->Pager_perPage*($this->pageID-1);
        $this -> paymentlist = array_slice($listByMemberCd, $sliceStart, $this->Pager_perPage);

        $log =& LogFactory::getLog();
        $log->debug("Admin_Moneymanage_Paymentlistが実行されました", "Admin_Moneymanage_Paymentlist#execute");

        return 'success';
    }
    
    function getSQLWhere(){
    	//〆日による条件づけ
        $now = getdate(time());
        $year = $now["year"];
        if(intval($this->month) > $now["mon"]) $year--;

        $where = "";
        if(ADMIN_PAYMENTLIST_CLOSING == 0){
            $stamp = mktime(0, 0, 0, $this -> month, 1, $year);
            $dateBefore = date('Y-m-d', $stamp);
            $dateAfter  = date('Y-m-d', strtotime("next month", $stamp));

        }else{
            $stamp = mktime(0, 0, 0, $this -> month, ADMIN_PAYMENTLIST_CLOSING, $year);
            $dateBefore = date('Y-m-d', strtotime("last month", $stamp));
            $dateAfter  = date('Y-m-d', $stamp);
        }
        
        $ret['termStart'] = $dateBefore;
        $ret['termEnd']   = $dateAfter;
        
        return $ret;
    }
    
    function account_format($var)
    {
        $number = $var['number'];
        if($number < 0){
            return "▲".number_format(abs($number));
        } else {
            return number_format($number);
        }
    }
}
?>
