Skip to content

A free open source IT asset/license management system

5.5 KB
Raw Blame
<?php

namespace Database\Factories;

use App\Models\Category;
use App\Models\Company;
use App\Models\Consumable;
use App\Models\Manufacturer;
use App\Models\Supplier;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;

class ConsumableFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var class-string<Consumable>
     */
    protected $model = Consumable::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'category_id' => Category::factory(),
            'company_id' => Company::factory(),
            'created_by' => User::factory()->superuser(),
            'item_no' => $this->faker->numberBetween(1000000, 50000000),
            'min_amt' => $this->faker->numberBetween($min = 1, $max = 2),
            'name' => $this->faker->words(3, true),
            'default_purchase_cost' => $this->faker->randomFloat(2, 1, 50),
            'legacy_purchase_date' => $this->faker->dateTimeBetween('-1 years', 'now', date_default_timezone_get())->format('Y-m-d'),
            'qty' => $this->faker->numberBetween(5, 10),
            'default_supplier_id' => Supplier::factory(),
        ];
    }

    public function cardstock()
    {
        return $this->state(function () {
            return [
                'name' => 'Cardstock (White)',
                'category_id' => function () {
                    return Category::where('name', 'Printer Paper')->first() ?? Category::factory()->consumablePaperCategory();
                },
                'manufacturer_id' => function () {
                    return Manufacturer::where('name', 'Avery')->first() ?? Manufacturer::factory()->avery();
                },
                'qty' => 10,
                'min_amt' => 2,
            ];
        });
    }

    public function paper()
    {
        return $this->state(function () {
            return [
                'name' => 'Laserjet Paper (Ream)',
                'category_id' => function () {
                    return Category::where('name', 'Printer Paper')->first() ?? Category::factory()->consumablePaperCategory();
                },
                'manufacturer_id' => function () {
                    return Manufacturer::where('name', 'Avery')->first() ?? Manufacturer::factory()->avery();
                },
                'qty' => 20,
                'min_amt' => 2,
            ];
        });
    }

    public function ink()
    {
        return $this->state(function () {
            return [
                'name' => 'Laserjet Toner (black)',
                'category_id' => function () {
                    return Category::where('name', 'Printer Ink')->first() ?? Category::factory()->consumableInkCategory();
                },
                'manufacturer_id' => function () {
                    return Manufacturer::where('name', 'HP')->first() ?? Manufacturer::factory()->hp();
                },
                'qty' => 20,
                'min_amt' => 2,
            ];
        });
    }

    public function withoutItemsRemaining()
    {
        return $this->state(function () {
            return [
                'qty' => 1,
            ];
        })->afterCreating(function (Consumable $consumable) {
            $user = User::factory()->create();

            $consumable->users()->attach($consumable->id, [
                'consumable_id' => $consumable->id,
                'created_by' => $user->id,
                'assigned_to' => $user->id,
                'note' => '',
            ]);
        });
    }

    public function requiringAcceptance()
    {
        return $this->afterCreating(function (Consumable $consumable) {
            $consumable->category->update(['require_acceptance' => 1]);
        });
    }

    public function notRequiringAcceptance()
    {
        return $this->afterCreating(function (Consumable $consumable) {
            $consumable->category->update(['require_acceptance' => 0]);
        });
    }

    public function archived(): self
    {
        return $this->state(fn () => ['archived' => true]);
    }

    public function checkedOutToUser(?User $user = null)
    {
        return $this->afterCreating(function (Consumable $consumable) use ($user) {
            $consumable->users()->attach($consumable->id, [
                'consumable_id' => $consumable->id,
                'created_at' => Carbon::now(),
                'created_by' => User::factory()->create()->id,
                'assigned_to' => $user->id ?? User::factory()->create()->id,
            ]);
        });
    }

    /**
     * See ComponentFactory::withInitialAcquisition for docs.
     */
    public function withInitialAcquisition(
        ?Supplier $supplier = null,
        ?float $unitCost = null,
        ?string $purchaseDate = null,
    ) {
        return $this->afterCreating(function (Consumable $consumable) use ($supplier, $unitCost, $purchaseDate) {
            $line = $consumable->orderItems()->latest('id')->first();
            if (! $line) {
                return;
            }
            if ($unitCost !== null) {
                $line->price = $unitCost;
                $line->save();
            }
            $order = $line->order;
            if (! $order) {
                return;
            }
            if ($supplier !== null) {
                $order->supplier_id = $supplier->id;
            }
            if ($purchaseDate !== null) {
                $order->purchase_date = Carbon::parse($purchaseDate);
            }
            $order->save();
        });
    }
}