File: /var/www/ipsremont-demo/app/Http/Controllers/FakeDataController.php
<?php
namespace App\Http\Controllers;
use App\Models\Order;
use App\Models\Part;
use App\Models\Service\Service;
use App\Repository\Branch\BranchRepository;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class FakeDataController extends \Illuminate\Routing\Controller
{
/**
*
* @return array
*/
public function getData()
{
if (env('APP_ENV') != 'production') {
Log::channel('api')->info(\request()->data);
$data = json_decode(\request()->data);
$test_response = [];
foreach ($data as $key => $item) {
$result = (rand(0, 9) > 3) ? 1 : 'какая-то ошибка';
array_push($test_response, ['id' => $key, 'code' => rand(10000, 99999), 'result' => $result]);
}
return $test_response;
}
}
/**
*
*/
public function orders1c()
{
$data = [];
$parts_models = Part::query()->where('external_id', '<>', '')
->inRandomOrder()
->limit(100)
->get();
$services = Service::query()->where('external_id', '<>', '')->get();
$maxService = count($services) - 1;
$orders = Order::query()->where('external_id', '<>', '')
->whereNotIn('status', ['canceled', 'completed'])
->inRandomOrder()
->limit(5)
->get();
$post_types = ['cdek', 'pek'];
for ($i = 0; $i < 3; $i++) {
$parts = [];
$amount = rand(1, 5);
for ($j = 0; $j < $amount; $j++) {
$p = $parts_models->shift();
array_push($parts, ['code' => $p->external_id, 'amount' => rand(0, 9), 'price' => $p->price]);
}
array_push($data, [
'id' => $orders[0]->id,
'service_code' => $orders[0]->service->external_id,
'branch_code' => $orders[0]->service->branch->external_id,
'code' => $orders[0]->external_id,
'status' => 'awaiting_confirmation',
'created_by_manager' => $orders[0]->created_by_manager,
'type' => (rand(0, 1)) ? 'paid' : 'warranty',
'name' => Str::random(),
'email' => Str::random() . '@gmail.com',
'phone' => rand(10000000000, 99999999999) . "",
'country' => 'Россия',
'city' => Str::random(),
'address' => Str::random(),
'additional_info' => Str::random(),
'price' => rand(1000, 9999) / 10,
'created_at' => now()->format(config('crud.default')),
'action' => 'updated',
'track_number' => time() . '',
'post_type' => $post_types[rand(0, 1)],
'invoice' => Str::random(),
]);
$service = $services[rand(0, $maxService)];
array_push($data, [
'id' => '',
'service_code' => $service->external_id,
'branch_code' => $service->branch->external_id,
'code' => rand(1000000, 9999999) . time(),
'status' => 'new',
'created_by_manager' => rand(0, 1),
'type' => (rand(0, 1)) ? 'paid' : 'warranty',
'name' => Str::random(),
'email' => Str::random() . '@gmail.com',
'phone' => rand(10000000000, 99999999999) . "",
'country' => 'Россия',
'city' => Str::random(),
'address' => Str::random(),
'additional_info' => Str::random(),
'price' => rand(1000, 9999) / 10,
'created_at' => now()->format(config('crud.default')),
'action' => 'new',
'parts' => $parts,
'invoice' => Str::random(),
]);
}
$data = json_encode($data);
dump($data);
$response = Http::post(env('APP_URL') . '/receiver', ['data' => $data]);
$response_data = $response->body();
dump($response_data);
}
public function parts1c()
{
$data = [];
$branches = BranchRepository::getDisplayed()->get();
for ($i = 0; $i < 5; $i++) {
$storage = [];
foreach ($branches as $branch) {
if ($branch->external_id) {
$storage[$branch->external_id] = rand(1, 300);
}
}
$storage = (object) $storage;
$parts = Part::query()->inRandomOrder()->limit(5)->get();
array_push(
$data, (object) [
"code" => rand(10000, 99999) . '',
"name" => Str::random(),
"unit" => "шт.",
"price" => rand(1, 99999) / 10,
"foto" => Str::random() . '.jpg',
"storage" => (object) $storage,
"action" => "new",
"analogs" => [$parts[0]->external_id, $parts[1]->external_id, $parts[2]->external_id],
]
);
array_push(
$data, (object) [
"code" => $parts[3]->external_id . '',
"name" => Str::random(),
"unit" => "шт.",
"price" => rand(1, 99999) / 10,
"foto" => Str::random() . '.jpg',
"storage" => (object) $storage,
"action" => "updated",
"analogs" => [$parts[0]->external_id, $parts[1]->external_id, $parts[2]->external_id],
]
);
array_push(
$data, (object) [
"code" => $parts[4]->external_id . '',
"name" => Str::random(),
"unit" => "шт.",
"price" => rand(1, 99999) / 10,
"foto" => Str::random() . '.jpg',
"storage" => (object) $storage,
"action" => "deleted",
]
);
}
return json_encode($data);
}
}