Created
January 14, 2022 15:47
-
-
Save midoalone/2df53d9df48f035cbed833e7f7b79d98 to your computer and use it in GitHub Desktop.
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 | |
namespace App\Containers\Invoices\Tasks; | |
use App\Containers\Accounthistories\Models\Accounthistories; | |
use App\Containers\Accounts\Models\Accounts; | |
use App\Containers\Invoiceitems\Models\Invoiceitems; | |
use App\Containers\Invoices\Data\Repositories\InvoicesRepository; | |
use App\Containers\Products\Models\Products; | |
use App\Ship\Parents\Tasks\Task; | |
use Illuminate\Support\Facades\Auth; | |
class CreateInvoicesTask extends Task { | |
protected $repository; | |
public function __construct( InvoicesRepository $repository ) { | |
$this->repository = $repository; | |
} | |
public function run( array $data ) { | |
$invoice = $this->repository->create( $data ); | |
$user_id = Auth::user()->id; | |
if ( request()->has( 'items' ) ) { | |
$items = request( 'items' ); | |
$items = json_decode( $items ); | |
$total = 0; | |
foreach ( $items as $item ) { | |
$product = Products::find( $item->product_id ); | |
$price = $product->sell_price; | |
if(isset($item->sell_price)) { | |
$price = $item->sell_price; | |
} | |
InvoiceItems::create( [ | |
'invoice_id' => $invoice->id, | |
"product_id" => $item->product_id, | |
"price" => $price, | |
"total_price" => $price * $item->quantity, | |
"quantity" => $item->quantity, | |
"user_id" => $user_id | |
] ); | |
$product->decrement( 'quantity', $item->quantity ); | |
$total += $price * $item->quantity; | |
} | |
// Update invoice total | |
$invoice->update( [ | |
"amount" => $total | |
] ); | |
// Add payment | |
$account = Accounts::find( request('account_id') ); | |
$account->update( [ | |
'balance' => $account->balance + $total | |
] ); | |
Accounthistories::create( [ | |
"type" => "add", | |
'amount' => $total, | |
'account_id' => $account->id, | |
'note' => " فاتورة كاش #{$invoice->id}", | |
'user_id' => $user_id, | |
'invoice_id' => $invoice->id, | |
"invoice_type" => "cash" | |
] ); | |
} | |
return $invoice; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment