carlos__z

Untitled

Aug 20th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.54 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() { /*falta optimizar o cambiar por link */
  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.        
  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.     public function output_report() {
  51.         $ranges = array(
  52.             'year'         => __( 'Year', 'woocommerce' ),
  53.             'last_month'   => __( 'Last month', 'woocommerce' ),
  54.             'month'        => __( 'This month', 'woocommerce' ),
  55.         );
  56.         $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : 'month';
  57.         if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', '7day' ) ) ) {
  58.             $current_range = 'month';
  59.         }
  60.         $this->check_current_range_nonce( $current_range );
  61.         $this->calculate_current_range( $current_range );
  62.         $hide_sidebar = true;
  63.         include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php' );
  64.     }
  65.  
  66.      /**
  67.      * Output an export link.
  68.      */
  69.     public function get_export_button() {
  70.         $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day';
  71.         boton_exportar();
  72.     }
  73.    
  74.     /**
  75.     * Get the main chart.
  76.     */
  77.    
  78.     public function get_main_chart() { 
  79.         global $wpdb;
  80.         $query_data = array(
  81.             'ID' => array(
  82.                 'type'     => 'post_data',
  83.                 'function' => '',
  84.                 'name'     => 'id_orden',
  85.                 'distinct' => true,
  86.             ),
  87.             '_order_total'   => array(
  88.                 'type'      => 'meta',
  89.                 'function'  => '',
  90.                 'name'      => 'orden_total'
  91.             ),
  92.         );
  93.    
  94.         $ventas_panuts_orders = $this->get_order_report_data( array(
  95.             'data'                  => $query_data,
  96.             'query_type'            => 'get_results',
  97.             //'group_by'              => 'country',
  98.             'filter_range'          => true,
  99.             'order_types'           => wc_get_order_types( 'sales-reports' ),
  100.             'order_status'          => array( 'completed' ),
  101.             'parent_order_status'   => false,
  102.         ) );
  103.     ?>
  104.         <table class="widefat">
  105.             <thead>
  106.                 <tr>
  107.                     <th><strong>Fecha</strong></th>
  108.                     <th><strong>Mes</strong></th>
  109.                     <th><strong>Orden</strong></th>
  110.                     <th><strong>Nombre</strong></th>
  111.                     <th><strong>Apellido</strong></th>
  112.                     <th><strong>DNI</strong></th>
  113.                     <th><strong>Dirección</strong></th>
  114.                     <th><strong>Distrito</strong></th>
  115.                     <th><strong>Correo</strong></th>
  116.                     <th><strong>Teléfono</strong></th>
  117.                     <th><strong>Producto</strong></th>
  118.                     <th><strong>Cantidad</strong></th>
  119.                     <th><strong>Precio Unitario</strong></th>
  120.                     <th><strong>Subtotal</strong></th>
  121.                     <th><strong>Costo de Envío</strong></th>
  122.                     <th><strong>Total</strong></th>
  123.                     <th><strong>Método de Pago</strong></th>
  124.                 </tr>
  125.             </thead>
  126.         <tbody>
  127.         <?php foreach( $ventas_panuts_orders as $order ) {
  128.            
  129.             $order_obj = wc_get_order( $order->id_orden );
  130.             $order_data = $order_obj->get_data();
  131.             $date_created  = $order_obj->order_date;
  132.             $strtotime = strtotime($date_created);
  133.  
  134.             $order_items = $order_obj->get_items();
  135.             $i=true;
  136.             foreach( $order_items as $product ) {
  137.                 ?>
  138.                 <tr>
  139.                 <td><?php echo $date_created; ?></td>
  140.                 <td><?php echo date('M',$strtotime); ?></td>
  141.                 <td><?php echo $order->id_orden; ?></td>
  142.                 <td style="text-transform: capitalize;"><?php echo $order_obj->get_billing_first_name(); ?></td>
  143.                 <td style="text-transform: capitalize;"><?php echo $order_obj->get_billing_last_name(); ?></td>
  144.                 <td><?php echo get_post_meta( $order->id_orden, 'billing_dni', true ); ?></td>
  145.                 <td style="text-transform: capitalize;"><?php echo $order_obj->get_billing_address_1().' '.$order_obj->get_billing_address_2();?></td>
  146.                 <td><?php echo WC()->countries->get_states( $order_obj->get_billing_country() )[$order_obj->get_billing_state()]; ?></td>
  147.                 <td><?php echo $order_obj->get_billing_email(); ?></td>
  148.                 <td><?php echo get_post_meta( $order->id_orden, 'billing_cellphone', true ); ?></td>
  149.                 <td><?php echo '<p>'.$product['name'].'</p>'; ?></td>
  150.                 <td align="center"><?php echo '<p>'.$product['qty'].'</p>'; ?></td>
  151.                 <td align="center"><?php echo '<p>'.$product->get_total()/$product['qty'].'</p>'; ?></td>  
  152.                 <td align="center"><?php echo $product->get_total(); ?></td>
  153.                 <?php
  154.                 if ($i){ ?>
  155.                     <td><?php echo $order_obj->get_shipping_total(); ?></td>
  156.                     <td><?php echo wc_price($order->orden_total); ?></td> <!-- total de orden -->
  157.                     <td><?php echo $order_obj->get_payment_method_title(); ?></td>
  158.                     <?php $i=false; } ?>
  159.                     </tr>
  160.             <?php }
  161.         } ?>
  162.         </tbody>
  163.         </table>
  164.         <?php
  165.     } /* HASTA ACA TODO FUNCIONA OK, ME MUESTRA EL REPORTE CORRECTO EN PANTALLA */
  166.    
  167.     public function generate_stock_report_csv() {
  168.         /*ESTA ES LA FUNCION QUE QUISIERA "ANIDAR" EN LA ANTERIOR get_main_chart */
  169.         // output headers so that the file is downloaded rather than displayed
  170.         header('Content-Type: text/csv; charset=utf-8');
  171.         // set file name with current date
  172.         header('Content-Disposition: attachment; filename=reporte-panuts-' . date('Y-m-d') . '.csv');
  173.         // create a file pointer connected to the output stream
  174.         $output = fopen('php://output', 'w');
  175.         // set the column headers for the csv
  176.         $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' );
  177.         // output the column headings
  178.         fputcsv($output, $headings );
  179.  
  180.         global $wpdb;
  181.         $query_data = array(
  182.             'ID' => array(
  183.                 'type'     => 'post_data',
  184.                 'function' => '',
  185.                 'name'     => 'id_orden',
  186.                 'distinct' => true,
  187.             ),
  188.             '_order_total'   => array(
  189.                 'type'      => 'meta',
  190.                 'function'  => '',
  191.                 'name'      => 'orden_total'
  192.             ),
  193.         );
  194.         $ventas_panuts_orders = $this->get_order_report_data( array(
  195.             'data'                  => $query_data,
  196.             'query_type'            => 'get_results',
  197.             //'group_by'              => 'country',
  198.             'filter_range'          => true,
  199.             'order_types'           => wc_get_order_types( 'sales-reports' ),
  200.             'order_status'          => array( 'completed' ),
  201.             'parent_order_status'   => false,
  202.         ) );
  203.  
  204.         $row = array( 'esto debería aparecer','si accedo a mi función');
  205.         fputcsv($output, $row);
  206.  
  207.         foreach( $ventas_panuts_orders as $order ) {
  208.  
  209.             $order_obj = wc_get_order( $order->id_orden );
  210.             $order_data = $order_obj->get_data();
  211.             $date_created  = $order_obj->order_date;
  212.             $strtotime = strtotime($date_created);
  213.  
  214.             $order_items = $order_obj->get_items();
  215.             $i=true;
  216.             foreach( $order_items as $product ) {
  217.             /*
  218.             Y ESTO ES LO QUE REALMENTE QUIERO QUE SE GRABE EN CADA ROW (aún en proceso, me falta agregar el if($i)
  219.  
  220.             $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());
  221.             fputcsv($output, $row);
  222.  
  223.  
  224.             */
  225.             }
  226.         }
  227.     }  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment