<?php
if (!defined('sugarEntry') || !sugarEntry)
    die('Not A Valid Entry Point');
if (!$current_user->is_admin) {
    ACLController::displayNoAccess(true);
    sugar_die();
}
global $sugar_config, $current_user, $mod_strings;

$db = $GLOBALS ['db'];
$rows = $db->query('SELECT id FROM operating_values WHERE name = "InvoiceNumberFormat" AND value0="Nistandardowe"');
$rr = $db->fetchByAssoc($rows);
$tmpid = $rr['id'];



if (isset($_POST['header1']) || isset($_POST['footer1'])) {
    $inf = null;
    if ($_POST['invoice_number_format'] == $tmpid) {
        $q = "insert into operating_values (name,value0) VALUES ('InvoiceNumberFormat', '" . trim($_POST['custom_number_format']) . "')";
        $db->query($q);
        $q = 'SELECT id FROM operating_values WHERE name = "InvoiceNumberFormat" AND value0= "' . trim($_POST['custom_number_format']) . '"';
        $tmp = $db->query($q);
        $rr = $db->fetchByAssoc($tmp);
        $inf = $rr['id'];
    } else {
        $inf = $_POST['invoice_number_format'];
    }
    $queryUpdate = "UPDATE operating_values SET "
            . "value1 = '" . $inf . "',"
            . "text0 = '" . $_POST['header1'] . "',"
            . "text1 = '" . $_POST['footer1'] . "' "
            . " WHERE id= '" . $_POST['docselect'] . "' AND"
            . " value4= '" . $_POST['languageselect'] . "'";
    $error = $db->query($queryUpdate);
}
// insert into operating_values (name,value0) VALUES ('InvoiceNumberFormat', '')
// insert into operating_values (name,value0) VALUES ('InvoiceNumberFormat', '{n}/{Y}')
// insert into operating_values (name,value0) VALUES ('InvoiceNumberFormat', '{n}/{U}/{Y}')
// insert into operating_values (name,value0) VALUES ('InvoiceNumberFormat', '{n}/{m}/{y}')
// insert into operating_values (name,value0) VALUES ('InvoiceNumberFormat', 'Nistandardowe')

$query = 'SELECT id, value0 FROM operating_values WHERE name = "InvoiceNumberFormat" ORDER BY value0';
$rows = $db->query($query);
$numberFormatList = array();
while ($rr = $db->fetchByAssoc($rows)) {
    $numberFormatList[$rr['id']] = $rr['value0'];
}
$data = null;
if (!isset($_POST['languageselect'])) {
    $querySelect = 'SELECT id id,value0 name FROM operating_values WHERE operating_values.name="headersDocuments" AND module_name="EcmSysInfos" AND value4="pl_pl" ORDER BY value3';
    $rows = $db->query($querySelect);
    $data = array();
    while ($rr = $db->fetchByAssoc($rows)) {
        $data[$rr['id']] = $rr['name'];
    }
} else {
    $querySelect = 'SELECT id id,value0 name FROM operating_values WHERE operating_values.name="headersDocuments" AND module_name="EcmSysInfos" AND value4="' . $_POST['languageselect'] . '" ORDER BY value3';
    $rows = $db->query($querySelect);
    $data = array();
    while ($rr = $db->fetchByAssoc($rows)) {
        $data[$rr['id']] = $rr['name'];
    }
}
$format = array();
$smarty = new Sugar_Smarty();


$smarty->assign('MOD', $mod_strings);
$smarty->assign('dataLanguage', $sugar_config['languages']);
$smarty->assign('data', $data);
$smarty->assign('InvoiceNumberFormat', $numberFormatList);
if (isset($_POST['docselect'])) {
    $smarty->assign('selected', $_POST['docselect']);
}
if (isset($_POST['docselect'])) {
    $smarty->assign('selectedLanguage', $_POST['languageselect']);
}
echo $smarty->display('modules/EcmSysInfos/tpl/headersDocuments.tpl');
