StocksNew/Modules/SalesEntry/app/Repositories/SalesEntryRepository.php

151 lines
5.2 KiB
PHP
Raw Permalink Normal View History

2024-09-11 06:47:15 +00:00
<?php
namespace Modules\SalesEntry\Repositories;
use Flasher\Laravel\Http\Request;
use Modules\SalesEntry\Models\SalesEntry;
use Modules\SalesEntry\Models\SalesEntryDetail;
use Illuminate\Support\Facades\DB;
class SalesEntryRepository implements SalesEntryInterface
{
2024-09-17 10:49:40 +00:00
// public function findAll()
// {
// return SalesEntry::when(true, function ($query) {
// })->paginate(20);
// }
public function findAll($filters = [], $limit = null, $offset = null)
2024-09-11 06:47:15 +00:00
{
2024-09-17 10:49:40 +00:00
return SalesEntry::when($filters, function ($query) use ($filters) {
if (isset($filters["category_id"])) {
$query->whereHas('salesEntryDetails', function ( $query) use ($filters) {
$query->where('category_id', '=', $filters["category_id"]);
});
}
if (isset($filters["product_id"])) {
$query->whereHas('salesEntryDetails', function ( $query) use ($filters) {
$query->where('product_id', '=', $filters["product_id"]);
});
}
if (isset($filters["stock_id"])) {
$query->whereHas('salesEntryDetails', function ( $query) use ($filters) {
$query->where('stock_id', '=', $filters["stock_id"]);
});
}
if (isset($filters["date"])) {
$explodeDate = explode("to", $filters['date']);
$query->whereBetween("sales_date", [$explodeDate[0], preg_replace('/\s+/', '', $explodeDate[1])]);
}
})->get();
2024-09-11 06:47:15 +00:00
}
public function getSalesEntryById($SalesEntryId)
{
return SalesEntry::findOrFail($SalesEntryId);
}
public function getSalesEntryByEmail($email)
{
return SalesEntry::where('email', $email)->first();
}
public function delete($SalesEntryId)
{
DB::transaction(function() use ($SalesEntryId) {
SalesEntryDetail::where('salesentry_id', $SalesEntryId)->delete();
SalesEntry::destroy($SalesEntryId);
});
}
public function create($request)
{
$salesEntryDetails = $request->except(SalesEntry::getFillableField());
$salesEntry = $request->only(SalesEntry::getFillableField());
$salesEntryData = SalesEntry::create($salesEntry);
$request->merge(['salesentry_id' => $salesEntryData->id]);
2024-09-17 10:49:40 +00:00
foreach ($salesEntryDetails['stock_id'] as $key => $stockId) {
// dd($request->input('salesentry_id'));
2024-09-11 06:47:15 +00:00
$data = [
'salesentry_id' => $request->input('salesentry_id'),
2024-09-17 10:49:40 +00:00
'product_id' => $salesEntryDetails['product_id'][$key],
'category_id' => $salesEntryDetails['category_id'][$key],
'stock_id' => $salesEntryDetails['stock_id'][$key],
'size_id' => $salesEntryDetails['size_id'][$key],
2024-09-11 06:47:15 +00:00
'rate' => $salesEntryDetails['rate'][$key],
2024-09-17 10:49:40 +00:00
// 'unit' => $salesEntryDetails['unit'][$key],
2024-09-11 06:47:15 +00:00
'quantity' => $salesEntryDetails['qty'][$key],
'amount' => $salesEntryDetails['amt'][$key],
'desc' => $salesEntryDetails['desc'][$key],
];
SalesEntryDetail::create($data);
}
}
public function update($SalesEntryId, $request)
{
$fillableFields = SalesEntry::getFillableField();
$salesEntryData = $request->only($fillableFields);
$salesEntry = SalesEntry::find($SalesEntryId);
$salesEntry->update($salesEntryData);
$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 = [
'salesentry_id' => $SalesEntryId,
'product_id' => $productId,
'unit' => $data['unit'][$key],
'rate' => $data['rate'][$key],
'quantity' => $data['qty'][$key],
'amount' => $data['amt'][$key],
'desc' => $data['desc'][$key],
];
$combinationKey = "{$SalesEntryId}_{$productId}_{$data['unit'][$key]}";
$salesEntryDetail = $salesEntry->salesEntryDetails()->where('product_id', $productId)->where('unit', $data['unit'][$key])->first();
if ($salesEntryDetail) {
$salesEntryDetail->update($obj);
} else {
SalesEntryDetail::create($obj);
}
$updatedCombinations[] = $combinationKey;
}
}
$salesEntry->salesEntryDetails()->each(function ($salesEntryDetail) use ($updatedCombinations) {
$combinationKey = "{$salesEntryDetail->salesEntry_id}_{$salesEntryDetail->product_id}_{$salesEntryDetail->unit}";
if (!in_array($combinationKey, $updatedCombinations)) {
$salesEntryDetail->delete();
}
});
return $salesEntry;
}
public function pluck()
{
return SalesEntry::pluck('name', 'id');
}
}