/**
  * Show the form for editing the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function edit($id)
 {
     // IF there is a previously uploaded award image file, delete it
     if (Session::has('award_image_upload')) {
         Storage::delete(storage_path('app\\uploads') . '\\' . Session::get('award_image_upload'));
         Session::delete('award_image_upload');
     }
     $award = Award::findOrFail($id);
     return view('award.edit')->with('award', $award);
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     // Read the database of allied awards
     $csv = Reader::createFromPath(storage_path('app/csv/issued_allied_awards.csv'));
     // This will read the CSV as set of data into the listed array keys
     $data = $csv->fetchAssoc(['playerName', 'playerLabel', 'awardName', 'campaign_id', 'awardedAt', 'submitterName', 'cincName', 'forumSource'], function ($row) {
         // manipulate the incoming data as it is read. This is turn it into
         // a set of data which can be immediately added to our awards table
         $drow = [];
         $drow['playerName'] = trim(strtolower($row[1]));
         $drow['playerLabel'] = trim($row[1]);
         // case as it appears in csv
         $drow['awardName'] = snake_case(strtolower(trim($row[2])));
         $drow['campaign_id'] = trim($row[3]) == '' ? null : trim($row[3]);
         $drow['awardedAt'] = trim($row[6]);
         $drow['submitterName'] = trim(strtolower($row[7]));
         $drow['cincName'] = trim(strtolower($row[8]));
         $drow['forumSource'] = trim(strtolower($row[9]));
         return $drow;
     });
     Player::unguard();
     foreach ($data as $item) {
         $award = Award::where('key', $item['awardName'])->first();
         if ($award == null) {
             Log::info(sprintf('Could not find an award with key %s', $item['awardName']));
             continue;
         }
         $submitter = Player::where('playerName', $item['submitterName'])->first();
         if ($submitter == null) {
             $submitter = Player::create(['playerName' => $item['submitterName'], 'isActive' => true]);
         }
         $cinc = Player::where('playerName', $item['cincName'])->first();
         if ($cinc == null) {
             $cinc = Player::create(['playerName' => $item['cincName'], 'isActive' => true]);
         }
         // check the rype of award we are presenting. If it's a squad award, find the squad and apply it
         // also, if there is a space in the name, its not a player name
         if ($award->awardedTo == 'squad' || preg_match('/\\s/', $item['playerName']) > 0) {
             $squad = Squad::where('key', snake_case($item['playerName']))->first();
             if ($squad == null) {
                 $squad = Squad::create(['key' => snake_case($item['playerName']), 'label' => $item['playerLabel'], 'isActive' => true]);
             }
             $squad->awards()->save($award, ['awardedBy_id' => $submitter->id, 'cinc_id' => $cinc->id, 'campaign_id' => $item['campaign_id'], 'awardedAt' => Carbon\Carbon::createFromFormat('d/m/Y', $item['awardedAt']), 'forumLink' => $item['forumSource']]);
         } else {
             $player = Player::where('playerName', $item['playerName'])->first();
             if ($player == null) {
                 $player = Player::create(['playerName' => $item['playerName'], 'isActive' => true]);
             }
             $player->awards()->save($award, ['awardedBy_id' => $submitter->id, 'cinc_id' => $cinc->id, 'campaign_id' => $item['campaign_id'], 'awardedAt' => Carbon\Carbon::createFromFormat('d/m/Y', $item['awardedAt']), 'forumLink' => $item['forumSource']]);
         }
     }
     Player::reguard();
 }
 public function getList()
 {
     $d = [];
     // GET LAST THREE YEARS OF DATA
     for ($i = 0; $i < 3; $i++) {
         $year = date("Y") - $i;
         $data = ['dependencia' => '901', "ejercicio" => $year];
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_URL, $this->apiContratos);
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
         $result = curl_exec($ch);
         $excercise = json_decode($result);
         $d = array_merge($d, $excercise);
     }
     // SAVE THEM TO THE DB
     foreach ($d as $c) {
         $contract = Contract::firstOrCreate(['ocdsid' => $c->ocdsID, 'ejercicio' => $c->ejercicio, 'cvedependencia' => $c->cveDependencia, 'nomDependencia' => $c->nomDependencia]);
         // save extra data
         $meta = $this->getMetaDataX($contract);
         if (!empty($meta) && !property_exists($meta, 'error')) {
             // add extra data to contracts
             $contract->uri = $meta->uri;
             $contract->published_date = date("Y-m-d", strtotime($meta->publishedDate));
             // create the publisher
             $contract->publisher_id = Publisher::firstOrCreate(["scheme" => $meta->publisher->scheme, "name" => $meta->publisher->name, "uri" => $meta->publisher->uri, "uid" => $meta->publisher->uid]);
             // update contract
             $contract->update();
             // create releases
             foreach ($meta->releases as $r) {
                 $release = Release::firstOrCreate(["local_id" => $r->id, "contract_id" => $contract->id, "ocid" => $contract->ocdsid, "date" => date("Y-m-d", strtotime($r->date)), "initiation_type" => $r->initiationType, "language" => $r->language]);
                 if (count($r->awards)) {
                     foreach ($r->awards as $aw) {
                         $award = Award::firstOrCreate(["release_id" => $release->id, "local_id" => $aw->id]);
                         $award->title = $aw->title;
                         $award->description = $aw->description;
                         $award->status = $aw->status;
                         $award->date = date("Y-m-d", strtotime($aw->date));
                         $award->value = $aw->value->amount;
                         $award->currency = $aw->value->currency;
                         $award->update();
                         if (count($aw->items)) {
                             foreach ($aw->items as $it) {
                                 $item = $award->items()->firstOrCreate(['local_id' => $it->id]);
                                 $item->quantity = $it->quantity;
                                 $item->description = $it->description;
                                 $item->unit = $it->unit->name;
                                 $item->update();
                             }
                         }
                         if (count($aw->suppliers)) {
                             foreach ($aw->suppliers as $sup) {
                                 $supplier = Supplier::firstOrCreate(["award_id" => $aw->id, "rfc" => $sup->identifier->id]);
                                 $supplier->name = $sup->name;
                                 $supplier->street = $sup->address->streetAddress;
                                 $supplier->locality = $sup->address->locality;
                                 $supplier->region = $sup->address->region;
                                 $supplier->zip = $sup->address->postalCode;
                                 $supplier->country = $sup->address->countryName;
                                 $supplier->contact_name = $sup->contactPoint->name;
                                 $supplier->email = $sup->contactPoint->email;
                                 $supplier->phone = $sup->contactPoint->telephone;
                                 $supplier->fax = $sup->contactPoint->faxNumber;
                                 $supplier->url = $sup->contactPoint->url;
                                 $supplier->update();
                             }
                         }
                     }
                 }
                 if (count($r->contracts)) {
                     foreach ($r->contracts as $single) {
                         $single_contract = SingleContract::firstOrCreate(["local_id" => $single->id, "release_id" => $release->id]);
                         $single_contract->award_id = $single->awardID;
                         $single_contract->title = $single->title;
                         $single_contract->description = $single->description;
                         $single_contract->status = $single->status;
                         $single_contract->contract_start = $single->period ? date("Y-m-d", strtotime($single->period->startDate)) : null;
                         $single_contract->contract_end = $single->period ? date("Y-m-d", strtotime($single->period->endDate)) : null;
                         $single_contract->amount = $single->value->amount;
                         $single_contract->currency = $single->value->currency;
                         $single_contract->date_signed = $single->dateSigned ? date("Y-m-d", strtotime($single->dateSigned)) : null;
                         $single_contract->documents = count($single->documents);
                         // ? implode(',',$r->tender->submissionMethod) : null;
                         $single_contract->update();
                         if (count($single->items)) {
                             foreach ($single->items as $it) {
                                 $item = $single_contract->items()->firstOrCreate(['local_id' => $it->id]);
                                 $item->quantity = $it->quantity;
                                 $item->description = $it->description;
                                 $item->unit = $it->unit->name;
                                 $item->update();
                             }
                         }
                     }
                 }
                 // create planning
                 if ($r->planning) {
                     $planning = Planning::firstOrCreate(["release_id" => $release->id]);
                     $planning->amount = $r->planning->budget->amount->amount;
                     $planning->currency = $r->planning->budget->amount->currency;
                     $planning->project = $r->planning->budget->project;
                     $planning->update();
                     //$release->planning_id = $planning->id;
                 }
                 // create tender
                 if ($r->tender) {
                     $tender = Tender::firstOrCreate(["release_id" => $release->id]);
                     $tender->local_id = $r->tender->id;
                     $tender->title = $r->tender->title;
                     $tender->description = $r->tender->description;
                     $tender->status = $r->tender->status;
                     $tender->amount = $r->tender->value ? $r->tender->value->amount : null;
                     $tender->currency = $r->tender->value ? $r->tender->value->currency : null;
                     $tender->procurement_method = $r->tender->procurementMethod;
                     $tender->award_criteria = $r->tender->awardCriteria;
                     $tender->tender_start = $r->tender->tenderPeriod ? date("Y-m-d", strtotime($r->tender->tenderPeriod->startDate)) : null;
                     $tender->tender_end = $r->tender->tenderPeriod ? date("Y-m-d", strtotime($r->tender->tenderPeriod->endDate)) : null;
                     $tender->enquiry_start = $r->tender->enquiryPeriod ? date("Y-m-d", strtotime($r->tender->enquiryPeriod->startDate)) : null;
                     $tender->enquiry_end = $r->tender->enquiryPeriod ? date("Y-m-d", strtotime($r->tender->enquiryPeriod->endDate)) : null;
                     $tender->award_start = $r->tender->awardPeriod ? date("Y-m-d", strtotime($r->tender->awardPeriod->startDate)) : null;
                     $tender->award_end = $r->tender->awardPeriod ? date("Y-m-d", strtotime($r->tender->awardPeriod->endDate)) : null;
                     $tender->has_enquiries = $r->tender->hasEnquiries;
                     $tender->eligibility_criteria = $r->tender->eligibilityCriteria;
                     $tender->submission_method = count($r->tender->submissionMethod) ? implode(',', $r->tender->submissionMethod) : null;
                     $tender->number_of_tenderers = $r->tender->numberOfTenderers;
                     $tender->update();
                     if (count($r->tender->tenderers)) {
                         foreach ($r->tender->tenderers as $tn) {
                             $tenderer = Tenderer::firstOrCreate(["rfc" => $tn->identifier->id]);
                             $tenderer->name = $tn->name;
                             $tenderer->street = $tn->address->streetAddress;
                             $tenderer->locality = $tn->address->locality;
                             $tenderer->region = $tn->address->region;
                             $tenderer->zip = $tn->address->postalCode;
                             $tenderer->country = $tn->address->countryName;
                             $tenderer->contact_name = $tn->contactPoint->name;
                             $tenderer->email = $tn->contactPoint->email;
                             $tenderer->phone = $tn->contactPoint->telephone;
                             $tenderer->fax = $tn->contactPoint->faxNumber;
                             $tenderer->url = $tn->contactPoint->url;
                             $tenderer->update();
                         }
                     }
                     if (count($r->tender->items)) {
                         foreach ($r->tender->items as $it) {
                             $item = $tender->items()->firstOrCreate(['local_id' => $it->id]);
                             $item->quantity = $it->quantity;
                             $item->description = $it->description;
                             $item->unit = $it->unit->name;
                             $item->update();
                         }
                     }
                 }
                 // create buyer
                 if ($r->buyer) {
                     $buyer = Buyer::firstOrCreate(["local_id" => $r->buyer->identifier->id, "name" => $r->buyer->name]);
                     $buyer->uri = $r->buyer->identifier->uri;
                     $buyer->update();
                     $release->buyer_id = $buyer->id;
                     $release->update();
                 }
             }
         }
     }
     echo ":D";
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $csv = Reader::createFromPath(storage_path('app/csv/allied_awards.csv'));
     // This will read the CSV as set of data into the listed array keys
     $awardsData = $csv->fetchAssoc(['awardType', 'isAllied', 'key', 'label', 'description', 'awardedTo', 'awardedBy', 'frequency', 'pri', 'awardReason', 'stat', 'country', 'branch', 'imageUrl'], function ($row) {
         //
         /**
          * manipulate the incoming data as it is read. This is turn it into
          * a set of data which can be immediately added to our awards table
          * 
          * Note that the CSV reader is REALLY weird. The order in which you do
          * things in this function actually matters in the returned array.
          */
         $drow = [];
         $key = snake_case(strtolower(trim($row[1])));
         switch ($key) {
             case 'aerialgunnerwings':
             case 'chiefgunnersmatebadge':
             case 'combatriflemanbadge':
             case 'expertmarksmanbadge':
             case 'fafacewingsbomber':
             case 'fafacewingsfighter':
             case 'fafnavalaviatorwings':
             case 'fafpilotwings':
             case 'fieldgunnerybadge':
             case 'flakscreenbadge':
             case 'guncrewbadge':
             case 'gunnersmatebadge':
             case 'paratroopwings':
             case 'rafbomberacewings':
             case 'rafnavalaviatorwings':
             case 'rafpilotwings':
             case 'seawolfbadge':
             case 'sharpshooterbadge':
             case 'tailgunnergoldwings':
             case 'tankdestroyerbadge':
             case 'tankhunterbadge':
             case 'transportpilotwings':
                 $drow['awardType'] = 'badge';
                 break;
             case 'elitecommandoaward':
                 $drow['awardType'] = 'medal';
                 break;
             default:
                 $drow['awardType'] = 'ribbon';
         }
         $drow['isAllied'] = true;
         $drow['key'] = $key;
         $drow['label'] = trim($row[1]);
         $drow['description'] = trim($row[3]);
         $drow['awardedTo'] = strtolower(trim($row[4]));
         $drow['awardedBy'] = strtolower(trim($row[5]));
         $drow['frequency'] = strtolower(trim($row[6]));
         $drow['pri'] = trim($row[7]);
         $drow['awardReason'] = strtolower(trim($row[8]));
         $drow['stat'] = strtolower(trim($row[9])) == 'y';
         $drow['country'] = strtolower(trim($row[10]));
         $drow['branch'] = strtolower(trim($row[11]));
         $drow['imageUrl'] = trim($row[2]);
         return $drow;
     });
     Award::unguard();
     $client = new Client();
     foreach ($awardsData as $item) {
         // Takes the url of the image
         $path_parts = pathinfo($item['imageUrl']);
         // download the image and store it locally
         $local_path = storage_path('app\\img') . '\\' . $item['key'] . '.' . strtolower($path_parts['extension']);
         if (!file_exists($local_path)) {
             try {
                 $client->get($item['imageUrl'], ['save_to' => $local_path]);
             } catch (Exception $ex) {
                 // A problem with getting the image from the remote server
                 Log::error(sprintf('Unable to download %s to %s: %s', $item['imageUrl'], $local_path, $ex->getMessage()));
             }
         }
         // Make sure it doesn't try to the save the image to the database
         unset($item['imageUrl']);
         // Save the award to the database
         Award::create($item);
     }
     Award::reguard();
 }
 private function saveAwards($release, $data)
 {
     if (count($data->awards)) {
         foreach ($data->awards as $aw) {
             $award = Award::firstOrCreate(["release_id" => $release->id, "local_id" => $aw->id]);
             $award->title = $aw->title;
             $award->description = $aw->description;
             $award->status = $aw->status;
             $award->date = date("Y-m-d", strtotime($aw->date));
             $award->value = $aw->value->amount;
             $award->currency = $aw->value->currency;
             $award->update();
             $this->saveItems($award, $aw);
             $this->saveSuppliers($award, $aw);
         }
     } else {
         //
     }
 }