HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/database/seeds/SampleOrders.php
<?php
namespace App\seeds;

use App\Addresses\Models\Address;
use App\Cart\ShoppingCart;
use App\Cart\ShoppingCartInterface;
use App\Checkout\CheckoutService;
use App\Orders\Models\Order;
use App\Orders\OrderStatus;
use App\Payment\Transaction;
use App\Payment\TransactionStatus;
use App\Products\AbstractProductable;
use App\Products\Product\Product;
use App\Users\SiteUser;
use App\Vat\VatScenarioEnum;
use Illuminate\Support\Facades\URL;
use Komma\KMS\Sites\SiteServiceInterface;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Seeder;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

/**
 * Class SampleOrders
 *
 * Needed for showcasing. Not for testing.
 *
 * @package App\seeds
 */
class SampleOrders extends Seeder
{
    /**
     * @throws \Throwable
     */
    public function run()
    {
        Auth::login(SiteUser::first());

        $checkoutService = new CheckoutService();
        $shoppingCart = app(ShoppingCartInterface::class);
        /** @var SiteServiceInterface  $siteService */
        $siteService = app(SiteServiceInterface::class);
        $siteService->setCurrentSiteToDefault();

        //Create very specific orders
        $shoppingCart->clear();
        $this->putHighVatProductInCart($shoppingCart);
        $this->createOrderFromShoppingCart($shoppingCart, $checkoutService);

        $shoppingCart->clear();
        $this->putLowVatProductInCart($shoppingCart);
        $this->createOrderFromShoppingCart($shoppingCart, $checkoutService);

        $shoppingCart->clear();
        $this->putHighVatProductInCart($shoppingCart);
        $this->putLowVatProductInCart($shoppingCart);
        $this->createOrderFromShoppingCart($shoppingCart, $checkoutService);

        $shoppingCart->clear();
        $this->putHighVatProductInCart($shoppingCart, 2, 3);
        $this->createOrderFromShoppingCart($shoppingCart, $checkoutService);

        //Create random orders.
        $sampleOrderAmount = 25;
        for ($index = 0; $index < $sampleOrderAmount; $index++) {
            $shoppingCart->clear();
            $this->putRandomProductablesToCart($shoppingCart);
            $this->createOrderFromShoppingCart($shoppingCart, $checkoutService);
        }
    }

    /**
     * Use the checkoutService and shoppingCart to create an order.
     *
     * @param ShoppingCartInterface $shoppingCart
     * @param CheckoutService       $checkoutService
     */
    private function createOrderFromShoppingCart(ShoppingCartInterface $shoppingCart, CheckoutService $checkoutService) {
        //Create a Customer first
        /** @var SiteUser $user */
        $user = factory(SiteUser::class)->make();
        $user->save();

        //Create some addresses for the order
        /** @var Address $addressForShipping */
        $addressForShipping = factory(Address::class)->make();
        $addressForShipping->siteUser()->associate($user);
        $addressForShipping->save();
        /** @var Address $addressForShipping */
        $addressForInvoice = factory(Address::class)->make();
        $addressForInvoice->siteUser()->associate($user);
        $addressForInvoice->save();

        //Create the order
        Model::reguard();
        $order = $checkoutService->createOrder($shoppingCart, $user, $addressForShipping, $addressForInvoice);
        Model::unguard();

        //And create a fake transaction for the order
        $this->createFakeTransaction($user, $order);
    }

    /**
     * @param ShoppingCartInterface $shoppingCart
     * @param int                   $quantityUnique
     * @param int                   $quantityPerProductable
     */
    private function putHighVatProductInCart(ShoppingCartInterface $shoppingCart, int $quantityUnique = 1, int $quantityPerProductable = 1) {
        Product::where('vat_scenario_enum', '=', VatScenarioEnum::high_inc)
//            ->orWhere('vat_scenario_enum', '=', VatScenarioEnum::high_inc)
            ->inRandomOrder()
            ->take($quantityUnique)
            ->get()->map(function (AbstractProductable $productable) use ($shoppingCart, $quantityPerProductable) {
                $shoppingCart->addProductable($productable, $quantityPerProductable);
            });
    }

    /**
     * @param ShoppingCartInterface $shoppingCart
     * @param int                   $quantityUnique
     * @param int                   $quantityPerProductable
     */
    private function putLowVatProductInCart(ShoppingCartInterface $shoppingCart, int $quantityUnique = 1, int $quantityPerProductable = 1) {
        Product::where('vat_scenario_enum', '=', VatScenarioEnum::low_inc)
//            ->orWhere('vat_scenario_enum', '=', VatScenarioEnum::low_ex)
            ->inRandomOrder()
            ->take($quantityUnique)
            ->get()->map(function (AbstractProductable $productable) use ($shoppingCart, $quantityPerProductable) {
                $shoppingCart->addProductable($productable, $quantityPerProductable);
            });
    }

    /**
     * @param ShoppingCartInterface $shoppingCart
     */
    private function putRandomProductablesToCart(ShoppingCartInterface $shoppingCart) {
        $productablesToAdd = mt_rand(1, 3);
        for($productableCounter = 0; $productableCounter < $productablesToAdd; $productableCounter++) {
            //Create a random productable with all sub models
            switch (mt_rand(1, 1)) {
                case 1:
                    $productable = Product::inRandomOrder()->first();
                    break;
//                case 2:
//                    $productable = ProductGroup::inRandomOrder()->first();
//                    break;
//                case 3:
//                    $productable = ProductComposite::inRandomOrder()->first();
//                    break;
            }

            $shoppingCart->addProductable($productable);
        }
    }

    /**
     * @param SiteUser $user
     * @param Order    $order
     *
     * @return Transaction
     */
    private function createFakeTransaction(SiteUser $user, Order $order): Transaction {
        $status = Arr::random(TransactionStatus::getAsArray());

        $fakeTransaction = new Transaction([
            'order_id' => $order->id,
            'amount' => $order->total,
            'psp_payment_reference' => Str::random(),
            'psp' => 'example PSP',
            'psp_id' => mt_rand(10000, 99999),
            'ip' => request()->ip(),
            'currency_iso_4217_code' => 'EUR',
            'error_code' => '',
            'account_reference' => '',
            'account_holder_name' => $user->first_name . ' ' . $user->last_name,
            'acccount_brand' => 'fake',
            'issuer_id' => 'somebank',
            'payment_method' => 'KommaPal',
            'status' => $status,
            'payment_link' => (app()->environment() === 'production') ? URL::to('/') : route('fakepsp', ['order' => $order]),
            'expire_date' => Carbon::now()->addMinutes(15)->toDateTimeString(),
            'payment_date' => ($status == OrderStatus::COMPLETED) ? Carbon::now()->addMinutes(1)->toDateTimeString() : null
        ]);
        $fakeTransaction->save();

        return $order->transactions()->save($fakeTransaction);
    }
}