public function displayRepricingPage()
 {
     // handle actions and show notes
     // $this->handleActions();
     if ($this->requestAction() == 'wpla_apply_lowest_price_to_all_items') {
         $this->applyLowestPricesToAllItems();
     }
     if ($this->requestAction() == 'wpla_resubmit_pnq_update') {
         $this->resubmitPnqUpdateForSelectedItems();
     }
     if ($this->requestAction() == 'wpla_bulk_apply_lowest_prices') {
         $this->applyLowestPricesToSelectedItems();
     }
     if ($this->requestAction() == 'wpla_bulk_apply_minmax_prices') {
         $this->applyMinMaxPrices();
     }
     // handle bulk action - get_compet_price
     if ($this->requestAction() == 'get_compet_price') {
         WPLA()->pages['listings']->get_compet_price();
         WPLA()->pages['listings']->get_lowest_offers();
     }
     if ($this->requestAction() == 'wpla_resubmit_all_failed_prices') {
         $lm = new WPLA_ListingsModel();
         $items = $lm->getWhere('pnq_status', -1);
         foreach ($items as $item) {
             // set pnq status to changed (1)
             $lm->updateWhere(array('id' => $item->id), array('pnq_status' => 1));
         }
         $this->showMessage(sprintf(__('%s failed prices were scheduled for resubmission.', 'wpla'), count($items)));
     }
     // create table and fetch items to show
     $this->repricingTable = new WPLA_RepricingTable();
     $this->repricingTable->prepare_items();
     $active_tab = 'repricing';
     $aData = array('plugin_url' => self::$PLUGIN_URL, 'message' => $this->message, 'listingsTable' => $this->repricingTable, 'default_account' => get_option('wpla_default_account_id'), 'tools_url' => 'admin.php?page=' . self::ParentMenuId . '-tools', 'form_action' => 'admin.php?page=' . self::ParentMenuId . '-tools' . '&tab=' . $active_tab);
     $this->display('tools_repricing', $aData);
 }
 public function checkSoldStock()
 {
     // get all published listings
     $lm = new WPLA_ListingsModel();
     $listings = $lm->getWhere('status', 'sold');
     $out_of_stock_products = array();
     // process published listings
     foreach ($listings as $item) {
         // get wc product
         $_product = $this->getProduct($item['post_id']);
         // checking parent variations makes no sense in WPLA, so skip them
         if ($_product->product_type == 'variable') {
             continue;
         }
         // check stock level
         // $stock = WPLA_ProductWrapper::getStock( $item['post_id'] );
         $stock = $_product ? $_product->get_total_stock() : 0;
         if ($stock == 0) {
             continue;
         }
         // mark listing as changed
         // if ( isset( $_REQUEST['mark_as_changed'] ) ) {
         // 	$lm->updateListing( $item['id'], array( 'status' => 'changed' ) );
         // 	$item['status'] = 'changed';
         // }
         // add to list of out of stock products
         $item['stock'] = $stock;
         $item['exists'] = $_product ? true : false;
         $item['parent_id'] = $_product->product_type == 'variation' ? $_product->parent->id : false;
         $out_of_stock_products[] = $item;
     }
     // return if empty
     if (empty($out_of_stock_products)) {
         WPLA()->showMessage('No sold products have stock in WooCommerce.', 0, 1);
         return;
     }
     $msg = '<p>';
     $msg .= 'Warning: Some sold listings are still in stock in WooCommerce.';
     $msg .= '</p>';
     // table header
     $msg .= '<table style="width:100%">';
     $msg .= "<tr>";
     $msg .= "<th style='text-align:left'>Stock</th>";
     $msg .= "<th style='text-align:left'>SKU</th>";
     $msg .= "<th style='text-align:left'>Product</th>";
     $msg .= "<th style='text-align:left'>Qty</th>";
     $msg .= "<th style='text-align:left'>ASIN</th>";
     $msg .= "<th style='text-align:left'>Ended at</th>";
     $msg .= "<th style='text-align:left'>Status</th>";
     $msg .= "</tr>";
     // table rows
     foreach ($out_of_stock_products as $item) {
         // get column data
         // $qty     = $item['quantity'] - $item['quantity_sold'];
         $sku = $item['sku'];
         $qty = $item['quantity'];
         $stock = $item['stock'] . ' x ';
         $title = $item['listing_title'];
         $post_id = $item['post_id'];
         $asin = $item['asin'];
         $status = $item['status'];
         $exists = $item['exists'];
         $date_ended = $item['date_finished'] ? $item['date_finished'] : $item['end_date'];
         // build links
         $amazon_url = $item['ViewItemURL'] ? $item['ViewItemURL'] : ($amazon_url = 'http://www.amazon.com/itm/' . $asin);
         $amazon_url = 'admin.php?page=wpla&s=' . $asin;
         $amazon_link = '<a href="' . $amazon_url . '" target="_blank">' . $asin . '</a>';
         $edit_link = '<a href="post.php?action=edit&post=' . ($item['parent_id'] ? $item['parent_id'] : $post_id) . '" target="_blank">' . $title . '</a>';
         // mark non existent products
         if (!$exists) {
             $stock = 'N/A';
             $post_id .= ' missing!';
         }
         // build table row
         $msg .= "<tr>";
         $msg .= "<td>{$stock}</td>";
         $msg .= "<td>{$sku}</td>";
         $msg .= "<td>{$edit_link} (ID {$post_id})</td>";
         $msg .= "<td>{$qty} x </td>";
         $msg .= "<td>{$amazon_link}</td>";
         $msg .= "<td>{$date_ended}</td>";
         $msg .= "<td>{$status}</td>";
         $msg .= "</tr>";
     }
     $msg .= '</table>';
     // show 'check again' button
     $msg .= '<p>';
     $url = 'admin.php?page=wpla-tools&action=check_wc_sold_stock&_wpnonce=' . wp_create_nonce('wpla_tools_page');
     $msg .= '<a href="' . $url . '" class="button">' . __('Check again', 'wpla') . '</a> &nbsp; ';
     $msg .= '</p>';
     // $msg .= '<p>';
     // $url = 'admin.php?page=wpla-tools&action=check_wc_out_of_stock&mark_as_changed=yes&_wpnonce='.wp_create_nonce('wpla_tools_page');
     // $msg .= '<a href="'.$url.'" class="button">'.__('Mark all as changed','wpla').'</a> &nbsp; ';
     // $msg .= 'Click this button to mark all found listings as changed in WP-Lister.';
     // $msg .= '</p>';
     WPLA()->showMessage($msg, 1, 1);
 }
 private function saveProfile()
 {
     // init profile
     $profile_id = $this->getValueFromPost('profile_id');
     $profile = new WPLA_AmazonProfile($profile_id);
     // fill in post data
     $post_data = $this->getPreprocessedPostData();
     $profile->fillFromArray($post_data);
     // add field data
     $profile->fields = maybe_serialize($this->getPreprocessedPostData('tpl_col_', true));
     // insert or update
     if ($profile_id) {
         $profile->update();
         $this->showMessage(__('Profile updated.', 'wpla'));
     } else {
         $profile->add();
         $this->showMessage(__('Profile added.', 'wpla'));
     }
     // error handling
     // if ($result===false) {
     // 	$this->showMessage( "There was a problem saving your profile.<br>SQL:<pre>".$wpdb->last_query.'</pre>'.$wpdb->last_error, true );
     // } else {
     // }
     // prepare for updating items
     // $profile    = new WPLA_AmazonProfile( $profile_id );
     $listingsModel = new WPLA_ListingsModel();
     // re-apply profile to all published
     if (!$profile_id) {
         return;
     }
     $items = $listingsModel->getWhere('profile_id', $profile_id);
     $listingsModel->applyProfileToListings($profile, $items);
     $this->showMessage(sprintf(__('%s items updated.', 'wplister'), count($items)));
 }
 public function handleActions()
 {
     // handle save listing
     if ($this->requestAction() == 'save_listing') {
         $this->saveListing();
     }
     // trigger create product
     if ($this->requestAction() == 'create_product') {
         $lm = new WPLA_ListingsModel();
         $listing = $lm->getItem($_REQUEST['listing']);
         if (!$listing) {
             return;
         }
         // create product
         $ProductsImporter = new WPLA_ProductsImporter();
         $success = $ProductsImporter->createProductFromAmazonListing($listing);
         $error = $ProductsImporter->lastError;
         $post_id = $ProductsImporter->lastPostID;
         $message = $ProductsImporter->message;
         if ($success) {
             // get parent post_id - for View Product button
             $_product = get_product($post_id);
             if ('variation' == $_product->product_type) {
                 $post_id = $_product->parent->id;
             }
             $msg = $message ? $message : sprintf(__('A new product (ID %s) was created for ASIN %s.', 'wpla'), $post_id, $listing['asin']);
             $msg .= sprintf('&nbsp;&nbsp;<a href="post.php?post=%s&action=edit" class="button button-small" target="_blank">%s</a>', $post_id, __('View product', 'wpla'));
             $this->showMessage($msg);
         } else {
             $error_msg = sprintf(__('Item %s could not be imported.', 'wpla'), $listing['asin']) . '<br>Error: ' . $error;
             $this->showMessage($error_msg, 1);
         }
     }
     // handle update from Amazon action
     if ($this->requestAction() == 'update') {
         // $this->initAC();
         // $this->EC->updateItemsFromEbay( $_REQUEST['listing'] );
         // $this->EC->closeEbay();
         // $this->showMessage( __('Selected items were updated from Amazon.','wpla') );
         $this->showMessage(__('Not implemented yet.', 'wpla'));
     }
     // handle delete action
     if ($this->requestAction() == 'delete') {
         $lm = new WPLA_ListingsModel();
         if (is_array($_REQUEST['listing'])) {
             foreach ($_REQUEST['listing'] as $id) {
                 $lm->deleteItem($id);
             }
         } elseif (is_numeric($_REQUEST['listing'])) {
             $lm->deleteItem($_REQUEST['listing']);
         }
         $this->showMessage(__('Selected listings were removed from WP-Lister.', 'wpla'));
     }
     // handle trash_listing action
     if ($this->requestAction() == 'trash_listing') {
         $items = is_array($_REQUEST['listing']) ? $_REQUEST['listing'] : array($_REQUEST['listing']);
         $lm = new WPLA_ListingsModel();
         foreach ($items as $id) {
             $lm->updateWhere(array('id' => $id), array('status' => 'trash'));
         }
         $this->showMessage(__('Selected items have been scheduled to be removed from your Amazon account.', 'wpla'));
     }
     // handle resubmit action
     if ($this->requestAction() == 'resubmit') {
         $items = is_array($_REQUEST['listing']) ? $_REQUEST['listing'] : array($_REQUEST['listing']);
         $lm = new WPLA_ListingsModel();
         foreach ($items as $id) {
             $lm->resubmitItem($id);
         }
         $this->showMessage(__('Selected items were prepared for resubmission.', 'wpla'));
     }
     if ($this->requestAction() == 'resubmit_all_failed') {
         $lm = new WPLA_ListingsModel();
         $items = $lm->getWhere('status', 'failed');
         foreach ($items as $item) {
             $lm->resubmitItem($item->id);
         }
         $this->showMessage(sprintf(__('%s failed items were prepared for resubmission.', 'wpla'), count($items)));
     }
     if ($this->requestAction() == 'wpla_clear_import_queue') {
         $lm = new WPLA_ListingsModel();
         $items = $lm->getWhere('status', 'imported');
         foreach ($items as $item) {
             $lm->deleteItem($item->id);
         }
         $this->showMessage(sprintf(__('%s items have been removed from the import queue.', 'wpla'), count($items)));
     }
     // handle toolbar action - prepare listing from product
     if ($this->requestAction() == 'wpla_prepare_single_listing') {
         // get profile
         $profile = isset($_REQUEST['profile_id']) ? WPLA_AmazonProfile::getProfile($_REQUEST['profile_id']) : false;
         if ($profile) {
             // prepare product
             $listingsModel = new WPLA_ListingsModel();
             $success = $listingsModel->prepareProductForListing($_REQUEST['product_id'], $_REQUEST['profile_id']);
             // $listingsModel->applyProfileToNewListings( $profile );
             if ($success) {
                 $this->showMessage(__('New listing was prepared from product.', 'wpla'));
             } else {
                 $this->showMessage(join('<br>', $listingsModel->warnings), 1);
             }
         }
     }
     // handle bulk action - get_compet_price
     if ($this->requestAction() == 'get_compet_price') {
         $this->get_compet_price();
         $this->get_lowest_offers();
         // do both
     }
     // handle bulk action - get_lowest_offers
     if ($this->requestAction() == 'get_lowest_offers') {
         $this->get_lowest_offers();
     }
     // handle wpla_dismiss_imported_products_notice action
     if ($this->requestAction() == 'wpla_dismiss_imported_products_notice') {
         self::updateOption('dismiss_imported_products_notice', '1');
     }
 }