Exemplo n.º 1
0
 /**
  * 
  */
 public function getItems($pa_options = null)
 {
     if (isset($pa_options['order_id']) && (int) $pa_options['order_id']) {
         $vn_order_id = (int) $pa_options['order_id'];
     } else {
         $vn_order_id = $this->getPrimaryKey();
     }
     if (!$vn_order_id) {
         return null;
     }
     $o_db = $this->getDb();
     $va_additional_fee_codes = $this->opo_client_services_config->getAssoc($this->get('order_type') == 'L' ? 'additional_loan_fees' : 'additional_order_item_fees');
     $qr_res = $o_db->query("\n\t \t\tSELECT i.*, objl.name, objl.name_sort, objl.locale_id, obj.idno, obj.idno_sort\n\t \t\tFROM ca_commerce_order_items i\n\t \t\tINNER JOIN ca_commerce_orders AS o ON o.order_id = i.order_id\n\t \t\tINNER JOIN ca_objects AS obj ON obj.object_id = i.object_id\n\t \t\tINNER JOIN ca_object_labels AS objl ON objl.object_id = obj.object_id\n\t \t\tWHERE\n\t \t\t\t(o.order_id = ?) AND (objl.is_preferred = 1)\n\t \t\tORDER BY i.rank\n\t \t", (int) $vn_order_id);
     $va_object_ids = $qr_res->getAllFieldValues('object_id');
     $va_item_ids = $qr_res->getAllFieldValues('item_id');
     $va_orders = array();
     // Get representation (page) counts
     $va_rep_counts = $va_total_rep_counts = array();
     $va_item_to_rep_ids = array();
     if (sizeof($va_item_ids)) {
         if ($this->get('order_type') == 'O') {
             $qr_rep_count = $o_db->query("\n\t\t\t\t\tSELECT coixor.item_id, coixor.representation_id, count(*) c\n\t\t\t\t\tFROM ca_commerce_order_items_x_object_representations coixor\n\t\t\t\t\tINNER JOIN ca_object_representations AS o_r ON o_r.representation_id = coixor.representation_id\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tcoixor.item_id IN (?) AND o_r.deleted = 0\n\t\t\t\t\tGROUP BY coixor.item_id\n\t\t\t\t", array($va_item_ids));
             while ($qr_rep_count->nextRow()) {
                 $va_rep_counts[(int) $qr_rep_count->get('item_id')] = (int) $qr_rep_count->get('c');
                 $va_item_to_rep_ids[(int) $qr_rep_count->get('item_id')] = (int) $qr_rep_count->get('representation_id');
             }
         } else {
             $qr_rep_count = $o_db->query("\n\t\t\t\t\tSELECT o.item_id, coixor.representation_id, count(*) c\n\t\t\t\t\tFROM ca_commerce_order_items o\n\t\t\t\t\tINNER JOIN ca_objects_x_object_representations AS coixor ON o.object_id = coixor.object_id\n\t\t\t\t\tINNER JOIN ca_object_representations AS o_r ON o_r.representation_id = coixor.representation_id\n\t\t\t\t\tWHERE\n\t\t\t\t\t\to.item_id IN (?) AND o_r.deleted = 0\n\t\t\t\t\tGROUP BY o.item_id\n\t\t\t\t", array($va_item_ids));
             while ($qr_rep_count->nextRow()) {
                 $va_rep_counts[(int) $qr_rep_count->get('item_id')] = (int) $qr_rep_count->get('c');
                 $va_item_to_rep_ids[(int) $qr_rep_count->get('item_id')] = (int) $qr_rep_count->get('representation_id');
             }
             $qr_rep_count = $o_db->query("\n\t\t\t\t\tSELECT o.item_id, coixor.representation_id\n\t\t\t\t\tFROM ca_commerce_order_items o\n\t\t\t\t\tINNER JOIN ca_objects_x_object_representations AS coixor ON o.object_id = coixor.object_id\n\t\t\t\t\tINNER JOIN ca_object_representations AS o_r ON o_r.representation_id = coixor.representation_id\n\t\t\t\t\tWHERE\n\t\t\t\t\t\to.item_id IN (?) AND o_r.deleted = 0 AND coixor.is_primary = 1\n\t\t\t\t", array($va_item_ids));
             while ($qr_rep_count->nextRow()) {
                 $va_item_to_rep_ids[(int) $qr_rep_count->get('item_id')] = (int) $qr_rep_count->get('representation_id');
             }
         }
         $qr_rep_count = $o_db->query("\n\t\t\t\tSELECT coixor.object_id, count(*) c\n\t\t\t\tFROM ca_objects_x_object_representations coixor\n\t\t\t\tINNER JOIN ca_object_representations AS o_r ON o_r.representation_id = coixor.representation_id\n\t\t\t\tWHERE\n\t\t\t\t\tcoixor.object_id IN (?) AND o_r.deleted = 0\n\t\t\t\tGROUP BY coixor.object_id\n\t\t\t", array($va_object_ids));
         while ($qr_rep_count->nextRow()) {
             $va_total_rep_counts[(int) $qr_rep_count->get('object_id')] = (int) $qr_rep_count->get('c');
         }
     }
     $qr_res->seek(0);
     // Get representations
     $t_rep = new ca_object_representations();
     $va_reps = $t_rep->getRepresentationMediaForIDs(array_values($va_item_to_rep_ids), array('thumbnail'));
     while ($qr_res->nextRow()) {
         $va_row = $qr_res->getRow();
         $vn_item_id = (int) $qr_res->get('item_id');
         $va_row['fee'] = sprintf("%4.2f", $va_row['fee']);
         $va_row['tax'] = sprintf("%4.2f", $va_row['tax']);
         $va_row['shipping_cost'] = sprintf("%4.2f", $va_row['shipping_cost']);
         $va_row['handling_cost'] = sprintf("%4.2f", $va_row['handling_cost']);
         $va_row['thumbnail_tag'] = $va_reps[$va_item_to_rep_ids[$va_row['item_id']]]['tags']['thumbnail'];
         $va_row['selected_representation_count'] = (int) $va_rep_counts[$vn_item_id];
         $va_row['representation_count'] = (int) $va_total_rep_counts[(int) $qr_res->get('object_id')];
         if (!is_array($va_fees = caUnserializeForDatabase($va_row['additional_fees']))) {
             $va_fees = array();
         }
         $vn_fee_total = 0;
         foreach ($va_additional_fee_codes as $vs_code => $va_info) {
             if (isset($va_fees[$vs_code])) {
                 $vn_fee = (double) $va_fees[$vs_code];
                 $vn_fee_total += $vn_fee;
             } else {
                 $vn_fee = (double) $va_info['default_cost'];
             }
             $va_row['ADDITIONAL_FEE_' . $vs_code] = sprintf("%4.2f", $vn_fee);
         }
         $va_row['additional_fees_total'] = sprintf("%4.2f", $vn_fee_total);
         $va_orders[$vn_item_id][(int) $qr_res->get('locale_id')] = $va_row;
     }
     $va_orders = caExtractValuesByUserLocale($va_orders);
     return $va_orders;
 }