paginate(20); // } public function findAll($filters = [], $limit = null, $offset = null) { 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(); } 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]); foreach ($salesEntryDetails['stock_id'] as $key => $stockId) { // dd($request->input('salesentry_id')); $data = [ 'salesentry_id' => $request->input('salesentry_id'), 'product_id' => $salesEntryDetails['product_id'][$key], 'category_id' => $salesEntryDetails['category_id'][$key], 'stock_id' => $salesEntryDetails['stock_id'][$key], 'size_id' => $salesEntryDetails['size_id'][$key], 'rate' => $salesEntryDetails['rate'][$key], // 'unit' => $salesEntryDetails['unit'][$key], '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'); } }