Created
November 22, 2013 11:14
-
-
Save ArthurGuy/7598288 to your computer and use it in GitHub Desktop.
Crunch Expenses App
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 | |
class CrunchExpenseController extends BaseController { | |
private $rules; | |
public function __construct() | |
{ | |
parent::__construct(); | |
$accessToken = \Crunch\OAuth\AccessToken::create('userToken', 'userSecret'); | |
\Crunch\Connection::createInstance('consumerKey', 'consumerSecret', 'http://localhost/', $accessToken, \Crunch\Connection::DEMO); | |
$this->rules = array( | |
'supplier' => 'required|integer', | |
'date' => 'required|date', | |
'description' => 'required', | |
'amount' => 'required|numeric', | |
'photo' => 'image' | |
); | |
} | |
public function getNewExpense() | |
{ | |
return View::make('crunch.expense')->withSuppliers(\Crunch\Supplier::all()); | |
} | |
public function postNewExpense() | |
{ | |
$data = Input::all(); | |
$validator = Validator::make(Input::all(), $this->rules); | |
if ($validator->fails()) | |
{ | |
return Redirect::to('crunch\expense')->withErrors($validator); | |
} | |
$supplier = \Crunch\Supplier::find(Input::get('supplier')); | |
$expenseDetails = new \Crunch\ExpenseDetail(array('postingDate'=>Input::get('date'), 'supplier'=>new \Crunch\Supplier(array('id'=>$supplier->id)))); | |
$expense = new Crunch\Expense(); | |
$expense->expenseDetails = $expenseDetails; | |
$expense->addLineItem(array('expenseType'=>$supplier->defaultExpenseType, 'lineItemDescription'=>Input::get('description'), 'lineItemAmount'=>new \Crunch\LineItemAmount(array('grossAmount'=>Input::get('amount'))))); | |
//$expense->addPayment(array('amount'=>10, 'paymentMethod'=>'EFT', 'bankAccount'=>'Main')); | |
if (Input::hasFile('photo')) | |
{ | |
$file = Input::file('photo'); | |
$mime = Input::file('photo')->getMimeType(); | |
$name = Input::file('photo')->getClientOriginalName(); | |
$path = Input::file('photo')->getRealPath(); | |
$fileData = base64_encode(file_get_contents($path)); | |
$expense->addAttachment(array('fileName'=>$name, 'contentType'=>$mime, 'fileData'=>$fileData)); | |
} | |
$expense->save(); | |
return Redirect::to('crunch\expense')->with('success', 'Expense Created'); | |
} | |
} |
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
<!DOCTYPE html> | |
<html lang="en-GB"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
<meta name="mobile-web-app-capable" content="yes"> | |
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"> | |
<title>Add Crunch Expense</title> | |
<link rel="stylesheet" href="{{ URL::asset('assets/css/bootstrap.css') }}"> | |
</head> | |
<body class="public"> | |
<form role="form" method="post" class="col-sm-10 col-sm-offset-1" enctype="multipart/form-data"> | |
<h1>New Expense</h1> | |
@if(Session::has('success')) | |
<div class="alert alert-success">{{ Session::get('success') }}</div> | |
@endif | |
@if(Session::has('errors')) | |
<div class="alert alert-danger">{{ Session::get('errors') }}</div> | |
@endif | |
<div class="form-group"> | |
<label for="inputSupplier">Supplier</label> | |
<select name="supplier" class="form-control" id="inputSupplier" required> | |
@foreach ($suppliers as $supplier) | |
<option value="{{ $supplier->id }}">{{ $supplier->name }}</option> | |
@endforeach | |
</select> | |
</div> | |
<div class="form-group"> | |
<label for="inputDate">Date</label> | |
<input name="date" type="date" class="form-control" id="inputDate" placeholder="" max="{{ date('Y-m-d') }}" required> | |
</div> | |
<div class="form-group"> | |
<label for="inputDescription">Description</label> | |
<input name="description" type="text" class="form-control" id="inputDescription" placeholder="What is it?" required> | |
</div> | |
<div class="form-group"> | |
<label for="inputAmount">Amount</label> | |
<input name="amount" type="number" step="0.01" min="0" class="form-control" id="inputAmount" placeholder="How much was it?" required> | |
</div> | |
<div class="form-group"> | |
<label for="inputPhoto">Receipt Photo</label> | |
<input name="photo" type="file" accept="image/*" capture="camera" id="inputPhoto"> | |
</div> | |
<button type="submit" class="btn btn-default">Save</button> | |
</form> | |
</body> | |
</html> |
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
Route::group(array('prefix' => 'crunch'), function() | |
{ | |
Route::get('expense', 'CrunchExpenseController@getNewExpense'); | |
Route::post('expense', 'CrunchExpenseController@postNewExpense'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment