function admin_print_pdf($hamper_id, $hamper_user_id = null, $save = false)
 {
     Configure::write('debug', 0);
     $this->layout = 'pdf';
     //dettagli paniere
     $hamper = $this->Hamper->find('first', array('conditions' => array('Hamper.id' => $hamper_id), 'contain' => array('Seller.name')));
     //dettagli prodotti ordinati per stampa pdf
     $conditions = array('OrderedProduct.hamper_id' => $hamper_id);
     if (isset($hamper_user_id)) {
         $conditions['OrderedProduct.user_id'] = $hamper_user_id;
         //            print_r($conditions); die();
     }
     $_orderedProducts = $this->Hamper->OrderedProduct->find('all', array('conditions' => $conditions, 'order' => array('Product.product_category_id asc', 'User.last_name asc', 'User.first_name asc', 'Product.name'), 'contain' => array('User' => array('fields' => array('id', 'fullname', 'phone', 'mobile', 'email')), 'Product' => array('fields' => array('id', 'name', 'code', 'units', 'option_1', 'option_2', 'product_category_id', 'items_in_a_box')), 'Product.ProductCategory' => array('fields' => array('id', 'name')))));
     $orderedProducts = array();
     foreach ($_orderedProducts as $product) {
         $user_id = $product['User']['id'];
         $orderedProducts[$user_id]['User'] = $product['User'];
         $orderedProducts[$user_id]['Products'][] = $product;
         //calcolo il totale per utente
         if (isset($orderedProducts[$user_id]['Total'])) {
             $orderedProducts[$user_id]['Total'] += $product['OrderedProduct']['value'];
         } else {
             $orderedProducts[$user_id]['Total'] = $product['OrderedProduct']['value'];
         }
     }
     //nuova query per ordinata per categoria
     $categoriesProducts = $this->Hamper->OrderedProduct->find('all', array('conditions' => $conditions, 'fields' => array('hamper_id', 'product_id', 'option_1', 'option_2', 'SUM(OrderedProduct.value) as total', 'SUM(OrderedProduct.quantity) as quantity'), 'group' => array('Product.product_category_id', 'product_id', 'OrderedProduct.option_1', 'OrderedProduct.option_2', 'OrderedProduct.note'), 'order' => array('product_category_id'), 'contain' => array('Product.id', 'Product.name', 'Product.units', 'Product.option_1', 'Product.option_2', 'Product.items_in_a_box', 'Hamper.delivery_date_on', 'Product.ProductCategory.id', 'Product.ProductCategory.name', 'Product.code')));
     $categories = array();
     foreach ($categoriesProducts as $i => $categoryProduct) {
         $categoryId = $categoryProduct['Product']['product_category_id'];
         $categoryData = $categoryProduct['Product']['ProductCategory'];
         $productId = $categoryProduct['Product']['id'];
         $productData = $categoryProduct['Product'];
         $categories[$categoryId]['ProductCategory'] = $categoryData;
         $categories[$categoryId]['Product'][$i]['Product'] = $productData;
         $categories[$categoryId]['Product'][$i]['OrderedProduct'] = $categoryProduct['OrderedProduct'];
         $categories[$categoryId]['Product'][$i]['OrderedProduct']['total'] = $categoryProduct['0']['total'];
         $categories[$categoryId]['Product'][$i]['OrderedProduct']['quantity'] = $categoryProduct['0']['quantity'];
     }
     // totale
     $total = array_sum(Set::extract('/Total', $orderedProducts));
     //trovo l'elenco degli utenti con ordini attivi
     $users = $this->Hamper->OrderedProduct->getPendingUsers();
     //trovo l'elenco dei produttori con ordini attivi
     $sellers = $this->Hamper->OrderedProduct->getPendingSellers();
     $this->set(compact('categories', 'orderedProducts', 'hamper', 'users', 'sellers', 'total', 'hamper_user_id', 'save'));
     if (!date_is_empty($hamper['Hamper']['delivery_date_on'])) {
         $pageTitle = Inflector::slug(Configure::read('GAS.name') . '_' . $hamper['Seller']['name'] . '_' . date('d-m-Y', strtotime($hamper['Hamper']['delivery_date_on']))) . '.pdf';
     } else {
         $pageTitle = Inflector::slug(Configure::read('GAS.name') . '_' . $hamper['Seller']['name']) . '.pdf';
     }
     $this->set('pageTitle', $pageTitle);
 }
 function admin_print_excel_hamper($hamper_id)
 {
     //         Configure::write('debug', 2);
     $this->layout = 'excel';
     $hamper = $this->OrderedProduct->Hamper->find('first', array('conditions' => array('Hamper.id' => $hamper_id), 'contain' => array('Seller.name')));
     $users = $this->OrderedProduct->find('all', array('conditions' => array('OrderedProduct.hamper_id' => $hamper_id), 'fields' => array('user_id', 'SUM(OrderedProduct.value) as total', 'SUM(OrderedProduct.quantity) as quantity'), 'group' => array('user_id'), 'order' => array('User.id'), 'contain' => array('User' => array('fields' => array('id', 'first_name', 'last_name')))));
     $totals = $this->OrderedProduct->find('all', array('conditions' => array('OrderedProduct.hamper_id' => $hamper_id), 'fields' => array('hamper_id', 'product_id', 'Product.code', 'option_1', 'option_2', 'SUM(OrderedProduct.value) as total', 'SUM(OrderedProduct.quantity) as quantity'), 'group' => array('hamper_id', 'product_id', 'OrderedProduct.option_1', 'OrderedProduct.option_2'), 'order' => array('hamper_id desc', 'Product.name'), 'contain' => array('Product.name', 'Product.code', 'Product.option_1', 'Product.option_2', 'Product.units', 'Product.value', 'Hamper.delivery_date_on')));
     foreach ($totals as $key => $product) {
         foreach ($users as $user) {
             $partial = $this->OrderedProduct->find('all', array('conditions' => array('OrderedProduct.hamper_id' => $hamper_id, 'OrderedProduct.user_id' => $user['User']['id'], 'OrderedProduct.product_id' => $product['Product']['id'], 'OrderedProduct.option_1' => $product['OrderedProduct']['option_1'], 'OrderedProduct.option_2' => $product['OrderedProduct']['option_2']), 'fields' => array('SUM(OrderedProduct.value) as total', 'SUM(OrderedProduct.quantity) as quantity'), 'group' => array('hamper_id', 'product_id', 'OrderedProduct.option_1', 'OrderedProduct.option_2'), 'order' => array('hamper_id desc', 'Product.name'), 'contain' => array('Product.name', 'Product.option_1', 'Product.option_2', 'Hamper.delivery_date_on')));
             if (!isset($partial[0])) {
                 $partial[0] = '';
             }
             $totals[$key]['Users'][$user['User']['id']] = $partial[0];
         }
     }
     $this->set(compact('hamper', 'totals', 'users'));
     if (!date_is_empty($hamper['Hamper']['delivery_date_on'])) {
         $pageTitle = Inflector::slug(Configure::read('GAS.name') . '_' . $hamper['Seller']['name'] . '_' . date('d-m-Y', strtotime($hamper['Hamper']['delivery_date_on'])));
     } else {
         $pageTitle = Inflector::slug(Configure::read('GAS.name') . '_' . $hamper['Seller']['name']);
     }
     $this->set('title_for_layout', $pageTitle);
 }