/** * Gets ID of Klarna part canceled state * * @return Array $statusArray Status */ function piKlarnaGetAllStatusIds() { $statusArray=Array( 'pending' => piKlarnaGetPendingStatusId(), 'accepted' => piKlarnaGetAcceptedStatusId(), 'declined' => piKlarnaGetDeclinedStatusId(), 'partSent' => piKlarnaGetPartReservedStatusId(), 'completeSent' => piKlarnaGetCompleteReservedStatusId(), 'partCanceled' => piKlarnaGetPartCanceledStatusId(), 'completeCanceled' => piKlarnaGetCompleteCancelStatusId(), 'partReturned' => piKlarnaGetPartReturnStatusId(), 'completeReturned' => piKlarnaGetCompleteReturnStatusId(), ); return $statusArray; }
/** * Cancels articles from order. Delivers a JSON String with article details * * @see templates/backend/plugins/PigmbhKlarnaPayment/index.php * @throws KlarnaException */ public function cancelArticlesAction() { $this->View()->setTemplate(); $piKlarnaArticleNr = explode(";", $this->Request()->articlenr); $piKlarnaQuantity = explode(";", $this->Request()->anzahl); $piKlarnaOrderNumber = $this->Request()->myordernumber; $piKlarnaArticleCounter = 0; $piKlarnaFlag = true; $piKlarnaArticle = array(); $piKlarnaNewOpen = array(); $piKlarnaNewCanceled = array(); $piKlarnaDelivered = array(); $piKlarnaTotalAmount = array(); $piKlarnaError = array(); $piKlarnaReturn = array(); $k = piKlarnaCreateKlarnaInstance($piKlarnaOrderNumber); $sql = "SELECT transactionid FROM Pi_klarna_payment_order_data WHERE order_number = ?"; $rno = Shopware()->Db()->fetchOne($sql, array($piKlarnaOrderNumber)); for ($i = 0; $i < sizeof($piKlarnaArticleNr); $i++) { $sql = "SELECT * FROM Pi_klarna_payment_order_detail WHERE ordernumber = ? AND bestell_nr = ?"; $piKlarnaArticle[$i] = Shopware()->Db()->fetchRow($sql, array($piKlarnaOrderNumber, $piKlarnaArticleNr[$i])); $piKlarnaNewOpen[$i] = $piKlarnaArticle[$i]['offen'] - $piKlarnaQuantity[$i]; $piKlarnaNewCanceled[$i] = $piKlarnaArticle[$i]['storniert'] + $piKlarnaQuantity[$i]; $piKlarnaDelivered[$i] = $piKlarnaArticle[$i]['bestellt'] - $piKlarnaNewCanceled[$i]; $piKlarnaTotalAmount[$i] = $piKlarnaNewOpen[$i] * $piKlarnaArticle[$i]['einzelpreis']; $piKlarnaTotalAmount[$i]+=$piKlarnaArticle[$i]['geliefert'] * $piKlarnaArticle[$i]['einzelpreis']; if ($piKlarnaArticle[$i]['bestellt'] - $piKlarnaQuantity[$i] != 0) { $piKlarnaFlag = false; } if ($piKlarnaQuantity[$i] > 0) { $piKlarnaArticleCounter++; } } $sql = "SELECT Count(*) FROM Pi_klarna_payment_order_detail WHERE ordernumber = ?"; $piKlarnaQuantityartikel = Shopware()->Db()->fetchOne($sql, array($piKlarnaOrderNumber)); if ($piKlarnaQuantityartikel != $piKlarnaArticleCounter) { $piKlarnaFlag = false; } if ($piKlarnaFlag == true) { $piKlarnaError['error'] = false; $piKlarnaError['errormessage'] = "Komplett"; try { $k->cancelReservation($rno); for ($i = 0; $i < sizeof($piKlarnaArticleNr); $i++) { $piKlarnaCompleteCancelStatusId=piKlarnaGetCompleteCancelStatusId(); $piKlarnaReserverationCanceledStatusId=piKlarnaGetReserverationCanceledStatusId(); $sql = "UPDATE Pi_klarna_payment_order_detail SET offen = ?, storniert = ?, gesamtpreis = ?, bezahlstatus = ?, versandstatus = ? WHERE ordernumber = ? AND bestell_nr = ?"; Shopware()->Db()->query($sql, array( (int)$piKlarnaNewOpen[$i], (int)$piKlarnaNewCanceled[$i], (double)$piKlarnaTotalAmount[$i], (int)$piKlarnaReserverationCanceledStatusId, (int)$piKlarnaCompleteCancelStatusId, $piKlarnaOrderNumber, $piKlarnaArticleNr[$i] )); $sql = "SELECT (bestellt - storniert) FROM Pi_klarna_payment_order_detail WHERE ordernumber = ? AND bestell_nr = ?"; $piKlarnaOrderquantity = Shopware()->Db()->fetchOne($sql, array($piKlarnaOrderNumber, $piKlarnaArticleNr[$i])); $sql = "UPDATE s_order_details SET quantity = ? WHERE ordernumber = ? AND articleordernumber = ?"; Shopware()->Db()->query($sql, array((int)$piKlarnaOrderquantity, $piKlarnaOrderNumber, $piKlarnaArticleNr[$i])); } $piKlarnaHistoryEvent = "<b class=\"red\">Bestellung vollständig storniert</b>"; $sql = $sql = "INSERT INTO Pi_klarna_payment_history (ordernumber, event) VALUES (?, ?)"; Shopware()->Db()->query($sql, array($piKlarnaOrderNumber, $piKlarnaHistoryEvent)); $piKlarnaCompleteCancelStatusId=piKlarnaGetCompleteCancelStatusId(); $piKlarnaReserverationCanceledStatusId=piKlarnaGetReserverationCanceledStatusId(); $sql = "UPDATE s_order SET status = ?, cleared = ?, invoice_shipping = 0, invoice_shipping_net = 0, dispatchID = 0 WHERE ordernumber = ?"; Shopware()->Db()->query($sql, array( (int)$piKlarnaCompleteCancelStatusId, (int)$piKlarnaReserverationCanceledStatusId, $piKlarnaOrderNumber )); piKlarnaCalculateNewAmount($piKlarnaOrderNumber); echo json_encode(array('articlename' => $piKlarnaFlag, 'k_return' => $piKlarnaError, 'komplett' => true)); } catch (Exception $e) { $piKlarnaError['error'] = true; $piKlarnaError['errormessage'] = $e->getMessage() . " (#" . $e->getCode() . ")"; echo json_encode(array('articlename' => $piKlarnaFlag, 'k_return' => $piKlarnaError, 'komplett' => true)); } } else { $piKlarnaNewRestAmount = 0; $piKlarnaDeliveredPrice = 0; $oldQuantity = 0; $sql = "SELECT geliefert,einzelpreis FROM Pi_klarna_payment_order_detail WHERE ordernumber = ?"; $piKlarnaDelivered = Shopware()->Db()->fetchAll($sql, array($piKlarnaOrderNumber)); for ($i = 0; $i < sizeof($piKlarnaDelivered); $i++) { $piKlarnaDeliveredPrice+=$piKlarnaDelivered[$i]['geliefert'] * $piKlarnaDelivered[$i]['einzelpreis']; } for ($i = 0; $i < sizeof($piKlarnaArticleNr); $i++) { $sql = "SELECT anzahl FROM Pi_klarna_payment_order_detail WHERE ordernumber = ? AND bestell_nr = ?"; $oldQuantity = Shopware()->Db()->fetchOne($sql, array($piKlarnaOrderNumber, $piKlarnaArticleNr[$i])); $sql = "SELECT einzelpreis FROM Pi_klarna_payment_order_detail WHERE ordernumber = ? AND bestell_nr= ?"; $price = Shopware()->Db()->fetchOne($sql, array($piKlarnaOrderNumber, $piKlarnaArticleNr[$i])); $piKlarnaNewAmount = $price * $piKlarnaQuantity[$i]; $piKlarnaNewRestAmount+=$piKlarnaNewAmount; } $sql = "SELECT invoice_amount FROM s_order WHERE ordernumber = ?"; $piKlarnaInvoiceAmount = Shopware()->Db()->fetchOne($sql, array($piKlarnaOrderNumber)); if ($piKlarnaNewRestAmount == $piKlarnaInvoiceAmount - $piKlarnaDeliveredPrice) { try { $k->cancelReservation($rno); for ($i = 0; $i < sizeof($piKlarnaArticleNr); $i++) { if ($piKlarnaArticle[$i]['name'] == 'Versandkosten') { $sql = "UPDATE s_order SET invoice_shipping = 0, invoice_shipping_net = 0, dispatchID = 0 WHERE ordernumber = ?"; Shopware()->Db()->query($sql, array($piKlarnaOrderNumber)); } $sql = " SELECT geliefert FROM Pi_klarna_payment_order_detail WHERE ordernumber = ? AND bestell_nr = ?"; $piKlarnaDelivered = Shopware()->Db()->fetchOne($sql, array($piKlarnaOrderNumber, $piKlarnaArticleNr[$i])); // $piKlarnaArticlename = str_replace("'", "\'", $piKlarnaArticle[$i]['name']); $piKlarnaArticlename = $piKlarnaArticle[$i]['name']; $piKlarnaHistoryEvent = "<span class=\"red\">Artikel storniert</span>"; $sql = "INSERT INTO Pi_klarna_payment_history ( ordernumber, event, name, bestellnr, anzahl ) VALUES ( ?, ?, ?, ?, ? )"; Shopware()->Db()->query($sql, array( $piKlarnaOrderNumber, $piKlarnaHistoryEvent, $piKlarnaArticlename, $piKlarnaArticleNr[$i], $piKlarnaQuantity[$i] )); if ($piKlarnaDelivered > 0) { $piKlarnaCompleteReservedStatusId=piKlarnaGetCompleteReservedStatusId(); $sql = "UPDATE Pi_klarna_payment_order_detail SET offen = ?, storniert = ?, gesamtpreis = ?, bezahlstatus = ?, versandstatus = 7 WHERE ordernumber = ? AND bestell_nr = ?"; Shopware()->Db()->query($sql, array( (int)$piKlarnaNewOpen[$i], (int)$piKlarnaNewCanceled[$i], (double)$piKlarnaTotalAmount[$i], (int)$piKlarnaCompleteReservedStatusId, $piKlarnaOrderNumber, $piKlarnaArticleNr[$i] )); } else { $piKlarnaCompleteCancelStatusId=piKlarnaGetCompleteCancelStatusId(); $piKlarnaReserverationCanceledStatusId=piKlarnaGetReserverationCanceledStatusId(); $sql = "UPDATE Pi_klarna_payment_order_detail SET offen = ?, storniert = ?, gesamtpreis = ?, bezahlstatus = ?, versandstatus = ? WHERE ordernumber = ? AND bestell_nr = ?"; Shopware()->Db()->query($sql, array( (int)$piKlarnaNewOpen[$i], (int)$piKlarnaNewCanceled[$i], (double)$piKlarnaTotalAmount[$i], (int)$piKlarnaReserverationCanceledStatusId, (int)$piKlarnaCompleteCancelStatusId, $piKlarnaOrderNumber, $piKlarnaArticleNr[$i] )); $sql = "SELECT (bestellt - storniert) FROM Pi_klarna_payment_order_detail WHERE ordernumber = ? AND bestell_nr = ?"; $piKlarnaOrderquantity = Shopware()->Db()->fetchOne($sql, array($piKlarnaOrderNumber, $piKlarnaArticleNr[$i])); $sql = "UPDATE s_order_details SET quantity = ? WHERE ordernumber = ? AND articleordernumber = ?"; Shopware()->Db()->query($sql, array((int)$piKlarnaOrderquantity, $piKlarnaOrderNumber, $piKlarnaArticleNr[$i])); } $piKlarnaReturn['error'] = false; $piKlarnaReturn['errormessage'] = "Komplett"; $sql = "SELECT (bestellt - storniert - retourniert) FROM Pi_klarna_payment_order_detail WHERE ordernumber = ? AND bestell_nr = ?"; $piKlarnaOrderquantity = Shopware()->Db()->fetchOne($sql, array($piKlarnaOrderNumber, $piKlarnaArticleNr[$i])); $sql = "UPDATE s_order_details SET quantity = ? WHERE ordernumber = ? AND articleordernumber = ?"; Shopware()->Db()->query($sql, array((int)$piKlarnaOrderquantity, $piKlarnaOrderNumber, $piKlarnaArticleNr[$i])); } $piKlarnaTotalFlag = true; $sql ="SELECT geliefert FROM Pi_klarna_payment_order_detail WHERE ordernumber = ?"; $piKlarnaDelivered = Shopware()->Db()->fetchAll($sql, array($piKlarnaOrderNumber)); for ($i = 0; $i < sizeof($piKlarnaDelivered); $i++) { if ($piKlarnaDelivered[$i]['geliefert'] > 0) { $piKlarnaTotalFlag = false; } } $piKlarnaAcceptedStatusId=piKlarnaGetAcceptedStatusId(); if ($piKlarnaTotalFlag == false) { $sql = "UPDATE s_order SET status = 7, cleared = ? WHERE ordernumber = ?"; Shopware()->Db()->query($sql, array($piKlarnaAcceptedStatusId, $piKlarnaOrderNumber)); $piKlarnaHistoryEvent = "<b class=\"green\">Bestellung vollständig versendet</b>"; $sql = "INSERT INTO Pi_klarna_payment_history (ordernumber, event) VALUES (?, ?)"; Shopware()->Db()->query($sql, array($piKlarnaOrderNumber, $piKlarnaHistoryEvent)); } else { $piKlarnaCompleteCancelStatusId=piKlarnaGetCompleteCancelStatusId(); $piKlarnaReserverationCanceledStatusId=piKlarnaGetReserverationCanceledStatusId(); $sql = "UPDATE s_order SET status = ?, cleared = ? WHERE ordernumber = ?"; Shopware()->Db()->query($sql, array((int)$piKlarnaCompleteCancelStatusId, (int)$piKlarnaReserverationCanceledStatusId, $piKlarnaOrderNumber)); $piKlarnaHistoryEvent = "<b class=\"red\">Bestellung vollständig storniert</b>"; $sql = "INSERT INTO Pi_klarna_payment_history (ordernumber, event) VALUES (?, ?)"; Shopware()->Db()->query($sql, array($piKlarnaOrderNumber, $piKlarnaHistoryEvent)); } piKlarnaCalculateNewAmount($piKlarnaOrderNumber); echo json_encode(array( 'articlename' => $piKlarnaFlag, 'k_return' => $piKlarnaReturn, 'komplett' => true, 'geliefert' => $piKlarnaDelivered, 'mytotalflag' => $piKlarnaTotalFlag )); } catch (Exception $e) { $piKlarnaReturn['error'] = true; $piKlarnaReturn['errormessage'] = $e->getMessage() . " (#" . $e->getCode() . ")"; echo json_encode(array( 'articlename' => $piKlarnaFlag, 'k_return' => $piKlarnaReturn, 'komplett' => true )); } } else { $piKlarnaReturn = piKlarnaChangeReservation($piKlarnaNewRestAmount, $rno, 'deletearticle'); if ($piKlarnaReturn['error'] == false) { for ($i = 0; $i < sizeof($piKlarnaArticleNr); $i++) { $sql = "UPDATE Pi_klarna_payment_order_detail SET offen = ?, storniert = ?, gesamtpreis = ? WHERE ordernumber = ? AND bestell_nr = ?"; Shopware()->Db()->query($sql, array( (int)$piKlarnaNewOpen[$i], (int)$piKlarnaNewCanceled[$i], (double)$piKlarnaTotalAmount[$i], $piKlarnaOrderNumber, $piKlarnaArticleNr[$i] )); $sql = "SELECT (bestellt - storniert) FROM Pi_klarna_payment_order_detail WHERE ordernumber = ? AND bestell_nr = ?"; $piKlarnaOrderquantity = Shopware()->Db()->fetchOne($sql, array($piKlarnaOrderNumber, $piKlarnaArticleNr[$i])); $sql = "UPDATE s_order_details SET quantity= ? WHERE ordernumber = ? AND articleordernumber = ?"; $piKlarnaOrderquantity = Shopware()->Db()->query($sql, array( (int)$piKlarnaOrderquantity, $piKlarnaOrderNumber, $piKlarnaArticleNr[$i])); if ($piKlarnaQuantity[$i] > 0) { if ($piKlarnaArticle[$i]['name'] == 'Versandkosten') { $sql = "UPDATE s_order SET invoice_shipping = 0, invoice_shipping_net = 0, dispatchID = 0 WHERE ordernumber = ?"; Shopware()->Db()->query($sql, array($piKlarnaOrderNumber)); } //$piKlarnaArticlename = str_replace("'", "\'", $piKlarnaArticle[$i]['name']); if(!get_magic_quotes_gpc()) { $piKlarnaArticlename = str_replace("'", "\'", $piKlarnaArticle[$i]['name']); } $piKlarnaHistoryEvent = "<span class=\"red\">Artikel storniert</span>"; $sql = "INSERT INTO Pi_klarna_payment_history ( ordernumber, event, name, bestellnr, anzahl ) VALUES ( ?, ?, ?, ?, ? )"; Shopware()->Db()->query($sql, array( $piKlarnaOrderNumber, $piKlarnaHistoryEvent, $piKlarnaArticlename, $piKlarnaArticleNr[$i], $piKlarnaQuantity[$i] )); if ($piKlarnaNewOpen[$i] == 0 && $piKlarnaDelivered[$i] == 0) { $piKlarnaCompleteCancelStatusId=piKlarnaGetCompleteCancelStatusId(); $piKlarnaReserverationCanceledStatusId=piKlarnaGetReserverationCanceledStatusId(); $sql = "UPDATE Pi_klarna_payment_order_detail SET bezahlstatus= ?, versandstatus= ? WHERE ordernumber = ? AND bestell_nr = ?"; Shopware()->Db()->query($sql, array( (int)$piKlarnaReserverationCanceledStatusId, (int)$piKlarnaCompleteCancelStatusId, $piKlarnaOrderNumber, $piKlarnaArticleNr[$i] )); } elseif ($piKlarnaNewOpen[$i] == 0 && $piKlarnaDelivered[$i] > 0) { $piKlarnaCompleteReservedStatusId=piKlarnaGetCompleteReservedStatusId(); $sql = "UPDATE Pi_klarna_payment_order_detail SET bezahlstatus = ?, versandstatus = 7 WHERE ordernumber = ? AND bestell_nr = ?"; Shopware()->Db()->query($sql, array( (int)$piKlarnaCompleteReservedStatusId, $piKlarnaOrderNumber, $piKlarnaArticleNr[$i] )); } elseif ($piKlarnaNewOpen[$i] > 0 && $piKlarnaDelivered[$i] == 0) { $piKlarnaPartCanceledStatusId=piKlarnaGetPartCanceledStatusId(); $sql = "UPDATE Pi_klarna_payment_order_detail SET bezahlstatus = 18, versandstatus = ? WHERE ordernumber = ? AND bestell_nr = ?"; Shopware()->Db()->query($sql, array( (int)$piKlarnaPartCanceledStatusId, $piKlarnaOrderNumber, $piKlarnaArticleNr[$i] )); } } } } echo json_encode(array( "total" => $piKlarnaFlag, "k_return" => $piKlarnaReturn, "neuerrestpreis" => $piKlarnaNewRestAmount, "rechnungspreis" => $piKlarnaInvoiceAmount, "geliefertpreis" => $piKlarnaDeliveredPrice )); } piKlarnaCalculateNewAmount($piKlarnaOrderNumber); } }