diff --git a/app/GrupoDeCompra.php b/app/GrupoDeCompra.php index 0522d47..07b8634 100644 --- a/app/GrupoDeCompra.php +++ b/app/GrupoDeCompra.php @@ -2,6 +2,7 @@ namespace App; +use App\Helpers\TransporteHelper; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; use League\Csv\CannotInsertRecord; @@ -16,6 +17,11 @@ class GrupoDeCompra extends Model protected $table = 'grupos_de_compra'; protected $hidden = ['password']; + public function subpedidos() + { + return $this->hasMany('App\Subpedido'); + } + public function toggleDevoluciones() { $this->devoluciones_habilitadas = !$this->devoluciones_habilitadas; @@ -23,16 +29,73 @@ class GrupoDeCompra extends Model return $this->devoluciones_habilitadas; } - public function subpedidos() - { - return $this->hasMany('App\Subpedido'); - } - public function pedidosAprobados() { return $this->subpedidos->where('aprobado', 1); } + public function totalARecaudar() + { + $total = 0; + foreach ($this->pedidosAprobados() as $subpedido) { + $total = $total + $subpedido->total(); + } + return $total; + } + + public function totalBarrial() + { + $total = 0; + foreach ($this->pedidosAprobados() as $subpedido) { + $total = $total + $subpedido->totalBarrial(); + } + return $total; + } + + public function totalDevoluciones() + { + $total = 0; + foreach ($this->pedidosAprobados() as $subpedido) { + $total = $total + $subpedido->devoluciones_total; + } + return $total; + } + + public function totalATransferir() + { + return $this->totalCentralesQueNoPaganTransporte() + + $this->totalCentralesQuePaganTransporte() + + $this->totalTransporte(); + } + + public function totalCentralesQueNoPaganTransporte() + { + $total = 0; + foreach ($this->pedidosAprobados() as $subpedido) { + $total = $total + $subpedido->totalCentralesQueNoPaganTransporte(); + } + return $total; + } + + public function totalCentralesQuePaganTransporte() + { + $total = 0; + foreach ($this->pedidosAprobados() as $subpedido) { + $total = $total + $subpedido->totalCentralesQuePaganTransporte(); + } + return $total; + } + + public function totalTransporte() + { + return TransporteHelper::totalTransporte($this->totalCentralesQuePaganTransporte()); + } + + public function cantidadTransporte() + { + return TransporteHelper::cantidadTransporte($this->totalCentralesQuePaganTransporte()); + } + public function exportarPlanillasAPdf() { $subpedidos = $this->pedidosAprobados(); diff --git a/app/Helpers/TransporteHelper.php b/app/Helpers/TransporteHelper.php new file mode 100644 index 0000000..8bfc3de --- /dev/null +++ b/app/Helpers/TransporteHelper.php @@ -0,0 +1,19 @@ + $this->id, + 'nombre' => $this->nombre, + 'devoluciones_habilitadas' => $this->devoluciones_habilitadas, + 'pedidos' => SubpedidoResource::collection($this->subpedidos), + 'total_a_recaudar' => number_format($this->totalARecaudar(),2), + 'total_barrial' => number_format($this->totalBarrial(),2), + 'total_devoluciones' => number_format($this->totalDevoluciones(),2), + 'total_a_transferir' => number_format($this->totalATransferir(),2), + 'total_transporte' => number_format($this->totalTransporte(),0), + 'cantidad_transporte' => number_format($this->cantidadTransporte(),0), + ]; + } +} diff --git a/app/Http/Resources/SubpedidoResource.php b/app/Http/Resources/SubpedidoResource.php index 3108eb5..d0da61f 100644 --- a/app/Http/Resources/SubpedidoResource.php +++ b/app/Http/Resources/SubpedidoResource.php @@ -17,16 +17,14 @@ class SubpedidoResource extends JsonResource return [ 'id' => $this->id, 'nombre' => $this->nombre, - 'subtotal_productos' => number_format($this->totalSinBonos(),0), - 'subtotal_bonos' => number_format($this->getSubtotalBonos(),0), - 'bonos_de_transporte' => $this->cantidadBDT(), - 'subtotal_bonos_de_transporte' => number_format($this->getSubtotalBDT(),0), - 'total' => number_format($this->getTotal(),0), - 'total_menos_devoluciones' => number_format($this->getTotalMenosDevoluciones(),0), 'grupo_de_compra' => $this->grupoDeCompra, 'productos' => $this->productos, 'aprobado' => (bool) $this->aprobado, - 'devoluciones_total' => (double) $this->devoluciones_total, + 'total' => number_format($this->total(),2), + 'total_transporte' => number_format($this->totalTransporte(),0), + 'cantidad_transporte' => number_format($this->cantidadTransporte(),0), + 'total_sin_devoluciones' => number_format($this->totalSinDevoluciones(),2), + 'devoluciones_total' => number_format($this->devoluciones_total,2), 'devoluciones_notas' => $this->devoluciones_notas ]; } diff --git a/app/Subpedido.php b/app/Subpedido.php index 62eccb1..f99dfdd 100644 --- a/app/Subpedido.php +++ b/app/Subpedido.php @@ -2,7 +2,7 @@ namespace App; -use League\Csv\Reader; +use App\Helpers\TransporteHelper; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; use Log; @@ -10,7 +10,8 @@ use App\Filtros\FiltroDeSubpedido; class Subpedido extends Model { - public $timestamps = false; + const COSTO_TRANSPORTE = 15; + public $timestamps = false; protected $fillable = ['grupo_de_compra_id', 'aprobado', 'nombre', 'devoluciones_total', 'devoluciones_notas']; public function productos() @@ -40,48 +41,66 @@ class Subpedido extends Model return $filtros->aplicar($query); } - //Subtotal de dinero de productos del pedido, sin bonos ni transporte - public function totalSinBonos() - { - return $this->productosSinBonos()->sum('total'); - } + public function total() + { + return $this->totalSinDevoluciones() - $this->devoluciones_total; + } - public function totalParaTransporte() { - $total = 0; - foreach ($this->productos()->get() as $producto) { - if ($producto->pagaTransporte()) { - $total += $producto->precio * $producto->pivot->cantidad; - } - } - return ceil($total); - } + public function totalSinDevoluciones() + { + return $this->totalBarrial() + $this->totalCentral(); + } - //Cantidad de bonos de transporte - public function cantidadBDT() - { - return ceil($this->totalParaTransporte() / 500); - } + public function totalBarrial() + { + return DB::table('producto_subpedido') + ->join('productos', 'producto_subpedido.producto_id', '=', 'productos.id') + ->where('producto_subpedido.subpedido_id', $this->id) + ->where('productos.nombre', 'like', '%barrial%') + ->selectRaw('SUM(productos.precio * producto_subpedido.cantidad) as total') + ->value('total'); + } - //Subtotal de dinero de bonos de transporte - public function getSubtotalBDT() - { - return $this->cantidadBDT() * 15; - } + public function totalCentral() + { + return $this->totalCentralesQueNoPaganTransporte() + $this->totalCentralesQuePaganTransporte() + $this->totalTransporte(); + } - //Subtotal de dinero de bonos (MPS, Sororo, etc) - public function getSubtotalBonos() - { - return $this->bonos()->sum('total'); - } + public function totalCentralesQueNoPaganTransporte() + { + return DB::table('producto_subpedido') + ->join('productos', 'producto_subpedido.producto_id', '=', 'productos.id') + ->where('producto_subpedido.subpedido_id', $this->id) + ->where('productos.nombre', 'not like', '%barrial%') + ->where(function ($query) { + $query->where('productos.categoria', 'like', '%SUBSIDIADO%') + ->orWhere('productos.bono', true); + }) + ->selectRaw('SUM(productos.precio * producto_subpedido.cantidad) as total') + ->value('total'); + } - public function getTotal() - { - return $this->totalSinBonos() + $this->getSubtotalBDT() + $this->getSubtotalBonos(); - } + public function totalCentralesQuePaganTransporte() + { + return DB::table('producto_subpedido') + ->join('productos', 'producto_subpedido.producto_id', '=', 'productos.id') + ->where('producto_subpedido.subpedido_id', $this->id) + ->where('productos.nombre', 'not like', '%barrial%') + ->where('productos.bono', false) + ->where('productos.categoria', 'not like', '%SUBSIDIADO%') + ->selectRaw('SUM(productos.precio * producto_subpedido.cantidad) as total') + ->value('total'); + } - public function getTotalMenosDevoluciones() { - return $this->getTotal() - $this->getDevoluciones(); - } + public function totalTransporte() + { + return TransporteHelper::totalTransporte($this->totalCentralesQuePaganTransporte()); + } + + public function cantidadTransporte() + { + return TransporteHelper::cantidadTransporte($this->totalCentralesQuePaganTransporte()); + } //Actualiza el pedido, agregando o quitando del subpedido según sea necesario. Debe ser llamado desde el controlador de subpedidos, luego de validar que los parámetros $producto y $cantidad son correctos. También calcula el subtotal por producto. public function syncProducto(Producto $producto, Int $cantidad, string $notas) { @@ -110,13 +129,6 @@ class Subpedido extends Model return $view->render(); } - public function getDevoluciones() { - return $this->devoluciones_total; - } - - public function getNotasDevoluciones() { - return $this->devoluciones_notas; - } public function syncDevoluciones(float $total, string $notas) { $this->devoluciones_total = $total; $this->devoluciones_notas = $notas; diff --git a/resources/js/components/admin/Body.vue b/resources/js/components/admin/Body.vue index 1b4b8c5..2083305 100644 --- a/resources/js/components/admin/Body.vue +++ b/resources/js/components/admin/Body.vue @@ -3,27 +3,18 @@
-
+
+ :gdc_id="gdc.id"> - + :gdc="this.gdc" + >
-

+

Todavía no hay ningún pedido para administrar.

-
- - -

- Todavía no hay pedidos aprobados. -

-
@@ -38,6 +29,7 @@ import TabsSecciones from "../comunes/TabsSecciones.vue"; import DropdownDescargar from "./DropdownDescargar.vue"; import TablaPedidos from "./TablaPedidos.vue"; import TablaBonos from "./TablaBonos.vue"; +import axios from "axios"; export default { components: { CaracteristicasOpcionales, @@ -48,12 +40,8 @@ export default { }, data() { return { - gdc: 0, - pedidos: [], - bonosDeTransporte: 0, - totalBonosBarriales: 0, + gdc: undefined, tabs: [{ id: "pedidos", nombre: "Pedidos" }, - { id: "bonos", nombre: "Bonos" }, { id: "caracteristicas", nombre: "Caracteristicas opcionales" }], tabActiva: "pedidos", seccionActiva: "pedidos-seccion", @@ -61,46 +49,32 @@ export default { }, computed: { hayPedidos: function() { - return this.pedidos.length !== 0 + return this.gdc && this.gdc.pedidos.length !== 0 }, hayAprobados: function() { - return this.pedidos.filter(p => p.aprobado).length > 0 + return this.gdc && this.gdc.pedidos.filter(p => p.aprobado).length > 0 } }, methods: { - fetchPedidos() { - axios.get("/api/grupos-de-compra/"+this.gdc+"/bonos-de-transporte", {}) - .then(response => this.bonosDeTransporte = response.data.bdt); - axios.get("/api/subpedidos/resources", { - params: { - grupo_de_compra: this.gdc - }}) - .then(response => { - this.pedidos = response.data.data - }).get; - axios.get("/api/grupos-de-compra/"+this.gdc+"/bonos-barriales", {}) - .then(response => this.totalBonosBarriales = response.data.bonos_barriales) - }, setSeccionActiva(tabId) { this.tabActiva = tabId; this.seccionActiva = tabId + "-seccion"; }, - getBonosBarriales() { - axios.get("/api/grupos-de-compra/"+this.gdc+"/bonos-barriales", {}) - .then(response => this.totalBonosBarriales = response.data.bonos_barriales) + actualizar() { + axios.get('/api/grupos-de-compra/' + this.$root.gdc) + .then(response => { + this.gdc = response.data.data; + console.log(this.gdc); + }) } }, - mounted() { + async mounted() { Event.$on('sync-aprobacion', (_) => { - this.fetchPedidos(); + this.actualizar(); }); - - axios.get("/admin/obtener_sesion").then(response => { - this.gdc = response.data.gdc; - this.fetchPedidos(); - this.bonosBarriales = this.getBonosBarriales() - }); - } + await new Promise(r => setTimeout(r, 1000)); + this.actualizar(); + }, } diff --git a/resources/js/components/admin/DropdownDescargar.vue b/resources/js/components/admin/DropdownDescargar.vue index 2396604..0b95488 100644 --- a/resources/js/components/admin/DropdownDescargar.vue +++ b/resources/js/components/admin/DropdownDescargar.vue @@ -14,13 +14,13 @@