/**
  *
  */
 public function CreateNewOrderFromCommunication()
 {
     if ($pn_communication_id = $this->request->getParameter('communication_id', pInteger)) {
         $t_comm = new ca_commerce_communications($pn_communication_id);
         if (!$t_comm->getPrimaryKey()) {
             $this->notification->addNotification(_t('Invalid message'), __NOTIFICATION_TYPE_ERROR__);
             $this->CustomerInfo();
             return;
         }
         $t_trans = new ca_commerce_transactions($t_comm->get('transaction_id'));
         if (!$t_trans->getPrimaryKey()) {
             $this->notification->addNotification(_t('Message is not associated with a transaction'), __NOTIFICATION_TYPE_ERROR__);
             $this->CustomerInfo();
             return;
         }
         $t_user = new ca_users($t_trans->get('user_id'));
         $this->opt_order->setMode(ACCESS_WRITE);
         $this->opt_order->set('transaction_id', $t_trans->getPrimaryKey());
         if ($t_user->getPrimaryKey()) {
             $this->opt_order->set('billing_fname', $t_user->get('fname'));
             $this->opt_order->set('billing_lname', $t_user->get('lname'));
             $this->opt_order->set('billing_email', $t_user->get('email'));
             $this->opt_order->set('shipping_fname', $t_user->get('fname'));
             $this->opt_order->set('shipping_lname', $t_user->get('lname'));
             $this->opt_order->set('shipping_email', $t_user->get('email'));
             // Pre-populate order with user's profile address
             $va_mapping = array('billing_organization' => 'user_profile_organization', 'billing_address1' => 'user_profile_address1', 'billing_address2' => 'user_profile_address2', 'billing_city' => 'user_profile_city', 'billing_zone' => 'user_profile_state', 'billing_postal_code' => 'user_profile_postalcode', 'billing_country' => 'user_profile_country', 'billing_phone' => 'user_profile_phone', 'billing_fax' => 'user_profile_fax', 'shipping_organization' => 'user_profile_organization', 'shipping_address1' => 'user_profile_address1', 'shipping_address2' => 'user_profile_address2', 'shipping_city' => 'user_profile_city', 'shipping_zone' => 'user_profile_state', 'shipping_postal_code' => 'user_profile_postalcode', 'shipping_country' => 'user_profile_country', 'shipping_phone' => 'user_profile_phone', 'shipping_fax' => 'user_profile_fax');
             foreach ($va_mapping as $vs_field => $vs_pref) {
                 $this->opt_order->set($vs_field, $t_user->getPreference($vs_pref));
             }
         }
         $this->opt_order->set('order_type', 'L');
         // L=loan
         $this->opt_order->insert();
         $this->request->setParameter('order_id', $this->opt_order->getPrimaryKey());
         if (!$this->opt_order->numErrors()) {
             $this->notification->addNotification(_t('Saved changes'), __NOTIFICATION_TYPE_INFO__);
             // Add items
             $t_set = new ca_sets($t_trans->get('set_id'));
             if ($t_set->getPrimaryKey()) {
                 $va_items = $t_set->getItems();
                 foreach ($va_items as $va_item_list) {
                     foreach ($va_item_list as $vn_i => $va_item) {
                         if (!is_array($va_item['selected_services'])) {
                             //$va_item['selected_services'] = array('DIGITAL_COPY');	// TODO: make default configurable
                         }
                         foreach ($va_item['selected_services'] as $vs_service) {
                             if ($t_item = $this->opt_order->addItem($va_item['row_id'], array('service' => $vs_service), array('representations_ids' => is_array($va_item['selected_representations']) && sizeof($va_item['selected_representations']) ? $va_item['selected_representations'] : null))) {
                                 $t_item->updateFee();
                             }
                         }
                     }
                 }
                 // Delete originating set if configured to do so
                 if ($this->opo_client_services_config->get('set_disposal_policy') == 'DELETE_WHEN_ORDER_CREATED') {
                     $t_set->setMode(ACCESS_WRITE);
                     $t_set->delete(true);
                 }
             }
         } else {
             $va_errors['general'] = $this->opt_order->errors();
             $this->notification->addNotification(_t('Errors occurred: %1', join('; ', $this->opt_order->getErrors())), __NOTIFICATION_TYPE_ERROR__);
         }
         $this->view->setVar('errors', $va_errors);
     }
     $this->OrderOverview();
 }
 /**
  *
  */
 public function haveAccessToMessage($pn_user_id, $pn_communication_id = null)
 {
     $t_user = new ca_users($pn_user_id);
     if ($t_user->canDoAction('can_manage_clients')) {
         return true;
     }
     if ($pn_communication_id) {
         $t_comm = new ca_commerce_communications($pn_communication_id);
         if (!$t_comm->getPrimaryKey()) {
             return false;
         }
     } else {
         $t_comm = $this;
     }
     $t_trans = new ca_commerce_transactions($t_comm->get('transaction_id'));
     if ($t_trans->getPrimaryKey()) {
         if ($t_trans->get('user_id') == $pn_user_id) {
             return true;
         }
     }
     return false;
 }