/home/dvjjulio/test.istyle.mx/app/Http/Controllers/RedController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Cliente;
use App\Venta;
use Auth;
use App\helpers;
use App\Helper;
use App\Comision;
use App\Nivel;
use App\Pais;
class RedController extends Controller
{
//
public function index()
{
$clientesI = new Cliente();
$clientes = $clientesI->Clientes();
//return $data['cliente'];
$cliente_id = Auth::user()->id;
$ids = '"' . $cliente_id . '"';
$cliente = $clientesI->Clientes($cliente_id);
$comisionCliente = Comision::where('cliente_id', $cliente_id)->where('ano', date('Y'))->where('mes', date('m'))->first();
$clientePais = 'Mexico';
if (isset($cliente[0]->pais)) $clientePais = $cliente[0]->pais;
$moneda = [
'Mexico' => 'Peso MX',
'USA' => 'Dolar USD',
'Canada' => 'Dolar C',
'Guatemala' => 'Quetzal Q',
'Chile' => 'Dolar USD',
'El Salvador' => 'Dolar USD',
];
$moneda_abrev = [
'Mexico' => 'MX',
'USA' => 'USD',
'Canada' => 'C',
'Guatemala' => 'Q',
'Chile' => 'USD',
'El Salvador' => 'USD',
];
$redes = new Venta();
$red = [];
$total = 0;
$comision = 0;
$red_size = 0;
for ($i = 1; $i <= 4; $i++) {
$red_first = $redes->red_first($ids, $i, null, null, $clientePais);
if (isset($red_first)) {
$ids = $this->get_ids($red_first);
$red[$i] = $red_first;
foreach ($red_first as $val) {
if ($val->total < 4000 | !$val->comisiona) {
$comision += 0;
} else {
$comision += $val->comision;
}
$red_size++;
$total += $val->total;
}
} else {
break;
}
}
$years = $redes->monthYearVenta();
$year = date('Y');
$month = date('m');
// $months = $redes->monthYearVenta(date('Y'));
$months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
$compra = $redes->cliente_compra($cliente_id, $year, $month);
$data = [
'total' => $total, 'comision' => $comision,
'red_size' => $red_size, 'years' => $years,
'months' => $months, 'compra' => $compra,
'year' => $year, 'month' => $month,
'moneda' => $moneda[$clientePais],
'moneda_abrev' => $moneda_abrev[$clientePais]
];
return view('Catalogos.Red')->with(compact('red', 'data', 'cliente', 'clientes', 'comisionCliente'));
}
public function getRed(Request $request)
{
$data = $request->all();
//return $data['cliente'];
$ids = '"' . $data['cliente'] . '"';
$clientId = null;
$clientesI = new Cliente();
$cliente = $clientesI->Clientes($data['cliente']);
$clientePais = 'Mexico';
if (isset($cliente[0]->pais)) $clientePais = $cliente[0]->pais;
$moneda = [
'Mexico' => 'Peso MX',
'USA' => 'Dolar USD',
'Canada' => 'USD',
'Guatemala' => 'Quetzal Q'
];
$moneda_abrev = [
'Mexico' => 'MX',
'USA' => 'USD',
'Canada' => 'USD',
'Guatemala' => 'Q'
];
$year = $data['year'] ?? date('Y');
$month = $data['month'] ?? date('m');
$comisionCliente = Comision::where('cliente_id', $data['cliente'])->where('ano', $year)->where('mes', $month)->first();
$redes = new Venta();
$red = [];
$total = 0;
$comision = 0;
$red_size = 0;
$message = "";
$controlLessFront = [];
for ($i = 1; $i <= 4; $i++) {
$red_first = $redes->red_first($ids, $i, $month, $year, $clientePais);
if (isset($red_first)) {
$ids = $this->get_ids($red_first);
$red[$i] = $red_first;
foreach($red_first as $val){
if($val->total >= 4000 && $val->comisiona){
$comision += $val->comision;
}
$red_size++;
$total += $val->total;
}
} else {
break;
}
}
// for ($i = 1; $i <= 4; $i++) {
// $red_first = $redes->red_first($ids, $i, $month, $year, $clientePais);
// if (isset($red_first)) {
// $ids = $this->get_ids($red_first);
// $red[$i] = $red_first;
// foreach($red_first as $val){
// if(($val->total < 4000 || $val->total >= 10000) | !$val->comisiona){
// $comision += 0;
// }else{
// $comision += $val->comision;
// }
// }
// } else {
// break;
// }
// }
if ($comision >= 400000 || $cliente[0]->nivel == 2) {
$this->checkLevel($cliente[0]->id, 2);
$frontales = $this->afterTen($data['cliente'], $data['year'], $data['month']);
if ($frontales < 10) {
$message = "Necesitas tus 10 frontales con venta para tus activos";
// $comision = 0;
$comisionType = Helper::getComisionCountry($cliente[0]->pais);
$firstLevel = Nivel::where('nivel', 'Primer')->first();
$comision = $frontales * $firstLevel->$comisionType;
$ids = '"' . $data['cliente'] . '"';
$year = $data['year'];
$month = $data['month'];
$front = 1;
for ($i = 1; $i <= 2; $i++) {
$red_first = $redes->red_first($ids, $i, $month, $year, $clientePais);
if (isset($red_first)) {
$ids = $this->get_ids($red_first);
foreach ($red_first as $val) {
if ($val->total >= 4000) {
if ($front <= $frontales) {
// $comision += $val->comision;
$secondLevel = Nivel::where('nivel', 'Segundo')->first();
$comision += $val->nivel === 'Segundo' ? $secondLevel->$comisionType : 0;
$front++;
}
}
}
$front = 1;
}
}
}
} else if (($comision >= 10000 && $comision <= 400000) || $cliente[0]->nivel == 1) {
$this->checkLevel($cliente[0]->id, 1);
$frontales = $this->afterTen($data['cliente'], $data['year'], $data['month']);
if ($frontales < 5) {
$comisionNivelCountry = Nivel::Where('nivel', 'Segundo')->first();
$message = "Necesitas tus 5 frontales con venta para tus activos";
// $comision = 0;
$comisionType = Helper::getComisionCountry($cliente[0]->pais);
$firstLevel = Nivel::where('nivel', 'Primer')->first();
$comision = $frontales * $firstLevel->$comisionType;
$ids = '"' . $data['cliente'] . '"';
$year = $data['year'];
$month = $data['month'];
$front = 1;
for ($i = 1; $i <= 2; $i++) {
$red_first = $redes->red_first($ids, $i, $month, $year, $clientePais);
if (isset($red_first)) {
$ids = $this->get_ids($red_first);
foreach ($red_first as $val) {
if ($val->total >= 4000) {
if ($front <= $frontales) {
$secondLevel = Nivel::where('nivel', 'Segundo')->first();
$comision += $val->nivel === 'Segundo' ? $secondLevel->$comisionType : 0;
// $comision += $val->comision;
$front++;
}
}
}
$front = 1;
}
}
}
}
$compra = $redes->cliente_compra($data['cliente'], $year, $month);
$data = [
'total' => $total,
'comision' => $comision,
'red_size' => $red_size,
'compra' => $compra,
'year' => $year,
'month' => $month,
'message' => $message,
'moneda' => $moneda[$clientePais],
'moneda_abrev' => $moneda_abrev[$clientePais]
];
return View('Catalogos.sectionRed')->with(compact('red', 'data', 'cliente', 'comisionCliente'));
}
public function checkLevel($id, $level)
{
$customer = Cliente::find($id);
if (!$customer->nivel) {
$customer->nivel = $level;
if ($customer->save()) {
return response()->json(['status' => true]);
} else {
return response()->json(['status' => true]);
}
} else {
return response()->json(['status' => true]);
}
}
public function afterTen($id, $year = null, $month = null)
{
$ids = '"' . $id . '"';
$redes = new Venta();
$frontales = 0;
$red_first = $redes->red_first($ids, 1, $month, $year);
if (isset($red_first)) {
foreach ($red_first as $key => $value) {
if ($value->total >= 4000) {
$frontales++;
}
}
}
return $frontales;
}
public function get_ids($red_first)
{
$ids = "";
foreach ($red_first as $key => $value) {
$ids .= '"' . $value->id . '",';
}
return substr($ids, 0, -1);
}
public function getMonths(Request $request)
{
$year = $request->input('year');
$redes = new Venta();
$months = $redes->monthYearVenta($year);
$months_select = "<option value=''>Selecciona</option>";
foreach ($months as $month) {
$months_select .= "<option value='" . $month->mes . "'>" . Helper::month($month->mes) . "</option>";
}
return $months_select;
}
}