Helpers->helper.php
<?php
//$username=Session::get('key');
//https://laravel.com/docs/8.x/queries#aggregates
//https://blog.quickadminpanel.com/5-ways-to-use-raw-database-queries-in-laravel
/*
Selamat Datang {{ Auth::user()->level }} : {{ Auth::user()->name }}
@if (session('status'))
<div class="alert alert-succes s" role="alert">
{{ session('status') }}
</div>
@endif
@extends('layouts.app')
@section('content')
*/
function getOngkir($kode){
$rp=20000;
if($kode=="Luar Jabodetabek"){$rp=30000;}
return $rp;
}
function getKategori($kode){
$hasil=\App\Kategori::find($kode)->value('nama_kategori');//BEST
return $hasil;
}
function getNominalJual($kode){
$results = DB::table('penjualan')->select('id_pelanggan','total','wilayah')->where('id_penjualan','=', $kode)->first();
$pelanggan=getPelanggan2($results->id_pelanggan);
$total=RP($results->total);
$wilayah=$results->wilayah;
return "$pelanggan $wilayah ->".$total;
}
function getListJual($kode){
$sql= "SELECT `jdetail`.`id_barang`,`jdetail`.`jumlah`,`jdetail`.`subtotal`,`penjualan`.`ongkir` FROM `penjualan`,`jdetail` WHERE `penjualan`.`id_penjualan`=`jdetail`.`id_penjualan` and `penjualan`.`id_penjualan`= '$kode'";
$results = DB::select($sql);
$gab="";
$total=0;
$ongkir=0;
foreach($results as $i){
$id_barang= $i->id_barang;
$jumlah= $i->jumlah;
$ongkir= $i->ongkir;
$subtotal= $i->subtotal;
$nb=getNamaBarang($id_barang);
$gab.="$nb ($jumlah),";
$total+=$subtotal;
}
$gab.="Total:".RP($total).",Ongkir:".RP($ongkir);
return $gab;
}
function getTotal($kode){
$sql= "SELECT `jdetail`.`subtotal` FROM `penjualan`,`jdetail` WHERE `penjualan`.`id_penjualan`=`jdetail`.`id_penjualan` and `penjualan`.`id_penjualan`= '$kode'";
$results = DB::select($sql);
$total=0;
foreach($results as $i){
$subtotal= $i->subtotal;
$total+=$subtotal;
}
return $total;
}
function getListMasuk($kode){
$sql= "SELECT `mdetail`.`id_barang`,`mdetail`.`jumlah`,`mdetail`.`subtotal` FROM `pemasukan`,`mdetail` WHERE `pemasukan`.`id_pemasukan`=`mdetail`.`id_pemasukan` and `pemasukan`.`id_pemasukan`= '$kode'";
$results = DB::select($sql);
$gab="";
$total=0;
foreach($results as $i){
$id_barang= $i->id_barang;
$jumlah= $i->jumlah;
$subtotal= $i->subtotal;
$nb=getNamaBarang($id_barang);
$gab.="$nb ($jumlah),";
$total+=$subtotal;
}
$gab.="Total:".RP($total);
return $gab;
}
function getPelanggan($kode){
//$hasil=\App\Pelanggan::find($kode)->value('nama_pelanggan');//BEST
$results = DB::select('select `nama_pelanggan` from `pelanggan` where `id_pelanggan` = ?', [$kode]);
$hasil=$results[0]->nama_pelanggan;
return $hasil;
}
function getPelanggan2($kode){
$results = DB::select('select `nama_pelanggan` from `pelanggan` where `id_pelanggan` = ?', [$kode]);
$hasil=$results[0]->nama_pelanggan;
return $hasil;
}
function getNamaBarang($kode){
// $pesan = Pesan::orderBy('id_pesan', 'desc')->get();
//$mdetail = Mdetail::all()->where('id_pemasukan',$id); //One to Many
//ModelName::find(id)->value('column_name');
$hasil=\App\Barang::find($kode)->value('nama_barang');//BEST
//$results = DB::select('select `nama_barang`,`stok` from `barang` where `id_barang` = ?', [$kode]);
//$hasil=$results[0]->nama_barang;
//$sql= "SELECT `nama_barang` FROM `barang` WHERE `id_barang`= '".$kode."'";
//$results = DB::select($sql);
//$hasil=$results[0]->nama_barang;
//$results = DB::table('barang')->select('nama_barang','stok')->where('id_barang', '=', $kode)->orderBy('id_barang','asc')->get();//ARRAY
//$hasil=$results[0]->nama_barang;
//foreach($results as $i){echo $i->user_id;}
//$results = DB::table('barang')->select('nama_barang')->where('id_barang','=', $kode)->first();
//$hasil=$results->nama_barang;
//$results = DB::table('barang')->select('nama_barang')->where('id_barang', $kode)->first();//first=return no array
//$hasil=$results->nama_barang;
//$hasil=DB::table('barang')->where('id_barang', $kode)->value('nama_barang');//L5
//$hasil=DB::table('barang')->where('id_barang', $kode)->pluck('nama_barang');//L4
//$hasil = DB::table('barang')->where('id_barang', $kode)->select('nama_barang')->pluck('nama_barang')->first();
//$hasil=DB::table('barang')->where('id_barang', $kode)->orderBy('id_barang', 'desc')->value('nama_barang');
//$hasil =\App\Barang::where('id_barang', $kode)->first()->nama_barang;
//$hasil = \App\Barang::where('id_barang',$kode)->value('nama_barang');
return $hasil;
}
//exe
//DB::statement('UPDATE users SET role_id = 1 WHERE role_id IS NULL AND YEAR(created_at) > 2020');
//DB::statement('DROP TABLE users');
//DB::statement('ALTER TABLE projects AUTO_INCREMENT=123');
//LIBRARY SQL //RAW
//\App\User::whereIn('mobile',['971700000', '965000000'])->select('id')->pluck('id')->all();
/*
$users = DB::table('users')
->selectRaw('count(*) as user_count, status')
->where('status', '<>', 1)
->groupBy('status')
->get();
$products = DB::table('products')
->leftjoin('category','category.product_id','=','products.id')
->selectRaw('COUNT(*) as nbr', 'products.*')
->groupBy('products.id')
->get();
$salaries = DB::table('salaries')
->selectRaw('companies.name as company_name, avg(salary) as avg_salary, count(*) as people_count')
->join('companies', 'salaries.company_id', '=', 'companies.id')
->groupBy('companies.id')
->orderByDesc('avg_salary')
->get();
$results = User::selectRaw('YEAR(birth_date) as year, COUNT(id) as amount')
->groupByRaw('YEAR(birth_date)')
->havingRaw('YEAR(birth_date) > 2000')
->orderByRaw('YEAR(birth_date)')
->get();
$products = Product::select('id', 'name')
->selectRaw('price - discount_price AS discount')
->get();
$users = DB::table('users')
->select('name', 'surname')
->selectRaw("(CASE WHEN (gender = 1) THEN 'M' ELSE 'F' END) as gender_text")
->get();
*/
/*
https://laravel.com/docs/8.x/requests
use Illuminate\Http\Request;
public function store(Request $request){
$name = $request->input('name');
$uri = $request->path();
$url = $request->url();
$urlWithQueryString = $request->fullUrl();
$request->fullUrlWithQuery(['type' => 'phone']);
$method = $request->method();
if ($request->isMethod('post')) {}
$value = $request->header('X-Header-Name');
$value = $request->header('X-Header-Name', 'default');
if ($request->hasHeader('X-Header-Name')) {}
$token = $request->bearerToken();
$ipAddress = $request->ip();
$contentTypes = $request->getAcceptableContentTypes();
if ($request->accepts(['text/html', 'application/json'])) {}
$preferred = $request->prefers(['text/html', 'application/json']);
if ($request->expectsJson()) {}
$input = $request->all();
$input = $request->collect();
$request->collect('users')->each(function ($user) {});
$name = $request->input('name');
$name = $request->input('name', 'Sally');
$name = $request->input('products.0.name');
$names = $request->input('products.*.name');
$input = $request->input();
$name = $request->query('name');
$name = $request->query('name', 'Helen');
$query = $request->query();
$name = $request->input('user.name');
$archived = $request->boolean('archived');
$name = $request->name;
$input = $request->only(['username', 'password']);
$input = $request->only('username', 'password');
$input = $request->except(['credit_card']);
$input = $request->except('credit_card');
if ($request->has('name')) {}
if ($request->has(['name', 'email'])) {}
$request->whenHas('name', function ($input) {});
if ($request->hasAny(['name', 'email'])) {}
if ($request->filled('name')) {}
$request->whenFilled('name', function ($input) {});
if ($request->missing('name')) {}
$request->flash();
$request->flashOnly(['username', 'email']);
$request->flashExcept('password');
return redirect('form')->withInput();
return redirect()->route('user.create')->withInput();
return redirect('form')->withInput(
$request->except('password')
);
$username = $request->old('username');
<input type="text" name="username" value="{{ old('username') }}">
$value = $request->cookie('name');
$file = $request->file('photo');
$file = $request->photo;
if ($request->hasFile('photo')) {}
if ($request->file('photo')->isValid()) {}
$path = $request->photo->path();
$extension = $request->photo->extension();
$path = $request->photo->store('images');
$path = $request->photo->store('images', 's3');
$path = $request->photo->storeAs('images', 'filename.jpg');
$path = $request->photo->storeAs('images', 'filename.jpg', 's3');
https://laravel.com/docs/8.x/requests
}//public
*/
function RP($angka){
$hasil = number_format($angka,0, ',' , '.');
return $hasil.";";
}
function terbilang($i){
$huruf = array("", "Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh", "Delapan", "Sembilan", "Sepuluh", "Sebelas");
if ($i < 12) return " " . $huruf[$i];
elseif ($i < 20) return terbilang($i - 10) . " Belas";
elseif ($i < 100) return terbilang($i / 10) . " Puluh" . terbilang($i % 10);
elseif ($i < 200) return " Seratus" . terbilang($i - 100);
elseif ($i < 1000) return terbilang($i / 100) . " Ratus" . terbilang($i % 100);
elseif ($i < 2000) return " Seribu" . terbilang($i - 1000);
elseif ($i < 1000000) return terbilang($i / 1000) . " Ribu" . terbilang($i % 1000);
elseif ($i < 1000000000) return terbilang($i / 1000000) . " Juta" . terbilang($i % 1000000);
}
?>
<?php function RP2($rupiah){return number_format($rupiah,"2",",",".");}?>
<?php
function WKT($sekarang){
if($sekarang=="0000-00-00"){$sekarang=date("Y-m-d");}
$tanggal = substr($sekarang,8,2)+0;
$bulan = substr($sekarang,5,2);
$tahun = substr($sekarang,0,4);
$judul_bln=array(1=> "Januari", "Februari", "Maret", "April", "Mei","Juni", "Juli", "Agustus", "September","Oktober", "November", "Desember");
$wk=$tanggal." ".$judul_bln[(int)$bulan]." ".$tahun;
return $wk;
}
?>
<?php
function WKTP($sekarang){
$tanggal = substr($sekarang,8,2)+0;
$bulan = substr($sekarang,5,2);
$tahun = substr($sekarang,2,2);
$judul_bln=array(1=> "Jan", "Feb", "Mar", "Apr", "Mei","Jun", "Jul", "Agu", "Sep","Okt", "Nov", "Des");
$wk=$tanggal." ".$judul_bln[(int)$bulan]."'".$tahun;
return $wk;
}
?>
<?php
function BAL($tanggal){
$arr=explode(" ",$tanggal);
if($arr[1]=="Januari"||$arr[1]=="January"){$bul="01";}
else if($arr[1]=="Februari"||$arr[1]=="February"){$bul="02";}
else if($arr[1]=="Maret"||$arr[1]=="March"){$bul="03";}
else if($arr[1]=="April"){$bul="04";}
else if($arr[1]=="Mei"||$arr[1]=="May"){$bul="05";}
else if($arr[1]=="Juni"||$arr[1]=="June"){$bul="06";}
else if($arr[1]=="Juli"||$arr[1]=="July"){$bul="07";}
else if($arr[1]=="Agustus"||$arr[1]=="August"){$bul="08";}
else if($arr[1]=="September"){$bul="09";}
else if($arr[1]=="Oktober"||$arr[1]=="October"){$bul="10";}
else if($arr[1]=="November"){$bul="11";}
else if($arr[1]=="Nopember"){$bul="11";}
else if($arr[1]=="Desember"||$arr[1]=="December"){$bul="12";}
return "$arr[2]-$bul-$arr[0]";
}
?>
<?php
function BALP($tanggal){
$arr=split(" ",$tanggal);
if($arr[1]=="Jan"){$bul="01";}
else if($arr[1]=="Feb"){$bul="02";}
else if($arr[1]=="Mar"){$bul="03";}
else if($arr[1]=="Apr"){$bul="04";}
else if($arr[1]=="Mei"){$bul="05";}
else if($arr[1]=="Jun"){$bul="06";}
else if($arr[1]=="Jul"){$bul="07";}
else if($arr[1]=="Agu"){$bul="08";}
else if($arr[1]=="Sep"){$bul="09";}
else if($arr[1]=="Okt"){$bul="10";}
else if($arr[1]=="Nov"){$bul="11";}
else if($arr[1]=="Nop"){$bul="11";}
else if($arr[1]=="Des"){$bul="12";}
return "$arr[2]-$bul-$arr[0]";
}
?>
Penjualan Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Penjualan;
use App\Pelanggan;
use DB;
use Auth;
use File;
class PenjualanController extends Controller
{
public function __construct(){
$this->middleware('auth');
}
public function index(){
$no = 0;
$penjualan = Penjualan::orderBy('id_penjualan', 'desc')->get();
return view('admin.penjualan.index', compact('penjualan', 'no'));
}
public function _index(){
$id_pelanggan=Auth::user()->rkey ;
$no = 0;
// $penjualan = Penjualan::orderBy('id_penjualan', 'desc')->get();
$penjualan = DB::select("select * from `pelanggan`,`penjualan` where `penjualan`.`id_pelanggan`='$id_pelanggan' and `penjualan`.`id_pelanggan`=`pelanggan`.`id_pelanggan` order by `penjualan`.`id_penjualan` desc");
return view('admin.penjualan._index', compact('penjualan', 'no'));
}
public function create(){
if(Auth::user()->level=="Pelanggan"){
return view('admin.penjualan._create');
}
else{
$pelanggan = Pelanggan::all();
return view('admin.penjualan.create', compact('pelanggan'));
}
}
public function store(Request $request){
$this->validate($request,[
'id_pelanggan'=>'required'
]);
$penjualan = New Penjualan;
$wilayah=$request->wilayah;
$penjualan->tanggal = date("Y-m-d");//$request->tanggal;
$penjualan->jam = date("H:i:s");//$request->jam;
$penjualan->id_pelanggan = $request->id_pelanggan;
$penjualan->alamat_pengiriman = $request->alamat_pengiriman;
$penjualan->tagihan =0;// $request->tagihan;
$ongkir=getOngkir($wilayah);
$penjualan->ongkir =$ongkir;// $request->ongkir;
$penjualan->total = $ongkir;//$request->total;
$penjualan->status = $request->status;
$penjualan->keterangan = $request->keterangan;
$penjualan->wilayah = $wilayah;
$penjualan->save();
return redirect('/penjualan')->with('pesan', 'Data Penjualan berhasil disimpan');
}
public function _storeOrder(Request $request){
if(session()->has('IDORDER')){
$id_penjualan= Session::get('IDORDER');
$jdetail = New Jdetail;
$id_barang=$request->id_barang;
$jumlah = $request->jumlah;
$barang = Barang::find($id_barang);
$harga=$barang->harga;
$nama_barang=$barang->nama_barang;
$jdetail->id_penjualan = $id_penjualan;
$jdetail->id_barang = $id_barang;
$jdetail->jumlah =$jumlah;
$jdetail->harga = $harga;
$jdetail->subtotal =$jumlah * $harga;
$jdetail->catatan = $request->catatan;
$jdetail->save();
$barang->stok = $barang->stok-$jumlah;
$barang->update();
$penjualan = Penjualan::find($id_penjualan);
$total=getTotal($id_penjualan);
$penjualan->tagihan =$total;
$penjualan->total =$penjualan->ongkir+$total;
$penjualan->update();
return redirect('/jdetail/'.$id_penjualan)->with('pesan', "Data $nama_barang berhasil disimpan");
}
else{
$penjualan = New Penjualan;
$wilayah=$request->wilayah;
$penjualan->tanggal = date("Y-m-d");//$request->tanggal;
$penjualan->jam = date("H:i:s");//$request->jam;
$penjualan->id_pelanggan = $id_pelanggan=Auth::user()->rkey ;
$penjualan->alamat_pengiriman = $request->alamat_pengiriman;
$penjualan->tagihan =0;
$ongkir=getOngkir($wilayah);
$penjualan->ongkir =$ongkir;// $request->ongkir;
$penjualan->total = $ongkir;//$request->total;
$penjualan->status = "Order";
$penjualan->keterangan = "-";
$penjualan->wilayah = $wilayah;
$penjualan->save();
$ARR = Penjualan::all()->last();
$value=$ARR->id_penjualan;
session(['IDORDER' => $value]);
$id_penjualan=$value;
//return redirect('/_penjualan')->with('pesan', 'Data Penjualan berhasil disimpan');
$jdetail = New Jdetail;
$id_barang=$request->id_barang;
$jumlah = $request->jumlah;
$barang = Barang::find($id_barang);
$harga=$barang->harga;
$nama_barang=$barang->nama_barang;
$jdetail->id_penjualan = $id_penjualan;
$jdetail->id_barang = $id_barang;
$jdetail->jumlah =$jumlah;
$jdetail->harga = $harga;
$jdetail->subtotal =$jumlah * $harga;
$jdetail->catatan = $request->catatan;
$jdetail->save();
$barang->stok = $barang->stok-$jumlah;
$barang->update();
$penjualan = Penjualan::find($id_penjualan);
$total=getTotal($id_penjualan);
$penjualan->tagihan =$total;
$penjualan->total =$penjualan->ongkir+$total;
$penjualan->update();
return redirect('/jdetail/'.$id_penjualan)->with('pesan', "Data $nama_barang berhasil disimpan");
}//else
}
public function _store(Request $request){
$penjualan = New Penjualan;
$wilayah=$request->wilayah;
$penjualan->tanggal = date("Y-m-d");//$request->tanggal;
$penjualan->jam = date("H:i:s");//$request->jam;
$penjualan->id_pelanggan = $id_pelanggan=Auth::user()->rkey ;
$penjualan->alamat_pengiriman = $request->alamat_pengiriman;
$penjualan->tagihan =0;
$ongkir=getOngkir($wilayah);
$penjualan->ongkir =$ongkir;// $request->ongkir;
$penjualan->total = $ongkir;//$request->total;
$penjualan->status = "Order";
$penjualan->keterangan = "-";
$penjualan->wilayah = $wilayah;
$penjualan->save();
return redirect('/_penjualan')->with('pesan', 'Data Penjualan berhasil disimpan');
}
public function edit($id_penjualan){
$DaftarPelanggan = Pelanggan::all();
$penjualan = Penjualan::find($id_penjualan);
return view('admin.penjualan.edit', compact('penjualan','DaftarPelanggan'));
}
public function _edit($id_penjualan){
$DaftarPelanggan = Pelanggan::all();
$penjualan = Penjualan::find($id_penjualan);
return view('admin.penjualan._edit', compact('penjualan','DaftarPelanggan'));
}
public function update(Request $request, $id){
$penjualan = Penjualan::find($id);
$penjualan->id_pelanggan = $request->id_pelanggan;
$penjualan->alamat_pengiriman = $request->alamat_pengiriman;
$wilayah=$request->wilayah;
$ongkir=getOngkir($wilayah);
$penjualan->ongkir =$ongkir;
$penjualan->total = $penjualan->tagihan+$ongkir;
$penjualan->wilayah = $wilayah;
$penjualan->status = $request->status;
$penjualan->keterangan = $request->keterangan;
$penjualan->update();
return redirect('/penjualan')->with('pesan','Data Penjualan berhasil di update');
}
public function _update(Request $request, $id){
$penjualan = Penjualan::find($id);
$penjualan->alamat_pengiriman = $request->alamat_pengiriman;
$wilayah=$request->wilayah;
$ongkir=getOngkir($wilayah);
$penjualan->ongkir =$ongkir;
$penjualan->total = $penjualan->tagihan+$ongkir;
$penjualan->wilayah = $wilayah;
$penjualan->keterangan = $request->keterangan;
$penjualan->update();
return redirect('/_penjualan')->with('pesan','Data Penjualan berhasil di update');
}
public function destroy($id){
$penjualan = Penjualan::find($id);
$penjualan->delete();
$ada=0;
$jdetail = \App\Jdetail::all()->where('id_penjualan',$id);
foreach($jdetail as $i){
$pk= $i->idj;
$Detail = \App\Jdetail::find($pk);
$id_barang= $i->id_barang;
$jumlahYgLama= $i->jumlah;
$barang = \App\Barang::find($id_barang);
$barang->stok = $barang->stok+$jumlahYgLama;
$barang->update();
$ada++;
$Detail->delete();
}
return redirect('/penjualan')->with('pesan', "Data Penjualan berikut $ada Transaksi Detailnya Berhasil di Hapus");
}
}