/**
  * only SR: Fill table sofort_orders_notification, e.g. in case of status-changes
  * @see updateTimeline()
  * @param int $sofortOrdersId - key from table sofort_orders
  * @param object $invoice with complete transactiondata
  * @param string $customerComment (optional)
  * @param string $sellerComment (optional)
  * @return last insert_id
  */
 function insertSofortOrdersNotification($sofortOrdersId, PnagInvoice $PnagInvoice, $customerComment = '', $sellerComment = '')
 {
     if (!$sofortOrdersId || !is_object($PnagInvoice->getTransactionData()) || $PnagInvoice->getTransactionData()->getPaymentMethod() != 'sr') {
         return false;
     }
     $sqlDataArray = array('sofort_orders_id' => $sofortOrdersId, 'items' => serialize($PnagInvoice->getItems()), 'amount' => $PnagInvoice->getAmount(), 'customer_comment' => $customerComment, 'seller_comment' => $sellerComment, 'status_id' => $PnagInvoice->getState(), 'status' => $PnagInvoice->getStatus(), 'status_reason' => $PnagInvoice->getStatusReason(), 'invoice_status' => $PnagInvoice->getStatusOfInvoice(), 'invoice_objection' => $PnagInvoice->getInvoiceObjection());
     xtc_db_query(HelperFunctions::getEscapedInsertInto('sofort_orders_notification', $sqlDataArray));
     return xtc_db_insert_id();
     // fetch and return the last insert id
 }
 /**
  * cart was edited at SOFORT-backend, apply changes in shop
  * @param PnagInvoice $PnagInvoice
  */
 public function editArticlesShop(PnagInvoice $PnagInvoice, $orderNumber)
 {
     $lng = $PnagInvoice->getLanguageCode();
     $newAmount = $PnagInvoice->getAmount();
     $invoiceArticles = $PnagInvoice->getItems();
     foreach ($invoiceArticles as $article) {
         $getTotalItems = explode('|', $article->itemId);
         if (count($getTotalItems) > '1') {
             $sofortIdArray[$getTotalItems[0]] = $getTotalItems[0];
             $sofortArticleArray[$getTotalItems[0]] = $article;
         } else {
             $ordersProductsId = $this->_getOrderProductsId($article->itemId, $orderNumber);
             $sofortIdArray[$ordersProductsId] = $ordersProductsId;
             $sofortArticleArray[$ordersProductsId] = $article;
         }
     }
     $shopProductsQuery = shopDbQuery("SELECT orders_products_id FROM " . TABLE_ORDERS_PRODUCTS . " WHERE orders_id = '" . $orderNumber . "'");
     while ($shopProductsResult = shopDbFetchArray($shopProductsQuery)) {
         $shopArticleArray[] = $shopProductsResult['orders_products_id'];
     }
     $taxLow = 0;
     $taxHigh = 0;
     $subtotal = 0;
     foreach ($shopArticleArray as $shopArticle) {
         if (!in_array($shopArticle, $sofortIdArray)) {
             $this->_sofortRestock($this->_getItemId($shopArticle, $orderNumber), $orderNumber, 0);
             $this->_deleteShopOrderArticle($shopArticle, $PnagInvoice->getStatusReason());
         } else {
             $qty = $sofortArticleArray[$shopArticle]->quantity;
             $price = $sofortArticleArray[$shopArticle]->unitPrice;
             $itemId = $sofortArticleArray[$shopArticle]->itemId;
             $this->_sofortRestock($itemId, $orderNumber, $qty);
             $this->_updateShopOrderArticle($shopArticle, $qty, $price, $PnagInvoice->getStatusReason());
             if ($sofortArticleArray[$shopArticle]->tax == '7.00') {
                 $taxLow += $sofortArticleArray[$shopArticle]->quantity * $sofortArticleArray[$shopArticle]->unitPrice;
             } elseif ($sofortArticleArray[$shopArticle]->tax == '19.00') {
                 $taxHigh += $sofortArticleArray[$shopArticle]->quantity * $sofortArticleArray[$shopArticle]->unitPrice;
             }
             $subtotal += $sofortArticleArray[$shopArticle]->quantity * $sofortArticleArray[$shopArticle]->unitPrice;
         }
     }
     $shipping = 0;
     $discount = array();
     $agio = array();
     foreach ($sofortIdArray as $sofortId) {
         if (!in_array($sofortId, $shopArticleArray)) {
             switch ($sofortId) {
                 case 'shipping':
                     $shipping = $sofortArticleArray[$sofortId]->unitPrice;
                     break;
                 case 'discount':
                     $splitItemId = explode('|', $sofortArticleArray[$sofortId]->itemId);
                     $discountClass = $splitItemId[1];
                     array_push($discount, array('class' => $discountClass, 'value' => $sofortArticleArray[$sofortId]->unitPrice));
                     break;
                 case 'agio':
                     $splitItemId = explode('|', $sofortArticleArray[$sofortId]->itemId);
                     $agioClass = $splitItemId[1];
                     array_push($agio, array('class' => $agioClass, 'value' => $sofortArticleArray[$sofortId]->unitPrice));
                     break;
                 default:
                     $this->_sofortRestock($sofortArticleArray[$sofortId]->itemId, $orderNumber, $sofortArticleArray[$sofortId]->quantity);
                     $this->_insertShopOrderArticle($sofortArticleArray[$sofortId], $orderNumber, $lng);
                     $subtotal += $sofortArticleArray[$sofortId]->quantity * $sofortArticleArray[$sofortId]->unitPrice;
                     break;
             }
             if ($sofortArticleArray[$sofortId]->tax == '7.00') {
                 $taxLow += $sofortArticleArray[$sofortId]->unitPrice * $sofortArticleArray[$sofortId]->quantity;
             } elseif ($sofortArticleArray[$sofortId]->tax == '19.00') {
                 $taxHigh += $sofortArticleArray[$sofortId]->unitPrice * $sofortArticleArray[$sofortId]->quantity;
             }
         }
     }
     $status = $PnagInvoice->getStatusReason();
     $this->_updateShopTotals($taxLow, $taxHigh, $subtotal, $newAmount, $orderNumber, $shipping, $discount, $agio, $status);
 }