public string AutoGenerateOrder()
        {
            string id = "";

            try
            {
                using (var context = new smlpobDB())
                {
                    var data = (from x in context.Ordermastertbl
                                .OrderByDescending(x => x.OrderNo)
                                select x.OrderNo).FirstOrDefault();

                    if (data != null)
                    {
                        string[] strSplit = data.Split('/');
                        int      noUrut   = int.Parse(strSplit[0]);
                        string   instansi = strSplit[1];
                        int      thn      = int.Parse(strSplit[3]);
                        int      thnNow   = int.Parse(DatetimeHelper.GetYearNow());

                        if (thnNow > thn)
                        {
                            id = "0001/LPBalittanah/" + DatetimeHelper.GetMonthNow() + "/" +
                                 DatetimeHelper.GetYearNow();
                        }
                        else
                        {
                            string newNoUrut = (noUrut + 1).ToString("0000");
                            id = newNoUrut + "/" + instansi + "/" + DatetimeHelper.GetMonthNow() + "/" +
                                 DatetimeHelper.GetYearNow();
                        }
                    }
                    else
                    {
                        id = "0001/LPBalittanah/" + DatetimeHelper.GetMonthNow() + "/" +
                             DatetimeHelper.GetYearNow();
                    }

                    return(id);
                }
            }
            catch (Exception ex)
            {
                LogHelpers.source  = this.GetType().Name;
                LogHelpers.message = ex.Message;
                LogHelpers.user    = currentUser;
                LogHelpers.WriteLog();
            }
            return(null);
        }
        public string AutoGenerateSample(string komoditas, string analisis)
        {
            try
            {
                int    no   = int.Parse(komoditas);
                string cmdt = "";
                using (var context = new smlpobDB())
                {
                    var kmd = (from x in context.Comoditytbl
                               where x.ComodityNo == no
                               select x.ComodityName).AsNoTracking().SingleOrDefault();
                    cmdt = kmd;
                }

                string id            = "";
                string kodeKomoditas = "";
                string kodeAnalisis  = "";

                // kode komoditas
                if (cmdt.Contains("Pupuk Organik"))
                {
                    kodeKomoditas = "Po.";
                }
                else if (cmdt.Contains("Pupuk Anorganik"))
                {
                    kodeKomoditas = "Pa.";
                }
                else if (cmdt.Contains("Tanah"))
                {
                    kodeKomoditas = "Th.";
                }
                else if (cmdt.Contains("Air"))
                {
                    kodeKomoditas = "A.";
                }
                else if (cmdt.Contains("Tanaman"))
                {
                    kodeKomoditas = "Tn.";
                }

                // kode analisis
                if (analisis.Contains("Kimia"))
                {
                    kodeAnalisis = "K.";
                }
                else if (analisis.Contains("Biologi"))
                {
                    kodeAnalisis = "B.";
                }
                else if (analisis.Contains("Fisika"))
                {
                    kodeAnalisis = "F.";
                }

                using (var context = new smlpobDB())
                {
                    var data = (from x in context.Ordersampletbl
                                .OrderByDescending(x => x.NoBalittanah)
                                where x.NoBalittanah.Contains(kodeKomoditas) &&
                                x.NoBalittanah.Contains(kodeAnalisis)
                                select x.NoBalittanah).AsNoTracking().SingleOrDefault();

                    if (data != null)
                    {
                        string[] strSplit   = data.Split('.');
                        int      noUrut1    = int.Parse(strSplit[2]);
                        int      noUrut2    = int.Parse(strSplit[5]);
                        string   newNoUrut1 = (noUrut1 + 1).ToString();
                        string   newNoUrut2 = (noUrut2 + 1).ToString();

                        id = DatetimeHelper.GetYearNow() + "." + DatetimeHelper.GetMonthNow() + "." + newNoUrut1 +
                             "." + kodeAnalisis + kodeKomoditas + newNoUrut2;
                    }
                    else
                    {
                        id = DatetimeHelper.GetYearNow() + "." + DatetimeHelper.GetMonthNow() + "." + "1" +
                             "." + kodeAnalisis + kodeKomoditas + "1";
                    }

                    var sample = new Ordersampletbl
                    {
                        NoBalittanah = id,
                        SampleCode   = "",
                    };

                    context.Ordersampletbl.Add(sample);
                    context.SaveChangesAsync();

                    return(id);
                }
            }
            catch (Exception ex)
            {
                LogHelpers.source  = this.GetType().Name;
                LogHelpers.message = ex.Message;
                LogHelpers.user    = currentUser;
                LogHelpers.WriteLog();
            }

            return(null);
        }