first upload all files
This commit is contained in:
289
Modules/Order/Entities/Order.php
Normal file
289
Modules/Order/Entities/Order.php
Normal file
@@ -0,0 +1,289 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Order\Entities;
|
||||
|
||||
use Modules\Cart\CartTax;
|
||||
use Modules\Cart\CartItem;
|
||||
use Modules\Support\Money;
|
||||
use Modules\Support\State;
|
||||
use Modules\Support\Country;
|
||||
use Modules\Media\Entities\File;
|
||||
use Modules\Tax\Entities\TaxRate;
|
||||
use Illuminate\Support\Collection;
|
||||
use Modules\Order\OrderCollection;
|
||||
use Modules\Coupon\Entities\Coupon;
|
||||
use Modules\Order\Admin\OrderTable;
|
||||
use Modules\Support\Eloquent\Model;
|
||||
use Modules\Payment\Facades\Gateway;
|
||||
use Modules\Payment\HasTransactionReference;
|
||||
use Modules\Shipping\Facades\ShippingMethod;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Modules\Transaction\Entities\Transaction;
|
||||
|
||||
class Order extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
const CANCELED = 'canceled';
|
||||
const COMPLETED = 'completed';
|
||||
const ON_HOLD = 'on_hold';
|
||||
const PENDING = 'pending';
|
||||
const PENDING_PAYMENT = 'pending_payment';
|
||||
const PROCESSING = 'processing';
|
||||
const REFUNDED = 'refunded';
|
||||
|
||||
/**
|
||||
* The attributes that aren't mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be mutated to dates.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $dates = ['start_date', 'end_date', 'deleted_at'];
|
||||
|
||||
public static function totalSales()
|
||||
{
|
||||
return Money::inDefaultCurrency(self::withoutCanceledOrders()->sum('total'));
|
||||
}
|
||||
|
||||
public function status()
|
||||
{
|
||||
return trans("order::statuses.{$this->status}");
|
||||
}
|
||||
|
||||
public function hasShippingMethod()
|
||||
{
|
||||
return !is_null($this->shipping_method);
|
||||
}
|
||||
|
||||
public function hasCoupon()
|
||||
{
|
||||
return !is_null($this->coupon);
|
||||
}
|
||||
|
||||
public function totalTax()
|
||||
{
|
||||
$total = 0;
|
||||
|
||||
if ($this->hasTax()) {
|
||||
$this->taxes()
|
||||
->get()
|
||||
->each(function ($tax) use (&$total) {
|
||||
$total += $tax->order_tax->amount->amount();
|
||||
});
|
||||
}
|
||||
|
||||
return Money::inDefaultCurrency($total);
|
||||
}
|
||||
|
||||
public function hasTax()
|
||||
{
|
||||
return $this->taxes->isNotEmpty();
|
||||
}
|
||||
|
||||
public function salesAnalytics()
|
||||
{
|
||||
return $this->normalizeOrders($this->ordersByWeekDay())->mapWithKeys(function ($orders, $weekDay) {
|
||||
return [$weekDay => $this->dataForChart($orders)];
|
||||
});
|
||||
}
|
||||
|
||||
private function ordersByWeekDay()
|
||||
{
|
||||
return self::select('total', 'created_at')
|
||||
->withoutCanceledOrders()
|
||||
->whereBetween('created_at', [now()->startOfWeek(), now()->endOfWeek()])
|
||||
->get()
|
||||
->reduce(function ($ordersByWeekDay, $order) {
|
||||
$ordersByWeekDay[$order->created_at->weekday()][] = $order;
|
||||
|
||||
return $ordersByWeekDay;
|
||||
});
|
||||
}
|
||||
|
||||
private function normalizeOrders($orders)
|
||||
{
|
||||
return Collection::times(7)->map(function ($dayOfWeek) use ($orders) {
|
||||
return new OrderCollection($orders[$dayOfWeek] ?? []);
|
||||
});
|
||||
}
|
||||
|
||||
private function dataForChart(OrderCollection $orders)
|
||||
{
|
||||
return [
|
||||
'total' => $orders->sumTotal(),
|
||||
'total_orders' => $orders->count(),
|
||||
];
|
||||
}
|
||||
|
||||
public function products()
|
||||
{
|
||||
return $this->hasMany(OrderProduct::class);
|
||||
}
|
||||
|
||||
public function downloads()
|
||||
{
|
||||
return $this->hasMany(OrderDownload::class);
|
||||
}
|
||||
|
||||
public function coupon()
|
||||
{
|
||||
return $this->belongsTo(Coupon::class)->withTrashed();
|
||||
}
|
||||
|
||||
public function taxes()
|
||||
{
|
||||
return $this->belongsToMany(TaxRate::class, 'order_taxes')
|
||||
->using(OrderTax::class)
|
||||
->as('order_tax')
|
||||
->withPivot('amount')
|
||||
->withTrashed();
|
||||
}
|
||||
|
||||
public function transaction()
|
||||
{
|
||||
return $this->hasOne(Transaction::class)->withTrashed();
|
||||
}
|
||||
|
||||
public function getSubTotalAttribute($subTotal)
|
||||
{
|
||||
return Money::inDefaultCurrency($subTotal);
|
||||
}
|
||||
|
||||
public function getShippingCostAttribute($shippingCost)
|
||||
{
|
||||
return Money::inDefaultCurrency($shippingCost);
|
||||
}
|
||||
|
||||
public function getDiscountAttribute($discount)
|
||||
{
|
||||
return Money::inDefaultCurrency($discount);
|
||||
}
|
||||
|
||||
public function getTaxAttribute($tax)
|
||||
{
|
||||
return Money::inDefaultCurrency($tax);
|
||||
}
|
||||
|
||||
public function getTotalAttribute($total)
|
||||
{
|
||||
return Money::inDefaultCurrency($total);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the order's shipping method.
|
||||
*
|
||||
* @param string $shippingMethod
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getShippingMethodAttribute($shippingMethod)
|
||||
{
|
||||
return ShippingMethod::get($shippingMethod)->label ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the order's payment method.
|
||||
*
|
||||
* @param string $paymentMethod
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPaymentMethodAttribute($paymentMethod)
|
||||
{
|
||||
return Gateway::get($paymentMethod)->label ?? '';
|
||||
}
|
||||
|
||||
public function getCustomerFullNameAttribute()
|
||||
{
|
||||
return "{$this->customer_first_name} {$this->customer_last_name}";
|
||||
}
|
||||
|
||||
public function getBillingFullNameAttribute()
|
||||
{
|
||||
return "{$this->billing_first_name} {$this->billing_last_name}";
|
||||
}
|
||||
|
||||
public function getShippingFullNameAttribute()
|
||||
{
|
||||
return "{$this->shipping_first_name} {$this->shipping_last_name}";
|
||||
}
|
||||
|
||||
public function getBillingCountryNameAttribute()
|
||||
{
|
||||
return Country::name($this->billing_country);
|
||||
}
|
||||
|
||||
public function getShippingCountryNameAttribute()
|
||||
{
|
||||
return Country::name($this->shipping_country);
|
||||
}
|
||||
|
||||
public function getBillingStateNameAttribute()
|
||||
{
|
||||
return State::name($this->billing_country, $this->billing_state);
|
||||
}
|
||||
|
||||
public function getShippingStateNameAttribute()
|
||||
{
|
||||
return State::name($this->shipping_country, $this->shipping_state);
|
||||
}
|
||||
|
||||
public function scopeWithoutCanceledOrders($query)
|
||||
{
|
||||
return $query->whereNotIn('status', [self::CANCELED, self::REFUNDED]);
|
||||
}
|
||||
|
||||
public function storeProducts(CartItem $cartItem)
|
||||
{
|
||||
$orderProduct = $this->products()->create([
|
||||
'product_id' => $cartItem->product->id,
|
||||
'unit_price' => $cartItem->unitPrice()->amount(),
|
||||
'qty' => $cartItem->qty,
|
||||
'line_total' => $cartItem->total()->amount(),
|
||||
]);
|
||||
|
||||
$orderProduct->storeOptions($cartItem->options);
|
||||
}
|
||||
|
||||
public function storeDownloads(CartItem $cartItem)
|
||||
{
|
||||
$cartItem->product->downloads->each(function (File $file) {
|
||||
$this->downloads()->create(['file_id' => $file->id]);
|
||||
});
|
||||
}
|
||||
|
||||
public function attachTax(CartTax $cartTax)
|
||||
{
|
||||
$this->taxes()->attach($cartTax->id(), ['amount' => $cartTax->amount()->amount()]);
|
||||
}
|
||||
|
||||
public function storeTransaction($response)
|
||||
{
|
||||
if (!$response instanceof HasTransactionReference) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->transaction()->create([
|
||||
'transaction_id' => $response->getTransactionReference(),
|
||||
'payment_method' => $this->attributes['payment_method'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get table data for the resource
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function table()
|
||||
{
|
||||
$query = $this->newQuery()->select(['id', 'customer_first_name', 'customer_last_name', 'customer_email', 'currency', 'total', 'status', 'created_at']);
|
||||
|
||||
return new OrderTable($query);
|
||||
}
|
||||
}
|
||||
45
Modules/Order/Entities/OrderDownload.php
Normal file
45
Modules/Order/Entities/OrderDownload.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Order\Entities;
|
||||
|
||||
use Modules\Media\Entities\File;
|
||||
use Modules\Support\Eloquent\Model;
|
||||
|
||||
class OrderDownload extends Model
|
||||
{
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $with = ['file'];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['file_id'];
|
||||
|
||||
public function getRealPathAttribute()
|
||||
{
|
||||
return $this->file->realPath();
|
||||
}
|
||||
|
||||
public function getFilenameAttribute()
|
||||
{
|
||||
return $this->file->file_name;
|
||||
}
|
||||
|
||||
public function file()
|
||||
{
|
||||
return $this->belongsTo(File::class);
|
||||
}
|
||||
}
|
||||
112
Modules/Order/Entities/OrderProduct.php
Normal file
112
Modules/Order/Entities/OrderProduct.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Order\Entities;
|
||||
|
||||
use Modules\Support\Money;
|
||||
use Modules\Support\Eloquent\Model;
|
||||
use Modules\Product\Entities\Product;
|
||||
|
||||
class OrderProduct extends Model
|
||||
{
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $with = ['product', 'options'];
|
||||
|
||||
/**
|
||||
* The attributes that aren't mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
public function url()
|
||||
{
|
||||
return route('products.show', ['slug' => $this->product->slug]);
|
||||
}
|
||||
|
||||
public function hasAnyOption()
|
||||
{
|
||||
return $this->options->isNotEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if order product has been deleted.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function trashed()
|
||||
{
|
||||
return $this->product->trashed();
|
||||
}
|
||||
|
||||
/**
|
||||
* Store order product's options.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Collection $options
|
||||
* @return void
|
||||
*/
|
||||
public function storeOptions($options)
|
||||
{
|
||||
$options->each(function ($option) {
|
||||
$orderProductOption = $this->options()->create([
|
||||
'order_product_id' => $this->id,
|
||||
'option_id' => $option->id,
|
||||
'value' => $option->isFieldType() ? $option->values->first()->label : null,
|
||||
]);
|
||||
|
||||
$orderProductOption->storeValues($this->product, $option->values);
|
||||
});
|
||||
}
|
||||
|
||||
public function product()
|
||||
{
|
||||
return $this->belongsTo(Product::class)
|
||||
->withoutGlobalScope('active')
|
||||
->withTrashed();
|
||||
}
|
||||
|
||||
public function options()
|
||||
{
|
||||
return $this->hasMany(OrderProductOption::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the order product's name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getNameAttribute()
|
||||
{
|
||||
return $this->product->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the order product's slug.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSlugAttribute()
|
||||
{
|
||||
return $this->product->slug;
|
||||
}
|
||||
|
||||
public function getUnitPriceAttribute($unitPrice)
|
||||
{
|
||||
return Money::inDefaultCurrency($unitPrice);
|
||||
}
|
||||
|
||||
public function getLineTotalAttribute($total)
|
||||
{
|
||||
return Money::inDefaultCurrency($total);
|
||||
}
|
||||
}
|
||||
64
Modules/Order/Entities/OrderProductOption.php
Normal file
64
Modules/Order/Entities/OrderProductOption.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Order\Entities;
|
||||
|
||||
use Modules\Option\Entities\Option;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Modules\Option\Entities\OptionValue;
|
||||
|
||||
class OrderProductOption extends Model
|
||||
{
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $with = ['option', 'values'];
|
||||
|
||||
/**
|
||||
* The attributes that aren't mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
public function option()
|
||||
{
|
||||
return $this->belongsTo(Option::class)->withTrashed();
|
||||
}
|
||||
|
||||
public function values()
|
||||
{
|
||||
return $this->belongsToMany(OptionValue::class, 'order_product_option_values')
|
||||
->using(OrderProductOptionValue::class)
|
||||
->withPivot('price');
|
||||
}
|
||||
|
||||
public function getNameAttribute()
|
||||
{
|
||||
return $this->option->name;
|
||||
}
|
||||
|
||||
public function isFieldType()
|
||||
{
|
||||
return $this->option->isFieldType();
|
||||
}
|
||||
|
||||
public function storeValues($product, $values)
|
||||
{
|
||||
$values = $values->mapWithKeys(function (OptionValue $value) use ($product) {
|
||||
return [$value->id => [
|
||||
'price' => $value->priceForProduct($product)->amount(),
|
||||
]];
|
||||
});
|
||||
|
||||
$this->values()->attach($values->all());
|
||||
}
|
||||
}
|
||||
14
Modules/Order/Entities/OrderProductOptionValue.php
Normal file
14
Modules/Order/Entities/OrderProductOptionValue.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Order\Entities;
|
||||
|
||||
use Modules\Support\Money;
|
||||
use Illuminate\Database\Eloquent\Relations\Pivot;
|
||||
|
||||
class OrderProductOptionValue extends Pivot
|
||||
{
|
||||
public function getPriceAttribute($price)
|
||||
{
|
||||
return Money::inDefaultCurrency($price);
|
||||
}
|
||||
}
|
||||
14
Modules/Order/Entities/OrderTax.php
Normal file
14
Modules/Order/Entities/OrderTax.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Order\Entities;
|
||||
|
||||
use Modules\Support\Money;
|
||||
use Illuminate\Database\Eloquent\Relations\Pivot;
|
||||
|
||||
class OrderTax extends Pivot
|
||||
{
|
||||
public function getAmountAttribute($amount)
|
||||
{
|
||||
return Money::inDefaultCurrency($amount);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user