<?php

if (!defined('sugarEntry') || !sugarEntry)
    die('-1');

if (!$_POST ['job'] || $_POST ['job'] == '')
    die('-1');

switch ($_POST ['job']) {
    case 'getCodeHeader':
        getCodeHeader($_POST['id'], $_POST['language']);
        break;
    case 'getCodeFooter':
        getCodeFooter($_POST['id'], $_POST['language']);
        break;
    case 'getInvoiceNumberFormat':
        getInvoiceNumberFormat($_POST['id']);
        break;
    case 'getDocListForLanguage':
        getDocListForLanguage($_POST['language']);
        break;
}

function getCodeHeader($id, $language = 'pl_pl') {
    $db = $GLOBALS ['db'];
    $q = 'SELECT text0 FROM operating_values WHERE operating_values.name="headersDocuments" AND module_name="EcmSysInfos" AND id="' . $id . '"';
    $rs = $db->query($q);
    $row = $db->fetchByAssoc($rs);
    print html_entity_decode($row ['text0']);
}

function getCodeFooter($id, $language = 'pl_pl') {
    $db = $GLOBALS ['db'];
    $q = 'SELECT text1 FROM operating_values WHERE operating_values.name="headersDocuments" AND module_name="EcmSysInfos" AND id="' . $id . '"';
    $rs = $db->query($q);
    $row = $db->fetchByAssoc($rs);
    print html_entity_decode($row ['text1']);
}

function getInvoiceNumberFormat($id) {
    $db = $GLOBALS ['db'];
    $q = 'SELECT value1 FROM operating_values WHERE operating_values.name="headersDocuments" AND module_name="EcmSysInfos" AND id="' . $id . '"';
    $rs = $db->query($q);
    $row = $db->fetchByAssoc($rs);
    print json_encode($row ['value1']);
}

function getDocListForLanguage($language) {
    $db = $GLOBALS ['db'];
    $querySelect = 'SELECT id id,value0 name FROM operating_values WHERE operating_values.name="headersDocuments" AND module_name="EcmSysInfos" AND value4="' . $language . '" ORDER BY value3';
    $rows = $db->query($querySelect);
    $data = array();
    while ($rr = $db->fetchByAssoc($rows)) {
        $data[$rr['id']] = $rr['name'];
    }
    print json_encode($data);
}

?>