File: D:/HostingSpaces/SBogers10/topswtwmobile.komma.pro/app/controllers/ReOrderController.php
<?php
/**
* This class will handle the reorderings.
*
* @author Tim Van Samang <timvansamang@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
use KommaApp\Shop\Orders\OrderService;
use KommaApp\Shop\Customers\CustomerService;
use KommaApp\Shop\Orders\order;
use \Illuminate\Http\Request;
use Komma\Kms\Products\Models\Product;
class ReOrderController extends \BaseController
{
/**
* @var CustomerService
*/
private $customerService;
/**
* @var Orderservice
*/
private $orderService;
/**
* @var CartController
*/
private $cartController;
private $request;
private $deprecateddProdcuts;
public function __construct(CustomerService $customerService, OrderService $orderService, CartController $cartController, Request $request)
{
$this->customerService = $customerService;
$this->orderService = $orderService;
$this->cartController = $cartController;
$this->request = $request;
}
/**
* This method will search for the products in an old order
* and add these to a (new) cart,so these can be re-ordered
*
* @param string $language_id
* @param order $order
*
* @return mixed
*
*/
public function reOrderOrder($language_id, Order $order)
{
// Setup shop languages from URI
\Shop::getLanguageService()->bootFromUri(\Shop::getId());
/**
* These functions aren't used at the moment, but may come in handy in the future
* $current_customer_id = $this->customerService->getLoggedInCustomer()->id
* $customer_id_of_order = $order->customer_id;
*/
$products_found = 0;
$products_not_found = array();
$deprecatedProducts = [];
//Loop trough de order products
foreach ($order->products()->getResults() as $order_product) {
//Collect the real product based on the internal_article_nummer from the order_product because the product_id is not set in de $order_product
if (!$product = Product::where('internal_article_number', $order_product->internal_article_number)
->whereHas('shopProducts',function($q)
{
$q->where('shop_id',\Shop::getId())->where('active',1);
})
->first()) {
//The product is not found, Check if it is deprecated and get the new product
if (!$product = $this->checkForDeprecatedProduct($order_product->internal_article_number)) {
$products_not_found[] = $order_product->internal_article_number;
continue;
}
$deprecatedProducts[$order_product->internal_article_number] = $product->internal_article_number;
}
//Add the product and the original quantity to the cart
if (!$this->cartController->addProductByIdExternal($product->id, $order_product->quantity)) {
$products_not_found[] = $order_product->internal_article_number;
continue;
}
$products_found++;
}
//TODO: Make sure the message are shown on the cart, and the home page. if so, make sure these can be translated
//Check if none of the products is found
if ($products_found == 0) {
//Return to the homepage with a message
return \Redirect::to(\Shop::getPageService()->page('cart')->route)->with(['errors' => [\Lang::get('checkout/cart.no_products_found')]]);
} //Check if all the products are found
else if (!empty($products_not_found)) {
//Return to the cart with a message
return \Redirect::to(\Shop::getPageService()->page('cart')->route)->with(['errors' => [\Lang::get('checkout/cart.not_all_products_found')]]);
}
//All the products were found, the page will redirect to the cart page in the correct language
$messages = [];
foreach ($deprecatedProducts as $oldProduct => $newProduct) {
$messages[] =\Lang::get('checkout/cart.product_is_deprecated',['productOld'=> $oldProduct, 'productNew'=> $newProduct]);
}
return \Redirect::to(\Shop::getPageService()->page('cart')->route)->with(['success' =>[\Lang::get('checkout/cart.all_products_added')],'messages'=> $messages]);
}
public function checkForDeprecatedProduct($oldTNumber)
{
if ($this->deprecateddProdcuts == null) {
//Check if the file with deprecated products exist
if (!file_exists(app_path() . '/KommaApp/Shop/Products/deprecated-products.json')) return false;
//load the file and decode it to json
$this->deprecatedProducts = json_decode(\File::get(app_path() . '/KommaApp/Shop/Products/deprecated-products.json'));
}
//Check if the oldTNumber is is in the list
if (!isset($this->deprecatedProducts->$oldTNumber)) return false;
//Check if we can find the new product
if (!$product = Product::where('internal_article_number', $this->deprecatedProducts->$oldTNumber)
->whereHas('shopProducts',function($q)
{
$q->where('shop_id',\Shop::getId())->where('active',1);
})
->first()) return false;
//Return product
return $product;
}
}