fix address-constrains; fix address global scopes
This commit is contained in:
parent
01d4cb026f
commit
910f76952f
|
|
@ -6,6 +6,14 @@ use Illuminate\Database\Eloquent\Builder;
|
|||
use Webkul\Checkout\Contracts\CartAddress as CartAddressContract;
|
||||
use Webkul\Core\Models\Address;
|
||||
|
||||
/**
|
||||
* Class CartAddress
|
||||
* @package Webkul\Checkout\Models
|
||||
*
|
||||
* @property integer $cart_id
|
||||
* @property Cart $cart
|
||||
*
|
||||
*/
|
||||
class CartAddress extends Address implements CartAddressContract
|
||||
{
|
||||
public const ADDRESS_TYPE_SHIPPING = 'cart_address_shipping';
|
||||
|
|
@ -23,7 +31,7 @@ class CartAddress extends Address implements CartAddressContract
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
protected static function booted()
|
||||
protected static function boot()
|
||||
{
|
||||
static::addGlobalScope('address_type', static function (Builder $builder) {
|
||||
$builder->whereIn('address_type', [
|
||||
|
|
@ -31,6 +39,8 @@ class CartAddress extends Address implements CartAddressContract
|
|||
self::ADDRESS_TYPE_SHIPPING
|
||||
]);
|
||||
});
|
||||
|
||||
parent::boot();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -50,9 +50,9 @@ class AddTableAddresses extends Migration
|
|||
|
||||
$table->timestamps();
|
||||
|
||||
$table->foreign(['customer_id'])->references('id')->on('customers');
|
||||
$table->foreign(['cart_id'])->references('id')->on('cart');
|
||||
$table->foreign(['order_id'])->references('id')->on('orders');
|
||||
$table->foreign(['customer_id'])->references('id')->on('customers')->onDelete('cascade');
|
||||
$table->foreign(['cart_id'])->references('id')->on('cart')->onDelete('cascade');
|
||||
$table->foreign(['order_id'])->references('id')->on('orders')->onDelete('cascade');
|
||||
});
|
||||
|
||||
$this->migrateCustomerAddresses();
|
||||
|
|
@ -241,7 +241,10 @@ SQL;
|
|||
->update(['cart_address_id' => $row->id]);
|
||||
});
|
||||
|
||||
$table->foreign(['cart_address_id'])->references('id')->on('addresses');
|
||||
$table->foreign(['cart_address_id'])
|
||||
->references('id')
|
||||
->on('addresses')
|
||||
->onDelete('cascade');
|
||||
});
|
||||
|
||||
Schema::table('invoices', static function (Blueprint $table) {
|
||||
|
|
@ -255,7 +258,10 @@ SQL;
|
|||
->update(['order_address_id' => $row->id]);
|
||||
});
|
||||
|
||||
$table->foreign(['order_address_id'])->references('id')->on('addresses');
|
||||
$table->foreign(['order_address_id'])
|
||||
->references('id')
|
||||
->on('addresses')
|
||||
->onDelete('cascade');
|
||||
});
|
||||
|
||||
Schema::table('shipments', static function (Blueprint $table) {
|
||||
|
|
@ -269,7 +275,10 @@ SQL;
|
|||
->update(['order_address_id' => $row->id]);
|
||||
});
|
||||
|
||||
$table->foreign(['order_address_id'])->references('id')->on('addresses');
|
||||
$table->foreign(['order_address_id'])
|
||||
->references('id')
|
||||
->on('addresses')
|
||||
->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,10 +4,39 @@
|
|||
namespace Webkul\Core\Models;
|
||||
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Webkul\Customer\Models\Customer;
|
||||
|
||||
/**
|
||||
* Class Address
|
||||
* @package Webkul\Core\Models
|
||||
*
|
||||
* @property string $address_type
|
||||
* @property integer $customer_id
|
||||
* @property Customer $customer
|
||||
* @property string $first_name
|
||||
* @property string $last_name
|
||||
* @property string $gender
|
||||
* @property string $company_name
|
||||
* @property string $address1
|
||||
* @property string $address2
|
||||
* @property string $postcode
|
||||
* @property string $city
|
||||
* @property string $state
|
||||
* @property string $country
|
||||
* @property string $email
|
||||
* @property string $phone
|
||||
* @property boolean $default_address
|
||||
* @property array $additional
|
||||
*
|
||||
* @property-read integer $id
|
||||
* @property-read string $name
|
||||
* @property-read Carbon $created_at
|
||||
* @property-read Carbon $updated_at
|
||||
*
|
||||
*/
|
||||
abstract class Address extends Model
|
||||
{
|
||||
protected $table = 'addresses';
|
||||
|
|
@ -36,6 +65,7 @@ abstract class Address extends Model
|
|||
'email',
|
||||
'phone',
|
||||
'default_address',
|
||||
'vat_id',
|
||||
'additional',
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -22,10 +22,12 @@ class CustomerAddress extends Address implements CustomerAddressContract
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
protected static function booted()
|
||||
protected static function boot()
|
||||
{
|
||||
static::addGlobalScope('address_type', static function (Builder $builder) {
|
||||
$builder->where('address_type', self::ADDRESS_TYPE);
|
||||
});
|
||||
|
||||
parent::boot();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ use Webkul\Sales\Models\OrderAddress;
|
|||
|
||||
$factory->define(OrderAddress::class, function (Faker $faker) {
|
||||
$customer = factory(Customer::class)->create();
|
||||
$customerAddress = factory(CustomerAddress::class)->create();
|
||||
$customerAddress = factory(CustomerAddress::class)->make();
|
||||
|
||||
return [
|
||||
'first_name' => $customer->first_name,
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ class Invoice extends Model implements InvoiceContract
|
|||
}
|
||||
|
||||
/**
|
||||
* Get the addresses for the shipment.
|
||||
* Get the address for the invoice.
|
||||
*/
|
||||
public function address()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,10 +2,19 @@
|
|||
|
||||
namespace Webkul\Sales\Models;
|
||||
|
||||
use Webkul\Checkout\Models\CartAddress;
|
||||
use Webkul\Core\Models\Address;
|
||||
use Webkul\Sales\Contracts\OrderAddress as OrderAddressContract;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
/**
|
||||
* Class OrderAddress
|
||||
* @package Webkul\Sales\Models
|
||||
*
|
||||
* @property integer $order_id
|
||||
* @property Order $order
|
||||
*
|
||||
*/
|
||||
class OrderAddress extends Address implements OrderAddressContract
|
||||
{
|
||||
public const ADDRESS_TYPE_SHIPPING = 'order_address_shipping';
|
||||
|
|
@ -23,14 +32,27 @@ class OrderAddress extends Address implements OrderAddressContract
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
protected static function booted()
|
||||
protected static function boot()
|
||||
{
|
||||
static::addGlobalScope('address_type', static function (Builder $builder) {
|
||||
static::addGlobalScope('address_type', function (Builder $builder) {
|
||||
$builder->whereIn('address_type', [
|
||||
self::ADDRESS_TYPE_BILLING,
|
||||
self::ADDRESS_TYPE_SHIPPING
|
||||
]);
|
||||
});
|
||||
|
||||
static::creating(static function ($address) {
|
||||
switch ($address->address_type) {
|
||||
case CartAddress::ADDRESS_TYPE_BILLING:
|
||||
$address->address_type = self::ADDRESS_TYPE_BILLING;
|
||||
break;
|
||||
case CartAddress::ADDRESS_TYPE_SHIPPING:
|
||||
$address->address_type = self::ADDRESS_TYPE_SHIPPING;
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
parent::boot();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ class Shipment extends Model implements ShipmentContract
|
|||
}
|
||||
|
||||
/**
|
||||
* Get the addresses for the shipment.
|
||||
* Get the address for the shipment.
|
||||
*/
|
||||
public function address()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -18,6 +18,25 @@ use Webkul\Shipping\Carriers\Free;
|
|||
|
||||
class AddressCest
|
||||
{
|
||||
public function testAddressFilters(UnitTester $I): void
|
||||
{
|
||||
$cartAddress = $I->have(CartAddress::class);
|
||||
$customerAddress = $I->have(CustomerAddress::class);
|
||||
$orderAddress = $I->have(OrderAddress::class);
|
||||
|
||||
$cartAddresses = CartAddress::all();
|
||||
$I->assertCount(1, $cartAddresses);
|
||||
$I->assertEquals($cartAddress->id, $cartAddresses[0]->id);
|
||||
|
||||
$customerAddresses = CustomerAddress::all();
|
||||
$I->assertCount(1, $customerAddresses);
|
||||
$I->assertEquals($customerAddress->id, $customerAddresses[0]->id);
|
||||
|
||||
$orderAddresses = OrderAddress::all();
|
||||
$I->assertCount(1, $orderAddresses);
|
||||
$I->assertEquals($orderAddress->id, $orderAddresses[0]->id);
|
||||
}
|
||||
|
||||
public function testCustomerAddressRelations(UnitTester $I): void
|
||||
{
|
||||
/** @var Customer $customer1 */
|
||||
|
|
|
|||
Loading…
Reference in New Issue