Skip to content

Instantly share code, notes, and snippets.

@elinardo10
Last active December 6, 2019 05:15
Show Gist options
  • Save elinardo10/b534c2b6236de2c06bca081221065bfc to your computer and use it in GitHub Desktop.
Save elinardo10/b534c2b6236de2c06bca081221065bfc to your computer and use it in GitHub Desktop.
work in InvoiceService
<?php
namespace App\Acme\Services;
use App\Acme\Models\Invoice;
use App\Acme\Models\InvoiceItem;
use App\Acme\Models\Product;
use App\Acme\Models\User;
use App\Acme\Resources\InvoiceResource;
use App\Acme\Resources\UserResource;
use App\Acme\Traits\ApiResponseTrait;
class InvoiceService
{
public function getInvoices()
{
$invoices = Invoice::with(['customer', 'items'])->orderBy('created_at', 'desc')->get();
return InvoiceResource::collection($invoices);
}
public function newInvoice()
{
$customers = User::whereHas('roles', function ($query) {
$query->where('name', 'like', 'customer%');
})
->has('products')
// ->doesntHave('invoices')
->get();
$customers->load('products');
foreach ($customers as $customer) {
foreach ($customer->products as $product) {
$invoiceItems = [
'name' => $product->name,
'unit_price' => $product->pivot->custom_price,
'qty' => $product->pivot->quantity
];
}
$invoice = [
'user_id' => $customer->id,
'due_date' => '2019-12-5',
'sub_total' => $product->pivot->custom_price * $product->pivot->quantity,
'type_payment' => $customer->type_payment,
'discount' => $product->pivot->discount,
'total' => $product->pivot->custom_price * $product->pivot->quantity - $product->pivot->discount,
];
}
$invoice = Invoice::create($invoice);
$invoiceItem = $invoice->items()->create($invoiceItems);
return new InvoiceResource($invoice);
}
public function deleteInvoice(Invoice $invoice)
{
$invoice->delete();
return '';
}
}
@tiagomatosweb
Copy link

tiagomatosweb commented Dec 6, 2019

foreach ($customers as $customer) {
            
            foreach ($customer->products as $product) {
                 // criar dados dos produtos aqui                    
            }

             // Criar dados da invoice aqui

             // Criar a invoice Invoice::create($invoice);

            // Criar items para invoice $invoice->items()->create($invoiceItems);
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment