| Server IP : 54.233.248.239 / Your IP : 172.28.20.13 Web Server : Apache System : Linux ip-172-28-29-189 6.5.0-1014-aws #14~22.04.1-Ubuntu SMP Thu Feb 15 15:27:06 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.2.34-43+ubuntu22.04.1+deb.sury.org+1 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/vinumday2_0/app/Modules/Adega/Controllers/Admin/ |
Upload File : |
<?php
namespace App\Modules\Adega\Controllers\Admin;
set_time_limit(0);
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Mail;
use Exception;
use App\Http\Requests;
use App\Http\Controllers\BaseController;
use App\Modules\Adega\Models\Adega;
use App\Modules\Confraria\Models\Confraria;
use DB;
use App\Services\GeralService;
class AdminAdegaController extends BaseController
{
private $modulo;
private $fields;
private $fks;
private $lastInsertId;
public function __construct(){
parent::__construct();
$this->middleware('auth');
$this->modulo = \App\Gerador::where('nome', 'Adega')->with('campos', 'fks')->first();
$this->fields = $this->modulo->campos;
$this->fks = $this->modulo->fks;
$this->adega_m = new Adega();
}
public function index(Request $request){
$query = $this->adega_m->select('adega.*', 'cadastro.nome', 'cadastro.sobrenome', 'sis_users.email');
$data['fields_listagem'] = array();
foreach ($this->fields as $field) {
if($field->listagem){
$data['fields_listagem'][] = $field;
}
}
foreach ($this->fks as $fk) {
if($fk->listagem){
if($fk->campoRelacionado->translatable){
$table = $fk->moduloRelacionado->nome_tabela.'_descricao';
$on = $fk->moduloRelacionado->nome_tabela.'_descricao.'.$fk->moduloRelacionado->nome_tabela.'_id';
}else{
$table = $fk->moduloRelacionado->nome_tabela;
$on = $fk->moduloRelacionado->nome_tabela.'.id';
}
$query->leftJoin($table, 'adega.'.$fk->nome, '=', $on);
$query->addSelect($table.'.'.$fk->campoRelacionado->nome.' as fk'.$fk->id);
$data['fields_listagem'][] = $fk;
}
}
$query->join('cadastro', 'cadastro.id', '=', 'adega.id_cliente');
$query->join('sis_users', 'cadastro.user_id', '=', 'sis_users.id');
if ($request->input('term'))
{
$query->where(function($qr) use ($request){
$qr->where(DB::raw('CONCAT(cadastro.nome, " ", cadastro.sobrenome)'), 'like', '%'.$request->input('term').'%');
$qr->orWhere('sis_users.email', 'like', '%'.$request->input('term').'%');
});
}
$query->orderBy('adega.data_abertura', 'DESC');
$data['adegas'] = $query->groupBy('adega.id')->paginate(20);
usort($data['fields_listagem'], function($a, $b) {
return $a->ordem - $b->ordem;
});
return view('Adega::admin/adega',$data);
}
public function adegaDivergente(Request $request){
$post = $request->input();
$query = Adega::select(
DB::raw('adega.*'),
DB::raw('cadastro.nome, cadastro.sobrenome, cadastro.slug cliente_slug'),
DB::raw('(
SELECT
count(ahpv.id)
FROM
adega_has_pedido_venda ahpv,
pedido_venda pv
WHERE
ahpv.id_pedido_venda = pv.id
AND ahpv.id_adega = adega.id
AND pv.id_status_pedido_venda = 5 AND pv.frete = 0
) pedidos_cancelados'),
DB::raw('(
SELECT
count(ahpv.id)
FROM
adega_has_pedido_venda ahpv,
pedido_venda pv
WHERE
ahpv.id_pedido_venda = pv.id
AND ahpv.id_adega = adega.id
AND pv.id_status_pedido_venda = 4 AND pv.frete = 0
) pedidos_estornados'),
DB::raw('(
SELECT
count(ahpv.id)
FROM
adega_has_pedido_venda ahpv,
pedido_venda pv
WHERE
ahpv.id_pedido_venda = pv.id
AND ahpv.id_adega = adega.id
AND pv.id_status_pedido_venda IN (2,7) AND pv.frete = 0
) qtde_pedidos'),
DB::raw('(
SELECT
sum(pvhp.valor_total)
FROM
adega_has_pedido_venda ahpv,
pedido_venda pv,
pedido_venda_has_produto pvhp
WHERE
ahpv.id_pedido_venda = pv.id
AND pv.id = pvhp.id_pedido_venda
AND ahpv.id_adega = adega.id
AND pv.id_status_pedido_venda IN (2,7) AND pv.frete = 0
) total_pedidos'),
DB::raw("(
SELECT
sum(pvhp.quantidade)
FROM
adega_has_pedido_venda ahpv,
pedido_venda pv,
pedido_venda_has_produto pvhp,
oferta
WHERE
ahpv.id_pedido_venda = pv.id
AND ahpv.id_adega = adega.id
AND pvhp.id_pedido_venda = pv.id
AND pvhp.id_oferta = oferta.id
AND pv.id_status_pedido_venda IN (2,7) AND pv.frete = 0 AND oferta.titulo NOT LIKE '%combo%'
) qtde_produtos"),
DB::raw("(
SELECT
sum(pvhp.quantidade) / (
SELECT
(COUNT(DISTINCT ohp.id_produto) / COUNT(DISTINCT ohp.id_oferta)) produtos_ofertas
FROM
adega_has_pedido_venda ahpv
INNER JOIN pedido_venda pv ON pv.id = ahpv.id_pedido_venda
INNER JOIN pedido_venda_has_produto pvhp ON pvhp.id_pedido_venda = pv.id
INNER JOIN oferta ON oferta.id = pvhp.id_oferta
INNER JOIN oferta_has_produto ohp ON ohp.id_oferta = oferta.id
WHERE adega.id = ahpv.id_adega AND pv.id_status_pedido_venda IN (2,7) AND pv.frete = 0 AND oferta.titulo LIKE '%combo%'
)
FROM
adega_has_pedido_venda ahpv,
pedido_venda pv,
pedido_venda_has_produto pvhp,
oferta
WHERE
ahpv.id_pedido_venda = pv.id
AND ahpv.id_adega = adega.id
AND pvhp.id_pedido_venda = pv.id
AND pvhp.id_oferta = oferta.id
AND pv.id_status_pedido_venda IN (2,7) AND pv.frete = 0 AND oferta.titulo LIKE '%combo%'
) qtde_produtos_combo")
);
$query->join('cadastro', 'cadastro.id', '=', 'adega.id_cliente');
$query->where('adega.status', 'A');
//Filtro Data Abertura
if ($request->input('term_data_inicio') && $request->input('term_data_fim'))
{
$query->whereBetween('adega.data_abertura', [$request->input('term_data_inicio') . " 00:00:00", $request->input('term_data_fim') . " 23:59:59"]);
}
elseif($request->input('term_data_inicio'))
{
$query->whereBetween('adega.data_abertura', [$request->input('term_data_inicio') . " 00:00:00", $request->input('term_data_inicio') . " 23:59:59"]);
}
//Filtro Cliente/ID Adega
if ($request->input('term'))
{
$query->where(function($qr) use ($request){
$qr->where('adega.id', '=', $request->input('term'));
$qr->orWhere('cadastro.nome', 'like', '%'.$request->input('term').'%');
});
}
$query->havingRaw('(qtde_produtos_combo + qtde_produtos) <> adega.quantidade_produtos OR total_pedidos <> adega.valor_total');
$query->orderBy('adega.data_abertura', 'DESC');
$adegas = $query->get();
$count = 0;
foreach ($adegas as $adega) {
$qtde_produtos = (!empty($adega->qtde_produtos) ? $adega->qtde_produtos : 0) + (!empty($adega->qtde_produtos_combo) ? $adega->qtde_produtos_combo : 0);
$qtde_produtos = number_format($qtde_produtos, 0, '','');
if($adega->quantidade_produtos == $qtde_produtos && $adega->valor_total == $adega->total_pedidos && $adega->quantidade_pedidos == $adega->qtde_pedidos){
unset($adegas[$count]);
}
$count++;
}
$data['adegas'] = $adegas;
return view('Adega::admin/adega-divergente',$data);
}
public function add(){
$data = array();
$data['modulo'] = $this->modulo;
$data['fields'] = [];
foreach ($this->fields as $field) {
$data['fields'][] = $field;
}
foreach ($this->fks as $fk) {
$data['fields'][] = $fk;
}
usort($data['fields'], function($a, $b) {
return $a->ordem - $b->ordem;
});
foreach ($this->fks as $fk) {
$classPath = '\App\Modules\\'.$fk->moduloRelacionado->nome.'\Models\\'.$fk->moduloRelacionado->nome;
$data['array_'.$fk->nome] = $classPath::get();
}
$data['nextId'] = $this->adega_m->getNextAutoIncrement();
return view('Adega::admin/form-adega', $data);
}
public function edit($id){
$id = Adega::where('id', $id)->value('id');
$data['modulo'] = $this->modulo;
$data['fields'] = [];
foreach ($this->fields as $field) {
$data['fields'][] = $field;
}
foreach ($this->fks as $fk) {
$data['fields'][] = $fk;
}
usort($data['fields'], function($a, $b) {
return $a->ordem - $b->ordem;
});
foreach ($this->fks as $fk) {
$classPath = '\App\Modules\\'.$fk->moduloRelacionado->nome.'\Models\\'.$fk->moduloRelacionado->nome;
$data['array_'.$fk->nome] = $classPath::get();
}
$data['adega'] = $this->adega_m->find($id);
if($this->modulo->galeria){
$data['adega']->imagens = $this->adega_m->getImagens($id);
}
//Confrarias
$data['confrarias'] = Confraria::where('confraria.deleted', 0)->orderBy('nome', 'ASC')->get();
return view('Adega::admin/form-adega',$data);
}
public function save(Request $request){
$post = $request->input();
$post['meta_keywords'] = (isset($post['meta_keywords'])) ? implode(',',$post['meta_keywords']) : null;
foreach ($this->fields as $field) {
$arrayFields[] = $field->nome;
}
foreach ($this->fks as $fk) {
$arrayFields[] = $fk->nome;
}
if($this->modulo->imagem){
$arrayFields[] = 'thumbnail_principal';
}
if($request->input('id')){
$id_adega = $this->adega_m->editar($arrayFields, $post, $request->input('id'));
}else{
$id_adega = $this->adega_m->criar($arrayFields, $post);
}
\Session::flash('type', 'success');
\Session::flash('message', "Alteracoes salvas com sucesso!");
return redirect('admin/adega');
}
public function upload_image(Request $request) {
if($request->hasFile('file')) {
//upload an image to the /img/tmp directory and return the filepath.
$file = $request->file('file');
$tmpFilePath = '/uploads/adega/';
$tmpFileName = time() . '-' . $file->getClientOriginalName();
$file = $file->move(public_path() . $tmpFilePath, $tmpFileName);
$path = $tmpFilePath . $tmpFileName;
return response()->json(array('path'=> $path, 'file_name'=>$tmpFileName), 200);
} else {
return response()->json(false, 200);
}
}
public function upload_galeria($id, Request $request) {
if($request->hasFile('file')) {
//upload an image to the /img/tmp directory and return the filepath.
$file = $request->file('file');
$tmpFilePath = '/uploads/adega/';
$tmpFileName = time() . '-' . $file->getClientOriginalName();
$file = $file->move(public_path() . $tmpFilePath, $tmpFileName);
$path = $tmpFilePath . $tmpFileName;
$this->adega_m->criar_imagem(array('id_adega' => $id, 'thumbnail_principal' => $tmpFileName));
return response()->json(array('path'=> $path, 'file_name'=>$tmpFileName), 200);
} else {
return response()->json(false, 200);
}
}
public function crop_image(Request $request) {
$img = \Image::make('uploads/adega/'.$request->input('file_name'));
$dataCrop = json_decode($request->input('data_crop'));
if($img->crop(intval($dataCrop->width), intval($dataCrop->height), intval($dataCrop->x), intval($dataCrop->y))->save('uploads/adega/thumb_'.$request->input('file_name'))){
@unlink('uploads/adega/'.$request->input('file_name'));
echo json_encode(array(
'status' => true,
'path' => '/uploads/adega/thumb_'.$request->input('file_name'),
'file_name' => 'thumb_'.$request->input('file_name'),
));
}else{
echo json_encode(array(
'status' => false,
'message' => 'Não foi possível alterar a imagem.'
));
}
}
public function atualizaAdegas(Request $request){
$ids = $request->input('ids_adega');
$status = true;
$message = "Adegas atualizadas com sucesso!";
$adegas = Adega::whereIn('id', $ids)->get();
foreach ($adegas as $adega) {
Log::info('Adega->atualizaAdegas: atualizando Adega: ' . $adega->id . ' / cliente: ' . $adega->cliente->nome . ' ' . $adega->cliente->sobrenome);
GeralService::recalcularAdega($adega->id_cliente, false, $adega->id, $adega->confraria_id);
}
return response()->json(['status' => $status, 'message' => $message]);
}
public function atualizaAdega(Request $request){
try {
if(!$request->input('id_cliente')){
return response()->json(array('status' => false, 'message' => 'cliente não informado!'));
}
$id_adega = $request->input('id_adega');
$adega = Adega::where('id', $id_adega)->first();
$id_cliente = $adega->id_cliente;
GeralService::recalcularAdega($id_cliente, false, $id_adega, $adega->confraria_id);
$Adega = Adega::where('id', $id_adega)->first();
return response()->json(array(
'status' => true,
'message' => 'Adega atualizada com sucesso!',
'quantidade_pedidos' => $Adega->quantidade_pedidos,
'quantidade_produtos' => $Adega->quantidade_produtos,
'valor_total' => number_format($Adega->valor_total,2,'.','')
));
} catch (Exception $e) {
return response()->json(array('status' => false, 'message' => $e->getMessage()));
}
}
public function delete($id){
$id = Adega::where('id', $id)->value('id');
$this->adega_m->deletar($id);
\Session::flash('type', 'success');
\Session::flash('message', "Registro removido com sucesso!");
return redirect('admin/adega');
}
public function delete_imagem($id){
try{
$imagem = $this->adega_m->getImagem($id);
$this->adega_m->deletar_imagem($id);
unlink('uploads/adega/'.$imagem->thumbnail_principal);
return response()->json(array('status' => true, 'message' => 'Registro removido com sucesso!'));
}catch(Exception $e){
return response()->json(array('status' => false, 'message' => $e->getMessage()));
}
}
private function slugify($string)
{
return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}
}