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

63 lines
2.3 KiB
PHP

<?php namespace Tps\Shops\Controllers;
use Backend\Classes\Controller;
use BackendMenu;
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;
class Report extends Controller
{
public $implement = [ 'Backend\Behaviors\ListController', 'Backend\Behaviors\FormController' ];
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');
}
public function onExtractReports($id)
{
$post = ModelsReport::where("id",$id)->get()->first();
$sessionKey = uniqid('session_key', true);
$path = $post->file()->withDeferred($sessionKey)->get()->first()->getPath();
$diviedPath = explode('storage/app', $path);
$extractor = new Extractor();
$storageDestinationPath= storage_path("app/extract/".hash('ripemd160', $post->name).'-'.$post->date);
if (!\File::exists( $storageDestinationPath)) {
\File::makeDirectory($storageDestinationPath, 0755, true);
}
$extractor->extract(storage_path('app/'. $diviedPath[1]), $storageDestinationPath);
$filenames = array_diff(scandir($storageDestinationPath), array('.', '..'));
foreach($filenames as $item){
$shop_number = explode(".", $item)[0];
$shop = Shop::where("shop_number", $shop_number)->get()->first();
if($shop){
$exist = ReportDetail::where('report_id', $id)->where("shop_id", $shop->id)->get();
if($exist->isEmpty()){
$report_detail = new ReportDetail();
$report_detail->report_id = $id;
$report_detail->shop_id = $shop->id;
$report_detail->file = explode('storage\app', $storageDestinationPath)[1].'/'.$item;
Log::info(explode('storage\app', $storageDestinationPath)[1].'/'.$item);
$report_detail->save();
Log::info("saved succesfully");
}else Log::info("Record exist");
}else Log::info("Shop not found");
}
}
}