public ActionResult Add(SalesOrderKonsolidasi model, string btnsave)
        {
            if (ModelState.IsValid)
            {
                //cek min max
                bool isPalid = true;
                Context.DaftarHargaKonsolidasi     dbDh     = RepoDHKonsolidasi.FindByItemId(model.RuteId.Value);
                Context.DaftarHargaKonsolidasiItem dbDhItem = dbDh.DaftarHargaKonsolidasiItem.Where(d => d.Id == model.RuteId.Value).FirstOrDefault();
                if (!isPalid)
                {
                    return(View("Form", model));
                }

                Context.SalesOrder            dbso   = new Context.SalesOrder();
                Context.SalesOrderKonsolidasi dbitem = new Context.SalesOrderKonsolidasi();
                model.setDb(dbitem);
                dbitem.Urutan              = RepoSalesOrder.getUrutanKonsolidasi(model.TanggalMasuk.Value) + 1;
                dbitem.SONumber            = RepoSalesOrder.generateKonsolidasi(model.TanggalMasuk.Value, dbitem.Urutan);
                dbitem.DN                  = "DN" + dbitem.SONumber;
                dbso.SalesOrderKonsolidasi = dbitem;

                if (btnsave == "save")
                {
                    dbso.Status = "save";
                }
                else if (btnsave == "draft")
                {
                    dbso.Status = "draft";
                }
                else
                {
                    dbso.Status = model.Status;
                }

                RepoSalesOrder.save(dbso);

                return(RedirectToAction("Index"));
            }
            return(View("Form", model));
        }
        public ActionResult Edit(SalesOrderKonsolidasi model, string btnsave)
        {
            if (ModelState.IsValid)
            {
                //cek min max
                bool isPalid = true;
                Context.DaftarHargaKonsolidasi     dbDh     = RepoDHKonsolidasi.FindByItemId(model.RuteId.Value);
                Context.DaftarHargaKonsolidasiItem dbDhItem = dbDh.DaftarHargaKonsolidasiItem.Where(d => d.Id == model.RuteId.Value).FirstOrDefault();
                if (
                    (dbDhItem.LookupCodeSatuan.Nama == model.PerhitunganDasar) || (model.PerhitunganDasar == "Tonase" && (dbDhItem.LookupCodeSatuan.Nama == "Ton" || dbDhItem.LookupCodeSatuan.Nama == "Kg"))
                    )
                {
                    if (model.PerhitunganDasar == "Tonase")
                    {
                        if (!((model.Tonase >= dbDhItem.MinKg) && (model.Tonase <= dbDhItem.MaxKg)))
                        {
                            isPalid = false;
                            ModelState.AddModelError("Tonase", "Nilai harus lebih besar dari " + dbDhItem.MinKg.ToString() + " dan lebih kecil dari " + dbDhItem.MaxKg.ToString());
                        }
                    }
                    else if (model.PerhitunganDasar == "Karton")
                    {
                        if (!((model.karton >= dbDhItem.MinKg) && (model.karton <= dbDhItem.MaxKg)))
                        {
                            isPalid = false;
                            ModelState.AddModelError("karton", "Nilai harus lebih besar dari " + dbDhItem.MinKg.ToString() + " dan lebih kecil dari " + dbDhItem.MaxKg.ToString());
                        }
                    }
                    else if (model.PerhitunganDasar == "Pallet")
                    {
                        if (!((model.Pallet >= dbDhItem.MinKg) && (model.Pallet <= dbDhItem.MaxKg)))
                        {
                            isPalid = false;
                            ModelState.AddModelError("Pallet", "Nilai harus lebih besar dari " + dbDhItem.MinKg.ToString() + " dan lebih kecil dari " + dbDhItem.MaxKg.ToString());
                        }
                    }
                    else if (model.PerhitunganDasar == "Container")
                    {
                        if (!((model.Container >= dbDhItem.MinKg) && (model.Container <= dbDhItem.MaxKg)))
                        {
                            isPalid = false;
                            ModelState.AddModelError("Container", "Nilai harus lebih besar dari " + dbDhItem.MinKg.ToString() + " dan lebih kecil dari " + dbDhItem.MaxKg.ToString());
                        }
                    }
                    else if (model.PerhitunganDasar == "m3")
                    {
                        if (!((model.m3 >= dbDhItem.MinKg) && (model.m3 <= dbDhItem.MaxKg)))
                        {
                            isPalid = false;
                            ModelState.AddModelError("m3", "Nilai harus lebih besar dari " + dbDhItem.MinKg.ToString() + " dan lebih kecil dari " + dbDhItem.MaxKg.ToString());
                        }
                    }
                }
                if (!isPalid)
                {
                    return(View("Form", model));
                }

                Context.SalesOrder dbitem = RepoSalesOrder.FindByPK(model.SalesOrderId.Value);

                model.setDb(dbitem.SalesOrderKonsolidasi);

                if (btnsave == "save")
                {
                    dbitem.Status = "save";
                }
                else if (btnsave == "draft")
                {
                    dbitem.Status = "draft";
                }
                else
                {
                    dbitem.Status = model.Status;
                }

                RepoSalesOrder.save(dbitem);


                return(RedirectToAction("Index"));
            }

            return(View("Form", model));
        }