<?php

error_reporting(E_ALL);



$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/EcmKpkw/EcmKpkw.php");

    $focus = new EcmKpkw();

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

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

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

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

    $focus = new EcmKpkw();

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

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

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

        ACLController::displayNoAccess(true);

        sugar_cleanup(true);
    }

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

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

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

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

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

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

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

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

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

    // $json = getJSONobj();

    // $pl = $_SESSION['PDF_ECMKPKW']['position_list'];

    // $focus->position_list = $pl;

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

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

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