function updateOrders($session, $days = false, $current_page = 1, $order_ids = false)
 {
     WPLE()->logger->info('*** updateOrders(' . $days . ') - page ' . $current_page);
     // this is a cron job if no number of days and no order IDs are requested
     $is_cron_job = $days == false && $order_ids == false ? true : false;
     $this->initServiceProxy($session);
     // set request handler
     $this->_cs->setHandler('OrderType', array(&$this, 'handleOrderType'));
     // $this->_cs->setHandler( 'PaginationResultType', array( & $this, 'handlePaginationResultType' ) );
     // build request
     $req = new GetOrdersRequestType();
     $req->setOrderRole('Seller');
     // $req->setIncludeContainingOrder(true);
     // check if we need to calculate lastdate
     if ($this->current_lastdate) {
         $lastdate = $this->current_lastdate;
         WPLE()->logger->info('used current_lastdate from last run: ' . $lastdate);
     } else {
         // period 30 days, which is the maximum allowed
         $now = time();
         $lastdate = $this->getDateOfLastOrder($this->account_id);
         WPLE()->logger->info("getDateOfLastOrder( {$this->account_id} ) returned: " . $lastdate);
         if ($lastdate) {
             $lastdate = mysql2date('U', $lastdate);
         }
         // if last date is older than 30 days, fall back to default
         if ($lastdate < $now - 3600 * 24 * 30) {
             WPLE()->logger->info('resetting lastdate - fall back default ');
             $lastdate = false;
         }
     }
     // save lastdate for next page
     $this->current_lastdate = $lastdate;
     // fetch orders by IDs
     if (is_array($order_ids)) {
         $OrderIDArray = new OrderIDArrayType();
         foreach ($order_ids as $id) {
             $order = $this->getItem($id);
             $OrderIDArray->addOrderID($order['order_id']);
         }
         $req->setOrderIDArray($OrderIDArray);
         // parameter $days
     } elseif ($days) {
         $req->NumberOfDays = $days;
         $this->NumberOfDays = $days;
         WPLE()->logger->info('NumberOfDays: ' . $req->NumberOfDays);
         // default: orders since last change
     } elseif ($lastdate) {
         $req->ModTimeFrom = gmdate('Y-m-d H:i:s', $lastdate);
         $req->ModTimeTo = gmdate('Y-m-d H:i:s', time());
         $this->ModTimeFrom = $req->ModTimeFrom;
         $this->ModTimeTo = $req->ModTimeTo;
         WPLE()->logger->info('lastdate: ' . $lastdate);
         WPLE()->logger->info('ModTimeFrom: ' . $req->ModTimeFrom);
         WPLE()->logger->info('ModTimeTo: ' . $req->ModTimeTo);
         // fallback: one day (max allowed by ebay: 30 days)
     } else {
         $days = 1;
         $req->NumberOfDays = $days;
         $this->NumberOfDays = $days;
         WPLE()->logger->info('NumberOfDays (fallback): ' . $req->NumberOfDays);
     }
     // $req->DetailLevel = $Facet_DetailLevelCodeType->ReturnAll;
     if (!$this->is_ajax()) {
         $req->setDetailLevel('ReturnAll');
     }
     // set pagination for first page
     $custom_page_size = get_option('wplister_fetch_orders_page_size', 50);
     $items_per_page = $is_cron_job ? $custom_page_size : 100;
     // For GetOrders, the maximum value is 100 and the default value is 25 (which is too low in some rare cases)
     $this->current_page = $current_page;
     $Pagination = new PaginationType();
     $Pagination->setEntriesPerPage($items_per_page);
     $Pagination->setPageNumber($this->current_page);
     $req->setPagination($Pagination);
     // get orders (single page)
     WPLE()->logger->info('fetching orders - page ' . $this->current_page);
     $res = $this->_cs->GetOrders($req);
     $this->total_pages = $res->PaginationResult->TotalNumberOfPages;
     $this->total_items = $res->PaginationResult->TotalNumberOfEntries;
     // get order with pagination helper (doesn't work as expected)
     // EbatNs_PaginationHelper($proxy, $callName, $request, $responseElementToMerge = '__COUNT_BY_HANDLER', $maxEntries = 200, $pageSize = 200, $initialPage = 1)
     // $helper = new EbatNs_PaginationHelper( $this->_cs, 'GetOrders', $req, 'OrderArray', 20, 10, 1);
     // $res = $helper->QueryAll();
     // handle response and check if successful
     if ($this->handleResponse($res)) {
         WPLE()->logger->info("*** Orders updated successfully.");
         // WPLE()->logger->info( "*** PaginationResult:".print_r($res->PaginationResult,1) );
         // WPLE()->logger->info( "*** processed response:".print_r($res,1) );
         WPLE()->logger->info("*** current_page : " . $this->current_page);
         WPLE()->logger->info("*** total_pages  : " . $this->total_pages);
         WPLE()->logger->info("*** total_items  : " . $this->total_items);
         WPLE()->logger->info("** count_inserted: " . $this->count_inserted);
         WPLE()->logger->info("** count_updated : " . $this->count_updated);
         WPLE()->logger->info("** count_skipped : " . $this->count_skipped);
         WPLE()->logger->info("** count_failed  : " . $this->count_failed);
         // fetch next page recursively - only in days mode, or if no new orders have been fetched yet
         if ($res->HasMoreOrders && (!$is_cron_job || $this->count_inserted == 0)) {
             $this->current_page++;
             $this->updateOrders($session, $days, $this->current_page);
         }
     } else {
         WPLE()->logger->error("Error on orders update" . print_r($res, 1));
     }
 }
Exemple #2
0
 public function retrieveOrders($params)
 {
     global $sugar_config;
     $shipping_service = $sugar_config['ebay_shipping_service'];
     $result = true;
     $account_id = $params['AccountID'];
     $this->session->setRequestToken($params['AuthToken']);
     $bean = BeanFactory::getBean('xeBayOrders');
     $req = new GetOrdersRequestType();
     if ($params['NumberOfDays'] != 90) {
         $req->setNumberOfDays($params['NumberOfDays']);
     } else {
         $req->setCreateTimeFrom(date('Y-m-dTH:i:s', strtotime('now - 88 days')));
     }
     $req->setOrderStatus($params['OrderStatus']);
     $pagination = new PaginationType();
     $pagination->setEntriesPerPage(50);
     $pageNumber = 1;
     $hasMoreOrders = false;
     do {
         $pagination->setPageNumber($pageNumber++);
         $req->setPagination($pagination);
         $res = $this->proxy->GetOrders($req);
         if ($this->testValid($res)) {
             $hasMoreOrders = $res->getHasMoreOrders();
             $ordersPerPage = $res->getOrdersPerPage();
             $orderArray = $res->getOrderArray();
             if (empty($orderArray)) {
                 break;
             }
             foreach ($orderArray as &$order) {
                 $eBayPaymentStatus = $order->getCheckoutStatus()->getEBayPaymentStatus();
                 if ($eBayPaymentStatus != "NoPaymentFailure") {
                     continue;
                 }
                 $completeStatus = $order->getCheckoutStatus()->getStatus();
                 if ($completeStatus != "Complete") {
                     continue;
                 }
                 $orderId = $order->getOrderID();
                 $duplicated = $bean->retrieve_by_string_fields(array('order_id' => $orderId));
                 if (empty($duplicated)) {
                     $bean->shipping_service = $shipping_service;
                     $bean->populateDefaultValues(true);
                 }
                 $bean->source_type = 'ebay';
                 $bean->xebayaccount_id = $account_id;
                 $bean->buyer_checkout_message = $order->getBuyerCheckoutMessage();
                 $bean->order_id = $order->getOrderID();
                 $bean->order_status = $order->getOrderStatus();
                 $bean->buyer_user_id = $order->getBuyerUserID();
                 $bean->subtotal_currency_id = $order->getSubtotal()->getTypeAttribute('currencyID');
                 $bean->subtotal_value = $order->getSubtotal()->getTypeValue();
                 $bean->total_currency_id = $order->getTotal()->getTypeAttribute('currencyID');
                 $bean->total_value = $order->getTotal()->getTypeValue();
                 $bean->date_entered = $order->getCreatedTime();
                 $bean->paid_time = $order->getPaidTime();
                 $bean->shipped_time = $order->getShippedTime();
                 if (!empty($bean->shipped_time)) {
                     $bean->handled_status = 'handled';
                 } else {
                     if (empty($duplicated)) {
                         $bean->handled_status = 'unhandled';
                     } else {
                         // do not change handled status
                     }
                 }
                 $bean->sales_record_number = $order->getShippingDetails()->getSellingManagerSalesRecordNumber();
                 $bean->eias_token = $order->getEIASToken();
                 $bean->payment_hold_status = $order->getPaymentHoldStatus();
                 $shippingAddress = $order->getShippingAddress();
                 $bean->name = $shippingAddress->getName();
                 $bean->street1 = $shippingAddress->getStreet1();
                 $bean->street2 = $shippingAddress->getStreet2();
                 $bean->city_name = $shippingAddress->getCityName();
                 $bean->state_or_province = $shippingAddress->getStateOrProvince();
                 $bean->country = $shippingAddress->getCountry();
                 $bean->country_name = $shippingAddress->getCountryName();
                 $bean->phone = $shippingAddress->getPhone();
                 $bean->postal_code = $shippingAddress->getPostalCode();
                 $bean->address_id = $shippingAddress->getAddressID();
                 $bean->address_owner = $shippingAddress->getAddressOwner();
                 $bean->external_address_id = $shippingAddress->getExternalAddressID();
                 if (empty($duplicated)) {
                     $bean->checkout_status_last_modified_time = $order->getCheckoutStatus()->getLastModifiedTime();
                     $bean->id = create_guid();
                     $bean->new_with_id = true;
                     $this->add_transactions($order->getTransactionArray(), $bean);
                     $bean->save();
                 } else {
                     $checkoutStatusLastModifiedTime = $order->getCheckoutStatus()->getLastModifiedTime();
                     if ($bean->checkout_status_last_modified_time != $checkoutStatusLastModifiedTime) {
                         $bean->checkout_status_last_modified_time = $checkoutStatusLastModifiedTime;
                         $bean->save();
                     }
                 }
             }
         } else {
             $this->dumpObject($res);
             $result = false;
             break;
         }
     } while ($hasMoreOrders);
     return $result;
 }