File: D:/HostingSpaces/SBogers10/topswtw.komma.pro/app/KommaApp/Shop/Maintenance/MaintenanceService.php
<?php
/**
* Short description for the file.
*
* @author Tim Van Samang <timvansamang@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
namespace KommaApp\Shop\Maintenance;
use Illuminate\Support\Facades\Validator;
use KommaApp\Shop\Customers\CustomerService;
use KommaApp\Shop\Maintenance\Models\Maintenance;
use KommaApp\Shop\Maintenance\Models\MaintenanceOrder;
use KommaApp\Shop\Orders\OrderToken;
class MaintenanceService
{
private $customerService;
public function __construct(CustomerService $customerService)
{
$this->customerService = $customerService;
}
public function getMaintenances()
{
//Check Shop
$maintenances = Maintenance::where('shop_id', '=', \Shop::getShop()->id);
//Check country
//Todo: make country dynamic
$maintenances = Maintenance::where('country_id', '=', \Shop::getCountryId());
$maintenances = $maintenances->with('translations')->get();
$frontEndMaintenances = [];
foreach ($maintenances as $m) {
$trans = $m->translations()->where('language_id', '=', \Shop::getLanguageService()->getCurrentLanguageId())->first();
$frontEndMaintenance = new \StdClass();
$frontEndMaintenance->id = $m->id;
$frontEndMaintenance->name = $trans->name;
$frontEndMaintenance->description = $trans->description;
$frontEndMaintenance->duration_time = $m->duration_time;
$frontEndMaintenance->price = $m->price;
$frontEndMaintenances[] = $frontEndMaintenance;
}
return $frontEndMaintenances;
}
public function createMaintenaceOrder($customer)
{
$maintenanceOrderId = null;
$maintenanceOrder = MaintenanceOrder::firstOrNew(['id' => $maintenanceOrderId]);
//Get the maintenance and translation
$maintenance = Maintenance::find(\Input::get('maintenance_type'));
$translation = $maintenance->translations()->where(['language_id' => \Shop::getLanguageService()->getCurrentLanguageId()])->first();
$maintenanceOrder->shop_id = \Shop::getShop()->id;
$maintenanceOrder->language_id = \Shop::getLanguageService()->getCurrentLanguageId();
$maintenanceOrder->customer_id = $customer->id;
//Details
$maintenanceOrder->domain_country = \Shop::getDomainCountry();
$maintenanceOrder->order_number = $maintenanceOrder::getNewOrderNumber();
$maintenanceOrder->status = '1';
// Invoice Address
$maintenanceOrder->invoice_first_name = \Input::get('first_name');
$maintenanceOrder->invoice_insertion = \Input::get('name_insertion');
$maintenanceOrder->invoice_last_name = \Input::get('last_name');
$maintenanceOrder->invoice_email = \Input::get('customer_email');
$maintenanceOrder->invoice_telephone = \Input::get('telephone');
$maintenanceOrder->invoice_company_name = null;
$maintenanceOrder->invoice_postal = \Input::get('postal');
$maintenanceOrder->invoice_number = \Input::get('house_number');
$maintenanceOrder->invoice_suffix = \Input::get('house_number_suffix');
$maintenanceOrder->invoice_street = \Input::get('street');
$maintenanceOrder->invoice_city = \Input::get('city');
$maintenanceOrder->invoice_country = \Input::get('country');
// Maintenance Address
$maintenanceOrder->maintenance_first_name = \Input::get('first_name');
$maintenanceOrder->maintenance_insertion = \Input::get('name_insertion');
$maintenanceOrder->maintenance_last_name = \Input::get('last_name');
$maintenanceOrder->maintenance_email = \Input::get('customer_email');
$maintenanceOrder->maintenance_telephone = \Input::get('telephone');
$maintenanceOrder->maintenance_company_name = null;
$maintenanceOrder->maintenance_postal = \Input::get('postal');
$maintenanceOrder->maintenance_number = \Input::get('house_number');
$maintenanceOrder->maintenance_suffix = \Input::get('house_number_suffix');
$maintenanceOrder->maintenance_street = \Input::get('street');
$maintenanceOrder->maintenance_city = \Input::get('city');
$maintenanceOrder->maintenance_country = \Input::get('country');
// $maintenanceOrder->remarks = \Input::get('remarks');
//Maintenance details
$maintenanceOrder->maintenance_id = $maintenance->id;
$maintenanceOrder->maintenance_title = $translation->name;
$maintenanceOrder->maintenance_description = $translation->description;
$maintenanceOrder->maintenance_price = $maintenance->price;
//Timestamps
$maintenanceOrder->ordered_at = \Carbon\Carbon::now();
//Generate and add the token
$token = $this->generateToken($maintenance->toArray());
$maintenanceOrder->order_token = $token;
$maintenanceOrder->save();
$maintenanceOrder->maintenance_duration = $maintenance->duration_time;
//Set the token and the id in the session
\Session::set('maintenanceOrder.token', $maintenanceOrder->order_token);
\Session::set('maintenanceOrder.id', $maintenanceOrder->id);
return $maintenanceOrder;
}
public function generateToken($data)
{
$secret = 'CEd}/Q`3=p5pX{NHgnB&D@Pu';
return \Hash::make(implode('-', $data) . $secret);
}
public function sentEmails($maintenanceOrder)
{
$customerMailer = \App::make('KommaApp\Shop\Mailers\CustomerMailer');
$adminMailer = \App::make('KommaApp\Shop\Mailers\AdminMailer');
//Sent to the customer
$customerMailer->sendMaintenanceOrderReceived($maintenanceOrder);
//Sent to Tops (and service guys)
$adminMailer->sendMaintenanceOrderReceived($maintenanceOrder);
//All emails sent
return true;
}
public function getMaintenanceOrderByToken($token)
{
if (!$maintenanceOrder = MaintenanceOrder::where('order_token', '=', $token)->first()) return false;
return $maintenanceOrder;
}
public function checkUserStatus($maintenanceOrder)
{
if (\Auth::customer()->get() && \Auth::customer()->get()->id == $maintenanceOrder->customer->id) return 'ok';
if (!$customer = $this->customerService->getCustomerByEmail($maintenanceOrder->invoice_email)) return 'new';
if ($customer->id != $maintenanceOrder->customer->id) return 'link';
return 'ok';
}
public function linkCustomer()
{
//Validate the form
if ($return = $this->customerService->validateLogin(\Input::all())) return $return;
//Check if we can login
$credentials = \Input::only('email', 'password');
$credentials['active'] = 1;
// Login with credentials
if (!\Auth::customer()->attempt($credentials)) {
return \Redirect::back()
->withErrors(['password' => \Lang::get('customer/login.wrong_password')]);
}
$maintenanceOrder = MaintenanceOrder::find(\Input::get('maintenance_order'));
$customer = \Auth::customer()->get();
// Link order with order_id with the user that is logged in.
$maintenanceOrder->customer()->associate($customer);
$maintenanceOrder->save();
return false;
}
public function createCustomer()
{
//Vallidate the input, the password and confirmation are required and should be the same And min 6 characters
$validator = Validator::make(\Input::all(),
['password' => 'required|min:6',
'password_confirmation' => 'required|same:password',]
, [
'password.min' => \Lang::get('customer/create.error_password_min'),
'password_confirmation.same' => \Lang::Get('customer/create.error_password_confirmation'),
]
);
//Check of the validation fails
if ($validator->fails()) {
//It fails, redirecot to the form with validation errors
return \Redirect::back()->withErrors($validator);
}
//All is well, save the password and activate the user
$password = \Input::get('password');
$maintenanceOrder = MaintenanceOrder::find(\Input::get('maintenance_order'));
// Link the account based on ther order
$result = $this->customerService->linkAccountFromMaintenanceOrder($maintenanceOrder, $password);
//If this fails return with errors
if (isset($result['error'])) {
return \Redirect::back()->with(['errors_msg' => [$result['error']]]);
}
return false;
}
}