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 billing address which belongs to the order and
  * saves it as the new last used address
  *
  * @param array $data
  * @return \Shopware\Models\Order\Billing
  */
 private function createBillingAddress($data)
 {
     /** @var Shopware\Models\Customer\Billing $billingCustomerModel */
     $billingCustomerModel = Shopware()->Models()->find('Shopware\\Models\\Customer\\Billing', $data['billingAddressId']);
     $billingOrderModel = new Shopware\Models\Order\Billing();
     $billingOrderModel->setCity($billingCustomerModel->getCity());
     $billingOrderModel->setStreet($billingCustomerModel->getStreet());
     $billingOrderModel->setSalutation($billingCustomerModel->getSalutation());
     $billingOrderModel->setZipCode($billingCustomerModel->getZipCode());
     $billingOrderModel->setFirstName($billingCustomerModel->getFirstName());
     $billingOrderModel->setLastName($billingCustomerModel->getLastName());
     $billingOrderModel->setAdditionalAddressLine1($billingCustomerModel->getAdditionalAddressLine1());
     $billingOrderModel->setAdditionalAddressLine2($billingCustomerModel->getAdditionalAddressLine2());
     $billingOrderModel->setVatId($billingCustomerModel->getVatId());
     $billingOrderModel->setPhone($billingCustomerModel->getPhone());
     $billingOrderModel->setFax($billingCustomerModel->getFax());
     $billingOrderModel->setCompany($billingCustomerModel->getCompany());
     $billingOrderModel->setDepartment($billingCustomerModel->getDepartment());
     $billingOrderModel->setNumber($billingCustomerModel->getNumber());
     $billingOrderModel->setCustomer($billingCustomerModel->getCustomer());
     if ($billingCustomerModel->getCountryId()) {
         /** @var Shopware\Models\Country\Country $countryModel */
         $countryModel = Shopware()->Models()->find('Shopware\\Models\\Country\\Country', $billingCustomerModel->getCountryId());
         $billingOrderModel->setCountry($countryModel);
     }
     if ($billingCustomerModel->getStateId()) {
         /** @var Shopware\Models\Country\State $stateModel */
         $stateModel = Shopware()->Models()->find('Shopware\\Models\\Country\\State', $billingCustomerModel->getStateId());
         $billingOrderModel->setState($stateModel);
     }
     return $billingOrderModel;
 }