/** * Creates a new lease and returns some basic details about it. * Request might also contain optional $_GET['duration'] parameter, otherwise a default value will be used. * * @param $string * @param Request $request * @return mixed */ public function lease($string, Request $request) { $picker = $this->pickResource($string); if ($picker['error']) { return $this->respondWithError($picker['error']); } $resource = Resource::find($picker['resourceId']); $leaseParams = Lease::stub($resource->id, null, $request->duration); $lease = Lease::create($leaseParams); $response = ['leased_at' => Carbon::now(), 'links' => ['rel' => 'details', 'uri' => '/leases/' . $lease->uuid]]; return $this->respond($response, 201); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // $this->validate($request, ['startdate' => 'required|date', 'enddate' => 'required|date', 'monthly_rent' => 'required|numeric', 'pet_rent' => 'numeric', 'deposit' => 'numeric', 'pet_deposit' => 'numeric']); $input = $request->all(); $input['startdate'] = Carbon::parse($input['startdate']); $input['enddate'] = Carbon::parse($input['enddate']); $apartment = Apartment::find($input['apartment_id']); // Check for Date overlap on existing leases if (!$apartment->checkAvailability($input['startdate'], $input['enddate'])) { return back()->withInput()->with('error', 'These dates are not available!'); } $lease = Lease::create(['apartment_id' => $input['apartment_id'], 'startdate' => $input['startdate'], 'enddate' => $input['enddate'], 'monthly_rent' => $input['monthly_rent'], 'pet_rent' => $input['pet_rent']]); //Create Lease Details $start = $lease->startdate; $end = $lease->enddate; $inc = \DateInterval::createFromDateString('first day of next month'); $p = new \DatePeriod($start, $inc, $end); foreach ($p as $d) { // echo $p . " - " . $d . "<br>"; // dd($p); $d = Carbon::instance($d); $lease_detail = new LeaseDetail(); $lease_detail->month = $d->format('n'); $lease_detail->year = $d->format('Y'); // echo $end->month . " " . $end->year . " + " . $d->format('n') . " " . $d->format('Y'); //If the startdate has the same month and year as the current month, calculate a partial if ($start->month == $d->format('n') && $start->year == $d->format('Y')) { $multiplier = (date('t', strtotime($d->format('Y-m-d'))) - ($start->day - 1)) / date('t', strtotime($d->format('Y-m-d'))); $lease_detail->startdate = $start; $lease_detail->enddate = Carbon::parse('last day of ' . $d->format('F') . " " . $d->year); // echo "Remaining Days/Total Days in Month (" . date('t',strtotime($d->format('Y-m-d'))) . " - " . ($start->day-1) . "/" . date('t',strtotime($d->format('Y-m-d'))) . ") Mulitiplier: "; } elseif ($end->month == $d->format('n') && $end->year == $d->format('Y')) { $multiplier = $end->day / date('t', strtotime($d->format('Y-m-d'))); $lease_detail->startdate = Carbon::parse('first day of ' . $d->format('F') . " " . $d->year); $lease_detail->enddate = $end; // echo "# of Days in Last Month/Total Days in Month (" . ($end->day) . "/" . date('t',strtotime($d->format('Y-m-d'))) . ") Mulitiplier: "; } else { //echo '- Full Month'; $multiplier = 1.0; $lease_detail->startdate = Carbon::parse('first day of ' . $d->format('F') . " " . $d->year); $lease_detail->enddate = Carbon::parse('last day of ' . $d->format('F') . " " . $d->year); } // echo $multiplier . "<br>"; $lease_detail->multiplier = $multiplier; $lease_detail->monthly_rent = round($lease->monthly_rent * $multiplier, 2); $lease_detail->monthly_pet_rent = round($lease->pet_rent * $multiplier, 2); $lease->details()->save($lease_detail); } // Create Lease Deposits if (!empty($input['deposit'])) { $ld = new LeaseDeposit(); $ld->amount = $input['deposit']; $ld->deposit_type = 'Damage Deposit'; $lease->leaseDeposits()->save($ld); } if (!empty($input['pet_deposit'])) { $pd = new LeaseDeposit(); $pd->amount = $input['pet_deposit']; $pd->deposit_type = 'Pet Deposit'; $lease->leaseDeposits()->save($pd); } return redirect()->action('LeaseController@show', [$apartment->name, $lease->id]); }