Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from nmarulo/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Nicolás Marulanda P authored Feb 7, 2018
2 parents 0260fff + 05dad0b commit a7a6c94
Show file tree
Hide file tree
Showing 324 changed files with 28,878 additions and 4,842 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
.idea
app/vendor
vendor/
*.vpp*
*.mwb*
demo-*.sql
25 changes: 25 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<IfModule mod_rewrite.c>

<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>

RewriteEngine On
# redirect all request from silver to public
RewriteRule ^(.*)$ public/$1 [L]

</IfModule>

#in case of public_html you can do something like this

#<IfModule mod_rewrite.c>

#<IfModule mod_negotiation.c>
# Options -MultiViews -Indexes
# </IfModule>

# RewriteEngine On
# #redirect all request from public html to silver/public
# RewriteRule ^(.*)$ silver/public/$1 [L]

#</IfModule>
68 changes: 68 additions & 0 deletions App/Controllers/Api/ClientsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\Controllers\Api;

use App\Facades\Api;
use App\Models\Clients;
use App\Models\Receipts;
use Silver\Core\Controller;
use Silver\Database\Query;
use Silver\Http\Request;

/**
* Clients controller
*/
class ClientsController extends Controller {

private $resourceName = "clients";

public function get() {
$payload = Clients::query()
->all(NULL, function($row) {
return $row->data();
});

return Api::create($payload, 200, $this->resourceName);
}

public function post(Request $request) {
$client = new Clients();
$client->id = $request->input('id');
$client->client_name = $request->input('client_name');
$client->client_address = $request->input('client_address');
$client->client_identification_document = $request->input('client_identification_document');
$client->client_city = $request->input('client_city');
$client = $client->save();

return Api::create($client->data(), 200, $this->resourceName);
}

public function put(Request $request) {
$client = new Clients();
$client->client_name = $request->input('client_name');
$client->client_address = $request->input('client_address');
$client->client_identification_document = $request->input('client_identification_document');
$client->client_city = $request->input('client_city');
$client = $client->save();

return Api::create($client->data(), 200, $this->resourceName);
}

public function delete(Request $request) {
$id = $request->input('id');
$receipt_numbers = intval(Query::count()
->from(Receipts::tableName())
->where('client_id', '=', $id)
->first()->count);

if ($receipt_numbers == 0) {
$client = new Clients();
$client->id = $id;
$client->delete();

return json_encode(TRUE);
}

return json_encode(FALSE);
}
}
66 changes: 66 additions & 0 deletions App/Controllers/Api/ProductsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Controllers\Api;

use App\Facades\Api;
use App\Models\Products;
use App\Models\ReceiptsProducts;
use Silver\Core\Controller;
use Silver\Database\Query;
use Silver\Http\Request;

/**
* ProductsApi controller
*/
class ProductsController extends Controller {

private $resourceName = 'products';

public function get() {
$payload = Products::query()
->all(NULL, function($row) {
return $row->data();
});

return Api::create($payload, 200, $this->resourceName);
}

public function post(Request $request) {
$product = new Products();
$product->id = $request->input('id');
$product->product_name = $request->input('product_name');
$product->product_price_unit = $request->input('product_price_unit');
$product->product_reference = $request->input('product_reference');
$product = $product->save();

return Api::create($product->data(), 200, $this->resourceName);
}

public function put(Request $request) {
$product = new Products();
$product->product_name = $request->input('product_name');
$product->product_price_unit = $request->input('product_price_unit');
$product->product_reference = $request->input('product_reference');
$product = $product->save();

return Api::create($product->data(), 200, $this->resourceName);
}

public function delete(Request $request) {
$id = $request->input('id');
$receipt_products = intval(Query::count()
->from(ReceiptsProducts::tableName())
->where('product_id', '=', $id)
->first()->count);

if ($receipt_products == 0) {
$products = new Products();
$products->id = $id;
$products->delete();

return json_encode(TRUE);
}

return json_encode(FALSE);
}
}
32 changes: 32 additions & 0 deletions App/Controllers/Api/ReceiptsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Controllers\Api;

use App\Facades\Api;
use App\Models\Receipts;
use Silver\Core\Controller;
use Silver\Http\Request;

/**
* ReceiptsApi controller
*/
class ReceiptsController extends Controller {

private $resourceName = 'receipts';

public function get() {
$payload = Receipts::query()
->all(NULL, function($row) {
return $row->data();
});

return Api::create($payload, 200, $this->resourceName);
}

public function delete(Request $request) {
Receipts::find($request->input('id'))
->delete();

return json_encode(TRUE);
}
}
134 changes: 134 additions & 0 deletions App/Controllers/ClientsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace App\Controllers;

use App\Facades\DataTableHTML;
use App\Facades\Messages;
use App\Facades\Pagination;
use App\Facades\Utils;
use App\Models\Clients;
use App\Models\Receipts;
use Silver\Core\Controller;
use Silver\Database\Query;
use Silver\Http\Redirect;
use Silver\Http\Request;
use Silver\Http\View;

/**
* clients controller
*/
class ClientsController extends Controller {

public function index(Request $request) {
return Pagination::viewMake($request, Clients::class, 'clients', 'clients.index', 'clients', DataTableHTML::orderBy($request, Clients::class));
}

public function form(Request $request, $id = FALSE) {
$client = new Clients();
$isUpdate = FALSE;
$actionValue = 'Nuevo';

if ($id) {
if (!$client = Clients::find($id)) {
Messages::addDanger('El cliente no existe.');
Redirect::to(URL . '/clients');
}

$isUpdate = TRUE;
$actionValue = 'Actualizar';
}

return $this->viewForm($request, $isUpdate, $actionValue, $client, $id);
}

private function viewForm(Request $request, $isUpdate, $actionValue, $client, $clientId) {
if (empty($clientId)) {
return View::make('clients.form')
->with('isUpdate', $isUpdate)
->with('actionValue', $actionValue)
->with('client', $client)
->with('receipts', []);
}

$currentModel = function() use ($clientId) {
return Query::count()
->from(Receipts::tableName())
->where('client_id', '=', $clientId)
->single();
};
$dataModel = function($limit, $offset) use ($clientId) {
return $this->getReceipts($clientId, $limit, $offset);
};

return Pagination::viewMake($request, $currentModel, 'receipts', 'clients.form', $clientId, $dataModel)
->with('isUpdate', $isUpdate)
->with('actionValue', $actionValue)
->with('client', $client);
}

private function getReceipts($clientId, $limit, $offset) {
return Receipts::query()
->where('client_id', '=', $clientId)
->orderBy('receipt_date', 'desc')
->orderBy('receipt_number', 'desc')
->limit($limit)
->offset($offset)
->all(NULL, function($row) {
$row->receipt_date = Utils::stringToDate($row->receipt_date, 'Y-m-d', Utils::getDateFormat());

return $row;
});
}

public function postForm(Request $request) {
$client = new Clients();
$id = $request->input('id');
$client->client_name = $request->input('client_name');
$client->client_address = $request->input('client_address');
$client->client_identification_document = $request->input('client_identification_document');
$client->client_city = $request->input('client_city');

if (empty($id)) {
$client->client_number_receipts = 0;

if ($client = Clients::create($client->data())) {
Messages::addSuccess('El cliente ha sido registrado correctamente.');
Redirect::to(URL . '/clients/form/' . $client->id);
} else {
Messages::addDanger('Error al registrar los datos del cliente.');
}
} else {
$client->id = $id;

if ($client->save()) {
Messages::addSuccess('Cliente actualizado correctamente.');
} else {
Messages::addDanger('Error al actualizar los datos del cliente.');
}
}

return $this->viewForm($request, TRUE, 'Actualizar', $client, $id);
}

public function postDelete(Request $request) {
$id = $request->input('id');
$receipt_numbers = intval(Query::count()
->from(Receipts::tableName())
->where('client_id', '=', $id)
->first()->count);

if ($receipt_numbers == 0) {
if ($client = Clients::find($id)) {
$client->delete();
Messages::addSuccess('Cliente eliminado correctamente.');
} else {
Messages::addDanger('El cliente no existe.');
}
} else {
Messages::addDanger('No se puede eliminar un cliente con facturas vinculadas.');
}

Redirect::to(\URL . '/clients');
}

}
Loading

0 comments on commit a7a6c94

Please sign in to comment.