public function nuevomovimientoAction() { $request = $this->request; if ($request->isPost()) { $post_data = $request->getPost(); $fecha = \DateTime::createFromFormat('d/m/Y', $post_data['cajachica_fecha']); //Creamos nuestro movimiento $cajachica = new \Cajachica(); $cajachica->setCajachicaFecha($fecha->format('Y-m-d'))->setIdconceptocajachica($post_data['idconcepto'])->setCajachicaTipomovimiento($post_data['cajachica_tipomoviento'])->setCajachicaCantidad($post_data['cajachica_cantidad'])->setCajachicaComprobante($post_data['cajachica_comprobante'])->setCajachicaPacientedoctor($post_data['cajachica_pacientedoctor'])->setCajachicaNota($post_data['cajachica_nota']); //Ya existe un movimiento? if (\CajachicaQuery::create()->exists()) { //Modificamos el balance de nuestra caja $first_row = \CajachicaQuery::create()->orderByIdcajachica('asc')->findOne(); $current_balance = $first_row->getCajachicaBalance(); if ($post_data['cajachica_tipomoviento'] == 'cargo') { $new_balance = $current_balance + $post_data['cajachica_cantidad']; } else { $new_balance = $current_balance - $post_data['cajachica_cantidad']; } $first_row->setCajachicaBalance($new_balance); $first_row->save(); } else { if ($post_data['cajachica_tipomoviento'] == 'cargo') { $new_balance = 0 + $post_data['cajachica_cantidad']; $cajachica->setCajachicaBalance($new_balance); } else { $new_balance = 0 - $post_data['cajachica_cantidad']; $cajachica->setCajachicaBalance($new_balance); } } $cajachica->save(); if (!$cajachica->isPrimaryKeyNull()) { return $this->getResponse()->setContent(\Zend\Json\Json::encode(array('response' => true, 'data' => array('idconceptocajachica' => $cajachica->getIdconceptocajachica(), 'id' => $cajachica->getIdcajachica(), 'fecha' => $cajachica->getCajachicaFecha('d-m-Y'), 'fecha_js' => $cajachica->getCajachicaFecha('m/d/Y'))))); } else { return $this->getResponse()->setContent(\Zend\Json\Json::encode(array('response' => false))); } } }