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-24 09:56:31 +00:00
|
|
|
$this->middleware('auth:customer');
|
2018-11-22 14:51:37 +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-24 09:56:31 +00:00
|
|
|
$this->customer['message'] = 'unauthorized';
|
|
|
|
|
$this->unAuthorized();
|
2018-11-21 06:29:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 09:56:31 +00:00
|
|
|
public function unAuthorized() {
|
|
|
|
|
return response()->json($this->customer, 401);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 06:29:18 +00:00
|
|
|
/**
|
|
|
|
|
* To get the details of user to display on profile
|
|
|
|
|
*
|
|
|
|
|
* @return response JSON
|
|
|
|
|
*/
|
2018-11-22 14:51:37 +00:00
|
|
|
public function get() {
|
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
|
|
|
}
|
|
|
|
|
|
2018-11-22 14:51:37 +00:00
|
|
|
public function getDefault() {
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 14:51:37 +00:00
|
|
|
public function create() {
|
2018-11-22 12:43:03 +00:00
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 14:51:37 +00:00
|
|
|
$result = $this->customerAddress->create($data);
|
2018-11-22 12:43:03 +00:00
|
|
|
|
2018-11-22 14:51:37 +00:00
|
|
|
if($result) {
|
|
|
|
|
return response()->json(true, 200);
|
2018-11-22 12:43:03 +00:00
|
|
|
} else {
|
2018-11-22 14:51:37 +00:00
|
|
|
return response()->json(false, 200);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete($id) {
|
|
|
|
|
$result = $this->customerAddress->delete($id);
|
|
|
|
|
|
|
|
|
|
return response()->json($result, 200);
|
|
|
|
|
}
|
2018-11-22 12:43:03 +00:00
|
|
|
|
2018-11-22 14:51:37 +00:00
|
|
|
public function makeDefault($id) {
|
|
|
|
|
$defaultAddress = $this->customer->default_address;
|
|
|
|
|
|
|
|
|
|
if($defaultAddress != null && $defaultAddress->count() > 0) {
|
|
|
|
|
$defaultAddress->update(['default_address' => 0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($this->customerAddress->find($id)->default_address == 1) {
|
|
|
|
|
return response()->json(false, 200);
|
2018-11-22 12:43:03 +00:00
|
|
|
}
|
2018-11-22 14:51:37 +00:00
|
|
|
|
|
|
|
|
$result = $this->customerAddress->update(['default_address' => 1], $id);
|
|
|
|
|
|
|
|
|
|
return response()->json($result, 200);
|
2018-11-21 06:29:18 +00:00
|
|
|
}
|
|
|
|
|
}
|