Last active
December 6, 2019 05:15
-
-
Save elinardo10/b534c2b6236de2c06bca081221065bfc to your computer and use it in GitHub Desktop.
work in InvoiceService
This file contains 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 | |
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
commented
Dec 6, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment