/** * Performs the work of inserting or updating the row in the database. * * If the object is new, it inserts it; otherwise an update is performed. * All related objects are also updated in this method. * * @param PropelPDO $con * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations. * @throws PropelException * @see save() */ protected function doSave(PropelPDO $con) { $affectedRows = 0; // initialize var to track total num of affected rows if (!$this->alreadyInSave) { $this->alreadyInSave = true; // We call the save method on the following object(s) if they // were passed to this object by their corresponding set // method. This object relates to these object(s) by a // foreign key reference. if ($this->aBanco !== null) { if ($this->aBanco->isModified() || $this->aBanco->isNew()) { $affectedRows += $this->aBanco->save($con); } $this->setBanco($this->aBanco); } if ($this->isNew() || $this->isModified()) { // persist changes if ($this->isNew()) { $this->doInsert($con); } else { $this->doUpdate($con); } $affectedRows += 1; $this->resetModified(); } $this->alreadyInSave = false; } return $affectedRows; }
public function nuevomovimientoAction() { $request = $this->request; if ($request->isPost()) { $post_data = $request->getPost(); $fecha = \DateTime::createFromFormat('d/m/Y', $post_data['banco_fecha']); //Creamos nuestro movimiento $banco = new \Banco(); $banco->setBancoFecha($fecha->format('Y-m-d'))->setIdconceptobanco($post_data['idconcepto'])->setBancoTipomovimiento($post_data['banco_tipomoviento'])->setBancoCantidad($post_data['banco_cantidad'])->setBancoComprobante($post_data['banco_comprobante'])->setBancoNota($post_data['banco_nota']); //Ya existe un movimiento? if (\BancoQuery::create()->exists()) { //Modificamos el balance de nuestra caja $first_row = \BancoQuery::create()->orderByIdbanco('asc')->findOne(); $current_balance = $first_row->getBancoBalance(); if ($post_data['banco_tipomoviento'] == 'cargo') { $new_balance = $current_balance + $post_data['banco_cantidad']; } else { $new_balance = $current_balance - $post_data['banco_cantidad']; } $first_row->setBancoBalance($new_balance); $first_row->save(); } else { if ($post_data['banco_tipomoviento'] == 'cargo') { $new_balance = 0 + $post_data['banco_cantidad']; $banco->setBancoBalance($new_balance); } else { $new_balance = 0 - $post_data['banco_cantidad']; $banco->setBancoBalance($new_balance); } } $banco->save(); $fecha = new \DateTime($banco->getBancoFecha()); $fechaJS = $fecha->format('m/d/Y'); $fecha = $fecha->format('d-m-Y'); if (!$banco->isPrimaryKeyNull()) { return $this->getResponse()->setContent(\Zend\Json\Json::encode(array('response' => true, 'data' => array('id' => $banco->getIdbanco(), 'fecha' => $fecha, 'fecha_js' => $fechaJS)))); } else { return $this->getResponse()->setContent(\Zend\Json\Json::encode(array('response' => false))); } } }