diff --git a/src/QUI/ERP/Accounting/Invoice/RestApi/Provider.php b/src/QUI/ERP/Accounting/Invoice/RestApi/Provider.php index 70df18877f5422d7f997977e8c33674ab74fa609..08c4be2a4755c11b674f7c5ce3e4d1fab27fefc9 100644 --- a/src/QUI/ERP/Accounting/Invoice/RestApi/Provider.php +++ b/src/QUI/ERP/Accounting/Invoice/RestApi/Provider.php @@ -196,27 +196,46 @@ public function createInvoice(RequestInterface $Request, ResponseInterface $Resp // Payment method $Payments = QUI\ERP\Accounting\Payments\Payments::getInstance(); - $payments = $Payments->getPayments([ - 'where' => [ - 'payment_type' => QUI\ERP\Accounting\Payments\Methods\Invoice\Payment::class - ] - ]); + $Customer = $InvoiceDraft->getCustomer(); + $Payment = false; - // Set default payment method - if (!empty($payments)) { - $DefaultPayment = $payments[0]; - $InvoiceDraft->setAttribute('payment_method', $DefaultPayment->getId()); + if ($Customer) { + $customerDefaultPaymentId = $Customer->getAttribute('quiqqer.erp.standard.payment'); + + if (!empty($customerDefaultPaymentId)) { + try { + $Payment = $Payments->getPayment($customerDefaultPaymentId); + } catch (\Exception $Exception) { + QUI\System\Log::writeDebugException($Exception); + } + } } - if (!empty($invoiceData['payment_method'])) { + if (!$Payment && !empty($invoiceData['payment_method'])) { try { $Payment = $Payments->getPayment((int)$invoiceData['payment_method']); - $InvoiceDraft->setAttribute('payment_method', $Payment->getId()); } catch (\Exception $Exception) { QUI\System\Log::writeException($Exception); } } + // Set default payment method + if (!$Payment) { + $payments = $Payments->getPayments([ + 'where' => [ + 'payment_type' => QUI\ERP\Accounting\Payments\Methods\Invoice\Payment::class + ] + ]); + + if (!empty($payments)) { + $Payment = $payments[0]; + } + } + + if ($Payment) { + $InvoiceDraft->setAttribute('payment_method', $Payment->getId()); + } + // Currency if (!empty($invoiceData['currency']) && CurrencyHandler::existCurrency($invoiceData['currency'])) { $InvoiceDraft->setCurrency($invoiceData['currency']); @@ -249,29 +268,40 @@ public function createInvoice(RequestInterface $Request, ResponseInterface $Resp $InvoiceDraft->setAttribute('additional_invoice_text', $invoiceText); } - // Articles + // Articles - Existing products + $ProductList = new QUI\ERP\Products\Product\ProductList(); + $ProductList->setUser($InvoiceDraft->getCustomer()); + foreach ($invoiceData['articles'] as $article) { - $Article = null; + if (empty($article['quiqqerProductId'])) { + continue; + } - if (!empty($article['quiqqerProductId'])) { - try { - $Product = QUI\ERP\Products\Handler\Products::getProduct((int)$article['quiqqerProductId']); - $UniqueProduct = $Product->createUniqueProduct($InvoiceDraft->getCustomer()); - $UniqueProduct->setQuantity((float)$article['quantity']); + try { + $Product = QUI\ERP\Products\Handler\Products::getProduct((int)$article['quiqqerProductId']); - $UniqueProduct->recalculation(); + $UniqueProduct = $Product->createUniqueProduct($InvoiceDraft->getCustomer()); + $UniqueProduct->setQuantity((float)$article['quantity']); - $Article = $UniqueProduct->toArticle(null, false); - } catch (\Exception $Exception) { - QUI\System\Log::writeException($Exception); - } + $ProductList->addProduct($UniqueProduct); + } catch (\Exception $Exception) { + QUI\System\Log::writeException($Exception); } + } - if (!$Article) { - $Article = new QUI\ERP\Accounting\Article($article); + $ProductList->recalculate(); + + foreach ($ProductList->getProducts() as $Product) { + $InvoiceDraft->addArticle($Product->toArticle()); + } + + // Articles - Custom + foreach ($invoiceData['articles'] as $article) { + if (!empty($article['quiqqerProductId'])) { + continue; } - $Article->calc(); + $Article = new QUI\ERP\Accounting\Article($article); $InvoiceDraft->addArticle($Article); }