Advertisement
palsushobhan

wcfm-regenerate-store-invoice

May 27th, 2021
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.10 KB | None | 0 0
  1. function invoice_regeration_required($order) {
  2.     if( wcfm_is_vendor() ) return false;
  3.     $current_order_status = apply_filters( 'wcfm_current_order_status', $order->get_status(), $order->get_id() );
  4.     if( in_array( $current_order_status, apply_filters( 'wcfm_pdf_invoice_download_disable_order_status', array( 'failed', 'cancelled', 'request', 'proposal', 'proposal-sent', 'proposal-expired', 'proposal-rejected', 'proposal-canceled', 'proposal-accepted' ) ) ) )
  5.         return false;
  6.     $wcfm_store_invoices = get_post_meta( $order->get_id(), '_wcfm_store_invoices', true );
  7.     if( $wcfm_store_invoices  && is_array( $wcfm_store_invoices ) && ! empty( $wcfm_store_invoices ) )
  8.         return false;
  9.     if( get_post_type( $order->get_id() ) == 'wc_booking' && isset($order->order) )
  10.         return false;
  11.     return true;
  12. }
  13. add_action( 'wp_ajax_wcfm_regenerate_store_invoice', function() {
  14.     global $WCFM, $WCFMu, $order, $order_id, $document, $document_type, $vendor_id, $process_item_ids, $process_product_ids, $process_shipping_items, $wcfm_vendor_invoice_id;
  15.     $order_id = $_POST['order_id'];
  16.     if($order_id) {
  17.         // $order_posted = get_post( $order_id );
  18.         $order = wc_get_order( $order_id );
  19.         $wcfm_store_invoices = array();
  20.         $wcfm_store_invoice_ids = array();
  21.         // Reset Site Language to Current Langugae
  22.         if ( defined( 'ICL_SITEPRESS_VERSION' ) && ! ICL_PLUGIN_INACTIVE && class_exists( 'SitePress' ) ) {
  23.             global $sitepress;
  24.             $lang = get_post_meta( $order_id, 'wpml_language', true );
  25.             if( !empty( $lang ) ) {
  26.                 $sitepress->switch_lang( $lang, true );
  27.             }
  28.         }
  29.         WPO_WCPDF()->translations();
  30.         $document_type = 'invoice';
  31.         $document = wcpdf_get_document( $document_type, (array) $order_id, true );
  32.         if( !$document ) return;
  33.        
  34.         do_action( 'wpo_wcpdf_before_pdf', $document_type, $document );
  35.        
  36.         $upload_dir = wp_upload_dir();
  37.         if (!empty($upload_dir['error'])) {
  38.             $tmp_path = false;
  39.         } else {
  40.             $upload_base = trailingslashit( $upload_dir['basedir'] );
  41.             $tmp_path = $upload_base . 'wcfm/vendor_invoice/';
  42.         }
  43.  
  44.         $tmp_path = apply_filters( 'wcfm_vendor_invoice_tmp_path', $tmp_path );
  45.         $error = '';
  46.         if ($tmp_path !== false) {
  47.             $tmp_path = trailingslashit( $tmp_path );
  48.             if ( !wp_mkdir_p( $tmp_path ) ) {
  49.                 $error = 'Fail to create upload directory';
  50.             }
  51.         } else {
  52.             $error = 'Invalid invoice path. Can\'t save';
  53.         }
  54.        
  55.         if(!$error) {
  56.             $invoice_settings = array(
  57.                 'paper_size'        => 'A4',
  58.                 'paper_orientation' => 'portrait',
  59.             );
  60.        
  61.             $cur_year  = date('Y');
  62.             $cur_month = date('m');
  63.            
  64.             $wcfm_store_invoice_items = array();
  65.             $line_items               = $order->get_items( 'line_item' );
  66.             $line_items_shipping      = $order->get_items( 'shipping' );
  67.             $processed_item_vendor    = array();
  68.        
  69.             if( !empty( $line_items ) ) {
  70.                 foreach ( $line_items as $item_id => $item ) {
  71.                     // $product  = $item->get_product();
  72.                     $product_id = $item->get_product_id();
  73.                     $vendor_id = wcfm_get_vendor_id_by_post( $product_id );
  74.                     if( !wcfm_is_vendor( $vendor_id ) ) continue;
  75.                     if( in_array( $vendor_id, $processed_item_vendor ) ) continue;
  76.                    
  77.                     if( $vendor_id && $WCFM->wcfm_vendor_support->wcfm_vendor_has_capability( $vendor_id, 'store_invoice' ) ) {
  78.                         if( !empty( $wcfm_store_invoices ) && isset( $wcfm_store_invoices[$vendor_id] ) && !empty( $wcfm_store_invoices[$vendor_id] ) && !is_array( $wcfm_store_invoices[$vendor_id] ) ) {
  79.                             $pdf_path = $wcfm_store_invoices[$vendor_id];
  80.                             if( file_exists( $pdf_path ) ) {
  81.                                 $attachments[] = $pdf_path;
  82.                                 $processed_item_vendor[$vendor_id] = $vendor_id;
  83.                             } else {
  84.                                 $wcfm_store_invoice_items[$vendor_id]['items'][] = $item_id;
  85.                                 $wcfm_store_invoice_items[$vendor_id]['products'][] = $product_id;
  86.                             }
  87.                         } else {
  88.                             $wcfm_store_invoice_items[$vendor_id]['items'][] = $item_id;
  89.                             $wcfm_store_invoice_items[$vendor_id]['products'][] = $product_id;
  90.                         }
  91.                     }
  92.                 }
  93.             }
  94.            
  95.        
  96.             if( !empty( $wcfm_store_invoice_items ) ) {
  97.                 foreach( $wcfm_store_invoice_items as $wcfm_store_invoice_item_key => $wcfm_store_invoice_item ) {
  98.                     $vendor_id             = $wcfm_store_invoice_item_key;
  99.                     $process_item_ids      = $wcfm_store_invoice_item['items'];
  100.                     $process_product_ids   = $wcfm_store_invoice_item['products'];
  101.                     $process_shipping_items = array();
  102.                    
  103.                     if( !empty( $line_items_shipping ) ) {
  104.                         foreach ( $line_items_shipping as $shipping_item_id => $shipping_item) {
  105.                             $order_item_shipping = new WC_Order_Item_Shipping($shipping_item_id);
  106.                             $shipping_vendor_id = $order_item_shipping->get_meta('vendor_id', true);
  107.                             if( $shipping_vendor_id && ( $shipping_vendor_id == $vendor_id ) ) {
  108.                                 $process_shipping_items[$shipping_item_id] = $shipping_item;
  109.                             }
  110.                         }
  111.                     }
  112.                    
  113.                     if( !$process_item_ids || !is_array( $process_item_ids ) || empty( $process_item_ids ) ) continue;
  114.                     if( !$process_product_ids || !is_array( $process_product_ids ) || empty( $process_product_ids ) ) continue;
  115.                    
  116.                     $store_invoice_path = $tmp_path . md5($vendor_id) . '/' . $cur_year . '/' . $cur_month . '/' . md5($order_id);
  117.                     if ( !wp_mkdir_p( $store_invoice_path ) ) {
  118.                         continue;
  119.                     } else {
  120.                         $store_invoice_path = trailingslashit( $store_invoice_path );
  121.                     }
  122.                    
  123.                     $wcfm_vendor_invoice_data = (array) wcfm_get_user_meta( $vendor_id, 'wcfm_vendor_invoice_options', true );
  124.                     $wcfm_vendor_last_invoice_no = get_user_meta( $vendor_id, '_wcfm_vendor_last_invoice_no', true );
  125.                     if( $wcfm_vendor_last_invoice_no ) $wcfm_vendor_last_invoice_no = absint( $wcfm_vendor_last_invoice_no );
  126.                     else $wcfm_vendor_last_invoice_no = 0;
  127.                     $wcfm_vendor_last_invoice_no++;
  128.                     $wcfm_vendor_invoice_prefix = isset( $wcfm_vendor_invoice_data['prefix'] ) ? $wcfm_vendor_invoice_data['prefix'] : '';
  129.                     $wcfm_vendor_invoice_sufix = isset( $wcfm_vendor_invoice_data['sufix'] ) ? $wcfm_vendor_invoice_data['sufix'] : '';
  130.                     $wcfm_vendor_invoice_digit = isset( $wcfm_vendor_invoice_data['digit'] ) ? absint($wcfm_vendor_invoice_data['digit']) : '';
  131.                     if( $wcfm_vendor_invoice_digit ) {
  132.                         $wcfm_vendor_invoice_id = sprintf( '%0'.$wcfm_vendor_invoice_digit.'u', $wcfm_vendor_last_invoice_no );
  133.                     } else {
  134.                         $wcfm_vendor_invoice_id = sprintf( '%06u', $wcfm_vendor_last_invoice_no );
  135.                     }
  136.                     $wcfm_vendor_invoice_id = $wcfm_vendor_invoice_prefix . $wcfm_vendor_invoice_id . $wcfm_vendor_invoice_sufix;
  137.                    
  138.                     $filename = __( 'invoice', 'wc-frontend-manager-ultimate' ) . '-' . $wcfm_vendor_invoice_id . '.pdf';
  139.                        
  140.                     try {
  141.                         // Fetching Main template
  142.                         if( is_rtl() ) {
  143.                             $template = $WCFMu->template->locate_template( 'vendor_invoice/store-invoice-rtl.php' );
  144.                         } else {
  145.                             $template = $WCFMu->template->locate_template( 'vendor_invoice/store-invoice.php' );
  146.                         }
  147.                         ob_start();
  148.                         if (file_exists($template)) {
  149.                             include($template);
  150.                         }
  151.                         $output_body = ob_get_clean();
  152.                
  153.                         // Fetching tempplate wrapper
  154.                         $template_wrapper = $WCFMu->template->locate_template( 'vendor_invoice/html-document-wrapper.php' );
  155.                         ob_start();
  156.                         if (file_exists($template_wrapper)) {
  157.                             include($template_wrapper);
  158.                         }
  159.                         $complete_document = ob_get_clean();
  160.                        
  161.                         // Try to clean up a bit of memory
  162.                         unset($output_body);
  163.                        
  164.                         // clean up special characters
  165.                         $complete_document = utf8_decode(mb_convert_encoding($complete_document, 'HTML-ENTITIES', 'UTF-8'));
  166.                        
  167.                         $pdf_maker = wcpdf_get_pdf_maker( $complete_document, $invoice_settings );
  168.                         $pdf_data = $pdf_maker->output();
  169.                        
  170.                         do_action( 'wpo_wcpdf_after_pdf', $document_type, $document );
  171.                        
  172.                         $pdf_path = $store_invoice_path . $filename;
  173.                         file_put_contents ( $pdf_path, $pdf_data );
  174.                         $attachments[] = $pdf_path;
  175.                         $wcfm_store_invoices[$vendor_id] = esc_sql($pdf_path);
  176.                         $wcfm_store_invoice_ids[$vendor_id] = $wcfm_vendor_invoice_id;
  177.                         update_user_meta( $vendor_id, '_wcfm_vendor_last_invoice_no', $wcfm_vendor_last_invoice_no );
  178.                        
  179.                         // Try to clean up a bit of memory
  180.                         unset($complete_document);
  181.                     } catch (Exception $e) {
  182.                         error_log($e->getMessage());
  183.                         continue;
  184.                     }
  185.                 }
  186.                 update_post_meta( $order_id, '_wcfm_store_invoices', $wcfm_store_invoices );
  187.                 update_post_meta( $order_id, '_wcfm_store_invoice_ids', $wcfm_store_invoice_ids );
  188.             }
  189.         }
  190.     }
  191.     die;
  192. });
  193. add_action( 'wcfm_order_details_after_order_table', function( $order ) {
  194.     if( invoice_regeration_required($order) ) {
  195.         echo '<a id="wcfm-order-store-invoice-' . $order->get_id() . '" data-order="' . $order->get_id() . '" href="#" class="wcfm_regenerate_store_invoice add_new_wcfm_ele_dashboard text_tip" style="float:left!important;color:#ffffff!important;margin-right:10px;" data-tip="' . __('Click to regenarate store invoice') . '"><span class="">' . __( 'Regenerate Store Invoice', 'wc-frontend-manager-ultimate') . '</span></a>';
  196.         echo '<div class="wcfm_clearfix"></div><br />';
  197.     ?>
  198.     <script>
  199.     jQuery(document).ready(function($) {
  200.         $('.wcfm_regenerate_store_invoice').click(function( event ) {
  201.             event.preventDefault();
  202.             var rconfirm = confirm( "Do you really want to do this?" );
  203.             if( rconfirm ) {
  204.                 var data = {
  205.                     action : 'wcfm_regenerate_store_invoice',
  206.                     order_id : $('.wcfm_regenerate_store_invoice').data('order')
  207.                 }
  208.                 $.ajax({
  209.                     type: 'POST',
  210.                     url: wcfm_params.ajax_url,
  211.                     data: data,
  212.                     success: function(response) {
  213.                         console.log(response);
  214.                        window.location = window.location.href;
  215.                     }
  216.                 });
  217.             }
  218.             return false;
  219.         });
  220.     });
  221.     </script>
  222.     <?php
  223.     }
  224. }, 501 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement