<?php

/*
 * LMS version 1.11-git
 *
 *  (C) Copyright 2001-2022 LMS Developers
 *
 *  Please, see the doc/AUTHORS for more information about authors!
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License Version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 *  USA.
 *
 *  $Id$
 */

if (isset($_GET['proforma'])) {
    $proforma = 1;
} elseif (isset($_GET['page']) || !empty($_POST)) {
    $SESSION->restore('ilproforma', $proforma);
} else {
    $proforma = 0;
}
$SESSION->save('ilproforma', $proforma);

$layout['pagetitle'] = $proforma ? trans('Pro Forma Invoice List') : trans('Invoices List');
$SESSION->add_history_entry();

$SESSION->restore('ilm', $marks);

if (isset($_POST['marks'])) {
    foreach ($_POST['marks'] as $id => $mark) {
        $marks[$id] = $mark;
    }
}
$SESSION->save('ilm', $marks);

if (isset($_POST['search'])) {
    $s = $_POST['search'];
} else {
    $SESSION->restore('ils', $s);
}
if (!isset($s)) {
     $year=date("Y", time());
     $month=date("m", time());
     $s = $year.'/'.$month;
}
$SESSION->save('ils', $s);

if (isset($_GET['o'])) {
    $o = $_GET['o'];
} else {
    $SESSION->restore('ilo', $o);
}
$SESSION->save('ilo', $o);

if (isset($_POST['cat'])) {
    $c = $_POST['cat'];
} else {
    $SESSION->restore('ilc', $c);
}

if (isset($_POST['customer'])) {
    $cid = intval($_POST['customer']);
} else {
    if (!empty($_GET['customer'])) {
        $cid = intval($_GET['customer']);
    } else {
        $SESSION->restore('ilcu', $cid);
    }
}

$SESSION->save('ilcu', $cid);

if (!isset($c)) {
    $c="month";
}
$SESSION->save('ilc', $c);

if (isset($_POST['numberplanid'])) {
    if ($_POST['numberplanid'] == 'all') {
        $np = array();
    } else {
        $np = $_POST['numberplanid'];
    }
} else {
    $SESSION->restore('ilnp', $np);
}
$SESSION->save('ilnp', $np);

if (isset($_POST['divisionid'])) {
    if (empty($_POST['divisionid'])) {
        $div = 0;
    } else {
        $div = $_POST['divisionid'];
    }
} else {
    $SESSION->restore('ildiv', $div);
}
$SESSION->save('ildiv', $div);

if (isset($_POST['search'])) {
    $h = isset($_POST['hideclosed']);
} elseif (($h = $SESSION->get('ilh')) === null) {
    $h = ConfigHelper::checkConfig('invoices.hide_closed');
}
$SESSION->save('ilh', $h);

if (isset($_POST['group'])) {
    if ($_POST['group'] == 'all') {
        $g = array();
    } else {
        $g = $_POST['group'];
    }
    $ge = $_POST['groupexclude'] ?? null;
} else {
    $SESSION->restore('ilg', $g);
    $SESSION->restore('ilge', $ge);
}
$SESSION->save('ilg', $g);
$SESSION->save('ilge', $ge);

if (isset($_POST['search'])) {
    $stm = $_POST['sendtoemail'];
} else {
    $SESSION->restore('ilstm', $stm);
}
$SESSION->save('ilstm', $stm);

if (isset($_POST['search'])) {
    $ksefStatus = $_POST['ksefstatus'];
} else {
    $SESSION->restore('ilksefstatus', $ksefStatus);
}
$SESSION->save('ilksefstatus', $ksefStatus);

if (isset($_POST['search'])) {
    $sp = isset($_POST['splitpayment']);
} else {
    $SESSION->restore('ilsp', $sp);
}
$SESSION->save('ilsp', $sp);

if (isset($_POST['search'])) {
    $wr = isset($_POST['withreceipt']);
} else {
    $SESSION->restore('ilwr', $wr);
}
$SESSION->save('ilwr', $wr);

if (isset($_POST['search'])) {
    $ts = isset($_POST['telecomservice']);
} else {
    $SESSION->restore('ilts', $ts);
}
$SESSION->save('ilts', $ts);

if (isset($_POST['search'])) {
    $re = isset($_POST['relatedentity']);
} else {
    $SESSION->restore('ilre', $re);
}
$SESSION->save('ilre', $re);

if ($c == 'cdate' && $s && preg_match('/^[0-9]{4}\/[0-9]{2}\/[0-9]{2}$/', $s)) {
    [$year, $month, $day] = explode('/', $s);
    $s = mktime(0, 0, 0, $month, $day, $year);
} elseif ($c == 'month' && $s && preg_match('/^[0-9]{4}\/[0-9]{2}$/', $s)) {
    [$year, $month] = explode('/', $s);
    $s = mktime(0, 0, 0, $month, 1, $year);
} elseif ($c == 'year' && $s && preg_match('/^[0-9]{4}$/', $s)) {
    $year = intval($s);
    $s = mktime(0, 0, 0, 1, 1, $year);
}

$total = intval($LMS->GetInvoiceList(array('search' => $s, 'cat' => $c, 'group' => $g, 'exclude'=> $ge,
    'numberplan' => $np, 'division' => $div, 'hideclosed' => $h, 'order' => $o, 'proforma' => $proforma,
    'splitpayment' => $sp, 'withreceipt' => $wr, 'telecomservice' => $ts, 'relatedentity' => $re,
    'sendtoemail' => $stm, 'ksefstatus' => $ksefStatus, 'count' => true, 'customer' => $cid)));

$limit = intval(ConfigHelper::getConfig('phpui.invoicelist_pagelimit', 100));
if (isset($_GET['page'])) {
    $page = $_GET['page'];
} elseif ($SESSION->is_set('ilpage')) {
    $SESSION->restore('ilpage', $page);
} else {
    $page = ceil($total / $limit);
}
if (empty($page) || $page > ceil($total / $limit)) {
    $page = 1;
}
$page = intval($page);
$offset = ($page - 1) * $limit;

$SESSION->save('ilpage', $page);

$invoicelist = $LMS->GetInvoiceList(array('search' => $s, 'cat' => $c, 'group' => $g, 'exclude'=> $ge,
    'numberplan' => $np, 'division' => $div, 'hideclosed' => $h, 'order' => $o, 'limit' => $limit, 'offset' => $offset,
    'proforma' => $proforma, 'splitpayment' => $sp, 'withreceipt' => $wr, 'telecomservice' => $ts, 'relatedentity' => $re,
    'sendtoemail' => $stm, 'ksefstatus' => $ksefStatus, 'count' => false, 'customer' => $cid));

$pagination = LMSPaginationFactory::getPagination($page, $total, $limit, ConfigHelper::checkConfig('phpui.short_pagescroller'));

$SESSION->restore('ilc', $listdata['cat']);
$SESSION->restore('ils', $listdata['search']);
$SESSION->restore('ilg', $listdata['group']);
$SESSION->restore('ilge', $listdata['groupexclude']);
$SESSION->restore('ilstm', $listdata['sendtoemail']);
$SESSION->restore('ilksefstatus', $listdata['ksefstatus']);
$SESSION->restore('ilnp', $listdata['numberplanid']);
$SESSION->restore('ildiv', $listdata['divisionid']);
$SESSION->restore('ilh', $listdata['hideclosed']);
$SESSION->restore('ilsp', $listdata['splitpayment']);
$SESSION->restore('ilwr', $listdata['withreceipt']);
$SESSION->restore('ilts', $listdata['telecomservice']);
$SESSION->restore('ilre', $listdata['relatedentity']);
$SESSION->restore('ilcu', $listdata['customer']);

$listdata['total'] = $total;
$listdata['order'] = $invoicelist['order'];
$listdata['direction'] = $invoicelist['direction'];

unset($invoicelist['order']);
unset($invoicelist['direction']);

if ($invoice = $SESSION->get('invoiceprint', true)) {
        $SMARTY->assign('invoice', $invoice);
        $SESSION->remove('invoiceprint', true);
}

$hook_data = $LMS->ExecuteHook(
    'invoicelist_before_display',
    array(
        'invoicelist' => $invoicelist,
    )
);
$invoicelist = $hook_data['invoicelist'];

$SMARTY->assign('listdata', $listdata);
$SMARTY->assign('pagination', $pagination);
$SMARTY->assign('marks', $marks);
$SMARTY->assign('grouplist', $LMS->CustomergroupGetAll());

if ($proforma) {
    $doctypes = array(DOC_INVOICE_PRO);
} else {
    $doctypes = array(DOC_INVOICE, DOC_CNOTE);
}
$SMARTY->assign('numberplans', $LMS->GetNumberPlans(array(
    'doctype' => $doctypes,
)));

$SMARTY->assign('proforma', $proforma);
$SMARTY->assign('divisions', $LMS->GetDivisions());
$SMARTY->assign('invoicelist', $invoicelist);
$SMARTY->display('invoice/invoicelist.html');
