public function createliquidacionAction()
 {
     try {
         $request = $this->getRequest();
         if ($this->getRequest()->isPost()) {
             $liqDataId = 0;
             $liqId = (int) $request->id;
             //if there's an empty string it defaults to 0
             $liqDate = $request->date;
             $variableDate = false;
             $lMapper = new Gyuser_Model_LiquidacionesDataMapper();
             $liq = new Gyuser_Model_Liquidaciones();
             $liq->setProvider_id($request->provider_id);
             $liq->setCommitted((int) $request->committed);
             $pMapper = new Gyuser_Model_ProvidersDataMapper();
             $prov = $pMapper->GetProviderById($liq->getProvider_id());
             $cMapper = new Gyuser_Model_ChequesDataMapper();
             $oMapper = new Gyuser_Model_OperationsDataMapper();
             //if liq has already been submitted then cleanup previous
             //selected cheques, rejected cheques and operations
             if ($liqId) {
                 $chequesList = $cMapper->UpdateLiquidacionDeleted($liqId);
                 $opertationsList = $oMapper->UpdateLiquidacionDeleted($liqId);
             } else {
                 $liqId = $lMapper->createNewLiq();
             }
             //check liq id has been created.
             if ($liqId) {
                 $liq->setId($liqId);
                 //depending on the status of the liquidacion
                 //set the appropriate dates and/or liq data
                 if ($liq->getCommitted() == 0) {
                     //set current date and current prov values
                     list($Day, $Month, $Year) = explode('/', $liqDate);
                     $liqDate = mktime(12, 0, 0, $Month, $Day, $Year);
                     $liqDate = date("Y-m-d", $liqDate);
                     $today = date("Y-m-d");
                     if ($liqDate != $today) {
                         $liq->setDate($liqDate);
                         $variableDate = true;
                     } else {
                         $liq->setDate($today);
                     }
                     //even if this date won't be stored in db, we needed to calculate checks today value
                     $provData = $prov;
                     //set the current provider values as prov data
                 } elseif ($liq->getCommitted() == 2) {
                     //status is en camino. save date and prov values
                     $liqData = $lMapper->GetLiquidacionesById($liq);
                     //set date as today or as date selected by user
                     if ($liqData['variable_date']) {
                         $liq->setDate($liqData['date']);
                         $variableDate = true;
                     } else {
                         $liq->setDate(date('Y-m-d'));
                     }
                     //set current prov values into liq for db save
                     $liq->setTasa_anual($prov->getTasa_anual());
                     $liq->setImpuesto_al_cheque($prov->getImpuesto_al_cheque());
                     $liq->setAcreditacion_capital($prov->getAcreditacion_capital());
                     $liq->setAcreditacion_interior($prov->getAcreditacion_interior());
                     $liq->setGastos_general($prov->getGastos_general());
                     $liq->setGastos_interior($prov->getGastos_interior());
                     $liq->setGastos_cheque_a_1($prov->getGastos_cheque_a_1());
                     $liq->setGastos_cheque_menor_a_1($prov->getGastos_cheque_menor_a_1());
                     $liq->setGastos_cheque_a_2($prov->getGastos_cheque_a_2());
                     $liq->setGastos_cheque_menor_a_2($prov->getGastos_cheque_menor_a_2());
                     $liq->setDate_delivered(date("Y-m-d"));
                     $provData = $prov;
                     //set the current provider values as prov data
                 } elseif ($liq->getCommitted() == 1) {
                     //to be consolidated get the prov values stored in liq
                     $liqData = $lMapper->GetLiquidacionesById($liq);
                     //set date as date saved on step 2
                     $liq->setDate($liqData['date']);
                     $provData = $lMapper->getProvData($liqId);
                     //set the saved prov data on liq to calculate checks values
                 }
                 /* update Liquidacion id in cheques */
                 $checksJson = json_decode($request->cheques_json);
                 $checkObj = new Gyuser_Model_Cheques();
                 $checkObj->setLiquidacion_id($liqId);
                 $checkObj->setProvider_id($liq->getProvider_id());
                 foreach ($checksJson as $check) {
                     $checkObj->setId($check->cheque_id);
                     if ($check->acreditacion_hrs) {
                         $checkObj->setAcreditacion_hrs($check->acreditacion_hrs);
                     }
                     $cMapper->UpdateLiquidacion($checkObj);
                 }
                 /* update Liquidacion id in rejected cheques */
                 $rejected_cheques_json = json_decode($request->rejected_cheques_json);
                 $rejCheckTotal = 0;
                 foreach ($rejected_cheques_json as $check) {
                     $checkDetails = $cMapper->getCheckDetails($check->rejected_cheque_id);
                     $gastos_type = $checkDetails->getRejected_type();
                     if ($gastos_type == "Denuncia") {
                         $checkDetails->setRejected_cost_prov($prov->getGastos_denuncia());
                     } elseif ($gastos_type == "Sin Fondos") {
                         $checkDetails->setRejected_cost_prov($prov->getGastos_rechazo());
                     }
                     //set the current liq id as the liq id where the rejected check was payed
                     $checkDetails->setRejected_liq_id($liqId);
                     //add the check total to the rej checks total
                     $rejCheckTotal += $checkDetails->getAmount() + $checkDetails->getRejected_cost_prov();
                     $cMapper->UpdateRejectedCheques($checkDetails);
                 }
                 $opsTotal = 0;
                 /* update Liquidacion id in operations */
                 $operations_json = json_decode($request->operations_json);
                 $opDetails = new Gyuser_Model_Operations();
                 $opDetails->setLiquidacion_id($liqId);
                 $commissionRate = $prov->getComisionRate();
                 foreach ($operations_json as $operation) {
                     $opDetails->setId($operation->operation_id);
                     $oMapper->UpdateLiquidacion($opDetails);
                     $amount = (double) $oMapper->GetAmount($operation->operation_id);
                     $commissionAmt = (double) $pMapper->calculateComisionAmt($amount, $commissionRate);
                     $opsTotal += (double) $pMapper->calculateProvPayment($amount, $commissionAmt);
                 }
                 $checksTotals = $cMapper->GetChequesTotalsJson($liq->getProvider_id(), $checksJson, $provData, $liq->getDate());
                 $acreditacion = floatval($request->acreditacion);
                 $amountDebt = $rejCheckTotal + $opsTotal - $acreditacion;
                 $accountBalance = $checksTotals['payingAmount'] - $amountDebt;
                 $liq->setId($liqId);
                 $liq->setAcreditacion($acreditacion);
                 $liq->setAmount_debt($amountDebt);
                 $liq->setAmount_payed($checksTotals['payingAmount']);
                 $liq->setPrevious_account_balance($prov->getBalance());
                 $liq->setCurrent_account_balance($accountBalance);
                 $liq->setChecks_qty($checksTotals['chequeChkCount']);
                 $liq->setAverage_days($checksTotals['dayAvg']);
                 $liq->setTotal_bruto($checksTotals['bruto']);
                 $liq->setImpuesto_al_cheque_amt($checksTotals['impuestoAlCheque']);
                 //the amount for impuesto al cheque
                 $liq->setIntereses($checksTotals['intereses']);
                 //also called descuento
                 $liq->setGastos_interior_fee($checksTotals['gastosInterior']);
                 //the amount for interior gastos
                 $liq->setGastos_general_fee($checksTotals['gastosGeneral']);
                 //the amount for general gastos
                 $liq->setGastos_varios($checksTotals['gastosOtros']);
                 $liq->setTotal_neto($checksTotals['payingAmount']);
                 $lMapper->createLiq($liq, $variableDate);
                 //update current balance
                 $prov->setBalance($accountBalance);
                 $pMapper->UpdateBalance($prov);
             } else {
                 throw new Exception('error inserting new liquidacion');
             }
             $this->_helper->redirector('providers', 'liquidaciones', 'gyuser', array('provider_id' => $request->provider_id));
         }
     } catch (Exception $e) {
         echo $e;
     }
 }
Ejemplo n.º 2
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;
 }
Ejemplo n.º 3
0
 public function RejectedChequeBySupplierId(Gyuser_Model_Cheques $obj)
 {
     $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', 'amount', 'local', 'liquidacion_id', 'status as cheque_status', 'rejected_cost', 'rejected_type', 'check_zip_code', 'credit_provider_id'));
     $select->joinLeft(array('lqs' => 'liquidaciones'), 'lqs.id = cqu.liquidacion_id');
     $select->joinLeft(array('ops' => 'operations'), 'cqu.operation_id = ops.id', array('client_id', 'bank_account_id'));
     $select->joinLeft(array('bas' => 'bank_accounts'), 'ops.bank_account_id = bas.id', array('bank_name'));
     $select->joinLeft(array('cls' => 'clients'), 'ops.client_id = cls.id', array('first_name', 'last_name'));
     $select->joinLeft(array('chqs' => 'cheques_status'), 'cqu.status = chqs.id', array('id as cheque_state_id', 'status_list'));
     $select->where('cqu.credit_provider_id = ?', $obj->getCredit_provider_id());
     //$select->where('ops.liquidacion_id IS NOT NULL');
     $select->where('cqu.status = ?', 3);
     //$select->where('cqu.local = ?', 1);
     //$select->where('cqu.date <= ?', date("Y-m-d"));
     $select->where('cqu.rejected_liquidacion_id IS NULL');
     //$select->where('cqu.status = ?', '');
     $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->setDate(date("d/m/Y", strtotime($row->cheque_date)));
         $entry->setCheck_n($row->check_n);
         $entry->setAmount($row->amount);
         $entry->setStatus($row->cheque_status);
         $entry->setRejected_gastos($row->rejected_cost);
         $entry->setRejected_type($row->rejected_type);
         $entry->setCheck_zip_code($row->check_zip_code);
         $entry->setLiquidacion_id($row->liquidacion_id);
         $entry->setCredit_provider_id($row->credit_provider_id);
         $clientObj = new Gyuser_Model_User();
         $clientObj->setFirst_name($row->first_name);
         $clientObj->setLast_name($row->last_name);
         $entry->setClients_obj($clientObj);
         $cheques_state_obj = new Gyuser_Model_ChequesStatus();
         $cheques_state_obj->setId($row->cheque_state_id);
         $cheques_state_obj->setStatus_list($row->status_list);
         $entry->setCheques_status_obj($cheques_state_obj);
         $_bank_accounts_obj = new Gyuser_Model_BankAccounts();
         $_bank_accounts_obj->setBank_name($row->bank_name);
         $entry->setBank_accounts_obj($_bank_accounts_obj);
         $entries[] = $entry;
     }
     return $entries;
 }
Ejemplo n.º 4
0
 public function rejectchequewithgastosajaxAction()
 {
     try {
         $request = $this->getRequest();
         if ($this->getRequest()->isPost()) {
             $this->_helper->layout->disableLayout();
             $this->_helper->viewRenderer->setNoRender();
             $mapper = new Gyuser_Model_ChequesDataMapper();
             $Obj = new Gyuser_Model_Cheques();
             $Obj->setId($request->id);
             $Obj->setRejected_gastos($request->gastos);
             $Obj->setRejected_type($request->gastos_type);
             $result = $mapper->RejectChequeWithGastos($Obj);
             if ($result) {
                 echo $result;
             } else {
                 echo "f";
             }
         }
     } catch (Exception $e) {
         echo $e;
     }
 }
Ejemplo n.º 5
0
 public function OperationStateChange(Gyuser_Model_Operations $obj, $chequesList = null, $chequeId = null)
 {
     $success = 0;
     $opResult = $this->GetStateByOperationId($obj);
     $currentStateId = (int) $opResult['stateid'];
     $newStateId = $obj->getState();
     //fix get op client_id and bank_account_id
     $op = $this->getDetailsByOpId($obj->getId());
     $obj->setClient_id($op->getClient_id());
     $obj->setBank_account_id($op->getBank_account_id());
     if ($currentStateId < $newStateId) {
         switch ($newStateId) {
             case 2:
                 $data = array('state_order_id' => $obj->getState(), 'cave_id' => $obj->getCave_id());
                 break;
             case 3:
             case 4:
                 $data = array('state_order_id' => $obj->getState(), 'report' => $obj->getReport());
                 break;
             case 5:
                 $mapper = new Gyuser_Model_OperationsDataMapper();
                 $arc_hrs = $mapper->GetAcreditacionHrsByOperationId($obj);
                 $chequesMapper = new Gyuser_Model_ChequesDataMapper();
                 $cheques = new Gyuser_Model_Cheques();
                 $cheques->setOperation_id($obj->getId());
                 $cheques->setClient_id($obj->getClient_id());
                 $cheques->setBank_account_id($obj->getBank_account_id());
                 $cheques->setAcreditacion_hrs($arc_hrs);
                 $cheques->setCheques_list($chequesList);
                 $result = $chequesMapper->SaveCheques($cheques);
                 $iMapper = new Gyuser_Model_InterestsDataMapper();
                 $interests = new Gyuser_Model_Interests();
                 $interests = $iMapper->getInterestRate($obj->getInterests_id());
                 $data = array('state_order_id' => $obj->getState(), 'amount' => $obj->getAmount(), 'plan_id' => $obj->getPlan_id(), 'interests_id' => $obj->getInterests_id(), 'tasa_porcentual' => $interests->getRate());
                 break;
             case 8:
             case 9:
                 $chequesMapper = new Gyuser_Model_ChequesDataMapper();
                 $cheques = new Gyuser_Model_Cheques();
                 $cheques->setId($chequeId);
                 $cheques->setOperation_id($obj->getId());
                 $cheques->setClient_id($obj->getClient_id());
                 $cheques->setBank_account_id($obj->getBank_account_id());
                 $cheques->setCheques_list($chequesList);
                 $chequesResult = $chequesMapper->SaveCheques($cheques);
                 $data = array('amount' => $obj->getAmount(), 'plan_id' => $obj->getPlan_id(), 'state_order_id' => $obj->getState());
                 break;
             case 6:
             case 7:
                 $chequesMapper = new Gyuser_Model_ChequesDataMapper();
                 $cheques = new Gyuser_Model_Cheques();
                 $cheques->setOperation_id($obj->getId());
                 $data = array('state_order_id' => $obj->getState());
                 break;
             case 99:
                 $chequesMapper = new Gyuser_Model_ChequesDataMapper();
                 $cheques = new Gyuser_Model_Cheques();
                 $cheques->setOperation_id($obj->getId());
                 $result = $chequesMapper->deleteByOperationId($cheques);
                 $data = array('state_order_id' => $obj->getState());
                 break;
             default:
                 $data = array('state_order_id' => $obj->getState());
                 break;
         }
         $id = (int) $obj->getId();
         $id = $this->getDbTable()->update($data, array('id = ?' => $id));
         $success = 1;
     } elseif ($currentStateId >= $newStateId) {
         //new state is smaller or equal to current state
         $success = 2;
     }
     //2 = the state has already been changed.
     return $success;
 }