public function SavePaymentForRejectedCheque(Gyuser_Model_Cobranzas $obj) { list($Day, $Month, $Year) = explode('/', $obj->getDate_paid()); $stampeddate = mktime(12, 0, 0, $Month, $Day, $Year); $Date_paid = date("Y-m-d", $stampeddate); $paymentType = $obj->getPayment_type(); $data = array('client_id' => $obj->getClient_id(), 'operation_id' => $obj->getOperation_id(), 'cheque_id' => $obj->getCheque_id(), 'date_paid' => $Date_paid, 'paid_amount' => $obj->getPaid_amount(), 'previous_balance' => $obj->getPrevious_balance(), 'current_balance' => $obj->getCurrent_balance(), 'payment_type' => $obj->getPayment_type()); $id = (int) $this->getDbTable()->insert($data); if ($id) { $chqMapper = new Gyuser_Model_ChequesDataMapper(); $chqObj = new Gyuser_Model_Cheques(); $chqObj->setBalance($obj->getCurrent_balance()); $chqObj->setId($obj->getCheque_id()); //$chqObj->setRejected_check_payment(1); $updateRes = $chqMapper->UpdateRejectedChequeBalance($chqObj); if ($paymentType == 2) { //cheque propio $newCheObj = $obj->getCheques_obj(); $newCheRes = $chqMapper->InsertChequeForRejectedCheque($newCheObj); if (!$newCheRes) { throw Exception('Error inserting cheque for rejected check on DB'); } } elseif ($paymentType == 3) { //cheque de tercero $BankMapper = new Gyuser_Model_BankAccountsDataMapper(); $newBankObj = $obj->getBank_accounts_obj(); $newBankId = $BankMapper->save($newBankObj); if (!$newBankId) { throw Exception('Error inserting bank account for rejected check on DB'); } $newCheObj = $obj->getCheques_obj(); $newCheObj->setRejected_bank_id($newBankId); $newCheRes = $chqMapper->InsertChequeForRejectedCheque($newCheObj); if (!$newCheRes) { throw Exception('Error inserting cheque for rejected check on DB'); } } if ($updateRes) { /// check if the operation has no pending checks, if it doesn't mark the whole operation as saldada $opMapper = new Gyuser_Model_OperationsDataMapper(); $operationPayed = $opMapper->checkOperationSaldada($obj->getOperation_id()); if ($operationPayed) { $id = $opMapper->setOperationSaldada($obj->getOperation_id()); if (!$id) { throw new Exception('There was an error updating the operation to operacion saldada'); } } else { //op. not yet payed. Here we might have no checks in cobranza but we can still have pending checks because of date. $operationInCobranza = $opMapper->checkOperationCobranza($obj->getOperation_id()); if (!$operationInCobranza) { //no checks in cobranza $id = $opMapper->setOperationEnCartera($obj->getOperation_id()); //get operation back to "cheques en cartera" state if (!$id) { throw new Exception('There was an error updating the operation to cheques en cartera'); } } } /// check if the user has no more cobranzas ops, if it doesn't set it as active $clMapper = new Gyuser_Model_UserDataMapper(); $cobranzasOp = $clMapper->checkCobranzasOp($obj->getClient_id()); if (!$cobranzasOp) { //FIX!! Gus asked to pass clients to active once they finish paying cobranzas $id = $clMapper->setActiveClient($obj->getClient_id()); //$id = $clMapper->setPasiveClient($obj->getClient_id()); if ($id) { $id = -1; } else { throw new Exception('There was an error setting the client to active'); } } } else { throw new Exception('There was an error updating the rejected check balance'); } } else { throw new Exception('There was an error adding the new payment for rejected check'); } return $id; }
public function updateOpAndClientsStatus() { $table = $this->getDbTable(); $select = $table->select(); $select->setIntegrityCheck(false); $select->from(array('operations'), array('id', 'client_id')); $resultSet = $table->fetchAll($select); $opMapper = new Gyuser_Model_OperationsDataMapper(); $clMapper = new Gyuser_Model_UserDataMapper(); foreach ($resultSet as $row) { /// check if the operation has no pending checks, if it doesn't mark the whole operation as saldada $operationPayed = $opMapper->checkOperationSaldada($row->id); if ($operationPayed) { $id = $opMapper->setOperationSaldada($row->id); /// check if the client has no pending operations, if it doesn't mark the client type as pasivo $pasiveClient = $clMapper->checkPasiveClient($row->client_id); if ($pasiveClient) { $id = $clMapper->setPasiveClient($row->client_id); } } elseif ($opMapper->checkOperationCobranza($row->id)) { //operation is en cobranza /// check if the client has no pending operations, if it doesn't mark the client type as pasivo $id = $clMapper->setCobranzaClient($row->client_id); } else { //operation is not saldada and is not in cobranza (client active) $cobranzas = $clMapper->checkCobranzasOp($row->client_id); if (!$cobranzas) { /// check if the client has no other operations in cobranza and mark it as active $id = $clMapper->setActiveClient($row->client_id); } } } }