Nueva logica para planilla de notas, a partir de producto
This commit is contained in:
		
							parent
							
								
									df05b31b86
								
							
						
					
					
						commit
						f84e1f2954
					
				
					 1 changed files with 40 additions and 0 deletions
				
			
		|  | @ -113,4 +113,44 @@ class Producto extends Model | ||||||
|             var_export($e->getRecords()); |             var_export($e->getRecords()); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     static public function planillaNotas() { | ||||||
|  |         $headers = ['Producto']; | ||||||
|  |         $barrios = DB::table('grupos_de_compra')->pluck('nombre')->toArray(); | ||||||
|  |         $headers = array_merge($headers, $barrios); | ||||||
|  | 
 | ||||||
|  |         $notasPorBarrio = DB::table('productos') | ||||||
|  |             ->join('producto_subpedido', 'productos.id', '=', 'producto_subpedido.producto_id') | ||||||
|  |             ->join('subpedidos', 'producto_subpedido.subpedido_id', '=', 'subpedidos.id') | ||||||
|  |             ->join('grupos_de_compra', 'subpedidos.grupo_de_compra_id', '=', 'grupos_de_compra.id') | ||||||
|  |             ->where('productos.requiere_notas', 1) | ||||||
|  |             ->select( | ||||||
|  |                 'productos.nombre as producto', | ||||||
|  |                 'grupos_de_compra.nombre as barrio', | ||||||
|  |                 'producto_subpedido.notas' | ||||||
|  |             ) | ||||||
|  |             ->get() | ||||||
|  |             ->groupBy('producto'); // Group notes by product
 | ||||||
|  | 
 | ||||||
|  |         $planilla = []; | ||||||
|  | 
 | ||||||
|  |         foreach ($notasPorBarrio as $producto => $notasGrupo) { | ||||||
|  |             $fila = [$producto]; | ||||||
|  | 
 | ||||||
|  |             foreach ($barrios as $barrio) { | ||||||
|  |                 $notas = $notasGrupo->where('barrio', $barrio)->pluck('notas')->implode('; '); | ||||||
|  |                 $fila[] = $notas ?: ''; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             $planilla[] = $fila; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         try { | ||||||
|  |             $writer = Writer::createFromPath(resource_path('csv/exports/notas-por-barrio.csv'), 'w'); | ||||||
|  |             $writer->insertOne($headers); | ||||||
|  |             $writer->insertAll($planilla); | ||||||
|  |         } catch (CannotInsertRecord $e) { | ||||||
|  |             var_export($e->getRecords()); | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue