File: /www/exchange0old/exchange/admin/cron/generating_statement_reports.php
<?php
$folders = explode(DIRECTORY_SEPARATOR, pathinfo(__FILE__, PATHINFO_DIRNAME));
array_pop($folders);
array_pop($folders);
include(implode(DIRECTORY_SEPARATOR, $folders) . '/Bin/init.php');
require_once(ROOT_FOLDER . 'Bin/Security.php');
include(implode(DIRECTORY_SEPARATOR, $folders) . '/Bin/constants.php');
require_once(ROOT_FOLDER . "classes/Storage.php");
require_once(ROOT_FOLDER . "admin/classes/Controller/CStatements.php");
Bin_Config::requireAdminModel('MDebtor', 'MReports', 'MStatements', 'MCEtr', 'MHelpers', 'MRva');
ini_set('memory_limit', '3072M');
ini_set('max_execution_time', '12800');
ini_set('max_input_time', '12800');
$options = getopt('d:o:r:');
$originatorIds = array_filter(explode(',', array_get($options, 'o')));
$toDate = date('Y-m-d', strtotime(array_get($options, 'd', 'today')));
$needRegenerate = (bool)array_get($options, 'r', false);
if (!empty($_REQUEST['originator_ids'])) {
$originatorIds = explode(',', array_get($_REQUEST, 'originator_ids'));
}
if (!empty($_REQUEST['to_date'])) {
$toDate = date('Y-m-d', strtotime(array_get($_REQUEST, 'to_date')));
}
if (!empty($_REQUEST['need_regenerate'])) {
$needRegenerate = true;
}
// Search for the second working day of this month
$rvaModel = new Model_MRva();
$checkDate = new DateTime($toDate);
$monthStartTime = strtotime($checkDate->format('Y-m-d') . ' first day of this month');
$monthEndTime = strtotime('+1 month', $monthStartTime);
$dayAfterFirstWorkingDay = null;
for ($i = $monthStartTime; $i < $monthEndTime; $i += 86400) {
$tempDate = new DateTime(date('Y-m-d', $i));
if (!$rvaModel->isNonBussines($tempDate)) { // if day is working
$lastDayOfThisMonth = date('Y-m-d', strtotime($tempDate->format('Y-m-d') . ' last day of this month'));
if ($tempDate->format('Y-m-d') != $lastDayOfThisMonth) {
$tempDate->add(new DateInterval('P1D'));
}
$dayAfterFirstWorkingDay = $tempDate;
break;
}
}
if ($dayAfterFirstWorkingDay->format('Y-m-d') === $checkDate->format('Y-m-d') or $needRegenerate) {
$toDate = date('Y-m-d', strtotime($toDate . ' -1 month last day of this month'));
$fromDate = date('Y-m-d', strtotime($toDate . ' first day of this month'));
$db = Bin_Db::connect();
$request = Bin_Request::getInstance();
$storage = Storage_Repository::get(Storage_Repository::STATEMENTS);
$selectOriginators = "SELECT im.user_id, dd.type, dd.currency_id
FROM invoice_master im
JOIN debtors_detail dd ON dd.debtor_id = im.debtor_id
WHERE im.root_invoice_id > 0
AND im.invoice_status = 3
UNION
SELECT mt.originator_id as user_id,
IFNULL(dd.type, 'creditor') as `type`,
IFNULL(dd.currency_id, mt.currency_id) as currency_id
FROM manual_transactions mt
LEFT JOIN debtors_detail dd ON dd.debtor_id = mt.debtor_id
WHERE mt.status = 1
AND (mt.debtor_id > 0 OR mt.currency_id > 0)
GROUP BY user_id, type, currency_id
ORDER BY user_id, type, currency_id";
$originatorsList = $db->query($selectOriginators)->getResultArray();
$originators = array();
foreach ($originatorsList as $ol) {
$originators[$ol['user_id']][$ol['type']][] = $ol;
}
foreach ($originators as $originatorKey => $originatorType) {
if (!empty($originatorIds) && !in_array($originatorKey, $originatorIds)) {
continue;
}
foreach ($originatorType as $originatorCurrency) {
$html = '';
$firstCurrency = true;
foreach ($originatorCurrency as $originator) {
$_POST = array(
'user_id' => $originator['user_id'],
'currency_id' => $originator['currency_id'],
'stmt_from' => $fromDate,
'stmt_to' => $toDate,
);
$_GET['type'] = $originator['type'];
$_REQUEST = array_merge($_POST, $_GET);
$request->post($_POST);
$request->query($_GET);
$tempHtml = Controller_CStatements::downloadPdf(true);
$tempHtml = substr($tempHtml, 0, strpos($tempHtml, '</body>'));
if (!$firstCurrency) {
$tempHtml = substr($tempHtml, strpos($tempHtml, '<body>') + 6);
}
$html .= $tempHtml . '<div style="page-break-after: always;"></div>';
$firstCurrency = false;
}
$html .= '</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper("letter", "portrait");
$dompdf->render();
$file = $dompdf->output();
$dirPath = "/" . date('Y', strtotime($toDate)) . '/' . date('m', strtotime($toDate)) . '/' . date('d', strtotime($toDate)) . '/';
if (!is_dir($dirPath)) {
mkdir($dirPath, 0777, TRUE);
}
$fileName = $dirPath . time() . "_" . $originator['user_id'] . "_" . $originator['type'] . ".pdf";
$storage->set($fileName, $file);
$db->builder()->values(array(
'user_id' => $originator['user_id'],
'type' => $originator['type'],
'date' => $toDate,
'path' => $fileName,
'created_at' => date('Y-m-d H:i:s'),
))->insert('statement_reports')->execute();
}
}
echo 'PDF files have been generated';
} else {
echo 'The script does not work for ' . $checkDate->format('Y-m-d');
}