Plugin Directory

Changeset 3105242

Timestamp:
06/20/2024 03:32:48 PM (3 weeks ago)
Author:
bastho
Message:

Format indent

File:
1 edited

Legend:

Unmodified
Added
Removed
  • accounting-for-woocommerce/trunk/inc/export-refunds.php

    r3105234 r3105242  
    1010
    1111function woocommerce_accounting_export_refunds_data() {
    12     global $woocommerce;
    13     global $post;
    14     global $small_number;
     12global $woocommerce;
     13global $post;
     14global $small_number;
    1515    global $rounding_precision;
    1616
    17     //test nonce
    18     if (! isset( $_POST['_check_refunds_export'] ) || ! wp_verify_nonce( $_POST['_check_refunds_export'], 'check_nonce_refunds_export')){
    19         print 'Sorry, your nonce did not verify.';
    20         exit;
    21     }
    22 
    23     $separator = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_separator']);
    24     $dec_separator = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_decimal_separator']);
    25     $ts1 = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_start_date']);
    26     $ts2 = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_end_date']);
    27     $date_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_date_opt']);
    28     $date_format_opt = sanitize_text_field($_POST['woocommerce_accounting_export_refunds_date_format']);
    29     $cust_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_cust_opt']);
    30     $pay_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_pay_opt']);
    31     $prod_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_prod_opt']);
    32     $taxes_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_taxes_opt']);
    33     $ship_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_ship_opt']);
    34     $factnum_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_factnum_opt']);
    35     $zero_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_zero_opt']);
    36 
    37     update_option( 'woocommerce_accounting_refunds_export_start_date',$ts1);
    38     update_option( 'woocommerce_accounting_refunds_export_end_date',$ts2);
    39     update_option( 'woocommerce_accounting_export_refunds_date_format',$date_format_opt);
    40     update_option( 'woocommerce_accounting_refunds_export_separator', $separator);
    41     update_option( 'woocommerce_accounting_refunds_export_date_opt',$date_opt);
    42     update_option( 'woocommerce_accounting_refunds_export_pay_opt',$pay_opt);
    43     update_option( 'woocommerce_accounting_refunds_export_prod_opt',$prod_opt);
    44     update_option( 'woocommerce_accounting_refunds_export_taxes_opt',$taxes_opt);
    45     update_option( 'woocommerce_accounting_refunds_export_ship_opt',$ship_opt);
    46     update_option( 'woocommerce_accounting_refunds_export_factnum_opt',$factnum_opt);
    47     update_option( 'woocommerce_accounting_refunds_export_cust_opt',$cust_opt);
    48     update_option( 'woocommerce_accounting_refunds_export_decimal_separator',$dec_separator);
    49     update_option( 'woocommerce_accounting_refunds_export_zero_opt',$zero_opt);
    50 
    51     // Use WC Order Query style
    52     // https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query#general
    53 
    54     $date_options = array(
    55         '0' => 'date_created',
    56         '1' => 'date_completed',
    57         '3' => 'date_paid',
    58     );
    59 
    60     $order_args = array(
    61         'limit' => -1,
    62         'type' => 'shop_order_refund',
    63         'status' => array( 'wc-completed' ),
    64         'orderby' => 'ID',
    65         'order' => 'ASC',
    66     );
    67 
    68     if(isset($date_options[$date_opt])) {
    69         $order_args[$date_options[$date_opt]] = "{$ts1}...{$ts2}";
    70     }
    71 
    72     $order_args = apply_filters('woocommerce_accounting:export:get_orders_refunds_args', $order_args, $date_opt, array( 'wc-completed' ), array($ts1, $ts2));
    73     $orders = wc_get_orders($order_args);
    74     do_action('woocommerce_accounting:export:launch', $orders);
    75    
    76     $compiledOrders = array();
    77 
    78     //Datas to export
    79     if (!empty($orders)) {
    80         //Generic datas
    81         $book_code = get_option('woocommerce_accounting_book_code_order');
    82         $gen_account_cust = get_option('woocommerce_accounting_generic_cust_accounting_account');
    83         $gen_account_prod = get_option ('woocommerce_accounting_generic_prod_accounting_account');
    84         $gen_account_fdp = get_option ('woocommerce_accounting_generic_fdp_accounting_account');
    85         $gen_account_tax = get_option ('woocommerce_accounting_generic_tax_accounting_account');
    86         $gen_analytic_prod = get_option ('woocommerce_accounting_generic_prod_analytic_account');
    87         $gen_analytic_fdp = get_option('woocommerce_accounting_generic_fdp_analytic_account');
    88         $lib_prefix = get_option ('woocommerce_accounting_lib_prefix');
    89 
    90         $rounding_precision = 2;
    91 
    92 
    93         foreach ($orders as $wcOrder)
    94         {
    95             // skip order if it is a regular order
    96             if($wcOrder instanceof \WC_Order){
    97                 continue;
    98             }
    99             $order = (object) array();
    100             $order->WCOrder = $wcOrder;
    101             $order->ID = $wcOrder->get_id();
    102             $order->status = 'wc-'.$wcOrder->get_status();
    103             $order->date_created = $wcOrder->get_date_created();
    104             $order->date_completed = $wcOrder->get_date_completed();
    105             $order->date_paid = $wcOrder->get_date_paid();
    106 
    107             $order->total = $wcOrder->get_total();
    108             $order->total_tax = (float) $wcOrder->get_total_tax();
    109             $order->shipping_tax = (float) $wcOrder->get_shipping_tax();
    110            
    111             //searching for parent order
    112             $order->ancestor_order = $wcOrder->get_parent_id();
    113             $order->AnsOrder = new \WC_Order($order->ancestor_order);
    114            
    115             $order->custid = $order->AnsOrder->get_customer_id();
    116             $order->gatewayid = $order->AnsOrder->get_payment_method();
    117 
    118             if ($date_opt == "0") {
    119                 $order->piecedate =  get_post_field( 'post_date', $order->ID );
    120             } else {
    121                 $invoice_date = get_post_meta($order->ID,'_wcpdf_credit_note_date',true);
    122                 if (!empty($invoice_date)) {
    123                     $order->piecedate = $invoice_date;
    124                 } else {
    125                     $order->piecedate = get_post_field( 'post_date', $order->ID );
    126                 }
    127             }
    128             //Date Format
    129             $order_year = date_i18n( 'Y', strtotime( $order->piecedate ) );
    130             $order_month = date_i18n( 'm', strtotime( $order->piecedate ) );
    131             $order_day = date_i18n ( 'd', strtotime( $order->piecedate ) ) ;
    132             if ($date_format_opt == "1") {
    133                 $order->piecedate = $order_day . '-' . $order_month . '-' . $order_year;
    134             } elseif ($date_format_opt == "2") {
    135                 $order->piecedate = $order_month . '-' . $order_day . '-' . $order_year;
    136             } elseif ($date_format_opt == "3") {
    137                 $order->piecedate = $order_year . '-' . $order_month . '-' . $order_day;
    138             } elseif ($date_format_opt == "4") {
    139                 $order->piecedate = $order_day . '/' . $order_month . '/' . $order_year;
    140             } elseif ($date_format_opt == "5") {
    141                 $order->piecedate = $order_month . '/' . $order_day . '/' . $order_year;
    142             } elseif ($date_format_opt == "6") {
    143                 $order->piecedate = $order_year . '/' . $order_month . '/' . $order_day;
    144             } else {
    145                 $order->piecedate = $order->piecedate;
    146             }
    147 
    148             // Test if plugin is activated
    149             if ( in_array( 'woocommerce-pdf-ips-pro/woocommerce-pdf-ips-pro.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    150                 $pro_settings = get_option( 'wpo_wcpdf_pro_settings' );
    151                 $number = get_post_meta($order->ID,'_wcpdf_credit_note_number',true);
    152                 $invoice_date = get_post_meta($order->ID,'_wcpdf_credit_note_date',true);
    153                
    154                 //Is invoice number defined ?
    155                 if ( ( !empty ($number) ) && $factnum_opt == 0 ){
    156                     //Use credit note number if is defined
    157                     $order_year = date_i18n( 'Y', strtotime( $order->piecedate ) );
    158                     $order_month = date_i18n( 'm', strtotime( $order->piecedate ) );
    159                     $order_day = date_i18n( 'd', strtotime( $order->piecedate ) );
    160                     $invoice_year = date_i18n( 'Y', strtotime( $invoice_date ) );
    161                     $invoice_month = date_i18n( 'm', strtotime( $invoice_date ) );
    162                     $invoice_day = date_i18n( 'd', strtotime( $invoice_date ) );
    163                     //Creating credit note numbers
    164                     $formats['prefix'] = $pro_settings['credit_note_number_formatting_prefix'];
    165                     $formats['suffix'] = $pro_settings['credit_note_number_formatting_suffix'];
    166                     $formats['padding'] = $pro_settings['credit_note_number_formatting_padding'];
    167 
    168                     // Replacements
    169                     foreach ($formats as $key => $value) {
    170                         $value = str_replace('[order_year]', $order_year, $value);
    171                         $value = str_replace('[order_month]', $order_month, $value);
    172                         $value = str_replace('[order_day]', $order_day, $value);
    173                         $value = str_replace('[invoice_year]', $invoice_year, $value);
    174                         $value = str_replace('[invoice_month]', $invoice_month, $value);
    175                         $value = str_replace('[invoice_day]', $invoice_day, $value);
    176                         $formats[$key] = $value;
    177                     }
    178                     // Padding
    179                     if ( ctype_digit( (string)$formats['padding'] ) ) {
    180                         $number = sprintf('%0'.$formats['padding'].'d', $number);
    181                     }
    182                     $formatted_refund_number = $formats['prefix'] . $number . $formats['suffix'] ;
    183                     $order->number = $formatted_refund_number;
    184                 }
    185                 else {
    186                     $order->number = $order->ancestor_order;
    187                 }
    188                 //Create the number if not created
    189             }
    190             else {
    191                 $order->number = $order->ancestor_order;
    192             }
    193             //Customer account type (customer, country or gateway)
    194             if ($cust_opt =="0") {
    195                 $order->account_cust = get_user_meta($order->custid,'woocommerce_accounting_user_accountingcode',true);
    196             }
    197             else if ($cust_opt =="1"){
    198                 $order_coutry_acc = get_option('woocommerce_accounting_gateways_libs');
    199                 $order->account_cust = $order_coutry_acc[$order->gatewayid]['acc'];
    200             }
    201             // Get the country only in label if prod by country
    202             if ($prod_opt == "2") {
    203                 $country_label = $order->AnsOrder->get_shipping_country();
    204             }
    205             else {
    206                 $country_label = "";
    207             }
    208             //activating payment gateway in lib
    209             if ($pay_opt == "1") {
    210                 $pay_meth = substr($order->AnsOrder->get_payment_method(), 0, 1) . substr($order->AnsOrder->get_payment_method(), 3, 1) . substr($order->AnsOrder->get_payment_method(), -1) ;
    211                 $order->lib = remove_accents (strtoupper($pay_meth) . ' ' .  $lib_prefix . ' ' . strtoupper($order->AnsOrder->get_billing_company()) . ' ' . ucfirst($order->AnsOrder->get_billing_last_name()) . ' ' . ucfirst($order->AnsOrder->get_billing_first_name()) . ' ' . $country_label);
    212                 $order->lib = trim($order->lib);
    213             }
    214             elseif ($pay_opt == "2") {
    215                 $order_pay_meth = $order->AnsOrder->get_payment_method();
    216                 $listed_pay_lib = get_option('woocommerce_accounting_gateways_libs')[$order_pay_meth]['lib'];
    217                 if (!empty ($listed_pay_lib)) {
    218                     $pay_meth = $listed_pay_lib;
    219                 } else {
    220                     $pay_meth = substr($order->AnsOrder->get_payment_method(),0,1) . substr($order->AnsOrder->get_payment_method(), 3, 1) . substr($order->AnsOrder->get_payment_method(), -1) ;
    221                 };
    222                 $order->lib = remove_accents (strtoupper($pay_meth) . ' ' .  $lib_prefix . ' ' . strtoupper($order->AnsOrder->get_billing_company()) . ' ' . ucfirst($order->AnsOrder->get_billing_last_name()) . ' ' . ucfirst($order->AnsOrder->get_billing_first_name()) . ' ' . $country_label);
    223                 $order->lib = trim($order->lib);
    224             }
    225             else {
    226                     $order->lib = remove_accents ($lib_prefix . ' ' . strtoupper($order->AnsOrder->get_billing_company()) . ' ' . ucfirst($order->AnsOrder->get_billing_last_name()) . ' ' . ucfirst($order->AnsOrder->get_billing_first_name()) . ' ' . $country_label);
    227                     $order->lib = trim($order->lib);
    228             }
    229             if ( $factnum_opt == 1 ){
    230                 //Creating invoice numbers as PDF Invoice plugin if set
    231                 if (!empty($number)){
    232                     $order_year = date_i18n( 'Y', strtotime( $order->piecedate ) );
    233                     $order_month = date_i18n( 'm', strtotime( $order->piecedate ) );
    234                     $order_day = date_i18n( 'd', strtotime( $order->piecedate ) );
    235                     $invoice_year = date_i18n( 'Y', strtotime( $invoice_date ) );
    236                     $invoice_month = date_i18n( 'm', strtotime( $invoice_date ) );
    237                     $invoice_day = date_i18n( 'd', strtotime( $invoice_date ) );
    238                    
    239                     //Creating credit note numbers
    240                     $formats['prefix'] = $pro_settings['credit_note_number_formatting_prefix'];
    241                     $formats['suffix'] = $pro_settings['credit_note_number_formatting_suffix'];
    242                     $formats['padding'] = $pro_settings['credit_note_number_formatting_padding'];
    243                     // Replacements
    244                     foreach ($formats as $key => $value) {
    245                         $value = str_replace('[order_year]', $order_year, $value);
    246                         $value = str_replace('[order_month]', $order_month, $value);
    247                         $value = str_replace('[order_day]', $order_day, $value);
    248                         $value = str_replace('[invoice_year]', $invoice_year, $value);
    249                         $value = str_replace('[invoice_month]', $invoice_month, $value);
    250                         $value = str_replace('[invoice_day]', $invoice_day, $value);
    251                         $formats[$key] = $value;
    252                     }
    253                     // Padding
    254                     if ( ctype_digit( (string)$formats['padding'] ) ) {
    255                         $number = sprintf('%0'.$formats['padding'].'d', $number);
    256                     }
    257                     $formatted_refund_number = $formats['prefix'] . $number . $formats['suffix'] ;
    258 
    259                     $order->lib = $formatted_refund_number . ' ' . $order->lib;
    260                 }
    261             }
    262             else {
    263                 if (!empty($formatted_refund_number)){
    264                     $order->lib = $order->ancestor_order . ' ' . $order->lib;
    265                 }
    266             }
    267             $order->outcome = round($order->total, $rounding_precision);
    268             $order->income_tax = round($order->total_tax > $small_number ? $order->total_tax : 0, $rounding_precision) + round($order->shipping_tax > $small_number ? $order->shipping_tax : 0, $rounding_precision);
    269             $order->income_fdpht = round($wcOrder->get_total_shipping(), $rounding_precision);
    270             $order->income_prodht = (($order->outcome) - (($order->income_tax) + ($order->income_fdpht)));
    271             $compiledOrders[] = $order;
    272         }
    273 
    274         // Let's prepare export
    275         $output = fopen("php://output",'w') or die ("Can't open php://output");
    276         $filename = 'woocommerce-accounting-wbk-refund-export-' . $ts1 . '-' . $ts2 . '.csv';
    277         if(wp_get_environment_type() == 'local'){
    278             header( 'Content-Disposition: inline; filename='.$filename );
     17//test nonce
     18if (! isset( $_POST['_check_refunds_export'] ) || ! wp_verify_nonce( $_POST['_check_refunds_export'], 'check_nonce_refunds_export')){
     19print 'Sorry, your nonce did not verify.';
     20exit;
     21}
     22
     23$separator = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_separator']);
     24$dec_separator = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_decimal_separator']);
     25$ts1 = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_start_date']);
     26$ts2 = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_end_date']);
     27$date_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_date_opt']);
     28$date_format_opt = sanitize_text_field($_POST['woocommerce_accounting_export_refunds_date_format']);
     29$cust_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_cust_opt']);
     30$pay_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_pay_opt']);
     31$prod_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_prod_opt']);
     32$taxes_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_taxes_opt']);
     33$ship_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_ship_opt']);
     34$factnum_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_factnum_opt']);
     35$zero_opt = sanitize_text_field($_POST['woocommerce_accounting_refunds_export_zero_opt']);
     36
     37update_option( 'woocommerce_accounting_refunds_export_start_date',$ts1);
     38update_option( 'woocommerce_accounting_refunds_export_end_date',$ts2);
     39update_option( 'woocommerce_accounting_export_refunds_date_format',$date_format_opt);
     40update_option( 'woocommerce_accounting_refunds_export_separator', $separator);
     41update_option( 'woocommerce_accounting_refunds_export_date_opt',$date_opt);
     42update_option( 'woocommerce_accounting_refunds_export_pay_opt',$pay_opt);
     43update_option( 'woocommerce_accounting_refunds_export_prod_opt',$prod_opt);
     44update_option( 'woocommerce_accounting_refunds_export_taxes_opt',$taxes_opt);
     45update_option( 'woocommerce_accounting_refunds_export_ship_opt',$ship_opt);
     46update_option( 'woocommerce_accounting_refunds_export_factnum_opt',$factnum_opt);
     47update_option( 'woocommerce_accounting_refunds_export_cust_opt',$cust_opt);
     48update_option( 'woocommerce_accounting_refunds_export_decimal_separator',$dec_separator);
     49update_option( 'woocommerce_accounting_refunds_export_zero_opt',$zero_opt);
     50
     51// Use WC Order Query style
     52// https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query#general
     53
     54$date_options = array(
     55'0' => 'date_created',
     56'1' => 'date_completed',
     57'3' => 'date_paid',
     58);
     59
     60$order_args = array(
     61'limit' => -1,
     62'type' => 'shop_order_refund',
     63'status' => array( 'wc-completed' ),
     64'orderby' => 'ID',
     65'order' => 'ASC',
     66);
     67
     68if(isset($date_options[$date_opt])) {
     69$order_args[$date_options[$date_opt]] = "{$ts1}...{$ts2}";
     70}
     71
     72$order_args = apply_filters('woocommerce_accounting:export:get_orders_refunds_args', $order_args, $date_opt, array( 'wc-completed' ), array($ts1, $ts2));
     73$orders = wc_get_orders($order_args);
     74do_action('woocommerce_accounting:export:launch', $orders);
     75   
     76$compiledOrders = array();
     77
     78//Datas to export
     79if (!empty($orders)) {
     80//Generic datas
     81$book_code = get_option('woocommerce_accounting_book_code_order');
     82$gen_account_cust = get_option('woocommerce_accounting_generic_cust_accounting_account');
     83$gen_account_prod = get_option ('woocommerce_accounting_generic_prod_accounting_account');
     84$gen_account_fdp = get_option ('woocommerce_accounting_generic_fdp_accounting_account');
     85$gen_account_tax = get_option ('woocommerce_accounting_generic_tax_accounting_account');
     86$gen_analytic_prod = get_option ('woocommerce_accounting_generic_prod_analytic_account');
     87$gen_analytic_fdp = get_option('woocommerce_accounting_generic_fdp_analytic_account');
     88$lib_prefix = get_option ('woocommerce_accounting_lib_prefix');
     89
     90$rounding_precision = 2;
     91
     92
     93foreach ($orders as $wcOrder)
     94{
     95// skip order if it is a regular order
     96if($wcOrder instanceof \WC_Order){
     97continue;
     98}
     99$order = (object) array();
     100$order->WCOrder = $wcOrder;
     101$order->ID = $wcOrder->get_id();
     102$order->status = 'wc-'.$wcOrder->get_status();
     103$order->date_created = $wcOrder->get_date_created();
     104$order->date_completed = $wcOrder->get_date_completed();
     105        $order->date_paid = $wcOrder->get_date_paid();
     106
     107$order->total = $wcOrder->get_total();
     108$order->total_tax = (float) $wcOrder->get_total_tax();
     109        $order->shipping_tax = (float) $wcOrder->get_shipping_tax();
     110           
     111//searching for parent order
     112$order->ancestor_order = $wcOrder->get_parent_id();
     113$order->AnsOrder = new \WC_Order($order->ancestor_order);
     114           
     115$order->custid = $order->AnsOrder->get_customer_id();
     116$order->gatewayid = $order->AnsOrder->get_payment_method();
     117
     118if ($date_opt == "0") {
     119$order->piecedate =  get_post_field( 'post_date', $order->ID );
     120} else {
     121$invoice_date = get_post_meta($order->ID,'_wcpdf_credit_note_date',true);
     122if (!empty($invoice_date)) {
     123$order->piecedate = $invoice_date;
     124} else {
     125$order->piecedate = get_post_field( 'post_date', $order->ID );
     126}
     127}
     128//Date Format
     129$order_year = date_i18n( 'Y', strtotime( $order->piecedate ) );
     130$order_month = date_i18n( 'm', strtotime( $order->piecedate ) );
     131$order_day = date_i18n ( 'd', strtotime( $order->piecedate ) ) ;
     132if ($date_format_opt == "1") {
     133$order->piecedate = $order_day . '-' . $order_month . '-' . $order_year;
     134} elseif ($date_format_opt == "2") {
     135$order->piecedate = $order_month . '-' . $order_day . '-' . $order_year;
     136} elseif ($date_format_opt == "3") {
     137$order->piecedate = $order_year . '-' . $order_month . '-' . $order_day;
     138} elseif ($date_format_opt == "4") {
     139$order->piecedate = $order_day . '/' . $order_month . '/' . $order_year;
     140} elseif ($date_format_opt == "5") {
     141$order->piecedate = $order_month . '/' . $order_day . '/' . $order_year;
     142} elseif ($date_format_opt == "6") {
     143$order->piecedate = $order_year . '/' . $order_month . '/' . $order_day;
     144} else {
     145$order->piecedate = $order->piecedate;
     146}
     147
     148// Test if plugin is activated
     149if ( in_array( 'woocommerce-pdf-ips-pro/woocommerce-pdf-ips-pro.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
     150$pro_settings = get_option( 'wpo_wcpdf_pro_settings' );
     151$number = get_post_meta($order->ID,'_wcpdf_credit_note_number',true);
     152$invoice_date = get_post_meta($order->ID,'_wcpdf_credit_note_date',true);
     153               
     154//Is invoice number defined ?
     155if ( ( !empty ($number) ) && $factnum_opt == 0 ){
     156//Use credit note number if is defined
     157$order_year = date_i18n( 'Y', strtotime( $order->piecedate ) );
     158$order_month = date_i18n( 'm', strtotime( $order->piecedate ) );
     159$order_day = date_i18n( 'd', strtotime( $order->piecedate ) );
     160$invoice_year = date_i18n( 'Y', strtotime( $invoice_date ) );
     161$invoice_month = date_i18n( 'm', strtotime( $invoice_date ) );
     162$invoice_day = date_i18n( 'd', strtotime( $invoice_date ) );
     163//Creating credit note numbers
     164$formats['prefix'] = $pro_settings['credit_note_number_formatting_prefix'];
     165$formats['suffix'] = $pro_settings['credit_note_number_formatting_suffix'];
     166$formats['padding'] = $pro_settings['credit_note_number_formatting_padding'];
     167
     168// Replacements
     169foreach ($formats as $key => $value) {
     170$value = str_replace('[order_year]', $order_year, $value);
     171$value = str_replace('[order_month]', $order_month, $value);
     172$value = str_replace('[order_day]', $order_day, $value);
     173$value = str_replace('[invoice_year]', $invoice_year, $value);
     174$value = str_replace('[invoice_month]', $invoice_month, $value);
     175$value = str_replace('[invoice_day]', $invoice_day, $value);
     176$formats[$key] = $value;
     177}
     178// Padding
     179if ( ctype_digit( (string)$formats['padding'] ) ) {
     180$number = sprintf('%0'.$formats['padding'].'d', $number);
     181}
     182$formatted_refund_number = $formats['prefix'] . $number . $formats['suffix'] ;
     183$order->number = $formatted_refund_number;
     184}
     185else {
     186$order->number = $order->ancestor_order;
     187}
     188//Create the number if not created
     189}
     190else {
     191$order->number = $order->ancestor_order;
     192}
     193//Customer account type (customer, country or gateway)
     194if ($cust_opt =="0") {
     195$order->account_cust = get_user_meta($order->custid,'woocommerce_accounting_user_accountingcode',true);
     196}
     197else if ($cust_opt =="1"){
     198$order_coutry_acc = get_option('woocommerce_accounting_gateways_libs');
     199$order->account_cust = $order_coutry_acc[$order->gatewayid]['acc'];
     200}
     201// Get the country only in label if prod by country
     202if ($prod_opt == "2") {
     203$country_label = $order->AnsOrder->get_shipping_country();
     204}
     205else {
     206$country_label = "";
     207}
     208//activating payment gateway in lib
     209if ($pay_opt == "1") {
     210$pay_meth = substr($order->AnsOrder->get_payment_method(), 0, 1) . substr($order->AnsOrder->get_payment_method(), 3, 1) . substr($order->AnsOrder->get_payment_method(), -1) ;
     211$order->lib = remove_accents (strtoupper($pay_meth) . ' ' .  $lib_prefix . ' ' . strtoupper($order->AnsOrder->get_billing_company()) . ' ' . ucfirst($order->AnsOrder->get_billing_last_name()) . ' ' . ucfirst($order->AnsOrder->get_billing_first_name()) . ' ' . $country_label);
     212$order->lib = trim($order->lib);
     213}
     214elseif ($pay_opt == "2") {
     215$order_pay_meth = $order->AnsOrder->get_payment_method();
     216$listed_pay_lib = get_option('woocommerce_accounting_gateways_libs')[$order_pay_meth]['lib'];
     217if (!empty ($listed_pay_lib)) {
     218$pay_meth = $listed_pay_lib;
     219} else {
     220$pay_meth = substr($order->AnsOrder->get_payment_method(),0,1) . substr($order->AnsOrder->get_payment_method(), 3, 1) . substr($order->AnsOrder->get_payment_method(), -1) ;
     221};
     222$order->lib = remove_accents (strtoupper($pay_meth) . ' ' .  $lib_prefix . ' ' . strtoupper($order->AnsOrder->get_billing_company()) . ' ' . ucfirst($order->AnsOrder->get_billing_last_name()) . ' ' . ucfirst($order->AnsOrder->get_billing_first_name()) . ' ' . $country_label);
     223$order->lib = trim($order->lib);
     224}
     225else {
     226$order->lib = remove_accents ($lib_prefix . ' ' . strtoupper($order->AnsOrder->get_billing_company()) . ' ' . ucfirst($order->AnsOrder->get_billing_last_name()) . ' ' . ucfirst($order->AnsOrder->get_billing_first_name()) . ' ' . $country_label);
     227$order->lib = trim($order->lib);
     228}
     229if ( $factnum_opt == 1 ){
     230//Creating invoice numbers as PDF Invoice plugin if set
     231if (!empty($number)){
     232$order_year = date_i18n( 'Y', strtotime( $order->piecedate ) );
     233$order_month = date_i18n( 'm', strtotime( $order->piecedate ) );
     234$order_day = date_i18n( 'd', strtotime( $order->piecedate ) );
     235$invoice_year = date_i18n( 'Y', strtotime( $invoice_date ) );
     236$invoice_month = date_i18n( 'm', strtotime( $invoice_date ) );
     237$invoice_day = date_i18n( 'd', strtotime( $invoice_date ) );
     238                   
     239//Creating credit note numbers
     240$formats['prefix'] = $pro_settings['credit_note_number_formatting_prefix'];
     241$formats['suffix'] = $pro_settings['credit_note_number_formatting_suffix'];
     242$formats['padding'] = $pro_settings['credit_note_number_formatting_padding'];
     243// Replacements
     244foreach ($formats as $key => $value) {
     245$value = str_replace('[order_year]', $order_year, $value);
     246$value = str_replace('[order_month]', $order_month, $value);
     247$value = str_replace('[order_day]', $order_day, $value);
     248$value = str_replace('[invoice_year]', $invoice_year, $value);
     249$value = str_replace('[invoice_month]', $invoice_month, $value);
     250$value = str_replace('[invoice_day]', $invoice_day, $value);
     251$formats[$key] = $value;
     252}
     253// Padding
     254if ( ctype_digit( (string)$formats['padding'] ) ) {
     255$number = sprintf('%0'.$formats['padding'].'d', $number);
     256}
     257$formatted_refund_number = $formats['prefix'] . $number . $formats['suffix'] ;
     258
     259$order->lib = $formatted_refund_number . ' ' . $order->lib;
     260}
     261}
     262else {
     263if (!empty($formatted_refund_number)){
     264$order->lib = $order->ancestor_order . ' ' . $order->lib;
     265}
     266}
     267$order->outcome = round($order->total, $rounding_precision);
     268$order->income_tax = round($order->total_tax > $small_number ? $order->total_tax : 0, $rounding_precision) + round($order->shipping_tax > $small_number ? $order->shipping_tax : 0, $rounding_precision);
     269$order->income_fdpht = round($wcOrder->get_total_shipping(), $rounding_precision);
     270$order->income_prodht = (($order->outcome) - (($order->income_tax) + ($order->income_fdpht)));
     271$compiledOrders[] = $order;
     272}
     273
     274// Let's prepare export
     275$output = fopen("php://output",'w') or die ("Can't open php://output");
     276$filename = 'woocommerce-accounting-wbk-refund-export-' . $ts1 . '-' . $ts2 . '.csv';
     277if(wp_get_environment_type() == 'local'){
     278header( 'Content-Disposition: inline; filename='.$filename );
    279279            header( 'Content-type: text/html' );
    280280            echo '<table  width="100%" cellspacing="2px;">';
    281         }
    282         else{
    283             header( 'Content-type: application/csv' );
    284             header( 'Content-Disposition: attachment; filename='.$filename );
    285             header('Content-Transfer-Encoding: UTF-8');
    286             header( 'Pragma: no-cache' );
    287             header( 'Expires: 0' );
    288             echo "\xEF\xBB\xBF";
    289         }
    290         // CSV Headers
    291         if (!empty($csv_headers['journal'])) {
    292             $csv_journal = $csv_headers['journal'];
    293         } else {
    294             $csv_journal = 'Code_Journal';
    295         }
    296         if (!empty($csv_headers['date'])) {
    297             $csv_date = $csv_headers['date'];
    298         } else {
    299             $csv_date = 'Date_de_piece';
    300         }
    301         if (!empty($csv_headers['number'])) {
    302             $csv_number = $csv_headers['number'];
    303         } else {
    304             $csv_number = 'Numero_de_piece';
    305         }
    306         if (!empty($csv_headers['code'])) {
    307             $csv_code = $csv_headers['code'];
    308         } else {
    309             $csv_code = 'Compte_Comptable';
    310         }
    311         if (!empty($csv_headers['label'])) {
    312             $csv_label = $csv_headers['label'];
    313         } else {
    314             $csv_label = 'Libelle';
    315         }
    316         if (!empty($csv_headers['outcome'])) {
    317             $csv_outcome = $csv_headers['outcome'];
    318         } else {
    319             $csv_outcome = 'Debit';
    320         }
    321         if (!empty($csv_headers['income'])) {
    322             $csv_income = $csv_headers['income'];
    323         } else {
    324             $csv_income = 'Credit';
    325         }
    326         if (!empty($csv_headers['center'])) {
    327             $csv_center = $csv_headers['center'];
    328         } else {
    329             $csv_center = 'Code_Analytique';
    330         }
    331 
    332         //Columns order preparation
    333         $col_map = get_option('woocommerce_accounting_colorder');
    334         $header_line = array(
    335             $col_map['Journal'] =>  $csv_journal,
    336             $col_map['Date'] => $csv_date,
    337             $col_map['Inv.Number'] =>   $csv_number,
    338             $col_map['Acc.Code'] => $csv_code,
    339             $col_map['Label'] =>    $csv_label,
    340             $col_map['Outcome'] =>  $csv_outcome,
    341             $col_map['Income'] =>   $csv_income,
    342             $col_map['Cost Center'] =>  $csv_center,
    343             $col_map['Empty 1'] =>  "",
    344             $col_map['Empty 2'] =>  "",
    345             $col_map['Empty 3'] =>  "",
    346             $col_map['Empty 4'] =>  ""
    347         );
    348         ksort($header_line);
    349        
    350         if ($separator == "t") {
    351             woocommerce_accounting_add_line($output, $header_line, "\t", true);
    352         }
    353         else {
    354             woocommerce_accounting_add_line($output, $header_line, $separator, true);
    355         }
    356 
    357         //Zero Option
    358         if ($zero_opt == "1") {
    359             $fillwith = 0.00;
    360         } else {
    361             $fillwith = "";
    362         }
    363 
    364         foreach($compiledOrders as $order) {
    365             $get_order = $order->WCOrder;
    366 
    367             /*-----------------------------------------------------------------------------*/
    368             /*---------------              Taxes for each order         -------------------*/
    369             /*-----------------------------------------------------------------------------*/
    370 
    371             $tax_applied_check = get_post_meta($order->ID,'_order_tax',true) + get_post_meta($order->ID,'_order_shipping_tax',true);
    372             // Is a tax set on this order ?
    373             if (($tax_applied_check != 0) && ($taxes_opt == "1") && ($order->order_discount == 0)){
    374                 // Searching for taxes applied
    375                 $tax_displays = array ();
    376 
    377                 foreach ($get_order->get_taxes() as $get_order_tax) {
    378                     $tax_rate_id = $get_order_tax['rate_id'];
    379                     // Getting specific account and amount if tax is set
    380                     if ( (!empty(get_option('woocommerce_accounting_tax_rates_accounting_account')[$tax_rate_id])) && (!empty(get_option('woocommerce_accounting_tax_rates_accounting_account')[$tax_rate_id]['acc']) ) ) {
    381                         $tax_line_account = get_option ('woocommerce_accounting_tax_rates_accounting_account')[$tax_rate_id]['acc'];
    382                         $tax_line_amount = $get_order_tax['tax_amount'] + $get_order_tax['shipping_tax_amount'];
    383 
    384                         $tax_displays [] = array (
    385                         'account' => $tax_line_account,
    386                         'amount' => $tax_line_amount
    387                         );
    388                     }
    389                     // Generic account and line total if not
    390                     else {
    391                         $tax_line_account = $gen_account_tax;
    392                         $tax_line_amount = $get_order_tax['tax_amount'] + $get_order_tax['shipping_tax_amount'];
    393 
    394                         $tax_displays [] = array (
    395                         'account' => $tax_line_account,
    396                         'amount' => $tax_line_amount
    397                         );
    398                     }
    399                 }
    400             }
    401             // If no specific tax : generic account and order tax total
    402             else {
    403                 $tax_line_account = $gen_account_tax;
    404                 $tax_line_amount = $order->income_tax;
    405             }
    406             /*-----------------------------------------------------------------------------*/
    407             /*--------------    Add shipping method accounting account  -------------------*/
    408             /*-----------------------------------------------------------------------------*/
    409             $order_shipping_methods = $get_order->get_shipping_methods( );
    410             foreach ($order_shipping_methods as $order_shipping_method){
    411                 $order_shipping_acc_list = get_option ('woocommerce_accounting_shipping_methods_accounting_account');
    412                 if ($ship_opt == "1") {
    413                     // Test if specific shipping method account is set
    414                     if (!empty($order_shipping_acc_list[$order_shipping_method['method_id']]['acc'])) {
    415                         $order_shipping_accounting_acc = $order_shipping_acc_list[$order_shipping_method['method_id']]['acc'];
    416                     } else {
    417                         $order_shipping_accounting_acc = $gen_account_fdp;
    418                     }
    419                     // Preparing specific analytic code
    420                     // Test if specific shipping method code is set
    421                     $order_shipping_ana_list = get_option ('woocommerce_accounting_shipping_methods_analytic_account');
    422                     if (!empty($order_shipping_ana_list[$order_shipping_method['method_id']]['ana'])) {
    423                         $order_shipping_analytic_acc = $order_shipping_ana_list[$order_shipping_method['method_id']]['ana'];
    424                     } else {
    425                         $order_shipping_analytic_acc = $gen_analytic_fdp;
    426                     }
    427                 } else {
    428                     $order_shipping_accounting_acc = $gen_account_fdp;
    429                     $order_shipping_analytic_acc = $gen_analytic_fdp;
    430                 }
    431             }
    432             /*-----------------------------------------------------------------------------*/
    433             /*--------------  Add products accounting codes and accounts  -----------------*/
    434             /*-----------------------------------------------------------------------------*/
    435             $bef_tax = get_post_meta($order->ID,'_cart_discount',true);
    436             if ($prod_opt == "1" && $order->order_discount == 0) {
    437                 $order_products = $get_order->get_items();
    438                 // Preparing table for datas
    439                 $order_products_datas = array ();
    440                 //Getting datas
    441                 foreach ($order_products as $item_id => $order_product) {
    442                     $order_product_id = $order_product['product_id'];
    443                     $order_product_acc = get_post_meta($order_product_id,'woocommerce_accounting_product_accounting_account',true);
    444                     $order_product_ana = get_post_meta($order_product_id,'woocommerce_accounting_product_accounting_analytic',true);
    445                     // Generic account if specific product account is not set
    446                     if (!empty ($order_product_acc)){
    447                         $order_prod_acc = $order_product_acc;
    448                     } else {
    449                         $order_prod_acc = $gen_account_prod;
    450                     }
    451                     // Generic analytic code if specific product account is not set
    452                     if (!empty ($order_product_ana)) {
    453                         $order_prod_ana = $order_product_ana;
    454                     }
    455                     else {
    456                         $order_prod_ana = $gen_analytic_prod;
    457                     }
    458                     //  Sort table
    459                     $order_products_datas[$item_id] = array (
    460                         'acc'=> $order_prod_acc,
    461                         'ana'=> $order_prod_ana,
    462                         'line_sub'=> $order_product['line_subtotal']
    463                         );
    464                 }
    465                 /*-----------------------------------------------------------------------------*/
    466                 /*--------------          Coupon amounts per product          -----------------*/
    467                 /*-----------------------------------------------------------------------------*/
    468                
    469                 //Coupons are considered as negative products
    470                 //Thinking ex. tax because it's illegal another way
    471                 if ($bef_tax > 0) {
    472                     //Amount of the coupon on each line (ex. tax)
    473                     $bef_order_products_discounts = array();
    474                     foreach ($order_products as $key=>$bef_order_product) {
    475                         $bef_order_product_id = $bef_order_product['product_id'];
    476                         $bef_discount_ht = ($bef_order_product['line_subtotal']) - ($bef_order_product['line_total']);
    477                         $bef_order_products_discounts[$key] = round($bef_discount_ht,2);
    478                     }
    479                 }
    480             }
    481             // Exceptionnal accounts
    482             $exptcred_acc = get_option('woocommerce_accounting_generic_exptcred_accounting_account');
    483             $exptchar_acc = get_option('woocommerce_accounting_generic_exptchar_accounting_account');
    484             /*Different cases*/
    485             //Order amount
    486             $chk_tot = round($order->outcome, $rounding_precision);
    487             //Products
    488             if ($prod_opt == "0" OR $prod_opt == "2" OR $order->order_discount > 0) {
    489                 $chk_prod = round($order->income_prodht,2);
    490             }
    491             else {
    492                 $col_prod = array();
    493                 foreach ($order_products as $order_product) {
    494                     $col_prod[] = round($order_product['line_subtotal'],2) ;
    495                 }
    496                 $chk_prod = round(array_sum($col_prod),2);
    497             }
    498             //Taxes
    499             if ($taxes_opt == "0" OR $order->order_discount > 0) {
    500                 $chk_tax = round($order->income_tax,2);
    501             }
    502             else {
    503                 $col_tax = array();
    504                 foreach ($get_order->get_taxes() as $get_order_tax) {
    505                     $tax_line_am = $get_order_tax['tax_amount'] + $get_order_tax['shipping_tax_amount'];
    506                     $col_tax[] = round($tax_line_am,2) ;
    507                 }
    508                 $chk_tax = round(array_sum($col_tax),2);
    509             }
    510             //Shipping
    511             $chk_fdp = round($order->income_fdpht,2);
    512             //Coupon
    513             if ($bef_tax > 0 && $prod_opt == "1" && $order->order_discount == 0) {
    514                 $chk_disc = round(array_sum($bef_order_products_discounts),2);
    515             }
    516             else {
    517                 $chk_disc = 0;
    518             }
    519             // Difference
    520             $chk_op = ($chk_tot + $chk_disc) - ($chk_prod + $chk_tax + $chk_fdp);
    521             $chk_op = round($chk_op,2);
    522             $chk = abs($chk_op);
    523            
    524             //Let's export ! (for tab)
    525             if ($separator == "t") {
    526                 //Alert system for more than rounding error difference
    527                 if ($chk > 0.03) {
    528 
    529                     woocommerce_accounting_add_line($output, array(
    530                         "ALERT !!! NEXT REFUND SEEMS TO BE A GENERIC REFUND OR TO BE AN ERROR. IT WILL BE ADDED IN AN EXCEPTIONNAL ACCOUNT. YOU SHOULD CHECK IT TO PRECISE WHAT HAS BEEN REFUNDED."
    531                     ),"\t");
    532                 }
    533                 //Customers
    534                 if ((!empty ($order->account_cust)) && ($order->outcome != 0))  {
    535                     $cust_line = array(
    536                         $col_map['Journal'] =>  $book_code,
    537                         $col_map['Date'] => $order->piecedate,
    538                         $col_map['Inv.Number'] =>   $order->number,
    539                         $col_map['Acc.Code'] => $order->account_cust,
    540                         $col_map['Label'] =>    $order->lib,
    541                         $col_map['Outcome'] =>  $fillwith,
    542                         $col_map['Income'] =>   str_replace('.',$dec_separator, abs(round($order->outcome,2))),
    543                         $col_map['Cost Center'] =>  "",
    544                         $col_map['Empty 1'] =>  "",
    545                         $col_map['Empty 2'] =>  "",
    546                         $col_map['Empty 3'] =>  "",
    547                         $col_map['Empty 4'] =>  ""
    548                     );
    549                     ksort($cust_line);
    550 
    551                     woocommerce_accounting_add_line($output,$cust_line,"\t");
    552                 }
    553                 else if ($order->outcome != 0) {
    554                     $cust_line = array(
    555                         $col_map['Journal'] =>  $book_code,
    556                         $col_map['Date'] => $order->piecedate,
    557                         $col_map['Inv.Number'] =>   $order->number,
    558                         $col_map['Acc.Code'] => $gen_account_cust,
    559                         $col_map['Label'] =>    $order->lib,
    560                         $col_map['Outcome'] =>  $fillwith,
    561                         $col_map['Income'] =>   str_replace('.',$dec_separator, abs(round($order->outcome,2))),
    562                         $col_map['Cost Center'] =>  "",
    563                         $col_map['Empty 1'] =>  "",
    564                         $col_map['Empty 2'] =>  "",
    565                         $col_map['Empty 3'] =>  "",
    566                         $col_map['Empty 4'] =>  ""
    567                             );
    568                     ksort($cust_line);
    569                     woocommerce_accounting_add_line($output,$cust_line,"\t");
    570                 }
    571                 //Shipping method
    572                 if ((!empty ($order_shipping_method)) && ($order->income_fdpht != 0)) {
    573                     $deliv_line = array(
    574                         $col_map['Journal'] =>  $book_code,
    575                         $col_map['Date'] => $order->piecedate,
    576                         $col_map['Inv.Number'] =>   $order->number,
    577                         $col_map['Acc.Code'] => $order_shipping_accounting_acc,
    578                         $col_map['Label'] =>    $order->lib,
    579                         $col_map['Outcome'] =>  str_replace('.',$dec_separator, abs(round($order->income_fdpht,2))),
    580                         $col_map['Income'] =>   $fillwith,
    581                         $col_map['Cost Center'] =>  $order_shipping_analytic_acc,
    582                         $col_map['Empty 1'] =>  "",
    583                         $col_map['Empty 2'] =>  "",
    584                         $col_map['Empty 3'] =>  "",
    585                         $col_map['Empty 4'] =>  ""
    586                     );
    587                     ksort($deliv_line);
    588                     woocommerce_accounting_add_line($output, $deliv_line, "\t");
    589                 }
    590 
    591                 //Products
    592                 if ($prod_opt == "1" && $order->order_discount == 0) {
    593                     foreach ($order_products_datas as $order_products_data) {
    594                         if ($order_products_data['line_sub'] != 0) {
    595                             $prod_line = array(
    596                                 $col_map['Journal'] =>  $book_code,
    597                                 $col_map['Date'] => $order->piecedate,
    598                                 $col_map['Inv.Number'] =>   $order->number,
    599                                 $col_map['Acc.Code'] => $order_products_data['acc'],
    600                                 $col_map['Label'] =>    $order->lib,
    601                                 $col_map['Outcome'] =>  str_replace('.',$dec_separator, abs(round($order_products_data['line_sub'],2))),
    602                                 $col_map['Income'] =>   $fillwith,
    603                                 $col_map['Cost Center'] =>  $order_products_data['ana'],
    604                                 $col_map['Empty 1'] =>  "",
    605                                 $col_map['Empty 2'] =>  "",
    606                                 $col_map['Empty 3'] =>  "",
    607                                 $col_map['Empty 4'] =>  ""
    608                             );
    609                             ksort($prod_line);
    610                             //Coupon
    611                             woocommerce_accounting_add_line($output, $prod_line,"\t");
    612                         }
    613                     }
    614                     if ($bef_tax > 0) {
    615                         foreach ($bef_order_products_discounts as $key => $bef_order_products_discount) {
    616                             $coupon_line = array(
    617                                 $col_map['Journal'] =>  $book_code,
    618                                 $col_map['Date'] => $order->piecedate,
    619                                 $col_map['Inv.Number'] =>   $order->number,
    620                                 $col_map['Acc.Code'] => $order_products_datas[$key]['acc'],
    621                                 $col_map['Label'] =>    $order->lib,
    622                                 $col_map['Outcome'] =>  str_replace('.',$dec_separator, abs(round($bef_order_products_discount,2))),
    623                                 $col_map['Income'] =>   $fillwith,
    624                                 $col_map['Cost Center'] =>  $order_products_datas[$key]['ana'],
    625                                 $col_map['Empty 1'] =>  "",
    626                                 $col_map['Empty 2'] =>  "",
    627                                 $col_map['Empty 3'] =>  "",
    628                                 $col_map['Empty 4'] =>  ""
    629                             );
    630                             ksort($coupon_line);
    631                             woocommerce_accounting_add_line($output, $coupon_line,"\t");
    632                         }
    633                     }   
    634                 //Simple Products
    635                 }
    636                 elseif ($prod_opt == "0" && $order->income_prodht != 0) {
    637                     $prod_line = array(
    638                         $col_map['Journal'] =>  $book_code,
    639                         $col_map['Date'] => $order->piecedate,
    640                         $col_map['Inv.Number'] =>   $order->number,
    641                         $col_map['Acc.Code'] => $gen_account_prod,
    642                         $col_map['Label'] =>    $order->lib,
    643                         $col_map['Outcome'] =>  str_replace('.',$dec_separator, abs(round($order->income_prodht,2))),
    644                         $col_map['Income'] =>   $fillwith,
    645                         $col_map['Cost Center'] =>  $gen_analytic_prod,
    646                         $col_map['Empty 1'] =>  "",
    647                         $col_map['Empty 2'] =>  "",
    648                         $col_map['Empty 3'] =>  "",
    649                         $col_map['Empty 4'] =>  ""
    650                         );
    651                         ksort($prod_line);
    652                         woocommerce_accounting_add_line($output, $prod_line,"\t");
    653                 }
    654                 //Simple products by country
    655                 elseif ($prod_opt == "2" && $order->income_prodht != 0) {
    656                     $order->countryid = $order->AnsOrder->get_shipping_country();
    657                     $order_coutry_acc = get_option('woocommerce_accounting_countries_acc');
    658                     $country_account_prod = $order_coutry_acc[$order->countryid]['acc'];
    659                     if (empty($country_account_prod)) {
    660                         $country_account_prod = $gen_account_prod;
    661                     }
    662                     $prod_line = array(
    663                         $col_map['Journal'] =>  $book_code,
    664                         $col_map['Date'] => $order->piecedate,
    665                         $col_map['Inv.Number'] =>   $order->number,
    666                         $col_map['Acc.Code'] => $country_account_prod,
    667                         $col_map['Label'] =>    $order->lib,
    668                         $col_map['Outcome'] =>  str_replace('.',$dec_separator, abs(round($order->income_prodht,2))),
    669                         $col_map['Income'] =>   $fillwith,
    670                         $col_map['Cost Center'] =>  $gen_analytic_prod,
    671                         $col_map['Empty 1'] =>  "",
    672                         $col_map['Empty 2'] =>  "",
    673                         $col_map['Empty 3'] =>  "",
    674                         $col_map['Empty 4'] =>  ""
    675                     );
    676                     ksort($prod_line);
    677                     woocommerce_accounting_add_line($output, $prod_line,"\t");
    678                 }
    679                
    680                 //Taxes
    681                 if (($tax_applied_check != 0) && ($taxes_opt == "1") && ($order->order_discount == 0)) {
    682                     foreach ($tax_displays as $tax_display) {
    683                         $tax_line = array(
    684                             $col_map['Journal'] =>  $book_code,
    685                             $col_map['Date'] => $order->piecedate,
    686                             $col_map['Inv.Number'] =>   $order->number,
    687                             $col_map['Acc.Code'] => $tax_display['account'],
    688                             $col_map['Label'] =>    $order->lib,
    689                             $col_map['Outcome'] =>  str_replace('.',$dec_separator, abs(round($tax_display['amount'],2))),
    690                             $col_map['Income'] =>   $fillwith,
    691                             $col_map['Cost Center'] =>  "",
    692                             $col_map['Empty 1'] =>  "",
    693                             $col_map['Empty 2'] =>  "",
    694                             $col_map['Empty 3'] =>  "",
    695                             $col_map['Empty 4'] =>  ""
    696                         );
    697                         ksort($tax_line);
    698                         woocommerce_accounting_add_line($output, $tax_line,"\t");
    699                     }
    700                 }
    701                 else if ($tax_line_amount != 0){
    702                     $tax_line = array(
    703                         $col_map['Journal'] =>  $book_code,
    704                         $col_map['Date'] => $order->piecedate,
    705                         $col_map['Inv.Number'] =>   $order->number,
    706                         $col_map['Acc.Code'] => $tax_line_account,
    707                         $col_map['Label'] =>    $order->lib,
    708                         $col_map['Outcome'] =>  str_replace('.',$dec_separator, abs(round($tax_line_amount,2))),
    709                         $col_map['Income'] =>   $fillwith,
    710                         $col_map['Cost Center'] =>  "",
    711                         $col_map['Empty 1'] =>  "",
    712                         $col_map['Empty 2'] =>  "",
    713                         $col_map['Empty 3'] =>  "",
    714                         $col_map['Empty 4'] =>  ""
    715                     );
    716                     ksort($tax_line);
    717                     woocommerce_accounting_add_line($output, $tax_line,"\t");
    718                 }
    719                 // Exceptionnal Income
    720                 if ($chk > 0 && $chk_op > 0) {
    721                     $cred_line = array(
    722                         $col_map['Journal'] =>  $book_code,
    723                         $col_map['Date'] => $order->piecedate,
    724                         $col_map['Inv.Number'] =>   $order->number,
    725                         $col_map['Acc.Code'] => $exptcred_acc,
    726                         $col_map['Label'] =>    $order->lib,
    727                         $col_map['Outcome'] =>  $fillwith,
    728                         $col_map['Income'] =>   str_replace('.',$dec_separator, round($chk,2)),
    729                         $col_map['Cost Center'] =>  "",
    730                         $col_map['Empty 1'] =>  "",
    731                         $col_map['Empty 2'] =>  "",
    732                         $col_map['Empty 3'] =>  "",
    733                         $col_map['Empty 4'] =>  ""
    734                     );
    735                     ksort($cred_line);
    736                     woocommerce_accounting_add_line($output, $cred_line,"\t");
    737                 }
    738                 //Exceptionnal Outcome
    739                 elseif ($chk > 0 && $chk_op < 0) {
    740                     $deb_line = array(
    741                         $col_map['Journal'] =>  $book_code,
    742                         $col_map['Date'] => $order->piecedate,
    743                         $col_map['Inv.Number'] =>   $order->number,
    744                         $col_map['Acc.Code'] => $exptchar_acc,
    745                         $col_map['Label'] =>    $order->lib,
    746                         $col_map['Outcome'] =>  str_replace('.',$dec_separator, round($chk,2)),
    747                         $col_map['Income'] =>   $fillwith,
    748                         $col_map['Cost Center'] =>  "",
    749                         $col_map['Empty 1'] =>  "",
    750                         $col_map['Empty 2'] =>  "",
    751                         $col_map['Empty 3'] =>  "",
    752                         $col_map['Empty 4'] =>  ""
    753                     );
    754                     ksort($deb_line);
    755                     woocommerce_accounting_add_line($output,$deb_line,"\t");
    756                 }
    757             // Other separator (not tab)
    758             }
    759             else {
    760                 //Alert system for more than rounding error difference
    761                 if ($chk > 0.03) {
    762                     woocommerce_accounting_add_line($output, array(
    763                         "ALERT !!! NEXT REFUND SEEMS TO BE A GENERIC REFUND OR TO BE AN ERROR. IT WILL BE ADDED IN AN EXCEPTIONNAL ACCOUNT. YOU SHOULD CHECK IT TO PRECISE WHAT HAS BEEN REFUNDED."
    764                     ),
    765                     $separator);
    766                 }
    767                 //Customers
    768                 if ((!empty ($order->account_cust)) && ($order->outcome != 0))  {
    769                     $cust_line = array(
    770                         $col_map['Journal'] =>  $book_code,
    771                         $col_map['Date'] => $order->piecedate,
    772                         $col_map['Inv.Number'] =>   $order->number,
    773                         $col_map['Acc.Code'] => $order->account_cust,
    774                         $col_map['Label'] =>    $order->lib,
    775                         $col_map['Outcome'] =>  $fillwith,
    776                         $col_map['Income'] =>   str_replace('.',$dec_separator, abs(round($order->outcome,2))),
    777                         $col_map['Cost Center'] =>  "",
    778                         $col_map['Empty 1'] =>  "",
    779                         $col_map['Empty 2'] =>  "",
    780                         $col_map['Empty 3'] =>  "",
    781                         $col_map['Empty 4'] =>  ""
    782                     );
    783                     ksort($cust_line);
    784                     woocommerce_accounting_add_line($output,$cust_line, $separator);
    785                 } else if ($order->outcome != 0) {
    786                     $cust_line = array(
    787                         $col_map['Journal'] =>  $book_code,
    788                         $col_map['Date'] => $order->piecedate,
    789                         $col_map['Inv.Number'] =>   $order->number,
    790                         $col_map['Acc.Code'] => $gen_account_cust,
    791                         $col_map['Label'] =>    $order->lib,
    792                         $col_map['Outcome'] =>  $fillwith,
    793                         $col_map['Income'] =>   str_replace('.',$dec_separator, abs(round($order->outcome,2))),
    794                         $col_map['Cost Center'] =>  "",
    795                         $col_map['Empty 1'] =>  "",
    796                         $col_map['Empty 2'] =>  "",
    797                         $col_map['Empty 3'] =>  "",
    798                         $col_map['Empty 4'] =>  ""
    799                     );
    800                     ksort($cust_line);
    801                     woocommerce_accounting_add_line($output,$cust_line, $separator);
    802                 }
    803                 //Shipping
    804                 if ((!empty ($order_shipping_method)) && ($order->income_fdpht != 0)) {
    805                     $deliv_line = array(
    806                         $col_map['Journal'] =>  $book_code,
    807                         $col_map['Date'] => $order->piecedate,
    808                         $col_map['Inv.Number'] =>   $order->number,
    809                         $col_map['Acc.Code'] => $order_shipping_accounting_acc,
    810                         $col_map['Label'] =>    $order->lib,
    811                         $col_map['Outcome'] =>  str_replace('.',$dec_separator, abs(round($order->income_fdpht,2))),
    812                         $col_map['Income'] =>   $fillwith,
    813                         $col_map['Cost Center'] =>  $order_shipping_analytic_acc,
    814                         $col_map['Empty 1'] =>  "",
    815                         $col_map['Empty 2'] =>  "",
    816                         $col_map['Empty 3'] =>  "",
    817                         $col_map['Empty 4'] =>  ""
    818                     );
    819                     ksort($deliv_line);
    820                     woocommerce_accounting_add_line($output, $deliv_line, $separator);
    821                 }
    822                 //Products
    823                 if ($prod_opt == "1" && $order->order_discount == 0) {
    824                     foreach ($order_products_datas as $order_products_data) {
    825                         if ($order_products_data['line_sub'] != 0) {
    826                             $prod_line = array(
    827                                 $col_map['Journal'] =>  $book_code,
    828                                 $col_map['Date'] => $order->piecedate,
    829                                 $col_map['Inv.Number'] =>   $order->number,
    830                                 $col_map['Acc.Code'] => $order_products_data['acc'],
    831                                 $col_map['Label'] =>    $order->lib,
    832                                 $col_map['Outcome'] =>  str_replace('.',$dec_separator, abs(round($order_products_data['line_sub'],2))),
    833                                 $col_map['Income'] =>   $fillwith,
    834                                 $col_map['Cost Center'] =>  $order_products_data['ana'],
    835                                 $col_map['Empty 1'] =>  "",
    836                                 $col_map['Empty 2'] =>  "",
    837                                 $col_map['Empty 3'] =>  "",
    838                                 $col_map['Empty 4'] =>  ""
    839                             );
    840                             ksort($prod_line);
    841                             woocommerce_accounting_add_line($output, $prod_line, $separator);
    842                         }
    843                     }
    844                     // Coupons
    845                     if ($bef_tax > 0) {
    846                         foreach ($bef_order_products_discounts as $key => $bef_order_products_discount) {
    847                             $coupon_line = array(
    848                                 $col_map['Journal'] =>  $book_code,
    849                                 $col_map['Date'] => $order->piecedate,
    850                                 $col_map['Inv.Number'] =>   $order->number,
    851                                 $col_map['Acc.Code'] => $order_products_datas[$key]['acc'],
    852                                 $col_map['Label'] =>    $order->lib,
    853                                 $col_map['Outcome'] =>  str_replace('.',$dec_separator, abs(round($bef_order_products_discount,2))),
    854                                 $col_map['Income'] =>   $fillwith,
    855                                 $col_map['Cost Center'] =>  $order_products_datas[$key]['ana'],
    856                                 $col_map['Empty 1'] =>  "",
    857                                 $col_map['Empty 2'] =>  "",
    858                                 $col_map['Empty 3'] =>  "",
    859                                 $col_map['Empty 4'] =>  ""
    860                             );
    861                             ksort($coupon_line);
    862                             woocommerce_accounting_add_line($output, $coupon_line, $separator);
    863                         }
    864                     }
    865                     //Simple products
    866                 } elseif ($prod_opt == "0" && $order->income_prodht != 0) {
    867                     $prod_line = array(
    868                         $col_map['Journal'] =>  $book_code,
    869                         $col_map['Date'] => $order->piecedate,
    870                         $col_map['Inv.Number'] =>   $order->number,
    871                         $col_map['Acc.Code'] => $gen_account_prod,
    872                         $col_map['Label'] =>    $order->lib,
    873                         $col_map['Outcome'] =>  str_replace('.',$dec_separator, abs(round($order->income_prodht,2))),
    874                         $col_map['Income'] =>   $fillwith,
    875                         $col_map['Cost Center'] =>  $gen_analytic_prod,
    876                         $col_map['Empty 1'] =>  "",
    877                         $col_map['Empty 2'] =>  "",
    878                         $col_map['Empty 3'] =>  "",
    879                         $col_map['Empty 4'] =>  ""
    880                     );
    881                     ksort($prod_line);
    882                     woocommerce_accounting_add_line($output, $prod_line, $separator);
    883                     //Simple products by country
    884                 }
    885                 elseif ($prod_opt == "2" && $order->income_prodht != 0) {
    886                     $wc_order = wc_get_order($order->ID);
    887                     $original_order_id = $wc_order->get_parent_id();
    888                     $original_order = wc_get_order( $original_order_id );
    889                     $order->countryid = $original_order->get_billing_country();
    890                     $order_country_acc = get_option('woocommerce_accounting_countries_acc');
    891                     $country_account_prod = $order_country_acc[$order->countryid]['acc'];
    892                     if (empty($country_account_prod)) {
    893                         $country_account_prod = $gen_account_prod;
    894                     }
    895                     $prod_line = array(
    896                         $col_map['Journal'] =>  $book_code,
    897                         $col_map['Date'] => $order->piecedate,
    898                         $col_map['Inv.Number'] =>   $order->number,
    899                         $col_map['Acc.Code'] => $country_account_prod,
    900                         $col_map['Label'] =>    $order->lib,
    901                         $col_map['Outcome'] =>  str_replace('.',$dec_separator, abs(round($order->income_prodht,2))),
    902                         $col_map['Income'] =>   $fillwith,
    903                         $col_map['Cost Center'] =>  $gen_account_prod,
    904                         $col_map['Empty 1'] =>  "",
    905                         $col_map['Empty 2'] =>  "",
    906                         $col_map['Empty 3'] =>  "",
    907                         $col_map['Empty 4'] =>  ""
    908                     );
    909                     ksort($prod_line);
    910                     woocommerce_accounting_add_line($output, $prod_line, $separator);
    911                 }
    912                 //Taxes
    913                 if (($tax_applied_check != 0) && ($taxes_opt == "1") && ($order->order_discount == 0)) {
    914                     foreach ($tax_displays as $tax_display) {
    915                         $tax_line = array(
    916                             $col_map['Journal'] =>  $book_code,
    917                             $col_map['Date'] => $order->piecedate,
    918                             $col_map['Inv.Number'] =>   $order->number,
    919                             $col_map['Acc.Code'] => $tax_display['account'],
    920                             $col_map['Label'] =>    $order->lib,
    921                             $col_map['Outcome'] =>  str_replace('.',$dec_separator, abs(round($tax_display['amount'],2))),
    922                             $col_map['Income'] =>   $fillwith,
    923                             $col_map['Cost Center'] =>  "",
    924                             $col_map['Empty 1'] =>  "",
    925                             $col_map['Empty 2'] =>  "",
    926                             $col_map['Empty 3'] =>  "",
    927                             $col_map['Empty 4'] =>  ""
    928                         );
    929                         ksort($tax_line);
    930                         woocommerce_accounting_add_line($output, $tax_line, $separator);
    931                     }
    932                 }
    933                 else if ($tax_line_amount != 0){
    934                     $tax_line = array(
    935                         $col_map['Journal'] =>  $book_code,
    936                         $col_map['Date'] => $order->piecedate,
    937                         $col_map['Inv.Number'] =>   $order->number,
    938                         $col_map['Acc.Code'] => $tax_line_account,
    939                         $col_map['Label'] =>    $order->lib,
    940                         $col_map['Outcome'] =>  str_replace('.',$dec_separator, abs(round($tax_line_amount,2))),
    941                         $col_map['Income'] =>   $fillwith,
    942                         $col_map['Cost Center'] =>  "",
    943                         $col_map['Empty 1'] =>  "",
    944                         $col_map['Empty 2'] =>  "",
    945                         $col_map['Empty 3'] =>  "",
    946                         $col_map['Empty 4'] =>  ""
    947                     );
    948                     ksort($tax_line);
    949                     woocommerce_accounting_add_line($output, $tax_line, $separator);
    950                 }
    951                 // Exceptionnal Income
    952                 if ($chk > 0 && $chk_op > 0) {
    953                     $cred_line = array(
    954                         $col_map['Journal'] =>  $book_code,
    955                         $col_map['Date'] => $order->piecedate,
    956                         $col_map['Inv.Number'] =>   $order->number,
    957                         $col_map['Acc.Code'] => $exptcred_acc,
    958                         $col_map['Label'] =>    $order->lib,
    959                         $col_map['Outcome'] =>  $fillwith,
    960                         $col_map['Income'] =>   str_replace('.',$dec_separator, round($chk,2)),
    961                         $col_map['Cost Center'] =>  "",
    962                         $col_map['Empty 1'] =>  "",
    963                         $col_map['Empty 2'] =>  "",
    964                         $col_map['Empty 3'] =>  "",
    965                         $col_map['Empty 4'] =>  ""
    966                     );
    967                     ksort($cred_line);
    968                     woocommerce_accounting_add_line($output, $cred_line, $separator);
    969                 }
    970                 //Exceptionnal Outcome
    971                 elseif ($chk > 0 && $chk_op < 0) {
    972                     $deb_line = array(
    973                         $col_map['Journal'] =>  $book_code,
    974                         $col_map['Date'] => $order->piecedate,
    975                         $col_map['Inv.Number'] =>   $order->number,
    976                         $col_map['Acc.Code'] => $exptchar_acc,
    977                         $col_map['Label'] =>    $order->lib,
    978                         $col_map['Outcome'] =>  str_replace('.',$dec_separator, round($chk,2)),
    979                         $col_map['Income'] =>   $fillwith,
    980                         $col_map['Cost Center'] =>  "",
    981                         $col_map['Empty 1'] =>  "",
    982                         $col_map['Empty 2'] =>  "",
    983                         $col_map['Empty 3'] =>  "",
    984                         $col_map['Empty 4'] =>  ""
    985                     );
    986                     ksort($deb_line);
    987                     woocommerce_accounting_add_line($output,$deb_line, $separator);
    988                 }
    989             }
    990         }
    991         fclose($output) or die("Can't close php://output");
    992         exit;
    993     }
    994     else {
    995         wp_safe_redirect(add_query_arg([
    996             'page'=>'woocommerce_accounting_exporter',
    997             'format'=>'refunds',
    998             'error'=>'no_orders',
    999         ], admin_url('admin.php')));
    1000         exit;
    1001     }
     281}
     282else{
     283header( 'Content-type: application/csv' );
     284header( 'Content-Disposition: attachment; filename='.$filename );
     285header('Content-Transfer-Encoding: UTF-8');
     286header( 'Pragma: no-cache' );
     287header( 'Expires: 0' );
     288echo "\xEF\xBB\xBF";
     289}
     290// CSV Headers
     291if (!empty($csv_headers['journal'])) {
     292$csv_journal = $csv_headers['journal'];
     293} else {
     294$csv_journal = 'Code_Journal';
     295}
     296if (!empty($csv_headers['date'])) {
     297$csv_date = $csv_headers['date'];
     298} else {
     299$csv_date = 'Date_de_piece';
     300}
     301if (!empty($csv_headers['number'])) {
     302$csv_number = $csv_headers['number'];
     303} else {
     304$csv_number = 'Numero_de_piece';
     305}
     306if (!empty($csv_headers['code'])) {
     307$csv_code = $csv_headers['code'];
     308} else {
     309$csv_code = 'Compte_Comptable';
     310}
     311if (!empty($csv_headers['label'])) {
     312$csv_label = $csv_headers['label'];
     313} else {
     314$csv_label = 'Libelle';
     315}
     316if (!empty($csv_headers['outcome'])) {
     317$csv_outcome = $csv_headers['outcome'];
     318} else {
     319$csv_outcome = 'Debit';
     320}
     321if (!empty($csv_headers['income'])) {
     322$csv_income = $csv_headers['income'];
     323} else {
     324$csv_income = 'Credit';
     325}
     326if (!empty($csv_headers['center'])) {
     327$csv_center = $csv_headers['center'];
     328} else {
     329$csv_center = 'Code_Analytique';
     330}
     331
     332//Columns order preparation
     333$col_map = get_option('woocommerce_accounting_colorder');
     334$header_line = array(
     335$csv_journal,
     336$csv_date,
     337$csv_number,
     338$csv_code,
     339$csv_label,
     340$csv_outcome,
     341$csv_income,
     342$csv_center,
     343"",
     344"",
     345"",
     346""
     347);
     348ksort($header_line);
     349       
     350if ($separator == "t") {
     351woocommerce_accounting_add_line($output, $header_line, "\t", true);
     352}
     353else {
     354woocommerce_accounting_add_line($output, $header_line, $separator, true);
     355}
     356
     357//Zero Option
     358if ($zero_opt == "1") {
     359$fillwith = 0.00;
     360} else {
     361$fillwith = "";
     362}
     363
     364foreach($compiledOrders as $order) {
     365$get_order = $order->WCOrder;
     366
     367/*-----------------------------------------------------------------------------*/
     368/*---------------              Taxes for each order         -------------------*/
     369/*-----------------------------------------------------------------------------*/
     370
     371$tax_applied_check = get_post_meta($order->ID,'_order_tax',true) + get_post_meta($order->ID,'_order_shipping_tax',true);
     372// Is a tax set on this order ?
     373if (($tax_applied_check != 0) && ($taxes_opt == "1") && ($order->order_discount == 0)){
     374// Searching for taxes applied
     375$tax_displays = array ();
     376
     377foreach ($get_order->get_taxes() as $get_order_tax) {
     378$tax_rate_id = $get_order_tax['rate_id'];
     379// Getting specific account and amount if tax is set
     380if ( (!empty(get_option('woocommerce_accounting_tax_rates_accounting_account')[$tax_rate_id])) && (!empty(get_option('woocommerce_accounting_tax_rates_accounting_account')[$tax_rate_id]['acc']) ) ) {
     381$tax_line_account = get_option ('woocommerce_accounting_tax_rates_accounting_account')[$tax_rate_id]['acc'];
     382$tax_line_amount = $get_order_tax['tax_amount'] + $get_order_tax['shipping_tax_amount'];
     383
     384$tax_displays [] = array (
     385'account' => $tax_line_account,
     386'amount' => $tax_line_amount
     387);
     388}
     389// Generic account and line total if not
     390else {
     391$tax_line_account = $gen_account_tax;
     392$tax_line_amount = $get_order_tax['tax_amount'] + $get_order_tax['shipping_tax_amount'];
     393
     394$tax_displays [] = array (
     395'account' => $tax_line_account,
     396'amount' => $tax_line_amount
     397);
     398}
     399}
     400}
     401// If no specific tax : generic account and order tax total
     402else {
     403$tax_line_account = $gen_account_tax;
     404$tax_line_amount = $order->income_tax;
     405}
     406/*-----------------------------------------------------------------------------*/
     407/*--------------    Add shipping method accounting account  -------------------*/
     408/*-----------------------------------------------------------------------------*/
     409$order_shipping_methods = $get_order->get_shipping_methods( );
     410foreach ($order_shipping_methods as $order_shipping_method){
     411$order_shipping_acc_list = get_option ('woocommerce_accounting_shipping_methods_accounting_account');
     412if ($ship_opt == "1") {
     413// Test if specific shipping method account is set
     414if (!empty($order_shipping_acc_list[$order_shipping_method['method_id']]['acc'])) {
     415$order_shipping_accounting_acc = $order_shipping_acc_list[$order_shipping_method['method_id']]['acc'];
     416} else {
     417$order_shipping_accounting_acc = $gen_account_fdp;
     418}
     419// Preparing specific analytic code
     420// Test if specific shipping method code is set
     421$order_shipping_ana_list = get_option ('woocommerce_accounting_shipping_methods_analytic_account');
     422if (!empty($order_shipping_ana_list[$order_shipping_method['method_id']]['ana'])) {
     423$order_shipping_analytic_acc = $order_shipping_ana_list[$order_shipping_method['method_id']]['ana'];
     424} else {
     425$order_shipping_analytic_acc = $gen_analytic_fdp;
     426}
     427} else {
     428$order_shipping_accounting_acc = $gen_account_fdp;
     429$order_shipping_analytic_acc = $gen_analytic_fdp;
     430}
     431}
     432/*-----------------------------------------------------------------------------*/
     433/*--------------  Add products accounting codes and accounts  -----------------*/
     434/*-----------------------------------------------------------------------------*/
     435$bef_tax = get_post_meta($order->ID,'_cart_discount',true);
     436if ($prod_opt == "1" && $order->order_discount == 0) {
     437$order_products = $get_order->get_items();
     438// Preparing table for datas
     439$order_products_datas = array ();
     440//Getting datas
     441foreach ($order_products as $item_id => $order_product) {
     442$order_product_id = $order_product['product_id'];
     443$order_product_acc = get_post_meta($order_product_id,'woocommerce_accounting_product_accounting_account',true);
     444$order_product_ana = get_post_meta($order_product_id,'woocommerce_accounting_product_accounting_analytic',true);
     445// Generic account if specific product account is not set
     446if (!empty ($order_product_acc)){
     447$order_prod_acc = $order_product_acc;
     448} else {
     449$order_prod_acc = $gen_account_prod;
     450}
     451// Generic analytic code if specific product account is not set
     452if (!empty ($order_product_ana)) {
     453$order_prod_ana = $order_product_ana;
     454}
     455else {
     456$order_prod_ana = $gen_analytic_prod;
     457}
     458//  Sort table
     459$order_products_datas[$item_id] = array (
     460'acc'=> $order_prod_acc,
     461'ana'=> $order_prod_ana,
     462'line_sub'=> $order_product['line_subtotal']
     463);
     464}
     465/*-----------------------------------------------------------------------------*/
     466/*--------------          Coupon amounts per product          -----------------*/
     467/*-----------------------------------------------------------------------------*/
     468               
     469//Coupons are considered as negative products
     470//Thinking ex. tax because it's illegal another way
     471if ($bef_tax > 0) {
     472//Amount of the coupon on each line (ex. tax)
     473$bef_order_products_discounts = array();
     474foreach ($order_products as $key=>$bef_order_product) {
     475$bef_order_product_id = $bef_order_product['product_id'];
     476$bef_discount_ht = ($bef_order_product['line_subtotal']) - ($bef_order_product['line_total']);
     477$bef_order_products_discounts[$key] = round($bef_discount_ht,2);
     478}
     479}
     480}
     481// Exceptionnal accounts
     482$exptcred_acc = get_option('woocommerce_accounting_generic_exptcred_accounting_account');
     483$exptchar_acc = get_option('woocommerce_accounting_generic_exptchar_accounting_account');
     484/*Different cases*/
     485//Order amount
     486$chk_tot = round($order->outcome, $rounding_precision);
     487//Products
     488if ($prod_opt == "0" OR $prod_opt == "2" OR $order->order_discount > 0) {
     489$chk_prod = round($order->income_prodht,2);
     490}
     491else {
     492$col_prod = array();
     493foreach ($order_products as $order_product) {
     494$col_prod[] = round($order_product['line_subtotal'],2) ;
     495}
     496$chk_prod = round(array_sum($col_prod),2);
     497}
     498//Taxes
     499if ($taxes_opt == "0" OR $order->order_discount > 0) {
     500$chk_tax = round($order->income_tax,2);
     501}
     502else {
     503$col_tax = array();
     504foreach ($get_order->get_taxes() as $get_order_tax) {
     505$tax_line_am = $get_order_tax['tax_amount'] + $get_order_tax['shipping_tax_amount'];
     506$col_tax[] = round($tax_line_am,2) ;
     507}
     508$chk_tax = round(array_sum($col_tax),2);
     509}
     510//Shipping
     511$chk_fdp = round($order->income_fdpht,2);
     512//Coupon
     513if ($bef_tax > 0 && $prod_opt == "1" && $order->order_discount == 0) {
     514$chk_disc = round(array_sum($bef_order_products_discounts),2);
     515}
     516else {
     517$chk_disc = 0;
     518}
     519// Difference
     520$chk_op = ($chk_tot + $chk_disc) - ($chk_prod + $chk_tax + $chk_fdp);
     521$chk_op = round($chk_op,2);
     522$chk = abs($chk_op);
     523           
     524//Let's export ! (for tab)
     525if ($separator == "t") {
     526//Alert system for more than rounding error difference
     527if ($chk > 0.03) {
     528
     529woocommerce_accounting_add_line($output, array(
     530"ALERT !!! NEXT REFUND SEEMS TO BE A GENERIC REFUND OR TO BE AN ERROR. IT WILL BE ADDED IN AN EXCEPTIONNAL ACCOUNT. YOU SHOULD CHECK IT TO PRECISE WHAT HAS BEEN REFUNDED."
     531),"\t");
     532}
     533//Customers
     534if ((!empty ($order->account_cust)) && ($order->outcome != 0))  {
     535$cust_line = array(
     536$book_code,
     537$order->piecedate,
     538$order->number,
     539$order->account_cust,
     540$order->lib,
     541$fillwith,
     542str_replace('.',$dec_separator, abs(round($order->outcome,2))),
     543"",
     544"",
     545"",
     546"",
     547""
     548);
     549ksort($cust_line);
     550
     551woocommerce_accounting_add_line($output,$cust_line,"\t");
     552}
     553else if ($order->outcome != 0) {
     554$cust_line = array(
     555$book_code,
     556$order->piecedate,
     557$order->number,
     558$gen_account_cust,
     559$order->lib,
     560$fillwith,
     561str_replace('.',$dec_separator, abs(round($order->outcome,2))),
     562"",
     563"",
     564"",
     565"",
     566""
     567);
     568ksort($cust_line);
     569woocommerce_accounting_add_line($output,$cust_line,"\t");
     570}
     571//Shipping method
     572if ((!empty ($order_shipping_method)) && ($order->income_fdpht != 0)) {
     573$deliv_line = array(
     574$book_code,
     575$order->piecedate,
     576$order->number,
     577$order_shipping_accounting_acc,
     578$order->lib,
     579str_replace('.',$dec_separator, abs(round($order->income_fdpht,2))),
     580$fillwith,
     581$order_shipping_analytic_acc,
     582"",
     583"",
     584"",
     585""
     586);
     587ksort($deliv_line);
     588woocommerce_accounting_add_line($output, $deliv_line, "\t");
     589}
     590
     591//Products
     592if ($prod_opt == "1" && $order->order_discount == 0) {
     593foreach ($order_products_datas as $order_products_data) {
     594if ($order_products_data['line_sub'] != 0) {
     595$prod_line = array(
     596$book_code,
     597$order->piecedate,
     598$order->number,
     599$order_products_data['acc'],
     600$order->lib,
     601str_replace('.',$dec_separator, abs(round($order_products_data['line_sub'],2))),
     602$fillwith,
     603$order_products_data['ana'],
     604"",
     605"",
     606"",
     607""
     608);
     609ksort($prod_line);
     610//Coupon
     611woocommerce_accounting_add_line($output, $prod_line,"\t");
     612}
     613}
     614if ($bef_tax > 0) {
     615foreach ($bef_order_products_discounts as $key => $bef_order_products_discount) {
     616$coupon_line = array(
     617$book_code,
     618$order->piecedate,
     619$order->number,
     620$order_products_datas[$key]['acc'],
     621$order->lib,
     622str_replace('.',$dec_separator, abs(round($bef_order_products_discount,2))),
     623$fillwith,
     624$order_products_datas[$key]['ana'],
     625"",
     626"",
     627"",
     628""
     629);
     630ksort($coupon_line);
     631woocommerce_accounting_add_line($output, $coupon_line,"\t");
     632}
     633                    }   
     634//Simple Products
     635}
     636elseif ($prod_opt == "0" && $order->income_prodht != 0) {
     637$prod_line = array(
     638$book_code,
     639$order->piecedate,
     640$order->number,
     641$gen_account_prod,
     642$order->lib,
     643str_replace('.',$dec_separator, abs(round($order->income_prodht,2))),
     644$fillwith,
     645$gen_analytic_prod,
     646"",
     647"",
     648"",
     649""
     650);
     651ksort($prod_line);
     652woocommerce_accounting_add_line($output, $prod_line,"\t");
     653}
     654//Simple products by country
     655elseif ($prod_opt == "2" && $order->income_prodht != 0) {
     656$order->countryid = $order->AnsOrder->get_shipping_country();
     657$order_coutry_acc = get_option('woocommerce_accounting_countries_acc');
     658$country_account_prod = $order_coutry_acc[$order->countryid]['acc'];
     659if (empty($country_account_prod)) {
     660$country_account_prod = $gen_account_prod;
     661}
     662$prod_line = array(
     663$book_code,
     664$order->piecedate,
     665$order->number,
     666$country_account_prod,
     667$order->lib,
     668str_replace('.',$dec_separator, abs(round($order->income_prodht,2))),
     669$fillwith,
     670$gen_analytic_prod,
     671"",
     672"",
     673"",
     674""
     675);
     676ksort($prod_line);
     677woocommerce_accounting_add_line($output, $prod_line,"\t");
     678}
     679               
     680//Taxes
     681if (($tax_applied_check != 0) && ($taxes_opt == "1") && ($order->order_discount == 0)) {
     682foreach ($tax_displays as $tax_display) {
     683$tax_line = array(
     684$book_code,
     685$order->piecedate,
     686$order->number,
     687$tax_display['account'],
     688$order->lib,
     689str_replace('.',$dec_separator, abs(round($tax_display['amount'],2))),
     690$fillwith,
     691"",
     692"",
     693"",
     694"",
     695""
     696);
     697ksort($tax_line);
     698woocommerce_accounting_add_line($output, $tax_line,"\t");
     699}
     700}
     701else if ($tax_line_amount != 0){
     702$tax_line = array(
     703$book_code,
     704$order->piecedate,
     705$order->number,
     706$tax_line_account,
     707$order->lib,
     708str_replace('.',$dec_separator, abs(round($tax_line_amount,2))),
     709$fillwith,
     710"",
     711"",
     712"",
     713"",
     714""
     715);
     716ksort($tax_line);
     717woocommerce_accounting_add_line($output, $tax_line,"\t");
     718}
     719// Exceptionnal Income
     720if ($chk > 0 && $chk_op > 0) {
     721$cred_line = array(
     722$book_code,
     723$order->piecedate,
     724$order->number,
     725$exptcred_acc,
     726$order->lib,
     727$fillwith,
     728str_replace('.',$dec_separator, round($chk,2)),
     729"",
     730"",
     731"",
     732"",
     733""
     734);
     735ksort($cred_line);
     736woocommerce_accounting_add_line($output, $cred_line,"\t");
     737}
     738//Exceptionnal Outcome
     739elseif ($chk > 0 && $chk_op < 0) {
     740$deb_line = array(
     741$book_code,
     742$order->piecedate,
     743$order->number,
     744$exptchar_acc,
     745$order->lib,
     746str_replace('.',$dec_separator, round($chk,2)),
     747$fillwith,
     748"",
     749"",
     750"",
     751"",
     752""
     753);
     754ksort($deb_line);
     755woocommerce_accounting_add_line($output,$deb_line,"\t");
     756}
     757// Other separator (not tab)
     758}
     759else {
     760//Alert system for more than rounding error difference
     761if ($chk > 0.03) {
     762woocommerce_accounting_add_line($output, array(
     763"ALERT !!! NEXT REFUND SEEMS TO BE A GENERIC REFUND OR TO BE AN ERROR. IT WILL BE ADDED IN AN EXCEPTIONNAL ACCOUNT. YOU SHOULD CHECK IT TO PRECISE WHAT HAS BEEN REFUNDED."
     764),
     765$separator);
     766}
     767//Customers
     768if ((!empty ($order->account_cust)) && ($order->outcome != 0))  {
     769$cust_line = array(
     770$book_code,
     771$order->piecedate,
     772$order->number,
     773$order->account_cust,
     774$order->lib,
     775$fillwith,
     776str_replace('.',$dec_separator, abs(round($order->outcome,2))),
     777"",
     778"",
     779"",
     780"",
     781""
     782);
     783ksort($cust_line);
     784woocommerce_accounting_add_line($output,$cust_line, $separator);
     785} else if ($order->outcome != 0) {
     786$cust_line = array(
     787$book_code,
     788$order->piecedate,
     789$order->number,
     790$gen_account_cust,
     791$order->lib,
     792$fillwith,
     793str_replace('.',$dec_separator, abs(round($order->outcome,2))),
     794"",
     795"",
     796"",
     797"",
     798""
     799);
     800ksort($cust_line);
     801woocommerce_accounting_add_line($output,$cust_line, $separator);
     802}
     803//Shipping
     804if ((!empty ($order_shipping_method)) && ($order->income_fdpht != 0)) {
     805$deliv_line = array(
     806$book_code,
     807$order->piecedate,
     808$order->number,
     809$order_shipping_accounting_acc,
     810$order->lib,
     811str_replace('.',$dec_separator, abs(round($order->income_fdpht,2))),
     812$fillwith,
     813$order_shipping_analytic_acc,
     814"",
     815"",
     816"",
     817""
     818);
     819ksort($deliv_line);
     820woocommerce_accounting_add_line($output, $deliv_line, $separator);
     821}
     822//Products
     823if ($prod_opt == "1" && $order->order_discount == 0) {
     824foreach ($order_products_datas as $order_products_data) {
     825if ($order_products_data['line_sub'] != 0) {
     826$prod_line = array(
     827$book_code,
     828$order->piecedate,
     829$order->number,
     830$order_products_data['acc'],
     831$order->lib,
     832str_replace('.',$dec_separator, abs(round($order_products_data['line_sub'],2))),
     833$fillwith,
     834$order_products_data['ana'],
     835"",
     836"",
     837"",
     838""
     839);
     840ksort($prod_line);
     841woocommerce_accounting_add_line($output, $prod_line, $separator);
     842}
     843}
     844// Coupons
     845if ($bef_tax > 0) {
     846foreach ($bef_order_products_discounts as $key => $bef_order_products_discount) {
     847$coupon_line = array(
     848$book_code,
     849$order->piecedate,
     850$order->number,
     851$order_products_datas[$key]['acc'],
     852$order->lib,
     853str_replace('.',$dec_separator, abs(round($bef_order_products_discount,2))),
     854$fillwith,
     855$order_products_datas[$key]['ana'],
     856"",
     857"",
     858"",
     859""
     860);
     861ksort($coupon_line);
     862woocommerce_accounting_add_line($output, $coupon_line, $separator);
     863}
     864}
     865//Simple products
     866} elseif ($prod_opt == "0" && $order->income_prodht != 0) {
     867$prod_line = array(
     868$book_code,
     869$order->piecedate,
     870$order->number,
     871$gen_account_prod,
     872$order->lib,
     873str_replace('.',$dec_separator, abs(round($order->income_prodht,2))),
     874$fillwith,
     875$gen_analytic_prod,
     876"",
     877"",
     878"",
     879""
     880);
     881ksort($prod_line);
     882woocommerce_accounting_add_line($output, $prod_line, $separator);
     883//Simple products by country
     884}
     885elseif ($prod_opt == "2" && $order->income_prodht != 0) {
     886$wc_order = wc_get_order($order->ID);
     887$original_order_id = $wc_order->get_parent_id();
     888$original_order = wc_get_order( $original_order_id );
     889$order->countryid = $original_order->get_billing_country();
     890$order_country_acc = get_option('woocommerce_accounting_countries_acc');
     891$country_account_prod = $order_country_acc[$order->countryid]['acc'];
     892if (empty($country_account_prod)) {
     893$country_account_prod = $gen_account_prod;
     894}
     895$prod_line = array(
     896$book_code,
     897$order->piecedate,
     898$order->number,
     899$country_account_prod,
     900$order->lib,
     901str_replace('.',$dec_separator, abs(round($order->income_prodht,2))),
     902$fillwith,
     903$gen_account_prod,
     904"",
     905"",
     906"",
     907""
     908);
     909ksort($prod_line);
     910woocommerce_accounting_add_line($output, $prod_line, $separator);
     911}
     912//Taxes
     913if (($tax_applied_check != 0) && ($taxes_opt == "1") && ($order->order_discount == 0)) {
     914foreach ($tax_displays as $tax_display) {
     915$tax_line = array(
     916$book_code,
     917$order->piecedate,
     918$order->number,
     919$tax_display['account'],
     920$order->lib,
     921str_replace('.',$dec_separator, abs(round($tax_display['amount'],2))),
     922$fillwith,
     923"",
     924"",
     925"",
     926"",
     927""
     928);
     929ksort($tax_line);
     930woocommerce_accounting_add_line($output, $tax_line, $separator);
     931}
     932}
     933else if ($tax_line_amount != 0){
     934$tax_line = array(
     935$book_code,
     936$order->piecedate,
     937$order->number,
     938$tax_line_account,
     939$order->lib,
     940str_replace('.',$dec_separator, abs(round($tax_line_amount,2))),
     941$fillwith,
     942"",
     943"",
     944"",
     945"",
     946""
     947);
     948ksort($tax_line);
     949woocommerce_accounting_add_line($output, $tax_line, $separator);
     950}
     951// Exceptionnal Income
     952if ($chk > 0 && $chk_op > 0) {
     953$cred_line = array(
     954$book_code,
     955$order->piecedate,
     956$order->number,
     957$exptcred_acc,
     958$order->lib,
     959$fillwith,
     960str_replace('.',$dec_separator, round($chk,2)),
     961"",
     962"",
     963"",
     964"",
     965""
     966);
     967ksort($cred_line);
     968woocommerce_accounting_add_line($output, $cred_line, $separator);
     969}
     970//Exceptionnal Outcome
     971elseif ($chk > 0 && $chk_op < 0) {
     972$deb_line = array(
     973$book_code,
     974$order->piecedate,
     975$order->number,
     976$exptchar_acc,
     977$order->lib,
     978str_replace('.',$dec_separator, round($chk,2)),
     979$fillwith,
     980"",
     981"",
     982"",
     983"",
     984""
     985);
     986ksort($deb_line);
     987woocommerce_accounting_add_line($output,$deb_line, $separator);
     988}
     989}
     990}
     991fclose($output) or die("Can't close php://output");
     992exit;
     993}
     994else {
     995wp_safe_redirect(add_query_arg([
     996'page'=>'woocommerce_accounting_exporter',
     997'format'=>'refunds',
     998'error'=>'no_orders',
     999], admin_url('admin.php')));
     1000exit;
     1001}
    10021002}
    10031003?>
Note: See TracChangeset for help on using the changeset viewer.