Well, I start with MySQL
CREATE TABLE `tbpedido` (
`ped` int NOT NULL AUTO_INCREMENT,
`total` float DEFAULT NULL,
`dia` date DEFAULT NULL,
`codp` mediumint DEFAULT NULL,
`horavenda` datetime DEFAULT NULL,
`vendido` date DEFAULT NULL,
`dinheiro` float DEFAULT NULL,
`troco` float DEFAULT NULL,
`cartao` tinyint NOT NULL,
`pix` tinyint NOT NULL,
PRIMARY KEY (`ped`)
) ENGINE=InnoDB AUTO_INCREMENT=950 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
The Model tbpedido.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class tbpedido extends Model {
use HasFactory;
public $timestamps=false;
protected $table="tbpedido";
protected $fillable=['ped','dia','vendido'];
function details(){return $this->hasMany(tbpedido::class,'vendido','vendido');}
function pessoas(){return $this->hasMany(tbpessoa::class,'codp','codp');}
}
TesteController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\tbpedido;
use App\Models\test;
Use DB;
class TesteController extends Controller {
public function index(Request $request){
$groups=tbpedido::whereNotNull('vendido')
->select('vendido',DB::raw("sum(total) as total"))
->groupBy('vendido')->orderBy('vendido','desc')->get();
$vendamensal=tbpedido::whereNotNull('vendido')->
select(DB::raw('date_format(vendido,"%Y %m") as mes, sum(total) as total'))->
groupBy('mes')->orderBy('mes','desc')->get();
return view('teste',compact('groups'),['vendamensal'=>$vendamensal]);}
}
teste.blade.php
@include('menu') // I use this to load Bootstrap CSS
<script>
document.title="Relatório de Vendas"
btmenu.innerHTML="Relatório de Vendas"
function alternar(e){
if(e.className=='table table-striped d-block'){
e.className='table table-striped d-none'} else {
e.className='table table-striped d-block'}}
</script>
<i>Para ver detalhes do dia clique no dia</i><br>
@foreach($vendamensal as $venda)
mês {{$venda->mes}} => R$ <font color=red> {{dec($venda->total)}}</font><br>
@endforeach
<table><tr><th>Dia<th style='text-align:right'>Total
@foreach($groups as $group)
@php $dia=date('Y-m-d',strtotime($group->vendido));$id="t".str_replace('-','',$dia); @endphp
<tr onclick="alternar({{$id}})"><td><b>{{date('d/m/y',strtotime($group->vendido))}}
<td align=right><b>{{dec($group->total)}}
<tr><td><table id={{$id}} class="table table-striped d-none"><th>{{$group->dia}}<th>Pedido<th>Total<th>dinheiro<th>troco<th>cartão<th>pix
@foreach($group->details as $detail)
<tr><td align=right>{{date('H:i',strtotime($detail->horavenda))}}
<td align=right>{{$detail->ped}}
<td align=right>{{dec($detail->total)}}
<td align=right>
@if(abs($detail->dinheiro)>0)
{{dec($detail->dinheiro)}} // dec is a helper function that changes "." to ","
@endif
<td align=right>
@if(abs($detail->troco)>0)
{{dec($detail->troco)}}
@endif
<td align=center>@if($detail->cartao==1) x @endif
<td align=center>@if($detail->pix==1) x @endif
@endforeach
</table>
@endforeach
</table>
The trouble is if is possible to work with one field (horavenda, datetime) instead two fields (vendido, date) and (horavenda, datetime). Thanks.