示例#1
0
 public function convertOrderAction()
 {
     if (!($orderId = $this->Request()->getParam('orderId'))) {
         $this->View()->assign(['success' => false, 'message' => $this->translateMessage('errorMessage/noOrderId', 'No orderId passed.')]);
         return;
     }
     // Get user, shipping and billing
     $builder = Shopware()->Models()->createQueryBuilder();
     $builder->select(array('orders', 'customer', 'billing', 'payment', 'shipping'))->from('Shopware\\Models\\Order\\Order', 'orders')->leftJoin('orders.customer', 'customer')->leftJoin('orders.payment', 'payment')->leftJoin('customer.billing', 'billing')->leftJoin('customer.shipping', 'shipping')->where("orders.id = ?1")->setParameter(1, $orderId);
     $result = $builder->getQuery()->getArrayResult();
     // Check requiered fields
     if (empty($result) || $result[0]['customer'] === null || $result[0]['customer']['billing'] === null) {
         $this->View()->assign(['success' => false, 'message' => $this->translateMessage('errorMessage/noCustomerData', 'Could not get required customer data.')]);
         return;
     }
     // Get ordernumber
     $numberRepository = Shopware()->Models()->getRepository('Shopware\\Models\\Order\\Number');
     $numberModel = $numberRepository->findOneBy(array('name' => 'invoice'));
     if ($numberModel === null) {
         $this->View()->assign(['success' => false, 'message' => $this->translateMessage('errorMessage/noOrdernumber', 'Could not get ordernumber.')]);
         return;
     }
     $newOrderNumber = $numberModel->getNumber() + 1;
     // Set new ordernumber
     $numberModel->setNumber($newOrderNumber);
     // set new ordernumber to the order
     $orderModel = Shopware()->Models()->find('Shopware\\Models\\Order\\Order', $orderId);
     $orderModel->setNumber($newOrderNumber);
     // refreshes the in stock correctly for this order if the user confirmed it
     if ((bool) $this->Request()->getParam('refreshInStock')) {
         $outOfStock = $this->getOutOfStockProducts($orderModel);
         if (!empty($outOfStock)) {
             $numbers = array_map(function (\Shopware\Models\Article\Detail $variant) {
                 return $variant->getNumber();
             }, $outOfStock);
             $this->View()->assign(['success' => false, 'message' => $this->translateMessage('errorMessage/notEnoughStock', "The following products haven't enough stock") . implode(', ', $numbers)]);
             return;
         }
         $this->convertCancelledOrderInStock($orderModel);
     }
     // If there is no shipping address, set billing address to be the shipping address
     if ($result[0]['customer']['shipping'] === null) {
         $result[0]['customer']['shipping'] = $result[0]['customer']['billing'];
     }
     // Create new entry in s_order_billingaddress
     $billingModel = new Shopware\Models\Order\Billing();
     $billingModel->fromArray($result[0]['customer']['billing']);
     $billingModel->setCountry(Shopware()->Models()->find('Shopware\\Models\\Country\\Country', $result[0]['customer']['billing']['countryId']));
     $billingModel->setCustomer(Shopware()->Models()->find('Shopware\\Models\\Customer\\Customer', $result[0]['customer']['billing']['customerId']));
     $billingModel->setOrder($orderModel);
     Shopware()->Models()->persist($billingModel);
     // Create new entry in s_order_shippingaddress
     $shippingModel = new Shopware\Models\Order\Shipping();
     $shippingModel->fromArray($result[0]['customer']['shipping']);
     $shippingModel->setCountry(Shopware()->Models()->find('Shopware\\Models\\Country\\Country', $result[0]['customer']['shipping']['countryId']));
     $shippingModel->setCustomer(Shopware()->Models()->find('Shopware\\Models\\Customer\\Customer', $result[0]['customer']['shipping']['customerId']));
     $shippingModel->setOrder($orderModel);
     Shopware()->Models()->persist($shippingModel);
     // Finally set the order to be a regular order
     $statusModel = Shopware()->Models()->find('Shopware\\Models\\Order\\Status', 1);
     $orderModel->setOrderStatus($statusModel);
     Shopware()->Models()->flush();
     $this->View()->assign(['success' => true]);
 }
 public function convertOrderAction()
 {
     if (!($orderId = $this->Request()->getParam('orderId'))) {
         $this->View()->assign(array('success' => false, 'message' => 'No orderId passed'));
         return;
     }
     // Get user, shipping and billing
     $builder = Shopware()->Models()->createQueryBuilder();
     $builder->select(array('orders', 'customer', 'billing', 'payment', 'shipping'))->from('Shopware\\Models\\Order\\Order', 'orders')->leftJoin('orders.customer', 'customer')->leftJoin('orders.payment', 'payment')->leftJoin('customer.billing', 'billing')->leftJoin('customer.shipping', 'shipping')->where("orders.id = ?1")->setParameter(1, $orderId);
     $result = $builder->getQuery()->getArrayResult();
     // Check requiered fields
     if (empty($result) || $result[0]['customer'] === null || $result[0]['customer']['billing'] === null) {
         $this->View()->assign(array('success' => false, 'message' => 'Could not get required customer data'));
         return;
     }
     // Get ordernumber
     $numberRepository = Shopware()->Models()->getRepository('Shopware\\Models\\Order\\Number');
     $numberModel = $numberRepository->findOneBy(array('name' => 'invoice'));
     if ($numberModel === null) {
         $this->View()->assign(array('success' => false, 'message' => 'Could not get ordernumber'));
         return;
     }
     $newOrderNumber = $numberModel->getNumber() + 1;
     // Set new ordernumber
     $numberModel->setNumber($newOrderNumber);
     // set new ordernumber to the order
     $orderModel = Shopware()->Models()->find('Shopware\\Models\\Order\\Order', $orderId);
     $orderModel->setNumber($newOrderNumber);
     // set new ordernumber to order details
     $orderDetailRepository = Shopware()->Models()->getRepository('Shopware\\Models\\Order\\Detail');
     $orderDetailModel = $orderDetailRepository->findOneBy(array('orderId' => $orderId));
     $orderDetailModel->setNumber($newOrderNumber);
     // If there is no shipping address, set billing address to be the shipping address
     if ($result[0]['customer']['shipping'] === null) {
         $result[0]['customer']['shipping'] = $result[0]['customer']['billing'];
     }
     // Create new entry in s_order_billingaddress
     $billingModel = new Shopware\Models\Order\Billing();
     $billingModel->fromArray($result[0]['customer']['billing']);
     $billingModel->setCountry(Shopware()->Models()->find('Shopware\\Models\\Country\\Country', $result[0]['customer']['billing']['countryId']));
     $billingModel->setCustomer(Shopware()->Models()->find('Shopware\\Models\\Customer\\Customer', $result[0]['customer']['billing']['customerId']));
     $billingModel->setOrder($orderModel);
     Shopware()->Models()->persist($billingModel);
     // Create new entry in s_order_shippingaddress
     $shippingModel = new Shopware\Models\Order\Shipping();
     $shippingModel->fromArray($result[0]['customer']['shipping']);
     $shippingModel->setCountry(Shopware()->Models()->find('Shopware\\Models\\Country\\Country', $result[0]['customer']['shipping']['countryId']));
     $shippingModel->setCustomer(Shopware()->Models()->find('Shopware\\Models\\Customer\\Customer', $result[0]['customer']['shipping']['customerId']));
     $shippingModel->setOrder($orderModel);
     Shopware()->Models()->persist($shippingModel);
     // Finally set the order to be a regular order
     $statusModel = Shopware()->Models()->find('Shopware\\Models\\Order\\Status', 1);
     $orderModel->setOrderStatus($statusModel);
     Shopware()->Models()->flush();
     $this->View()->assign(array('success' => true));
 }
 /**
  * creates the shipping address which belongs to the order and
  * saves it as the new last used address
  *
  * @param array $data
  * @return \Shopware\Models\Order\Shipping
  */
 private function createShippingAddress($data)
 {
     if ($data['shippingAddressId']) {
         /** @var Shopware\Models\Customer\Shipping $addressHolderModel */
         $addressHolderModel = Shopware()->Models()->find('Shopware\\Models\\Customer\\Shipping', $data['shippingAddressId']);
     } else {
         /** @var Shopware\Models\Customer\Billing $shippingAddressHolder */
         $addressHolderModel = Shopware()->Models()->find('Shopware\\Models\\Customer\\Billing', $data['billingAddressId']);
         $this->equalBillingAddress = true;
     }
     $shippingOrderModel = new Shopware\Models\Order\Shipping();
     $shippingOrderModel->setCity($addressHolderModel->getCity());
     $shippingOrderModel->setStreet($addressHolderModel->getStreet());
     $shippingOrderModel->setSalutation($addressHolderModel->getSalutation());
     $shippingOrderModel->setZipCode($addressHolderModel->getZipCode());
     $shippingOrderModel->setFirstName($addressHolderModel->getFirstName());
     $shippingOrderModel->setLastName($addressHolderModel->getLastName());
     $shippingOrderModel->setAdditionalAddressLine1($addressHolderModel->getAdditionalAddressLine1());
     $shippingOrderModel->setAdditionalAddressLine2($addressHolderModel->getAdditionalAddressLine2());
     $shippingOrderModel->setCompany($addressHolderModel->getCompany());
     $shippingOrderModel->setDepartment($addressHolderModel->getDepartment());
     $shippingOrderModel->setCustomer($addressHolderModel->getCustomer());
     if ($addressHolderModel->getCountryId()) {
         /** @var Shopware\Models\Country\Country $countryModel */
         $countryModel = Shopware()->Models()->find('Shopware\\Models\\Country\\Country', $addressHolderModel->getCountryId());
         $shippingOrderModel->setCountry($countryModel);
     }
     if ($addressHolderModel->getStateId()) {
         /** @var Shopware\Models\Country\State $stateModel */
         $stateModel = Shopware()->Models()->find('Shopware\\Models\\Country\\State', $addressHolderModel->getStateId());
         $shippingOrderModel->setState($stateModel);
     }
     return $shippingOrderModel;
 }