Created
February 24, 2019 17:28
-
-
Save BerezhniyDmitro/d8dd585ff26143cdbb6d6ebbebbc2a3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\OptionPrices; | |
use App\Price; | |
use App\TypeHouse; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Storage; | |
use Illuminate\Support\Facades\Validator; | |
use Maatwebsite\Excel\Facades\Excel; | |
class PriceController extends Controller | |
{ | |
private $optionSpricesNumberIndex = [ | |
3 => 'otliv', | |
4 => 'podokonnik', | |
2 => 'moskitka', | |
5 => 'kozyrek_dlya_balkonov_i_lodzhiy', | |
1 => 'montazh', | |
]; | |
/** | |
* Create a new controller instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
$this->middleware('auth'); | |
} | |
public function index() | |
{ | |
$files = empty(Storage::files('prices')) ? [] : Storage::files('prices'); | |
return view('parser/main', ['files' => $files]); | |
} | |
public function upload(Request $request) | |
{ | |
if (empty($request->file('file_price'))) { | |
return view('parser/error', []); | |
} | |
$validator = Validator::make( | |
[ | |
'file' => $request->file(), | |
'extension' => strtolower($request->file('file_price')->clientExtension()), | |
], | |
[ | |
'file' => 'required', | |
'extension' => 'required|in:xlsx,xls', | |
] | |
); | |
if ($validator->fails()) { | |
return view('parser/error', []); | |
} | |
$request->file('file_price')->storeAs( | |
'prices', $request->file('file_price')->getClientOriginalName() | |
); | |
return redirect('/price'); | |
} | |
public function parse(Request $request) | |
{ | |
$file = new \SplFileInfo($request->get('name')); | |
$path = 'storage/app/' . $file->getPathname(); | |
$data = Excel::load($path, function($reader) {})->all()->toArray(); | |
$data = reset($data); | |
$data = array_reformat($data, 'id_sayt'); | |
$prices = Price::all(); | |
$logInfo = []; | |
foreach ($prices as $price) { | |
if (empty($price->external_price)) { | |
continue; | |
} | |
$externalId = $price->external_price->external_id; | |
if (empty($data[$externalId])) { | |
$logInfo[] = [ | |
'message' => sprintf('Запись с ID %s, ранее присутствовала в файле прайса, сейчас ее нет. Цена не была обновлена', $externalId), | |
'status' => 'warning', | |
]; | |
continue; | |
} | |
$newPrice = $data[$externalId]['summa_grn.']; //может не быть зависит от названия колонки. | |
$newPrice = preg_replace('/[^[:print:]]/', '', $newPrice); | |
$newPrice = preg_replace('/[^[:print:]]/', '', $newPrice); | |
$newPrice = preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $newPrice); | |
$newPrice = preg_replace('/\s+/', '', $newPrice); | |
$newPrice = str_replace(',', '.', $newPrice); | |
$newPrice = (float) $newPrice; | |
$oldPrice = $price->price; | |
$price->price = $newPrice; | |
$price->save(); | |
$priceOptions = OptionPrices::where('type_house_id', '=', $price->type_house_id) | |
->where('construct_id', '=', $price->construct_id) | |
->get(); | |
foreach ($priceOptions as $priceOption) { | |
$key = $this->optionSpricesNumberIndex[$priceOption->option_id]; | |
$priceOption->price = $data[$externalId][$key]; | |
$priceOption->save(); | |
} | |
if ((float) $oldPrice === $newPrice) { | |
continue; | |
} | |
$logInfo[] = [ | |
'message' => sprintf( | |
'Цена на %s, c профилем %s и фурнитурой %s и стеклопакетом %s. Была обновлена со старой цены %s на новую цену %s', | |
$data[$externalId]['primechanie'], | |
$data[$externalId]['profil'], | |
$data[$externalId]['furnitura'], | |
$data[$externalId]['steklopaket'], | |
$oldPrice, | |
$newPrice | |
), | |
'status' => 'success', | |
]; | |
} | |
return view('parser/success', ['log_info' => $logInfo]); | |
} | |
public function delete(Request $request) | |
{ | |
Storage::delete($request->get('name')); | |
return redirect('/price'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment