Advertisement
Guest User

Custom Report

a guest
Aug 25th, 2019
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.24 KB | None | 0 0
  1. <?php
  2.  
  3. include_once(WC()->plugin_path().'/includes/admin/reports/class-wc-admin-report.php');
  4. add_filter( 'woocommerce_admin_reports', 'my_custom_woocommerce_admin_reports', 10, 1 );
  5. function my_custom_woocommerce_admin_reports( $reports ) {
  6.     $ventas_panuts = array(
  7.         'ventas_panuts' => array(
  8.             'title'         => 'Informe Personalizado',
  9.             'description'   => '',
  10.             'hide_title'    => true,
  11.             'callback'      => 'informe_panuts_callback',
  12.         ),
  13.     );
  14.     // This can be: orders, customers, stock or taxes, based on where we want to insert our new reports page
  15.     $reports['orders']['reports'] = array_merge( $reports['orders']['reports'], $ventas_panuts);
  16.     return $reports;
  17. }
  18.  
  19. function informe_panuts_callback() {
  20.     $report = new WC_Reporte_Ventas_Panuts();
  21.     $report->output_report();
  22. }
  23.  
  24. function boton_exportar() {
  25.     $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day';
  26.     ?>
  27.         <form method="post" id="export-form" action="">
  28.         <?php submit_button('Descargar Reporte', 'primary', 'download_csv' ); ?>
  29.         </form>
  30.         <?php
  31. }
  32.  
  33. add_action('admin_init', 'endo_stock_report_admin_init');
  34. function endo_stock_report_admin_init() {
  35.     if ( isset($_POST['download_csv']) /*&& $plugin_page == 'endo_stock_report'*/ ) {
  36.        //revisar
  37.         $reporte = new WC_Reporte_Ventas_Panuts();
  38.         $reporte->generate_stock_report_csv();
  39.        
  40.         die();
  41.     }
  42. }
  43.  
  44.  
  45. class WC_Reporte_Ventas_Panuts extends WC_Admin_Report {
  46.  
  47.     /**
  48.     * Output the report.
  49.     */
  50.    
  51.     private $prerow;
  52.    
  53.     public $colector = array( 'esto es','la declaracion','del colector',':(');
  54.    
  55.     public function __construct($prerow) {
  56.         // revisar en casa esto
  57.         $row = array( 'esto es','la declaracion','del colector');
  58.         $prerow = $row;
  59.         $this->prerow = $prerow;  
  60.     }
  61.    
  62.     public function output_report() {
  63.         $ranges = array(
  64.             'year'         => __( 'Year', 'woocommerce' ),
  65.             'last_month'   => __( 'Last month', 'woocommerce' ),
  66.             'month'        => __( 'This month', 'woocommerce' ),
  67.         );
  68.         $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : 'month';
  69.         if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', '7day' ) ) ) {
  70.             $current_range = 'month';
  71.         }
  72.         $this->check_current_range_nonce( $current_range );
  73.         $this->calculate_current_range( $current_range );
  74.         $hide_sidebar = true;
  75.         include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php' );
  76.     }
  77.  
  78.      /**
  79.      * Output an export link.
  80.      */
  81.     public function get_export_button() {
  82.         $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day';
  83.         boton_exportar();
  84.     }
  85.    
  86.     /**
  87.     * Get the main chart.
  88.     */
  89.    
  90.     public function get_main_chart($prerow) {  
  91.         global $wpdb;
  92.         $query_data = array(
  93.             'ID' => array(
  94.                 'type'     => 'post_data',
  95.                 'function' => '',
  96.                 'name'     => 'id_orden',
  97.                 'distinct' => true,
  98.             ),
  99.             '_order_total'   => array(
  100.                 'type'      => 'meta',
  101.                 'function'  => '',
  102.                 'name'      => 'orden_total'
  103.             ),
  104.         );
  105.    
  106.         $ventas_panuts_orders = $this->get_order_report_data( array(
  107.             'data'                  => $query_data,
  108.             'query_type'            => 'get_results',
  109.             //'group_by'              => 'country',
  110.             'filter_range'          => true,
  111.             'order_types'           => wc_get_order_types( 'sales-reports' ),
  112.             'order_status'          => array( 'completed' ),
  113.             'parent_order_status'   => false,
  114.         ) );
  115.     ?>
  116.         <table class="widefat">
  117.             <thead>
  118.                 <tr>
  119.                     <th><strong>Fecha</strong></th>
  120.                     <th><strong>Mes</strong></th>
  121.                     <th><strong>Orden</strong></th>
  122.                     <th><strong>Nombre</strong></th>
  123.                     <th><strong>Apellido</strong></th>
  124.                     <th><strong>DNI</strong></th>
  125.                     <th><strong>Dirección</strong></th>
  126.                     <th><strong>Distrito</strong></th>
  127.                     <th><strong>Correo</strong></th>
  128.                     <th><strong>Teléfono</strong></th>
  129.                     <th><strong>Producto</strong></th>
  130.                     <th><strong>Cantidad</strong></th>
  131.                     <th><strong>Precio Unitario</strong></th>
  132.                     <th><strong>Subtotal</strong></th>
  133.                     <th><strong>Costo de Envío</strong></th>
  134.                     <th><strong>Total</strong></th>
  135.                     <th><strong>Método de Pago</strong></th>
  136.                 </tr>
  137.             </thead>
  138.         <tbody>
  139.         <?php foreach( $ventas_panuts_orders as $order ) {
  140.            
  141.             $order_obj = wc_get_order( $order->id_orden );
  142.             $order_data = $order_obj->get_data();
  143.             $date_created  = $order_obj->order_date;
  144.             $strtotime = strtotime($date_created);
  145.  
  146.             $order_items = $order_obj->get_items();
  147.             $i=true;
  148.             foreach( $order_items as $product ) {
  149.                 ?>
  150.                 <tr>
  151.                 <td><?php echo $date_created; ?></td>
  152.                 <td><?php echo date('M',$strtotime); ?></td>
  153.                 <td><?php echo $order->id_orden; ?></td>
  154.                 <td style="text-transform: capitalize;"><?php echo $order_obj->get_billing_first_name(); ?></td>
  155.                 <td style="text-transform: capitalize;"><?php echo $order_obj->get_billing_last_name(); ?></td>
  156.                 <td><?php echo get_post_meta( $order->id_orden, 'billing_dni', true ); ?></td>
  157.                 <td style="text-transform: capitalize;"><?php echo $order_obj->get_billing_address_1().' '.$order_obj->get_billing_address_2();?></td>
  158.                 <td><?php echo WC()->countries->get_states( $order_obj->get_billing_country() )[$order_obj->get_billing_state()]; ?></td>
  159.                 <td><?php echo $order_obj->get_billing_email(); ?></td>
  160.                 <td><?php echo get_post_meta( $order->id_orden, 'billing_cellphone', true ); ?></td>
  161.                 <td><?php echo '<p>'.$product['name'].'</p>'; ?></td>
  162.                 <td align="center"><?php echo '<p>'.$product['qty'].'</p>'; ?></td>
  163.                 <td align="center"><?php echo '<p>'.$product->get_total()/$product['qty'].'</p>'; ?></td>  
  164.                 <td align="center"><?php echo $product->get_total(); ?></td>
  165.                 <?php
  166.                 if ($i){ ?>
  167.                     <td><?php echo $order_obj->get_shipping_total(); ?></td>
  168.                     <td><?php echo wc_price($order->orden_total); ?></td> <!-- total de orden -->
  169.                     <td><?php echo $order_obj->get_payment_method_title(); ?></td>
  170.                     <?php $i=false; } ?>
  171.                     </tr>
  172.             <?php }
  173.         } ?>
  174.         </tbody>
  175.         </table>
  176.         <?php
  177.            
  178.         global $colector;
  179.         $this->prerow  = array( 'esto debería aparecer','si se ejecuta','en get_main_chart','y jala row con datos');  
  180.        
  181.            
  182.     }
  183.    
  184.     function generate_stock_report_csv() {
  185.         // output headers so that the file is downloaded rather than displayed
  186.         header('Content-Type: text/csv; charset=utf-8');
  187.         // set file name with current date
  188.         header('Content-Disposition: attachment; filename=reporte-panuts-' . date('Y-m-d') . '.csv');
  189.         // create a file pointer connected to the output stream
  190.         $output = fopen('php://output', 'w');
  191.         // set the column headers for the csv
  192.         $headings = array( 'Fecha', 'Mes', 'Orden', 'Nombre', 'Apellido', 'DNI', 'Dirección', 'Distrito', 'Correo', 'Teléfono', 'Producto', 'Cantidad', 'Precio', 'Subtotal', 'Envío', 'Total', 'Método de Pago' );
  193.         // output the column headings
  194.         fputcsv($output, $headings );
  195.        
  196.         global $colector;
  197.        
  198.         $row = $colector;
  199.         $row = $this->prerow;
  200.         $row = array( 'y esto debería aparecer','si accedo a mi función de botón',$order->id_orden,date('M',$strtotime));
  201.         fputcsv($output, $row);
  202.        
  203.        
  204.        
  205.         foreach( $ventas_panuts_orders as $order ) {
  206.  
  207.             $i=true;
  208.             foreach( $order_items as $product ) {
  209.            
  210.             $row = array( $date_created . ', ' . date('M',$strtotime) . ', ' . $order->id_orden . ', ' . $order_obj->get_billing_first_name() . ', ' . $order_obj->get_billing_last_name() . ', ' . get_post_meta( $order->id_orden, 'billing_dni', true ) . ', ' . $order_obj->get_billing_address_1().' '.$order_obj->get_billing_address_2() . ', ' . WC()->countries->get_states( $order_obj->get_billing_country() )[$order_obj->get_billing_state()] . ', ' . $order_obj->get_billing_email() . ', ' . get_post_meta( $order->id_orden, 'billing_cellphone', true ) . ', ' . $product['name'] . ', ' . $product['qty'] . ', ' . $product->get_total()/$product['qty'] . ', ' . $product->get_total());
  211.             fputcsv($output, $row);
  212.                 /*?>
  213.                     <tr>
  214.  
  215.                         <?php
  216.                         if ($i){ ?>
  217.                             <td><?php echo $order_obj->get_shipping_total(); ?></td>
  218.                             <td><?php echo wc_price($order->orden_total); ?></td> <!-- total de orden -->
  219.                             <td><?php echo $order_obj->get_payment_method_title(); ?></td>
  220.                             <?php $i=false; } ?>
  221.                             </tr>
  222.                            
  223.             <?php
  224.            
  225.          */
  226.             }
  227.         }
  228.    
  229.    
  230.     }
  231.    
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement