Heera/Modules/Estimate/app/Http/Controllers/EstimateController.php
2024-05-16 09:31:08 +05:45

195 lines
5.4 KiB
PHP

<?php
namespace Modules\Estimate\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\Billing\Repositories\BillingComponentInterface;
use Modules\Customer\Repositories\CustomerInterface;
use Modules\Estimate\Models\Estimate;
use Modules\Estimate\Models\EstimateDetail;
use Modules\Estimate\Repositories\EstimateDetailInterface;
use Modules\Estimate\Repositories\EstimateInterface;
class EstimateController extends Controller
{
private $estimateRepository;
private $estimateDetailRepository;
private $customerRepository;
private $billingComponentRepository;
public function __construct(
EstimateInterface $estimateRepository,
BillingComponentInterface $billingComponentRepository,
CustomerInterface $customerRepository
) {
$this->estimateRepository = $estimateRepository;
$this->customerRepository = $customerRepository;
$this->billingComponentRepository = $billingComponentRepository;
}
/**
* Display a listing of the resource.
*/
public function index()
{
$data['title'] = 'Estimate List';
$data['statusList'] = Estimate::APPROVAL_STATUS;
$data['estimates'] = $this->estimateRepository->findAll();
return view('estimate::estimate.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['title'] = 'Create Estimate';
$data['editable'] = false;
$data['customerList'] = $this->customerRepository->pluck();
$data['billingComponentList'] = $this->billingComponentRepository->pluck();
return view('estimate::estimate.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
$request->merge([
'approval_status' => 1,
]);
try {
DB::beginTransaction();
$estimateData = $request->only(Estimate::getFillableFields());
$estimate = $this->estimateRepository->create($estimateData);
$estimateDetails = json_decode($request->estimateDetails);
foreach ($estimateDetails as $item) {
$estimateDetailData = [
'estimate_id' => $estimate->id,
'billing_component_id' => $item->billing_component_id,
'price' => $item->price,
'qty' => $item->qty,
'tax_amount' => $item->tax_amount,
];
EstimateDetail::create($estimateDetailData);
}
toastr()->success('Estimate created succesfully');
DB::commit();
} catch (\Throwable $th) {
echo $th->getMessage();
DB::rollback();
toastr()->error($th->getMessage());
}
return redirect()->route('estimate.index');
}
/**
* Show the specified resource.
*/
public function show($id)
{
$data['title'] = 'Show Estimate';
$data['estimate'] = $this->estimateRepository->getEstimateById($id);
$data['statusList'] = Estimate::APPROVAL_STATUS;
$data['customerList'] = $this->customerRepository->pluck();
return view('estimate::estimate.show', $data);
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$data['title'] = 'Edit Estimate';
$data['editable'] = true;
$data['estimate'] = $this->estimateRepository->getEstimateById($id);
$data['customerList'] = $this->customerRepository->pluck();
$data['billingComponentList'] = $this->billingComponentRepository->pluck();
return view('estimate::estimate.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
$estimateData = $request->only(Estimate::getFillableFields());
try {
$this->estimateRepository->update($id, $estimateData);
flash()->success('Estimate updated succesfully');
} catch (\Throwable $th) {
echo $th->getMessage();
flash()->error($th->getMessage());
}
return redirect()->route('estimate.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
try {
$EstimateModel = $this->estimateRepository->getEstimateById($id);
$EstimateModel->delete();
flash()->success('Estimate deleted succesfully');
} catch (\Throwable $th) {
flash()->error($th->getMessage());
}
return response()->json(['status' => true, 'message' => 'Estimate deleted succesfully']);
}
public function changeStatus(Request $request)
{
try {
$estimate = $this->estimateRepository->getEstimateById($request->estimate_id);
$estimate->update([
'approval_status' => $request->approval_status,
'approval_remarks' => $request->approval_remarks,
'approval_on' => now(),
'approval_by' => auth()->user()->id,
]);
return redirect()->back();
} catch (\Throwable $th) {
flash()->error($th->getMessage());
}
}
}