<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Carbon\Carbon;

class CodeGenerator extends Controller
{
    public static function code($table, $field, $lebar = 0, $awalan)
    {
        //        CodeGenerator::code('m_company', 'c_id', 7, 'MB');
        $code = DB::table($table)->select($field)->orderBy($field, 'desc')->limit(1);
        $countData = $code->count();
        if ($countData == 0) {
            $nomor = 1;
        } else {
            $getData = $code->get();
            $row = array();
            foreach ($getData as $value) {
                $row = array($value->$field);
            }
            $nomor = intval(substr($row[0], strlen($awalan))) + 1;
        }

        if ($lebar > 0) {
            $angka = $awalan . str_pad($nomor, $lebar, "0", STR_PAD_LEFT);
        } else {
            $angka = $awalan . $nomor;
        }

        return $angka;
    }

    public static function codeWithSeparator($table, $field, $mulai = 0, $panjang = 0, $lebar = 0, $awalan, $separator, $version=1)
    {
        // mulai: index (start with '1') after awalan and separator (date-start) ex: PC-001/01/01/2019 so mulai = 8
        // panjang: date-length (default with 10)
        // lebar: max digits of incrementing number
        // CodeGenerator::codeWithSeparator('m_company', 'c_id', 8, 10, 3, 'MB', '-');
        if ($version == 1) {
            $code = DB::table($table)->where(DB::raw('substr(' . $field . ', ' . $mulai . ', ' . $panjang . ')'), '=', Carbon::now('Asia/Jakarta')->format('d/m/Y'));

            $countData = $code->count();

            $nomor = $countData + 1;


            if ($lebar > 0) {
                $angka = $awalan . $separator . str_pad($nomor, $lebar, "0", STR_PAD_LEFT) . '/' . Carbon::now('Asia/Jakarta')->format('d/m/Y');
            } else {
                $angka = $awalan . $separator . $nomor . '/' . Carbon::now('Asia/Jakarta')->format('d/m/Y');
            }
        } else if ($version == 2) {
            // ex : UH2020010001
            $code = DB::table($table)->where(DB::raw('substr(' . $field . ', ' . $mulai . ', ' . $panjang . ')'), '=', Carbon::now('Asia/Jakarta')->format('ym'));

            $countData = $code->count();

            $nomor = $countData + 1;


            if ($lebar > 0) {
                $angka = $awalan . $separator . Carbon::now('Asia/Jakarta')->format('ym') . str_pad($nomor, $lebar, "0", STR_PAD_LEFT);
            } else {
                $angka = $awalan . $separator . Carbon::now('Asia/Jakarta')->format('ym') . $nomor;
            }
        }

        return $angka;
    }

    public static function numberOfTransaction($table, $field, $mulai = 1, $panjang = 1, $lebar = 0, $awalan, $transaction_of = null, $tgl_jurnal = null)
	{
        if ($transaction_of == null) {
            $date = date('d m Y');
            $month = explode(" ", $date);
            $number = DB::table($table)
                        ->select($field)
                        ->whereMonth('created_at', $month[1])
                        ->whereYear('created_at', $month[2])
                        ->where(DB::raw('substr(' . $field . ', ' . $mulai . ', ' . $panjang . ')'), '=', $awalan)
                        ->orderBy($field, 'desc')
                        ->limit(1);
            $countData = $number->count();
            if ($countData == 0) {
                $nomor = 1;
            } else {
                $getData = $number->get();
                $row = array();
                foreach ($getData as $value) {
                    $row = array($value->$field);
                }
                $nomor = intval(substr($row[0], strlen($awalan))) + 1;
            }

            if ($lebar > 0) {
                $angka = $awalan . str_pad($nomor, $lebar, "0", STR_PAD_LEFT);
            } else {
                $angka = $awalan . $nomor;
            }
        } else if ($transaction_of == "kas_bon") {
            $date = date('Y-m-d');
            $explode_date = explode("-", $date);

            $code = DB::table($table)->where(DB::raw('substr(' . $field . ', ' . $mulai . ', ' . $panjang . ')'), '=', substr($explode_date[0], -2).$explode_date[1]);

            $countData = $code->count();

            if ($countData == 0) {
                $nomor = $countData + 1;
            } else {
                $no_trans = DB::table($table)->where(DB::raw('substr(' . $field . ', ' . $mulai . ', ' . $panjang . ')'), '=', substr($explode_date[0], -2).$explode_date[1])->orderBy($field, 'desc')->limit(1)->first()->$field;
                $last_number = substr($no_trans, 6);
                $nomor = (int)$last_number + 1;
            }

            if ($lebar > 0) {
                $angka = $awalan . substr($explode_date[0], -2) . $explode_date[1] . str_pad($nomor, $lebar, "0", STR_PAD_LEFT);
            } else {
                $angka = $awalan . substr($explode_date[0], -2) . $explode_date[1] . $nomor;
            }
        } else if ($transaction_of == "JPP" || $transaction_of == "JPS") {
            // ex:JPP2002001
            $date = $tgl_jurnal;
            $explode_date = explode("-", $date);

            $code = DB::table($table)->where(DB::raw('substr(' . $field . ', ' . $mulai . ', ' . $panjang . ')'), '=', substr($explode_date[0], -2).$explode_date[1]);

            $countData = $code->count();

            if ($countData == 0) {
                $nomor = $countData + 1;
            } else {
                $no_trans = DB::table($table)->where(DB::raw('substr(' . $field . ', ' . $mulai . ', ' . $panjang . ')'), '=', substr($explode_date[0], -2).$explode_date[1])->orderBy($field, 'desc')->limit(1)->first()->$field;
                $last_number = substr($no_trans, 7);
                $nomor = (int)$last_number + 1;
            }

            if ($lebar > 0) {
                $angka = $awalan . substr($explode_date[0], -2) . $explode_date[1] . str_pad($nomor, $lebar, "0", STR_PAD_LEFT);
            } else {
                $angka = $awalan . substr($explode_date[0], -2) . $explode_date[1] . $nomor;
            }
        }

        return $angka;
	}
}
