Plugin Directory

Changeset 3107971

Timestamp:
06/26/2024 09:43:28 AM (2 weeks ago)
Author:
bastho
Message:

Deploy stable branch - v1.6.4

Location:
accounting-for-woocommerce/tags/stable
Files:
4 edited

Legend:

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

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

    r3104457 r3107971  
    7676    $order_args = array(
    7777        'limit' => -1,
     78
    7879        'status' => $order_status,
    7980        'orderby' => 'ID',
     
    9394        //Generic accounts
    9495        $csv_headers = get_option ('woocommerce_accounting_columns_headers');
    95         $book_code = get_option('woocommerce_accounting_book_code_order');
    9696        $original_book_code = get_option('woocommerce_accounting_book_code_order');
    9797        $book_code_status = get_option('woocommerce_accounting_status_code');
     
    108108        $order_shipping_acc_list = get_option ('woocommerce_accounting_shipping_methods_accounting_account');
    109109        $pdfoptions = get_option('wpo_wcpdf_template_settings');
     110
     111
     112
     113
    110114    }
    111115
    112116    foreach ($orders as $wcOrder)
    113117    {
     118
     119
     120
     121
    114122        $order = (object) array();
     123
    115124        $order->ID = $wcOrder->get_id();
    116         $order->status = $wcOrder->get_status();
     125        $order->status = $wcOrder->get_status();
    117126        $order->date_created = $wcOrder->get_date_created();
    118127        $order->date_completed = $wcOrder->get_date_completed();
     
    139148        $order->original_date = $order->piecedate;
    140149
    141         $meta_order_tax = (float) $wcOrder->get_total_tax();
    142         $meta_order_shipping_tax = (float) intval($wcOrder->get_shipping_tax());
     150        $_tax = (float) $wcOrder->get_total_tax();
     151        $);
    143152
    144153        $order->outcome = round($wcOrder->get_total(), $rounding_precision);
    145         $order->income_tax = round($meta_order_tax > $small_number ?  $meta_order_tax : 0, $rounding_precision) + round($meta_order_shipping_tax > $small_number ?  $meta_order_shipping_tax : 0, $rounding_precision);
     154        $order->income_tax = round($shipping_tax : 0, $rounding_precision);
    146155        $order->income_fdpht = round($wcOrder->get_total_shipping(), $rounding_precision);
    147156        $order->income_prodht = ( ($order->outcome) - ( ($order->income_tax) + ($order->income_fdpht) ) );
     
    158167
    159168        $order->number = $order->ID;
    160        
     169
    161170        //Customers exports options
    162         $order->account_cust = '';
     171        $order->account_cust = ;
    163172        if ($cust_opt == "0") {
    164173            $order->account_cust = $gen_account_cust;
    165174        }
     175
     176
     177
     178
     179
    166180
    167181        // Get the country only in label if prod by country
    168182        if ($prod_opt == "2") {
    169             $country_label = get_post_meta($order->ID,'_shipping_country',true);
     183            $country_label = );
    170184        }
    171185        else {
     
    174188        //Payment gateway indication in label
    175189        if ($pay_opt == "1") {
    176             $pay_meth = substr(get_post_meta($order->ID,'_payment_method',true),0,1) . substr(get_post_meta($order->ID,'_payment_method',true), 3, 1) . substr(get_post_meta($order->ID,'_payment_method',true),-1) ;
    177             $order->lib = remove_accents (strtoupper($pay_meth) . ' ' .  get_option ('woocommerce_accounting_lib_prefix') . ' ' . strtoupper(get_post_meta($order->ID,'_billing_company',true)) . ' ' . ucfirst(get_post_meta($order->ID,'_billing_last_name',true)) . ' ' . ucfirst(get_post_meta($order->ID,'_billing_first_name',true)) . ' ' . $country_label);
     190            $pay_meth = substr(;
     191            $order->lib = remove_accents (strtoupper($pay_meth) . ' ' .  )) . ' ' . $country_label);
    178192            $order->lib = trim($order->lib);
    179193        }
    180194        elseif ($pay_opt == "2") {
    181             $order_pay_meth = get_post_meta($order->ID,'_payment_method',true);
    182             if (!empty (get_option('woocommerce_accounting_gateways_libs')[$order_pay_meth]['lib'])) {
    183                 $listed_pay_lib = get_option('woocommerce_accounting_gateways_libs')[$order_pay_meth]['lib'];
     195            if (!empty ($gateways_libs[$order->gatewayid]['lib'])) {
     196                $listed_pay_lib = $gateways_libs[$order->gatewayid]['lib'];
    184197                $pay_meth = $listed_pay_lib;
    185198            }
    186199            else {
    187                 $pay_meth = substr(get_post_meta($order->ID,'_payment_method',true),0,1) . substr(get_post_meta($order->ID,'_payment_method',true), 3, 1) . substr(get_post_meta($order->ID,'_payment_method',true),-1) ;
     200                $pay_meth = substr(;
    188201            };
    189             $order->lib = remove_accents (strtoupper($pay_meth) . ' ' .  get_option ('woocommerce_accounting_lib_prefix') . ' ' . strtoupper(get_post_meta($order->ID,'_billing_company',true)) . ' ' . ucfirst(get_post_meta($order->ID,'_billing_last_name',true)) . ' ' . ucfirst(get_post_meta($order->ID,'_billing_first_name',true)) . ' ' . $country_label);
     202            $order->lib = remove_accents (strtoupper($pay_meth) . ' ' .  )) . ' ' . $country_label);
    190203            $order->lib = trim($order->lib);
    191204        }
    192205        else {
    193             $order->lib = remove_accents (get_option ('woocommerce_accounting_lib_prefix') . ' ' . strtoupper(get_post_meta($order->ID,'_billing_company',true)) . ' ' . ucfirst(get_post_meta($order->ID,'_billing_last_name',true)) . ' ' . ucfirst(get_post_meta($order->ID,'_billing_first_name',true)) . ' ' . $country_label);
     206            $order->lib = remove_accents ()) . ' ' . $country_label);
    194207            $order->lib = trim($order->lib);
    195208        }
     
    307320            $col_map = array_flip($col_map);
    308321        }
    309         $original_book_code = get_option('woocommerce_accounting_book_code_order');
    310322
    311323        //Index table to order columns
     
    340352            }
    341353
    342             $get_order = new WC_Order ( $order->ID );
    343             $post_status = get_post_status($order->ID);
    344 
    345             if(isset($book_code_status[$post_status]) && $book_code_status[$post_status] != ""){
    346                 $book_code = $book_code_status[$post_status];
     354            $get_order = $order->WCOrder;
     355            if(isset($book_code_status[$order->status]) && $book_code_status[$order->status] != ""){
     356                $book_code = $book_code_status[$order->status];
    347357            }
    348358            else{
     
    353363            /*-----------------------------------------------------------------------------*/
    354364
    355             $meta_order_tax = (float) get_post_meta($order->ID,'_order_tax',true, 0);
    356             $meta_order_shipping_tax = (float) get_post_meta($order->ID,'_order_shipping_tax',true, 0);
    357             $tax_applied_check = ($meta_order_tax > $small_number ?  $meta_order_tax : 0) + ($meta_order_shipping_tax > $small_number ?  $meta_order_shipping_tax : 0);
     365            $tax_applied_check = ($order->total_tax > $small_number ?  $order->total_tax : 0) + ($order->shipping_tax > $small_number ?  $order->shipping_tax : 0);
    358366            // Tax application test
    359367            if (($tax_applied_check != 0) && ($taxes_opt == "1") && ($order->order_discount == 0)){
     
    505513                    $col_map['Date'] =>    $order->piecedate,
    506514                    $col_map['Inv.Number'] =>    $order->number,
    507                     $col_map['Acc.Code'] =>    isset($book_account_status[$post_status]) ? $book_account_status[$post_status] : $gen_account_cust,
     515                    $col_map['Acc.Code'] =>    isset($book_account_status[$status] : $gen_account_cust,
    508516                    $col_map['Label'] =>    $order->lib,
    509517                    $col_map['Outcome'] =>    woocommerce_accounting_format_number(round($order->outcome, $rounding_precision)),
     
    637645                $wc_order = wc_get_order($order->ID);
    638646                $order->countryid = $wc_order->get_billing_country();
    639                 $order_country_acc = get_option('woocommerce_accounting_countries_acc');
    640                 $country_account_prod = $order_country_acc[$order->countryid]['acc'];
    641                 if (empty($country_account_prod)) {
    642                     $country_account_prod = $gen_account_prod;
     647                $country_account_prod = $gen_account_prod;
     648                if (!empty($order_country_acc[$order->countryid]['acc'])) {
     649                    $country_account_prod = $order_country_acc[$order->countryid]['acc'];
    643650                }
    644651                if (round($order->income_prodht, $rounding_precision) > 0) {
  • accounting-for-woocommerce/tags/stable/inc/utils.php

    r3104457 r3107971  
    1818 * @return string
    1919 */
    20 function woocommerce_accounting_format_number($number) : string{
     20function woocommerce_accounting_format_number($number){
    2121    global $rounding_precision;
    2222    $dec_separator = get_option('woocommerce_accounting_export_decimal_separator');
     
    3838 * @return float
    3939 */
    40 function woocommerce_accounting_unformat_number($string) : float{
     40function woocommerce_accounting_unformat_number($string){
    4141    return (float) str_replace(',', '.', $string);
    4242}
  • accounting-for-woocommerce/tags/stable/readme.txt

    r3104457 r3107971  
    7272- Optimize data fetching
    7373- Use main methods of WC_Order to get data
     74
     75
     76
     77
    7478- Ensure variable types for PHP8.3
     79
    7580
    7681### 1.6.3
Note: See TracChangeset for help on using the changeset viewer.