Beispiel #1
0
        public ActionResult Add(SalesOrderPickup model, string btnsave)
        {
            SalesOrderLoadUnload[] resultLoad = JsonConvert.DeserializeObject <SalesOrderLoadUnload[]>(model.StrLoad);
            model.ListLoad = resultLoad.ToList();
            SalesOrderLoadUnload[] resultUnload = JsonConvert.DeserializeObject <SalesOrderLoadUnload[]>(model.StrUnload);
            model.ListUnload = resultUnload.ToList();
            if (ModelState.IsValid)
            {
                Context.SalesOrder       dbso   = new Context.SalesOrder();
                Context.SalesOrderPickup dbitem = new Context.SalesOrderPickup();
                model.setDb(dbitem);
                dbitem.Urutan         = RepoSalesOrder.getUrutanPickup(model.TanggalPickup.Value) + 1;
                dbitem.SONumber       = RepoSalesOrder.generatePickup(model.TanggalPickup.Value, dbitem.Urutan);
                dbso.SalesOrderPickup = 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(RevisiKeterangan model)
        {
            var query = "";

            Context.SalesOrder dbso = RepoSalesOrder.FindByPK(model.IdSalesOrder.Value);
            if (ModelState.IsValid)
            {
                if (dbso.SalesOrderOncallId.HasValue)
                {
                    dbso.SalesOrderOncall.Keterangan = model.KeteranganRevisi;
                }
                else if (dbso.SalesOrderPickupId.HasValue)
                {
                    dbso.SalesOrderPickup.Keterangan = model.KeteranganRevisi;
                    query += "UPDATE dbo.\"SalesOrderPickup\" SET \"Keterangan\" = " + model.KeteranganRevisi + " WHERE \"SalesOrderPickupId\" = " + dbso.SalesOrderPickupId + ";";
                }

                RepoSalesOrder.save(dbso);
                RepoAuditrail.SetAuditTrail(query, "List Order", "Revisi Keterangan", UserPrincipal.id);

                return(RedirectToAction("Index", "ListOrder"));
            }

            return(View("Form", model));
        }
        public ActionResult Edit(SalesOrderKontrak model, string btnsave)
        {
            Context.SalesOrder dbitem = RepoSalesOrder.FindByPK(model.SalesOrderId.Value);
            if (btnsave != null && btnsave != "")
            {
                dbitem.Status = btnsave;
            }
            else
            {
                dbitem.Status = model.Status;
            }

            model.setDbOpertional(dbitem.SalesOrderKontrak);

            #region list kontrak SO
            dbitem.SalesOrderKontrak.SalesOrderKontrakListSo.Clear();

            int idx = 1;
            for (int j = 1; j <= (dbitem.SalesOrderKontrak.Rit); j++)
            {
                foreach (var dateItem in dbitem.SalesOrderKontrak.SalesOrderKontrakDetail)
                {
                    int trukIdx = 1;
                    foreach (var trukItem in dbitem.SalesOrderKontrak.SalesOrderKontrakTruck)
                    {
                        Context.SalesOrderKontrakListSo dblist = new Context.SalesOrderKontrakListSo();
                        dblist.SalesKontrakId = dbitem.SalesOrderKontrakId;
                        dblist.NoSo           = RepoSalesOrderKontrakListSo.generateCodeListSo(dbitem.SalesOrderKontrak.SONumber, dateItem.MuatDate, j, trukIdx, dbitem.SalesOrderKontrak.Urutan);
                        dblist.MuatDate       = dateItem.MuatDate;
                        dblist.IdDataTruck    = trukItem.DataTruckId;
                        dblist.Driver1Id      = trukItem.IdDriver1;
                        dblist.Driver2Id      = trukItem.IdDriver2;
                        RepoSalesOrderKontrakListSo.OnlyAdd(dblist);
                        idx++;
                        trukIdx++;
                    }
                }
            }
            #endregion list kontrak SO

            RepoSalesOrder.save(dbitem);
            return(RedirectToAction("Index"));
        }
        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(Removal model)
        {
            Context.SalesOrder     dbso         = RepoSalesOrder.FindByPK(model.IdSo.Value);
            SalesOrderLoadUnload[] resultUnload = new SalesOrderLoadUnload[0];
            if (model.StrUnload != null && model.StrUnload != "")
            {
                resultUnload     = JsonConvert.DeserializeObject <SalesOrderLoadUnload[]>(model.StrUnload);
                model.ListUnload = resultUnload.ToList();
            }

            if (ModelState.IsValid)
            {
                //rubah data so na
                if (dbso.SalesOrderOncallId.HasValue)
                {
                    dbso.SalesOrderOncall.IdDaftarHargaItem  = model.IdRute;
                    dbso.SalesOrderOncall.StrDaftarHargaItem = model.StrRute;
                    dbso.SalesOrderOncall.SalesOrderOnCallUnLoadingAdd.Clear();
                    foreach (var item in resultUnload)
                    {
                        dbso.SalesOrderOncall.SalesOrderOnCallUnLoadingAdd.Add(new Context.SalesOrderOnCallUnLoadingAdd()
                        {
                            CustomerId = dbso.SalesOrderOncall.CustomerId,
                            CustomerUnloadingAddressId = item.Id,
                            urutan   = item.urutan,
                            IsSelect = item.IsSelect
                        });
                    }
                }
                else if (dbso.SalesOrderPickupId.HasValue)
                {
                    dbso.SalesOrderPickup.RuteId = model.IdRute;
                    dbso.SalesOrderPickup.SalesOrderPickupUnLoadingAdd.Clear();
                    RepoAuditrail.saveDelAllSalesOrderPickupUnLoadingAddQuery(dbso.SalesOrderPickup, UserPrincipal.id);
                    foreach (var item in resultUnload)
                    {
                        dbso.SalesOrderOncall.SalesOrderOnCallUnLoadingAdd.Add(new Context.SalesOrderOnCallUnLoadingAdd()
                        {
                            CustomerId = dbso.SalesOrderOncall.CustomerId,
                            CustomerUnloadingAddressId = item.Id,
                            urutan   = item.urutan,
                            IsSelect = item.IsSelect
                        });
                    }
                }
                else if (dbso.SalesOrderProsesKonsolidasiId.HasValue)
                {
                    dbso.SalesOrderProsesKonsolidasi.IdDaftarHargaItem  = model.IdRute;
                    dbso.SalesOrderProsesKonsolidasi.StrDaftarHargaItem = model.StrRute;
                }

                dbso.AdminUangJalan.Removal.Add(new Context.Removal()
                {
                    StatusTagihan     = model.StatusTagihan,
                    TanggalRemoval    = model.TanggalRemoval,
                    JamRemoval        = model.JamRemoval,
                    KeteranganRemoval = model.Keterangan,
                    IdDriver1         = dbso.AdminUangJalan.IdDriver1,
                    IdDriver2         = dbso.AdminUangJalan.IdDriver2,
                    IdSO = dbso.Id
                });
                RepoSalesOrder.save(dbso);
                RepoAuditrail.saveUpdSalesOrderProsesKonsolidasiQuery(dbso.SalesOrderProsesKonsolidasi, UserPrincipal.id);
                return(RedirectToAction("Index", "ListOrder"));
            }

            if (dbso.SalesOrderOncallId.HasValue)
            {
                model.ModelOncall = new SalesOrderOncall(dbso);
                return(View("FormOncall", model));
            }
            else if (dbso.SalesOrderPickupId.HasValue)
            {
                model.ModelPickup = new SalesOrderPickup(dbso);
                return(View("FormPickup", model));
            }
            else if (dbso.SalesOrderProsesKonsolidasiId.HasValue)
            {
                model.ModelKonsolidasi = new SalesOrderProsesKonsolidasi(dbso);
                return(View("FormKonsolidasi", model));
            }

            return(View(""));
        }
        public ActionResult Add(SalesOrderKontrak model, string btnsave)
        {
            //validasi tambahan
            bool palid = true;

            if (btnsave == null && model.Status == "save")
            {
                List <Context.DaftarHargaKontrak> dummy = RepoDaftarHarga.FindAll().Where(d =>
                                                                                          d.IdCust == model.CustomerId &&
                                                                                          d.DaftarHargaKontrakItem.Any(i => i.IdJenisTruck == model.JenisTruckId)).ToList();

                if (dummy.Count() < 1)
                {
                    palid            = false;
                    ViewBag.errorMsg = "Tidak terdapat daftar harga untuk jenis truck " + RepoJenisTruck.FindByPK(model.JenisTruckId.Value).StrJenisTruck;
                }
                else
                {
                    if (model.JsonDateMuat != null && model.JsonDateMuat != "")
                    {
                        List <string> wadah = new List <string>();
                        foreach (string item in model.JsonDateMuat.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            DateTime valDate = DateTime.Parse(item);

                            if (!dummy.Any(d => valDate >= d.PeriodStart && valDate <= d.PeriodEnd))
                            {
                                wadah.Add(item);
                            }
                        }
                        if (wadah.Count() > 0)
                        {
                            palid            = false;
                            ViewBag.errorMsg = "Tidak terdapat daftar harga untuk jenis truck " + RepoJenisTruck.FindByPK(model.JenisTruckId.Value).StrJenisTruck +
                                               " pada tanggal " + string.Join(", ", wadah);
                        }
                    }
                    else
                    {
                        palid            = false;
                        ViewBag.errorMsg = "Harap pilih tanggal muat.";
                    }
                }
            }
            else
            {
                if (model.JsonDateMuat == null || model.JsonDateMuat == "")
                {
                    palid            = false;
                    ViewBag.errorMsg = "Harap pilih tanggal muat.";
                }
            }

            if (palid)
            {
                if (ModelState.IsValid)
                {
                    if (model.existingMuatDate != "" && model.existingMuatDate != null)
                    {
                        model.existingMuatDate = RepoSalesOrder.IsMuatDateExist(model.JsonDateMuat.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries).ToList(), model.CustomerId.Value);
                        if (model.existingMuatDate != "")
                        {
                            return(View("Form", model));
                        }
                    }

                    Context.SalesOrder        dbso   = new Context.SalesOrder();
                    Context.SalesOrderKontrak dbitem = new Context.SalesOrderKontrak();

                    model.setDb(dbitem);
                    dbitem.DocDate         = DateTime.Now;
                    dbitem.Urutan          = RepoSalesOrder.getUrutanKontrak() + 1;
                    dbitem.SONumber        = RepoSalesOrder.generateCodeKontrak(dbitem.Urutan);
                    dbitem.DN              = "DN" + dbitem.SONumber;
                    dbso.SalesOrderKontrak = 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));
        }
Beispiel #7
0
        public ActionResult EditOncall(SalesOrderOncall model, string btnsave)
        {
            //validasi manual
            bool palid  = true;
            int  urutan = 1;

            if (btnsave != "draft" && model.Status != "draft")
            {
                if (!model.IdDataTruck.HasValue)
                {
                    ModelState.AddModelError("IdDataTruck", "Truk harus diisi.");
                    palid = false;
                }
                if (!model.Driver1Id.HasValue || model.Driver1Id.Value == 0)
                {
                    ModelState.AddModelError("Driver1Id", "Driver harus diisi.");
                    palid = false;
                }
                if (model.Driver1Id != null)
                {
                    Context.Driver                 item = RepoDriver.FindAll().Where(d => d.Id == model.Driver1Id.Value).FirstOrDefault();
                    List <Context.SalesOrder>      dbso = RepoSalesOrder.FindAll().ToList();
                    List <Context.SettlementBatal> dbsb = RepoSettlementBatal.FindAll().Where(s => s.IsProses == false).ToList();
                    Driver             driver           = new Driver(item, dbso, dbsb);
                    Context.SalesOrder dbsoDriver       = RepoSalesOrder.FindAll().Where(d =>
                                                                                         (d.Status == "save" || d.Status == "draft planning" || d.Status == "draft konfirmasi" || d.Status == "save konfirmasi" || d.Status == "dispatched") && (
                                                                                             (d.SalesOrderOncallId.HasValue && (d.SalesOrderOncall.Driver1Id == model.Driver1Id || d.SalesOrderOncall.Driver2Id == model.Driver1Id)) ||
                                                                                             (d.SalesOrderPickupId.HasValue && (d.SalesOrderPickup.Driver1Id == model.Driver1Id || d.SalesOrderPickup.Driver2Id == model.Driver1Id)) ||
                                                                                             (d.SalesOrderProsesKonsolidasiId.HasValue && (d.SalesOrderProsesKonsolidasi.Driver1Id == model.Driver1Id || d.SalesOrderProsesKonsolidasi.Driver2Id == model.Driver1Id))
                                                                                             )
                                                                                         ).FirstOrDefault();
                    if (driver.StatusSo != "Available" && dbsoDriver != null && RepoSalesOrder.FindArea(dbsoDriver.SalesOrderOncall.IdDaftarHargaItem.Value) != "YES")
                    {
                        List <Context.SalesOrder> dbsoDriverList = RepoSalesOrder.FindAll().Where(d =>
                                                                                                  (d.Status == "save" || d.Status == "draft planning" || d.Status == "draft konfirmasi" || d.Status == "save konfirmasi" || d.Status == "dispatched") && (
                                                                                                      (d.SalesOrderOncallId.HasValue && (d.SalesOrderOncall.Driver1Id == model.Driver1Id || d.SalesOrderOncall.Driver2Id == model.Driver1Id)) ||
                                                                                                      (d.SalesOrderPickupId.HasValue && (d.SalesOrderPickup.Driver1Id == model.Driver1Id || d.SalesOrderPickup.Driver2Id == model.Driver1Id)) ||
                                                                                                      (d.SalesOrderProsesKonsolidasiId.HasValue && (d.SalesOrderProsesKonsolidasi.Driver1Id == model.Driver1Id || d.SalesOrderProsesKonsolidasi.Driver2Id == model.Driver1Id))
                                                                                                      )
                                                                                                  ).ToList();
                        urutan = dbsoDriver.urutan + 1;

                        //SO yg sdh jalan
                        Context.Rute rute       = RepoSalesOrder.FindRute(dbsoDriver.SalesOrderOncall.IdDaftarHargaItem.Value);
                        int          waktuKerja = int.Parse(rute.WaktuKerja.ToString());

                        //SO yg sedang diplanning
                        Context.SalesOrder so           = RepoSalesOrder.FindByOnCall(model.SalesOrderId.Value);
                        Context.Rute       rutePlanning = null;
                        if (so != null && so.SalesOrderOncall != null && so.SalesOrderOncall.IdDaftarHargaItem != null)
                        {
                            rutePlanning = RepoSalesOrder.FindRute(so.SalesOrderOncall.IdDaftarHargaItem.Value);
                            int  waktuKerjaPlanning = int.Parse(rutePlanning.WaktuKerja.ToString());
                            bool used_at_that_date  = dbsoDriverList.Any(d =>
                                                                         DateTime.Parse(d.SalesOrderOncall.TanggalMuat.ToString()).AddDays(waktuKerja) >= model.TanggalMuat ||
                                                                         DateTime.Parse(d.SalesOrderOncall.TanggalMuat.ToString()) <= DateTime.Parse(model.TanggalMuat.ToString()).AddDays(waktuKerja)
                                                                         );
                            if (used_at_that_date)
                            {
                                ModelState.AddModelError("Driver1Id", "Driver tidak tersedia, harap pilih driver lain.");
                                palid = false;
                            }
                            else if (urutan > 2 && rutePlanning.IsAreaPulang != true)
                            {
                                ModelState.AddModelError("Driver1Id", "Driver tidak tersedia, harap pilih driver lain.");
                                palid = false;
                            }
                        }
                    }
                }
            }
            if (model.Status == "draft")
            {
                if (model.CommentUser == "" || model.CommentUser == null)
                {
                    ModelState.AddModelError("CommentUser", "Comment harus diisi.");
                    palid = false;
                }
            }
            if (palid)
            {
                Context.SalesOrder dbitem = RepoSalesOrder.FindByPK(model.SalesOrderId.Value);
                dbitem.isReturn  = false;
                dbitem.UpdatedBy = UserPrincipal.id;
                dbitem.urutan    = urutan;
                string act = "";
                if (btnsave != null && btnsave != "")
                {
                    act           = "Draft";
                    dbitem.Status = btnsave;
                }
                else
                {
                    if (model.Status.ToLower() == "save")
                    {
                        act             = "Return";
                        dbitem.isReturn = true;
                    }
                    else
                    {
                        act             = "Submit";
                        dbitem.isReturn = false;
                    }

                    dbitem.Status = model.Status;
                }

                model.setDbOperasional(dbitem.SalesOrderOncall, act, "Operational");
                RepoSalesOrder.save(dbitem);
                RepoAuditrail.savePlanningHistory(dbitem);
                Context.so_mstr   dbptnr = Reposo_mstr.FindByPK(dbitem.SalesOrderOncall.SONumber);
                Context.DataTruck truck  = RepoDataTruck.FindByPK(model.IdDataTruck);
                try
                {
                    dbptnr.so_vehicle = Repocode_mstr.FindByCodeName(truck.VehicleNo).id;
                    Reposo_mstr.UpdateSoMstrVehicle(dbptnr);
                }
                catch (Exception)
                {
                }
                return(RedirectToAction("Index"));
            }
            else
            {
                ViewBag.kondisi  = "planning";
                ViewBag.name     = model.SONumber;
                ViewBag.Title    = "Planning Sales Order Oncall " + model.SONumber;
                ViewBag.PostData = "EditOncall";
                return(View("SalesOrderOncall/FormReadOnly", model));
            }
        }
        public ActionResult Edit(RevisiTanggal model)
        {
            Context.SalesOrder    dbso      = RepoSalesOrder.FindByPK(model.IdSalesOrder.Value);
            Context.RevisiTanggal revisiTgl = new Context.RevisiTanggal();

            if (ModelState.IsValid)
            {
                if (dbso.Status == "dispatched")
                {
                    //batalkeun so na
                    Context.SettlementBatal dbsettlement        = new Context.SettlementBatal();
                    Context.AdminUangJalan  dummyAdminUangJalan = dbso.AdminUangJalan;
                    Context.BatalOrder      batalOrder          = new Context.BatalOrder();
                    //batal
                    dbso.Status             = "batal order";
                    batalOrder.IdSalesOrder = dbso.Id;
                    batalOrder.Keterangan   = "Revisi Tanggal";
                    batalOrder.ModifiedDate = DateTime.Now;
                    dbso.KeteranganBatal    = "Revisi tanggal dari " + (dbso.SalesOrderOncallId.HasValue ? (dbso.SalesOrderOncall.TanggalMuat.ToString()).Split(' ')[0] : dbso.SalesOrderPickupId.HasValue ? dbso.SalesOrderPickup.TanggalPickup.ToString().Split(' ')[0] : dbso.SalesOrderProsesKonsolidasi.TanggalMuat.ToString().Split(' ')[0]) + " ke " + model.TanggalBaru.ToString().Split(' ')[0] + " - " + model.KeteranganRevisi;
                    RepoSalesOrder.save(dbso);
                    RepoBatalOrder.save(batalOrder, UserPrincipal.id);
                    //settlement batal
                    dbsettlement.IdDriver     = dummyAdminUangJalan.IdDriver1;
                    dbsettlement.IdSalesOrder = dbso.Id;
                    if (dummyAdminUangJalan.AdminUangJalanUangTf.Any(d => d.Keterangan == "Tunai"))
                    {
                        dbsettlement.KasDiterima = dummyAdminUangJalan.AdminUangJalanUangTf.Where(d => d.Keterangan == "Tunai").FirstOrDefault().JumlahTransfer;
                    }
                    if (dummyAdminUangJalan.AdminUangJalanUangTf.Any(d => d.Keterangan.Contains("Transfer")))
                    {
                        dbsettlement.TransferDiterima = dummyAdminUangJalan.AdminUangJalanUangTf.Where(d => d.Keterangan.Contains("Transfer")).Sum(t => t.JumlahTransfer);
                    }
                    dbsettlement.SolarDiterima = dummyAdminUangJalan.AdminUangJalanVoucherSpbu.Sum(s => s.Value);
                    dbsettlement.KapalDiterima = dummyAdminUangJalan.AdminUangJalanVoucherKapal.Sum(s => s.Value);
                    dbsettlement.JenisBatal    = "Batal Order";
                    RepoSettBatal.save(dbsettlement, UserPrincipal.id, "Revisi Tanggal");
                    //create so baru
                    Context.SalesOrder dummySo = new Context.SalesOrder();
                    dummySo.isReturn         = true;
                    dummySo.DateRevised      = true;
                    dummySo.Status           = "Draft";
                    dummySo.AdminUangJalanId = null;
                    dummySo.AdminUangJalan   = null;
                    dummySo.DateStatus       = DateTime.Now;

                    if (dbso.SalesOrderOncallId.HasValue)
                    {
                        //urus anak na
                        Context.SalesOrderOncall dboncall    = new Context.SalesOrderOncall();
                        SalesOrderOncall         modelOncall = new SalesOrderOncall(dbso);
                        modelOncall.setDb(dboncall);
                        dboncall.SalesOrderOnCallId = 0;
                        dboncall.TanggalMuat        = model.TanggalBaru;
                        dboncall.JamMuat            = model.JamBaru.Value;
                        dboncall.Urutan             = RepoSalesOrder.getUrutanOnCAll(modelOncall.TanggalMuat.Value) + 1;
                        dboncall.SONumber           = RepoSalesOrder.generateCodeOnCall(modelOncall.TanggalMuat.Value, dboncall.Urutan);
                        dboncall.DN         = "DN" + dboncall.SONumber;
                        dboncall.Keterangan = "Revisi tanggal dari " + (dbso.SalesOrderOncallId.HasValue ? (dbso.SalesOrderOncall.TanggalMuat.ToString()).Split(' ')[0] : dbso.SalesOrderPickupId.HasValue ? dbso.SalesOrderPickup.TanggalPickup.ToString().Split(' ')[0] : dbso.SalesOrderProsesKonsolidasi.TanggalMuat.ToString().Split(' ')[0]) + " ke " + model.TanggalBaru.ToString().Split(' ')[0] + " - " + model.KeteranganRevisi + " - " + dbso.SalesOrderOncall.Keterangan;

                        dummySo.SalesOrderOncall = dboncall;
                        RepoAuditrail.SetAuditTrail("INSERT INTO dbo.\"SalesOrderOncall\" (\"SONumber\", \"Urutan\", \"TanggalOrder\", \"JamOrder\", \"CustomerId\", \"PrioritasId\", \"JenisTruckId\", \"ProductId\", " +
                                                    "\"TanggalMuat\", \"JamMuat\", \"Keterangan\", \"KeteranganLoading\", \"KeteranganUnloading\", \"IdDaftarHargaItem\", \"StrDaftarHargaItem\", \"StrMultidrop\", \"IdDataTruck\", \"Driver1Id\", " +
                                                    "\"KeteranganDriver1\", \"Driver2Id\", \"KeteranganDriver2\", \"IsCash\", \"KeteranganRek\", \"IdDriverTitip\", \"DN\", \"KeteranganDataTruck\", \"AtmId\") VALUES (" + dboncall.SONumber + ", "
                                                    + dboncall.Urutan + ", " + dboncall.TanggalOrder + ", " + dboncall.JamOrder + ", " + dboncall.CustomerId + ", " + dboncall.PrioritasId + ", " + dboncall.JenisTruckId + ", " + dboncall.ProductId +
                                                    ", " + dboncall.TanggalMuat + ", " + dboncall.JamMuat + ", " + dboncall.Keterangan + ", " + dboncall.KeteranganLoading + ", " + dboncall.KeteranganUnloading + ", " + dboncall.IdDaftarHargaItem +
                                                    "," + dboncall.StrDaftarHargaItem + ", " + dboncall.StrMultidrop + ", " + dboncall.IdDataTruck + ", " + dboncall.Driver1Id + ", " + dboncall.KeteranganDriver1 + ", " + dboncall.Driver2Id + ", " +
                                                    dboncall.KeteranganDriver2 + ", " + dboncall.IsCash + ", " + dboncall.KeteranganRek + ", " + dboncall.IdDriverTitip + ", " + dboncall.DN + ", " + dboncall.KeteranganDataTruck + ", " +
                                                    dboncall.AtmId + ");", "List Order", "Revisi Jenis Truk", UserPrincipal.id);
                    }
                    else if (dbso.SalesOrderPickupId.HasValue)
                    {
                        Context.SalesOrderPickup dbpickup    = new Context.SalesOrderPickup();
                        SalesOrderPickup         modelPickup = new SalesOrderPickup(dbso);
                        modelPickup.setDb(dbpickup);
                        dbpickup.SalesOrderPickupId = 0;
                        dbpickup.TanggalPickup      = model.TanggalBaru.Value;
                        dbpickup.JamPickup          = model.JamBaru.Value;
                        dbpickup.Urutan             = RepoSalesOrder.getUrutanPickup(modelPickup.TanggalPickup.Value) + 1;
                        dbpickup.SONumber           = RepoSalesOrder.generatePickup(modelPickup.TanggalPickup.Value, dbpickup.Urutan);
                        dbpickup.Keterangan         = "Revisi tanggal dari " + (dbso.SalesOrderOncallId.HasValue ? (dbso.SalesOrderOncall.TanggalMuat.ToString()).Split(' ')[0] : dbso.SalesOrderPickupId.HasValue ? dbso.SalesOrderPickup.TanggalPickup.ToString().Split(' ')[0] : dbso.SalesOrderProsesKonsolidasi.TanggalMuat.ToString().Split(' ')[0]) + " ke " + model.TanggalBaru.ToString().Split(' ')[0] + " - " + model.KeteranganRevisi + " - " + dbso.SalesOrderPickup.Keterangan;

                        dummySo.SalesOrderPickup = dbpickup;
                    }
                    else if (dbso.SalesOrderProsesKonsolidasiId.HasValue)
                    {
                        Context.SalesOrderProsesKonsolidasi dbkonsolidasi    = new Context.SalesOrderProsesKonsolidasi();
                        SalesOrderProsesKonsolidasi         modelKonsolidasi = new SalesOrderProsesKonsolidasi(dbso);
                        modelKonsolidasi.setDb(dbkonsolidasi);
                        dbkonsolidasi.SalesOrderProsesKonsolidasiId = 0;
                        dbkonsolidasi.TanggalMuat = model.TanggalBaru.Value;
                        dbkonsolidasi.JamMuat     = model.JamBaru.Value;
                        dbkonsolidasi.Urutan      = RepoSalesOrder.getUrutanProsesKonsolidasi(modelKonsolidasi.TanggalMuat.Value) + 1;
                        dbkonsolidasi.SONumber    = RepoSalesOrder.generateProsesKonsolidasi(modelKonsolidasi.TanggalMuat.Value, dbkonsolidasi.Urutan);
                        dbkonsolidasi.DN          = "DN" + dbkonsolidasi.SONumber;
                        dbkonsolidasi.Keterangan  = "Revisi tanggal dari " + (dbso.SalesOrderOncallId.HasValue ? (dbso.SalesOrderOncall.TanggalMuat.ToString()).Split(' ')[0] : dbso.SalesOrderPickupId.HasValue ? dbso.SalesOrderPickup.TanggalPickup.ToString().Split(' ')[0] : dbso.SalesOrderProsesKonsolidasi.TanggalMuat.ToString().Split(' ')[0]) + " ke " + model.TanggalBaru.ToString().Split(' ')[0] + " - " + model.KeteranganRevisi + " - " + dbso.SalesOrderProsesKonsolidasi.Keterangan;

                        dummySo.SalesOrderProsesKonsolidasi = dbkonsolidasi;
                        RepoAuditrail.saveSalesOrderProsesKonsolidasiQuery(dummySo.SalesOrderProsesKonsolidasi, UserPrincipal.id);
                    }

                    dummySo.Id = 0;
                    string sod_guid = Guid.NewGuid().ToString();
                    dummySo.oidErp = sod_guid;
                    RepoSalesOrder.save(dummySo);
                    SyncToERP(dummySo, sod_guid);
                }
                else
                {
                    if (dbso.SalesOrderOncallId.HasValue)
                    {
                        dbso.SalesOrderOncall.TanggalMuat = model.TanggalBaru;
                        dbso.SalesOrderOncall.JamMuat     = model.JamBaru.Value;
                        RepoAuditrail.SetAuditTrail(
                            "UPDATE dbo.\"SalesOrderOncall\" SET \"TanggalMuat\" = " + model.TanggalBaru + ", \"JamMuat\" = " + model.JamBaru.Value + " WHERE \"SalesOrderOnCallId\" = " + dbso.SalesOrderOncallId + ";",
                            "List Order", "Revisi Rute", UserPrincipal.id
                            );
                    }
                    else if (dbso.SalesOrderPickupId.HasValue)
                    {
                        dbso.SalesOrderPickup.TanggalPickup = model.TanggalBaru.Value;
                        dbso.SalesOrderPickup.JamPickup     = model.JamBaru.Value;
                    }
                    else if (dbso.SalesOrderProsesKonsolidasiId.HasValue)
                    {
                        dbso.SalesOrderProsesKonsolidasi.TanggalMuat = model.TanggalBaru.Value;
                        dbso.SalesOrderProsesKonsolidasi.JamMuat     = model.JamBaru.Value;
                        RepoAuditrail.saveUpdSalesOrderProsesKonsolidasiQuery(dbso.SalesOrderProsesKonsolidasi, UserPrincipal.id);
                    }
                    revisiTgl.IdSalesOrder = model.IdSalesOrder.Value;
                    dbso.DateRevised       = true;
                    RepoSalesOrder.save(dbso);
                    RepoRevisiTanggal.save(revisiTgl);
                }

                return(RedirectToAction("Index", "ListOrder"));
            }

            return(View("Form", model));
        }
        public ActionResult EditOncall(SalesOrderOncall model, string btnsave)
        {
            //validasi manual
            bool palid = true;

            if (btnsave != "draft planning")
            {
                if (!model.IdDataTruck.HasValue)
                {
                    ModelState.AddModelError("IdDataTruck", "Truk harus diisi.");
                    palid = false;
                }
                if (!model.Driver1Id.HasValue)
                {
                    ModelState.AddModelError("Driver1Id", "Driver harus diisi.");
                    palid = false;
                }
            }
            if (!model.AtmId.HasValue || model.AtmId.Value == 0)
            {
                ModelState.AddModelError("AtmId", "Rekening harus diisi.");
                palid = false;
            }
            if (palid)
            {
                Context.SalesOrder dbitem = RepoSalesOrder.FindByPK(model.SalesOrderId.Value);
                string             act    = "";
                if (btnsave != null && btnsave != "")
                {
                    act           = "Draft";
                    dbitem.Status = btnsave;
                }
                else
                {
                    if (model.Status.ToLower() == "save")
                    {
                        act             = "Return";
                        dbitem.isReturn = true;
                    }
                    else
                    {
                        act             = "Submit";
                        dbitem.isReturn = false;
                    }

                    dbitem.Status = model.Status;
                }

                model.setDbOperasional(dbitem.SalesOrderOncall, act, "Operational");
                dbitem.UpdatedBy = UserPrincipal.id;
                RepoSalesOrder.save(dbitem);
                RepoAuditrail.saveKonfirmasiHistory(dbitem);
                if (dbitem.AdminUangJalanId.HasValue)
                {
                    Context.AdminUangJalan dbauj = RepoAdminUangJalan.FindByPK(dbitem.AdminUangJalanId.Value);
                    dbauj.IdDriver1 = dbitem.SalesOrderOncall.Driver1Id;
                    RepoAdminUangJalan.save(dbauj);
                }
                return(RedirectToAction("Index"));
            }
            else
            {
                ViewBag.kondisi  = "konfirmasi";
                ViewBag.name     = model.SONumber;
                ViewBag.Title    = "Konfirmsai Sales Order Oncall " + model.SONumber;
                ViewBag.PostData = "EditOncall";
                return(View("SalesOrderOncall/FormReadOnly", model));
            }
        }
        public ActionResult Add(SettlementReg model)
        {
            SettlementRegTambahanBiaya[] res = JsonConvert.DeserializeObject <SettlementRegTambahanBiaya[]>(model.StrBiayaTambahan);
            model.ListBiayaTambahan = res.ToList();
            Context.ERPConfig  erpConfig = RepoERPConfig.FindByFrist();
            Context.SalesOrder dbso      = RepoSalesOrder.FindByPK(model.IdSalesOrder.Value);
            string             sod_oid   = Guid.NewGuid().ToString();

            if (ModelState.IsValid)
            {
                if (model.listIdSoKontrak != "" && model.listIdSoKontrak != null)
                {
                    List <int> ListIdDumy = model.listIdSoKontrak.Split(new string[] { "." }, StringSplitOptions.None).ToList().Select(int.Parse).ToList();
                    List <Context.SalesOrderKontrakListSo> dbsoDummy = dbso.SalesOrderKontrak.SalesOrderKontrakListSo.Where(d => ListIdDumy.Contains(d.Id)).ToList();
                    //dbso.SalesOrderKontrak.SalesOrderKontrakListSo = dbsoDummy;
                    foreach (var item in dbsoDummy)
                    {
                        item.Status = "settlement";
                    }
                    Context.SettlementReguler dbitem = new Context.SettlementReguler();
                    model.SetDb(dbitem);
                    RepoSalesOrder.save(dbso);
                    RepoSettlementReg.save(dbitem, UserPrincipal.id);
                    foreach (Context.SettlementRegulerTambahanBiaya srtb in dbitem.SettlementRegulerTambahanBiaya)
                    {
                        RepoAuditrail.saveSettlementRegulerBiayaTambahanQuery(srtb, UserPrincipal.id);
                    }
                }
                else
                {
                    Context.SettlementReguler dbitem = new Context.SettlementReguler();
                    string code = (dbso.SalesOrderOncallId.HasValue ? dbso.SalesOrderOncall.SONumber : dbso.SalesOrderProsesKonsolidasiId.HasValue ? dbso.SalesOrderProsesKonsolidasi.SONumber : dbso.SalesOrderPickupId.HasValue ? dbso.SalesOrderPickup.SONumber : dbso.SalesOrderKontrak.SONumber);
                    dbitem.Code = "SR-" + code;
                    string unyu = dbitem.Code;
                    model.SetDb(dbitem);
                    Context.AdminUangJalan db = RepoAdminUangJalan.FindByPK(dbso.AdminUangJalanId.Value);
                    dbso.Status    = "settlement";
                    dbso.UpdatedBy = UserPrincipal.id;
                    if (dbso.oidErp == null)
                    {
                        dbso.oidErp = sod_oid;
                    }
                    RepoSalesOrder.save(dbso);
                    RepoSettlementReg.save(dbitem, UserPrincipal.id);
                    RepoAuditrail.saveSettRegHistory(dbso);
                    foreach (Context.SettlementRegulerTambahanBiaya srtb in dbitem.SettlementRegulerTambahanBiaya)
                    {
                        RepoAuditrail.saveSettlementRegulerBiayaTambahanQuery(srtb, UserPrincipal.id);
                    }

                    //lebah dieu sync ERPna
                    //klo ada revisi rute
                    //D
                    Repoglt_det.saveFromAc(1, dbitem.Code, dbitem.KasSelisih + dbitem.TransferSelisih + dbitem.SolarSelisih + (dbitem.KapalSelisih == null ? 0 : dbitem.KapalSelisih), 0, Repoac_mstr.FindByPk(erpConfig.IdKasbonDriver)); //Kasbon Driver (Piutang)
                    Repoglt_det.saveFromBk(2, dbitem.Code, dbitem.KasAktual + dbitem.TransferAktual + dbitem.SolarAktual + (dbitem.KapalAktual == null ? 0 : dbitem.KapalAktual), 0, Repobk_mstr.FindByPk(erpConfig.IdCashCredit));        //Cash
                    Repoglt_det.saveFromAc(3, dbitem.Code, dbitem.KasDiakui + dbitem.TransferDiakui + dbitem.SolarDiakui + (dbitem.KapalDiakui == null ? 0 : dbitem.KapalDiakui), 0, Repoac_mstr.FindByPk(erpConfig.IdBiayaPerjalanan));   //Biaya

                    //K
                    decimal?tambahanRute  = db.AdminUangJalanTambahanRute.Sum(s => s.values);
                    decimal?boronganDasar = db.TotalBorongan - db.Kawalan - db.Timbangan - db.Karantina - db.SPSI - db.Multidrop - tambahanRute - db.AdminUangJalanTambahanLain.Sum(s => s.Values);
                    Repoglt_det.saveFromAc(1, dbitem.Code, 0, boronganDasar, Repoac_mstr.FindByPk(erpConfig.IdBoronganDasar));
                    Repoglt_det.saveFromAc(2, dbitem.Code, 0, db.Kawalan, Repoac_mstr.FindByPk(erpConfig.IdKawalan));
                    Repoglt_det.saveFromAc(3, dbitem.Code, 0, db.Timbangan, Repoac_mstr.FindByPk(erpConfig.IdTimbangan));
                    Repoglt_det.saveFromAc(4, dbitem.Code, 0, db.Karantina, Repoac_mstr.FindByPk(erpConfig.IdKarantina));
                    Repoglt_det.saveFromAc(5, dbitem.Code, 0, db.SPSI, Repoac_mstr.FindByPk(erpConfig.IdSPSI));
                    Repoglt_det.saveFromAc(6, dbitem.Code, 0, db.Multidrop, Repoac_mstr.FindByPk(erpConfig.IdMultidrop));
                    Repoglt_det.saveFromAc(7, dbitem.Code, 0, tambahanRute, Repoac_mstr.FindByPk(erpConfig.IdTambahanRuteMuat));
                    Repoglt_det.saveFromAc(8, dbitem.Code, 0, db.AdminUangJalanTambahanLain.Sum(s => s.Values), Repoac_mstr.FindByPk(erpConfig.IdTambahanRuteLain));
                    Repoglt_det.saveFromAc(4, dbitem.Code, 0, db.TotalPotonganDriver, Repoac_mstr.FindByPk(erpConfig.IdKasbonAuj)); //Hutang ke Driver, jurnal balik dr auj

                    //Cash&Transfer pada biaya tambahan
                    Repoglt_det.saveFromAc(3, dbitem.Code, dbitem.SettlementRegulerTambahanBiaya.Sum(d => d.Value), 0, Repoac_mstr.FindByPk(erpConfig.IdBiayaPerjalanan)); //D, Biaya Tambahan
                    Repoglt_det.saveFromAc(2, dbitem.Code, 0, dbitem.TotalCash, Repoac_mstr.FindByPk(erpConfig.IdCashCredit));                                             //K, Cash
                    Repoglt_det.saveFromAc(2, dbitem.Code, 0, dbitem.TotalTf, Repoac_mstr.FindByPk(erpConfig.IdTfCredit));                                                 //K, Transfer
                }
                return(RedirectToAction("Index"));
            }

            return(View("Form", model));
        }
Beispiel #11
0
        public ActionResult Add(SalesOrderProsesKonsolidasi model, string btnsave)
        {
            //cek validasi so
            List <string> MsgError = new List <string>();

            //if (model.IdJnsTruck.HasValue)
            //{
            //    if (model.StrListSo != null && model.StrListSo != "")
            //    {
            //        foreach (string item in model.StrListSo.Split(','))
            //        {
            //            IsValidSo(int.Parse(item), model.IdJnsTruck.Value, MsgError);
            //        }
            //    }
            //}

            SalesOrderLoadUnload[] resultLoad = JsonConvert.DeserializeObject <SalesOrderLoadUnload[]>(model.StrLoad);
            model.ListLoad = resultLoad.ToList();
            SalesOrderLoadUnload[] resultUnload = JsonConvert.DeserializeObject <SalesOrderLoadUnload[]>(model.StrUnload);
            model.ListUnload = resultUnload.ToList();

            if (ModelState.IsValid && MsgError.Count < 1)
            {
                Context.SalesOrder dbso = new Context.SalesOrder();
                Context.SalesOrderProsesKonsolidasi dbitem = new Context.SalesOrderProsesKonsolidasi();
                SetIsselect(model.StrListSo, null);
                model.setDb(dbitem);
                dbitem.Urutan   = RepoSalesOrder.getUrutanProsesKonsolidasi(model.TanggalMuat.Value) + 1;
                dbitem.SONumber = RepoSalesOrder.generateProsesKonsolidasi(model.TanggalMuat.Value, dbitem.Urutan);
                dbitem.DN       = "DN" + dbitem.SONumber;
                dbso.SalesOrderProsesKonsolidasi = dbitem;

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

                RepoSalesOrder.save(dbso);
                RepoAuditrail.saveSalesOrderProsesKonsolidasiQuery(dbitem, UserPrincipal.id);
                foreach (Context.SalesOrderProsesKonsolidasiItem item in dbitem.SalesOrderProsesKonsolidasiItem)
                {
                    RepoAuditrail.saveSalesOrderProsesKonsolidasiItemQuery(item, UserPrincipal.id);
                }
                foreach (Context.SalesOrderProsesKonsolidasiLoadingAdd item in dbitem.SalesOrderProsesKonsolidasiLoadingAdd)
                {
                    RepoAuditrail.saveSalesOrderProsesKonsolidasiLoadingAddQuery(item, UserPrincipal.id);
                }
                foreach (Context.SalesOrderProsesKonsolidasiUnLoadingAdd item in dbitem.SalesOrderProsesKonsolidasiUnLoadingAdd)
                {
                    RepoAuditrail.saveSalesOrderProsesKonsolidasiUnLoadingAddQuery(item, UserPrincipal.id);
                }

                return(RedirectToAction("Index"));
            }


            ViewBag.ErrorSo = MsgError;

            return(View("Form", model));
        }
        public JsonResult Proses(string listSo, int idSo)
        {
            ResponeModel response = new ResponeModel(true);

            Context.SalesOrder dbitem = RepoSalesOrder.FindByPK(idSo);
            List <ListTruckSo> res    = JsonConvert.DeserializeObject <ListTruckSo[]>(listSo).ToList();

            //int urutListSo = (RepoSalesOrderKontrakListSo.getUrutanProses(dbitem.SalesOrderKontrakId)) + 1;
            foreach (ListTruckSo item in res)
            {
                Context.SalesOrderKontrakListSo dblist = RepoSalesOrderKontrakListSo.FindByPK(item.Id);
                dblist.IdDataTruck = item.IdTruk == null ? null : item.IdTruk;
                dblist.Driver1Id   = item.IdDriver1 == null ? null : item.IdDriver1;
                dblist.Driver2Id   = item.IdDriver2 == null ? null : item.IdDriver2;
                dblist.IsProses    = true;
                dblist.Status      = "save konfirmasi";
                try
                {
                    RepoSalesOrderKontrakListSo.save(dblist);
                    if (dbitem.SalesOrderKontrak.SalesOrderKontrakListSo.Any(s => s.IsProses == false))
                    {
                        dbitem.Status = "draft konfirmasi";
                    }
                    else
                    {
                        dbitem.Status = "save konfirmasi";
                    }
                    RepoSalesOrder.save(dbitem);
                }
                catch (Exception e)
                {
                    response.Success = false;
                    response.Message = e.Message;
                }
            }

            var data = res.GroupBy(d => d.IdTruk).Select(grp => grp.ToList());

            foreach (var item in data)
            {
                foreach (var item2 in item)
                {
                    Context.SalesOrderKontrakListSo dblist = RepoSalesOrderKontrakListSo.FindByPK(item2.Id);
                    dblist.IdDataTruck = item2.IdTruk == null ? null : item2.IdTruk;
                    dblist.Driver1Id   = item2.IdDriver1 == null ? null : item2.IdDriver1;
                    dblist.Driver2Id   = item2.IdDriver2 == null ? null : item2.IdDriver2;
                    dblist.IsProses    = true;
                    //dblist.Urutan = urutListSo;
                    dblist.Status = "save konfirmasi";

                    try
                    {
                        RepoSalesOrderKontrakListSo.save(dblist);
                        if (dbitem.SalesOrderKontrak.SalesOrderKontrakListSo.Any(s => s.IsProses == false))
                        {
                            dbitem.Status = "draft konfirmasi";
                        }
                        else
                        {
                            dbitem.Status = "save konfirmasi";
                        }
                        RepoSalesOrder.save(dbitem);
                    }
                    catch (Exception e)
                    {
                        response.Success = false;
                        response.Message = e.Message;
                    }
                }
                //urutListSo++;
            }

            return(Json(response));
        }
        public ActionResult Add(SalesOrderOncall model, string btnsave)
        {
            //validasi tambahan
            bool palid = true;

            if (ModelState.IsValid)
            {
                if (btnsave == null && model.Status == "save")
                {
                    List <Context.DaftarHargaOnCall> dummy = RepoDaftarHarga.FindAll().Where(d => d.IdCust == model.CustomerId && (model.TanggalMuat >= d.PeriodStart && model.TanggalMuat <= d.PeriodEnd)).ToList();

                    if (dummy.Where(d => d.DaftarHargaOnCallItem.Any(i => i.IdJenisTruck == model.JenisTruckId)).Count() < 1)
                    {
                        palid            = false;
                        ViewBag.errorMsg = "Tidak terdapat daftar harga untuk jenis truck " + RepoJnsTruck.FindByPK(model.JenisTruckId.Value).StrJenisTruck;
                    }
                }
            }

            SalesOrderLoadUnload[] resultLoad = JsonConvert.DeserializeObject <SalesOrderLoadUnload[]>(model.StrLoad);
            model.ListLoad = resultLoad.ToList();
            SalesOrderLoadUnload[] resultUnload = JsonConvert.DeserializeObject <SalesOrderLoadUnload[]>(model.StrUnload);
            model.ListUnload = resultUnload.ToList();

            if (palid)
            {
                if (ModelState.IsValid)
                {
                    Context.SalesOrder       dbso   = new Context.SalesOrder();
                    Context.SalesOrderOncall dbitem = new Context.SalesOrderOncall();
                    dbitem.Urutan   = RepoSalesOrder.getUrutanOnCAll(model.TanggalMuat.Value) + 1;
                    dbitem.SONumber = RepoSalesOrder.generateCodeOnCall(model.TanggalMuat.Value, dbitem.Urutan);
                    dbitem.DN       = "DN" + dbitem.SONumber;
                    if (btnsave == "save")
                    {
                        dbso.Status = "save";
                    }
                    else if (btnsave == "draft")
                    {
                        dbso.Status = "draft";
                    }
                    else
                    {
                        dbso.Status = model.Status;
                    }

                    if (model.Status == "save")
                    {
                        //{ Nama: "Sales Order", Id: "S" },
                        //{ Nama: "Planning Order", Id: "P" },
                        //{ Nama: "Konfirmasi Planning", Id: "KP" },
                        //{ Nama: "Admin Uang Jalan", Id: "A" },
                        //{ Nama: "Kasir", Id: "K" }
                        RepoListNotif.save("P",
                                           "PLANNING SO : " + model.SONumber + " " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + " belum diproses.",
                                           "PLANNING SO : " + model.SONumber + " " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + " belum diproses.");
                    }

                    model.setDb(dbitem, model.Status == "save" ? "Submit" : model.Status == "draft" ? "Draft" : "", "Marketing");
                    dbso.SalesOrderOncall = dbitem;
                    dbso.isReturn         = false;
                    dbso.CreatedBy        = UserPrincipal.id;
                    dbso.UpdatedBy        = UserPrincipal.id;
                    string sod_guid = Guid.NewGuid().ToString();
                    dbso.oidErp = sod_guid;
                    RepoSalesOrder.save(dbso);
                    Context.SalesOrder so = RepoSalesOrder.FindByOnCallCode(dbitem.SONumber);
                    RepoAuditrail.saveOrderHistory(so);
                    return(RedirectToAction("Index"));
                }
            }

            return(View("Form", model));
        }
        public ActionResult Edit(RevisiRute model)
        {
            Context.SalesOrder dbso       = RepoSalesOrder.FindByPK(model.IdSalesOrder.Value);
            Context.RevisiRute revisiRute = new Context.RevisiRute();
            revisiRute.Code = "RR" + (dbso.SalesOrderOncallId.HasValue ? dbso.SalesOrderOncall.SONumber : dbso.SalesOrderProsesKonsolidasiId.HasValue ? dbso.SalesOrderProsesKonsolidasi.SONumber : dbso.SalesOrderPickupId.HasValue ? dbso.SalesOrderPickup.SONumber : dbso.SalesOrderKontrak.SONumber);
            Context.SalesOrder dummySo = new Context.SalesOrder();

            if (ModelState.IsValid)
            {
                if (dbso.Status == "dispatched" || dbso.Status == "admin uang jalan")
                {
                    Context.AdminUangJalan dummyAdminUangJalan = dbso.AdminUangJalan;
                    Context.BatalOrder     batalOrder          = new Context.BatalOrder();
                    //batal
                    dbso.Status             = "batal order";
                    dbso.KeteranganBatal    = "Revisi rute dari " + model.RuteLama + " ke " + model.RuteBaru;//"Revisi RUte" dari <rute sebelumnya> ke <rute yang dipilih > [isi dari keterangan revisi rute ]
                    batalOrder.IdSalesOrder = dbso.Id;
                    batalOrder.Keterangan   = "Revisi Rute";
                    batalOrder.ModifiedDate = DateTime.Now;
                    RepoSalesOrder.save(dbso);
                    RepoBatalOrder.save(batalOrder, UserPrincipal.id);
                    if (dbso.Status == "dispatched")
                    {
                        //batalkeun so na
                        Context.SettlementBatal dbsettlement = new Context.SettlementBatal();
                        //settlement batal
                        dbsettlement.IdDriver     = dummyAdminUangJalan.IdDriver1;
                        dbsettlement.IdSalesOrder = dbso.Id;
                        if (dummyAdminUangJalan.AdminUangJalanUangTf.Any(d => d.Keterangan == "Tunai"))
                        {
                            dbsettlement.KasDiterima = dummyAdminUangJalan.AdminUangJalanUangTf.Where(d => d.Keterangan == "Tunai").FirstOrDefault().JumlahTransfer;
                        }
                        if (dummyAdminUangJalan.AdminUangJalanUangTf.Any(d => d.Keterangan.Contains("Transfer")))
                        {
                            dbsettlement.TransferDiterima = dummyAdminUangJalan.AdminUangJalanUangTf.Where(d => d.Keterangan.Contains("Transfer")).Sum(t => t.JumlahTransfer);
                        }
                        dbsettlement.SolarDiterima = dummyAdminUangJalan.AdminUangJalanVoucherSpbu.Sum(s => s.Value);
                        dbsettlement.KapalDiterima = dummyAdminUangJalan.AdminUangJalanVoucherKapal.Sum(s => s.Value);
                        dbsettlement.JenisBatal    = "Batal Order";
                        RepoSettBatal.save(dbsettlement, UserPrincipal.id, "Batal Order");
                    }
                    else
                    {
                        //jurnal balik cynt
                        Context.ERPConfig erpConfig     = RepoERPConfig.FindByFrist();
                        decimal?          tambahanRute  = dummyAdminUangJalan.AdminUangJalanTambahanRute.Sum(s => s.values);
                        decimal?          boronganDasar = dummyAdminUangJalan.TotalAlokasi - dummyAdminUangJalan.Kawalan - dummyAdminUangJalan.Timbangan - dummyAdminUangJalan.Karantina - dummyAdminUangJalan.SPSI - dummyAdminUangJalan.Multidrop - tambahanRute - dummyAdminUangJalan.AdminUangJalanTambahanLain.Sum(s => s.Values);
                        Repoglt_det.saveFromAc(1, revisiRute.Code, 0, boronganDasar, Repoac_mstr.FindByPk(erpConfig.IdBoronganDasar));
                        Repoglt_det.saveFromAc(2, revisiRute.Code, 0, dummyAdminUangJalan.Kawalan, Repoac_mstr.FindByPk(erpConfig.IdKawalan));
                        Repoglt_det.saveFromAc(3, revisiRute.Code, 0, dummyAdminUangJalan.Timbangan, Repoac_mstr.FindByPk(erpConfig.IdTimbangan));
                        Repoglt_det.saveFromAc(4, revisiRute.Code, 0, dummyAdminUangJalan.Karantina, Repoac_mstr.FindByPk(erpConfig.IdKarantina));
                        Repoglt_det.saveFromAc(5, revisiRute.Code, 0, dummyAdminUangJalan.SPSI, Repoac_mstr.FindByPk(erpConfig.IdSPSI));
                        Repoglt_det.saveFromAc(6, revisiRute.Code, 0, dummyAdminUangJalan.Multidrop, Repoac_mstr.FindByPk(erpConfig.IdMultidrop));
                        Repoglt_det.saveFromAc(7, revisiRute.Code, 0, tambahanRute, Repoac_mstr.FindByPk(erpConfig.IdTambahanRuteMuat));
                        Repoglt_det.saveFromAc(8, revisiRute.Code, 0, dummyAdminUangJalan.AdminUangJalanTambahanLain.Sum(s => s.Values), Repoac_mstr.FindByPk(erpConfig.IdTambahanRuteLain));
                        Repoglt_det.saveFromAc(9, revisiRute.Code, dummyAdminUangJalan.KasbonDriver1, 0, Repoac_mstr.FindByPk(erpConfig.IdKasbonAuj));
                        Repoglt_det.saveFromAc(10, revisiRute.Code, dummyAdminUangJalan.KlaimDriver1, 0, Repoac_mstr.FindByPk(erpConfig.IdKlaimAuj));
                        Repoglt_det.saveFromAc(11, revisiRute.Code, dummyAdminUangJalan.AdminUangJalanPotonganDriver.Sum(s => s.Value), 0, Repoac_mstr.FindByPk(erpConfig.IdPotonganLainAuj));
                        Repoglt_det.saveFromAc(12, revisiRute.Code, dummyAdminUangJalan.AdminUangJalanVoucherKapal.Sum(s => s.Value), 0, Repoac_mstr.FindByPk(erpConfig.IdVoucherKapal));
                        Repoglt_det.saveFromAc(13, revisiRute.Code, dummyAdminUangJalan.AdminUangJalanVoucherSpbu.Sum(s => s.Value), 0, Repoac_mstr.FindByPk(erpConfig.IdVoucherSolar));
                        Repoglt_det.saveFromAc(14, revisiRute.Code, dummyAdminUangJalan.AdminUangJalanUangTf.Sum(s => s.Value), 0, Repoac_mstr.FindByPk(erpConfig.IdAUJCredit));
                    }
                    //create so baru
                    dummySo.isReturn         = true;
                    dummySo.Status           = "Draft";
                    dummySo.AdminUangJalanId = null;
                    dummySo.AdminUangJalan   = null;
                    dummySo.DateStatus       = DateTime.Now;

                    try{
                        SalesOrderLoadUnload[] resultLoad = JsonConvert.DeserializeObject <SalesOrderLoadUnload[]>(model.StrLoad);
                        model.ListLoad = resultLoad.ToList();
                        SalesOrderLoadUnload[] resultUnload = JsonConvert.DeserializeObject <SalesOrderLoadUnload[]>(model.StrUnload);
                        model.ListUnload = resultUnload.ToList();
                    }
                    catch (Exception e)
                    {
                    }

                    if (dbso.SalesOrderOncallId.HasValue)
                    {
                        //urus anak na
                        Context.SalesOrderOncall dboncall    = new Context.SalesOrderOncall();
                        SalesOrderOncall         modelOncall = new SalesOrderOncall(dbso);
                        modelOncall.setDb(dboncall);
                        dboncall.SalesOrderOnCallId = 0;
                        dboncall.Urutan             = RepoSalesOrder.getUrutanOnCAll(modelOncall.TanggalMuat.Value) + 1;
                        dboncall.SONumber           = RepoSalesOrder.generateCodeOnCall(modelOncall.TanggalMuat.Value, dboncall.Urutan);
                        dboncall.DN = "DN" + dboncall.SONumber;
                        dboncall.IdDaftarHargaItem  = model.IdRute;
                        dboncall.StrDaftarHargaItem = model.RuteBaru;
                        dboncall.StrMultidrop       = model.MultidropBaru;
                        dboncall.SalesOrderOnCallLoadingAdd.Clear();
                        dboncall.Keterangan = "Revisi rute dari " + model.RuteLama + " ke " + model.RuteBaru + " - " + dbso.SalesOrderOncall.Keterangan;
                        foreach (SalesOrderLoadUnload item in model.ListLoad)
                        {
                            dboncall.SalesOrderOnCallLoadingAdd.Add(new Context.SalesOrderOnCallLoadingAdd()
                            {
                                CustomerId = dboncall.CustomerId,
                                CustomerLoadingAddressId = item.Id,
                                urutan   = item.urutan,
                                IsSelect = item.IsSelect
                            });
                        }

                        dboncall.SalesOrderOnCallUnLoadingAdd.Clear();
                        foreach (SalesOrderLoadUnload item in model.ListUnload)
                        {
                            dboncall.SalesOrderOnCallUnLoadingAdd.Add(new Context.SalesOrderOnCallUnLoadingAdd()
                            {
                                CustomerId = dboncall.CustomerId,
                                CustomerUnloadingAddressId = item.Id,
                                urutan   = item.urutan,
                                IsSelect = item.IsSelect
                            });
                        }

                        dummySo.SalesOrderOncall = dboncall;
                        RepoAuditrail.SetAuditTrail("INSERT INTO dbo.\"SalesOrderOncall\" (\"SONumber\", \"Urutan\", \"TanggalOrder\", \"JamOrder\", \"CustomerId\", \"PrioritasId\", \"JenisTruckId\", \"ProductId\", " +
                                                    "\"TanggalMuat\", \"JamMuat\", \"Keterangan\", \"KeteranganLoading\", \"KeteranganUnloading\", \"IdDaftarHargaItem\", \"StrDaftarHargaItem\", \"StrMultidrop\", \"IdDataTruck\", \"Driver1Id\", " +
                                                    "\"KeteranganDriver1\", \"Driver2Id\", \"KeteranganDriver2\", \"IsCash\", \"KeteranganRek\", \"IdDriverTitip\", \"DN\", \"KeteranganDataTruck\", \"AtmId\") VALUES (" + dboncall.SONumber + ", "
                                                    + dboncall.Urutan + ", " + dboncall.TanggalOrder + ", " + dboncall.JamOrder + ", " + dboncall.CustomerId + ", " + dboncall.PrioritasId + ", " + dboncall.JenisTruckId + ", " + dboncall.ProductId +
                                                    ", " + dboncall.TanggalMuat + ", " + dboncall.JamMuat + ", " + dboncall.Keterangan + ", " + dboncall.KeteranganLoading + ", " + dboncall.KeteranganUnloading + ", " + dboncall.IdDaftarHargaItem +
                                                    "," + dboncall.StrDaftarHargaItem + ", " + dboncall.StrMultidrop + ", " + dboncall.IdDataTruck + ", " + dboncall.Driver1Id + ", " + dboncall.KeteranganDriver1 + ", " + dboncall.Driver2Id + ", " +
                                                    dboncall.KeteranganDriver2 + ", " + dboncall.IsCash + ", " + dboncall.KeteranganRek + ", " + dboncall.IdDriverTitip + ", " + dboncall.DN + ", " + dboncall.KeteranganDataTruck + ", " +
                                                    dboncall.AtmId + ");", "List Order", "Revisi Jenis Truk", UserPrincipal.id);
                    }
                    else if (dbso.SalesOrderPickupId.HasValue)
                    {
                        Context.SalesOrderPickup dbpickup    = new Context.SalesOrderPickup();
                        SalesOrderPickup         modelPickup = new SalesOrderPickup(dbso);
                        modelPickup.setDb(dbpickup);

                        dbpickup.SalesOrderPickupId = 0;
                        dbpickup.Urutan             = RepoSalesOrder.getUrutanPickup(modelPickup.TanggalPickup.Value) + 1;
                        dbpickup.SONumber           = RepoSalesOrder.generatePickup(modelPickup.TanggalPickup.Value, dbpickup.Urutan);
                        dbpickup.Keterangan         = "Revisi rute dari " + model.RuteLama + " ke " + model.RuteBaru + " - " + dbso.SalesOrderPickup.Keterangan;

                        dbpickup.SalesOrderPickupLoadingAdd.Clear();
                        foreach (SalesOrderLoadUnload item in model.ListLoad)
                        {
                            dbpickup.SalesOrderPickupLoadingAdd.Add(new Context.SalesOrderPickupLoadingAdd()
                            {
                                CustomerId = dbpickup.CustomerId,
                                CustomerLoadingAddressId = item.Id,
                                urutan   = item.urutan,
                                IsSelect = item.IsSelect
                            });
                        }

                        dbpickup.SalesOrderPickupUnLoadingAdd.Clear();
                        RepoAuditrail.saveDelAllSalesOrderPickupUnLoadingAddQuery(dbpickup, UserPrincipal.id);
                        foreach (SalesOrderLoadUnload item in model.ListUnload)
                        {
                            dbpickup.SalesOrderPickupUnLoadingAdd.Add(new Context.SalesOrderPickupUnLoadingAdd()
                            {
                                CustomerId = dbpickup.CustomerId,
                                CustomerUnloadingAddressId = item.Id,
                                urutan   = item.urutan,
                                IsSelect = item.IsSelect
                            });
                        }

                        dummySo.SalesOrderPickup = dbpickup;
                    }
                    else if (dbso.SalesOrderProsesKonsolidasiId.HasValue)
                    {
                        Context.SalesOrderProsesKonsolidasi dbkonsolidasi    = new Context.SalesOrderProsesKonsolidasi();
                        SalesOrderProsesKonsolidasi         modelKonsolidasi = new SalesOrderProsesKonsolidasi(dbso);
                        modelKonsolidasi.setDb(dbkonsolidasi);

                        dbkonsolidasi.SalesOrderProsesKonsolidasiId = 0;
                        try{
                            dbkonsolidasi.Urutan = RepoSalesOrder.getUrutanProsesKonsolidasi(modelKonsolidasi.TanggalMuat.Value) + 1;
                        }
                        catch (Exception e)
                        {
                            dbkonsolidasi.Urutan = 0;
                        }
                        dbkonsolidasi.SONumber           = RepoSalesOrder.generateProsesKonsolidasi(modelKonsolidasi.TanggalMuat.Value, dbkonsolidasi.Urutan);
                        dbkonsolidasi.DN                 = "DN" + dbkonsolidasi.SONumber;
                        dbkonsolidasi.IdDaftarHargaItem  = model.IdRute;
                        dbkonsolidasi.StrDaftarHargaItem = model.RuteBaru;
                        dbkonsolidasi.Multidrop          = model.MultidropBaru;
                        dbkonsolidasi.Keterangan         = "Revisi rute dari " + model.RuteLama + " ke " + model.RuteBaru + " - " + dbso.SalesOrderProsesKonsolidasi.Keterangan;

                        dummySo.SalesOrderProsesKonsolidasi = dbkonsolidasi;
                        RepoAuditrail.saveSalesOrderProsesKonsolidasiQuery(dummySo.SalesOrderProsesKonsolidasi, UserPrincipal.id);
                    }

                    dummySo.Id = 0;
                    RepoSalesOrder.save(dummySo);
                    if (dbso.SalesOrderPickupId.HasValue)
                    {
                        foreach (Context.SalesOrderPickupUnLoadingAdd sopula in dbso.SalesOrderPickup.SalesOrderPickupUnLoadingAdd)
                        {
                            RepoAuditrail.saveSalesOrderPickupUnLoadingAddQuery(sopula, UserPrincipal.id);
                        }
                    }
                }
                else
                {
                    if (dbso.SalesOrderOncallId.HasValue)
                    {
                        dbso.SalesOrderOncall.IdDaftarHargaItem  = model.IdRute;
                        dbso.SalesOrderOncall.StrDaftarHargaItem = model.RuteBaru;
                        dbso.SalesOrderOncall.StrMultidrop       = model.MultidropBaru;
                        RepoAuditrail.SetAuditTrail(
                            "UPDATE dbo.\"SalesOrderOncall\" SET \"IdDaftarHargaItem\" = " + model.IdRute + ", \"StrDaftarHargaItem\" = " + model.RuteBaru + ", \"StrMultidrop\" = " + model.MultidropBaru +
                            " WHERE \"SalesOrderOnCallId\" = " + dbso.SalesOrderOncallId + ";",
                            "List Order", "Revisi Rute", UserPrincipal.id
                            );
                    }
                    else if (dbso.SalesOrderPickupId.HasValue)
                    {
                        dbso.SalesOrderPickup.RuteId = model.IdRute;
                    }
                    else if (dbso.SalesOrderProsesKonsolidasiId.HasValue)
                    {
                        dbso.SalesOrderProsesKonsolidasi.IdDaftarHargaItem  = model.IdRute;
                        dbso.SalesOrderProsesKonsolidasi.StrDaftarHargaItem = model.RuteBaru;
                        dbso.SalesOrderProsesKonsolidasi.Multidrop          = model.MultidropBaru;
                        RepoAuditrail.saveUpdSalesOrderProsesKonsolidasiQuery(dbso.SalesOrderProsesKonsolidasi, UserPrincipal.id);
                    }
                    revisiRute.IdSalesOrder = model.IdSalesOrder.Value;
                    dbso.RuteRevised        = true;
                    dbso.AdminUangJalanId   = null;
                    dbso.AdminUangJalan     = null;
                    RepoSalesOrder.save(dbso);
                    string sod_guid = Guid.NewGuid().ToString();
                    SyncToERP(dummySo, sod_guid);
                    dbso.oidErp = sod_guid;
                    RepoRevisiRute.save(revisiRute);
                }

                return(RedirectToAction("Index", "ListOrder"));
            }

            model = new RevisiRute(dbso);

            if (dbso.SalesOrderOncallId.HasValue)
            {
                return(View("FormOncall", model));
            }
            else if (dbso.SalesOrderPickupId.HasValue)
            {
                return(View("FormPickup", model));
            }
            else if (dbso.SalesOrderProsesKonsolidasiId.HasValue)
            {
                return(View("FormKonsolidasi", model));
            }

            return(View(""));
        }
        public ActionResult Edit(AdminUangJalan model, string Keterangan, string IsTransfer)
        {
            Context.SalesOrder     dbitem = RepoSalesOrder.FindByPK(model.IdSalesOrder.Value);
            Context.AdminUangJalan dummyAdminUangJalan = dbitem.AdminUangJalan;
            Context.BatalOrder     batalOrder          = new Context.BatalOrder();
            string code        = "BT-" + (dbitem.SalesOrderOncallId.HasValue ? dbitem.SalesOrderOncall.SONumber : dbitem.SalesOrderProsesKonsolidasiId.HasValue ? dbitem.SalesOrderProsesKonsolidasi.SONumber : dbitem.SalesOrderPickup.SONumber);
            int?   urutanBatal = RepoBatalOrder.FindAll().Where(d => d.Code == code).Count() == 0 ? 1 : RepoBatalOrder.FindAll().Where(d => d.Code == code).Max(d => d.UrutanBatal) + 1;

            //cek status na
            Context.ERPConfig erpConfig = RepoERPConfig.FindByFrist();
            if (dbitem.Status == "dispatched")
            {
                Context.SettlementBatal dbsettlement = new Context.SettlementBatal();
                decimal?tambahanRute  = dummyAdminUangJalan.AdminUangJalanTambahanRute.Sum(s => s.values);
                decimal?boronganDasar = dummyAdminUangJalan.TotalAlokasi - dummyAdminUangJalan.Kawalan - dummyAdminUangJalan.Timbangan - dummyAdminUangJalan.Karantina - dummyAdminUangJalan.SPSI - dummyAdminUangJalan.Multidrop - tambahanRute - dummyAdminUangJalan.AdminUangJalanTambahanLain.Sum(s => s.Values);
                //ambil data
                dbsettlement.IdSalesOrder = model.IdSalesOrder;
                if (dummyAdminUangJalan.AdminUangJalanUangTf.Any(d => d.Keterangan == "Tunai"))
                {
                    dbsettlement.KasDiterima = dummyAdminUangJalan.AdminUangJalanUangTf.Where(d => d.Keterangan == "Tunai").FirstOrDefault().JumlahTransfer;
                }
                if (dummyAdminUangJalan.AdminUangJalanUangTf.Any(d => d.Keterangan.Contains("Transfer")))
                {
                    dbsettlement.TransferDiterima = dummyAdminUangJalan.AdminUangJalanUangTf.Where(d => d.Keterangan.Contains("Transfer")).Sum(t => t.JumlahTransfer);
                }
                dbsettlement.SolarDiterima = dummyAdminUangJalan.AdminUangJalanVoucherSpbu.Sum(s => s.Value);
                dbsettlement.KapalDiterima = dummyAdminUangJalan.AdminUangJalanVoucherKapal.Sum(s => s.Value);
                dbsettlement.JenisBatal    = "Batal Truk";
                dbsettlement.IdDriver      = dbitem.SalesOrderOncallId.HasValue ? dbitem.SalesOrderOncall.Driver1Id : dbitem.SalesOrderProsesKonsolidasiId.HasValue ? dbitem.SalesOrderProsesKonsolidasi.Driver1Id : dbitem.SalesOrderPickup.Driver1Id;
                dbsettlement.IdDataTruck   = dbitem.SalesOrderOncallId.HasValue ? dbitem.SalesOrderOncall.IdDataTruck : dbitem.SalesOrderProsesKonsolidasiId.HasValue ? dbitem.SalesOrderProsesKonsolidasi.IdDataTruck : dbitem.SalesOrderPickup.IdDataTruck;
                RepoSettBatal.save(dbsettlement, UserPrincipal.id, "Batal Truk");
                decimal?TotalBiaya = (dummyAdminUangJalan.KasbonDriver1 ?? 0) + (dummyAdminUangJalan.KlaimDriver1 ?? 0) + dummyAdminUangJalan.AdminUangJalanPotonganDriver.Select(d => d.Value).Sum() +
                                     dummyAdminUangJalan.AdminUangJalanVoucherKapal.Select(d => d.Value).Sum() + dummyAdminUangJalan.AdminUangJalanVoucherSpbu.Select(d => d.Value).Sum() +
                                     dummyAdminUangJalan.AdminUangJalanUangTf.Select(d => d.Value).Sum();
                decimal TotalUtangDriver = (dbsettlement.SolarDiterima == null ? 0 : dbsettlement.SolarDiterima.Value) + (dbsettlement.KapalDiterima == null ? 0 : dbsettlement.KapalDiterima.Value) + (dbsettlement.TransferDiterima == null ? 0 : dbsettlement.TransferDiterima.Value) + (dbsettlement.KasDiterima == null ? 0 : dbsettlement.KasDiterima.Value);
                Repoglt_det.saveFromAc(1, code + "-" + urutanBatal, TotalBiaya, 0, Repoac_mstr.FindByPk(erpConfig.IdAUJCredit));
                Repoglt_det.saveFromAc(2, code + "-" + urutanBatal, 0, boronganDasar, Repoac_mstr.FindByPk(erpConfig.IdBoronganDasar));
                Repoglt_det.saveFromAc(3, code + "-" + urutanBatal, 0, dummyAdminUangJalan.Kawalan, Repoac_mstr.FindByPk(erpConfig.IdKawalan));
                Repoglt_det.saveFromAc(4, code + "-" + urutanBatal, 0, dummyAdminUangJalan.Timbangan, Repoac_mstr.FindByPk(erpConfig.IdTimbangan));
                Repoglt_det.saveFromAc(5, code + "-" + urutanBatal, 0, dummyAdminUangJalan.Karantina, Repoac_mstr.FindByPk(erpConfig.IdKarantina));
                Repoglt_det.saveFromAc(6, code + "-" + urutanBatal, 0, dummyAdminUangJalan.SPSI, Repoac_mstr.FindByPk(erpConfig.IdSPSI));
                Repoglt_det.saveFromAc(7, code + "-" + urutanBatal, 0, dummyAdminUangJalan.Multidrop, Repoac_mstr.FindByPk(erpConfig.IdMultidrop));
                Repoglt_det.saveFromAc(8, code + "-" + urutanBatal, 0, tambahanRute + dummyAdminUangJalan.AdminUangJalanTambahanLain.Sum(s => s.Values), Repoac_mstr.FindByPk(erpConfig.IdTambahanRuteMuat));
                Repoglt_det.saveFromAc(9, code + "-" + urutanBatal, RepoSalesOrder.Harga(dbitem), 0, Repoac_mstr.FindByPk(erpConfig.IdPendapatanUsahaBlmInv));
                Repoglt_det.saveFromAc(10, code + "-" + urutanBatal, 0, RepoSalesOrder.Harga(dbitem), Repoac_mstr.FindByPk(erpConfig.IdPiutangDagang));
//                try {//masuklah ke saldo piutang driver, save to pby_mstr&det
                var glt_oid = Guid.NewGuid().ToString();
                Repopbyd_det.saveMstr(glt_oid, code + "-" + urutanBatal, erpConfig.IdAUJCredit.Value, "Batal Order " + code, dbsettlement.IdDriver.Value + 7000000);
                Repopbyd_det.save(
                    glt_oid, code + "-" + urutanBatal, erpConfig.IdAUJCredit.Value, "Batal Order " + code, dbsettlement.IdDriver.Value + 7000000, erpConfig.IdAUJCredit.Value, Repoac_mstr.FindByPk(erpConfig.IdAUJCredit).ac_name, TotalUtangDriver
                    );
                //              }
                //            catch (Exception e) {}
            }
            else if (dbitem.Status == "admin uang jalan")
            {
                //lebah dieu sync ERPna
                decimal?tambahanRute  = dummyAdminUangJalan.AdminUangJalanTambahanRute.Sum(s => s.values);
                decimal?boronganDasar = dummyAdminUangJalan.TotalAlokasi - dummyAdminUangJalan.Kawalan - dummyAdminUangJalan.Timbangan - dummyAdminUangJalan.Karantina - dummyAdminUangJalan.SPSI - dummyAdminUangJalan.Multidrop - tambahanRute - dummyAdminUangJalan.AdminUangJalanTambahanLain.Sum(s => s.Values);
                Repoglt_det.save(1, new Context.glt_det(), dbitem, code + "-" + urutanBatal, 0, boronganDasar, Repoac_mstr.FindByPk(erpConfig.IdBoronganDasar));
                Repoglt_det.save(2, new Context.glt_det(), dbitem, code + "-" + urutanBatal, 0, dummyAdminUangJalan.Kawalan, Repoac_mstr.FindByPk(erpConfig.IdKawalan));
                Repoglt_det.save(3, new Context.glt_det(), dbitem, code + "-" + urutanBatal, 0, dummyAdminUangJalan.Timbangan, Repoac_mstr.FindByPk(erpConfig.IdTimbangan));
                Repoglt_det.save(4, new Context.glt_det(), dbitem, code + "-" + urutanBatal, 0, dummyAdminUangJalan.Karantina, Repoac_mstr.FindByPk(erpConfig.IdKarantina));
                Repoglt_det.save(5, new Context.glt_det(), dbitem, code + "-" + urutanBatal, 0, dummyAdminUangJalan.SPSI, Repoac_mstr.FindByPk(erpConfig.IdSPSI));
                Repoglt_det.save(6, new Context.glt_det(), dbitem, code + "-" + urutanBatal, 0, dummyAdminUangJalan.Multidrop, Repoac_mstr.FindByPk(erpConfig.IdMultidrop));
                Repoglt_det.save(7, new Context.glt_det(), dbitem, code + "-" + urutanBatal, 0, tambahanRute, Repoac_mstr.FindByPk(erpConfig.IdTambahanRuteMuat));
                Repoglt_det.save(8, new Context.glt_det(), dbitem, code + "-" + urutanBatal, 0, dummyAdminUangJalan.AdminUangJalanTambahanLain.Sum(s => s.Values), Repoac_mstr.FindByPk(erpConfig.IdTambahanRuteLain));
                Repoglt_det.save(9, new Context.glt_det(), dbitem, code + "-" + urutanBatal, dummyAdminUangJalan.KasbonDriver1, 0, Repoac_mstr.FindByPk(erpConfig.IdKasbonAuj));
                Repoglt_det.save(10, new Context.glt_det(), dbitem, code + "-" + urutanBatal, dummyAdminUangJalan.KlaimDriver1, 0, Repoac_mstr.FindByPk(erpConfig.IdKlaimAuj));
                Repoglt_det.save(11, new Context.glt_det(), dbitem, code + "-" + urutanBatal, dummyAdminUangJalan.AdminUangJalanPotonganDriver.Sum(s => s.Value), 0, Repoac_mstr.FindByPk(erpConfig.IdPotonganLainAuj));
                Repoglt_det.save(12, new Context.glt_det(), dbitem, code + "-" + urutanBatal, dummyAdminUangJalan.AdminUangJalanVoucherKapal.Sum(s => s.Value), 0, Repoac_mstr.FindByPk(erpConfig.IdVoucherKapal));
                Repoglt_det.save(13, new Context.glt_det(), dbitem, code + "-" + urutanBatal, dummyAdminUangJalan.AdminUangJalanVoucherSpbu.Sum(s => s.Value), 0, Repoac_mstr.FindByPk(erpConfig.IdVoucherSolar));
                Repoglt_det.save(14, new Context.glt_det(), dbitem, code + "-" + urutanBatal, dummyAdminUangJalan.AdminUangJalanUangTf.Sum(s => s.Value), 0, Repoac_mstr.FindByPk(erpConfig.IdAUJCredit));
            }

            if (dbitem.Status != "batal order")
            {
                dbitem.Status      = "save";
                dbitem.IsBatalTruk = true;
                if (dbitem.SalesOrderOncallId.HasValue)
                {
                    dbitem.KeteranganBatal              = "Ganti truck dari " + dbitem.SalesOrderOncall.DataTruck.VehicleNo + " " + Keterangan; //Ganti truck dari <nopol sebelumnya > ke <nopol yang dipilih> [isi dari keterangan batal truck ]
                    dbitem.SalesOrderOncall.Keterangan  = "Ganti truck dari " + dbitem.SalesOrderOncall.DataTruck.VehicleNo + " " + Keterangan; //Ganti truck dari <nopol sebelumnya > ke <nopol yang dipilih> [isi dari keterangan batal truck ]
                    dbitem.SalesOrderOncall.Driver1Id   = null;
                    dbitem.SalesOrderOncall.Driver2Id   = null;
                    dbitem.SalesOrderOncall.IdDataTruck = null;
                    RepoAuditrail.SetAuditTrail(
                        "UPDATE dbo.\"SalesOrderOncall\" SET \"Driver1Id\" = NULL , Driver2Id = NULL, IdDataTruck = NULL WHERE \"SalesOrderOnCallId\" = " + dbitem.SalesOrderOncallId, "List Order", "Batal Truk", UserPrincipal.id
                        );
                }
                else if (dbitem.SalesOrderPickupId.HasValue)
                {
                    dbitem.KeteranganBatal              = "Ganti truck dari " + dbitem.SalesOrderPickup.DataTruck.VehicleNo + " " + Keterangan; //Ganti truck dari <nopol sebelumnya > ke <nopol yang dipilih> [isi dari keterangan batal truck ]
                    dbitem.SalesOrderPickup.Keterangan  = "Ganti truck dari " + dbitem.SalesOrderPickup.DataTruck.VehicleNo + " " + Keterangan; //Ganti truck dari <nopol sebelumnya > ke <nopol yang dipilih> [isi dari keterangan batal truck ]
                    dbitem.SalesOrderPickup.Driver1Id   = null;
                    dbitem.SalesOrderPickup.Driver2Id   = null;
                    dbitem.SalesOrderPickup.IdDataTruck = null;
                }
                else if (dbitem.SalesOrderProsesKonsolidasiId.HasValue)
                {
                    dbitem.KeteranganBatal = "Ganti truck dari " + dbitem.SalesOrderProsesKonsolidasi.DataTruck.VehicleNo + " " + Keterangan;                         //Ganti truck dari <nopol sebelumnya > ke <nopol yang dipilih> [isi dari keterangan batal truck ]
                    dbitem.SalesOrderProsesKonsolidasi.Keterangan  = "Ganti truck dari " + dbitem.SalesOrderProsesKonsolidasi.DataTruck.VehicleNo + " " + Keterangan; //Ganti truck dari <nopol sebelumnya > ke <nopol yang dipilih> [isi dari keterangan batal truck ]
                    dbitem.SalesOrderProsesKonsolidasi.Driver1Id   = null;
                    dbitem.SalesOrderProsesKonsolidasi.Driver2Id   = null;
                    dbitem.SalesOrderProsesKonsolidasi.IdDataTruck = null;
                }
                batalOrder.IdSalesOrder = dbitem.Id;
                batalOrder.Keterangan   = Keterangan;
                batalOrder.ModifiedDate = DateTime.Now;
                batalOrder.IsBatalTruk  = true;
                batalOrder.Code         = code;
                batalOrder.UrutanBatal  = urutanBatal;
                batalOrder.IsTransfer   = IsTransfer == "1";
                RepoSalesOrder.save(dbitem);
                RepoBatalOrder.save(batalOrder, UserPrincipal.id);

                return(RedirectToAction("index", "ListOrder"));
            }
            ViewBag.status = "batal order";
            return(View("Form", model));
        }
        public ActionResult Edit(AdminUangJalan model, string Keterangan, string KeteranganBatal)
        {
            Context.SalesOrder     dbitem = RepoSalesOrder.FindByPK(model.IdSalesOrder.Value);
            Context.AdminUangJalan dummyAdminUangJalan = dbitem.AdminUangJalan;
            Context.BatalOrder     batalOrder          = new Context.BatalOrder();
            string code = "BO-" + (
                dbitem.SalesOrderOncallId.HasValue ? dbitem.SalesOrderOncall.SONumber : dbitem.SalesOrderProsesKonsolidasiId.HasValue ? dbitem.SalesOrderProsesKonsolidasi.SONumber : dbitem.SalesOrderPickupId.HasValue ?
                dbitem.SalesOrderPickup.SONumber : dbitem.SalesOrderKontrak.SONumber
                );

            //lebah dieu sync ERPna
            Context.ERPConfig erpConfig = RepoERPConfig.FindByFrist();
            //cek status na
            if (dbitem.Status == "dispatched")
            {
                decimal?tambahanRute  = dummyAdminUangJalan.AdminUangJalanTambahanRute.Sum(s => s.values);
                decimal?boronganDasar = dummyAdminUangJalan.TotalAlokasi - dummyAdminUangJalan.Kawalan - dummyAdminUangJalan.Timbangan - dummyAdminUangJalan.Karantina - dummyAdminUangJalan.SPSI -
                                        dummyAdminUangJalan.Multidrop - tambahanRute - dummyAdminUangJalan.AdminUangJalanTambahanLain.Sum(s => s.Values);
                Context.SettlementBatal dbsettlement = new Context.SettlementBatal();
                //ambil data
                dbsettlement.IdSalesOrder = model.IdSalesOrder;
                if (dummyAdminUangJalan.AdminUangJalanUangTf.Any(d => d.Keterangan == "Tunai"))
                {
                    dbsettlement.KasDiterima = dummyAdminUangJalan.AdminUangJalanUangTf.Where(d => d.Keterangan == "Tunai").FirstOrDefault().JumlahTransfer;
                }
                if (dummyAdminUangJalan.AdminUangJalanUangTf.Any(d => d.Keterangan.Contains("Transfer")))
                {
                    dbsettlement.TransferDiterima = dummyAdminUangJalan.AdminUangJalanUangTf.Where(d => d.Keterangan.Contains("Transfer")).Sum(t => t.JumlahTransfer);
                }
                dbsettlement.SolarDiterima = dummyAdminUangJalan.AdminUangJalanVoucherSpbu.Sum(s => s.Value);
                dbsettlement.KapalDiterima = dummyAdminUangJalan.AdminUangJalanVoucherKapal.Sum(s => s.Value);
                dbsettlement.JenisBatal    = "Batal Order";
                dbsettlement.IdDriver      = dbitem.SalesOrderOncallId.HasValue ? dbitem.SalesOrderOncall.Driver1Id : dbitem.SalesOrderProsesKonsolidasiId.HasValue ?
                                             dbitem.SalesOrderProsesKonsolidasi.Driver1Id : dbitem.SalesOrderPickup.Driver1Id;
                RepoSettBatal.save(dbsettlement, UserPrincipal.id, "Batal Order");
                decimal?TotalBiaya = (dummyAdminUangJalan.KasbonDriver1 ?? 0) + (dummyAdminUangJalan.KlaimDriver1 ?? 0) + dummyAdminUangJalan.AdminUangJalanPotonganDriver.Select(d => d.Value).Sum() +
                                     dummyAdminUangJalan.AdminUangJalanVoucherKapal.Select(d => d.Value).Sum() + dummyAdminUangJalan.AdminUangJalanVoucherSpbu.Select(d => d.Value).Sum() +
                                     dummyAdminUangJalan.AdminUangJalanUangTf.Select(d => d.Value).Sum();
                decimal TotalUtangDriver = (dbsettlement.SolarDiterima == null ? 0 : dbsettlement.SolarDiterima.Value) + (dbsettlement.KapalDiterima == null ? 0 : dbsettlement.KapalDiterima.Value) +
                                           (dbsettlement.TransferDiterima == null ? 0 : dbsettlement.TransferDiterima.Value) + (dbsettlement.KasDiterima == null ? 0 : dbsettlement.KasDiterima.Value);
                Repoglt_det.saveFromAc(1, code, TotalBiaya, 0, Repoac_mstr.FindByPk(erpConfig.IdAUJCredit));
                Repoglt_det.saveFromAc(2, code, 0, boronganDasar, Repoac_mstr.FindByPk(erpConfig.IdBoronganDasar));
                Repoglt_det.saveFromAc(3, code, 0, dummyAdminUangJalan.Kawalan, Repoac_mstr.FindByPk(erpConfig.IdKawalan));
                Repoglt_det.saveFromAc(4, code, 0, dummyAdminUangJalan.Timbangan, Repoac_mstr.FindByPk(erpConfig.IdTimbangan));
                Repoglt_det.saveFromAc(5, code, 0, dummyAdminUangJalan.Karantina, Repoac_mstr.FindByPk(erpConfig.IdKarantina));
                Repoglt_det.saveFromAc(6, code, 0, dummyAdminUangJalan.SPSI, Repoac_mstr.FindByPk(erpConfig.IdSPSI));
                Repoglt_det.saveFromAc(7, code, 0, dummyAdminUangJalan.Multidrop, Repoac_mstr.FindByPk(erpConfig.IdMultidrop));
                Repoglt_det.saveFromAc(8, code, 0, tambahanRute + dummyAdminUangJalan.AdminUangJalanTambahanLain.Sum(s => s.Values), Repoac_mstr.FindByPk(erpConfig.IdTambahanRuteMuat));
                Repoglt_det.saveFromAc(9, code, RepoSalesOrder.Harga(dbitem), 0, Repoac_mstr.FindByPk(erpConfig.IdPendapatanUsahaBlmInv));
                Repoglt_det.saveFromAc(10, code, 0, RepoSalesOrder.Harga(dbitem), Repoac_mstr.FindByPk(erpConfig.IdPiutangDagang));
//                try {//masuklah ke saldo piutang driver, save to pby_mstr&det
                var glt_oid = Guid.NewGuid().ToString();
                Repopbyd_det.saveMstr(glt_oid, code, erpConfig.IdAUJCredit.Value, "Batal Order " + code, dbsettlement.IdDriver.Value + 7000000);
                Repopbyd_det.save(
                    glt_oid, code, erpConfig.IdAUJCredit.Value, "Batal Order " + code, dbsettlement.IdDriver.Value + 7000000,
                    erpConfig.IdAUJCredit.Value, Repoac_mstr.FindByPk(erpConfig.IdAUJCredit).ac_name, TotalUtangDriver
                    );
                //              }
                //            catch (Exception e) {}
            }
            else if (dbitem.Status == "admin uang jalan")
            {
                decimal?tambahanRute  = dummyAdminUangJalan.AdminUangJalanTambahanRute.Sum(s => s.values);
                decimal?boronganDasar = dummyAdminUangJalan.TotalAlokasi - dummyAdminUangJalan.Kawalan - dummyAdminUangJalan.Timbangan - dummyAdminUangJalan.Karantina - dummyAdminUangJalan.SPSI - dummyAdminUangJalan.Multidrop - tambahanRute - dummyAdminUangJalan.AdminUangJalanTambahanLain.Sum(s => s.Values);
                Repoglt_det.saveFromAc(
                    1, code,
                    (dummyAdminUangJalan.KasbonDriver1 ?? 0) + (dummyAdminUangJalan.KlaimDriver1 ?? 0) + dummyAdminUangJalan.AdminUangJalanPotonganDriver.Select(d => d.Value).Sum() + dummyAdminUangJalan.AdminUangJalanVoucherKapal.Select(d => d.Value).Sum() +
                    dummyAdminUangJalan.AdminUangJalanVoucherSpbu.Select(d => d.Value).Sum() + dummyAdminUangJalan.AdminUangJalanUangTf.Select(d => d.Value).Sum(), 0,
                    Repoac_mstr.FindByPk(erpConfig.IdAUJCredit)
                    );
                Repoglt_det.saveFromAc(2, code, 0, boronganDasar, Repoac_mstr.FindByPk(erpConfig.IdBoronganDasar));
                Repoglt_det.saveFromAc(3, code, 0, dummyAdminUangJalan.Kawalan, Repoac_mstr.FindByPk(erpConfig.IdKawalan));
                Repoglt_det.saveFromAc(4, code, 0, dummyAdminUangJalan.Timbangan, Repoac_mstr.FindByPk(erpConfig.IdTimbangan));
                Repoglt_det.saveFromAc(5, code, 0, dummyAdminUangJalan.Karantina, Repoac_mstr.FindByPk(erpConfig.IdKarantina));
                Repoglt_det.saveFromAc(6, code, 0, dummyAdminUangJalan.SPSI, Repoac_mstr.FindByPk(erpConfig.IdSPSI));
                Repoglt_det.saveFromAc(7, code, 0, dummyAdminUangJalan.Multidrop, Repoac_mstr.FindByPk(erpConfig.IdMultidrop));
                Repoglt_det.saveFromAc(8, code, 0, tambahanRute + dummyAdminUangJalan.AdminUangJalanTambahanLain.Sum(s => s.Values), Repoac_mstr.FindByPk(erpConfig.IdTambahanRuteMuat));
            }

            dbitem.Status           = "batal order";
            dbitem.UpdatedBy        = UserPrincipal.id;
            batalOrder.IdSalesOrder = dbitem.Id;
            batalOrder.Keterangan   = Keterangan;
            dbitem.KeteranganBatal  = KeteranganBatal;
            batalOrder.ModifiedDate = DateTime.Now;
            batalOrder.Code         = code;
            RepoSalesOrder.save(dbitem);
            RepoAuditrail.saveBOHistory(dbitem);

            return(RedirectToAction("index", "ListOrder"));
        }