hi friends I am a beginner in Laravel and I would like you to help with this code to be able to edit and delete, since the create and the list I have.
controller
<?php
namespace App\Http\Controllers;
use App\Repositories\ProductRepository;
use Illuminate\Http\Request,
App\Repositories\ClientRepository,
App\Repositories\InvoiceRepository,
App\Http\Requests,
PDF;
class InvoiceController extends Controller
{
private $_clientRepo;
private $_productRepo;
private $_invoiceRepo;
public function __CONSTRUCT(
ClientRepository $clientRepo,
ProductRepository $productRepo,
InvoiceRepository $invoiceRepo
)
{
$this->_clientRepo = $clientRepo;
$this->_productRepo = $productRepo;
$this->_invoiceRepo = $invoiceRepo;
}
public function index()
{
return view('invoice.index', ['model' => $this->_invoiceRepo->getAll()
]
);
}
public function detail($id)
{
return view('invoice.detail', ['model' => $this->_invoiceRepo->get($id)
]);
}
public function pdf($id)
{
$model = $this->_invoiceRepo->get($id);
$invoice_name = sprintf('comprobante-%s.pdf', str_pad ($model->id, 7, '0', STR_PAD_LEFT));
$pdf = PDF::loadView('invoice.pdf', ['model' => $model
]);
return $pdf->download($invoice_name);
}
public function add()
{
return view('invoice.add');
}
public function save(Request $req)
{
$data = (object)[
'iva' => $req->input('iva'),
'subTotal' => $req->input('subTotal'),
'total' => $req->input('total'),
'client_id' => $req->input('client_id'),
'detail' => []
];
foreach($req->input('detail') as $d){
$data->detail[] = (object)[
'product_id' => $d['id'],
'quantity' => $d['quantity'],
'unitPrice' => $d['price'],
'total' => $d['total']
];
}
return $this->_invoiceRepo->save($data);
}
public function findClient(Request $req)
{
return $this->_clientRepo
->findByName($req->input('q'));
}
public function findProduct(Request $req)
{
return $this->_productRepo
->findByName($req->input('q'));
}
}
model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Invoice extends Model {
public function detail(){
return $this->hasMany('App\InvoiceItem');
}
public function client(){
return $this->belongsTo('App\Client');
}
}
repository
<?php
namespace App\Repositories;
use App\Invoice;
use App\InvoiceItem;
use DB;
class InvoiceRepository {
private $model;
public function __construct(){
$this->model = new Invoice();
}
public function get($id) {
return $this->model->find($id);
}
public function getAll() {
return $this->model->orderBy('id', 'desc')->get();
}
public function save($data) {
$return = (object)['response' => false];
try {
DB::beginTransaction();
$this->model->iva = $data->iva;
$this->model->subTotal = $data->subTotal;
$this->model->total = $data->total;
$this->model->client_id = $data->client_id;
$this->model->save();
$detail = [];
foreach($data->detail as $d) {
$obj = new InvoiceItem;
$obj->product_id = $d->product_id;
$obj->quantity = $d->quantity;
$obj->unitPrice = $d->unitPrice;
$obj->total = $d->total;
$detail[] = $obj;
}
$this->model->detail()->saveMany($detail);
$return->response = true;
DB::commit();
} catch (\Exception $e){
DB::rollBack();
}
return json_encode($return);
}
}
view list
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading" ><h2 class="page-header" align="center">FACTURAS</h2></div>
<div class="panel-body">
<!-- <form class="navbar-form navbar-left pull-right" action="{{ route('Client.index') }}" method="get">
</form>-->
<a class="btn btn-info btn-block" href="{{url('invoice/add')}}">Nueva Factura</a>
</div>
<div class="table-container">
<table class="table table-striped">
<thead>
<tr>
<th class="text-center">Clientes</th>
<th style="width:100px;" class="text-right">IVA</th>
<th style="width:160px;" class="text-right">Sub Total</th>
<th style="width:160px;" class="text-right">Total</th>
<th style="width:180px;" class="text-center">Creado</th>
<th style="width:180px;" class="text-center">Imprimir</th>
<th style="width:30px;"></th>
</tr>
</thead>
<tbody>
@foreach ($model as $m)
<tr>
<td class="text-left">
<a href="{{url('invoice/detail/' . $m->id )}}">
{{ $m->client->name }}
</a>
</td>
<td class="text-right">$ {{number_format($m->iva, 2)}}</td>
<td class="text-right">$ {{number_format($m->subTotal, 2)}}</td>
<td class="text-right">$ {{number_format($m->total, 2)}}</td>
<td class="text-right">{{ $m->created_at }}</td>
<td class="text-right">
<a class="btn btn-success btn-block btn-xs" href="{{ url('invoice/pdf/' . $m->id) }}">
<i class="fa fa-file-pdf-o"></i> Descargar
</a>
</td>
<td>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
@endsection
view add
<invoice>
<div class="well well-sm">
<div class="row">
<div class="col-xs-6">
<input id="client" class="form-control typeahead" type="text" placeholder="Cliente" />
</div>
<div class="col-xs-2">
<input class="form-control" type="text" placeholder="Ruc" readonly value="{ruc}" />
</div>
<div class="col-xs-4">
<input class="form-control" type="text" placeholder="Dirección" readonly value="{address}" />
</div>
</div>
</div>
<div class="row">
<div class="col-xs-7">
<input id="product" class="form-control" type="text" placeholder="Nombre del producto" />
</div>
<div class="col-xs-2">
<input id="quantity" class="form-control" type="text" placeholder="Cantidad" />
</div>
<div class="col-xs-2">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">S/.</span>
<input class="form-control" type="text" placeholder="Precio" value="{price}" readonly />
</div>
</div>
<div class="col-xs-1">
<button onclick={__addProductToDetail} class="btn btn-primary form-control" id="btn-agregar">
<i class="glyphicon glyphicon-plus"></i>
</button>
</div>
</div>
<hr />
<table class="table table-striped">
<thead>
<tr>
<th style="width:40px;"></th>
<th>Producto</th>
<th style="width:100px;">Cantidad</th>
<th style="width:100px;">P.U</th>
<th style="width:100px;">Total</th>
</tr>
</thead>
<tbody>
<tr each={detail}>
<td>
<button onclick={__removeProductFromDetail} class="btn btn-danger btn-xs btn-block">X</button>
</td>
<td>{name}</td>
<td class="text-right">{quantity}</td>
<td class="text-right">$ {price}</td>
<td class="text-right">$ {total}</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="4" class="text-right"><b>IVA</b></td>
<td class="text-right">$ {iva.toFixed(2)}</td>
</tr>
<tr>
<td colspan="4" class="text-right"><b>Sub Total</b></td>
<td class="text-right">$ {subTotal.toFixed(2)}</td>
</tr>
<tr>
<td colspan="4" class="text-right"><b>Total</b></td>
<td class="text-right">$ {total.toFixed(2)}</td>
</tr>
</tfoot>
</table>
<button if={detail.length > 0 && client_id > 0} onclick={__save} class="btn btn-default btn-lg btn-block">
Guardar
</button>
<script>
var self = this;
// Detalle del comprobante
self.client_id = 0;
self.detail = [];
self.iva = 0;
self.subTotal = 0;
self.total = 0;
self.on('mount', function(){
__clientAutocomplete();
__productAutocomplete();
})
__removeProductFromDetail(e) {
var item = e.item,
index = this.detail.indexOf(item);
this.detail.splice(index, 1);
__calculate();
}
__addProductToDetail() {
self.detail.push({
id: self.product_id,
name: self.product.value,
quantity: parseFloat(self.quantity.value),
price: parseFloat(self.price),
total: parseFloat(self.price * self.quantity.value)
});
self.product_id = 0;
self.product.value = '';
self.quantity.value = '';
self.price = '';
__calculate();
}
__save() {
$.post(baseUrl('invoice/save'), {
client_id: self.client_id,
iva: self.iva,
subTotal: self.subTotal,
total: self.total,
detail: self.detail
}, function(r){
if(r.response) {
window.location.href = baseUrl('invoice');
} else {
alert('Ocurrio un error');
}
}, 'json')
}
function __calculate() {
var total = 0;
self.detail.forEach(function(e){
total += e.total;
});
self.total = total;
self.subTotal = parseFloat(total / 1.19);
self.iva = parseFloat(total - self.subTotal);
}
function __clientAutocomplete(){
var client = $("#client"),
options = {
url: function(q) {
return baseUrl('invoice/findClient?q=' + q);
},
getValue: 'name',
list: {
onClickEvent: function() {
var e = client.getSelectedItemData();
self.client_id = e.id;
self.ruc = e.ruc;
self.address = e.address;
self.update();
}
}
};
client.easyAutocomplete(options);
}
function __productAutocomplete(){
var product = $("#product"),
options = {
url: function(q) {
return baseUrl('invoice/findProduct?q=' + q);
},
getValue: 'name',
list: {
onClickEvent: function() {
var e = product.getSelectedItemData();
self.product_id = e.id;
self.price = e.price;
self.update();
}
}
};
product.easyAutocomplete(options);
}
</script>
</invoice>