sarga/packages/Webkul/Sales/src/Models/Order.php

216 lines
4.8 KiB
PHP
Raw Normal View History

<?php
namespace Webkul\Sales\Models;
use Illuminate\Database\Eloquent\Model;
use Webkul\Sales\Contracts\Order as OrderContract;
class Order extends Model implements OrderContract
{
2018-10-15 10:39:09 +00:00
protected $guarded = ['id', 'items', 'shipping_address', 'billing_address', 'customer', 'channel', 'payment', 'created_at', 'updated_at'];
protected $statusLabel = [
'pending' => 'Pending',
2018-11-16 10:11:08 +00:00
'pending_payment' => 'Pending Payment',
'processing' => 'Processing',
2018-10-15 10:39:09 +00:00
'completed' => 'Completed',
'canceled' => 'Canceled',
'closed' => 'Closed',
2018-11-16 10:11:08 +00:00
'fraud' => 'Fraud'
];
/**
* Get the order items record associated with the order.
*/
public function getCustomerFullNameAttribute()
{
return $this->customer_first_name . ' ' . $this->customer_last_name;
}
/**
* Returns the status label from status code
*/
public function getStatusLabelAttribute()
{
return $this->statusLabel[$this->status];
}
2018-10-15 10:39:09 +00:00
/**
* Return base total due amount
*/
public function getBaseTotalDueAttribute()
{
2019-09-19 09:56:07 +00:00
return $this->base_grand_total - $this->base_grand_total_invoiced;
2018-10-15 10:39:09 +00:00
}
/**
* Return total due amount
*/
public function getTotalDueAttribute()
{
2019-09-19 09:56:07 +00:00
return $this->grand_total - $this->grand_total_invoiced;
2018-10-15 10:39:09 +00:00
}
/**
* Get the order items record associated with the order.
*/
public function items()
{
2018-10-05 11:59:43 +00:00
return $this->hasMany(OrderItemProxy::modelClass())->whereNull('parent_id');
}
/**
* Get the order shipments record associated with the order.
*/
public function shipments()
{
return $this->hasMany(ShipmentProxy::modelClass());
}
/**
* Get the order invoices record associated with the order.
*/
public function invoices()
{
return $this->hasMany(InvoiceProxy::modelClass());
}
2019-09-18 13:27:40 +00:00
/**
* Get the order refunds record associated with the order.
*/
public function refunds()
{
return $this->hasMany(RefundProxy::modelClass());
}
/**
* Get the customer record associated with the order.
*/
public function customer()
{
return $this->morphTo();
}
/**
* Get the addresses for the order.
*/
public function addresses()
{
return $this->hasMany(OrderAddressProxy::modelClass());
}
2018-10-04 06:42:06 +00:00
/**
* Get the payment for the order.
*/
public function payment()
{
return $this->hasOne(OrderPaymentProxy::modelClass());
2018-10-04 06:42:06 +00:00
}
/**
* Get the biling address for the order.
*/
public function billing_address()
{
return $this->addresses()->where('address_type', 'billing');
}
/**
* Get billing address for the order.
*/
public function getBillingAddressAttribute()
{
return $this->billing_address()->first();
}
/**
* Get the shipping address for the order.
*/
public function shipping_address()
{
return $this->addresses()->where('address_type', 'shipping');
}
/**
* Get shipping address for the order.
*/
public function getShippingAddressAttribute()
{
return $this->shipping_address()->first();
}
/**
* Get the channel record associated with the order.
*/
public function channel()
{
return $this->morphTo();
}
/**
* Checks if new shipment is allow or not
*/
public function canShip()
{
2019-01-15 11:54:41 +00:00
if ($this->status == 'fraud')
2018-11-16 10:11:08 +00:00
return false;
foreach ($this->items as $item) {
2019-09-18 13:27:40 +00:00
if ($item->qty_to_ship > 0)
return true;
}
return false;
}
/**
* Checks if new invoice is allow or not
*/
public function canInvoice()
{
2019-01-15 11:54:41 +00:00
if ($this->status == 'fraud')
2018-11-16 10:11:08 +00:00
return false;
foreach ($this->items as $item) {
2019-09-18 13:27:40 +00:00
if ($item->qty_to_invoice > 0)
return true;
}
return false;
}
2018-10-15 10:39:09 +00:00
/**
2019-09-18 13:27:40 +00:00
* Checks if order can be canceled or not
2018-10-15 10:39:09 +00:00
*/
public function canCancel()
{
2019-01-15 11:54:41 +00:00
if ($this->status == 'fraud')
2018-11-16 10:11:08 +00:00
return false;
2019-01-15 11:54:41 +00:00
foreach ($this->items as $item) {
2019-09-18 13:27:40 +00:00
if ($item->qty_to_cancel > 0)
2018-10-15 10:39:09 +00:00
return true;
}
return false;
}
2019-09-18 13:27:40 +00:00
/**
* Checks if order can be refunded or not
*/
public function canRefund()
{
if ($this->status == 'fraud')
return false;
foreach ($this->items as $item) {
if ($item->qty_to_refund > 0)
return true;
}
2019-09-19 08:22:33 +00:00
if ($this->base_grand_total_invoiced - $this->base_grand_total_refunded - $this->refunds()->sum('base_adjustment_fee') > 0)
2019-09-18 13:27:40 +00:00
return true;
return false;
}
}