Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function invoice_regeration_required($order) {
- if( wcfm_is_vendor() ) return false;
- $current_order_status = apply_filters( 'wcfm_current_order_status', $order->get_status(), $order->get_id() );
- 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' ) ) ) )
- return false;
- $wcfm_store_invoices = get_post_meta( $order->get_id(), '_wcfm_store_invoices', true );
- if( $wcfm_store_invoices && is_array( $wcfm_store_invoices ) && ! empty( $wcfm_store_invoices ) )
- return false;
- if( get_post_type( $order->get_id() ) == 'wc_booking' && isset($order->order) )
- return false;
- return true;
- }
- add_action( 'wp_ajax_wcfm_regenerate_store_invoice', function() {
- global $WCFM, $WCFMu, $order, $order_id, $document, $document_type, $vendor_id, $process_item_ids, $process_product_ids, $process_shipping_items, $wcfm_vendor_invoice_id;
- $order_id = $_POST['order_id'];
- if($order_id) {
- // $order_posted = get_post( $order_id );
- $order = wc_get_order( $order_id );
- $wcfm_store_invoices = array();
- $wcfm_store_invoice_ids = array();
- // Reset Site Language to Current Langugae
- if ( defined( 'ICL_SITEPRESS_VERSION' ) && ! ICL_PLUGIN_INACTIVE && class_exists( 'SitePress' ) ) {
- global $sitepress;
- $lang = get_post_meta( $order_id, 'wpml_language', true );
- if( !empty( $lang ) ) {
- $sitepress->switch_lang( $lang, true );
- }
- }
- WPO_WCPDF()->translations();
- $document_type = 'invoice';
- $document = wcpdf_get_document( $document_type, (array) $order_id, true );
- if( !$document ) return;
- do_action( 'wpo_wcpdf_before_pdf', $document_type, $document );
- $upload_dir = wp_upload_dir();
- if (!empty($upload_dir['error'])) {
- $tmp_path = false;
- } else {
- $upload_base = trailingslashit( $upload_dir['basedir'] );
- $tmp_path = $upload_base . 'wcfm/vendor_invoice/';
- }
- $tmp_path = apply_filters( 'wcfm_vendor_invoice_tmp_path', $tmp_path );
- $error = '';
- if ($tmp_path !== false) {
- $tmp_path = trailingslashit( $tmp_path );
- if ( !wp_mkdir_p( $tmp_path ) ) {
- $error = 'Fail to create upload directory';
- }
- } else {
- $error = 'Invalid invoice path. Can\'t save';
- }
- if(!$error) {
- $invoice_settings = array(
- 'paper_size' => 'A4',
- 'paper_orientation' => 'portrait',
- );
- $cur_year = date('Y');
- $cur_month = date('m');
- $wcfm_store_invoice_items = array();
- $line_items = $order->get_items( 'line_item' );
- $line_items_shipping = $order->get_items( 'shipping' );
- $processed_item_vendor = array();
- if( !empty( $line_items ) ) {
- foreach ( $line_items as $item_id => $item ) {
- // $product = $item->get_product();
- $product_id = $item->get_product_id();
- $vendor_id = wcfm_get_vendor_id_by_post( $product_id );
- if( !wcfm_is_vendor( $vendor_id ) ) continue;
- if( in_array( $vendor_id, $processed_item_vendor ) ) continue;
- if( $vendor_id && $WCFM->wcfm_vendor_support->wcfm_vendor_has_capability( $vendor_id, 'store_invoice' ) ) {
- if( !empty( $wcfm_store_invoices ) && isset( $wcfm_store_invoices[$vendor_id] ) && !empty( $wcfm_store_invoices[$vendor_id] ) && !is_array( $wcfm_store_invoices[$vendor_id] ) ) {
- $pdf_path = $wcfm_store_invoices[$vendor_id];
- if( file_exists( $pdf_path ) ) {
- $attachments[] = $pdf_path;
- $processed_item_vendor[$vendor_id] = $vendor_id;
- } else {
- $wcfm_store_invoice_items[$vendor_id]['items'][] = $item_id;
- $wcfm_store_invoice_items[$vendor_id]['products'][] = $product_id;
- }
- } else {
- $wcfm_store_invoice_items[$vendor_id]['items'][] = $item_id;
- $wcfm_store_invoice_items[$vendor_id]['products'][] = $product_id;
- }
- }
- }
- }
- if( !empty( $wcfm_store_invoice_items ) ) {
- foreach( $wcfm_store_invoice_items as $wcfm_store_invoice_item_key => $wcfm_store_invoice_item ) {
- $vendor_id = $wcfm_store_invoice_item_key;
- $process_item_ids = $wcfm_store_invoice_item['items'];
- $process_product_ids = $wcfm_store_invoice_item['products'];
- $process_shipping_items = array();
- if( !empty( $line_items_shipping ) ) {
- foreach ( $line_items_shipping as $shipping_item_id => $shipping_item) {
- $order_item_shipping = new WC_Order_Item_Shipping($shipping_item_id);
- $shipping_vendor_id = $order_item_shipping->get_meta('vendor_id', true);
- if( $shipping_vendor_id && ( $shipping_vendor_id == $vendor_id ) ) {
- $process_shipping_items[$shipping_item_id] = $shipping_item;
- }
- }
- }
- if( !$process_item_ids || !is_array( $process_item_ids ) || empty( $process_item_ids ) ) continue;
- if( !$process_product_ids || !is_array( $process_product_ids ) || empty( $process_product_ids ) ) continue;
- $store_invoice_path = $tmp_path . md5($vendor_id) . '/' . $cur_year . '/' . $cur_month . '/' . md5($order_id);
- if ( !wp_mkdir_p( $store_invoice_path ) ) {
- continue;
- } else {
- $store_invoice_path = trailingslashit( $store_invoice_path );
- }
- $wcfm_vendor_invoice_data = (array) wcfm_get_user_meta( $vendor_id, 'wcfm_vendor_invoice_options', true );
- $wcfm_vendor_last_invoice_no = get_user_meta( $vendor_id, '_wcfm_vendor_last_invoice_no', true );
- if( $wcfm_vendor_last_invoice_no ) $wcfm_vendor_last_invoice_no = absint( $wcfm_vendor_last_invoice_no );
- else $wcfm_vendor_last_invoice_no = 0;
- $wcfm_vendor_last_invoice_no++;
- $wcfm_vendor_invoice_prefix = isset( $wcfm_vendor_invoice_data['prefix'] ) ? $wcfm_vendor_invoice_data['prefix'] : '';
- $wcfm_vendor_invoice_sufix = isset( $wcfm_vendor_invoice_data['sufix'] ) ? $wcfm_vendor_invoice_data['sufix'] : '';
- $wcfm_vendor_invoice_digit = isset( $wcfm_vendor_invoice_data['digit'] ) ? absint($wcfm_vendor_invoice_data['digit']) : '';
- if( $wcfm_vendor_invoice_digit ) {
- $wcfm_vendor_invoice_id = sprintf( '%0'.$wcfm_vendor_invoice_digit.'u', $wcfm_vendor_last_invoice_no );
- } else {
- $wcfm_vendor_invoice_id = sprintf( '%06u', $wcfm_vendor_last_invoice_no );
- }
- $wcfm_vendor_invoice_id = $wcfm_vendor_invoice_prefix . $wcfm_vendor_invoice_id . $wcfm_vendor_invoice_sufix;
- $filename = __( 'invoice', 'wc-frontend-manager-ultimate' ) . '-' . $wcfm_vendor_invoice_id . '.pdf';
- try {
- // Fetching Main template
- if( is_rtl() ) {
- $template = $WCFMu->template->locate_template( 'vendor_invoice/store-invoice-rtl.php' );
- } else {
- $template = $WCFMu->template->locate_template( 'vendor_invoice/store-invoice.php' );
- }
- ob_start();
- if (file_exists($template)) {
- include($template);
- }
- $output_body = ob_get_clean();
- // Fetching tempplate wrapper
- $template_wrapper = $WCFMu->template->locate_template( 'vendor_invoice/html-document-wrapper.php' );
- ob_start();
- if (file_exists($template_wrapper)) {
- include($template_wrapper);
- }
- $complete_document = ob_get_clean();
- // Try to clean up a bit of memory
- unset($output_body);
- // clean up special characters
- $complete_document = utf8_decode(mb_convert_encoding($complete_document, 'HTML-ENTITIES', 'UTF-8'));
- $pdf_maker = wcpdf_get_pdf_maker( $complete_document, $invoice_settings );
- $pdf_data = $pdf_maker->output();
- do_action( 'wpo_wcpdf_after_pdf', $document_type, $document );
- $pdf_path = $store_invoice_path . $filename;
- file_put_contents ( $pdf_path, $pdf_data );
- $attachments[] = $pdf_path;
- $wcfm_store_invoices[$vendor_id] = esc_sql($pdf_path);
- $wcfm_store_invoice_ids[$vendor_id] = $wcfm_vendor_invoice_id;
- update_user_meta( $vendor_id, '_wcfm_vendor_last_invoice_no', $wcfm_vendor_last_invoice_no );
- // Try to clean up a bit of memory
- unset($complete_document);
- } catch (Exception $e) {
- error_log($e->getMessage());
- continue;
- }
- }
- update_post_meta( $order_id, '_wcfm_store_invoices', $wcfm_store_invoices );
- update_post_meta( $order_id, '_wcfm_store_invoice_ids', $wcfm_store_invoice_ids );
- }
- }
- }
- die;
- });
- add_action( 'wcfm_order_details_after_order_table', function( $order ) {
- if( invoice_regeration_required($order) ) {
- 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>';
- echo '<div class="wcfm_clearfix"></div><br />';
- ?>
- <script>
- jQuery(document).ready(function($) {
- $('.wcfm_regenerate_store_invoice').click(function( event ) {
- event.preventDefault();
- var rconfirm = confirm( "Do you really want to do this?" );
- if( rconfirm ) {
- var data = {
- action : 'wcfm_regenerate_store_invoice',
- order_id : $('.wcfm_regenerate_store_invoice').data('order')
- }
- $.ajax({
- type: 'POST',
- url: wcfm_params.ajax_url,
- data: data,
- success: function(response) {
- console.log(response);
- window.location = window.location.href;
- }
- });
- }
- return false;
- });
- });
- </script>
- <?php
- }
- }, 501 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement