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 GetTercerosCheques() { $table = $this->getDbTable(); $select = $table->select(); $select->setIntegrityCheck(false); $select->from(array('cqu' => 'cheques'), array('id as cheque_id', 'operation_id', 'date as cheque_date', 'check_n', 'balance', 'amount as cheque_amount', 'liquidacion_id', 'status as cheque_status', 'terceros_today_value', 'terceros_tasa_anual', 'terceros_days')); $select->joinLeft(array('cls1' => 'clients'), 'cqu.client_id = cls1.id', array('id as tercero_client_id', 'first_name as tercero_first_name', 'last_name as tercero_last_name')); $select->joinInner(array('ops' => 'operations'), 'cqu.operation_id = ops.id', array('amount as operation_amount', 'type as operation_type')); $select->joinLeft(array('cls' => 'clients'), 'ops.client_id = cls.id', array('id as client_id', 'first_name', 'last_name', 'business')); //$select->joinLeft(array('cps' => 'credit_providers'), 'cqu.credit_provider_id = cps.id', array('name as sup_name')); $select->joinLeft(array('cps' => 'providers'), 'cqu.provider_id = cps.id', array('name as prov_name')); $select->joinLeft(array('chqs' => 'cheques_status'), 'cqu.status = chqs.id', array('status_list')); $select->joinLeft(array('liqs' => 'liquidaciones'), 'cqu.liquidacion_id = liqs.id', array('date as liq_date')); $select->where('ops.type = ?', 2); //op with terceros $select->where('cqu.status != ?', 5); //5=cheque fantasma $select->where('cqu.status != ?', 6); //6=en proceso $select->order('cqu.id ASC'); $resultSet = $table->fetchAll($select); $entries = array(); foreach ($resultSet as $row) { $entry = new Gyuser_Model_Cheques(); $entry->setId($row->cheque_id); $entry->setOperation_id($row->operation_id); $entry->setOperation_type($row->operation_type); $entry->setClient_id($row->client_id); $entry->setFirst_name($row->first_name); $entry->setLast_name($row->last_name); $entry->setTercero_client_id($row->tercero_client_id); $entry->setTercero_first_name($row->tercero_first_name); $entry->setTercero_last_name($row->tercero_last_name); $entry->setTerceros_today_value($row->terceros_today_value); $entry->setTerceros_tasa_anual($row->terceros_tasa_anual); $entry->setTerceros_days($row->terceros_days); $entry->setEmpresa($row->business); $entry->setDate(date("d/m/Y", strtotime($row->cheque_date))); $entry->setCheck_n($row->check_n); $entry->setAmount($row->cheque_amount); $entry->setBalance($row->balance); $entry->setStatus($row->cheque_status); $entry->setStatus_name($row->status_list); if ($row->liquidacion_id) { //the check was passed to a prov $entry->setLiquidacion_id($row->liquidacion_id); $entry->setCave_name($row->prov_name); $entry->setLiq_date(date("d/m/Y", strtotime($row->liq_date))); /* if($row->sup_name) //it was passed to a credit provider $entry->setCave_name($row->sup_name); else //otherwise if name is null it means it was passed to lavalle $entry->setCave_name('Lavalle'); */ } $entries[] = $entry; } return $entries; }