Berkarar/plugins/tps/shops/controllers/Report.php

66 lines
2.4 KiB
PHP
Raw Normal View History

2022-09-13 14:01:10 +00:00
<?php namespace Tps\Shops\Controllers;
use Backend\Classes\Controller;
use BackendMenu;
2022-09-15 19:49:54 +00:00
use Illuminate\Support\Facades\Log;
use Tps\Shops\Models\Report as ModelsReport;
use Tps\Shops\Classes\Extractor;
use Tps\Shops\Models\ReportDetail;
use Tps\Shops\Models\Shop;
2022-09-13 14:01:10 +00:00
class Report extends Controller
{
public $implement = [ 'Backend\Behaviors\ListController', 'Backend\Behaviors\FormController' ];
2023-08-04 08:35:28 +00:00
2022-09-13 14:01:10 +00:00
public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public function __construct()
{
parent::__construct();
BackendMenu::setContext('Tps.Shops', 'main-menu-item', 'side-menu-item6');
}
2022-09-15 19:49:54 +00:00
public function onExtractReports($id)
2022-09-13 14:01:10 +00:00
{
2023-08-04 08:35:28 +00:00
$post = ModelsReport::where("id", $id)->get()->first();
2022-09-15 19:49:54 +00:00
$sessionKey = uniqid('session_key', true);
$path = $post->file()->withDeferred($sessionKey)->get()->first()->getPath();
2023-08-04 08:35:28 +00:00
$dividedPath = explode('storage/app', $path);
2022-09-15 19:49:54 +00:00
$storageDestinationPath= storage_path("app/extract/".hash('ripemd160', $post->name).'-'.$post->date);
2023-08-04 08:35:28 +00:00
if (!\File::exists($storageDestinationPath)) {
2022-09-15 19:49:54 +00:00
\File::makeDirectory($storageDestinationPath, 0755, true);
}
2023-08-04 08:35:28 +00:00
$extractor = new Extractor();
$extractor->extract(storage_path('app/'. $dividedPath[1]), $storageDestinationPath);
2022-09-15 19:49:54 +00:00
$filenames = array_diff(scandir($storageDestinationPath), array('.', '..'));
2023-08-04 08:35:28 +00:00
foreach ($filenames as $item) {
2022-09-15 19:49:54 +00:00
$shop_number = explode(".", $item)[0];
$shop = Shop::where("shop_number", $shop_number)->get()->first();
2023-08-04 08:35:28 +00:00
if ($shop) {
2022-09-15 19:49:54 +00:00
$exist = ReportDetail::where('report_id', $id)->where("shop_id", $shop->id)->get();
2023-08-04 08:35:28 +00:00
if ($exist->isEmpty()) {
2022-09-15 19:49:54 +00:00
$report_detail = new ReportDetail();
$report_detail->report_id = $id;
$report_detail->shop_id = $shop->id;
2023-08-04 09:18:16 +00:00
// on local explode storage\app on live server storage/app
$report_detail->file = '/extract'.explode('extract', $storageDestinationPath)[1].'/'.$item;
2022-09-15 19:49:54 +00:00
$report_detail->save();
Log::info("saved succesfully");
2023-08-04 08:35:28 +00:00
}else {
Log::info("Record exist");
}
}else {
Log::info("Shop not found");
}
2022-09-15 19:49:54 +00:00
}
2022-09-13 14:01:10 +00:00
}
}