Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- // use Illuminate\Support\Facades\Validator;
- use App\Pelanggan;
- use App\Produk;
- use App\ProdukHarga;
- use App\Transaksi;
- use Carbon\Carbon;
- use DB;
- class TransaksiController extends Controller
- {
- public function formTransaksi()
- {
- $pelanggan = Pelanggan::orderBy('nama', 'ASC')->get();
- $produk = Produk::orderBy('varian', 'ASC')->get();
- return view('transaksi.form', compact('pelanggan', 'produk'));
- }
- public function getHargaProduk()
- {
- $harga = ProdukHarga::where('produk_id', request()->id)->get();
- return response()->json(['status' => 'success', 'data' => $harga]);
- }
- public function saveTransaksi(Request $request)
- {
- //VALIDASI
- $this->validate($request, [
- 'tipe_pelanggan' => 'required',
- 'produk_id' => 'required|exist:produk,id',
- 'layanan' => 'required|exist:produk_harga,id',
- 'jaminan' => 'required',
- 'foto_jaminan' => 'required|image|mimes:jpg,jpeg,png,gif',
- 'tgl_pinjam' => 'required|date',
- 'lama_pinjam' => 'required'
- ]);
- $this->validate($request, [
- 'varian' => 'required|string|max:50',
- 'merk' => 'required|string|max:20',
- 'plat' => 'required|string|unique:produk',
- 'gambar' => 'required|image|mimes:png,jpg,jpeg,gif'
- ]);
- DB::beginTransaction();
- try {
- //PELANGGAN BARU
- if ($request->tipe_pelanggan == 0) {
- //HANDLE FILE UPDATE
- $ktpFile = $request->file('foto_ktp');
- $filename = $request->nik . '.' . $ktpFile->getClientOriginalExtension();
- $ktpFile->storeAs('public/pelanggan', $filename);
- $pelanggan = Pelanggan::create([
- 'nik' => $request->nik,
- 'foto_ktp' =>$request->$filename,
- 'nama' => $request->nama,
- 'notelp' =>$request->nama,
- 'alamat' =>$request->alamat
- ]);
- } else {
- //PELANGGAN LAMA
- $pelanggan = Pelanggan::find($request->pelanggan_id);
- }
- $existing = Transaksi::orderBy('created_at', 'DESC')->first();
- $faktur = 'PP-';
- if($existing){
- //Nomor Faktur diawali dari PP-1 dst
- $explode = explode('-', $existing->faktur);
- $faktur = 'PP-' . $explode[1] + 1;
- }
- $jaminanFile = $request->file('foto_jaminan');
- $filenameJaminan = $faktur . '.' . $jaminanFile->getClientOriginalExtension();
- $jaminanFile->storeAs('public/transaksi', $filenameJaminan);
- $produkHarga = ProdukHarga::find($request->layanan);
- $tgl_pinjam = Carbon::parse($request->tgl_pinjam);
- Transaksi::create([
- 'faktur' => $faktur,
- 'pelanggan_id' => $pelanggan->id,
- 'jaminan' => $request->jaminan,
- 'foto_jaminan' => $filenameJaminan,
- 'tgl_pinjam' => $tgl_pinjam->format('Y-m-d'),
- 'tgl_kembali' => $tgl_pinjam->addDays($request->lama_pinjam)->format('Y-m-d'),
- 'harga' => $produkHarga->harga,
- 'denda' => 0,
- 'tgl_dikembalikan' => '',
- 'produk_id' => $request->produk_id,
- 'user_id' => auth()->user()->id,
- 'status' => 0
- ]);
- DB::commit();
- return redirect()->back()->with(['success' => '#' . $faktur . ' Berhasil ditambahkan']);
- } catch (\Exception $e) {
- DB::rollback();
- return $e->getMessage();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement