/** * Finaly save order and send order confirmation to customer * @access public */ public function sSaveOrder() { $this->sComment = stripslashes($this->sComment); $this->sComment = stripcslashes($this->sComment); $this->sShippingData["AmountNumeric"] = $this->sShippingData["AmountNumeric"] ? $this->sShippingData["AmountNumeric"] : "0"; if ($this->isTransactionExist($this->bookingId)) { return false; } // Insert basic-data of the order $orderNumber = $this->sGetOrderNumber(); $this->sOrderNumber = $orderNumber; if (!$this->sShippingcostsNumeric) { $this->sShippingcostsNumeric = "0"; } if (!$this->sBasketData["AmountWithTaxNumeric"]) { $this->sBasketData["AmountWithTaxNumeric"] = $this->sBasketData["AmountNumeric"]; } if ($this->isTaxFree($this->sSYSTEM->sUSERGROUPDATA["tax"], $this->sSYSTEM->sUSERGROUPDATA["id"])) { $net = "1"; } else { $net = "0"; } if ($this->dispatchId) { $dispatchId = $this->dispatchId; } else { $dispatchId = "0"; } $this->sBasketData["AmountNetNumeric"] = round($this->sBasketData["AmountNetNumeric"], 2); if (empty($this->sSYSTEM->sCurrency["currency"])) { $this->sSYSTEM->sCurrency["currency"] = "EUR"; } if (empty($this->sSYSTEM->sCurrency["factor"])) { $this->sSYSTEM->sCurrency["factor"] = "1"; } $shop = Shopware()->Shop(); $mainShop = $shop->getMain() !== null ? $shop->getMain() : $shop; $taxfree = "0"; if (!empty($this->sNet)) { // Complete net delivery $net = "1"; $this->sBasketData["AmountWithTaxNumeric"] = $this->sBasketData["AmountNetNumeric"]; $this->sShippingcostsNumeric = $this->sShippingcostsNumericNet; $taxfree = "1"; } $partner = $this->getPartnerCode($this->sUserData["additional"]["user"]["affiliate"]); $orderParams = array('ordernumber' => $orderNumber, 'userID' => $this->sUserData["additional"]["user"]["id"], 'invoice_amount' => $this->sBasketData["AmountWithTaxNumeric"], 'invoice_amount_net' => $this->sBasketData["AmountNetNumeric"], 'invoice_shipping' => floatval($this->sShippingcostsNumeric), 'invoice_shipping_net' => floatval($this->sShippingcostsNumericNet), 'ordertime' => new Zend_Db_Expr('NOW()'), 'status' => 0, 'cleared' => 17, 'paymentID' => $this->sUserData["additional"]["user"]["paymentID"], 'transactionID' => (string) $this->bookingId, 'customercomment' => $this->sComment, 'net' => $net, 'taxfree' => $taxfree, 'partnerID' => (string) $partner, 'temporaryID' => (string) $this->uniqueID, 'referer' => (string) $this->getSession()->offsetGet('sReferer'), 'language' => $shop->getId(), 'dispatchID' => $dispatchId, 'currency' => $this->sSYSTEM->sCurrency["currency"], 'currencyFactor' => $this->sSYSTEM->sCurrency["factor"], 'subshopID' => $mainShop->getId(), 'remote_addr' => (string) $_SERVER['REMOTE_ADDR'], 'deviceType' => $this->deviceType); $orderParams = $this->eventManager->filter('Shopware_Modules_Order_SaveOrder_FilterParams', $orderParams, array('subject' => $this)); try { $this->db->beginTransaction(); $affectedRows = $this->db->insert('s_order', $orderParams); $orderID = $this->db->lastInsertId(); $this->db->commit(); } catch (Exception $e) { $this->db->rollBack(); throw new Enlight_Exception("Shopware Order Fatal-Error {$_SERVER["HTTP_HOST"]} :" . $e->getMessage(), 0, $e); } if (!$affectedRows || !$orderID) { throw new Enlight_Exception("Shopware Order Fatal-Error {$_SERVER["HTTP_HOST"]} : No rows affected or no order id created.", 0); } try { $paymentData = Shopware()->Modules()->Admin()->sGetPaymentMeanById($this->sUserData["additional"]["user"]["paymentID"], Shopware()->Modules()->Admin()->sGetUserData()); $paymentClass = Shopware()->Modules()->Admin()->sInitiatePaymentClass($paymentData); if ($paymentClass instanceof \ShopwarePlugin\PaymentMethods\Components\BasePaymentMethod) { $paymentClass->createPaymentInstance($orderID, $this->sUserData["additional"]["user"]["id"], $this->sUserData["additional"]["user"]["paymentID"]); } } catch (\Exception $e) { //Payment method code failure } $attributeSql = "INSERT INTO s_order_attributes (orderID, attribute1, attribute2, attribute3, attribute4, attribute5, attribute6)\n VALUES (\n " . $orderID . ",\n " . $this->db->quote((string) $this->o_attr_1) . ",\n " . $this->db->quote((string) $this->o_attr_2) . ",\n " . $this->db->quote((string) $this->o_attr_3) . ",\n " . $this->db->quote((string) $this->o_attr_4) . ",\n " . $this->db->quote((string) $this->o_attr_5) . ",\n " . $this->db->quote((string) $this->o_attr_6) . "\n )"; $attributeSql = $this->eventManager->filter('Shopware_Modules_Order_SaveOrderAttributes_FilterSQL', $attributeSql, array('subject' => $this)); $this->db->executeUpdate($attributeSql); $attributes = $this->getOrderAttributes($orderID); unset($attributes['id']); unset($attributes['orderID']); $position = 0; foreach ($this->sBasketData["content"] as $key => $basketRow) { $position++; $basketRow = $this->formatBasketRow($basketRow); $preparedQuery = "\n INSERT INTO s_order_details\n (orderID,\n ordernumber,\n articleID,\n articleordernumber,\n price,\n quantity,\n name,\n status,\n releasedate,\n modus,\n esdarticle,\n taxID,\n tax_rate,\n ean,\n unit,\n pack_unit\n )\n VALUES (%d, %s, %d, %s, %f, %d, %s, %d, %s, %d, %d, %d, %f, %s, %s, %s)\n "; $sql = sprintf($preparedQuery, $orderID, $this->db->quote((string) $orderNumber), $basketRow["articleID"], $this->db->quote((string) $basketRow["ordernumber"]), $basketRow["priceNumeric"], $basketRow["quantity"], $this->db->quote((string) $basketRow["articlename"]), 0, $this->db->quote((string) $basketRow["releasedate"]), $basketRow["modus"], $basketRow["esdarticle"], $basketRow["taxID"], $basketRow["tax_rate"], $this->db->quote((string) $basketRow["ean"]), $this->db->quote((string) $basketRow["itemUnit"]), $this->db->quote((string) $basketRow["packunit"])); $sql = $this->eventManager->filter('Shopware_Modules_Order_SaveOrder_FilterDetailsSQL', $sql, array('subject' => $this, 'row' => $basketRow, 'user' => $this->sUserData, 'order' => array("id" => $orderID, "number" => $orderNumber))); // Check for individual voucher - code if ($basketRow["modus"] == 2) { //reserve the basket voucher for the current user. $this->reserveVoucher($basketRow["ordernumber"], $this->sUserData["additional"]["user"]["id"], $basketRow["articleID"]); } if ($basketRow["esdarticle"]) { $esdOrder = true; } try { $this->db->executeUpdate($sql); $orderdetailsID = $this->db->lastInsertId(); } catch (Exception $e) { throw new Enlight_Exception("Shopware Order Fatal-Error {$_SERVER["HTTP_HOST"]} :" . $e->getMessage(), 0, $e); } $this->sBasketData['content'][$key]['orderDetailId'] = $orderdetailsID; //new attribute tables $attributeSql = "INSERT INTO s_order_details_attributes (detailID, attribute1, attribute2, attribute3, attribute4, attribute5, attribute6)\n VALUES (" . $orderdetailsID . "," . $this->db->quote((string) $basketRow["ob_attr1"]) . "," . $this->db->quote((string) $basketRow["ob_attr2"]) . "," . $this->db->quote((string) $basketRow["ob_attr3"]) . "," . $this->db->quote((string) $basketRow["ob_attr4"]) . "," . $this->db->quote((string) $basketRow["ob_attr5"]) . "," . $this->db->quote((string) $basketRow["ob_attr6"]) . ")"; $attributeSql = $this->eventManager->filter('Shopware_Modules_Order_SaveOrderAttributes_FilterDetailsSQL', $attributeSql, array('subject' => $this, 'row' => $basketRow, 'user' => $this->sUserData, 'order' => array("id" => $orderID, "number" => $orderNumber))); $this->db->executeUpdate($attributeSql); $detailAttributes = $this->getOrderDetailAttributes($orderdetailsID); unset($detailAttributes['id']); unset($detailAttributes['detailID']); $this->sBasketData['content'][$key]['attributes'] = $detailAttributes; // Update sales and stock if ($basketRow["priceNumeric"] >= 0) { $this->refreshOrderedVariant($basketRow["ordernumber"], $basketRow["quantity"]); } // For esd-articles, assign serial number if needed // Check if this article is esd-only (check in variants, too -> later) if ($basketRow["esdarticle"]) { $basketRow = $this->handleESDOrder($basketRow, $orderID, $orderdetailsID); // Add assignedSerials to basketcontent if (!empty($basketRow['assignedSerials'])) { $this->sBasketData["content"][$key]['serials'] = $basketRow['assignedSerials']; } } } // For every article in basket $this->eventManager->notify('Shopware_Modules_Order_SaveOrder_ProcessDetails', array('subject' => $this, 'details' => $this->sBasketData['content'])); $this->sUserData = $this->getUserDataForMail($this->sUserData); $details = $this->getOrderDetailsForMail($this->sBasketData["content"]); $variables = array("sOrderDetails" => $details, "billingaddress" => $this->sUserData["billingaddress"], "shippingaddress" => $this->sUserData["shippingaddress"], "additional" => $this->sUserData["additional"], "sShippingCosts" => $this->sSYSTEM->sMODULES['sArticles']->sFormatPrice($this->sShippingcosts) . " " . $this->sSYSTEM->sCurrency["currency"], "sAmount" => $this->sAmountWithTax ? $this->sSYSTEM->sMODULES['sArticles']->sFormatPrice($this->sAmountWithTax) . " " . $this->sSYSTEM->sCurrency["currency"] : $this->sSYSTEM->sMODULES['sArticles']->sFormatPrice($this->sAmount) . " " . $this->sSYSTEM->sCurrency["currency"], "sAmountNet" => $this->sSYSTEM->sMODULES['sArticles']->sFormatPrice($this->sBasketData["AmountNetNumeric"]) . " " . $this->sSYSTEM->sCurrency["currency"], "sTaxRates" => $this->sBasketData["sTaxRates"], "ordernumber" => $orderNumber, "sOrderDay" => date("d.m.Y"), "sOrderTime" => date("H:i"), "sComment" => $this->sComment, 'attributes' => $attributes, "sEsd" => $esdOrder); if ($dispatchId) { $variables["sDispatch"] = $this->sSYSTEM->sMODULES['sAdmin']->sGetPremiumDispatch($dispatchId); } if ($this->bookingId) { $variables['sBookingID'] = $this->bookingId; } // Save Billing and Shipping-Address to retrace in future $this->sSaveBillingAddress($this->sUserData["billingaddress"], $orderID); $this->sSaveShippingAddress($this->sUserData["shippingaddress"], $orderID); // Completed - Garbage basket / temporary - order $this->sDeleteTemporaryOrder(); $this->db->executeUpdate("DELETE FROM s_order_basket WHERE sessionID=?", array($this->getSession()->offsetGet('sessionId'))); $confirmMailDeliveryFailed = false; try { $this->sendMail($variables); } catch (\Exception $e) { $confirmMailDeliveryFailed = true; $email = $this->sUserData['additional']['user']['email']; $this->logOrderMailException($e, $orderNumber, $email); } // Check if voucher is affected $this->sTellFriend(); if ($this->getSession()->offsetExists('sOrderVariables')) { $variables = $this->getSession()->offsetGet('sOrderVariables'); $variables['sOrderNumber'] = $orderNumber; $variables['confirmMailDeliveryFailed'] = $confirmMailDeliveryFailed; $this->getSession()->offsetSet('sOrderVariables', $variables); } return $orderNumber; }