Files
nnterp-react-admin/database/seeders/DemoContext.php
2026-03-14 19:30:05 +08:00

188 lines
6.2 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\User;
use App\Models\Warehouse;
use App\Models\SalesInvoice;
use App\Models\PurchaseInvoice;
use App\Models\SalesInvoiceReturn;
use App\Models\SalesProposal;
use Illuminate\Support\Facades\DB;
use Workdo\ProductService\Models\ProductServiceItem;
use Workdo\ProductService\Models\ProductServiceTax;
use Workdo\ProductService\Models\ProductServiceCategory;
use Workdo\Account\Models\BankAccount;
use Workdo\Account\Models\ChartOfAccount;
use Workdo\Account\Models\RevenueCategories;
use Workdo\Account\Models\ExpenseCategories;
use Workdo\Hrm\Models\Employee;
use Workdo\Hrm\Models\Department;
use Workdo\Hrm\Models\Branch;
use Workdo\Payroll\Models\PayGroup;
use Workdo\Payroll\Models\SalaryComponent;
class DemoContext
{
public int $userId;
public array $staffIds = [];
public array $clientIds = [];
public array $vendorIds = [];
public array $productIds = [];
public array $serviceIds = [];
public array $categoryIds = [];
public array $taxIds = [];
public array $warehouseIds = [];
public array $bankAccountIds = [];
public array $chartOfAccountIds = [];
public array $revenueCategoryIds = [];
public array $expenseCategoryIds = [];
public array $employeeIds = [];
public array $employeeUserIds = [];
public array $departmentIds = [];
public array $branchIds = [];
public array $payGroupIds = [];
public array $salaryComponentIds = [];
// WFM module IDs
public array $designationIds = [];
public array $skillIds = [];
public array $engagementIds = [];
public array $salesInvoiceIds = [];
public array $salesInvoiceItemIds = [];
public array $purchaseInvoiceIds = [];
public array $salesReturnIds = [];
public array $salesProposalIds = [];
// Property Management
public array $propertyIds = [];
public function __construct(int $userId)
{
$this->userId = $userId;
}
public function hydrate(): self
{
$this->staffIds = User::where('created_by', $this->userId)
->where('type', 'staff')->pluck('id')->toArray();
$this->clientIds = User::where('created_by', $this->userId)
->where('type', 'client')->pluck('id')->toArray();
$this->vendorIds = User::where('created_by', $this->userId)
->where('type', 'vendor')->pluck('id')->toArray();
$this->productIds = ProductServiceItem::where('created_by', $this->userId)
->where('type', '!=', 'service')->where('is_active', true)
->pluck('id')->toArray();
$this->serviceIds = ProductServiceItem::where('created_by', $this->userId)
->where('type', 'service')->where('is_active', true)
->pluck('id')->toArray();
$this->categoryIds = ProductServiceCategory::where('created_by', $this->userId)
->pluck('id')->toArray();
$this->taxIds = ProductServiceTax::where('created_by', $this->userId)
->pluck('id')->toArray();
$this->warehouseIds = Warehouse::where('created_by', $this->userId)
->where('is_active', true)->pluck('id')->toArray();
$this->bankAccountIds = BankAccount::where('created_by', $this->userId)
->where('is_active', true)->pluck('id')->toArray();
$this->chartOfAccountIds = ChartOfAccount::where('created_by', $this->userId)
->where('is_active', true)->pluck('id')->toArray();
$this->revenueCategoryIds = RevenueCategories::where('created_by', $this->userId)
->pluck('id')->toArray();
$this->expenseCategoryIds = ExpenseCategories::where('created_by', $this->userId)
->pluck('id')->toArray();
$this->employeeIds = Employee::where('created_by', $this->userId)
->pluck('id')->toArray();
$this->employeeUserIds = Employee::where('created_by', $this->userId)
->pluck('user_id')->toArray();
$this->departmentIds = Department::where('created_by', $this->userId)
->pluck('id')->toArray();
$this->branchIds = Branch::where('created_by', $this->userId)
->pluck('id')->toArray();
$this->payGroupIds = PayGroup::where('created_by', $this->userId)
->pluck('id')->toArray();
$this->salaryComponentIds = SalaryComponent::where('created_by', $this->userId)
->where('is_active', true)->pluck('id')->toArray();
$this->designationIds = DB::table('designations')
->where('created_by', $this->userId)->pluck('id')->toArray();
return $this;
}
public function randomClient(): ?int
{
return !empty($this->clientIds) ? $this->clientIds[array_rand($this->clientIds)] : null;
}
public function randomVendor(): ?int
{
return !empty($this->vendorIds) ? $this->vendorIds[array_rand($this->vendorIds)] : null;
}
public function randomProduct(): ?int
{
return !empty($this->productIds) ? $this->productIds[array_rand($this->productIds)] : null;
}
public function randomWarehouse(): ?int
{
return !empty($this->warehouseIds) ? $this->warehouseIds[array_rand($this->warehouseIds)] : null;
}
public function randomStaff(): ?int
{
return !empty($this->staffIds) ? $this->staffIds[array_rand($this->staffIds)] : null;
}
public function randomBankAccount(): ?int
{
return !empty($this->bankAccountIds) ? $this->bankAccountIds[array_rand($this->bankAccountIds)] : null;
}
public function randomEmployee(): ?int
{
return !empty($this->employeeUserIds) ? $this->employeeUserIds[array_rand($this->employeeUserIds)] : null;
}
public function randomBranch(): ?int
{
return !empty($this->branchIds) ? $this->branchIds[array_rand($this->branchIds)] : null;
}
public function randomDesignation(): ?int
{
return !empty($this->designationIds) ? $this->designationIds[array_rand($this->designationIds)] : null;
}
public function randomProducts(int $count): array
{
if (empty($this->productIds)) return [];
$count = min($count, count($this->productIds));
$keys = array_rand($this->productIds, $count);
if (!is_array($keys)) $keys = [$keys];
return array_map(fn($k) => $this->productIds[$k], $keys);
}
}