Skip to content

Instantly share code, notes, and snippets.

@midoalone
Created January 14, 2022 15:50
Show Gist options
  • Save midoalone/f0c8f2c5f4bd42d2ed0c173689fde895 to your computer and use it in GitHub Desktop.
Save midoalone/f0c8f2c5f4bd42d2ed0c173689fde895 to your computer and use it in GitHub Desktop.
<?php
namespace App\Containers\Returned\Tasks;
use App\Containers\Accounthistories\Models\Accounthistories;
use App\Containers\Accounts\Models\Accounts;
use App\Containers\Products\Models\Products;
use App\Containers\Returned\Data\Repositories\ReturnedRepository;
use App\Containers\ReturnedItems\Models\ReturnedItems;
use App\Ship\Parents\Tasks\Task;
use Illuminate\Support\Facades\Auth;
class CreateReturnedTask extends Task {
protected $repository;
public function __construct( ReturnedRepository $repository ) {
$this->repository = $repository;
}
public function run( array $data ) {
$user_id = Auth::user()->id;
$invoice = $this->repository->create( $data );
$invoice_type = $data['invoice_type'];
$account = Accounts::find($data['account_id']);
$total = $data['total'];
$items = request( 'items' );
$items = json_decode( $items );
foreach ( $items as $item ) {
$product = Products::find( $item->product_id );
ReturnedItems::create( [
"returned_id" => $invoice->id,
"product_id" => $item->product_id,
"price" => $item->price,
"total" => $item->price * $item->quantity,
"quantity" => $item->quantity,
] );
// Bill account
if ( $invoice_type == "bill" ) {
$product->decrement( 'quantity', $item->quantity );
}
// Cash account : Return products to store
if ( $invoice_type == "cash" ) {
$product->increment( 'quantity', $item->quantity );
}
}
// Bill account
if ( $invoice_type == "bill" ) {
$account->increment('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" => "bill"
]);
}
// Cash account
if ( $invoice_type == "cash" ) {
$account->decrement('balance', $total);
Accounthistories::create([
"type" => "discount",
'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