public static function processAmazonShipmentsReportPage($report, $rows, $job, $task)
 {
     $wc_orders_processed = 0;
     // process rows
     foreach ($rows as $row) {
         // check for MCF order ID
         $order_id = str_replace('#', '', $row['merchant-order-id']);
         $order_item_id = $row['merchant-order-item-id'];
         $is_mcf_order = true;
         // if ( empty( $order_id ) ) continue;
         // if ( empty( $order_item_id ) ) continue;
         // no merchant-order-id means this order was placed on Amazon - find WooCommerce order by reference
         if (empty($order_id)) {
             $amazon_order_id = $row['amazon-order-id'];
             $is_mcf_order = false;
             $om = new WPLA_OrdersModel();
             $order = $om->getOrderByOrderID($amazon_order_id);
             if ($order) {
                 $order_id = $order->post_id;
             }
         }
         if (empty($order_id)) {
             continue;
         }
         // get WooCommerce order
         $_order = wc_get_order($order_id);
         if (!$_order) {
             continue;
         }
         // echo "<pre>";print_r($_order);echo"</pre>";#die();
         // echo "<pre>";print_r($row);echo"</pre>";die();
         $shipment_date = $row['shipment-date'];
         $estimated_arrival_date = $row['estimated-arrival-date'];
         $ship_service_level = $row['ship-service-level'];
         $tracking_number = $row['tracking-number'];
         $carrier = $row['carrier'];
         // update order meta fields
         update_post_meta($order_id, '_wpla_fba_submission_status', 'shipped');
         update_post_meta($order_id, '_wpla_fba_shipment_date', $shipment_date);
         update_post_meta($order_id, '_wpla_fba_estimated_arrival_date', $estimated_arrival_date);
         update_post_meta($order_id, '_wpla_fba_ship_service_level', $ship_service_level);
         update_post_meta($order_id, '_wpla_fba_tracking_number', $tracking_number);
         update_post_meta($order_id, '_wpla_fba_ship_carrier', $carrier);
         // update meta fields for WooCommerce Shipment Tracking plugin
         update_post_meta($order_id, '_date_shipped', strtotime($shipment_date));
         update_post_meta($order_id, '_tracking_number', $tracking_number);
         update_post_meta($order_id, '_custom_tracking_provider', $carrier);
         update_post_meta($order_id, '_tracking_provider', '');
         // known providers - would require mapping ('usps' <=> 'USPS')
         $wc_orders_processed++;
         // skip further processing for non-MCF orders - no need to to update orders placed on Amazon
         if (!$is_mcf_order) {
             continue;
         }
         // complete order
         $_order->update_status('completed');
         // notify WPLE - mark order as shipped on eBay
         $args = array();
         $args['TrackingNumber'] = $tracking_number;
         $args['TrackingCarrier'] = $carrier;
         $args['ShippedTime'] = $shipment_date;
         // $args['FeedbackText']    = 'Thank You...';
         do_action('wple_complete_sale_on_ebay', $order_id, $args);
     }
     // build response
     $response = new stdClass();
     $response->job = $job;
     $response->task = $task;
     $response->errors = '';
     $response->success = true;
     $response->count = $wc_orders_processed;
     return $response;
 }
 function updateShipmentFeed($post_id)
 {
     $feed_type = '_POST_FLAT_FILE_FULFILLMENT_DATA_';
     $order_id = get_post_meta($post_id, '_wpla_amazon_order_id', true);
     $om = new WPLA_OrdersModel();
     $order = $om->getOrderByOrderID($order_id);
     $account = new WPLA_AmazonAccount($order->account_id);
     // echo "<pre>";print_r($account);echo"</pre>";die();
     WPLA()->logger->info('updateShipmentFeed() ' . $feed_type . ' - order id: ' . $order_id);
     WPLA()->logger->info('updateShipmentFeed() - post id: ' . $post_id . ' - account id: ' . $account->id);
     // create pending feed if it doesn't exist
     if (!($this->id = self::getPendingFeedId($feed_type, null, $account->id))) {
         # build feed data
         WPLA()->logger->info('building shipment data feed...');
         $csv = WPLA_FeedDataBuilder::buildShippingFeedData($post_id, $order_id, $account->id, true);
         if (!$csv) {
             WPLA()->logger->warn('no feed data - not creating feed');
             return;
         }
         // add new feed
         $this->FeedType = $feed_type;
         $this->status = 'pending';
         $this->account_id = $account->id;
         $this->date_created = date('Y-m-d H:i:s');
         $this->data = $csv;
         $this->add();
         WPLA()->logger->info('added NEW feed - id ' . $this->id);
     } else {
         WPLA()->logger->info('found existing feed ' . $this->id);
         $existing_feed = new WPLA_AmazonFeed($this->id);
         # append feed data
         WPLA()->logger->info('updating shipment data feed...');
         $csv = WPLA_FeedDataBuilder::buildShippingFeedData($post_id, $order_id, $account->id, false);
         $this->data = $existing_feed->data . $csv;
     }
     // update feed
     $this->line_count = sizeof($csv);
     $this->FeedProcessingStatus = 'pending';
     $this->date_created = date('Y-m-d H:i:s');
     $this->update();
     WPLA()->logger->info('feed was built and updated - ' . $this->id);
 }
 function wpla_woocommerce_custom_shop_order_columns($column)
 {
     global $post, $woocommerce;
     switch ($column) {
         case 'wpl_order_src':
             $amazon_order_id = get_post_meta($post->ID, '_wpla_amazon_order_id', true);
             $tagged_as_fba = false;
             if ($amazon_order_id) {
                 // get order details
                 $om = new WPLA_OrdersModel();
                 $order = $om->getOrderByOrderID($amazon_order_id);
                 $account = $order ? WPLA_AmazonAccount::getAccount($order->account_id) : false;
                 $tooltip = 'This order was placed on Amazon.';
                 if ($account) {
                     $tooltip .= '<br>(' . $account->title . ')';
                 }
                 echo '<img src="' . WPLA_URL . 'img/amazon-orange-16x16.png" style="width:16px;vertical-align:middle;padding:0;" class="tips" data-tip="' . $tooltip . '" />';
                 // check for FBA
                 if ($order) {
                     $order_details = json_decode($order->details);
                     if (is_object($order_details) && $order_details->FulfillmentChannel == 'AFN') {
                         echo '&nbsp;<small style="font-size:10px;color:silver">' . 'FBA' . '</small>';
                         $tagged_as_fba = true;
                     }
                 }
             }
             // if amazon order
             // show submission status if it exists - for non-amazon orders as well
             if ($submission_status = get_post_meta($post->ID, '_wpla_submission_result', true)) {
                 if ($submission_status == 'success') {
                     echo '<br><img src="' . WPLA_URL . 'img/icon-success-32x32.png" style="width:12px;vertical-align:middle;padding:0;" class="tips" data-tip="This order was marked as shipped on Amazon" />';
                 } else {
                     $history = maybe_unserialize($submission_status);
                     $error_count = is_array($history) ? sizeof(@$history['errors']) : false;
                     if ($error_count) {
                         echo '<br><img src="' . WPLA_URL . 'img/error.gif" style="vertical-align:middle;padding:0;" class="tips" data-tip="There was a problem - this order could not be marked as shipped on Amazon!" />';
                     }
                 }
             }
             // show FBA submission status if it exists - for non-amazon orders as well
             if ($submission_status = get_post_meta($post->ID, '_wpla_fba_submission_status', true)) {
                 if (!$tagged_as_fba) {
                     echo '<small style="font-size:10px;">' . 'FBA' . '</small>&nbsp;';
                 }
                 if ($submission_status == 'success') {
                     echo '<img src="' . WPLA_URL . 'img/icon-success-32x32.png" style="width:12px;vertical-align:middle;padding:0;" class="tips" data-tip="This order was successfully submitted to be fulfilled by Amazon." />';
                 } elseif ($submission_status == 'shipped') {
                     echo '<img src="' . WPLA_URL . 'img/icon-success-32x32.png" style="width:12px;vertical-align:middle;padding:0;" class="tips" data-tip="This order has been fulfilled by Amazon." />';
                 } else {
                     $history = maybe_unserialize(get_post_meta($post->ID, '_wpla_fba_submission_result', true));
                     $error_count = is_array($history) ? sizeof(@$history['errors']) : false;
                     if ($error_count) {
                         echo '<img src="' . WPLA_URL . 'img/error.gif" style="vertical-align:middle;padding:0;" class="tips" data-tip="There was a problem submitting this order to be fulfilled by Amazon!" />';
                     }
                 }
             }
             break;
     }
     // switch ($column)
 }