StocksNew/Modules/PurchaseEntry/app/Repositories/PurchaseEntryRepository.php

151 lines
5.5 KiB
PHP
Raw Normal View History

2024-09-19 18:33:08 +05:45
<?php
namespace Modules\PurchaseEntry\Repositories;
use Flasher\Laravel\Http\Request;
use Modules\PurchaseEntry\Models\PurchaseEntry;
use Modules\PurchaseEntry\Models\PurchaseEntryDetail;
use Illuminate\Support\Facades\DB;
class PurchaseEntryRepository implements PurchaseEntryInterface
{
// public function findAll()
// {
// return PurchaseEntry::when(true, function ($query) {
// })->paginate(20);
// }
public function findAll($filters = [], $limit = null, $offset = null)
{
return PurchaseEntry::when($filters, function ($query) use ($filters) {
if (isset($filters["ingredient_category_id"])) {
$query->whereHas('purchaseEntryDetails', function ( $query) use ($filters) {
$query->where('ingredient_category_id', '=', $filters["ingredient_category_id"]);
});
}
if (isset($filters["ingredient_id"])) {
$query->whereHas('purchaseEntryDetails', function ( $query) use ($filters) {
$query->where('ingredient_id', '=', $filters["ingredient_id"]);
});
}
// if (isset($filters["stock_id"])) {
// $query->whereHas('purchaseEntryDetails', function ( $query) use ($filters) {
// $query->where('stock_id', '=', $filters["stock_id"]);
// });
// }
if (isset($filters["date"])) {
$explodeDate = explode("to", $filters['date']);
$query->whereBetween("purchase_date", [$explodeDate[0], preg_replace('/\s+/', '', $explodeDate[1])]);
}
})->get();
}
public function getPurchaseEntryById($PurchaseEntryId)
{
return PurchaseEntry::findOrFail($PurchaseEntryId);
}
public function getPurchaseEntryByEmail($email)
{
return PurchaseEntry::where('email', $email)->first();
}
public function delete($PurchaseEntryId)
{
DB::transaction(function() use ($PurchaseEntryId) {
PurchaseEntryDetail::where('purchaseentry_id', $PurchaseEntryId)->delete();
PurchaseEntry::destroy($PurchaseEntryId);
});
}
public function create($request)
{
$purchaseEntryDetails = $request->except(PurchaseEntry::getFillableField());
$purchaseEntry = $request->only(PurchaseEntry::getFillableField());
$purchaseEntryData = PurchaseEntry::create($purchaseEntry);
$request->merge(['purchaseentry_id' => $purchaseEntryData->id]);
foreach ($purchaseEntryDetails['ingredient_id'] as $key => $ingredientId) {
// dd($request->input('purchaseentry_id'));
$data = [
'purchaseentry_id' => $request->input('purchaseentry_id'),
'ingredient_id' => $purchaseEntryDetails['ingredient_id'][$key],
'ingredient_category_id' => $purchaseEntryDetails['ingredient_category_id'][$key],
// 'stock_id' => $purchaseEntryDetails['stock_id'][$key],
// 'size_id' => $purchaseEntryDetails['size_id'][$key],
'rate' => $purchaseEntryDetails['price'][$key],
'unit_id' => $purchaseEntryDetails['unit_id'][$key],
'quantity' => $purchaseEntryDetails['qty'][$key],
'amount' => $purchaseEntryDetails['amt'][$key],
'desc' => $purchaseEntryDetails['desc'][$key],
];
PurchaseEntryDetail::create($data);
}
}
public function update($PurchaseEntryId, $request)
{
$fillableFields = PurchaseEntry::getFillableField();
$purchaseEntryData = $request->only($fillableFields);
$purchaseEntry = PurchaseEntry::find($PurchaseEntryId);
$purchaseEntry->update($purchaseEntryData);
$additionalExcludes = ['_method', '_token'];
$excludeFields = array_merge($fillableFields, $additionalExcludes);
$data = $request->except($excludeFields);
$updatedCombinations = [];
if (isset($data['product_id'])) {
foreach ($data['product_id'] as $key => $productId) {
$obj = [
'purchaseentry_id' => $PurchaseEntryId,
'product_id' => $productId,
'unit' => $data['unit'][$key],
'rate' => $data['rate'][$key],
'quantity' => $data['qty'][$key],
'amount' => $data['amt'][$key],
'desc' => $data['desc'][$key],
];
$combinationKey = "{$PurchaseEntryId}_{$productId}_{$data['unit'][$key]}";
$purchaseEntryDetail = $purchaseEntry->purchaseEntryDetails()->where('product_id', $productId)->where('unit', $data['unit'][$key])->first();
if ($purchaseEntryDetail) {
$purchaseEntryDetail->update($obj);
} else {
PurchaseEntryDetail::create($obj);
}
$updatedCombinations[] = $combinationKey;
}
}
$purchaseEntry->purchaseEntryDetails()->each(function ($purchaseEntryDetail) use ($updatedCombinations) {
$combinationKey = "{$purchaseEntryDetail->purchaseEntry_id}_{$purchaseEntryDetail->product_id}_{$purchaseEntryDetail->unit}";
if (!in_array($combinationKey, $updatedCombinations)) {
$purchaseEntryDetail->delete();
}
});
return $purchaseEntry;
}
public function pluck()
{
return PurchaseEntry::pluck('name', 'id');
}
}