/** * Funció que guarda una sessió **/ public function doSave($fromFormulari = false) { //Si entrem aquí des del formulari hem de validar les dades if ($fromFormulari) { if (empty($this->sessio['tmp_Dia'])) { throw new MyException("Has d'entrar un dia."); } if (empty($this->sessio['tmp_Hora'])) { throw new MyException("Has d'entrar una hora."); } if (empty($this->sessio['tmp_DiaCompra'])) { throw new MyException("Has d'entrar un dia d'inici de venda."); } if (empty($this->sessio['tmp_HoraCompra'])) { throw new MyException("Has d'entrar una hora d'inici de venda."); } if (empty($this->sessio['s_idTeatre'])) { throw new MyException("Has d'escollir un teatre."); } if (empty($this->sessio['s_Preus'])) { throw new MyException("Has d'entrar preus per un espectacle."); } if (empty($this->sessio['s_idEspectacle']) || !is_numeric($this->sessio['s_idEspectacle'])) { throw new MyException("Les sessions han de tenir un espectacle relacionat."); } $PreuVisibleInternet = false; foreach ($this->sessio['s_Preus'] as $OP) { if ($OP->isVisibleInternet()) { $PreuVisibleInternet = true; } $OP->ValidaPreu(); } if (!$PreuVisibleInternet && $this->sessio['s_VisibleWeb']) { throw new MyException("Si és visible a Internet, cal que hi hagi preus visibles a internet."); } //Convertim la data i hora en format UNIX $this->sessio['s_DataHora'] = G::ConvertToUnixTimestamp($this->sessio['tmp_Dia'], $this->sessio['tmp_Hora'], true); //Convertim la data i hora d'obertura de venda a UNIX $this->sessio['s_DataHoraCompra'] = G::ConvertToUnixTimestamp($this->sessio['tmp_DiaCompra'], $this->sessio['tmp_HoraCompra'], true); //Calculem els seients totals i els lliures si cal if ($this->sessio['s_Localitats'] == 0) { $TTO = new TeatreTableObject(null, TeatreTableObject::FROM_EMPTY); $TTO->loadById($this->sessio['s_idTeatre']); $this->sessio['s_Localitats'] = $TTO->getNumeroLocalitats(); } if ($this->sessio['s_SeientsLliures'] == 0) { $this->sessio['s_SeientsLliures'] = $this->sessio['s_Localitats']; } // if( $this->sessio['s_SeientsLliures'] > 0 ) throw new MyException("Estàs modificant un espectacle o sessió que ja té entrades venudes. Allibera les places abans de poder-ho modificar."); //Marquem que ja és correcte i el guardem $this->sessio['s_Correcte'] = 1; } //Convertim els preus en format json per guardar $this->sessio['s_Preus'] = json_encode($this->sessio['s_Preus']); //Esborrem els camps de suport $this->treuExtres(); //Guardem la sessió if (!$this->getSessioId()) { $this->sessio['s_id'] = $this->db->insert('sessions', $this->sessio); } else { $this->db->update('sessions', $this->sessio, array('s_id' => $this->sessio['s_id'])); } //Actualitzo la taula de mesos $stmt = $this->db->prepare('CALL update_mesos()'); $stmt->execute(); }