public static function boot() { parent::boot(); // Setup event bindings... Balance::saving(function ($balance) { $balance->user_id = Auth::id(); return $balance; }); $user_id = Auth::id(); $balance = Balance::where('user_id', $user_id)->orderBy('id', 'DESC')->first(); if (count($balance) > 0) { $date = Carbon::createFromFormat('Y-m-d H:i:s', $balance->created_at); if ($date->isToday()) { // Deixa como está. } else { // Cria o pro=imeiro registro $todayAmount = DB::table('transactions')->where('created_at', '>', date("Y-m-d") . " 00:00:00")->where('created_at', '<=', date('Y-m-d H:i:s'))->where('user_id', Auth::id())->where('done', 1)->sum('amount'); $newAmount = $balance->amount + $todayAmount; // Cria um novo pro dia de hoje $balance = Balance::create(['amount' => $newAmount, 'user_id' => $user_id]); } } else { // Cria o pro=imeiro registro $amount = DB::table('transactions')->where('created_at', '<=', date('Y-m-d H:i:s'))->where('user_id', Auth::id())->where('done', 1)->sum('amount'); $balance = Balance::create(['amount' => $amount, 'user_id' => $user_id]); } }
public function states_index() { $user_id = Auth::user()->id; $colonia = Session::get("colonia"); $urbanism = Urbanism::where('colony_id', '=', $colonia)->first(); $urbanismo = $urbanism->id; $urb_name = $urbanism->Colony->name; $ano = date("Y"); $vigencia = date("Y-m"); $monthly_fee = MonthlyFee::where('monthly_fee.urbanism_id', '=', $urbanismo)->where(DB::raw('DATE_FORMAT(monthly_fee.since,\'%Y-%m\')'), '=', $vigencia)->orderBy('monthly_fee.created_at', 'desc')->pluck('amount'); $monthly_all = MonthlyFee::where('monthly_fee.urbanism_id', '=', $urbanismo)->where(DB::raw('DATE_FORMAT(monthly_fee.since,\'%Y\')'), '=', $ano)->get(); $monthly_ini = MonthlyFee::where('monthly_fee.urbanism_id', '=', $urbanismo)->where(DB::raw('DATE_FORMAT(monthly_fee.since,\'%Y\')'), '=', $ano)->orderBy('monthly_fee.created_at', 'ASC')->pluck('since'); $mes_ini = (int) date("m", strtotime($monthly_ini)); $months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Decem"); $cuotas = array(); foreach ($monthly_all as $cuota_mensual) { $ini = (int) date("m", strtotime($cuota_mensual->since)); $fin = (int) date("m", strtotime($cuota_mensual->until)); if ($cuota_mensual->until == NULL) { $fin = (int) date("m"); } for ($i = $ini; $i <= $fin; $i++) { $cuotas[$months[$i - 1]] = $cuota_mensual->amount; } } $neighbor = Neighbors::with('NeighborProperty')->where('user_id', '=', $user_id)->get(); foreach ($neighbor as $vecino) { foreach ($vecino->NeighborProperty as $property) { if ($property->urbanism_id == $urbanismo) { $property_id = $property->id; } } } $balance = Balance::where('neighbor_property_id', '=', $property_id)->pluck('amount'); if ($balance) { $saldo = $balance; } else { $saldo = 0; } $payments_states = PaymentStates::where('neighbor_property_id', '=', $property_id)->where('year', '=', $ano)->get(); $breadcrumbs = Neighbors::with('NeighborProperty')->where('user_id', '=', $user_id)->first(); $breadcrumbs_data = $breadcrumbs->name . " " . $breadcrumbs->last_name . " [ " . $urb_name . " ]"; return View::make('dashboard.payments.account_states.states', ['payments' => $payments_states, 'cuotas' => $cuotas, 'months' => $months, 'saldo' => $saldo, 'prueba' => $user_id, 'mes_ini' => $mes_ini, 'sobrante' => 0, 'breadcrumbs_data' => $breadcrumbs_data]); }
public function takeMoney($amount, $wallet_id, $user_id) { $balanceCoin = Balance::where('user_id', '=', $user_id)->where('wallet_id', '=', $wallet_id)->first(); if ($amount == 0) { return true; } if (isset($balanceCoin->amount)) { //update balance $old_amount = $balanceCoin->amount; if ($old_amount < $amount) { return false; } else { $new_amount = $old_amount - $amount; Log::info("Take Money " . $wallet_id . " -- User: "******": " . $new_amount . ' -- amount: ' . $amount); //echo "<br>Take New ".$wallet_id.": ".$new_amount.' -- amount: '.$amount; Balance::where('id', $balanceCoin->id)->update(array('amount' => $new_amount)); return true; } } else { //insert balance return false; } }
public function charges_store() { $user_id = Auth::user()->id; $colonia = Session::get("colonia"); $urbanism = Urbanism::where('colony_id', '=', $colonia)->first(); $collector = Collector::where('user_id', '=', $user_id)->where('urbanism_id', '=', $urbanism->id)->first(); $ano = date("Y"); $neighbor = Input::get('valorid'); $amount = Input::get('amount'); $income_charge = new Payment(); $income_charge->neighbor_property_id = $neighbor; $income_charge->collector_id = $collector->id; $income_charge->amount = $amount; $income_charge->sub_account_id = Input::get('sub_account_id'); $income_charge->coments = Input::get('coments'); $income_charge->deposit = null; $income_charge->debt = null; $income_charge->status_id = 1; $income_charge->updated_at = date('Y') . '-01-01 ' . date('H:i:s'); if ($income_charge->save()) { $neighbor_payments = $income_charge; $payment_state = PaymentStates::where('neighbor_property_id', '=', $neighbor)->where('year', '=', $ano)->orderBy('created_at', 'desc')->first(); $months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); $monthly_all = MonthlyFee::where('monthly_fee.urbanism_id', '=', $collector->urbanism_id)->where(DB::raw('DATE_FORMAT(monthly_fee.since,\'%Y\')'), '=', $ano)->get(); $monthly_ini = MonthlyFee::where('monthly_fee.urbanism_id', '=', $collector->urbanism_id)->where(DB::raw('DATE_FORMAT(monthly_fee.since,\'%Y\')'), '=', $ano)->orderBy('monthly_fee.created_at', 'ASC')->pluck('since'); $mes_ini = (int) date("m", strtotime($monthly_ini)); $cuotas = array(); foreach ($monthly_all as $cuota_mensual) { $ini = (int) date("m", strtotime($cuota_mensual->since)); $fin = (int) date("m", strtotime($cuota_mensual->until)); if ($cuota_mensual->until == NULL) { $fin = (int) date("m"); } for ($i = $ini; $i <= $fin; $i++) { $cuotas[$months[$i - 1]] = $cuota_mensual->amount; } } $acumulado = $amount; //para guardar el monto $ValorInicial = $amount; //para guardar el valor inicial del monto ingresado $ValorResto = 0; //para comparar con el valor inicial $balance = Balance::where('neighbor_property_id', '=', $neighbor)->pluck('amount'); $balance_saldo = Balance::where('neighbor_property_id', '=', $neighbor)->first(); if ($balance) { $saldo = $balance; if ($saldo > $amount) { $saldo = $saldo - $amount; $amount = 0; $balance_saldo->amount = $saldo; $balance_saldo->update(['id']); } else { if ($saldo < $amount) { $saldo = $amount - $saldo; $amount = $saldo; $balance_saldo->delete(['id']); } else { $saldo = 0; $amount = 0; $balance_saldo->delete(['id']); } } } else { $saldo = 0; } //Si el usuario no ha realizado ningún pago, se llena la tabla Payment_States por primera vez if (!$payment_state) { $TotalCuotas = 0; $payment_state_detail = new PaymentStates(); $payment_state_detail->neighbor_property_id = $neighbor_payments->neighbor_property_id; $payment_state_detail->year = date('Y', strtotime($neighbor_payments->created_at)); //Ciclo hasta el mes actual for ($i = $mes_ini - 1; $i < date('m'); $i++) { if (date('m') != $i + 1) { if ($amount > $cuotas[$months[$i]]) { $payment_state_detail->{$months}[$i] = $cuotas[$months[$i]]; $amount = $amount - $cuotas[$months[$i]]; } else { if ($amount < $cuotas[$months[$i]] && $amount != 0) { $payment_state_detail->{$months}[$i] = $amount; $amount = 0; } else { $payment_state_detail->{$months}[$i] = $cuotas[$months[$i]]; $amount = 0; } } } else { //si es el mes actual // for: suma todas las cuotas de cada mes, las q se deben for ($j = $mes_ini - 1; $j < date('m'); $j++) { if ($payment_state_detail->{$months}[$j] == null) { $TotalCuotas = $TotalCuotas + $cuotas[$months[$j]]; } elseif ($payment_state_detail->{$months}[$j] < $cuotas[$months[$j]] && $payment_state_detail->{$months}[$j] > 0) { $resto_mes = $cuotas[$months[$j]] - $payment_state_detail->{$months}[$j]; $TotalCuotas = $TotalCuotas + $resto_mes; } } if ($TotalCuotas > $amount) { $TotalCuotas = $TotalCuotas - $amount; $payment_state_detail->{$months}[$i] = "-" . $TotalCuotas; } elseif ($TotalCuotas < $amount) { $TotalCuotas = $amount - $TotalCuotas; $payment_state_detail->{$months}[$i] = $cuotas[$months[$i]] + $TotalCuotas; $amount = $TotalCuotas; } elseif ($TotalCuotas == $amount) { //si la cuota es igual al monto que esta pagando $payment_state_detail->{$months}[$i] = $cuotas[$months[$i]]; $amount = 0; } } } //fin del ciclo $payment_state_detail->accumulated = $acumulado; //Se guardan los datos en Payment_States $payment_state_detail->save(); } else { //si ya tiene registros en payment_state $fondo = $payment_state->accumulated + $amount; $valorResto = 0; $abono = 0; for ($i = $mes_ini - 1; $i < date('m'); $i++) { if (date('m') != $i + 1) { if ($payment_state->{$months}[$i]) { if ($payment_state->{$months}[$i] != $cuotas[$months[$i]] && $payment_state->{$months}[$i] > 0) { $valorResto = $ValorResto + $payment_state->{$months}[$i]; $amount = $amount + $valorResto; if ($amount > $cuotas[$months[$i]]) { $payment_state->{$months}[$i] = $cuotas[$months[$i]]; $amount = $amount - $cuotas[$months[$i]]; } else { $payment_state->{$months}[$i] = $amount; $amount = 0; } } } else { if ($amount > $cuotas[$months[$i]]) { $payment_state->{$months}[$i] = $cuotas[$months[$i]]; $amount = $amount - $cuotas[$months[$i]]; } else { if ($amount < $cuotas[$months[$i]]) { $payment_state->{$months}[$i] = $amount; $amount = 0; } else { $payment_state->{$months}[$i] = $cuotas[$months[$i]]; $amount = 0; } } } } else { //si es el mes actual if ($payment_state->{$months}[$i] < 0) { $deuda = -1 * $payment_state->{$months}[$i]; $deuda = $amount - $deuda; if ($deuda < 0) { $payment_state->{$months}[$i] = $deuda; } else { if ($deuda == 0) { $payment_state->{$months}[$i] = $cuotas[$months[$i]]; } else { $payment_state->{$months}[$i] = $deuda + $cuotas[$months[$i]]; } } } else { $abono = $payment_state->{$months}[$i] + $amount; $payment_state->{$months}[$i] = $abono; } } } $payment_state->accumulated = $fondo; $payment_state->update(['id']); } //---envio por email de recibo al pagador--- $mensaje = null; $monto = Input::get('amount'); $infoCobrador = Neighbors::with('NeighborProperty')->where('user_id', '=', $user_id)->first(); $infoPagador = NeighborProperty::where('id', '=', $neighbor)->first(); $email = UserNeighbors::where('id', '=', $infoPagador->Neighbors->user_id)->pluck('email'); $tipoUrb = $infoPagador->Urbanism->UrbanismType->id; if ($tipoUrb == 3) { $Domicilio = "Piso " . $infoPagador->Building->description . ' - Apartamento ' . $infoPagador->num_house_or_apartment; } else { $Domicilio = "Calle " . $infoPagador->Street->name . ' - Casa ' . $infoPagador->num_house_or_apartment; } $Colony = Urbanism::with('Colony.City')->where('id', '=', $infoPagador->Urbanism->id)->first(); $state = DB::table('states')->where('id', $Colony->Colony->City->state_id)->first(); $dias = array("Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"); $meses = array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"); $fecha = $dias[date('w')] . " " . date('d') . " de " . $meses[date('n') - 1] . " del " . date('Y'); $pay_states = PaymentStates::where('neighbor_property_id', '=', $neighbor)->where('year', '=', $ano)->get(); $data = array('Cobrador' => $infoCobrador->name . " " . $infoCobrador->last_name, 'Nombre' => $infoPagador->Neighbors->name . " " . $infoPagador->Neighbors->last_name, 'Domicilio' => $Domicilio, 'monto' => $monto, 'Fecha' => $fecha, 'colonia' => $Colony->Colony->name, 'urbanismo' => $infoPagador->Urbanism->name, 'estado' => $state->name, 'ciudad' => $Colony->Colony->City->name, 'payments' => $pay_states, 'months' => $months, 'cuotas' => $cuotas, 'mes_ini' => $mes_ini); try { Mail::send('emails.Recibo', $data, function ($message) use($email) { $message->to($email); $message->subject('Recibo de pago'); }); } catch (Exception $exc) { Mail::send('emails.Recibo', $data, function ($message) use($email) { $message->to($email); $message->subject('Recibo de pago'); }); } // ---/fin de envio por email de recibo al pagador/---- //se agrego el logs Event::fire('logs', 'hizo un Cobro al Domicilio ' . $Domicilio . ' de un Monto de' . $monto); //fin logs return Redirect::action('IncomesController@charges_index')->with('error', false)->with('msg', 'Cobro ingresado con éxito.')->with('class', 'info'); } else { return Redirect::back()->with('error', true)->with('msg', '¡Algo salió mal! Contacte con administrador.')->with('class', 'danger'); } }
public function deleteWallet() { $wallet_id = Input::get('wallet_id'); $wallet = Wallet::find($wallet_id); if (isset($wallet->id)) { $markets = Market::where("wallet_from", $wallet_id)->orwhere("wallet_to", $wallet_id)->get(); $arr_markets = array(0); foreach ($markets as $market) { $arr_markets[] = $market->id; } FeeWithdraw::where("wallet_id", $wallet_id)->delete(); WalletLimitTrade::where("wallet_id", $wallet_id)->delete(); UserAddressDeposit::where("wallet_id", $wallet_id)->delete(); Balance::where("wallet_id", $wallet_id)->delete(); Transfer::where("wallet_id", $wallet_id)->delete(); Deposit::where("wallet_id", $wallet_id)->delete(); Withdraw::where("wallet_id", $wallet_id)->delete(); Order::whereIn('market_id', $arr_markets)->delete(); Trade::whereIn('market_id', $arr_markets)->delete(); FeeTrade::whereIn('market_id', $arr_markets)->delete(); Market::where("wallet_from", $wallet_id)->orwhere("wallet_to", $wallet_id)->delete(); Wallet::where('id', $wallet_id)->delete(); $message = $wallet->type . " " . Lang::get('admin_messages.delete_success'); if (Input::get('isAjax')) { echo json_encode(array('status' => 'success', 'message' => $message)); exit; } else { return Redirect::to('admin/manage/wallets')->with('success', $message); } } else { $message = Lang::get('admin_messages.wallet_not_exist'); if (Input::get('isAjax')) { echo json_encode(array('status' => 'error', 'message' => $message)); exit; } else { return Redirect::to('admin/manage/wallets')->with('error', $message); } } }