Beispiel #1
0
        public ActionResult Add(Klaim model)
        {
            KlaimProduct[] result = JsonConvert.DeserializeObject <KlaimProduct[]>(model.strProduk);
            model.ListProduct = result.ToList();
            KlaimAttachment[] resultAtt = JsonConvert.DeserializeObject <KlaimAttachment[]>(model.StrAtt);
            model.ListAtt = resultAtt.ToList();
            if (ModelState.IsValid && IsValid(model, ModelState))
            {
                Context.Klaim dbitem = new Context.Klaim();
                int           seq    = RepoKlaim.getUrutanOnCAll(model.TanggalPengajuan.Value);
                dbitem.NoKlaim = RepoKlaim.GenerateCode(model.TanggalPengajuan.Value, seq);
                bool Isexist = RepoKlaim.IsExist(model.NoKlaim);

                if (Isexist)
                {
                    ModelState.AddModelError("NoKlaim", "No Klaim telah dipakai.");
                    return(View("Form", model));
                }
                if (model.BebanClaimDriver + model.BebanClaimKantor != model.BebanClaim)
                {
                    ModelState.AddModelError("BebanClaimDriver", "Beban Klaim Driver + Beban Klaim Kantor harus sama dengan Beban Klaim");
                    return(View("Form", model));
                }

                model.setDb(dbitem);
                RepoKlaim.save(dbitem, UserPrincipal.id);
                //tambah beban klaim nya driver
                int idDriver            = 0;
                Context.SalesOrder dbso = RepoSalesOrder.FindByPK(model.SOKlaimId.Value);
                if (dbitem.SalesOrderKontrakId == 0)
                {
                    idDriver = dbso.AdminUangJalan.IdDriver1.Value;
                }
                else
                {
                    idDriver = dbso.SalesOrderKontrak.SalesOrderKontrakListSo.Where(
                        d => d.Id == dbitem.SalesOrderKontrakId
                        ).FirstOrDefault().Driver1Id.Value;
                }
                Context.BebanKlaimDriver dbKlaimDriver = new Context.BebanKlaimDriver()
                {
                    IdDriver = idDriver,
                    Klaim    = dbitem
                };
                RepoKlaimDriver.save(dbKlaimDriver, UserPrincipal.id);
                //lebah dieu sync ERPna
                Context.ERPConfig erpConfig = RepoERPConfig.FindByFrist();
                Repoglt_det.saveFromAc(1, dbitem.NoKlaim, decimal.Parse(dbitem.TotalPengajuanClaim.Value.ToString()), 0, Repoac_mstr.FindByPk(erpConfig.IdBiayaKlaim));  //D
                Repoglt_det.saveFromAc(2, dbitem.NoKlaim, 0, decimal.Parse(dbitem.TotalPengajuanClaim.Value.ToString()), Repoac_mstr.FindByPk(erpConfig.IdKreditKlaim)); //K
                return(RedirectToAction("Index"));
            }
            return(View("Form", model));
        }
 public ActionResult batalInap(SolarInap model)
 {
     if (ModelState.IsValid)
     {
         Context.SolarInap  dbitem = RepoSolarInap.FindByPK(model.Id);
         Context.SalesOrder dbso   = RepoSalesOrder.FindByPK(model.IdSO.Value);
         model.setDbSolarInap(dbitem);
         RepoSolarInap.save(dbitem, UserPrincipal.id);
         if (dbitem.Transfer > 0)
         {
             //#Jurnal Kasbon Driver pada Biaya
             string            so_code   = (dbso.SalesOrderOncallId.HasValue ? dbso.SalesOrderOncall.SONumber : dbso.SalesOrderProsesKonsolidasiId.HasValue ? dbso.SalesOrderProsesKonsolidasi.SONumber : dbso.SalesOrderPickupId.HasValue ? dbso.SalesOrderPickup.SONumber : dbso.SalesOrderKontrak.SONumber);
             string            code      = "SIBI-" + dbitem.TanggalDari.ToString() + so_code;
             Context.ERPConfig erpConfig = RepoERPConfig.FindByFrist();
             Repoglt_det.saveFromAc(1, code, model.Transfer, 0, Repoac_mstr.FindByPk(erpConfig.IdKasbonDriver)); //D, Kasbon Driver
             Repoglt_det.saveFromAc(2, code, 0, model.Transfer, Repoac_mstr.FindByPk(erpConfig.IdBiayaInap));    //K, Biaya Inap
             //masuklah ke saldo piutang driver, save to pby_mstr&det
             try{
                 var glt_oid = Guid.NewGuid().ToString();
                 Repopbyd_det.saveMstr(glt_oid, dbitem.Code, erpConfig.IdTfCredit.Value, "Batal Inap " + code, dbitem.IdDriver.Value);
                 Repopbyd_det.save(glt_oid, dbitem.Code, erpConfig.IdTfCredit.Value, "Batal Inap " + code, dbitem.IdDriver.Value, erpConfig.IdKasbonDriver.Value, Repoac_mstr.FindByPk(erpConfig.IdKasbonDriver).ac_name, model.Transfer);
             }
             catch (Exception e)
             {
             }
         }
         return(RedirectToAction("Index"));
     }
     return(View("batalInap", model));
 }
        public JsonResult SaveDp(Context.SalesOrderOncallDp model)
        {
            Context.SalesOrder dbitem = RepoSalesOrder.FindByOnCall(model.SalesOrderOnCallId);
            if (model.Id == 0)
            {
                dbitem.SalesOrderOncall.SalesOrderOncallDp.Add(model);
                var query = "INSERT INTO \"dbo\".\"SalesOrderOncallDp\" (\"SalesOrderOnCallId\", \"Tanggal\", \"Penerima\", \"Jenis\", \"RekeningId\", \"Jumlah\") VALUES (" + model.SalesOrderOnCallId +
                            ", " + model.Tanggal + ", " + model.Penerima + ", " + model.Jenis + ", " + model.RekeningId + ", " + model.Jumlah + ");";
                RepoAuditrail.SetAuditTrail(query, "Input DP", "List Order", UserPrincipal.id);
            }
            else
            {
                Context.SalesOrderOncallDp dbdp = dbitem.SalesOrderOncall.SalesOrderOncallDp.Where(d => d.Id == model.Id).FirstOrDefault();
                dbdp.Tanggal    = model.Tanggal;
                dbdp.Penerima   = model.Penerima;
                dbdp.RekeningId = model.RekeningId;
                dbdp.Jenis      = model.Jenis;
                dbdp.Jumlah     = model.Jumlah;
                var query = "UPDATE \"dbo\".\"SalesOrderOncallDp\" SET \"Tanggal\" = " + dbdp.Tanggal + ", \"Penerima\" = " + dbdp.Penerima + ", \"Jenis\" = \" = " + dbdp.Jenis +
                            ", \"RekeningId\" = " + dbdp.RekeningId + "\"Jumlah\" = " + dbdp.Jumlah + " WHERE \"Id\" = " + dbdp.Id + ";";
                RepoAuditrail.SetAuditTrail(query, "Input DP", "List Order", UserPrincipal.id);
            }

            RepoSalesOrder.save(dbitem);
            //lebah dieu sync ERPna
            Context.ERPConfig erpConfig = RepoERPConfig.FindByFrist();
            string            code      = "DP-" + (dbitem.SalesOrderOncallId.HasValue ? dbitem.SalesOrderOncall.SONumber : dbitem.SalesOrderProsesKonsolidasiId.HasValue ? dbitem.SalesOrderProsesKonsolidasi.SONumber : dbitem.SalesOrderPickupId.HasValue ? dbitem.SalesOrderPickup.SONumber : dbitem.SalesOrderKontrak.SONumber);

            //Jurnal
            Repoglt_det.saveFromAc(1, code, model.Jumlah, 0, Repoac_mstr.FindByPk(erpConfig.IdCashCredit)); //Cash
            Repoglt_det.saveFromAc(2, code, 0, model.Jumlah, Repoac_mstr.FindByPk(erpConfig.IdDP));         //Uang Di Muka
            //Tambah Saldo Customer
            Context.ptnr_mstr dbptnr = Repoptnr_mstr.FindByPK(dbitem.Id);

            ResponeModel response = new ResponeModel(true);

            return(Json(response));
        }
        public ActionResult Edit(SettlementBatal model)
        {
            if (ModelState.IsValid)
            {
                Context.SettlementBatal dbitem = RepoSettlementBatal.FindByPK(model.Id);
                if (dbitem.IdSoKontrak == "" || dbitem.IdSoKontrak == null)
                {
                    Context.SalesOrder     dbso      = RepoSalesOrder.FindByPK(model.IdSalesOrder.Value);
                    Context.AdminUangJalan db        = RepoAdminUangJalan.FindByPK(dbso.AdminUangJalanId.Value);
                    Context.ERPConfig      erpConfig = RepoERPConfig.FindByFrist();
                    dbitem.Code = "SB-" + (dbso.SalesOrderOncallId.HasValue ? dbso.SalesOrderOncall.SONumber : dbso.SalesOrderProsesKonsolidasiId.HasValue ?
                                           dbso.SalesOrderProsesKonsolidasi.SONumber : dbso.SalesOrderPickupId.HasValue ? dbso.SalesOrderPickup.SONumber : dbso.SalesOrderKontrak.SONumber);
                    model.SetDb(dbitem);

                    //Jurnal Pengembalian Uang Jalan //(uang, dll dikembalikan utuh), mau utuh mau ngga, nominal cash yg dikembalikan mah ambil dari form :p
                    decimal nominalHutangUangJalanDriver = (model.KasAktual == null ? 0 : model.KasAktual.Value) + (model.TransferAktual == null ? 0 : model.TransferAktual.Value);
                    decimal nomDiakui  = (model.KasDiakui == null ? 0 : model.KasDiakui.Value) + (model.TransferDiakui == null ? 0 : model.TransferDiakui.Value) + (model.SolarDiakui == null ? 0 : model.SolarDiakui.Value) + (model.KapalDiakui == null ? 0 : model.KapalDiakui.Value);
                    decimal nomSelisih = (dbitem.KasSelisih == null ? 0 : dbitem.KasSelisih.Value) + (dbitem.TransferSelisih == null ? 0 : dbitem.TransferSelisih.Value) + (dbitem.KapalSelisih == null ? 0 : dbitem.KapalSelisih.Value);

                    nominalHutangUangJalanDriver += decimal.Parse(db.AdminUangJalanVoucherSpbu.Select(d => d.Value).Sum().ToString());
                    nominalHutangUangJalanDriver += decimal.Parse(db.AdminUangJalanVoucherKapal.Select(d => d.Value).Sum().ToString());
                    nominalHutangUangJalanDriver += db.KasbonDriver1 == null ? 0 : db.KasbonDriver1.Value;
                    nominalHutangUangJalanDriver += db.KlaimDriver1 == null ? 0 : db.KlaimDriver1.Value;
                    nominalHutangUangJalanDriver += decimal.Parse(db.AdminUangJalanPotonganDriver.Select(d => d.Value).Sum().ToString());
                    decimal?TotalBiaya = nomDiakui;
                    int     idx        = 2;
                    if (nomDiakui > 0)
                    {
                        Repoglt_det.saveFromAc(2, "SB-" + dbso.SalesOrderOncall.SONumber, nomDiakui, 0, Repoac_mstr.FindByPk(erpConfig.IdBiayaBatalJalan));//BIAYA BATAL JALAN
                    }
                    foreach (Context.AdminUangJalanVoucherSpbu aujvs in db.AdminUangJalanVoucherSpbu)
                    {
                        idx++;
                        if (model.SPBUKembali != null && model.SPBUKembali.Contains(aujvs.Keterangan))                                                                                //jurnal balik, ga jadi hutangnya
                        {
                            Repoglt_det.saveFromAc(idx, "SB-" + dbso.SalesOrderOncall.SONumber, aujvs.Value, 0, Repoac_mstr.FindByPk(RepoLookup.FindByName(aujvs.Keterangan).ac_id)); //HUTANG SPBU 34.171.04 Pangkalan 2 dan atau
                            TotalBiaya += aujvs.Value;
                        }
                        else //jadi hutang SPBU, masuk ke AP Inv
                        {
                            string           a        = aujvs.Keterangan;
                            string           username = UserPrincipal.username;
                            int?             vend     = RepoLookup.FindByName(aujvs.Keterangan).VendorId;
                            int              vendorId = RepoLookup.FindByName(aujvs.Keterangan).VendorId.Value;
                            Context.Customer cust     = RepoCustomer.FindByPK(RepoLookup.FindByName(aujvs.Keterangan).VendorId.Value);
                            Repogr_mstr.save(aujvs.Value, cust, username, dbitem.Code);
                        }
                    }
                    Repoglt_det.saveFromAc(idx, "SB-" + dbso.SalesOrderOncall.SONumber, dbitem.SolarSelisih, 0, Repoac_mstr.FindByPk(erpConfig.IdPiutangDriverBatalJalanSementaraSolar));
                    TotalBiaya += dbitem.SolarSelisih;
                    if (dbitem.KapalAktual > 0)
                    {
                        foreach (Context.AdminUangJalanVoucherKapal aujvs in db.AdminUangJalanVoucherKapal)
                        {
                            idx++;
                            Repoglt_det.saveFromAc(idx, "SB-" + dbso.SalesOrderOncall.SONumber, aujvs.Value, 0, Repoac_mstr.FindByPk(RepoLookup.FindByName(aujvs.Keterangan).ac_id));//HUTANG SPBU 34.171.04 Pangkalan 2 dan atau
                            TotalBiaya += aujvs.Value;
                        }
                    }
//                if (nomSelisih > 0){
                    idx++;
                    Repoglt_det.saveFromAc(idx, "SB-" + dbso.SalesOrderOncall.SONumber, nomSelisih, 0, Repoac_mstr.FindByPk(erpConfig.IdPiutangDriverBatalJalan), "Selisih Kas/Transfer/Kapal");//PIUTANG DRIVER BATAL JALAN
                    TotalBiaya += nomSelisih;
                    //              }
                    //mau utuh mau ngga, nominal cash yg dikembalikan mah ambil dari form :p
                    Repoglt_det.saveFromAc(2, "SB-" + dbso.SalesOrderOncall.SONumber, model.KasAktual, 0, Repoac_mstr.FindByPk(RepoMasterPool.FindByIPAddress().IdCreditCash)); //BCA Audy 386-7957777 atau
                    Repoglt_det.saveFromAc(2, "SB-" + dbso.SalesOrderOncall.SONumber, model.TransferAktual, 0, Repoac_mstr.FindByPk(dbitem.IdCreditTf));                        //BCA Audy 386-7957777 atau
                    TotalBiaya += (model.KasAktual + model.TransferAktual);
                    Repoglt_det.saveFromAc(1, "SB-" + dbso.SalesOrderOncall.SONumber, 0, TotalBiaya, Repoac_mstr.FindByPk(erpConfig.IdAUJCredit));                              //Hutang Uang Jalan Driver

                    RepoSettlementBatal.save(dbitem, UserPrincipal.id, "Settlement Batal");
                    try {//masuklah ke saldo piutang driver, save to pby_mstr&det
                        var glt_oid = Guid.NewGuid().ToString();
                        Repopbyd_det.saveMstr(glt_oid, dbitem.Code, erpConfig.IdAUJCredit.Value, "Settlement Batal " + dbitem.Code, db.IdDriver1.Value + 7000000);
                        Repopbyd_det.save(
                            glt_oid, dbitem.Code, erpConfig.IdAUJCredit.Value, "Settlement Batal " + dbitem.Code, db.IdDriver1.Value + 7000000,
                            erpConfig.IdAUJCredit.Value, Repoac_mstr.FindByPk(erpConfig.IdAUJCredit).ac_name, 0
                            );
                    }
                    catch (Exception e) {}
                }
                else
                {
                    Context.SalesOrder dbso = RepoSalesOrder.FindByPK(model.IdSalesOrder.Value);
                    Context.SalesOrderKontrakListSo dbKontrak = dbso.SalesOrderKontrak.SalesOrderKontrakListSo.Where(d => d.Id == int.Parse(dbitem.IdSoKontrak)).FirstOrDefault();
                    Context.AdminUangJalan          db        = RepoAdminUangJalan.FindByPK(dbKontrak.IdAdminUangJalan.Value);
                    Context.ERPConfig erpConfig = RepoERPConfig.FindByFrist();
                    dbitem.Code = "SB-" + (dbso.SalesOrderOncallId.HasValue ? dbso.SalesOrderOncall.SONumber : dbso.SalesOrderProsesKonsolidasiId.HasValue ?
                                           dbso.SalesOrderProsesKonsolidasi.SONumber : dbso.SalesOrderPickupId.HasValue ? dbso.SalesOrderPickup.SONumber : dbso.SalesOrderKontrak.SONumber);
                    model.SetDb(dbitem);

                    //Jurnal Pengembalian Uang Jalan //(uang, dll dikembalikan utuh), mau utuh mau ngga, nominal cash yg dikembalikan mah ambil dari form :p
                    decimal nominalHutangUangJalanDriver = model.KasAktual ?? model.KasAktual.Value + model.TransferAktual ?? model.TransferAktual.Value;
                    decimal nomDiakui  = model.KasDiakui ?? model.KasDiakui.Value + model.TransferDiakui ?? model.TransferDiakui.Value + model.SolarDiakui ?? model.SolarDiakui.Value + model.KapalDiakui ?? model.KapalDiakui.Value;
                    decimal nomSelisih = dbitem.KasSelisih.Value + dbitem.TransferSelisih.Value + dbitem.KapalSelisih.Value;

                    nominalHutangUangJalanDriver += decimal.Parse(db.AdminUangJalanVoucherSpbu.Select(d => d.Value).Sum().ToString());
                    nominalHutangUangJalanDriver += decimal.Parse(db.AdminUangJalanVoucherKapal.Select(d => d.Value).Sum().ToString());
                    nominalHutangUangJalanDriver += db.KasbonDriver1 == null ? 0 : db.KasbonDriver1.Value;
                    nominalHutangUangJalanDriver += db.KlaimDriver1 == null ? 0 : db.KlaimDriver1.Value;
                    nominalHutangUangJalanDriver += decimal.Parse(db.AdminUangJalanPotonganDriver.Select(d => d.Value).Sum().ToString());
                    int idx = 2;
                    Repoglt_det.saveFromAc(1, "SB-" + dbKontrak.NoSo, 0, nominalHutangUangJalanDriver, Repoac_mstr.FindByPk(erpConfig.IdAUJCredit));//Hutang Uang Jalan Driver
                    if (nomDiakui > 0)
                    {
                        Repoglt_det.saveFromAc(2, "SB-" + dbKontrak.NoSo, nomDiakui, 0, Repoac_mstr.FindByPk(erpConfig.IdBiayaBatalJalan));//BIAYA BATAL JALAN
                    }
                    foreach (Context.AdminUangJalanVoucherSpbu aujvs in db.AdminUangJalanVoucherSpbu)
                    {
                        idx++;
                        if (model.SPBUKembali.Contains(aujvs.Keterangan))
                        {
                            Repoglt_det.saveFromAc(idx, "SB-" + dbKontrak.NoSo, aujvs.Value, 0, Repoac_mstr.FindByPk(RepoLookup.FindByName(aujvs.Keterangan).ac_id));//HUTANG SPBU 34.171.04 Pangkalan 2 dan atau
                        }
                        else
                        {
                            Repoglt_det.saveFromAc(idx, "SB-" + dbKontrak.NoSo, aujvs.Value, 0, Repoac_mstr.FindByPk(erpConfig.IdPiutangDriverBatalJalanSementaraSolar), "Voucher SPBU " + aujvs.Keterangan);
                        }
                    }
                    foreach (Context.AdminUangJalanVoucherKapal aujvs in db.AdminUangJalanVoucherKapal)
                    {
                        idx++;
                        Repoglt_det.saveFromAc(idx, "SB-" + dbKontrak.NoSo, aujvs.Value, 0, Repoac_mstr.FindByPk(RepoLookup.FindByName(aujvs.Keterangan).ac_id));//HUTANG SPBU 34.171.04 Pangkalan 2 dan atau
                    }
//                if (nomSelisih > 0){
                    idx++;
                    Repoglt_det.saveFromAc(idx, "SB-" + dbKontrak.NoSo, nomSelisih, 0, Repoac_mstr.FindByPk(erpConfig.IdPiutangDriverBatalJalan), "Selisih Kas/Transfer/Kapal");//PIUTANG DRIVER BATAL JALAN
                    //              }
                    if (db.PotonganB > 0)
                    {
                        idx++;
                        Repoglt_det.saveFromAc(idx, "SB-" + dbKontrak.NoSo, db.PotonganB, 0, Repoac_mstr.FindByPk(erpConfig.IdPiutangDriverBatalJalan));//PIUTANG DRIVER BATAL JALAN
                    }
                    if (db.PotonganP > 0)
                    {
                        idx++;
                        Repoglt_det.saveFromAc(idx, "SB-" + dbKontrak.NoSo, db.PotonganP, 0, Repoac_mstr.FindByPk(erpConfig.IdPiutangDriverPribadi));//PIUTANG DRIVER BATAL JALAN / SEMENTARA TUNAI
                    }
                    if (db.PotonganK > 0)
                    {
                        idx++;
                        Repoglt_det.saveFromAc(idx, "SB-" + dbKontrak.NoSo, db.PotonganK, 0, Repoac_mstr.FindByPk(erpConfig.IdPendapatanPengembalianPiutangDriver));
                    }
                    if (db.PotonganT > 0)
                    {
                        idx++;
                        Repoglt_det.saveFromAc(idx, "SB-" + dbKontrak.NoSo, db.PotonganT, 0, Repoac_mstr.FindByPk(erpConfig.IdTabunganDriver));
                    }

                    //mau utuh mau ngga, nominal cash yg dikembalikan mah ambil dari form :p
                    Repoglt_det.saveFromAc(2, "SB-" + dbKontrak.NoSo, model.KasAktual, 0, Repoac_mstr.FindByPk(RepoMasterPool.FindByIPAddress().IdCreditCash));            //BCA Audy 386-7957777 atau
                    Repoglt_det.saveFromAc(2, "SB-" + dbKontrak.NoSo, model.TransferAktual, 0, Repoac_mstr.FindByPk(db.AdminUangJalanUangTf.FirstOrDefault().IdCreditTf)); //BCA Audy 386-7957777 atau

                    RepoSettlementBatal.save(dbitem, UserPrincipal.id, "Settlement Batal");
                    try {//masuklah ke saldo piutang driver, save to pby_mstr&det
                        var glt_oid = Guid.NewGuid().ToString();
                        Repopbyd_det.saveMstr(glt_oid, dbitem.Code, erpConfig.IdAUJCredit.Value, "Settlement Batal " + dbitem.Code, db.IdDriver1.Value + 7000000);
                        Repopbyd_det.save(
                            glt_oid, dbitem.Code, erpConfig.IdAUJCredit.Value, "Settlement Batal " + dbitem.Code, db.IdDriver1.Value + 7000000,
                            erpConfig.IdAUJCredit.Value, Repoac_mstr.FindByPk(erpConfig.IdAUJCredit).ac_name, 0
                            );
                    }
                    catch (Exception e) {}
                }
                return(RedirectToAction("Index"));
            }

            return(View("Form", 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));
        }
        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"));
        }