Changeset 1729806
- Timestamp:
- 09/14/2017 10:27:38 AM (9 years ago)
- Location:
- woo-payjoe-beleg-schnittstelle/trunk
- Files:
-
- 3 edited
-
README.txt (modified) (1 diff)
-
admin/class-weslink-payjoe-opbeleg-orders.php (modified) (2 diffs)
-
weslink-payjoe-opbeleg.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
woo-payjoe-beleg-schnittstelle/trunk/README.txt
r1718746 r1729806 75 75 * Verzeichnisse werden nun im WordPress-Stil aufgerufen 76 76 77 = 1.2.2 = 78 * Mehr Stabilität beim manuellen Upload von vielen Bestellungen 79 77 80 == Upgrade Notice == 78 81 79 82 = 1.0.0 = 80 83 * Einreichen der erste Versions auf WordPress.org 84 85 = 1.0.1 = 86 * Erste Version des Plugins 87 * Support für WP Overnight WooCommerce PDF Invoices & Packing Slips 88 89 = 1.2.0 = 90 * Support für WooCommerce Germanized hinzugefügt 91 92 = 1.2.1 = 93 * Verzeichnisse werden nun im WordPress-Stil aufgerufen 94 95 = 1.2.2 = 96 * Mehr Stabilität beim manuellen Upload von vielen Bestellungen 81 97 82 98 == Contribute == -
woo-payjoe-beleg-schnittstelle/trunk/admin/class-weslink-payjoe-opbeleg-orders.php
r1718741 r1729806 12 12 */ 13 13 14 require_once plugin_dir_path(__FILE__) . '/../helpers/helpers.php'; 15 require_once plugin_dir_path(__FILE__) . '/partials/constants.php'; 16 17 class Weslink_Payjoe_Opbeleg_Orders 18 { 19 /** 20 * Returns order details 21 */ 22 public function get_orders( $log_json_data = false ) 23 { 24 $args = array( 25 'post_type' => wc_get_order_types() 26 , 'posts_per_page' => -1 27 , 'post_status' => array_keys( wc_get_order_statuses() ) 28 ); 29 30 31 /* Deccide which plugin is used for Invoicing 32 * get option 33 * '0' => 'WP Overnight WooCommerce PDF Invoices & Packing Slips', 34 * '1' => 'WooCommerce Germanized', 35 */ 36 37 if ( get_option('payjoe_invoice_options') == 0) { 38 $invoice_number_field_key = '_wcpdf_invoice_number'; 39 $invoice_date_field_key = '_wcpdf_invoice_date'; 40 41 }elseif (get_option('payjoe_invoice_options') == 1){ 42 $this->mapGermanizedInvoiceNumber(); 43 $invoice_number_field_key = '_payjoe_invoice_number'; 44 $invoice_date_field_key = '_payjoe_invoice_date'; 45 } 46 47 48 49 50 $args['meta_query'] = array( 51 'relation' => 'AND' 52 , array( 53 'key' => $invoice_number_field_key, 54 'value' => (int)get_option('payjoe_startrenr'), 55 'type' => 'NUMERIC', 56 'compare' => '>=' 57 ) 58 , array( 59 array( 60 'relation' => 'OR' 61 , array( 62 'key' => '_payjoe_status', 63 'value' => (int)PAYJOE_STATUS_OK, 64 'type' => 'NUMERIC', 65 'compare' => '!=' // already submitted to API but error 66 ) 67 , array( 68 'key' => '_payjoe_status', 69 'compare' => 'NOT EXISTS', // not submitted to API yet 70 ) 71 ) 72 ) 73 ); 74 75 $orders = new WP_Query($args); 76 77 if ($orders->have_posts()) { 78 79 /** 80 * This will be returned data 81 */ 82 $ctwOrderData = array(); 83 $ctwOrderData['orders'] = array(); 84 $invoice_number = null; 85 /** 86 * Loop over each order 87 */ 88 while ($orders->have_posts()) { 89 $orders->the_post(); 90 $invoice_number = get_post_meta( get_the_ID(), $invoice_number_field_key, true ); 91 92 //echo "ID: ".get_the_ID(); 93 //echo " INV: ".$invoice_number; 94 //echo ">DEBUG ENDE \n\n!"; 95 //exit(); 96 97 $order_details = new WC_Order(get_the_ID()); 98 $OPBelegUpload = array( 99 'UserName' => get_option('payjoe_username'), 100 'APIKey' => get_option('payjoe_apikey'), 101 'OPBelegZugangID' => intval( get_option('payjoe_zugangsid') ), 102 'OPAuftragsposten' => array() 103 ); 104 105 // OPBeleg - invoice 106 $currency_code = Weslink_Payjoe_Helpers::get_currency_code( $order_details->get_order_currency() ); 107 $OPBelegUpload['OPAuftragsposten'] = array(); //just one OPAUftragsposten at a OPBelegUpload 108 109 $obj_op_auftragsposten = array(); 110 111 $OPBelegdatum = get_post_meta( get_the_ID(), $invoice_date_field_key, true ); 112 113 $OPBelegdatum = ( $OPBelegdatum ) ? $OPBelegdatum : ''; 114 if ( $OPBelegdatum ) { 115 $m_datetime = new DateTime( $OPBelegdatum ); 116 $OPBelegdatum = $m_datetime->format( 'c' ); 117 } 118 119 $OPBelegdatum = get_post_meta( get_the_ID(), $invoice_date_field_key, true ); 120 if ($OPBelegdatum ==""){ 121 //This field seesm to be emtpy, so we use the order date instead. 122 $OPBelegdatum = $order_details->order_date; 123 } 124 125 $obj_op_auftragsposten['OPBeleg'] = array( 126 'OPBelegZugangID' => intval( get_option('payjoe_zugangsid') ), 127 'OPBelegtyp' => 0, //0= rechnung, 1= gutschrift 128 'OPZahlungsArt' => $order_details->payment_method, 129 'OPBelegHerkunft' => "woocommerce", 130 'OPBelegdatum' => $OPBelegdatum, 131 'OPBelegNummer' => $invoice_number, //_wcpdf_invoice_number 132 'OPBelegKundenNr' => $order_details->customer_user, // get_post_meta(get_the_ID, '_customer_user', true); 133 //'OPBelegDebitorenNr' => "", // ? 134 'OPBelegBestellNr' => $order_details->get_order_number(), 135 'OPBelegWaehrung' => intval( $currency_code ? $currency_code : '' ), // currency code 136 //'OPBelegUstID' => "", // VAT ID <--- is customer VAT ID ? 137 'OPBelegTransaktionsID' => $order_details->get_transaction_id(), 138 //'OPBelegFaelligBis' => "", // ? due date 139 //'OPBelegReferenz1' => $order_details->payment_method_title, 140 // 'OPBelegReferenz2' => "", 141 // 'OPBelegReferenz3' => "", 142 // 'OPBelegReferenz4' => "", 143 // 'OPBelegReferenz5' => "" 144 ); 145 146 // OPBelegLieferadresse - delivery address 147 $obj_op_auftragsposten['OPBelegLieferadresse'] = array( 148 'OPBelegAdresseLand' => $order_details->shipping_country, 149 'OPBelegAdresseFirma' => $order_details->shipping_company, 150 'OPBelegAdresseName' => $order_details->shipping_last_name, 151 'OPBelegAdresseVorname' => $order_details->shipping_first_name, 152 'OPBelegAdresseEmail' => $order_details->billing_email, //not available, so we reuse here the billing address 153 'OPBelegAdresseStrasse' => $order_details->shipping_address_1.' '.$order_details->shipping_address_2, 154 'OPBelegAdressePLZ' => $order_details->shipping_postcode, 155 'OPBelegAdresseOrt' => $order_details->shipping_city 156 ); 157 158 // OPBelegRechnungsadresse - billing address 159 $obj_op_auftragsposten['OPBelegRechnungsadresse'] = array( 160 'OPBelegAdresseLand' => $order_details->billing_country, 161 'OPBelegAdresseFirma' => $order_details->billing_company, 162 'OPBelegAdresseName' => $order_details->billing_last_name, 163 'OPBelegAdresseVorname' => $order_details->billing_first_name, 164 'OPBelegAdresseEmail' => $order_details->billing_email, 165 'OPBelegAdresseStrasse' => $order_details->billing_address_1.' '.$order_details->billing_address_2, 166 'OPBelegAdressePLZ' => $order_details->billing_postcode, 167 'OPBelegAdresseOrt' => $order_details->billing_city 168 ); 169 170 // OPBelegpositionen - invoice items 171 $obj_op_auftragsposten['OPBelegpositionen'] = array(); 172 173 // create OPBelegposition Objects 174 $total_net_amount = 0; 175 $items = $order_details->get_items(); 176 $taxes = $order_details->get_taxes(); 177 178 $OPBelegpositions = array(); 179 180 // tax group 181 foreach( $items as $item_id => $item ) { 182 $line_tax_data = isset( $item['line_tax_data'] ) ? $item['line_tax_data'] : ''; 183 $tax_data = maybe_unserialize( $line_tax_data ); 184 $item_total = $item[ 'line_total' ]; 185 186 // in case discount/coupon is applied, use 'subtotal' instead 187 if ( isset( $item['line_subtotal'] ) && $item['line_subtotal'] != $item['line_total'] ) { 188 $item_total = $item[ 'line_subtotal' ]; 189 } 190 191 $tax_item_id = 0; 192 $tax_item_total = 0; 193 foreach( $taxes as $tax_item ) { 194 $tmp_rate_id = $tax_item['rate_id']; 195 $tax_item_total = isset( $tax_data['total'][ $tmp_rate_id ] ) ? $tax_data['total'][ $tmp_rate_id ] : null; 196 $tax_item_subtotal = isset( $tax_data['subtotal'][ $tmp_rate_id ] ) ? $tax_data['subtotal'][ $tmp_rate_id ] : null; 197 198 if ( $tax_item_total && $tax_item_subtotal ) { 199 $tax_item_id = $tmp_rate_id; 200 // in case discount/coupon is applied, use 'subtotal' instead 201 if ( isset( $tax_item_subtotal ) && $tax_item_subtotal != $tax_item_total ) { 202 $tax_item_total = $tax_item_subtotal; 203 } 204 205 break; 206 } 207 } 208 209 // guess tax rate 210 $tax_rate = 0; 211 if ( $item_total && $tax_item_total ) { 212 $tax_rate = round( ( $tax_item_total / $item_total ) * 100, 4 ); 213 } 214 215 // collect VAT Group info 216 if ( !isset( $OPBelegpositions[ $tax_item_id ] ) ) { 217 $OPBelegpositions[ $tax_item_id ] = array( 218 'OPBelegBuchungstext' => 0, 219 //'OPSteuerschluessel' => '', 220 //'OPBelegKostenstelle' => '', 221 //'OPBelegKostentraeger' => '', 222 'OPBelegPostenGesamtNetto' => 0, 223 'OPBelegPostenGesamtBrutto' => 0, 224 'OPBelegSteuersatz' => $tax_rate, 225 //'OPBelegHabenKonto' => '' 226 ); 227 } 228 229 $OPBelegpositions[ $tax_item_id ][ 'OPBelegPostenGesamtNetto' ] += $item_total; 230 $OPBelegpositions[ $tax_item_id ][ 'OPBelegPostenGesamtBrutto' ] += $item_total + $tax_item_total; 231 232 $total_net_amount += $item_total; 233 } 234 235 // discount / coupon amount 236 $total_net_discounted_amount = $order_details->cart_discount; 237 $discount_group = array(); 238 foreach ( $OPBelegpositions as $tax_id => $each_OPBelegposition ) { 239 $OPBelegPostenGesamtNetto = ( ( $each_OPBelegposition[ 'OPBelegPostenGesamtNetto' ] / ( $total_net_amount / 100 ) ) / 100 ) * $total_net_discounted_amount; 240 $OPBelegPostenGesamtBrutto = $OPBelegPostenGesamtNetto + ( $OPBelegPostenGesamtNetto * $each_OPBelegposition[ 'OPBelegSteuersatz' ] / 100 ); 241 if($OPBelegPostenGesamtNetto == 0) { 242 //skip lines with 0-values 243 continue; 244 } 245 $discount_group[ $tax_id ] = array( 246 'OPBelegBuchungstext' => 1, 247 //'OPSteuerschluessel' => '', 248 //'OPBelegKostenstelle' => '', 249 //'OPBelegKostentraeger' => '', 250 'OPBelegPostenGesamtNetto' => round( $OPBelegPostenGesamtNetto, 2 ), 251 'OPBelegPostenGesamtBrutto' => round( $OPBelegPostenGesamtBrutto, 2 ), 252 'OPBelegSteuersatz' => $each_OPBelegposition[ 'OPBelegSteuersatz' ] 253 ); 254 } 255 256 /* 257 * TODO: add seperate lines for each shipping and coupon tax class 258 * currently the wc api gives us just one tax information for alle shipping item. 259 */ 260 // shipping amount 261 $total_shipping = $order_details->get_total_shipping(); 262 if( $total_shipping ) { 263 $shipping_tax_rate = round( ( $order_details->get_shipping_tax() / $total_shipping ) * 100, 2 ); 264 $OPBelegpositions[ 'shipping' ] = array( 265 'OPBelegBuchungstext' => 2, 266 //'OPSteuerschluessel' => '', 267 //'OPBelegKostenstelle' => '', 268 //'OPBelegKostentraeger' => '', 269 'OPBelegPostenGesamtNetto' => $total_shipping, 270 'OPBelegPostenGesamtBrutto' => $total_shipping*($shipping_tax_rate/100+1), 271 'OPBelegSteuersatz' => $shipping_tax_rate, 272 // 'OPBelegHabenKonto' => '' //if this is not submitted it crashes 273 ); 274 } 275 276 // add discount info 277 foreach ( $discount_group as $each_discount_group ) { 278 array_push( $OPBelegpositions, $each_discount_group ); 279 } 280 281 $obj_op_auftragsposten['OPBelegpositionen'] = array_values( $OPBelegpositions ); 282 283 array_push($OPBelegUpload['OPAuftragsposten'], $obj_op_auftragsposten); 284 285 //send order to payJoe 286 $result = $this->uploadBelegtoPayJoe( $OPBelegUpload,$log_json_data ); 287 $this->handleAPIResult( $result, get_the_ID(), $invoice_number,$log_json_data ); 288 289 if ( $log_json_data ) { 290 echo "\n ---------------------------- NEXT ORDER ----------------------------\n"; 291 echo "\n ---------------------------- API RESULT----------------------------\n"; 292 echo json_encode($result); 293 echo "\n ---------------------------- JSON DATA ----------------------------\n"; 294 echo json_encode($OPBelegUpload); 295 echo "\n --------------------------------------------------------------------\n"; 296 echo "\n -------------------------- Data array view -------------------------\n"; 297 print_r($OPBelegUpload); 298 echo "\n --------------------------------------------------------------------\n"; 299 } 300 301 // save to global orders 302 $ctwOrderData[ 'orders' ][ get_the_ID() ] = $OPBelegUpload; 303 } 304 wp_reset_postdata(); 305 306 $json_data = json_encode($ctwOrderData['orders']); 307 308 return $json_data; 309 310 } else { 311 _e( "No orders availlabe for upload to PayJoe", 'weslink-payjoe-opbeleg' ); //new WP_Error('no_orders', __('Keine Betellungen für den Export vorhanden.', 'woocommerce-simply-order-export')); 312 } 313 } 14 require_once plugin_dir_path( __FILE__ ) . '/../helpers/helpers.php'; 15 require_once plugin_dir_path( __FILE__ ) . '/partials/constants.php'; 16 17 class Weslink_Payjoe_Opbeleg_Orders { 18 /** 19 * Returns order details 20 */ 21 public function get_orders( $log_json_data = false ) { 22 23 $args = array( 24 'post_type' => wc_get_order_types(), 25 'posts_per_page' => 100, 26 'post_status' => array_keys( wc_get_order_statuses() ) 27 ); 28 29 30 /* Deccide which plugin is used for Invoicing 31 * get option 32 * '0' => 'WP Overnight WooCommerce PDF Invoices & Packing Slips', 33 * '1' => 'WooCommerce Germanized', 34 */ 35 36 if ( get_option( 'payjoe_invoice_options' ) == 0 ) { 37 $invoice_number_field_key = '_wcpdf_invoice_number'; 38 $invoice_date_field_key = '_wcpdf_invoice_date'; 39 40 } elseif ( get_option( 'payjoe_invoice_options' ) == 1 ) { 41 42 while ( $this->mapGermanizedInvoiceNumber() ) { 43 // while invoices are there to be mappend, map them :-) 44 }; 45 $invoice_number_field_key = '_payjoe_invoice_number'; 46 $invoice_date_field_key = '_payjoe_invoice_date'; 47 } 48 49 50 $args['meta_query'] = array( 51 'relation' => 'AND' 52 , 53 array( 54 'key' => $invoice_number_field_key, 55 'value' => (int) get_option( 'payjoe_startrenr' ), 56 'type' => 'NUMERIC', 57 'compare' => '>=' 58 ) 59 , 60 array( 61 array( 62 'relation' => 'OR' 63 , 64 array( 65 'key' => '_payjoe_status', 66 'value' => (int) PAYJOE_STATUS_OK, 67 'type' => 'NUMERIC', 68 'compare' => '!=' // already submitted to API but error 69 ) 70 , 71 array( 72 'key' => '_payjoe_status', 73 'compare' => 'NOT EXISTS', // not submitted to API yet 74 ) 75 ) 76 ) 77 ); 78 $orders = new WP_Query( $args ); 79 if ( $orders->have_posts() ) { 80 81 /** 82 * This will be returned data 83 */ 84 $ctwOrderData = array(); 85 $ctwOrderData['orders'] = array(); 86 $invoice_number = null; 87 /** 88 * Loop over each order 89 */ 90 while ( $orders->have_posts() ) { 91 $orders->the_post(); 92 $invoice_number = get_post_meta( get_the_ID(), $invoice_number_field_key, true ); 93 94 //echo "ID: ".get_the_ID(); 95 //echo " INV: ".$invoice_number; 96 //echo ">DEBUG ENDE \n\n!"; 97 //exit(); 98 99 $order_details = new WC_Order( get_the_ID() ); 100 $OPBelegUpload = array( 101 'UserName' => get_option( 'payjoe_username' ), 102 'APIKey' => get_option( 'payjoe_apikey' ), 103 'OPBelegZugangID' => intval( get_option( 'payjoe_zugangsid' ) ), 104 'OPAuftragsposten' => array() 105 ); 106 107 // OPBeleg - invoice 108 $currency_code = Helpers::get_currency_code( $order_details->get_order_currency() ); 109 $OPBelegUpload['OPAuftragsposten'] = array(); //just one OPAUftragsposten at a OPBelegUpload 110 111 $obj_op_auftragsposten = array(); 112 113 $OPBelegdatum = get_post_meta( get_the_ID(), $invoice_date_field_key, true ); 114 115 $OPBelegdatum = ( $OPBelegdatum ) ? $OPBelegdatum : ''; 116 if ( $OPBelegdatum ) { 117 $m_datetime = new DateTime( $OPBelegdatum ); 118 $OPBelegdatum = $m_datetime->format( 'c' ); 119 } 120 121 $OPBelegdatum = get_post_meta( get_the_ID(), $invoice_date_field_key, true ); 122 if ( $OPBelegdatum == "" ) { 123 //This field seesm to be emtpy, so we use the order date instead. 124 $OPBelegdatum = $order_details->order_date; 125 } 126 127 $obj_op_auftragsposten['OPBeleg'] = array( 128 'OPBelegZugangID' => intval( get_option( 'payjoe_zugangsid' ) ), 129 'OPBelegtyp' => 0, 130 //0= rechnung, 1= gutschrift 131 'OPZahlungsArt' => $order_details->payment_method, 132 'OPBelegHerkunft' => "woocommerce", 133 'OPBelegdatum' => $OPBelegdatum, 134 'OPBelegNummer' => $invoice_number, 135 //_wcpdf_invoice_number 136 'OPBelegKundenNr' => $order_details->customer_user, 137 // get_post_meta(get_the_ID, '_customer_user', true); 138 //'OPBelegDebitorenNr' => "", // ? 139 'OPBelegBestellNr' => $order_details->get_order_number(), 140 'OPBelegWaehrung' => intval( $currency_code ? $currency_code : '' ), 141 // currency code 142 //'OPBelegUstID' => "", // VAT ID <--- is customer VAT ID ? 143 'OPBelegTransaktionsID' => $order_details->get_transaction_id(), 144 //'OPBelegFaelligBis' => "", // ? due date 145 //'OPBelegReferenz1' => $order_details->payment_method_title, 146 // 'OPBelegReferenz2' => "", 147 // 'OPBelegReferenz3' => "", 148 // 'OPBelegReferenz4' => "", 149 // 'OPBelegReferenz5' => "" 150 ); 151 152 // OPBelegLieferadresse - delivery address 153 $obj_op_auftragsposten['OPBelegLieferadresse'] = array( 154 'OPBelegAdresseLand' => $order_details->shipping_country, 155 'OPBelegAdresseFirma' => $order_details->shipping_company, 156 'OPBelegAdresseName' => $order_details->shipping_last_name, 157 'OPBelegAdresseVorname' => $order_details->shipping_first_name, 158 'OPBelegAdresseEmail' => $order_details->billing_email, 159 //not available, so we reuse here the billing address 160 'OPBelegAdresseStrasse' => $order_details->shipping_address_1 . ' ' . $order_details->shipping_address_2, 161 'OPBelegAdressePLZ' => $order_details->shipping_postcode, 162 'OPBelegAdresseOrt' => $order_details->shipping_city 163 ); 164 165 // OPBelegRechnungsadresse - billing address 166 $obj_op_auftragsposten['OPBelegRechnungsadresse'] = array( 167 'OPBelegAdresseLand' => $order_details->billing_country, 168 'OPBelegAdresseFirma' => $order_details->billing_company, 169 'OPBelegAdresseName' => $order_details->billing_last_name, 170 'OPBelegAdresseVorname' => $order_details->billing_first_name, 171 'OPBelegAdresseEmail' => $order_details->billing_email, 172 'OPBelegAdresseStrasse' => $order_details->billing_address_1 . ' ' . $order_details->billing_address_2, 173 'OPBelegAdressePLZ' => $order_details->billing_postcode, 174 'OPBelegAdresseOrt' => $order_details->billing_city 175 ); 176 177 // OPBelegpositionen - invoice items 178 $obj_op_auftragsposten['OPBelegpositionen'] = array(); 179 180 // create OPBelegposition Objects 181 $total_net_amount = 0; 182 $items = $order_details->get_items(); 183 $taxes = $order_details->get_taxes(); 184 185 $OPBelegpositions = array(); 186 187 // tax group 188 foreach ( $items as $item_id => $item ) { 189 $line_tax_data = isset( $item['line_tax_data'] ) ? $item['line_tax_data'] : ''; 190 $tax_data = maybe_unserialize( $line_tax_data ); 191 $item_total = $item['line_total']; 192 193 // in case discount/coupon is applied, use 'subtotal' instead 194 if ( isset( $item['line_subtotal'] ) && $item['line_subtotal'] != $item['line_total'] ) { 195 $item_total = $item['line_subtotal']; 196 } 197 198 $tax_item_id = 0; 199 $tax_item_total = 0; 200 foreach ( $taxes as $tax_item ) { 201 $tmp_rate_id = $tax_item['rate_id']; 202 $tax_item_total = isset( $tax_data['total'][ $tmp_rate_id ] ) ? $tax_data['total'][ $tmp_rate_id ] : null; 203 $tax_item_subtotal = isset( $tax_data['subtotal'][ $tmp_rate_id ] ) ? $tax_data['subtotal'][ $tmp_rate_id ] : null; 204 205 if ( $tax_item_total && $tax_item_subtotal ) { 206 $tax_item_id = $tmp_rate_id; 207 // in case discount/coupon is applied, use 'subtotal' instead 208 if ( isset( $tax_item_subtotal ) && $tax_item_subtotal != $tax_item_total ) { 209 $tax_item_total = $tax_item_subtotal; 210 } 211 212 break; 213 } 214 } 215 216 // guess tax rate 217 $tax_rate = 0; 218 if ( $item_total && $tax_item_total ) { 219 $tax_rate = round( ( $tax_item_total / $item_total ) * 100, 4 ); 220 } 221 222 // collect VAT Group info 223 if ( ! is_array( $OPBelegpositions ) ) { 224 $OPBelegpositions = array(); 225 } 226 if ( ! isset( $OPBelegpositions[ $tax_item_id ] ) ) { 227 $OPBelegpositions[ $tax_item_id ] = array( 228 'OPBelegBuchungstext' => 0, 229 //'OPSteuerschluessel' => '', 230 //'OPBelegKostenstelle' => '', 231 //'OPBelegKostentraeger' => '', 232 'OPBelegPostenGesamtNetto' => 0, 233 'OPBelegPostenGesamtBrutto' => 0, 234 'OPBelegSteuersatz' => $tax_rate, 235 //'OPBelegHabenKonto' => '' 236 ); 237 } 238 239 $OPBelegpositions[ $tax_item_id ]['OPBelegPostenGesamtNetto'] += $item_total; 240 $OPBelegpositions[ $tax_item_id ]['OPBelegPostenGesamtBrutto'] += $item_total + $tax_item_total; 241 242 $total_net_amount += $item_total; 243 } 244 245 // discount / coupon amount 246 $total_net_discounted_amount = $order_details->cart_discount; 247 $discount_group = array(); 248 foreach ( $OPBelegpositions as $tax_id => $each_OPBelegposition ) { 249 $OPBelegPostenGesamtNetto = ( ( $each_OPBelegposition['OPBelegPostenGesamtNetto'] / ( $total_net_amount / 100 ) ) / 100 ) * $total_net_discounted_amount; 250 $OPBelegPostenGesamtBrutto = $OPBelegPostenGesamtNetto + ( $OPBelegPostenGesamtNetto * $each_OPBelegposition['OPBelegSteuersatz'] / 100 ); 251 if ( $OPBelegPostenGesamtNetto == 0 ) { 252 //skip lines with 0-values 253 continue; 254 } 255 $discount_group[ $tax_id ] = array( 256 'OPBelegBuchungstext' => 1, 257 //'OPSteuerschluessel' => '', 258 //'OPBelegKostenstelle' => '', 259 //'OPBelegKostentraeger' => '', 260 'OPBelegPostenGesamtNetto' => round( $OPBelegPostenGesamtNetto, 2 ), 261 'OPBelegPostenGesamtBrutto' => round( $OPBelegPostenGesamtBrutto, 2 ), 262 'OPBelegSteuersatz' => $each_OPBelegposition['OPBelegSteuersatz'] 263 ); 264 } 265 266 /* 267 * TODO: add seperate lines for each shipping and coupon tax class 268 * currently the wc api gives us just one tax information for alle shipping item. 269 */ 270 // shipping amount 271 $total_shipping = $order_details->get_total_shipping(); 272 if ( $total_shipping ) { 273 $shipping_tax_rate = round( ( $order_details->get_shipping_tax() / $total_shipping ) * 100, 2 ); 274 $OPBelegpositions['shipping'] = array( 275 'OPBelegBuchungstext' => 2, 276 //'OPSteuerschluessel' => '', 277 //'OPBelegKostenstelle' => '', 278 //'OPBelegKostentraeger' => '', 279 'OPBelegPostenGesamtNetto' => $total_shipping, 280 'OPBelegPostenGesamtBrutto' => $total_shipping * ( $shipping_tax_rate / 100 + 1 ), 281 'OPBelegSteuersatz' => $shipping_tax_rate, 282 // 'OPBelegHabenKonto' => '' //if this is not submitted it crashes 283 ); 284 } 285 286 // add discount info 287 foreach ( $discount_group as $each_discount_group ) { 288 array_push( $OPBelegpositions, $each_discount_group ); 289 } 290 291 $obj_op_auftragsposten['OPBelegpositionen'] = array_values( $OPBelegpositions ); 292 293 array_push( $OPBelegUpload['OPAuftragsposten'], $obj_op_auftragsposten ); 294 295 //send order to payJoe 296 $result = $this->uploadBelegtoPayJoe( $OPBelegUpload, $log_json_data ); 297 $this->handleAPIResult( $result, get_the_ID(), $invoice_number, $log_json_data ); 298 299 if ( $log_json_data ) { 300 echo "\n ---------------------------- NEXT ORDER ----------------------------\n"; 301 echo "\n ---------------------------- API RESULT----------------------------\n"; 302 echo json_encode( $result ); 303 echo "\n ---------------------------- JSON DATA ----------------------------\n"; 304 echo json_encode( $OPBelegUpload ); 305 echo "\n --------------------------------------------------------------------\n"; 306 echo "\n -------------------------- Data array view -------------------------\n"; 307 print_r( $OPBelegUpload ); 308 echo "\n --------------------------------------------------------------------\n"; 309 } 310 311 // save to global orders 312 $ctwOrderData['orders'][ get_the_ID() ] = $OPBelegUpload; 313 } 314 wp_reset_postdata(); 315 316 $json_data = json_encode( $ctwOrderData['orders'] ); 317 318 return $json_data; 319 320 } else { 321 _e( "No orders availlabe for upload to PayJoe", 'weslink-payjoe-opbeleg' ); //new WP_Error('no_orders', __('Keine Betellungen für den Export vorhanden.', 'woocommerce-simply-order-export')); 322 } 323 } 314 324 315 325 /* 316 326 * 317 327 */ 318 function mapGermanizedInvoiceNumber() {328 function mapGermanizedInvoiceNumber() { 319 329 $args = array( 320 'post_type' => wc_get_order_types(), 321 'post_status' => array_keys( wc_get_order_statuses() ), 322 'posts_per_page' => -1, 323 'meta_query' => array( 330 'post_type' => wc_get_order_types(), 331 'post_status' => array_keys( wc_get_order_statuses() ), 332 'posts_per_page' => 500, 333 'meta_query' => array( 334 'relation' => 'AND', 324 335 array( 325 336 'key' => '_payjoe_invoice_number', 326 337 'compare' => 'NOT EXISTS' 338 ), 339 array( 340 'key' => '_invoices', 341 'compare' => 'EXISTS' 342 //'value' => '' 327 343 ) 344 328 345 ) 329 346 ); 330 347 348 331 349 $the_query = new WP_Query( $args ); 350 351 if ( $the_query->have_posts() == false ) { 352 return false; 353 } 354 355 echo $the_query->post_count . " Bestellungen von Germanized für PayJoe Vorbereitet."; 332 356 // The Loop 333 357 if ( $the_query->have_posts() ) { … … 336 360 $orderID = get_the_ID(); 337 361 338 $belegeArray = get_post_meta($orderID,'_invoices',true); 339 340 If(!$belegeArray) continue; 341 foreach ($belegeArray as $belegID){ 342 if(get_post_meta($belegID,'_type',true) == 'simple'){ 343 $invoice_number = get_post_meta($belegID,'_invoice_number',true); 344 $invoice_date = get_the_date('c',$belegID); 345 update_post_meta($orderID,'_payjoe_invoice_number',$invoice_number ); 346 update_post_meta($orderID,'_payjoe_invoice_date',$invoice_date ); 347 } 348 } 349 362 363 $belegeArray = get_post_meta( $orderID, '_invoices', true ); 364 365 If ( ! $belegeArray ) { 366 continue; 367 } 368 369 370 foreach ( $belegeArray as $belegID ) { 371 if ( get_post_meta( $belegID, '_type', true ) == 'simple' ) { 372 $invoice_number = get_post_meta( $belegID, '_invoice_number', true ); 373 $invoice_date = get_the_date( 'c', $belegID ); 374 update_post_meta( $orderID, '_payjoe_invoice_number', $invoice_number ); 375 update_post_meta( $orderID, '_payjoe_invoice_date', $invoice_date ); 376 } 377 } 350 378 351 379 } 352 353 380 /* Restore original Post Data */ 354 381 wp_reset_postdata(); 355 382 } 383 384 return true; 356 385 } 357 386 358 function handleAPIResult($result, $post_id, $invoice_number, $log_json_data=false){ 359 $result = trim( $result ); 360 361 $msg = array( 362 'error' => false, 363 'info' => null 364 ); 365 366 if( $result ) { 367 $result = json_decode($result, true); 368 369 if( $result['Erfolgreich'] == 1) { 370 //all was good! 371 $msg[ 'info' ] = __( 'Submit success', 'weslink-payjoe-opbeleg' ); 372 }else { 373 //there was an error 374 foreach( $result['Fehlerliste'] as $e_error ) { 375 $error_string = "\n- " . $e_error[ 'OPBelegBuchungstext' ]; 376 unset( $e_error[ 'OPBelegBuchungstext' ] ); 377 foreach( $e_error as $key => $val ) { 378 if( $key == 'OPBelegErrorReasons' ) { 379 $error_string .= sprintf("\n+ %s", $key ); 380 foreach( $val as $sub_val ) { 381 foreach( $sub_val as $sub_key => $subs_val ) { 382 $error_string .= sprintf("\n++ %s: %s", $sub_key, $subs_val ); 383 } 384 } 385 } else { 386 $error_string .= sprintf("\n+ %s: %s", $key, $val ); 387 } 388 } 389 390 if( !isset( $msg[ 'error' ] ) ) { 391 $msg[ 'error' ] = ''; 392 } 393 $msg[ 'error' ] .= $error_string; 394 } 395 } 396 } else { 397 $msg[ 'error' ] = __( 'No data responded from API Server.', 'weslink-payjoe-opbeleg' ) .json_encode($result); 398 } 399 400 if($msg['error']){ 401 $tpl = "<span style='color:red'>%s: %s</span>\n\n"; 402 $err_string = sprintf( $tpl, __("Error"), $msg[ 'error' ] ); 403 $this->send_error_notfication_to_admin($err_string); 404 echo $err_string; 405 }else{ 406 $tpl = "<span style='color:green'>%s</span>\n\n"; 407 echo sprintf( $tpl, __("Success"), $msg[ 'info' ] ); 408 } 409 // inform the hooks 410 do_action( 'weslink-payjoe-opbeleg-post-upload', $msg, $post_id, $invoice_number ); //call actions listening for "weslink-payjoe-opbeleg-post-upload" (update_latest_processed_invoice_number, update_payjoe_status 411 } 412 413 function uploadBelegtoPayJoe($data) 414 { 415 $url = 'https://api.payjoe.de/api/opbelegupload'; 416 $curl = curl_init($url); 417 $data = json_encode($data); 418 419 $options = array( 420 CURLOPT_POSTFIELDS => $data, 421 CURLOPT_POST => 1, 422 CURLOPT_RETURNTRANSFER => 1, 423 CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks 424 CURLOPT_HTTPHEADER => array( 425 'Content-Type: application/json', 426 'Content-Length: ' . strlen($data) 427 ) 428 ); 429 curl_setopt_array( $curl, $options ); 430 $result = curl_exec($curl); 431 curl_close($curl); 432 433 return $result; 434 } 435 436 437 function send_error_notfication_to_admin($message){ 438 $message = "This is an automatic email from the Woocommerce PayJoe Plugin. There has been an error with the Pajoe Upload: \n \n'.$message.'\n If you have enabled debugging, you can check the logfiles at uploads/payjoe/ to get more information."; 439 $to = get_bloginfo('admin_email'); 440 $subject = 'Payjoe upload error at '.get_home_url(); 441 wp_mail( $to, $subject, $message); 442 } 387 function handleAPIResult( $result, $post_id, $invoice_number, $log_json_data = false ) { 388 $result = trim( $result ); 389 390 $msg = array( 391 'error' => false, 392 'info' => null 393 ); 394 395 if ( $result ) { 396 $result = json_decode( $result, true ); 397 398 if ( $result['Erfolgreich'] == 1 ) { 399 //all was good! 400 $msg['info'] = __( 'Submit success', 'weslink-payjoe-opbeleg' ); 401 } else { 402 //there was an error 403 foreach ( $result['Fehlerliste'] as $e_error ) { 404 $error_string = "\n- " . $e_error['OPBelegBuchungstext']; 405 unset( $e_error['OPBelegBuchungstext'] ); 406 foreach ( $e_error as $key => $val ) { 407 if ( $key == 'OPBelegErrorReasons' ) { 408 $error_string .= sprintf( "\n+ %s", $key ); 409 foreach ( $val as $sub_val ) { 410 foreach ( $sub_val as $sub_key => $subs_val ) { 411 $error_string .= sprintf( "\n++ %s: %s", $sub_key, $subs_val ); 412 } 413 } 414 } else { 415 $error_string .= sprintf( "\n+ %s: %s", $key, $val ); 416 } 417 } 418 419 if ( ! isset( $msg['error'] ) ) { 420 $msg['error'] = ''; 421 } 422 $msg['error'] .= $error_string; 423 } 424 } 425 } else { 426 $msg['error'] = __( 'No data responded from API Server.', 'weslink-payjoe-opbeleg' ) . json_encode( $result ); 427 } 428 429 if ( $msg['error'] ) { 430 $tpl = "<span style='color:red'>%s: %s</span>\n\n"; 431 $err_string = sprintf( $tpl, __( "Error" ), $msg['error'] ); 432 $this->send_error_notfication_to_admin( $err_string ); 433 echo $err_string; 434 } else { 435 $tpl = "<span style='color:green'>%s</span>\n\n"; 436 echo sprintf( $tpl, __( "Success" ), $msg['info'] ); 437 } 438 // inform the hooks 439 do_action( 'weslink-payjoe-opbeleg-post-upload', $msg, $post_id, $invoice_number ); //call actions listening for "weslink-payjoe-opbeleg-post-upload" (update_latest_processed_invoice_number, update_payjoe_status 440 } 441 442 function uploadBelegtoPayJoe( $data ) { 443 $url = 'https://api.payjoe.de/api/opbelegupload'; 444 $curl = curl_init( $url ); 445 $data = json_encode( $data ); 446 447 $options = array( 448 CURLOPT_POSTFIELDS => $data, 449 CURLOPT_POST => 1, 450 CURLOPT_RETURNTRANSFER => 1, 451 CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks 452 CURLOPT_HTTPHEADER => array( 453 'Content-Type: application/json', 454 'Content-Length: ' . strlen( $data ) 455 ) 456 ); 457 curl_setopt_array( $curl, $options ); 458 $result = curl_exec( $curl ); 459 curl_close( $curl ); 460 461 return $result; 462 } 463 464 465 function send_error_notfication_to_admin( $message ) { 466 $message = "This is an automatic email from the Woocommerce PayJoe Plugin. There has been an error with the Pajoe Upload: \n \n'.$message.'\n If you have enabled debugging, you can check the logfiles at uploads/payjoe/ to get more information."; 467 $to = get_bloginfo( 'admin_email' ); 468 $subject = 'Payjoe upload error at ' . get_home_url(); 469 wp_mail( $to, $subject, $message ); 470 } 443 471 444 472 } -
woo-payjoe-beleg-schnittstelle/trunk/weslink-payjoe-opbeleg.php
r1718741 r1729806 16 16 * Plugin Name: Woo PayJoe Beleg Schnittstelle 17 17 * Description: Plugin zum Upload von Rechnungen zu PayJoe 18 * Version: 1.2. 118 * Version: 1.2.2 19 19 * Author: WESLINK _ YOUR LINK TO WORDPRESS 20 20 * Author URI: http://weslink.de
Note: See TracChangeset
for help on using the changeset viewer.