Skip to content

Instantly share code, notes, and snippets.

@maxca
Created September 26, 2018 07:17
Show Gist options
  • Save maxca/9659327fb0fdc361f89e7d3ac6753404 to your computer and use it in GitHub Desktop.
Save maxca/9659327fb0fdc361f89e7d3ac6753404 to your computer and use it in GitHub Desktop.
for jadet project
<?php
namespace App\Repositories\Members;
use App\Repositories\BaseRepositoryWrap;
use App\Forcatings;
class MembersRepository extends BaseRepositoryWrap
{
/**
* set total count
* @var integer
*/
protected $totalCount;
/**
* set aveg
* @var [type]
*/
protected $average;
/**
* set title name of page
* @var string
*/
protected $title = 'Jadet';
public function __construct()
{
parent::__construct();
}
/**
* get show list view wrap with data.
* @return view object
*/
public function getList()
{
$data = $this->genDataExp();
return view('template.master-list-raw', $data);
}
public function getCreateForm()
{
return parent::getCreateForm();
}
public function genDataExp()
{
$data['title'] = 'forcasting';
$data['data'] = Forcatings::all();
$dataTest = $data['data']->toArray();
if(!empty($dataTest)) {
$data['movingAverage'] = $this->movingAverage($dataTest);
$data['exponetial'] = $this->exponetial($dataTest);
$data['multiCaro'] = $this->multiCaro($dataTest);
}
return $data;
}
public function readExcel($file)
{
Forcatings::truncate();
$data = parent::readExcel($file);
Forcatings::insert($data);
return $data;
}
public function exportExcelsd()
{
$data = $this->genDataExp();
return parent::exportExcel('export', $data,'template.body-excel');
}
public function renderProcess()
{
$data = $this->genDataExp();
$data['title'] = 'Jadet forecasting';
return view('template.MovinngForecast', $data);
}
public function clearData()
{
return Forcatings::truncate();
}
public function movingAverage($data=[])
{
$skip = [0,1];
$summary =0;
$response = [];
foreach ($data as $key => $value) {
if(!in_array($key ,$skip)) {
$response[$key]['MovinngAvg'] = ($data[$key -2]['demand'] + $data[$key-1]['demand']) /2;
$response[$key]['error'] = $value['demand'] - $response[$key]['MovinngAvg'];
$response[$key]['abs'] = abs($response[$key]['error']);
$response[$key]['percen'] = intval(($response[$key]['abs'] / $value['demand']) * 100);
}else {
$response[$key]['MovinngAvg'] = '';
$response[$key]['error'] = '';
$response[$key]['abs'] = '';
$response[$key]['percen'] = '';
}
$summary += $value['demand'];
}
$this->totalCount = $summary;
$this->average = $summary /12;
$response['next'] = ($data[10]['demand'] + $data[11]['demand']) /2;
$response['total'] = $summary;
$response['avg_demand'] = $this->average;
$response['avg_moving'] = ($this->arraySum($response,'MovinngAvg') ) /10;
$response['avg_error'] = $response['avg_demand'] - $response['avg_moving'];
$response['avg_abs'] = abs($response['avg_error']);
$response['avg_erros'] = intval(($response['avg_abs'] /$response['avg_demand']) * 100);
return $response;
}
public function arraySum($data ,$key)
{
return array_reduce($data, function($carry, $item) use($key) {
$carry += $item[$key];
return $carry;
});
}
public function exponetial($data)
{
$skip = [0];
$resp = [];
foreach ($data as $key => $value) {
if(!in_array($key ,$skip)) {
if($key == 1) {
$resp[$key]['exp'] = $data[0]['demand'];
} else {
$resp[$key]['exp'] = $resp[$key-1]['exp'] + 0.6 * ($data[$key-1]['demand'] - $resp[$key-1]['exp']);
}
$resp[$key]['error'] = $value['demand'] - $resp[$key]['exp'];
$resp[$key]['abs'] = abs($resp[$key]['error']);
$resp[$key]['percen'] = intval(($resp[$key]['abs'] / $value['demand']) * 100);
} else{
$resp[$key]['exp'] = '';
$resp[$key]['error'] = '';
$resp[$key]['abs'] = '';
$resp[$key]['percen'] = '';
}
}
$resp['next'] = $resp[11]['exp'] + 0.6 * ($data[11]['demand'] - $resp[11]['exp']);
$resp['total'] = $this->totalCount;
$resp['avg_demand'] = $this->average;
$resp['avg_exp'] = ($this->arraySum($resp,'exp') ) /11;
$resp['avg_error'] = $resp['avg_demand'] - $resp['avg_exp'];
$resp['avg_abs'] = abs($resp['avg_error']);
$resp['avg_erros'] = intval(($resp['avg_abs'] / $resp['avg_demand']) * 100);
return $resp;
}
public function multiCaro($data)
{
$checkData = collect($data);
$max = $checkData->max('demand');
$min = $checkData->min('demand');
$response = array();
foreach ($data as $key => $value) {
$response[$key]['multiple'] = rand($max,$min);
$response[$key]['error'] = $response[$key]['multiple'] - $value['demand'];
$response[$key]['abs'] = abs($response[$key]['error']);
$response[$key]['percen'] = intval(($response[$key]['abs'] / $value['demand']) * 100);
}
$response['total'] = $this->totalCount;
$response['avg_demand'] = $this->average;
$response['avg_multiple'] = ($this->arraySum($response,'multiple') ) /12;
$response['avg_error'] = $response['avg_demand'] - $response['avg_multiple'];
$response['avg_abs'] = abs($response['avg_error']);
$response['avg_erros'] = intval(($response['avg_abs'] / $response['avg_demand']) * 100);
$response['next'] = rand($max,$min);
$response['maxs'] = $max;
$response['mins'] = $min;
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment