<?php

error_reporting(E_PARSE | E_WARNING);

$json = getJSONobj();
$pll = array();
$i = 0;
while (isset($_POST['p_' . $i])) {
    $pll[] = $json->decode(htmlspecialchars_decode($_POST['p_' . $i]));
    $_POST['p_' . $i] = '';
    $i++;
}
$_POST = $json->decode(htmlspecialchars_decode($_POST['otherFormData']));
$_POST['position_list'] = $pll;

if (isset($_REQUEST['record']) && $_REQUEST['record'] != '' && $_REQUEST['cache'] != "fromJava") {
    require_once("modules/Accounts/Account.php");

    $focus = new Account();

    $method = (isset($_REQUEST['method']) && $_REQUEST['method'] != '') ? $_REQUEST['method'] : 'D';

    $focus->getPDF($_REQUEST['record'], $method, null, @$_REQUEST['type']);
} else

if ($_REQUEST['cache'] == "fromJava" && $_GET['from'] != "EcmInvoiceOuts") {
    $_SESSION['PDF_ECMINVOICEOUTS'] = $_POST;
} else

if ($_GET['from'] == "EcmInvoiceOuts") {
    require_once("modules/EcmInvoiceOuts/EcmInvoiceOut.php");

    $focus = new EcmInvoiceOut();

    if (isset($_SESSION['PDF_ECMINVOICEOUTS']['record']) && $_SESSION['PDF_ECMINVOICEOUTS']['record'] != '') {

        $focus->retrieve($_SESSION['PDF_ECMINVOICEOUTS']['record']);
    }

    if (!$focus->ACLAccess('Save')) {

        ACLController::displayNoAccess(true);

        sugar_cleanup(true);
    }

    foreach ($focus->column_fields as $field) {

        if (isset($_SESSION['PDF_ECMINVOICEOUTS'][$field])) {

            $value = $_SESSION['PDF_ECMINVOICEOUTS'][$field];

            $focus->$field = $value;
        }
    }

    foreach ($focus->additional_column_fields as $field) {

        if (isset($_SESSION['PDF_ECMINVOICEOUTS'][$field])) {

            $value = $_SESSION['PDF_ECMINVOICEOUTS'][$field];

            $focus->$field = $value;
        }
    }

    if (isset($_SESSION['PDF_ECMINVOICEOUTS']['to_is_vat_free']) && $_SESSION['PDF_ECMINVOICEOUTS']['to_is_vat_free'])
        $focus->to_is_vat_free = 1;
    else
        $focus->to_is_vat_free = 0;

    $json = getJSONobj();

    $pl = $_SESSION['PDF_ECMINVOICEOUTS']['position_list'];

    $focus->position_list = $pl;

    $focus->document_no = $_SESSION['PDF_ECMINVOICEOUTS']['document_no'];

    $focus->wz_id = $_SESSION['PDF_ECMINVOICEOUTS']['out_id'];

    $focus->ecmpaymentcondition_text = EcmInvoiceOut::getTranslation('EcmPaymentConditions', $focus->ecmpaymentcondition_id, $focus->ecmlanguage);
    //die();
    $focus->getPDF();
}