Initial import
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\models;
|
||||
|
||||
use app\models\parsed\CartModel;
|
||||
use app\services\Cart;
|
||||
|
||||
/**
|
||||
* Class Order
|
||||
* @package app\models
|
||||
* @property $id
|
||||
* @property $user_id
|
||||
* @property $order_date
|
||||
* @property $total_price
|
||||
* @property $user_email
|
||||
* @property $user_name
|
||||
* @property \app\models\UserPublic $user
|
||||
* @property \app\models\OrderPayment $payment
|
||||
* @property \app\models\OrderBooking[] $booking
|
||||
*/
|
||||
class Order extends _Base
|
||||
{
|
||||
public function getUser()
|
||||
{
|
||||
return $this->hasOne(UserPublic::class, ['id' => 'user_id']);
|
||||
}
|
||||
|
||||
public static function record($data, $user)
|
||||
{
|
||||
$cart_data = json_decode($data['cart_data']);
|
||||
$invoice_data = json_decode($data['invoice_data']);
|
||||
if (sizeof($cart_data) == 0) {
|
||||
header('Location: /');
|
||||
exit;
|
||||
}
|
||||
$order = new Order();
|
||||
$order->user_id = $user->id;
|
||||
$order->order_date = date('Y-m-d H:i:s');
|
||||
|
||||
$order->save();
|
||||
$totalPrice = 0;
|
||||
|
||||
foreach ($cart_data as $cartElement) {
|
||||
$cartObject = Cart::decodeKey($cartElement);
|
||||
//Order Booking
|
||||
|
||||
if ($cartObject->model == OrderBooking::class) {
|
||||
$booking = OrderBooking::findOne($cartObject->id);
|
||||
if ($booking) {
|
||||
$booking->order_id = $order->id;
|
||||
$booking->save();
|
||||
$totalPrice += $booking->single_price;
|
||||
}
|
||||
} else {
|
||||
|
||||
/** @var RegisterObjects|\app\models\register\Collections|Subscriptions $modelClass */
|
||||
$modelClass = $cartObject->model;
|
||||
$model = $modelClass::findOne($cartObject->id);
|
||||
if ($model) {
|
||||
$cartModel = $model->setCart(new CartModel());
|
||||
$product = new OrderProduct();
|
||||
$product->name_bg = $cartModel->name_bg;
|
||||
$product->name_en = $cartModel->name_en;
|
||||
$product->text_bg = $cartModel->text_bg;
|
||||
$product->text_en = $cartModel->text_en;
|
||||
$product->order_id = $order->id;
|
||||
$product->price = $cartModel->price;
|
||||
$product->model_class = $modelClass;
|
||||
$product->model_id = $model->id;
|
||||
$product->save();
|
||||
$totalPrice += $cartModel->price;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
OrderInvoice::record($invoice_data, $order->id);
|
||||
$order->total_price = $totalPrice;
|
||||
$order->save();
|
||||
return $order;
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return 'HeritageBG order';
|
||||
}
|
||||
|
||||
public function getPayment()
|
||||
{
|
||||
return $this->hasOne(OrderPayment::class, ['order_id' => 'id']);
|
||||
}
|
||||
|
||||
public function getBooking()
|
||||
{
|
||||
return $this->hasMany(OrderBooking::class, ['order_id' => 'id']);
|
||||
}
|
||||
|
||||
public function statusStr()
|
||||
{
|
||||
$lg = \Yii::$app->language;
|
||||
if($this->payment && $this->payment->status == 'PAID') {
|
||||
return $lg == 'en' ? 'Paid' : 'Платено';
|
||||
}
|
||||
return $lg == 'en' ? 'Payment pending' : 'В очакване на плащане';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user