Files
simrs-jatim/webservice/farmasi/app/Http/Controllers/PenjualanController.php
2024-04-19 14:04:41 +07:00

239 lines
8.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use DB;
use App\Depo;
use App\LogJual;
use App\LogJualDetil;
use App\LogMutasiBarang;
use App\TMPasien;
use Illuminate\Http\Request;
use Auth;
class PenjualanController extends Controller
{
public function showJual()
{
return response()->json(LogJual::select('NoJual','TglJual','TMOrgDepo.Nama as nama_upf','NamaPx','AlamatPx')
->join('TMOrgDepo','TMOrgDepo.Kode','TTLogJual.KodeDepo')
->whereRaw('CONVERT(VARCHAR(10), TglJual, 103) = \''.date('d/m/Y').'\'')
->get());
}
public function showJualDetil($nojual)
{
if(preg_match("/-/", $nojual))
{
$nojual = preg_replace("/-/", ' ', $nojual);
}
$obj_query = LogJual::select('NoJual','TMOrgDepo.Nama as nama_upf','NoRMPx','NoBilling','NamaPx','AlamatPx','TMLogBarang.Nama as namaBarang','TTLogJualD.Jumlah','TTLogJualD.Harga','TMPetugas.Nama as nama_petugas','jaminan.Nama as nama_penjamin','TotJual')
->selectRaw('CONCAT(CONVERT(VARCHAR(10), TglJual, 103),\' \',CONVERT(VARCHAR(10), TglJual, 108)) as TglJual')
->selectRaw('(SELECT SUM(Harga*Jumlah) from TTLogJualD as d2 where d2.KodeJual = TTLogJual.Kode GROUP BY d2.KodeJual) as TotDetil')
->selectRaw('(TotJual-(SELECT SUM(Harga*Jumlah) from TTLogJualD as d2 where d2.KodeJual = TTLogJual.Kode GROUP BY d2.KodeJual)) as Pembulatan')
->join('TMOrgDepo','TMOrgDepo.Kode','TTLogJual.KodeDepo')
->join('TTLogJualD','TTLogJual.Kode','TTLogJualD.KodeJual')
->join('TMLogBarang','TTLogJualD.KodeBarang','TMLogBarang.Kode')
->join('TMPetugas','TTLogJual.IdPetugas','TMPetugas.Kode')
->leftJoin('TMLogReference as jaminan','TTLogJual.KodeJamin','jaminan.Kode')
->where('NoJual','=',$nojual)
->get();
return response()->json($obj_query);
}
public function showJualFiltered($search)
{
return response()->json(LogJual::select('NoJual','TglJual','TMOrgDepo.Nama as nama_upf','NamaPx','AlamatPx')
->join('TMOrgDepo','TMOrgDepo.Kode','TTLogJual.KodeDepo')
->whereRaw('CONVERT(VARCHAR(10), TglJual, 103) = \''.date('d/m/Y').'\' AND (TMOrgDepo.Nama like \'%'.$search.'%\' OR NamaPx like \'%'.$search.'%\' OR AlamatPx like \'%'.$search.'%\')')
->get());
}
public function showJualFilteredDate($tgl)
{
$tgl = preg_replace("/-/","/",$tgl);
return response()->json(LogJual::select('NoJual','TglJual','TMOrgDepo.Nama as nama_upf','NamaPx','AlamatPx')
->join('TMOrgDepo','TMOrgDepo.Kode','TTLogJual.KodeDepo')
->whereRaw('CONVERT(VARCHAR(10), TglJual, 103) = \''.$tgl.'\'')
->get());
}
public function showTotalJual()
{
return response()->json(LogJual::selectRaw('count(NoJual) as jumlah')
->join('TMOrgDepo','TMOrgDepo.Kode','TTLogJual.KodeDepo')
->whereRaw('CONVERT(VARCHAR(10), TglJual, 103) = \''.date('d/m/Y').'\'')
->get());
}
public function showDetil($id)
{
return response()->json(LogJualDetil::where('KodeJual',$id)->get());
}
private function insert_loop(Request $request)
{
$arr_input = (array) $request->input('arr_input');
$totalJual = 0;
foreach($arr_input as $detil){
if($detil['Jumlah'] == '' ) { continue; }
$totalJual += ($detil['Harga']*$detil['Jumlah']);
}
if($totalJual == 0)
{
return response()->json(['status'=>'error','message'=>'Data barang kosong.'],200);
}
$input = (array) $request->input('input');
// get kode jual
$tmp_jual = LogJual::where('Kode','like',$input['KodeDepo'].date('Ym').'%')->orderBy('Kode','desc')->first();
if(!empty($tmp_jual)){
$kd_jual = str_replace($input['KodeDepo'], "", $tmp_jual->Kode);
$kd_jual = substr($kd_jual,6);
$kd_jual += 1;
$kd_jual = date('Ym').str_pad($kd_jual, 6, "0", STR_PAD_LEFT);
}
else {
$kd_jual = date('Ym').'000001';
}
// get no jual depo
$findDepo = Depo::find($input['KodeDepo']);
$tmp_jual2 = LogJual::where('NoJual','like',$findDepo->Inisial.date('Ym').'%')->orderBy('NoJual','desc')->first();
if(!empty($tmp_jual2)){
$no_jual = str_replace($findDepo->Inisial, "", $tmp_jual2->NoJual);
$no_jual = substr($no_jual,6);
$no_jual += 1;
$no_jual = date('Ym').str_pad($no_jual, 6, "0", STR_PAD_LEFT);
}
else {
$no_jual = date('Ym').'000001';
}
$input['Kode'] = $input['KodeDepo'].$kd_jual;
$input['NoJual'] = $findDepo->Inisial.$no_jual;
$input['TglJual'] = date('Y-m-d H:i:s');
$input['NoMinta'] = $input['KodeDepo'].$kd_jual;
$input['Aktif'] = 1;
$input['StCetak'] = 1;
$input['StJual'] = 1;
$input['TotJual'] = $totalJual;
return $input;
}
public function create(Request $request)
{
$inputvar = (array) $request->input('input');
$arr_input = (array) $request->input('arr_input');
// generate data to insert with 3 times trying
// DB::enableQueryLog();
$is_success = 0;
for( $i = 0; $i < 3; $i++) {
if($is_success == 0) {
$input = $this->insert_loop($request);
$LogJual = LogJual::firstOrNew($input);
if(!$LogJual->save()) {
$is_success = 0;
}
else {
$is_success = 1;
}
}
else {
break;
}
}
// return response()->json(DB::getQueryLog());exit;
$LogJualD = $LogMutasi = [];
if($is_success)
{
$i = 1;
foreach($arr_input as $detil)
{
if($detil['Jumlah'] == '') continue;
$detil['Kode'] = $input['Kode'].date('Ym').str_pad($i, 4, '0', STR_PAD_LEFT);
$detil['KodeJual'] = $input['Kode'];
$detil['StStock'] = $detil['StStock']*1;
$detil['StRacik'] = $detil['StRacik']*1;
$detil['Frek'] = $detil['Frek']*1;
$detil['JFrek'] = $detil['JFrek']*1;
$detil['Jumlah'] = $detil['Jumlah']*1;
$detil['Harga'] = $detil['Harga']*1;
$detil['HargaRetur'] = $detil['Harga'];
$detil['HPP'] = $detil['HPP']*1;
$detil['SP'] = $detil['SP']*1;
$detil['SS'] = $detil['SS']*1;
$detil['SSR'] = $detil['SSR']*1;
$detil['SM'] = $detil['SM']*1;
$detil['PenG'] = $detil['PenG']*1;
$detil['PenF'] = $detil['PenF']*1;
unset($detil['subtotal']);
$LogJualDetil = LogJualDetil::create($detil);
$LogMutasiBarang = LogMutasiBarang::where([
['KodePeriode',date('Ym')],
['KodeDepo',$input['KodeDepo']],
['KodeBarang',$detil['KodeBarang']],
['StStock',$detil['StStock']]
])
->update([
'Keluar' => DB::raw('(Keluar+'.$detil['Jumlah'].')'),
'StockAkhir' => DB::raw('(StockAkhir-'.$detil['Jumlah'].')')
]);
$LogJualD[] = $LogJualDetil;
$LogMutasi[] = $LogMutasiBarang;
$i++;
}
}
// return response()->json(DB::getQueryLog());exit;
if($is_success) {
// create pasien
$arr_pasien['NoRM'] = $inputvar['NoRMPx'];
$arr_pasien['Nama'] = $inputvar['NamaPx'];
$arr_pasien['JKL'] = NULL;
$arr_pasien['Alamat'] = '';
$arr_pasien['TglLahir'] = $inputvar['TglLahir'];
$savePasien = TMPasien::updateOrCreate($arr_pasien);
return response()->json([
'status'=>'sukses',
'message'=>'Data penjualan obat telah disimpan.',
'main'=>$LogJual,
'detil'=>$LogJualD,
'mutasi'=>$LogMutasi
], 201);
}
else {
return response()->json([
'status'=>'error',
'message'=>'Data penjualan obat gagal disimpan.'
], 400);
}
}
public function update($id, Request $request)
{
$author = Barang::findOrFail($id);
$author->update($request->all());
return response()->json($author, 200);
}
public function delete($id)
{
Barang::findOrFail($id)->delete();
return response('Deleted Successfully', 200);
}
}