You are on page 1of 5

<?

php

namespace App\Http\Livewire\CashingPermession;

use App\Models\equalizQuantite;
use App\Models\ExportItem;
use App\Models\FactorySetting;
use App\Models\Item;
use App\Models\ItemMovement;
use App\Models\ItemPreviosBalance;
use App\Models\ItemStore;
use App\Models\Store;
use App\Models\TotalEqualizQuantite;
use App\Models\TotalExportItem;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use Livewire\Component;

class Cashing extends Component


{
public $notes, $store_id, $date, $datte, $new_notes, $new_store_id, $item_id,
$itemss, $itemStorsEdit, $newitem, $export_id, $item_idd;
public $inputs = [['item_id' => '', 'amount' => '']];
public $newInputs = [['item_idd' => '', 'amountt' => '']];
public $arr = [['item_idd' => '', 'amountt' => '']];
public $showModal = false;

public function render()


{

$stores = Store::get();

$items = ItemStore::where('store_id', $this->store_id)->with('items')-


>get();
$newItems = Item::get();
// $this->itemStorsEdit = ExportItem::where('total_export_item_id',13)-
>get();
$exports = TotalExportItem::orderBy('id')->paginate(10);
$quantites = TotalEqualizQuantite::get();
$factory = FactorySetting::first();
return view('livewire.cashing-permession.cashing', compact('stores',
'items', 'newItems', 'quantites', 'factory', 'exports'));
}

public function rules()


{
return [
'store_id' => 'required',
'inputs.*.amount' => 'lte:' . $this->itemss,
];
}

protected $messages = [
'required' => '‫'هذا الحقل مطلوب‬,
];

public function mount()


{
$this->date = date('Y-m-d');
$this->datte = date('Y-m-d');

public function updated($fields)


{
$this->validateOnly($fields);
$this->getvalue();

public function getvalue()


{
foreach ($this->inputs as $key => $input) {
$this->itemss = ItemStore::where('item_id', $input['item_id'])-
>where('store_id', $this->store_id)->value('balance');
$this->inputs[$key]['Current_quantity'] = $this->itemss;
}
foreach ($this->arr as $i=>$ar){
$this->arr[$i]['new_current_amount'] =ExportItem::where('item_id',
$ar['item_idd'])->value('quantity') ;
}
}

public function rest()


{
$this->store_id = '';
$this->date = '';
$this->notes = '';
}

public function store()


{

$this->validate($this->rules());

$errors = new MessageBag(['inputs.*.amount' => '‫ '" يجب ادخال رقم اقل من‬.
$this->itemss]);

$this->withErrors($errors);

$total = TotalExportItem::create([
'store_id' => $this->store_id,
'date' => $this->date,
'notes' => $this->notes,
]);
foreach ($this->inputs as $key => $input) {

$diff = $input['amount'] - $input['Current_quantity'];


ExportItem::create([
'total_export_item_id' => $total->id,
'item_id' => $input['item_id'],
'quantity' => $input['amount']
]);
if ($diff <= 0) {
ItemMovement::create([
'importQuantity' => $diff,
'store_id' => $this->store_id,
'item_id' => $input['item_id'],
'firstTermBalance' => $input['Current_quantity'],
'exportQuantity' => $input['amount']
]);
$itemMovement = ItemMovement::where('item_id', $input['item_id'])-
>where('store_id', $this->store_id)->first();
ItemPreviosBalance::where('item_id', $input['item_id'])-
>updateOrCreate(
[
'store_id' => $this->store_id,
'item_id' => $input['item_id'],
'date' => $this->date

], [

'amount' => $input['amount'] - $input['Current_quantity'],


]
);

ItemStore::where('item_id', $input['item_id'])->update([
'balance' => $input['amount']
]);
}

}
$this->rest();
$this->inputs = [['item_id' => '', 'amount' => '']];
session()->flash('success', '‫;)'تم الحفظ بنجاح‬

public function addInputs()


{
$this->inputs [] = ['item_id' => '', 'amount' => ''];

public function removeInputs($index)


{
unset($this->inputs[$index]);
$this->inputs = array_values($this->inputs);

public function newRow()


{
$this->newInputs [] = ['item_idd' => '', 'amountt' => ''];
}

public function removeRow($index)


{

unset($this->newInputs[$index]);
$this->newInputs = array_values($this->newInputs);
}

public function PrintExportItem($id)


{

$Id = ExportItem::find($id);
$this->export_id = $id;

return view('livewire.admin.exportItem');
}

public function delete($id)


{
$this->export_id = $id;
}

public function destroy()


{
TotalExportItem::where('id', $this->export_id)
->delete();
session()->flash('success', '‫;)'تم الحذف بنجاح‬
$this->dispatchBrowserEvent('close-modal');
}

private function withErrors(MessageBag $errors)


{
}

public function edit($id)


{

$this->showModal=true;
$totalExport = TotalExportItem::find($id);
$this->export_id = $id;
$this->new_store_id = $totalExport->store_id;
$this->new_notes = $totalExport->notes;
// dd($this->all());

// $this->itemss = ItemStore::where('store_id', $this->new_store_id)-


>get();
//$newItems = Item::get();
//dd($newItems);
foreach ($this->arr as $key => $ar) {
$this->itemss = ExportItem::where('total_export_item_id', $totalExport-
>id)->with('items')->get();

foreach ($this->itemss as $i => $it) {

$this->arr[$key]['item_idd'] = $it->item_id;

$this->arr[$key]['new_current_amount'] = $it->quantity;
// dd($this->newInputs[$key]['new_current_amount']);
}

// }
}
}

public function update()


{
dd($this->all());
}

You might also like