<?php
namespace App\Http\Controllers;
use App\Models\DetailService;
use App\Models\LaporanKasir;
use App\Models\PembayaranService;
use App\Models\Pengguna;
use App\Models\Pesan;
use App\Models\PesanAction;
use App\Models\PesanCustom;
use App\Models\PesanOtomatis;
use App\Models\Profil;
use App\Models\riwayat_transaksi;
use App\Models\RiwayatTransaksi;
use App\Models\Service;
use App\Models\Teknisi;
use Barryvdh\DomPDF\Facade\Pdf;
use DateTime;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
class PembayaranServiceController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(): Response
{
}
/**
* Show the form for creating a new resource.
*/
public function create(): Response
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id): Response
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id): Response
{
$datas = Service::find($id);
$pengguna = Pengguna::all();
$teknisi = Teknisi::all();
$pembayaran_services = PembayaranService::where('service_id', $datas->id)->first();
$data = [
"datas" => $datas,
"pengguna" => $pengguna,
"teknisi" => $teknisi,
"pembayaran_services" => $pembayaran_services,
];
return response()->view("pembayaran_service.edit", $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
DB::beginTransaction();
try {
$uang_bayar = str_replace(
"Rp ",
"",
str_replace(".", "", $request->uang_bayar)
);
$total_bayar = str_replace(
"Rp ",
"",
str_replace(".", "", $request->total_bayar)
);
$grand_total = str_replace(
"Rp.",
"",
str_replace(",", "", $request->grand_total)
);
$profil = Profil::latest()->first();
$datas = Service::with('pelanggan', 'pengguna')->find($id);
$sales = Pengguna::find($request->sale_id);
$kasir = Pengguna::find($request->kasir_id);
if ($datas->uang_bayar + $uang_bayar >= $datas->grand_total) {
$data_service = [
"status_pembayaran" => "lunas",
"status_transaksi" => "success",
"status" => "selesai",
"tanggal_selesai" => now(),
"jam_selesai" => now()->addHours(8),
"sales_out" => $sales->nama_pengguna,
"kasir_out" => $kasir->nama_pengguna,
"teknisi_id" => $request->teknisi_id,
];
$pembayaran = PembayaranService::where('service_id', $id);
$pembayaran_pertama = $pembayaran->first();
if (isset($pembayaran_pertama)) {
$bukti_transfer_old = $pembayaran_pertama->bukti_transfer;
} else {
$tanggal = $datas->tanggal;
$lk_kasir = LaporanKasir::where('tanggal', $tanggal)->whereRaw("FIND_IN_SET(?, services)", [$id])->get();
// foreach ($lk_kasir as $laporan) {
// $uang_muka_cash = $laporan->uang_muka_cash;
// $uang_muka_transfer = $laporan->uang_muka_transfer;
// if ($request->metode_bayar == "cash") {
// if ($datas->metode_bayar == "cash") {
// $uang_muka_cash = $laporan->uang_muka_cash - $datas->uang_bayar;
// } else {
// $uang_muka_transfer = $laporan->uang_muka_transfer - $datas->uang_bayar;
// }
// $omset_cash = $laporan->omset_cash + $datas->uang_bayar;
// $laporan->update([
// 'omset_cash' => $omset_cash,
// 'uang_muka_cash' => $uang_muka_cash,
// 'uang_muka_transfer' => $uang_muka_transfer,
// ]);
// } else {
// if ($datas->metode_bayar == "cash") {
// $uang_muka_cash = $laporan->uang_muka_cash - $datas->uang_bayar;
// } else {
// $uang_muka_transfer = $laporan->uang_muka_transfer - $datas->uang_bayar;
// }
// $omset_cash = $laporan->omset_cash + $datas->uang_bayar;
// $laporan->update([
// 'omset_cash' => $omset_cash,
// 'uang_muka_cash' => $uang_muka_cash,
// 'uang_muka_transfer' => $uang_muka_transfer,
// ]);
// }
// }
}
// update service
$datas->update($data_service);
// delete pembayaran lama
$pembayaran->delete();
$data_pembayaran = [
"metode_bayar" => $request->metode_bayar,
"akhir_garansi" => $request->akhir_garansi,
"sale_id" => $request->sale_id,
"total_bayar" => $total_bayar,
"uang_bayar" => $uang_bayar,
"uang_muka" => $datas->uang_bayar,
"service_id" => $id,
];
if ($request->hasFile('bukti_transfer')) {
if (isset($bukti_transfer_old)) {
File::delete(public_path('images/Bukti_transfer') . "/" . $bukti_transfer_old);
}
$file = $request->file('bukti_transfer');
$ekstensi = $file->extension();
$baru = "BuktiTF-" . date('ymdhis') . ".$ekstensi";
$file->move(public_path('images/Bukti_transfer'), $baru);
$data_pembayaran['bukti_transfer'] = $baru;
} else {
if (isset($bukti_transfer_old)) {
$data_pembayaran['bukti_transfer'] = $bukti_transfer_old;
}
}
PembayaranService::create($data_pembayaran);
RiwayatTransaksi::where('no_transaksi', $datas->no_service)->delete();
RiwayatTransaksi::create([
"pelanggan_id" => $datas->pelanggan->id,
"no_transaksi" => $datas->no_service,
"jenis_transaksi" => "Service HP",
"tanggal" => now(),
"biaya" => $datas->grand_total,
]);
// detail update
$detail = DetailService::where('service_id', $id)->get();
if ($detail) {
foreach ($detail as $detail) {
$akhir_garansi_detail = date("Y-m-d", strtotime(now()->format('Y-m-d') . " + " . $detail->sparepart->durasi_garansi . " " . $detail->sparepart->ket_waktu));
$akhir = ['akhir_garansi' => $akhir_garansi_detail];
$detail->where('id', $detail->id)->update($akhir);
}
}
// pesan Action
$pesan_action = PesanAction::where('status', 'success')->first();
if (isset($pesan_action)) {
$placeholders = [
'{ service_id }',
'{ nama_pelanggan }',
'{ status }',
'{ biaya }',
'{ uang_muka }',
'{ grand_total }',
'{ tipe_hp }',
'{ kerusakan }',
'{ kasir }',
'{ nama_toko }'
];
$replacements = [
$datas->no_service,
$datas->pelanggan->nama_pelanggan,
ucwords(str_replace('_', ' ', $datas->status)),
$datas->biaya,
$datas->uang_bayar,
$datas->grand_total,
$datas->merk . ' (' . $datas->tipe . ')',
$datas->kerusakan,
$datas->pengguna->nama_pengguna,
@$profil->nama_toko ?? "-"
];
$isi_pesan = str_replace($placeholders, $replacements, $pesan_action->isi_pesan);
if ($pesan_action->nota == 1) {
//pesan pdf
$datas = Service::find($id);
$sales = Pengguna::where('id', $datas->sale_id)->first();
$profil = Profil::first();
$pembayaran_service = PembayaranService::where('service_id', $id)->latest()->first();
$data = [
"datas" => $datas,
"sales" => $sales,
"profil" => $profil,
"pembayaran_service" => $pembayaran_service,
];
$pdf = Pdf::loadView('service.nota', $data);
$fileName = 'NotaPembayaran-' . date('Y') . $datas->pelanggan->telp_pelanggan . '' . date('m') . '' . date('d') . '' . date('his') . '.pdf';
$pdf->setPaper('A4', 'landscape');
$pdf->render();
$path = public_path('pdf/Nota/' . $fileName);
$pdf->save($path);
$pdfUrl = url('pdf/Nota/' . $fileName);
$pesan_pdf = \Helper::sendNotaPDF($datas->pelanggan->telp_pelanggan, $pdfUrl);
File::delete($path);
}
}
//pesan text
Pesan::create([
"isi_pesan" => $isi_pesan,
"service_id" => $id,
]);
\Helper::sendWa($datas->pelanggan->telp_pelanggan, $isi_pesan);
//pesan Otomatis
$pesan_otomatis = PesanCustom::where('status', 'success')->get();
$id_pesan = '';
if (isset($pesan_otomatis)) {
foreach ($pesan_otomatis as $item) {
// replace autotext
$placeholders = [
'{ service_id }',
'{ nama_pelanggan }',
'{ status }',
'{ biaya }',
'{ uang_muka }',
'{ grand_total }',
'{ tipe_hp }',
'{ kerusakan }',
'{ kasir }',
'{ nama_toko }'
];
$replacements = [
$datas->no_service,
$datas->pelanggan->nama_pelanggan,
ucwords(str_replace('_', ' ', $datas->status)),
$datas->biaya,
$datas->uang_bayar,
$datas->grand_total,
$datas->merk . ' (' . $datas->tipe . ')',
$datas->kerusakan,
$datas->pengguna->nama_pengguna,
@$profil->nama_toko ?? "-"
];
$pesan_custom = str_replace($placeholders, $replacements, $item->isi_pesan);
// waktu
$datetimes = now()->addHours(7);
if ($item->satuan_waktu == 'hari') {
$datetimes = $datetimes->addDays($item->jumlah_waktu);
} elseif ($item->satuan_waktu == 'bulan') {
$datetimes = $datetimes->addMonths($item->jumlah_waktu);
} elseif ($item->satuan_waktu == 'tahun') {
$datetimes = $datetimes->addYears($item->jumlah_waktu);
}
$carbonDate = Carbon::parse($item->jam);
$jam = $carbonDate->subHours(1);
list($hours, $minutes) = explode(':', $jam->format('H:i'));
$datetimes = $datetimes->setTime($hours, $minutes);
$id_pesans = [];
// pengeriman pesan
if ($item->nota == 1) {
$sales = Pengguna::where('id', $datas->sale_id)->first();
$pembayaran_service = PembayaranService::where('service_id', $id)->latest()->first();
$profil = Profil::first();
$data = [
"datas" => $datas,
"sales" => $sales,
"pembayaran_service" => $pembayaran_service,
"profil" => $profil
];
$pdf = Pdf::loadView('service.nota', $data);
$fileName = 'NotaPegangan-' . $datas->pelanggan->telp_pelanggan . date('Y') . '' . date('m') . '' . date('d') . '' . date('his') . '.pdf';
$pdf->setPaper('A4', 'landscape');
$pdf->render();
$path = public_path('pdf/Nota/' . $fileName);
$pdf->save($path);
// jalan kan route kirim nota
// return $pdf->stream($path, array("Attachment" => false));
$pdfUrl = url('pdf/Nota/' . $fileName);
// pesan otomatis nota
$id_pesan_pdf = \Helper::SchaduleNotaPDF($datas->pelanggan->telp_pelanggan, $pdfUrl, $datetimes->format('Y-m-d H:i'));
$id_pesans[] = $id_pesan_pdf;
File::delete($path);
}
// pesan otomatis biasa
$id_pesan = \Helper::SchaduleMessage($datas->pelanggan->telp_pelanggan, $pesan_custom, $datetimes->format('Y-m-d H:i'));
$id_pesans[] = $id_pesan;
foreach ($id_pesans as $id) {
$pesan_otomatis = [
'id_pesan' => $id,
'id_service' => $datas->id,
'status_batal' => $item->status_batal
];
PesanOtomatis::create($pesan_otomatis);
}
}
}
// Remove pesan
$pesan_otomatis_remove = PesanOtomatis::where('id_service', $datas->id)->where('status_batal', 'success')->get();
if (isset($pesan_otomatis_remove)) {
foreach ($pesan_otomatis_remove as $pesan) {
\Helper::RemoveMessage($pesan->id_pesan);
$pesan->delete();
}
}
DB::commit();
return redirect()->route('service.index')->with('success', 'Pembayaran Service berhasil ditambah');
if ($pesan_pdf) {
return redirect()->route('service.index')->with('success', 'Pembayaran Service berhasil ditambah');
} else {
return redirect()->route('service.index')->with('error', 'Pesan nota Service gagal ditambah');
}
} else {
DB::rollback();
return redirect()->back()->with('error', 'Uang yang dibayar Kurang!');
}
} catch (\Throwable $th) {
DB::rollback();
return redirect()->route('service.index')->with('error', $th->getMessage());
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id): RedirectResponse
{
//
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]