Exemplo n.º 1
0
 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;
 }