/** * */ private function _initView($o_result_context) { $va_options = array('type' => 'O'); $t_order = new ca_commerce_orders(); $t_order->set('order_type', 'O'); $t_order_item = new ca_commerce_order_items(); // Set drop-down fields to be null-able here so we don't get them auto-setting to their defaults $t_order->setFieldAttribute('order_status', 'IS_NULL', true); // filtering options foreach (array('order_status' => 'string', 'created_on' => 'string', 'loan_checkout_date' => 'string', 'loan_due_date' => 'string', 'loan_return_date' => 'string', 'search' => 'string', 'transaction_id' => 'int', 'user_id' => 'int') as $vs_f => $vs_type) { if (array_key_exists($vs_f, $_REQUEST)) { $vm_v = $this->request->getParameter($vs_f, pString); $o_result_context->setParameter('caClientOrderList_' . $vs_f, $vm_v); } else { if (!($vm_v = $this->request->getParameter($vs_f, pString))) { $vm_v = $o_result_context->getParameter('caClientOrderList_' . $vs_f); } } // transaction_id may appear in GET requests since the communication editor links to this page with transaction_id as a param if ($vs_f == 'transaction_id') { $vm_v = $this->request->getParameter($vs_f, pString); } switch ($vs_type) { case 'int': if (strlen($vm_v)) { $vm_v = (int) $vm_v; } break; } if ($vs_f != 'search') { $t_order->set($vs_f, $vm_v); } $va_options[$vs_f] = $vm_v; } $this->view->setVar('t_order', $t_order); $this->view->setVar('t_order_item', $t_order_item); $this->view->setVar('filter_options', $va_options); $this->view->setVar('order_list', $va_order_list = $t_order->getOrders($va_options)); $va_order_ids = array(); foreach ($va_order_list as $vn_i => $va_order) { $va_order_ids[] = $va_order['order_id']; } $o_result_context->setResultList($va_order_ids); $o_result_context->setAsLastFind(); $o_result_context->saveContext(); $this->view->setVar('client_services_config', $this->opo_client_services_config = Configuration::load($this->request->config->get('client_services_config'))); $this->view->setVar('currency', $this->opo_client_services_config->get('currency')); $this->view->setVar('currency_symbol', $this->opo_client_services_config->get('currency_symbol')); }
/** * Perform client services-related periodic tasks */ public function hookPeriodicTask(&$pa_params) { $t_log = new Eventlog(); $o_db = new Db(); if (!(bool) $this->opo_config->get('enable_client_services')) { return true; } // Find any orders with status PROCESSED_AWAITING_MEDIA_ACCESS and fetch media $qr_orders = $o_db->query("\n\t\t\t\tSELECT order_id\n\t\t\t\tFROM ca_commerce_orders\n\t\t\t\tWHERE\n\t\t\t\t\torder_status = 'PROCESSED_AWAITING_MEDIA_ACCESS'\n\t\t\t"); // // Set up HTTP client for REST calls // if ($this->opo_client_services_config->get('remote_media_base_url')) { $vs_base_url = $this->opo_client_services_config->get('remote_media_base_url'); $o_client = new RestClient($vs_base_url . "/service.php/iteminfo/ItemInfo/rest"); try { $o_res = $o_client->auth($this->opo_client_services_config->get('remote_media_username'), $this->opo_client_services_config->get('remote_media_password'))->get(); if (!$o_res->isSuccess()) { $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Could not authenticate to remote system %1', $vs_base_url), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); } while ($qr_orders->nextRow()) { $t_order = new ca_commerce_orders($qr_orders->get('order_id')); $vb_download_errors = false; if ($t_order->getPrimaryKey() && sizeof($va_missing_media = $t_order->itemsMissingDownloadableMedia())) { $va_missing_media_representation_ids = $t_order->itemsMissingDownloadableMedia('original', array('returnRepresentationIDs' => true)); foreach ($va_missing_media as $vn_object_id => $va_representation_md5s) { foreach ($va_representation_md5s as $vn_i => $vs_representation_md5) { $o_xml = $o_client->getObjectRepresentationURLByMD5($vs_representation_md5, 'original')->get(); $vs_url = (string) $o_xml->getObjectRepresentationURLByMD5->original; if (!$vs_url) { continue; } // media no longer exists // fetch the file $t_rep = new ca_object_representations($va_missing_media_representation_ids[$vn_object_id][$vn_i]); if ($t_rep->getPrimaryKey() && ($vs_target_path = $t_rep->getMediaPath('media', 'original'))) { if ($r_source = fopen($vs_url, "rb")) { if ($r_target = fopen($vs_target_path, "wb")) { while (feof($r_source) === false) { fwrite($r_target, fread($r_source, 1024 * 8), 1024 * 8); } fclose($r_target); } else { $vb_download_errors = true; $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Could not open target path %1', $vs_target_path), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); } fclose($r_source); } else { $vb_download_errors = true; $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Could not open download URL "%1"', $vs_url), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); } // verify the file was downloaded correctly if (($vs_target_md5 = md5_file($vs_target_path)) !== $vs_representation_md5) { unlink($vs_target_path); $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Media file %1 failed to be downloaded from url "%2"; checksums differ: %3/%4', $vs_target_path, $vs_url, $vs_representation_md5, $vs_target_md5), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); $vb_download_errors = true; } } else { $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Invalid representation_id "%1" or target path "%2"', $vn_representation_id, $vs_representation_md5, $vs_target_path), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); $vb_download_errors = true; } } } } if (!$vb_download_errors) { $t_order->setMode(ACCESS_WRITE); $t_order->set('order_status', 'PROCESSED'); $t_order->update(); if ($t_order->numErrors()) { $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Change of order status to PROCESSED from PROCESSED_AWAITING_MEDIA_ACCESS failed for order_id %1: %2', $t_order->getPrimaryKey(), join('; ', $t_order->getErrors())), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); } } } } catch (Exception $e) { // noop } } // Find any orders with status PROCESSED_AWAITING_DIGITIZATION where all media are now present $qr_orders = $o_db->query("\n\t\t\t\tSELECT order_id\n\t\t\t\tFROM ca_commerce_orders\n\t\t\t\tWHERE\n\t\t\t\t\torder_status = 'PROCESSED_AWAITING_DIGITIZATION'\n\t\t\t"); while ($qr_orders->nextRow()) { $t_order = new ca_commerce_orders($qr_orders->get('order_id')); if ($t_order->getPrimaryKey() && !sizeof($t_order->itemsWithNoDownloadableMedia())) { $t_order->setMode(ACCESS_WRITE); $t_order->set('order_status', 'PROCESSED'); $t_order->update(); if ($t_order->numErrors()) { $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Change of order status to PROCESSED from PROCESSED_AWAITING_DIGITIZATION failed for order_id %1: %2', $t_order->getPrimaryKey(), join('; ', $t_order->getErrors())), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); } } } // Find orders paid/shipped more than X days ago and mark them as "COMPLETED" $vn_days = (int) $this->opo_client_services_config->get('completed_order_age_threshold'); if ($vn_days > 1) { $vn_threshold = (int) (time() - $vn_days * 24 * 60 * 60); $qr_orders = $o_db->query("\n\t\t\t\t\tSELECT order_id\n\t\t\t\t\tFROM ca_commerce_orders\n\t\t\t\t\tWHERE\n\t\t\t\t\t\t(order_status = 'PROCESSED') \n\t\t\t\t\t\tAND \n\t\t\t\t\t\t((payment_received_on > 0) AND (payment_received_on < ?))\n\t\t\t\t\t\tAND \n\t\t\t\t\t\t(\n\t\t\t\t\t\t\t(shipping_date IS NULL AND shipped_on_date IS NULL)\n\t\t\t\t\t\t\tOR\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t(shipped_on_date > 0) AND (shipped_on_date < ?)\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t)\n\t\t\t\t", $vn_threshold, $vn_threshold); while ($qr_orders->nextRow()) { $t_order = new ca_commerce_orders($qr_orders->get('order_id')); if ($t_order->getPrimaryKey()) { $t_order->setMode(ACCESS_WRITE); $t_order->set('order_status', 'COMPLETED'); $t_order->update(); if ($t_order->numErrors()) { $t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Change of order status to COMPLETED from PROCESSED failed for order_id %1: %2', $t_order->getPrimaryKey(), join('; ', $t_order->getErrors())), 'SOURCE' => 'clientServicesPlugin->hookPeriodicTask')); } } } } return true; }
/** * */ public function Info() { $t_order = new ca_commerce_orders(); $t_order->set('order_type', 'L'); $this->view->setVar('order_list', $va_order_list = $t_order->getOrders(array('type' => 'L'))); return $this->render('widget_loans_info_html.php', true); }