public function run()
 {
     ProductDetail::create(['name' => 'images', 'input_type' => 'image', 'default_values' => '', 'validation_rules' => '{"images_1":"required_without_all:feature_images_2,feature_images_3,feature_images_4,feature_images_5,|","images_2":"required_without_all:feature_images_1,feature_images_3,feature_images_4,feature_images_5,|","images_3":"required_without_all:feature_images_1,feature_images_2,feature_images_4,feature_images_5,|","images_4":"required_without_all:feature_images_1,feature_images_2,feature_images_3,feature_images_5,|","images_5":"required_without_all:feature_images_1,feature_images_2,feature_images_3,feature_images_4,|"}', 'help_message' => '', 'type_products' => 'all', 'max_num_values' => 5]);
     ProductDetail::create(['name' => 'virtual weight', 'input_type' => 'text', 'default_values' => '', 'validation_rules' => '', 'help_message' => '', 'type_products' => 'key', 'max_num_values' => 1]);
     ProductDetail::create(['name' => 'OS', 'input_type' => 'text', 'default_values' => '', 'validation_rules' => '', 'help_message' => '', 'type_products' => 'key', 'max_num_values' => 1]);
     ProductDetail::create(['name' => 'weight', 'input_type' => 'text', 'default_values' => '', 'validation_rules' => '', 'help_message' => '', 'type_products' => 'item', 'max_num_values' => 1]);
     ProductDetail::create(['name' => 'dimensions', 'input_type' => 'text', 'default_values' => '', 'validation_rules' => '', 'help_message' => '', 'type_products' => 'item', 'max_num_values' => 1]);
     ProductDetail::create(['name' => 'color', 'input_type' => 'text', 'default_values' => '', 'validation_rules' => '', 'help_message' => '', 'type_products' => 'item', 'max_num_values' => 1]);
     ProductDetail::create(['name' => 'model', 'input_type' => 'text', 'default_values' => '', 'validation_rules' => '', 'help_message' => '', 'type_products' => 'all', 'max_num_values' => 1]);
 }
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store(Request $request)
 {
     $this->validate($request, ['name' => 'required|max:255']);
     $input = $request->all();
     $input['branch_id'] = Auth::user()->branch_id;
     $input['company_id'] = Auth::user()->company_id;
     $input['user_id'] = Auth::user()->id;
     $input['account_year_id'] = session('account');
     ProductDetail::create($input);
     flash()->success('ProductDetail Created Successfully !');
     return redirect('productdetail');
 }
Beispiel #3
0
 public function __construct()
 {
     $this->features = ProductDetail::get();
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id, Request $request)
 {
     $v = Validator::make($request->all(), $this->form_rules, trans('features.validation_messages'));
     if ($v->fails()) {
         return redirect()->back()->withErrors($v->errors())->withInput(\Input::except('default', 'message'));
     }
     $data = $this->accommodateDataStorage($request->all());
     if (array_key_exists('error', $data)) {
         return redirect()->back()->withErrors($data)->withInput(\Input::except('default', 'message'));
     }
     ProductDetail::where('id', $id)->update($data);
     \Session::flash('message', trans('features.update_message'));
     return redirect('wpanel/features');
 }
 /**
  *   validate product feature, as specified in the table of product details.
  *
  *   @param [array] $data all inputs
  *
  *   @return [string|array]
  */
 private function validateFeatures($data)
 {
     $features = ProductDetail::all()->toArray();
     $features_rules = [];
     $message_rules = [];
     foreach ($features as $row) {
         if ($row['status'] == 'active') {
             if ($row['max_num_values'] * 1 == 1) {
                 $features_rules['feature_' . $row['indexByName']] = $row['validationRulesArray'][$row['indexByName'] . '_1'] ? $row['validationRulesArray'][$row['indexByName'] . '_1'] : '';
                 $message_rules = array_merge($message_rules, $this->validationMessagesFeatures($row['validationRulesArray'][$row['indexByName'] . '_1'], 'feature_' . $row['indexByName'], $row['upperName']));
             } else {
                 for ($i = 1; $i <= $row['max_num_values'] * 1; $i++) {
                     $features_rules['feature_' . $row['indexByName'] . '_' . $i] = $row['validationRulesArray'][$row['indexByName'] . '_' . $i] ? $row['validationRulesArray'][$row['indexByName'] . '_' . $i] : '';
                     $message_rules = array_merge($message_rules, $this->validationMessagesFeatures($row['validationRulesArray'][$row['indexByName'] . '_' . $i], 'feature_' . $row['indexByName'] . '_' . $i, $row['upperName']));
                 }
             }
         }
     }
     // dd($data, $features_rules,$message_rules);
     $v = Validator::make($data, $features_rules, $message_rules);
     if ($v->fails()) {
         $array = [];
         $errors = $v->errors()->toArray();
         foreach ($errors as $error) {
             foreach ($error as $row) {
                 $array[] = $row;
             }
         }
         return array_unique($array);
     }
     $array = [];
     foreach ($features as $row) {
         $values = [];
         if ($row['max_num_values'] * 1 !== 1) {
             for ($i = 1; $i <= $row['max_num_values'] * 1; $i++) {
                 if (!$data['feature_' . $row['indexByName'] . '_' . $i]) {
                     continue;
                 }
                 if ($row['help_message'] != '' && strpos('video image document', $row['input_type']) === false) {
                     $message = '';
                     if (isset($row['helpMessageArray']['general'])) {
                         $message = $row['helpMessageArray']['general'];
                     } elseif (isset($row['helpMessageArray']['specific'])) {
                         $message = $row['helpMessageArray']['specific'][$row['indexByName'] . '_' . $i];
                     } elseif (isset($row['helpMessageArray']['general_selection'])) {
                         $message = $data['help_msg_' . $row['indexByName']];
                     } elseif (isset($row['helpMessageArray']['specific_selection'])) {
                         $message = $data['help_msg_' . $row['indexByName'] . '_' . $i];
                     }
                     $values[] = [$data['feature_' . $row['indexByName'] . '_' . $i], $message];
                 } else {
                     $values[] = $data['feature_' . $row['indexByName'] . '_' . $i];
                 }
             }
         } else {
             if (isset($data['feature_' . $row['indexByName']]) && !$data['feature_' . $row['indexByName']]) {
                 continue;
             }
             if ($row['help_message'] != '' && strpos('video image document', $row['input_type']) === false) {
                 $message = '';
                 if (isset($row['helpMessageArray']['general'])) {
                     $message = $row['helpMessageArray']['general'];
                 } elseif (isset($row['helpMessageArray']['general_selection'])) {
                     $message = $data['help_msg_' . $row['indexByName']];
                 }
                 $values = [$data['feature_' . $row['indexByName']], $message];
             } else {
                 $values = isset($data['feature_' . $row['indexByName']]) ? $data['feature_' . $row['indexByName']] : '';
             }
         }
         if ($values) {
             $array[$row['indexByName']] = $values;
         }
     }
     return json_encode($array);
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $this->info('Please wait, you are updating products details and nutrients info in database');
     foreach (Product::where('error', 1)->get() as $pr) {
         $pr->error = 0;
         $pr->save();
     }
     $max = 5000;
     $this->output->progressStart($max);
     while ($max > 0) {
         $new_product = true;
         $product = Product::where('error', 0)->has('nutrients', '==', 0)->first();
         if (is_null($product)) {
             $update_product_detail = ProductDetail::orderBy('updated_at', 'asc')->first();
             $ndb_no = $update_product_detail->product->ndb_no;
             $new_product = false;
         } else {
             $ndb_no = $product->ndb_no;
         }
         // create curl resource
         $ch = curl_init();
         // set url
         /*
         ndb_no - NDB food number
         type - Report type
         format	- report format: JSON or XML
         */
         curl_setopt($ch, CURLOPT_URL, "http://api.nal.usda.gov/ndb/reports/?ndbno=" . $ndb_no . "&type=f&format=json&api_key=" . env('API_KEY', 'DEMO_KEY'));
         //return the transfer as a string
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
         curl_setopt($ch, CURLOPT_TIMEOUT, 400);
         //timeout in seconds
         // $output contains the output string
         $output = curl_exec($ch);
         // close curl resource to free up system resources
         curl_close($ch);
         $max--;
         $data = json_decode($output);
         if (!isset($data) || json_last_error() !== JSON_ERROR_NONE) {
             $product = Product::where('ndb_no', $ndb_no)->first();
             $product->error = true;
             $product->save();
             //$this->info('One fail');
         } else {
             $details = $data->report;
             $food = $details->food;
             if ($ndb_no == $food->ndbno) {
                 //update Product
                 $product = Product::where('ndb_no', $ndb_no)->first();
                 $product->description = $food->name;
                 $product->food_group = $food->fg;
                 $product->save();
                 if (is_null($product->detail)) {
                     //create new ProductDetails
                     $product_detail = new ProductDetail();
                     $product_detail->scientific_name = $food->sn;
                     $product_detail->commercial_name = $food->cn;
                     $product_detail->manufacturer = $food->manu;
                     $product_detail->nitrogen_to_protein_conversion_factor = $food->nf;
                     $product_detail->carbohydrate_factor = $food->cf;
                     $product_detail->fat_factor = $food->ff;
                     $product_detail->protein_factor = $food->pf;
                     $product_detail->refuse_percent = $food->r;
                     $product_detail->refuse_description = $food->rd;
                     $product_detail->product()->associate($product);
                     $product_detail->save();
                 } else {
                     //update ProductDetails
                     $product_detail = ProductDetail::find($product->id);
                     // check if it works
                     $product_detail->scientific_name = $food->sn;
                     $product_detail->commercial_name = $food->cn;
                     $product_detail->manufacturer = $food->manu;
                     $product_detail->nitrogen_to_protein_conversion_factor = $food->nf;
                     $product_detail->carbohydrate_factor = $food->cf;
                     $product_detail->fat_factor = $food->ff;
                     $product_detail->protein_factor = $food->pf;
                     $product_detail->refuse_percent = $food->r;
                     $product_detail->refuse_description = $food->rd;
                     $product_detail->update_count = $product_detail->update_count + 1;
                     $product_detail->save();
                 }
                 if (!$new_product) {
                     $product->nutrients()->detach();
                 }
                 foreach ($food->nutrients as $key => $nutrient_info) {
                     // update or create new nutrient
                     $nutrient = Nutrient::firstOrCreate(['id' => $nutrient_info->nutrient_id]);
                     $nutrient->name = $nutrient_info->name;
                     $nutrient->group = $nutrient_info->group;
                     $nutrient->unit = $nutrient_info->unit;
                     $nutrient->save();
                     // create data to nutrient_product table
                     $nutrient->products()->attach($product->id, ['value' => $nutrient_info->value]);
                 }
             }
             //$this->info('One more done ' . $max);
         }
         $this->output->progressAdvance();
     }
     $this->output->progressFinish();
     $this->info('Success');
 }