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;
     }
 }