paginate(20); } 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['product_id'] as $key => $productId) { $data = [ 'salesentry_id' => $request->input('salesentry_id'), 'product_id' => $productId, 'unit' => $salesEntryDetails['unit'][$key], 'rate' => $salesEntryDetails['rate'][$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'); } }