Ir para conteúdo
Fórum Script Brasil
  • 0

Um modal dentro do outro


Frank K Hosaka

Pergunta

Fazer modal no PHP ou no Laravel foi bem difícil para mim, pois eu não tinha nenhuma familiaridade com o JavaScript.

Já o Livewire trouxe um novo conceito, onde você divide o HTML em várias partes, e você vai exibindo de acordo com a necessidade. No meu caso, eu dividi o HTML em $modal, $modal2 e $modal3. Dentro do primeiro $modal existem dezenas de "submodais" que são controlados pela variável $comando. $modal2 mostra a nota do fornecedor onde tem quantidade e preço. O $modal3 mostra a mesma nota, mas com o CFOP e o NCM.

 

arquivo app > Livewire > NotaFiscal.php
<?php
namespace App\Livewire;
use App\Models\tbdiario;
use App\Models\tbpessoa;
use App\Models\tbhistprod;
use App\Models\tbprod;
use Illuminate\Support\Facades\DB;
use Livewire\Attributes\Layout;
use Livewire\Component;
use Livewire\WithFileUploads;

#[Layout('components.layouts.app',['titulo'=>'NF do Fornecedor'])]
class NotaFiscal extends Component {

    use WithFileUploads;
    public $comando="pegar xml",$criterio,$difAliqICMS,$inativos=[],$indicador,$lcto;
    public $modal=true,$modal2,$modal3;
    public $nNF,$opcoes=[],$previa=[],$quant,$soma,$xNome,$XmlFile,$vNF;

    function updatedXmlFile() {
        $this->previa=[];
        $xmlContent=file_get_contents($this->XmlFile->getRealPath());
        $nfe=simplexml_load_string($xmlContent);
        $itens=$nfe->NFe->infNFe->det;
        $this->difAliqICMS=0;
        $aliquotaInterna=0.18;
        foreach ($itens as $item) {
            if ((float)$item->prod->CFOP == 6102 || (float)$item->prod->CFOP ==6101) {
                $vBC = (float)$item->imposto->ICMS->ICMS10->vBC;
                $vBC = $vBC ? $vBC : (float)$item->imposto->ICMS->ICMS00->vBC;
                $vICMS = (float)$item->imposto->ICMS->ICMS10->vICMS;
                $vICMS = $vICMS ? $vICMS : (float)$item->imposto->ICMS->ICMS00->vICMS;
                $this->difAliqICMS += round(($vBC * $aliquotaInterna - $vICMS+0.00001),2);
            }
        }     
        $this->vNF = (float)$nfe->NFe->infNFe->total->ICMSTot->vNF;   
        $this->nNF = (string)$nfe->NFe->infNFe->ide->nNF; 
        $this->xNome = (string)$nfe->NFe->infNFe->emit->xNome;
        $nome = explode(" ", $this->xNome)[0]; 
        $codp = tbpessoa::where('pessoa','like','%'.$nome.'%')->value('codp');
        $this->soma=0;
        foreach ($itens as $item) {
            $codforn = (string)$item->prod->cProd; 
            $criterio= strtolower($nome[0].$codforn);
            $consulta = tbprod::where('codforn','like','%'.$criterio.'%')->get();
            $cfop = (string)$item->prod->CFOP;
            $ncm = (string)$item->prod->NCM;
            if (count($consulta) == 1) {
                $codprod = $consulta[0]->codprod;
                if(substr($consulta[0]->cfop,0,1)==0) {
                    $this->inativos[]=$consulta[0]->codprod;
                }
                $class=($cfop == substr($consulta[0]->cfop,-4)) ? "w-[50px] text-right" : 
                    "w-[50px] text-red-500 text-right";
                $class2=($ncm == $consulta[0]->cf) ? "w-[100px] text-right" : 
                "w-[100px] text-red-500 text-right";
            } 
            if (count($consulta) > 1) {
                $consulta2 = tbprod::where('codforn','like','%'.$criterio)->get();
                if(count($consulta2) == 1){
                    $codprod = $consulta2[0]->codprod;
                    $consulta = $consulta2;
                } else {
                    $codprod = "mult";
                }
            }
            if (count($consulta)==0) {
                $codprod = "none";
            } 
            $produto = substr((string)$item->prod->xProd,0,79); 
            $quantidade = (float)$item->prod->qCom;
            if($cfop==6101 || $cfop==6102 || $cfop==5101) {
                $cfop=5102;
            }
            if($cfop==6401 || $cfop==6402 || $cfop==6403 || $cfop==5401 || $cfop==5403 || $cfop==5655) {
                $cfop=5405;
            } 
            if (is_numeric($codprod)) {
                $produtoDobrado = [506, 507, 508, 509, 510, 519, 1768, 1770, 1772];
                if (in_array($codprod, $produtoDobrado)) {
                    $quantidade = 2 * $quantidade;
                }
                $produtox5=[1798];
                if (in_array($codprod,$produtox5 )) {
                    $quantidade = 5 * $quantidade;
                }
                $produtoX10 = [2192,1782,2456];
                if (in_array($codprod, $produtoX10)) {
                    $quantidade = 10 * $quantidade;
                }
                $produtox12 = [2403,2406,2496,2497,2498,2499];
                if (in_array($codprod, $produtox12)) {
                    $quantidade = 12 * $quantidade;
                }
                $produtox24 = [2493,2405,2494,2495,2409];
                if (in_array($codprod, $produtox24)) {
                    $quantidade = 24 * $quantidade;
                }
            }
            $vICMSST1 = ((float)$item->imposto->ICMS->ICMS10->vICMSST) ?
                (float)$item->imposto->ICMS->ICMS10->vICMSST : null;
            $vICMSST2 = ((float)$item->imposto->ICMS->ICMSSN202->vICMSST) ?
                (float)$item->imposto->ICMS->ICMSSN202->vICMSST : null;
            $vICMSST = $vICMSST1 ? $vICMSST1 : $vICMSST2;
            $difAliq = 0;
            if ((float)$item->prod->CFOP == 6102 || (float)$item->prod->CFOP == 6101){
                $vBC = (float)$item->imposto->ICMS->ICMS10->vBC;
                $vBC = $vBC ? $vBC : (float)$item->imposto->ICMS->ICMS00->vBC;
                $vICMS = (float)$item->imposto->ICMS->ICMS10->vICMS;
                $vICMS = $vICMS ? $vICMS : (float)$item->imposto->ICMS->ICMS00->vICMS;
                $difAliq = round(($vBC * $aliquotaInterna - $vICMS+0.00001),2);
            }
            $vIPI =(isset($item->imposto->IPI->IPITrib->vIPI) && (float)$item->imposto->IPI->IPITrib->vIPI) ? 
                (float)$item->imposto->IPI->IPITrib->vIPI : 0;
            $vProd = (string)$item->prod->vProd;
            $valorTotal = $vProd + $vICMSST + $difAliq + $vIPI;
            $this->soma += $valorTotal;
            $custoAtual = intval($valorTotal / $quantidade * 100) / 100;
            $custoAnterior=0;
            $marg=0;
            $cclass="w-[70px] text-right";
            $nclass="w-[100px] text-right";
            $fclass="w-[50px] text-right";
            if (is_numeric($codprod)) {
                $consulta = tbprod::where('codprod',$codprod)->first();
                $custoAnterior = $consulta->custo;
                $marg=$consulta->marg;
                if (abs($custoAnterior - $custoAtual) > 0.02) {
                    $cclass = 'w-[70px] text-right text-red-500';
                }
                if ($ncm !== $consulta->cf) {
                    $nclass = 'w-[100px] text-right text-red-500';
                }
                if (trim($cfop) !== trim(substr($consulta->cfop,-4))) {
                    $fclass = 'w-[50px] text-right text-red-500';
                }
            }
            $this->previa[]=['codforn'=>$codforn,'codprod'=>$codprod,'prod'=>$produto,
                'qt'=>$quantidade,'custoanterior'=>$custoAnterior,'custoatual'=>$custoAtual,
                'custototal'=>$valorTotal, 'fclass'=>$fclass,
                'cfop'=>$cfop,'codp'=>$codp,'ncm'=>$ncm,'nclass'=>$nclass,'cclass'=>$cclass,
                'marg'=>$marg];
        }
        $this->modal=false;
        $this->modal2=true;
    }

    function atualizarCfop($indicador) {
        $codprod=$this->previa[$indicador]['codprod'];
        $cfop=$this->previa[$indicador]['cfop'];
        DB::table('tbprod')->where('codprod',$codprod)
            ->update(['cfop' => 
            DB::raw("CONCAT(LEFT(cfop, LENGTH(cfop) - 4), $cfop)")]);
        $this->previa[$indicador]['fclass']='w-[50px] text-right';
    }

    function atualizarNcm($indicador) {
        $codprod=$this->previa[$indicador]['codprod'];
        $ncm=$this->previa[$indicador]['ncm'];
        tbprod::where('codprod',$codprod)->update(['cf'=>$ncm]);
        $this->previa[$indicador]['nclass']='w-[100px] text-right';
    }

    function cancelar() { $this->modal=false; }

    function custo($indicador) {
        $this->indicador=$indicador;
        $this->comando="alterar custo";
        $this->modal=true;
    }

    function custoConfirmado() {
        $custo=$this->previa[$this->indicador]['custoatual'];
        $codprod=$this->previa[$this->indicador]['codprod'];
        $this->previa[$this->indicador]['cclass']="w-[70px] text-right";
        tbprod::where('codprod',$codprod)->update(['custo'=>$custo]);
        $this->modal=false;
    }

    function mostrarMais() {
        if($this->modal2) {
            $this->modal2=false;
            $this->modal3=true;
        } else {
            $this->modal2=true;
            $this->modal3=false;
        }
    }

    function multiplo($indicador) {
        $this->criterio=$this->previa[$indicador]['codforn'];
        $consulta=tbprod::where('codforn','like',"%$this->criterio")->get();
        $this->opcoes=[];
        foreach($consulta as $c) {
            $this->opcoes[]=['indicador'=>$indicador,'codprod'=>$c->codprod,'prod'=>$c->prod];
        }
        $this->modal=true;
    }

    function qtDefinir($indicador) {
        $this->indicador=$indicador;
        $this->comando="qt";
        $this->modal=true;
    }

    function qtDefinida() {
        $quant=$this->quant;
        $custo=round($this->previa[$this->indicador]['custototal']/$quant,2);
        $this->previa[$this->indicador]['qt']=$quant;
        $this->previa[$this->indicador]['custoatual']=$custo;
        $this->previa[$this->indicador]['cclass']="w-[50px] text-right text-red-500";
        $this->modal=false;
    }

    function selecionado($chave) {
        $indicador=$this->opcoes[$chave]['indicador'];
        $codprod=$this->opcoes[$chave]['codprod'];
        $this->previa[$indicador]['codprod']=$codprod;
        $consulta=tbprod::where('codprod',$codprod)->first();
        $this->previa[$indicador]['marg']=$consulta->marg;
        $this->previa[$indicador]['custoanterior']=$consulta->custo;
        if($this->previa[$indicador]['custoanterior'] !== $this->previa[$indicador]['custoatual']) {
            $previa[$indicador]['pclass']="w-[50px] text-right text-red-200";
        }
        $this->modal=false;
    }

    function transferir() { 
        if(count(tbhistprod::where('lcto',$this->lcto)->get())>0){
            $this->comando="transferido";
            $this->modal=true;
        } else {
            $dia=tbdiario::where('lcto',$this->lcto)->value('dia');
            foreach($this->previa as $p) {
                tbhistprod::create(['codprod'=>$p['codprod'],'dia'=>$dia,'qt'=>$p['qt'],
                    'custototal'=>$p['custototal'],'codp'=>$p['codp'],'lcto'=>$this->lcto]);
            }
            return redirect('ldiario');
        }
    }
}

arquivo resources > views > livewire > nota-fiscal.blade.php
<div>
    @if($modal)
        <div class="fixed inset-0 flex items-center justify-center z-10" style="background-color: rgba(243,244,246,0.5)">
            <div class="bg-white p-6 rounded shadow-lg">
                @if($comando=="pegar xml")
                    <flux:input type="file" wire:model="XmlFile" label="Pegar arquivo xml"/>
                @endif
                @if($comando=="escolher produto")
                    @foreach($opcoes as $chave => $o)
                        <flux:button wire:click="selecionado({{$chave}})">{{ $o['codprod']." ".$o['prod'] }} </flux:button>
                    @endforeach
                    <p>Existem muitos produtos com o critério {{$criterio}}. Escolha um.</p>
                @endif
                @if($comando=="alterar custo")
                    <div class=w-[300px]>
                        <flux:input.group>
                            <flux:input.group.prefix>Custo Anterior</flux:input.group.prefix>
                            <flux:input value="{{dec($previa[$indicador]['custoanterior'])}}" />
                        </flux:input.group>
                        <flux:input.group>
                            <flux:input.group.prefix>Custo Atual</flux:input.group.prefix>
                            <flux:input value="{{ dec($previa[$indicador]['custoatual']) }}" />
                        </flux:input.group>
                        <flux:button wire:click="custoConfirmado">Confirmar</flux:button>
                    </div>
                @endif
                @if($comando=="qt")
                    <div class=w-[300px]>
                        <flux:input.group>
                            <flux:input.group.prefix>Definir Quantidade</flux:input.group.prefix>
                            <flux:input wire:model="quant" wire:change="qtDefinida" autofocus autocomplete="off" />
                        </flux:input.group>
                        <flux:button wire:click="cancelar" class="mt-5">Cancelar</flux:button>
                    </div>
                @endif
                 @if($comando=="transferido")
                    <div class="text-lg">Documento já transferido anteriormente</div>
                @endif
            </div>
        </div>
    @endif

    @if($modal2 || $modal3)
        @if(count($inativos))
            <div class=mt-5>{{ "verificar ".print_r($inativos) }}</div>
        @endif
        <div class=flex>
            <div class=w-[530px]>NF {{ $nNF." ".$xNome }}</div>
            <div class="w-[100px] text-right">{{ dec($vNF) }}</div>
        </div>
        @if($difAliqICMS!==0)
            <div class="flex">
                <div class="w-[530px] text-right">Diferença de Alíquota de ICMS</div>
                <div class="w-[100px] text-right"><?=dec($difAliqICMS)?></div>
            </div>
            <div class="flex">
                <div class="w-[530px] text-right">Total a conferir</div>
                <div class="w-[100px] text-right"><?=dec($difAliqICMS+$vNF)?></div>
            </div>
        @endif

        @if($modal2)
            <div class="flex even:bg-gray-200">
                <div class="w-[70px] text-right">Cforn</div>
                <div class="w-[50px] text-right">Cod</div>
                <div class="w-[288px] px-2">Produto</div>
                <div class="w-[50px] text-right">Mg</div>
                <div class="w-[50px] text-right">Qt</div>
                <div class="w-[70px] text-right">Preço</div>
                <div class="w-[100px] text-right font-semibold" wire:click=mostrarMais>Total</div>
            </div>
            @foreach($previa as $key => $p)
                <div class="flex even:bg-gray-200">
                    <div class="w-[70px] text-right truncate">{{$p['codforn']}}</div>
                    @if($p['codprod']=="none")
                        <div class="w-[50px] text-right text-red-500">{{$p['codprod']}}</div>
                    @endif
                    @if($p['codprod']=="mult")
                        <div class="w-[50px] text-right text-red-500" wire:click="multiplo({{$key}})">
                            {{ $p['codprod']}}
                        </div>
                    @endif
                    @if($p['codprod']!=="none" && $p['codprod']!=="mult")
                        <div class="w-[50px] text-right">{{$p['codprod']}}</div>
                    @endif
                    <div class="w-[288px] px-2 truncate">{{$p['prod']}}</div>
                    <div class="w-[50px] text-right">{{$p['marg']}}</div>
                    <div class="w-[50px] text-right" wire:click="qtDefinir({{$key}})">{{$p['qt']}}</div>
                    <div class="{{ $p['cclass']}}" wire:click="custo({{$key}})">{{dec($p['custoatual'])}}</div>
                    <div class="w-[100px] text-right">{{dec($p['custototal'])}}</div>
                </div>
            @endforeach
        @endif

        @if($modal3)
            <div class="flex even:bg-gray-200">
                <div class="w-[70px] text-right">Cforn</div>
                <div class="w-[50px] text-right">Cod</div>
                <div class="w-[358px] px-2">Produto</div>
                <div class="w-[50px] text-right">CFOP</div>
                <div class="w-[100px] text-right font-semibold" wire:click="mostrarMais">NCM</div>
            </div>
            @foreach($previa as $key => $p)
                <div class="flex even:bg-gray-200">
                    <div class="w-[70px] text-right truncate">{{$p['codforn']}}</div>
                    @if($p['codprod']=="none")
                        <div class="w-[50px] text-right text-red-500">{{$p['codprod']}}</div>
                    @endif
                    @if($p['codprod']=="mult")
                        <div class="w-[50px] text-right text-red-500" wire:click="multiplo({{$key}})">
                            {{ $p['codprod']}}
                        </div>
                    @endif
                    @if($p['codprod']!=="none" && $p['codprod']!=="mult")
                        <div class="w-[50px] text-right">{{$p['codprod']}}</div>
                    @endif
                    <div class="w-[358px] px-2 truncate">{{$p['prod']}}</div>
                    <div class="{{$p['fclass']}}" wire:click="atualizarCfop({{ $key }})">
                        {{$p['cfop']}}
                    </div>
                    <div class="{{$p['nclass']}}" wire:click="atualizarNcm({{ $key }})">
                        {{$p['ncm']}}
                    </div>
                </div>
            @endforeach
        @endif

        <div class=flex>
            <div class="w-[300px]">
                <flux:input.group>
                    <flux:button>Enviar para Lançamento</flux:button>
                    <flux:input wire:model="lcto" wire:change="transferir" />
                </flux:input.group>
            </div>
            <div class="w-[330px] text-right">{{ dec($soma) }}</div>
        </div>
    @endif  
</div>

 

Link para o comentário
Compartilhar em outros sites

0 respostass a esta questão

Posts Recomendados

Até agora não há respostas para essa pergunta

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,5k
    • Posts
      652,3k
×
×
  • Criar Novo...