File: D:/HostingSpaces/SBogers10/debierbaron.komma.pro/data/app/Komma/Shop/ShopService.php
<?php
/**
* Short description for the file.
*
* @author Tim Van Samang <timvansamang@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
namespace Komma\Shop;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\MessageBag;
use Komma\Boxes\BoxService;
use Komma\Mailers\MailService;
use Komma\Orders\Models\Order;
use Komma\OrderDiscount\Models\OrderDiscount;
use Komma\Orders\Models\OrderProducts;
use Komma\Shipments\ShipmentService;
use Komma\Subscriptions\Models\Subscription;
class ShopService
{
private $boxService;
private $mailService;
private $shipmentService;
public function __construct(BoxService $boxService, MailService $mailService, ShipmentService $shipmentService)
{
$this->boxService = $boxService;
$this->mailService = $mailService;
$this->shipmentService = $shipmentService;
}
/*----------------------------------------*
| |
| Validation |
| |
*----------------------------------------*/
public function validateCheckoutForm()
{
$errors = new MessageBag();
if ($addressErrors = $this->validateAddress()) $errors->merge($addressErrors);
$eighteenYearsAgo = \Carbon\Carbon::now()->addDay()->subYears(18)->format('d-m-Y');
$input = \Input::all();
$rules = [
'terms_and_conditions' => 'required',
'birth_day' => 'required|date-format:d-m-Y|before:' . $eighteenYearsAgo,
];
$validator = Validator::make(
$input,
$rules
);
if ($validator->fails()) {
$errors->merge($validator->messages());
}
if ($errors->count() > 0) return $errors;
}
public function validateAddress($prefix = '')
{
$rules = [
$prefix . 'first_name' => 'required',
$prefix . 'last_name' => 'required',
$prefix . 'email' => 'required|email',
$prefix . 'postal' => 'required',
$prefix . 'house_number' => 'required',
$prefix . 'street' => 'required',
$prefix . 'city' => 'required',
$prefix . 'country' => 'required|',
];
$validator = Validator::make(
\Input::all(),
$rules
);
if ($validator->fails()) {
return $validator->messages();
}
}
/*----------------------------------------*
| |
| Cart |
| |
*----------------------------------------*/
public function addProductToCart($bodId, $quantity, $absolute = true)
{
//Check if the session already has the current box
if (\Session::has('cart.products.' . $bodId)) {
//Set the box from session to the box vartiable
$box = \Session::get('cart.products.' . $bodId);
} else {
//No, get the box via the boxService
$box = $this->boxService->getBox($bodId);
}
//Set the quantity
if ($absolute) {
//If absolute is true the given quantity is absolute, so set this as the new quantity
$box->quantity = $quantity;
} else {
//If the quantity is not absolute add these to the current quantity
$oldQuantity = (isset($box->quantity) ? $box->quantity : 0);
$box->quantity = $quantity + $oldQuantity;
}
\Session::set('cart.products.' . $box->id, $box);
$this->updateCart();
}
public function updateCart()
{
$product_price = 0;
foreach (\Session::get('cart.products') as $product) {
$product_price += $product->price * $product->quantity;
}
\Session::set('cart.original_price', $product_price);
\Session::set('cart.product_price', $product_price);
if (!\Session::has('cart.discount')) {
\Session::set('cart.total_price', $product_price);
return;
}
if (\Session::get('cart.discount.type') == 'absolute') {
\Session::set('cart.total_price', $product_price - \Session::get('cart.discount.value'));
} elseif (\Session::get('cart.discount.type') == 'percentage') {
$percentage = (100 - \Session::get('cart.discount.value')) / 100;
\Session::set('cart.total_price', $product_price * $percentage);
}
}
public function deleteProductFromCart($productId)
{
\Session::forget('cart.products.' . $productId);
$this->updateCart();
}
/*----------------------------------------*
| |
| Order discount |
| |
*----------------------------------------*/
public function validateCoupon($coupon)
{
//Check if we can find a coupon
if (!$orderDiscount = OrderDiscount::where('custom_coupon_code', '=', $coupon)->first()) return false;
return $this->applyDiscount($orderDiscount, $coupon);
}
public function applyDiscount($orderDiscount, $coupon)
{
$originalPrice = \Session::get('cart.original_price');
//Check if the min price is set and is bigger then the originalPrice; If true return false
if ($orderDiscount->price_min && $orderDiscount->price_min > $originalPrice) return false;
//Check if the max price is set and is smaller then the original price; If true return false
if ($orderDiscount->price_max && $orderDiscount->price_max < $originalPrice) return false;
//check if the date from is set and is bigger then the current date; if true return false
if ($orderDiscount->date_from && $orderDiscount->date_from > \Carbon\Carbon::now()) return false;
//Check if the date untull is set and is smaller then the current date; if true return false
if ($orderDiscount->date_until && $orderDiscount->date_until < \Carbon\Carbon::now()) return false;
$discount = (object)[];
if ($orderDiscount->discount_percentage) {
$discount->type = 'percentage';
$discount->value = $orderDiscount->discount_percentage;
} elseif ($orderDiscount->discount_absolute) {
$discount->type = 'absolute';
$discount->value = $orderDiscount->discount_absolute;
} elseif ($orderDiscount->discount_no_shipping_cost == 1) {
$discount->type = 'absolute';
$discount->value = 0;
} else {
return false;
}
$discount->id = $orderDiscount->id;
$discount->name = $orderDiscount->name;
$discount->description = $orderDiscount->translation->description;
$discount->coupon = $coupon;
\Session::set('cart.discount', (array)$discount);
$this->updateCart();
return true;
}
/*----------------------------------------*
| |
| Orders |
| |
*----------------------------------------*/
public function saveOrder()
{
//Create new order
$order = new Order();
$order->order_number = $this->getNewOrderNumber();
$order->birth_day = \Input::get('birth_day');
//Invoice
$order->invoice_first_name = \Input::get('first_name');
$order->invoice_name_insertion = \Input::get('name_insertion');
$order->invoice_last_name = \Input::get('last_name');
$order->invoice_email = \Input::get('email');
$order->invoice_postal = \Input::get('postal');
$order->invoice_house_number = \Input::get('house_number');
$order->invoice_house_number_suffix = \Input::get('house_number_suffix');
$order->invoice_street = \Input::get('street');
$order->invoice_city = \Input::get('city');
$order->invoice_country = \Input::get('country');
//Shipping
//Invoice
$order->shipping_first_name = \Input::get('first_name');
$order->shipping_name_insertion = \Input::get('name_insertion');
$order->shipping_last_name = \Input::get('last_name');
$order->shipping_postal = \Input::get('postal');
$order->shipping_house_number = \Input::get('house_number');
$order->shipping_house_number_suffix = \Input::get('house_number_suffix');
$order->shipping_street = \Input::get('street');
$order->shipping_city = \Input::get('city');
$order->shipping_country = \Input::get('country');
//Amount
$order->original_price = \Session::get('cart.original_price');
$order->price_total = \Session::get('cart.total_price');
//Discount
$order->discount_description = \Session::get('cart.discount.description');
$order->discount_price = $order->original_price - $order->price_total;
if (\Session::get('cart.discount.type') == 'absolute') {
$order->discount_absolute = \Session::get('cart.discount.value');
}
if (\Session::get('cart.discount.type') == 'percentage') {
$order->discount_percentage = \Session::get('cart.discount.value');
}
$order->coupon_code = \Session::get('cart.discount.coupon');
//Save the order
$order->save();
//Loop over the products
foreach (\Session::get('cart.products') as $product) {
$orderProduct = OrderProducts::firstOrNew(['order_id' => $order->id, 'internal_article_number' => $product->id]);
$orderProduct->title = $product->name;
$orderProduct->description = $product->description;
$orderProduct->quantity = $product->quantity;
$orderProduct->price = $product->price;
$orderProduct->save();
}
return $order;
}
public function getNewOrderNumber()
{
$LastOrderNumber = null;
//Get the order with the highest order number
if ($order = Order::select('order_number')->orderBy('order_number', 'DESC')->first()) $LastOrderNumber = $order->order_number;
//If lastOrderNumber is null, et the ordernumber from the config
if (!$LastOrderNumber) $LastOrderNumber = \Config::get('bierbaron.order_number_start');
//If the lastOrderNumber is smaller then the config order start +1, set the ordernumber from the config
if ($LastOrderNumber < \Config::get('bierbaron.order_number_start') + 1) $LastOrderNumber = \Config::get('bierbaron.order_number_start');
//Add 1 to the orderNumber
$orderNumber = $LastOrderNumber + 1;
return $orderNumber;
}
public function getOrderWithPayment($payment)
{
//Get the order based on the order id
$order = Order::select('orders.*')
->where('orders.id', '=', $payment->metadata->order_id)
//Join on the order_transactions for an extra check
->leftJoin('order_transactions as ot', 'ot.order_id', '=', 'orders.id')
->where('ot.custom_identifier', '=', $payment->id)
->with(['transactions' => function ($query) use ($payment) {
$query->where('custom_identifier', '=', $payment->id);
}]
)
->first();
return $order;
}
/**
* Update the order with a given status
*
* @param $order
* @param $status
*/
public function updateOrder($order, $status)
{
//Set the order status
$order->status = $status;
$order->save();
//When the order is canceled we don't have to do anything for this order
if ($status == 'canceled') return;
if ($status == 'paid') $this->handlePaidOrder($order);
}
/**
* This does everything when the order is set to paid
*
* @param $order
* @return true
*/
private function handlePaidOrder($order)
{
//If the order paid_at date is set, this has been done before
if ($order->paid_at != null) return;
//Save
// the date from now to the order
$order->paid_at = \Carbon\Carbon::now();
$order->save();
//Send mail to customer?
$this->mailService->sendCustomerPaidMail($order);
//Send email to admin
$this->mailService->sendAdminPaidMail($order);
//apply order to sendCloud
$this->shipmentService->sendOrderWithSendCloud($order);
}
/*----------------------------------------*
| |
| Subscriptions |
| |
*----------------------------------------*/
///
public function saveSubscription()
{
//Create new order
$subscription = new Subscription();
$subscription->subscription_number = $this->getNewSubscriptionNumber();
$subscription->birth_day = \Input::get('birth_day');
//Invoice
$subscription->invoice_first_name = \Input::get('first_name');
$subscription->invoice_name_insertion = \Input::get('name_insertion');
$subscription->invoice_last_name = \Input::get('last_name');
$subscription->invoice_email = \Input::get('email');
$subscription->invoice_postal = \Input::get('postal');
$subscription->invoice_house_number = \Input::get('house_number');
$subscription->invoice_house_number_suffix = \Input::get('house_number_suffix');
$subscription->invoice_street = \Input::get('street');
$subscription->invoice_city = \Input::get('city');
$subscription->invoice_country = \Input::get('country');
//Shipping
//Invoice
$subscription->shipping_first_name = \Input::get('first_name');
$subscription->shipping_name_insertion = \Input::get('name_insertion');
$subscription->shipping_last_name = \Input::get('last_name');
$subscription->shipping_postal = \Input::get('postal');
$subscription->shipping_house_number = \Input::get('house_number');
$subscription->shipping_house_number_suffix = \Input::get('house_number_suffix');
$subscription->shipping_street = \Input::get('street');
$subscription->shipping_city = \Input::get('city');
$subscription->shipping_country = \Input::get('country');
//Subscription
$subscription->monthly_fee = \Config::get('bierbaron.subscription_monthly_fee');
$subscription->original_fee = \Config::get('bierbaron.subscription_monthly_fee');
$subscription->status = $subscription::inactive;
//Save the order
$subscription->save();
return $subscription;
}
public function getNewSubscriptionNumber()
{
$LastOrderNumber = null;
//Get the subscription with the highest subscription number
if ($subscription = Subscription::select('subscription_number')->orderBy('subscription_number', 'DESC')->first()) $LastNumber = $subscription->subscription_number;
//If lastOrderNumber is null, et the ordernumber from the config
if (!$LastNumber) $LastNumber = \Config::get('bierbaron.subscription_number_start');
//If the lastOrderNumber is smaller then the config subscription start +1, set the ordernumber from the config
if ($LastNumber < \Config::get('bierbaron.subscription_number_start') + 1) $LastNumber = \Config::get('bierbaron.subscription_number_start');
//Add 1 to the orderNumber
$subscriptionNumber = $LastNumber + 1;
return $subscriptionNumber;
}
public function getSubscriptionWithPayment($payment)
{
//Get the order based on the order id
$subscriptions = Subscription::select('subscriptions.*')
->where('subscriptions.id', '=', $payment->metadata->subscription_id)
//Join on the order_transactions for an extra check
->leftJoin('subscription_transactions as st', 'st.subscription_id', '=', 'subscriptions.id')
->where('st.custom_identifier', '=', $payment->id)
->with(['transactions' => function ($query) use ($payment) {
$query->where('custom_identifier', '=', $payment->id);
}]
)
->first();
return $subscriptions;
}
/**
* Update the order with a given status
*
* @param $subscription
* @param $status
*/
public function updateSubscription($subscription, $status)
{
//Set the subscription status
$subscription->status = $status;
$subscription->save();
//When the order is canceled we don't have to do anyth
if ($status == 'canceled') return;
if ($status == 'paid') {
$subscription->status = $subscription::active;
$subscription->save();
}
}
}