public function add() { if ($this->request->is('post') || $this->request->is('put')) { $this->Expense->create(); unset($this->request->data['Template']); $this->request->data['Expense']['date'] = $this->Expense->dateFormatBeforeSave($this->request->data['Expense']['date']); //pr($this->Expense->find('first')); //pr($this->request->data); //die(); //$this->Expense->unBindModel(array('belongsTo' => array('Buyer'))); //$this->Expense->unBindModel(array('belongsTo' => array('House'))); if ($this->Expense->saveAll($this->request->data, array('deep' => true))) { // mandar notificacion via email foreach ($this->request->data['Perro'] as $perro) { $users_idTo[$perro['user_id']] = $perro['user_id']; } $namesTo = $this->Expense->Buyer->find('list', array('fields' => array('Buyer.id', 'Buyer.full_name'), 'conditions' => array('Buyer.id' => $users_idTo))); $users_idTo[$this->request->data['Expense']['user_id']] = $this->request->data['Expense']['user_id']; $emailsTo = $this->Expense->Buyer->find('list', array('fields' => array('Buyer.id', 'Buyer.email'), 'conditions' => array('Buyer.id' => $users_idTo, 'Buyer.notifications' => 1))); $nameFrom = $this->Expense->Buyer->find('list', array('fields' => array('Buyer.id', 'Buyer.full_name'), 'conditions' => array('Buyer.id' => $this->request->data['Expense']['user_id']))); $houseName = $this->Expense->House->find('list', array('fields' => array('House.id', 'House.name'), 'conditions' => array('House.id' => $this->request->data['Expense']['house_id']))); $vars = array('page' => NAME_SYSTEM, 'type' => 'Registro de Gasto', 'data' => $this->request->data['Expense'], 'nameHouse' => implode('', $houseName), 'nameFrom' => implode('', $nameFrom), 'namesTo' => implode(', ', $namesTo), 'countTo' => count($namesTo)); $this->GenPassword->sendPassword('send_notification', 'Notifications', $emailsTo, $vars); // end mandar notificacion via email $this->Session->setFlash(__('Gasto almacenado correctamente.'), 'alert', array('plugin' => 'BoostCake', 'class' => 'alert-success')); //$this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('El Gasto no se ha almacenado, intente de nuevo.'), 'alert', array('plugin' => 'BoostCake', 'class' => 'alert-danger')); } $this->redirect(array('action' => 'index')); } else { App::import('Model', 'Service'); $mService = new Service(); App::import('Model', 'House'); $mHouse = new House(); $users_id = $mHouse->query('SELECT user_id FROM houses_users WHERE house_id="' . $this->Session->read("Auth.User.House") . '"'); $users_id = Set::classicExtract($users_id, '{n}.houses_users.user_id'); $this->set(array('sessionHouse' => $this->Session->read("Auth.User.House"), 'houses' => $this->Expense->House->find("list"), 'users' => $this->Expense->Buyer->find('list', array('conditions' => array('Buyer.id' => $users_id, 'Buyer.active' => 1))), 'sessionUser' => $this->Session->read("Auth.User"), 'services' => $mService->find('list', array('conditions' => array('Service.active' => 1))))); } }