/** * Recursive function * TODO: move to another place * @param Room $room * @param Property $property * @param $data * @param $rate - rate value for update chanel * @param $weekDays * @param $errors * @param $depth */ function updateChannelRate($room, $property, $data, $rate, $weekDays, &$errors, &$depth) { if ($depth > 5) { //infinity loop protection return; } //get plan mapping $maps = InventoryMap::getByKeys(null, $property->id, $room->id)->get(); foreach ($maps as $mapping) { //get channel $channelSettings = PropertiesChannel::getSettings($mapping->channel_id, $mapping->property_id); if (!$channelSettings) { continue; } $channel = ChannelFactory::create($channelSettings); $channel->setCurrency($property->currency); //updating rates $result = $channel->setRate($mapping->inventory_code, $mapping->plan_code, $data['from_date'], $data['to_date'], $weekDays, $rate, isset($data['single_rate']) ? $data['single_rate'] : null); if (is_array($result)) { $formattedErrors = []; foreach ($result as $error) { $formattedErrors[] = $channelSettings->channel()->name . ': ' . $error; } $errors += $formattedErrors; } } //check if children rooms exist if ($children = $room->children()->get()) { if (!$children->isEmpty()) { $depth++; //so we go deep so lets do rate of current ROOM as default rate, //like if we directly set this rate in form $data['rate'] = $rate; foreach ($children as $child) { switch ($child->formula_type) { case 'x': $rate = $data['rate'] * $child->formula_value; break; case '+': $rate = $data['rate'] + $child->formula_value; break; case '-': $rate = $data['rate'] - $child->formula_value; break; } $this->updateChannelRate($child, $property, $data, $rate, $weekDays, $errors, $depth); } } } }
/** * Update the specified room in storage. * * @return Response */ public function postMap() { $validator = Validator::make($data = Input::all(), InventoryMap::$rules); if ($validator->fails()) { return Redirect::back()->withErrors($validator)->withInput(); } $data['property_id'] = Property::getLoggedId(); InventoryMap::getByKeys($data['channel_id'], $data['property_id'], $data['room_id'])->delete(); //get inventory room name if ($data['code'] && $data['plans']) { $inventory = Inventory::getByKeys($data['channel_id'], $data['property_id'], $data['code'])->first(); if ($inventory) { $preparedData = ['name' => $inventory->name, 'room_id' => $data['room_id'], 'inventory_code' => $data['code'], 'channel_id' => $data['channel_id'], 'property_id' => $data['property_id']]; foreach ($data['plans'] as $planCode) { $plan = InventoryPlan::getByKeys($data['channel_id'], $data['property_id'], $planCode)->first(); if ($plan) { $preparedData['plan_code'] = $plan->code; $preparedData['plan_name'] = $plan->name; InventoryMap::create($preparedData); } } } } return Redirect::action('RoomsController@getIndex'); }