Newer
Older
<?php
/**
* This file contains package_quiqqer_invoice_ajax_invoices_temporary_save
*/
/**
* Saves the temporary invoice
*
* @param integer|string $invoiceId
* @param string $data - JSON data
*/
QUI::$Ajax->registerFunction(
'package_quiqqer_invoice_ajax_invoices_temporary_save',
function ($invoiceId, $data) {
$Invoice = QUI\ERP\Accounting\Invoice\Utils\Invoice::getTemporaryInvoiceByString($invoiceId);
if (empty($data['customer_id'])) {
$data['invoice_address_id'] = '';
$data['invoice_address'] = '';
}
if (!empty($data['addressDelivery'])) {
$delivery = $data['addressDelivery'];
try {
$Invoice->setDeliveryAddress($delivery);
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
} else {
$Invoice->removeDeliveryAddress();
$Invoice->clearArticles();
if (isset($data['articles'])) {
$Invoice->importArticles($data['articles']);
unset($data['articles']);
}
if (isset($data['priceFactors'])) {
try {
$List = new QUI\ERP\Accounting\PriceFactors\FactorList($data['priceFactors']);
$Invoice->getArticles()->importPriceFactors($List);
} catch (QUI\Exception $Exception) {
}
}
if (isset($data['currency'])) {
$Invoice->setCurrency($data['currency']);
}
if (!empty($data['currencyRate'])) {
$Currency = $Invoice->getCurrency();
$Currency->setExchangeRate(floatval($data['currencyRate']));
$Invoice->setAttribute('invoice_address', false); // needed because of address reset
// Attached customer files
$Invoice->clearCustomerFiles();
if (!empty($data['attached_customer_files'])) {
if (is_string($data['attached_customer_files'])) {
$customerFiles = json_decode($data['attached_customer_files'], true);
} else {
$customerFiles = $data['attached_customer_files'];
}
foreach ($customerFiles as $entry) {
try {
$Invoice->addCustomerFile($entry['hash'], $entry['options']);
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
}

Henning Leutz
committed
['invoiceId', 'data'],