Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/medad.corals.io/Corals/modules/Medad/database/seeds/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/medad.corals.io/Corals/modules/Medad/database/seeds/MedadDummyData.php
<?php

namespace Corals\Modules\Medad\database\seeds;

use Carbon\Carbon;
use Corals\Modules\Medad\Facades\Medad;
use Corals\Modules\Medad\Models\Branch;
use Corals\Modules\Medad\Models\Company;
use Corals\Modules\Medad\Models\CompanyRelation;
use Corals\Modules\Medad\Models\DeliveryNote;
use Corals\Modules\Medad\Models\Invoice;
use Corals\Modules\Medad\Models\Project;
use Corals\Modules\Medad\Models\PurchaseOrder;
use Corals\Modules\Medad\Models\Quotation;
use Corals\Modules\Medad\Models\QuotationRequest;
use Corals\Modules\Medad\Models\Transaction;
use Corals\Modules\Medad\Models\Vehicle;
use Corals\Modules\Medad\Models\VehicleDriver;
use Corals\Modules\Medad\Models\VehicleService;
use Corals\Modules\Utility\Facades\ListOfValue\ListOfValues;
use Corals\Modules\Utility\Models\Address\Location;
use Corals\User\Models\User;
use Faker\Generator as Faker;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class MedadDummyData extends Seeder
{
    /**
     * @var Faker|mixed
     */
    private $faker;
    protected array $locationIds;
    protected $makeCodes;
    protected $vehicleServices;

    /**
     * @return void
     * @throws BindingResolutionException
     */
    public function run()
    {
        $this->faker = app()->make(Faker::class);
        $this->makeCodes = ListOfValues::get('vehicle-makes', true);
        $this->vehicleServices = ListOfValues::get('vehicle-services', true);


        $this->createLocations();


        foreach (range(1, 8) as $i) {
            try {
                $this->createCompany();
            } catch (\Exception $exception) {
                logger('company');
                logger($exception->getMessage());
            }
        }
        $companies = Company::all();

        foreach ($companies as $company) {

            if ($company->type == 'customer') {
                try{
                $this->createCompanyRelation($company, 'supplier');
                } catch (\Exception $exception) {
                    logger('companyRelation');
                    logger($exception->getMessage());
                }
            }

            if ($company->type == 'supplier') {
                try{
                $this->createCompanyRelation($company, 'customer');
                } catch (\Exception $exception) {
                    logger('companyRelation');
                    logger($exception->getMessage());
                }
            }
        }

        foreach ($companies as $company) {
            $branches = Company::where('parent_id', $company->id)->get();
            foreach ($branches as $branch) {
                if ($company->type == 'supplier') {
                    try{
                    $this->createQuotation($company, $branch);
                    } catch (\Exception $exception) {
                        logger('quotation');
                        logger($exception->getMessage());
                    }
                    try{
                    $this->createPurchaseOrderSupplier($company, $branch);
                    } catch (\Exception $exception) {
                        logger('purchaseOrder');
                        logger($exception->getMessage());
                    }
                }
                if ($company->type == 'customer') {
                    try {
                        $this->createProjects($company, $branch);
                    } catch (\Exception $exception) {
                        logger('project');
                        logger($exception->getMessage());
                    }
                    try{
                    $this->createQuotationRequest($company, $branch);
                    } catch (\Exception $exception) {
                        logger('quotationRequest');
                        logger($exception->getMessage());
                    }
                    try{
                    $this->createPurchaseOrderCustomer($company, $branch);
                    } catch (\Exception $exception) {
                        logger('purchaseOrder');
                        logger($exception->getMessage());
                    }
                }

            }

        }
    }

    /**
     * @return void
     * @throws \Exception
     */
    protected function createCompany()
    {
        tap(Company::create([
            'name' => $this->faker->company,
            'code' => random_int(1000000, 9999999),
            'tin' => $this->faker->numerify('######'),
            'email' => $this->faker->unique()->safeEmail,
            'phone_number' => $this->faker->numerify('(###) ### ###'),
            'website' => $this->faker->domainName,
            'location_id' => $this->faker->randomKey($this->locationIds),
            'type' => $this->faker->randomKey(trans('Medad::attributes.company.type_options')),
            'status' => $this->faker->randomKey(trans('Medad::attributes.company.status_options')),
        ]), function (Company $company) {
            $company->update(['parent_id' => $company->id]);

            foreach (range(0, random_int(0, 4)) as $branchSeq) {
                try {
                    $this->createBranch($company, $branchSeq);
                } catch (\Exception $exception) {
                    logger('branch');
                    logger($exception->getMessage());
                }
            }

            foreach (range(0, random_int(0, 4)) as $vehicleSeq) {
                try {
                    $this->createVehicle($company, $vehicleSeq);
                } catch (\Exception $exception) {
                    logger('vehicle');
                    logger($exception->getMessage());
                }
            }

            foreach (['medad_company_admin', 'medad_company_member'] as $role) {
                foreach (range(0, random_int(0, 2)) as $i) {
                    try {
                        $this->createUsers($company, $role);
                    } catch (\Exception $exception) {
                        logger('users');
                        logger($exception->getMessage());
                    }
                }
            }
        });
    }

    protected function createBranch(Company $company, $branchSeq)
    {
        $branchSeq += 1;

        tap(Branch::create([
            'name' => $company->name . ' ' . sprintf('%02d', $branchSeq),
            'code' => $company->code . '_' . sprintf('%02d', $branchSeq),
            'parent_id' => $company->id,
            'email' => $branchSeq . '_' . $this->faker->unique()->safeEmail,
            'phone_number' => $this->faker->numerify('(###) ### ###'),
            'location_id' => $this->faker->randomKey($this->locationIds),
        ]), function ($branch) use ($company) {
            foreach (['medad_admin', 'medad_finance', 'medad_driver', 'medad_company_member'] as $role) {
                foreach (range(0, random_int(0, 2)) as $i) {
                    try {
                        $this->createUsers($branch, $role);
                    } catch (\Exception $exception) {
                        logger('users');
                        logger($exception->getMessage());
                    }
                }
            }


        });
    }

    protected function createCompanyRelation(Company $company, $relationType)
    {
        $companies = Company::all();
        foreach ($companies as $relationComany) {
            $relation = CompanyRelation::where('company_id', $company->id)->where('second_company_id', $relationComany->id)->exists();
            if (!$relation) {
                tap(CompanyRelation::create([
                    'company_id' => $company->id,
                    'second_company_id' => $relationComany->id,
                    'relation_type' => $relationType,
                ]), function ($companyRelation) {
                });
            }
        }
    }

    protected function createVehicle(Company $company, $vehicleSeq)
    {
        $vehicleMake = $this->makeCodes->random();
        $vehicleModel = $vehicleMake->children->random();

        tap(Vehicle::create([
            'model_year' => $this->faker->year(),
            'company_id' => $company->id,
            'licence_plate' => $this->faker->unique()->numerify('###-###'),
            "make_code" => $vehicleMake->code,
            'model_code' => $vehicleModel->code,
            'current_fuel_level' => $this->faker->randomKey(trans('Medad::attributes.fuel_level_options')),
            'engine_fuel' => $this->faker->randomKey(trans('Medad::attributes.vehicle.engine_fuel_options')),
            'current_odometer' => random_int(1000, 10000),
            'color' => $this->faker->colorName,
        ]), function ($vehicle) use ($company) {
            foreach (range(0, random_int(0, 20)) as $i) {
                try {
                    $this->createVehicleServices($vehicle, $company);
                } catch (\Exception $exception) {
                    logger('vehicleService');
                    logger($exception);
                }
            }
            $vehicleDriversCompany = Medad::getCompanyUsers('medad_driver', $company)->toArray();
            foreach (range(0, random_int(1, 20)) as $i) {
                try {
                    $this->createVehicleDrivers($vehicle, $vehicleDriversCompany, $i);
                } catch (\Exception $exception) {
                    logger('VehicleDriver');
                    logger($exception->getMessage());
                }
            }
        });
    }

    protected function createUsers($object, $role)
    {
        tap([
            'name' => $this->faker->firstName,
            'last_name' => $this->faker->lastName,
            'email' => $object->id . '_' . $this->faker->unique()->safeEmail,
            'password' => '123456',
            'phone_number' => $this->faker->numerify('(###) ### ###')
        ], function ($userData) use ($object, $role) {
            $user = User::create($userData);

            $user->assignRole($role);

            $object->admins()->attach([
                $user->id => [
                    'is_admin' => Str::contains($role, 'admin')
                ]
            ]);
        });
    }

    protected function createProjects($company, $branch)
    {
        $projectManager = User::create([
            'name' => $this->faker->firstName,
            'last_name' => $this->faker->lastName,
            'email' => $branch->id . '_' . $this->faker->unique()->safeEmail,
            'password' => '123456',
            'phone_number' => $this->faker->numerify('(###) ### ###')
        ]);

        $projectManager->assignRole('medad_project_manager');

        tap(Project::create([
            'name' => $this->faker->word,
            'description' => $this->faker->realText(),
            'manager_id' => $projectManager->id,
            'branch_id' => $branch->id,
            'address' => $this->faker->address(),
        ]), function ($project) use ($branch, $company) {

            $suppliers = Medad::getCompanySuppliers($branch);
            foreach ($suppliers as $supplierId => $supplier) {
                DB::table('medad_company_project')->insert([
                    'project_id' => $project->id,
                    'company_id' => $supplierId,
                    'relation_type' => 'supplier',
                ]);
            }

            $receivers = Medad::getCompanyUsers('medad_company_member', $company);
            foreach ($receivers as $receiversId => $receiver) {
                DB::table('medad_model_users')->insert([
                    'model_type' => Project::class,
                    'model_id' => $project->id,
                    'user_id' => $receiversId,
                    'role' => 'receiver',
                ]);
            }

        });
    }

    protected function createVehicleServices($vehicle, $company)
    {
        $vehicleServices = $this->vehicleServices->random();

        $user = User::create([
            'name' => $this->faker->firstName,
            'last_name' => $this->faker->lastName,
            'email' => $this->faker->unique()->safeEmail,
            'password' => '123456',
            'phone_number' => $this->faker->numerify('(###) ### ###')
        ]);

        $user->assignRole('medad_driver');

        $company->users()->attach($user->id);

        tap(VehicleService::create([
            'code' => VehicleService::getSeqCode('SRV'),
            'vehicle_id' => $vehicle->id,
            'properties' => $this->faker->realText(),
            'user_id' => $user->id,
            'service_type_code' => $vehicleServices->code,
            'odometer_reading' => random_int(1000, 100000),
            'fuel_level' => $this->faker->randomKey(trans('Medad::attributes.fuel_level_options')),
            'service_date' => $this->faker->date(),
            'work_performed' => $this->faker->realText(),
            'cost' => $this->faker->randomFloat(),
        ]), function ($vehicleService) {
        });
    }


    protected function createVehicleDrivers($vehicle, $vehicleDriversCompany, $i)
    {
        $startDate = Carbon::parse($this->faker->date());
        $endDate = $i == 0 ? null : $startDate->addDays(random_int(10, 100));

        tap(VehicleDriver::create([
            'vehicle_id' => $vehicle->id,
            'driver_id' => $this->faker->randomKey($vehicleDriversCompany),
            'start_date' => $startDate,
            'end_date' => $endDate,
        ]), function ($vehicleDriver) {
        });
    }


    protected function createLocations()
    {
        $locations = [];

        foreach (range(0, 20) as $i) {
            $address = $this->faker->address;

            $locations[] = [
                'address' => $address,
                'name' => $address,
                'slug' => Str::slug($address),
                'lat' => $this->faker->latitude,
                'long' => $this->faker->longitude,
                'module' => 'Medad',
                'status' => 'active',
            ];
        }

        Location::query()->insert($locations);
        $this->locationIds = Location::query()->where('module', 'Medad')->pluck('id')->toArray();
        $this->locationIds = array_combine($this->locationIds, $this->locationIds);
    }

    protected function createQuotation($company, $branch)
    {
        $customerCompany = Medad::getCompanyCustomers($branch);

        foreach ($customerCompany as $id => $customer) {
            $branches = Company::where('parent_id', $id)->get();
            $users = Medad::getCompanyUsers(null, Company::find($company->id));
            foreach ($users as $userId => $user) {
                foreach ($branches as $customerBranch) {
                    tap(Quotation::create([
                        'customer_company_id' => $id,
                        'customer_branch_id' => $customerBranch->id,
                        'supplier_company_id' => $company->id,
                        'supplier_branch_id' => $branch->id,
                        'code' => random_int(1000000, 9999999),
                        'types_count' => random_int(1, 100),
                        'total_quantity' => random_int(1, 100),
                        'amount' => random_int(1, 100),
                        'expiration_date' => $this->faker->date(),
                        'status' => $this->faker->randomKey(trans('Medad::attributes.quotation.status_options')),
                        'owner_id' => $userId,
                        'owner_company_id'=>$company->id,
                    ]), function ($quotation) use ($userId) {
                        if ($quotation->status == 'open') {
                            try{
                            $this->createGeneratePurchaseOrder($quotation, $userId, 'supplier');
                            } catch (\Exception $exception) {
                                logger('purchaseOrder');
                                logger($exception->getMessage());
                            }
                        }
                    });
                }
            }
        }
    }

    protected function createQuotationRequest($company, $branch)
    {
        $projects = Project::where('branch_id', $branch->id)->get();
        $users = Medad::getCompanyUsers(null, Company::find($company->id));
        foreach ($users as $userId => $user) {
            foreach ($projects as $project) {
                tap(QuotationRequest::create([
                    'code' => random_int(1000000, 9999999),
                    'customer_company_id' => $company->id,
                    'customer_branch_id' => $branch->id,
                    'types_count' => random_int(1, 100),
                    'total_quantity' => random_int(1, 100),
                    'status' => $this->faker->randomKey(trans('Medad::attributes.quotation-request.status_options')),
                    'owner_id' => $userId,
                    'owner_company_id'=>$company->id,
                    'project_id' => $project->id,
                ]), function ($quotationRequest) use ($project, $branch, $userId) {

                    $suppliers = $project->projectSuppliers()->pluck('medad_companies.name', 'medad_companies.id');
                    $this->createSuppliers($quotationRequest, $suppliers, $branch, $userId);


                });
            }
        }
    }

    protected function createSuppliers($quotationRequest, $suppliers, $branch, $userId)
    {
        foreach ($suppliers as $supplierId => $supplier) {
            DB::table('medad_quotation_request_suppliers')->insert([
                'quotation_request_id' => $quotationRequest->id,
                'company_id' => $supplierId,
                'branch_id' => $branch->id,
            ]);
            if ($quotationRequest->status == 'open') {
                try{
                $this->createGenerateQuotation($supplierId, $quotationRequest, $userId);
                } catch (\Exception $exception) {
                    logger('quotation');
                    logger($exception->getMessage());
                }
            }
        }
    }

    protected function createGenerateQuotation($supplierId, $quotationRequest, $userId)
    {
        tap(Quotation::create([
            'quotation_request_id' => $quotationRequest->id,
            'project_id' => $quotationRequest->project_id,
            'customer_company_id' => $quotationRequest->customer_company_id,
            'supplier_company_id' => $supplierId,
            'customer_branch_id' => $quotationRequest->customer_branch_id,
            'code' => random_int(1000000, 9999999),
            'types_count' => random_int(1, 100),
            'total_quantity' => random_int(1, 100),
            'amount' => random_int(1, 100),
            'expiration_date' => $this->faker->date(),
            'status' => $this->faker->randomKey(trans('Medad::attributes.quotation.status_options')),
            'owner_id' => $userId,
            'owner_company_id'=>$quotationRequest->owner_company_id,
        ]), function ($quotation) use ($userId) {
            if ($quotation->status == 'open') {
                try{
                $this->createGeneratePurchaseOrder($quotation, $userId, 'customer');
                } catch (\Exception $exception) {
                    logger('purchaseOrder');
                    logger($exception->getMessage());
                }
            }
        });
    }

    protected function createPurchaseOrderSupplier($company, $branch)
    {
        $customerCompany = Medad::getCompanyCustomers($branch);

        foreach ($customerCompany as $id => $customer) {
            $branches = Company::where('parent_id', $id)->get();
            $users = Medad::getCompanyUsers(null, Company::find($company->id));
            foreach ($users as $userId => $user) {
                foreach ($branches as $customerBranch) {
                    tap(PurchaseOrder::create([
                        'code' => random_int(1000000, 9999999),
                        'customer_company_id' => $id,
                        'customer_branch_id' => $customerBranch->id,
                        'supplier_company_id' => $company->id,
                        'supplier_branch_id' => $branch->id,
                        'types_count' => random_int(1, 100),
                        'total_quantity' => random_int(1, 100),
                        'amount' => random_int(1, 100),
                        'status' => $this->faker->randomKey(trans('Medad::attributes.purchase_order.status_options')),
                        'owner_id' => $userId,
                        'owner_company_id'=>$company->id,
                        'address' => $this->faker->address(),
                    ]), function ($purchaseOrder) use ($company){
                        try {
                            $this->createDeliveryNote($company,$purchaseOrder);
                        } catch (\Exception $exception) {
                            logger('deliveryNote');
                            logger($exception->getMessage());
                        }
                        try {
                            $this->createInvoice($company,$purchaseOrder);
                        } catch (\Exception $exception) {
                            logger('invoice');
                            logger($exception->getMessage());
                        }
                    });
                }
            }
        }
    }

    protected function createPurchaseOrderCustomer($company, $branch)
    {
        $projects = Project::where('branch_id', $branch->id)->get();

        foreach ($projects as $project) {
            $suppliers = $project->projectSuppliers()->pluck('medad_companies.name', 'medad_companies.id');
            foreach ($suppliers as $id => $supplier) {
                $branches = Company::where('parent_id', $id)->get();
                $users = Medad::getCompanyUsers(null, Company::find($company->id));
                foreach ($users as $userId => $user) {

                    foreach ($branches as $customerBranch) {
                        tap(PurchaseOrder::create([
                            'code' => random_int(1000000, 9999999),
                            'customer_company_id' => $company->id,
                            'customer_branch_id' => $branch->id,
                            'supplier_company_id' => $id,
                            'supplier_branch_id' => $customerBranch->id,
                            'types_count' => random_int(1, 100),
                            'total_quantity' => random_int(1, 100),
                            'amount' => random_int(1, 100),
                            'status' => $this->faker->randomKey(trans('Medad::attributes.purchase_order.status_options')),
                            'owner_id' => $userId,
                            'owner_company_id'=>$company->id,
                            'address' => $this->faker->address(),
                            'project_id' => $project->id,
                        ]), function ($purchaseOrder) use ($project) {

                            $receivers = $project->receivers->pluck('full_name', 'id');
                            foreach ($receivers as $receiversId => $receiver) {
                                DB::table('medad_model_users')->insert([
                                    'model_type' => PurchaseOrder::class,
                                    'model_id' => $purchaseOrder->id,
                                    'user_id' => $receiversId,
                                    'role' => 'receiver',
                                ]);
                            }
                        });
                    }
                }
            }
        }

    }

    protected function createGeneratePurchaseOrder($quotation, $userId, $target)
    {
        tap(PurchaseOrder::create([
            'code' => random_int(1000000, 9999999),
            'customer_company_id' => $quotation->customer_company_id,
            'customer_branch_id' => $quotation->customer_branch_id,
            'supplier_company_id' => $quotation->supplier_company_id,
            'supplier_branch_id' => $quotation->supplier_branch_id,
            'types_count' => random_int(1, 100),
            'total_quantity' => random_int(1, 100),
            'amount' => random_int(1, 100),
            'status' => $this->faker->randomKey(trans('Medad::attributes.purchase_order.status_options')),
            'owner_id' => $userId,
            'address' => $this->faker->address(),
            'project_id' => $target == 'customer' ? $quotation->project_id : null,
            'quotation_id' => $target == 'customer' ? $quotation->id : null,
            'owner_company_id'=>$quotation->owner_company_id,
        ]), function ($purchaseOrder) {
        });
    }

    protected function createDeliveryNote($company,$purchaseOrder)
    {
            if ($purchaseOrder->status == 'open') {
                tap(DeliveryNote::create([
                    'code' => random_int(1000000, 9999999),
                    'customer_company_id' => $purchaseOrder->customer_company_id,
                    'customer_branch_id' => $purchaseOrder->customer_branch_id,
                    'supplier_company_id' => $purchaseOrder->supplier_company_id,
                    'supplier_branch_id' => $purchaseOrder->supplier_branch_id,
                    'types_count' => random_int(1, 100),
                    'total_quantity' => random_int(1, 100),
                    'status' => $this->faker->randomKey(['draft' => 'Draft', 'open' => 'Open']),
                    'owner_id' => $purchaseOrder->owner_id,
                    'owner_company_id'=>$company->id,
                    'project_id' => $purchaseOrder->project_id,
                    'purchase_order_id' => $purchaseOrder->id,
                ]), function ($deliveryNote) {
                });
            }
    }

    protected function createInvoice($company,$purchaseOrder)
    {
            if ($purchaseOrder->status == 'open') {
                tap(Invoice::create([
                    'code' => random_int(1000000, 9999999),
                    'customer_company_id' => $purchaseOrder->customer_company_id,
                    'customer_branch_id' => $purchaseOrder->customer_branch_id,
                    'supplier_company_id' => $purchaseOrder->supplier_company_id,
                    'supplier_branch_id' => $purchaseOrder->supplier_branch_id,
                    'types_count' => random_int(1, 100),
                    'total_quantity' => random_int(1, 100),
                    'amount' => random_int(1, 100),
                    'status' => $this->faker->randomKey(trans('Medad::attributes.invoice.status_options')),
                    'owner_id' => $purchaseOrder->owner_id,
                    'owner_company_id'=>$company->id,
                    'project_id' => $purchaseOrder->project_id,
                    'purchase_order_id' => $purchaseOrder->id,
                ]), function ($invoice) {
                    if ($invoice->status == 'sent') {
                        try{
                        $this->createTransaction($invoice);
                        } catch (\Exception $exception) {
                            logger('transaction');
                            logger($exception->getMessage());
                        }
                    }
                });
            }

    }

    protected function createTransaction($invoice)
    {
        tap(Transaction::create([
            'code' => random_int(1000000, 9999999),
            'customer_company_id' => $invoice->customer_company_id,
            'customer_branch_id' => $invoice->customer_branch_id,
            'supplier_company_id' => $invoice->supplier_company_id,
            'supplier_branch_id' => $invoice->supplier_branch_id,
            'types_count' => random_int(1, 100),
            'total_quantity' => random_int(1, 100),
            'amount' => random_int(1, 100),
            'status' => $this->faker->randomKey(trans('Medad::attributes.transaction.status_options')),
            'owner_id' => $invoice->owner_id,
            'owner_company_id'=>$invoice->owner_company_id,
            'project_id' => $invoice->project_id,
            'invoice_id' => $invoice->id,
        ]), function ($transaction) {
        });
    }
}

Spamworldpro Mini