Created
September 18, 2014 19:42
-
-
Save ricardo-rossi/78b7d8c313a06f22fb70 to your computer and use it in GitHub Desktop.
Loads sample data into database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Endata\Data; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Facades\File; | |
use Ingredient; | |
use Nutrient; | |
use Composition; | |
use Settings; | |
use Plant; | |
use Formula; | |
use FormulaIngredient; | |
/** | |
* | |
*/ | |
class Sample { | |
public $plant_id = 0; | |
public $id = 0; | |
public $currency_id = 0; | |
public $weight_id = 0; | |
public $sample = true; | |
/** | |
* [create description] | |
* @param [type] $id [description] | |
* @param [type] $currency_id [description] | |
* @param [type] $weight_id [description] | |
* @return [type] [description] | |
*/ | |
public function create($key, $id, $currency_id, $weight_id, $sample = true) | |
{ | |
$dir = '/sample_data/' . $key; | |
$s = new Sample(); | |
$s->setId($id); | |
$s->setSample($sample); | |
$s->setCurrencyId($currency_id); | |
$s->setWeightId($weight_id); | |
$s->initAccount(); | |
if($s->isSample()) | |
{ | |
$nutrients = $s->csv_to_array(app_path() . $dir . '/import_nut.csv'); | |
$s->importNutrients($nutrients); | |
$ingredients = $s->csv_to_array(app_path() . $dir . '/import_ing.csv'); | |
$s->importIngredients($ingredients); | |
$compositions = $s->csv_to_array(app_path() . $dir . '/import_compositions.csv'); | |
$s->importCompositions($compositions); | |
$formulas = $s->csv_to_array(app_path() . $dir . '/import_formulas.csv'); | |
$s->importFormulas($formulas); | |
} | |
} | |
public function setSample($sample) { | |
$this->sample = $sample; | |
} | |
public function isSample() { | |
return $this->sample; | |
} | |
public function setId($id) { | |
$this->id = $id; | |
} | |
public function setCurrencyId($currency_id) { | |
$this->currency_id = $currency_id; | |
} | |
public function setWeightId($weight_id) { | |
$this->weight_id = $weight_id; | |
} | |
public function id() { | |
return $this->id; | |
} | |
public function currencyId() { | |
return $this->currency_id; | |
} | |
public function weightId() { | |
return $this->weight_id; | |
} | |
/** | |
* [initAccount description] | |
* @return [type] [description] | |
*/ | |
public function initAccount() | |
{ | |
$settings = new Settings(); | |
$settings->account_id = $this->id(); | |
$settings->currency_id = $this->currencyId(); | |
$settings->weight_id = $this->weightId(); | |
$settings->save(); | |
$location = new Plant(); | |
$location->account_id = $this->id(); | |
$location->name = "Warehouse"; | |
$location->save(); | |
$this->plant_id = $location->id; | |
} | |
/** | |
* [importNutrients description] | |
* @param [type] $nutrients [description] | |
* @return [type] [description] | |
*/ | |
public function importNutrients($nutrients) | |
{ | |
$rows = []; | |
foreach ($nutrients as $nutrient) | |
{ | |
$rows[] = array( | |
'account_id' => $this->id(), | |
'name' => $nutrient['nutrient'], | |
'unit' => $nutrient['unit'], | |
'code' => $nutrient['code'], | |
'created_at' => DB::raw('CURRENT_TIMESTAMP'), | |
'updated_at' => DB::raw('CURRENT_TIMESTAMP') | |
); | |
} | |
DB::table('nutrients')->insert($rows); | |
} | |
/** | |
* [importIngredients description] | |
* @param [type] $ingredients [description] | |
* @return [type] [description] | |
*/ | |
public function importIngredients($ingredients) | |
{ | |
$rows = []; | |
foreach ($ingredients as $ingredient) | |
{ | |
$rows[] = array( | |
'account_id' => $this->id(), | |
'name' => $ingredient['ingredient'], | |
'price' => Sample::randomFloat(), | |
'plant_id' => $this->plant_id, | |
'dry_matter' => $ingredient['dm'], | |
'code' => $ingredient['code'], | |
'created_at' => DB::raw('CURRENT_TIMESTAMP'), | |
'updated_at' => DB::raw('CURRENT_TIMESTAMP') | |
); | |
} | |
DB::table('products')->insert($rows); | |
} | |
/** | |
* [importCompositions description] | |
* @param [type] $compositions [description] | |
* @return [type] [description] | |
*/ | |
public function importCompositions($compositions) | |
{ | |
$account_id = $this->id(); | |
$nutrients = Nutrient::where('account_id', $account_id )->get(); | |
foreach ($compositions as $composition) | |
{ | |
$ingredient = Ingredient::where('name', $composition['ingredient']) | |
->where('account_id', $account_id )->first(); | |
$rows = []; | |
foreach ($nutrients as $nutrient) | |
{ | |
$value = $composition[$nutrient->name]; | |
if(!$value) { $value = 0; } | |
$rows[] = array( | |
'account_id' => $account_id, | |
'nutrient_id' => $nutrient->id, | |
'ingredient_id' => $ingredient->id, | |
'value' => $value); | |
} | |
DB::table('compositions')->insert($rows); | |
} | |
} | |
/** | |
* [importFormulas description] | |
* @param [type] $formulas [description] | |
* @return [type] [description] | |
*/ | |
public function importFormulas($formulas) | |
{ | |
$account_id = $this->id(); | |
foreach($formulas[0] as $key=>$value) | |
{ | |
if($key != 'ingredient') | |
{ | |
Sample::createFormula($account_id, $key, $formulas); | |
} | |
} | |
} | |
/** | |
* [createFormula description] | |
* @param [type] $account_id [description] | |
* @param [type] $name [description] | |
* @param [type] $formulas [description] | |
* @return [type] [description] | |
*/ | |
function createFormula($account_id, $name, $formulas) | |
{ | |
$myFormula = new Formula(); | |
$myFormula->account_id = $account_id; | |
$myFormula->name = $name; | |
$myFormula->save(); | |
foreach ($formulas as $formula) | |
{ | |
if($formula[$name]) | |
{ | |
$ingredient = Ingredient::where('name', $formula['ingredient']) | |
->where('account_id', $account_id )->first(); | |
$forIng = new FormulaIngredient; | |
$forIng->formula_id = $myFormula->id; | |
$forIng->account_id = $account_id; | |
$forIng->amount = $formula[$name]; | |
$forIng->ingredient_id = $ingredient->id; | |
$forIng->save(); | |
} | |
} | |
} | |
/** | |
* Convert a comma separated file into an associated array. | |
* The first row should contain the array keys. | |
*/ | |
function csv_to_array($filename='', $delimiter=',') { | |
ini_set('auto_detect_line_endings',TRUE); | |
if(!file_exists($filename) || !is_readable($filename)) | |
return FALSE; | |
$header = NULL; | |
$data = array(); | |
if (($handle = fopen($filename, 'r')) !== FALSE) | |
{ | |
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) | |
{ | |
if (!$header) { | |
$header = $row; | |
} | |
else { | |
if (count($header) > count($row)) { | |
$difference = count($header) - count($row); | |
for ($i = 1; $i <= $difference; $i++) { | |
$row[count($row) + 1] = $delimiter; | |
} | |
} | |
$data[] = array_combine($header, $row); | |
} | |
} | |
fclose($handle); | |
} | |
return $data; | |
} | |
/** | |
* [randomFloat description] | |
* @param integer $min [description] | |
* @param integer $max [description] | |
* @return [type] [description] | |
*/ | |
function randomFloat($min = 0, $max = 10) { | |
return number_format(($min + mt_rand() / mt_getrandmax() * ($max - $min)), 2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment