carlos__z

Untitled

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