2018-11-21 06:29:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\API\Http\Controllers\Customer;
|
|
|
|
|
|
|
|
|
|
use Webkul\API\Http\Controllers\Controller;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
|
use Illuminate\Support\Facades\Event;
|
2018-11-22 12:43:03 +00:00
|
|
|
use Webkul\Customer\Repositories\CustomerAddressRepository as CustomerAddress;
|
2018-11-21 06:29:18 +00:00
|
|
|
use Auth;
|
|
|
|
|
use Cart;
|
2018-11-22 12:43:03 +00:00
|
|
|
use Validator;
|
2018-11-21 06:29:18 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Address controller for the APIs Customer Address
|
|
|
|
|
*
|
|
|
|
|
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
|
|
|
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
|
|
|
*/
|
|
|
|
|
class AddressController extends Controller
|
|
|
|
|
{
|
|
|
|
|
protected $customer;
|
2018-11-22 12:43:03 +00:00
|
|
|
protected $customerAddress;
|
2018-11-21 06:29:18 +00:00
|
|
|
|
2018-11-22 12:43:03 +00:00
|
|
|
public function __construct(CustomerAddress $customerAddress)
|
2018-11-21 06:29:18 +00:00
|
|
|
{
|
2018-11-22 12:43:03 +00:00
|
|
|
$this->customerAddress = $customerAddress;
|
|
|
|
|
|
2018-11-21 06:29:18 +00:00
|
|
|
if(auth()->guard('customer')->check()) {
|
|
|
|
|
$this->customer = auth()->guard('customer')->user();
|
|
|
|
|
} else {
|
2018-11-22 12:43:03 +00:00
|
|
|
$this->customer = false;
|
2018-11-21 06:29:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* To get the details of user to display on profile
|
|
|
|
|
*
|
|
|
|
|
* @return response JSON
|
|
|
|
|
*/
|
|
|
|
|
public function getAddress() {
|
2018-11-22 12:43:03 +00:00
|
|
|
if($this->customer == false) {
|
|
|
|
|
return response()->json($this->customer, 401);
|
|
|
|
|
} else {
|
|
|
|
|
$addresses = $this->customer->addresses;
|
2018-11-21 06:29:18 +00:00
|
|
|
|
2018-11-22 12:43:03 +00:00
|
|
|
return response()->json($addresses, 200);
|
|
|
|
|
}
|
2018-11-21 06:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getDefaultAddress() {
|
2018-11-22 12:43:03 +00:00
|
|
|
if($this->customer == false) {
|
|
|
|
|
return response()->json($this->customer, 401);
|
|
|
|
|
} else {
|
|
|
|
|
$defaultAddress = $this->customer->default_address;
|
|
|
|
|
|
|
|
|
|
if ($defaultAddress->count() > 0) {
|
|
|
|
|
return response()->json($defaultAddress, 200);
|
|
|
|
|
} else {
|
|
|
|
|
return response()->json(['false, default address not found'], 200);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createAddress() {
|
|
|
|
|
$data = request()->all();
|
|
|
|
|
|
|
|
|
|
$validator = Validator::make(request()->all(), [
|
|
|
|
|
'address1' => 'string|required',
|
|
|
|
|
'address2' => 'string',
|
|
|
|
|
'country' => 'string|required',
|
|
|
|
|
'state' => 'string|required',
|
|
|
|
|
'city' => 'string|required',
|
|
|
|
|
'postcode' => 'required',
|
|
|
|
|
'phone' => 'required'
|
|
|
|
|
]);
|
2018-11-21 06:29:18 +00:00
|
|
|
|
2018-11-22 12:43:03 +00:00
|
|
|
if ($validator->fails()) {
|
|
|
|
|
return response()->json($validator->messages(), 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cust_id['customer_id'] = $this->customer->id;
|
|
|
|
|
$data = array_merge($cust_id, $data);
|
|
|
|
|
|
|
|
|
|
if($this->customer->addresses->count() == 0) {
|
|
|
|
|
$data['default_address'] = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($this->address->create($data)) {
|
|
|
|
|
session()->flash('success', 'Address have been successfully added.');
|
|
|
|
|
|
|
|
|
|
return redirect()->route($this->_config['redirect']);
|
|
|
|
|
} else {
|
|
|
|
|
session()->flash('error', 'Address cannot be added.');
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
2018-11-21 06:29:18 +00:00
|
|
|
}
|
|
|
|
|
}
|