diff --git a/packages/Webkul/Checkout/src/Models/Cart.php b/packages/Webkul/Checkout/src/Models/Cart.php index 05ff14562..dbc924c63 100755 --- a/packages/Webkul/Checkout/src/Models/Cart.php +++ b/packages/Webkul/Checkout/src/Models/Cart.php @@ -48,7 +48,7 @@ class Cart extends Model implements CartContract */ public function billing_address() { - return $this->addresses()->where('address_type', 'billing'); + return $this->addresses()->where('address_type', CartAddress::ADDRESS_TYPE_BILLING); } /** @@ -64,7 +64,7 @@ class Cart extends Model implements CartContract */ public function shipping_address() { - return $this->addresses()->where('address_type', 'shipping'); + return $this->addresses()->where('address_type', CartAddress::ADDRESS_TYPE_SHIPPING); } /** diff --git a/packages/Webkul/Checkout/src/Models/CartAddress.php b/packages/Webkul/Checkout/src/Models/CartAddress.php index 307526c9a..bcc4cb7f1 100755 --- a/packages/Webkul/Checkout/src/Models/CartAddress.php +++ b/packages/Webkul/Checkout/src/Models/CartAddress.php @@ -2,29 +2,29 @@ namespace Webkul\Checkout\Models; -use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; use Webkul\Checkout\Contracts\CartAddress as CartAddressContract; +use Webkul\Core\Models\Address; -class CartAddress extends Model implements CartAddressContract +class CartAddress extends Address implements CartAddressContract { - protected $table = 'cart_address'; + public const ADDRESS_TYPE_SHIPPING = 'cart_address_shipping'; + public const ADDRESS_TYPE_BILLING = 'cart_address_billing'; - protected $fillable = [ - 'first_name', - 'last_name', - 'email', - 'company_name', - 'vat_id', - 'address1', - 'city', - 'state', - 'postcode', - 'country', - 'phone', - 'address_type', - 'cart_id', - 'customer_id', - ]; + /** + * The "booted" method of the model. + * + * @return void + */ + protected static function booted() + { + static::addGlobalScope('address_type', function (Builder $builder) { + $builder->whereIn('address_type', [ + self::ADDRESS_TYPE_BILLING, + self::ADDRESS_TYPE_SHIPPING + ]); + }); + } /** * Get the shipping rates for the cart address. @@ -35,10 +35,10 @@ class CartAddress extends Model implements CartAddressContract } /** - * Get all of the attributes for the attribute groups. + * Get the cart record associated with the address. */ - public function getNameAttribute() + public function cart() { - return $this->first_name . ' ' . $this->last_name; + return $this->belongsTo(Cart::class); } } \ No newline at end of file diff --git a/packages/Webkul/Core/src/Database/Migrations/2020_04_16_185147_add_table_addresses.php b/packages/Webkul/Core/src/Database/Migrations/2020_04_16_185147_add_table_addresses.php new file mode 100644 index 000000000..feb7cc934 --- /dev/null +++ b/packages/Webkul/Core/src/Database/Migrations/2020_04_16_185147_add_table_addresses.php @@ -0,0 +1,215 @@ +increments('id'); + $table->string('address_type'); + $table->unsignedInteger('customer_id')->nullable()->comment('null if guest checkout'); + $table->unsignedInteger('cart_id')->nullable()->comment('only for cart_addresses'); + $table->unsignedInteger('order_id')->nullable()->comment('only for order_addresses'); + + $table->string('first_name'); + $table->string('last_name'); + $table->string('gender')->nullable(); + $table->string('company_name')->nullable(); + $table->string('address1'); + $table->string('address2')->nullable(); + $table->string('postcode'); + $table->string('city'); + $table->string('state'); + $table->string('country'); + $table->string('email')->nullable(); + $table->string('phone')->nullable(); + + $table->string('vat_id')->nullable(); + $table->boolean('default_address') + ->default(false) + ->comment('only for customer_addresses'); + + $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'); + }); + + $this->insertCustomerAddresses(); + $this->insertCartAddresses(); + $this->insertOrderAddresses(); + + Schema::drop('customer_addresses'); + Schema::drop('cart_address'); + Schema::drop('order_address'); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + throw new Exception('you cannot revert this migration: data would be lost'); + } + + private function insertCustomerAddresses(): void + { + $dbPrefix = DB::getTablePrefix(); + + $insertCustomerAddresses = <<< SQL + INSERT INTO ${dbPrefix}addresses( + address_type, + customer_id, + first_name, + last_name, + gender, + company_name, + address1, + address2, + postcode, + city, + state, + country, + email, + phone, + default_address, + created_at, + updated_at + ) + SELECT + "customer", + customer_id, + first_name, + last_name, + (SELECT gender FROM customers c WHERE c.id=ca.customer_id), + company_name, + address1, + address2, + postcode, + city, + state, + country, + null, + phone, + default_address, + created_at, + updated_at + FROM customer_addresses ca; +SQL; + + DB::unprepared($insertCustomerAddresses); + } + + private function insertCartAddresses(): void + { + $dbPrefix = DB::getTablePrefix(); + + $insertCustomerAddresses = <<< SQL + INSERT INTO ${dbPrefix}addresses( + address_type, + customer_id, + cart_id, + first_name, + last_name, + gender, + company_name, + address1, + address2, + postcode, + city, + state, + country, + email, + phone, + default_address, + created_at, + updated_at + ) + SELECT + (CASE ca.address_type='billing' THEN "cart_address_billing" ELSE "cart_address_shipping" END), + customer_id, + cart_id, + first_name, + last_name, + (SELECT gender FROM customers c WHERE c.id=ca.customer_id), + company_name, + address1, + address2, + postcode, + city, + state, + country, + email, + phone, + default_address, + created_at, + updated_at + FROM cart_address ca; +SQL; + + DB::unprepared($insertCustomerAddresses); + } + + private function insertOrderAddresses(): void + { + $dbPrefix = DB::getTablePrefix(); + + $insertCustomerAddresses = <<< SQL + INSERT INTO ${dbPrefix}addresses( + address_type, + customer_id, + order_id, + first_name, + last_name, + gender, + company_name, + address1, + address2, + postcode, + city, + state, + country, + email, + phone, + default_address, + created_at, + updated_at + ) + SELECT + (CASE ca.address_type='billing' THEN "order_address_billing" ELSE "order_address_shipping" END), + customer_id, + order_id, + first_name, + last_name, + (SELECT gender FROM customers c WHERE c.id=os.customer_id), + company_name, + address1, + address2, + postcode, + city, + state, + country, + email, + phone, + default_address, + created_at, + updated_at + FROM order_address oa; +SQL; + + DB::unprepared($insertCustomerAddresses); + } +} diff --git a/packages/Webkul/Core/src/Models/Address.php b/packages/Webkul/Core/src/Models/Address.php new file mode 100644 index 000000000..b882d2eec --- /dev/null +++ b/packages/Webkul/Core/src/Models/Address.php @@ -0,0 +1,52 @@ +first_name . ' ' . $this->last_name; + } + + /** + * Get the customer record associated with the address. + */ + public function customer() + { + return $this->belongsTo(Customer::class); + } +} diff --git a/packages/Webkul/Customer/src/Models/CustomerAddress.php b/packages/Webkul/Customer/src/Models/CustomerAddress.php index 389857a53..28cb96b35 100755 --- a/packages/Webkul/Customer/src/Models/CustomerAddress.php +++ b/packages/Webkul/Customer/src/Models/CustomerAddress.php @@ -2,34 +2,23 @@ namespace Webkul\Customer\Models; -use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; +use Webkul\Core\Models\Address; use Webkul\Customer\Contracts\CustomerAddress as CustomerAddressContract; -class CustomerAddress extends Model implements CustomerAddressContract +class CustomerAddress extends Address implements CustomerAddressContract { - protected $table = 'customer_addresses'; - - protected $fillable = [ - 'customer_id', - 'company_name', - 'vat_id', - 'address1', - 'address2', - 'country', - 'state', - 'city', - 'postcode', - 'phone', - 'default_address', - 'first_name', - 'last_name', - ]; + public const ADDRESS_TYPE = 'customer'; /** - * Get the customer address full name. + * The "booted" method of the model. + * + * @return void */ - public function getNameAttribute() + protected static function booted() { - return $this->first_name . ' ' . $this->last_name; + static::addGlobalScope('address_type', function (Builder $builder) { + $builder->where('address_type', self::ADDRESS_TYPE); + }); } } diff --git a/packages/Webkul/Sales/src/Models/Order.php b/packages/Webkul/Sales/src/Models/Order.php index 48d5725de..dfb9ab4d1 100755 --- a/packages/Webkul/Sales/src/Models/Order.php +++ b/packages/Webkul/Sales/src/Models/Order.php @@ -140,7 +140,7 @@ class Order extends Model implements OrderContract */ public function billing_address() { - return $this->addresses()->where('address_type', 'billing'); + return $this->addresses()->where('address_type', OrderAddress::ADDRESS_TYPE_BILLING); } /** @@ -156,7 +156,7 @@ class Order extends Model implements OrderContract */ public function shipping_address() { - return $this->addresses()->where('address_type', 'shipping'); + return $this->addresses()->where('address_type', OrderAddress::ADDRESS_TYPE_SHIPPING); } /** diff --git a/packages/Webkul/Sales/src/Models/OrderAddress.php b/packages/Webkul/Sales/src/Models/OrderAddress.php index d1bcddc62..a964e8ead 100755 --- a/packages/Webkul/Sales/src/Models/OrderAddress.php +++ b/packages/Webkul/Sales/src/Models/OrderAddress.php @@ -2,47 +2,35 @@ namespace Webkul\Sales\Models; -use Illuminate\Database\Eloquent\Model; -use Webkul\Customer\Models\Customer; +use Webkul\Core\Models\Address; use Webkul\Sales\Contracts\OrderAddress as OrderAddressContract; +use Illuminate\Database\Eloquent\Builder; -class OrderAddress extends Model implements OrderAddressContract +class OrderAddress extends Address implements OrderAddressContract { - protected $table = 'order_address'; - - protected $guarded = ['id', 'created_at', 'updated_at']; - - protected $fillable = [ - 'first_name', - 'last_name', - 'email', - 'company_name', - 'vat_id', - 'address1', - 'address2', - 'city', - 'state', - 'postcode', - 'country', - 'phone', - 'address_type', - 'cart_id', - 'customer_id', - ]; + public const ADDRESS_TYPE_SHIPPING = 'order_address_shipping'; + public const ADDRESS_TYPE_BILLING = 'order_address_billing'; /** - * Get of the customer fullname. + * The "booted" method of the model. + * + * @return void */ - public function getNameAttribute() + protected static function booted() { - return $this->first_name . ' ' . $this->last_name; + static::addGlobalScope('address_type', function (Builder $builder) { + $builder->whereIn('address_type', [ + self::ADDRESS_TYPE_BILLING, + self::ADDRESS_TYPE_SHIPPING + ]); + }); } /** - * Get the customer record associated with the order. + * Get the order record associated with the address. */ - public function customer() + public function order() { - return $this->belongsTo(Customer::class); + return $this->belongsTo(Order::class); } } \ No newline at end of file