Esempio n. 1
0
        private void ImportData(int[] ids)
        {
            Form thisx = this;

            if (ipProgress == null)
            {
                ipProgress = new InPopup(this, pnlProgress);
            }
            if (fpProgress == null)
            {
                fpProgress = new FakeProgress(progbProgress);
            }

            BackgroundWorker bgw = new BackgroundWorker();

            bgw.DoWork += (a, b) =>
            {
                fpProgress.Start();

                using (var db = new Database())
                {
                    if (bgw.CancellationPending)
                    {
                        return;
                    }

                    db.Commands.Add(db.CreateCommand("[usp_AppSetting_LIST]"));
                    db.Commands[0].Parameters.Add(new Parameter("@Key", SqlDbType.VarChar, "WiserDC_Host"));
                    DataTable dtbl2 = db.Commands[0].ExecuteDataTable();

                    string host = "http://devwiserdc.sas-autoparts.com:8000";
                    if (dtbl2.Rows.Count > 0)
                    {
                        host = dtbl2.Rows[0]["Value"].ToString();
                    }
                    else
                    {
                        MessageBox.Show("Download gagal, Wiser belum di setting");
                        return;
                    }

                    Guid       _headerRowID, _detailID;
                    List <int> scs = new List <int>();
                    foreach (int i in ids)
                    {
                        JSON             cur  = mdat[i.ToString()];
                        List <Parameter> cmdl = new List <Parameter>();
                        db.Commands.Clear();

                        //DateTime tTerima = DateTime.ParseExact(cur["TglTerima"].StringValue, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                        //MessageBox.Show(cur["NotaRowID"].GuidValue(DBNull.Value).ToString());
                        //return;

                        generateNoKoreksi();

                        _headerRowID = Guid.NewGuid();
                        cmdl.Add(new Parameter("@RowID", SqlDbType.UniqueIdentifier, _headerRowID));
                        cmdl.Add(new Parameter("@RecordID", SqlDbType.VarChar, FingerPrintWiser(cur["wiserid"].StringValue)));
                        cmdl.Add(new Parameter("@NotaJualDetailWiserID", SqlDbType.Int, cur["NotaJualDetailWiserID"].NumberValue));
                        cmdl.Add(new Parameter("@TglKoreksi", SqlDbType.DateTime, cur["TglKoreksi"].DateTimeValue(DBNull.Value)));
                        cmdl.Add(new Parameter("@NoKoreksi", SqlDbType.VarChar, strNoKoreksi));
                        cmdl.Add(new Parameter("@BarangID", SqlDbType.VarChar, cur["BarangID"].StringValue));
                        cmdl.Add(new Parameter("@QtyNotaBaru", SqlDbType.Int, cur["QtyNotaBaru"].NumberValue));
                        cmdl.Add(new Parameter("@HrgJualBaru", SqlDbType.Money, cur["HrgJualBaru"].NumberValue));
                        cmdl.Add(new Parameter("@Catatan", SqlDbType.VarChar, cur["Catatan"].StringValue));
                        cmdl.Add(new Parameter("@KodeToko", SqlDbType.VarChar, cur["KodeToko"].StringValue));
                        cmdl.Add(new Parameter("@Sumber", SqlDbType.VarChar, cur["Sumber"].StringValue));
                        cmdl.Add(new Parameter("@HrgJualKoreksi", SqlDbType.Int, cur["HrgJualKoreksi"].NumberValue));
                        cmdl.Add(new Parameter("@QtyNotaKoreksi", SqlDbType.Int, cur["QtyNotaKoreksi"].NumberValue));
                        cmdl.Add(new Parameter("@SyncFlag", SqlDbType.Bit, 0));
                        //cmdl.Add(new Parameter("@LinkID", SqlDbType.VarChar, ""));
                        cmdl.Add(new Parameter("@LastUpdatedBy", SqlDbType.VarChar, cur["LastUpdatedBy"].StringValue));
                        cmdl.Add(new Parameter("@wiserid", SqlDbType.Int, cur["wiserid"].NumberValue));
                        cmdl.Add(new Parameter("@WiserTag", SqlDbType.VarChar, "WISERDC"));

                        try
                        {
                            db.Commands.Clear();
                            db.Commands.Add(db.CreateCommand("usp_KoreksiPenjualan_WISERDC_SET"));
                            db.Commands[0].Parameters = cmdl;
                            db.Commands[0].ExecuteNonQuery();

                            db.Commands.Add(db.CreateCommand("usp_Numerator_UPDATE"));
                            db.Commands[1].Parameters.Add(new Parameter("@doc", SqlDbType.VarChar, docNoKoreksi));
                            db.Commands[1].Parameters.Add(new Parameter("@depan", SqlDbType.VarChar, depan));
                            db.Commands[1].Parameters.Add(new Parameter("@belakang", SqlDbType.VarChar, belakang));
                            db.Commands[1].Parameters.Add(new Parameter("@nomor", SqlDbType.Int, iNomor));
                            db.Commands[1].Parameters.Add(new Parameter("@lebar", SqlDbType.VarChar, lebar));
                            db.Commands[1].Parameters.Add(new Parameter("@lastUpdatedBy", SqlDbType.VarChar, SecurityManager.UserID));

                            db.Commands[1].ExecuteNonQuery();

                            scs.Add((int)cur["wiserid"].NumberValue);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error: " + ex);
                        }
                    }

                    if (scs.Count > 0)
                    {
                        MessageBox.Show("DATA BERHASIL DITAMBAHKAN");
                    }
                    else
                    {
                        MessageBox.Show("Synch gagal");
                    }

                    if (scs.Count > 0)
                    {
                        if (!hasSynch)
                        {
                            hasSynch = true;
                        }
                        MarkAsSuccess(scs.ToArray());

                        JSON opt = new JSON(JSONType.Object);
                        JSON lst = new JSON(JSONType.Array);
                        foreach (int ix in scs)
                        {
                            lst.ArrAdd(new JSON(ix));
                        }
                        opt.ObjAdd("mark", new JSON(true));
                        opt.ObjAdd("ids", lst);

                        string errm = "";

                        XNet       xn  = new XNet(host + "/api/koreksipenjualan/synch", XNetMethod.GET);
                        XNetThread xnt = xn.Send(opt, r =>
                        {
                            if (r.Error != null)
                            {
                                errm = r.Error.Message;
                            }
                            else if (r.Output.Length > 0)
                            {
                                JSON jres = JSON.Parse(r.Output);
                                if (jres.Type == JSONType.Object)
                                {
                                    if (jres.ObjExists("Result") && jres["Result"].BoolValue)
                                    {
                                        errm = "";
                                    }
                                    else if (jres.ObjExists("Msg"))
                                    {
                                        errm = jres["Msg"].StringValue;
                                    }
                                    else
                                    {
                                        errm = "Marking to server failed";
                                    }
                                    return;
                                }
                            }
                            errm = "Marking to server failed";
                        });

                        if (errm.Length > 0)
                        {
                            MessageBox.Show("Server message:\n" + errm);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Synch gagal");
                    }

                    b.Result = true;
                    return;
                }
                if (b.Result != null && !b.Result.Equals(true))
                {
                    MessageBox.Show(b.Result.ToString());
                    return;
                }
            };
            bgw.RunWorkerCompleted += (a, b) =>
            {
                bool r = false;
                if (b.Cancelled)
                {
                    MessageBox.Show(thisx, "Operasi di gagalkan");
                }
                else if (b.Error != null)
                {
                    MessageBox.Show(thisx, b.Error.Message);
                }
                else
                {
                    r = true;
                }

                ipProgress.Close(r);
                this.Close();
            };

            ipProgress.OpenDialog(this, a =>
            {
            }, () => bgw.CancelAsync());
            bgw.RunWorkerAsync();
        }
        private void ImportData(int[] ids)
        {
            Form thisx = this;

            if (ipProgress == null)
            {
                ipProgress = new InPopup(this, pnlProgress);
            }
            if (fpProgress == null)
            {
                fpProgress = new FakeProgress(progbProgress);
            }

            BackgroundWorker bgw = new BackgroundWorker();

            bgw.DoWork += (a, b) =>
            {
                fpProgress.Start();

                using (var db = new Database())
                {
                    if (bgw.CancellationPending)
                    {
                        return;
                    }

                    db.Commands.Add(db.CreateCommand("[usp_AppSetting_LIST]"));
                    db.Commands[0].Parameters.Add(new Parameter("@Key", SqlDbType.VarChar, "WiserDC_Host"));
                    DataTable dtbl2 = db.Commands[0].ExecuteDataTable();

                    string host = "http://devwiserdc.sas-autoparts.com:8000";
                    if (dtbl2.Rows.Count > 0)
                    {
                        host = dtbl2.Rows[0]["Value"].ToString();
                    }
                    else
                    {
                        MessageBox.Show("Download gagal, Wiser belum di setting");
                        return;
                    }

                    Guid       _headerRowID, _detailID;
                    List <int> scs = new List <int>();
                    foreach (int i in ids)
                    {
                        JSON             cur  = mdat[i.ToString()];
                        List <Parameter> cmdl = new List <Parameter>();
                        db.Commands.Clear();

                        //DateTime tTerima = DateTime.ParseExact(cur["TglTerima"].StringValue, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                        //MessageBox.Show(cur["NotaRowID"].GuidValue(DBNull.Value).ToString());
                        //return;
                        if (cur["NotaRowID"].GuidValue(DBNull.Value) == DBNull.Value)
                        {
                            _headerRowID = Guid.NewGuid();
                        }
                        else
                        {
                            _headerRowID = (Guid)cur["NotaRowID"].GuidValue(DBNull.Value);
                        }
                        cmdl.Add(new Parameter("@RowID", SqlDbType.UniqueIdentifier, _headerRowID));
                        cmdl.Add(new Parameter("@HtrID", SqlDbType.VarChar, FingerPrintWiser(cur["wiserid"].StringValue)));
                        cmdl.Add(new Parameter("@RecordID", SqlDbType.VarChar, FingerPrintWiser(cur["wiserid"].StringValue)));
                        cmdl.Add(new Parameter("@DOID", SqlDbType.UniqueIdentifier, cur["DOID"].GuidValue(DBNull.Value)));
                        cmdl.Add(new Parameter("@Cabang1", SqlDbType.VarChar, cur["Cabang1"].StringValue));
                        cmdl.Add(new Parameter("@Cabang2", SqlDbType.VarChar, cur["Cabang2"].StringValue));
                        cmdl.Add(new Parameter("@Cabang3", SqlDbType.VarChar, cur["Cabang3"].StringValue));
                        cmdl.Add(new Parameter("@NoNota", SqlDbType.VarChar, cur["NoNota"].StringValue));
                        cmdl.Add(new Parameter("@TglNota", SqlDbType.DateTime, cur["TglNota"].DateTimeValue(DBNull.Value)));
                        cmdl.Add(new Parameter("@NoSuratJalan", SqlDbType.VarChar, cur["NoSuratJalan"].StringValue));
                        cmdl.Add(new Parameter("@TglSuratJalan", SqlDbType.DateTime, cur["TglSuratJalan"].DateTimeValue(DBNull.Value)));
                        cmdl.Add(new Parameter("@KodeToko", SqlDbType.VarChar, cur["KodeToko"].StringValue));
                        cmdl.Add(new Parameter("@KodeSales", SqlDbType.VarChar, cur["KodeSales"].StringValue));
                        cmdl.Add(new Parameter("@TglTerima", SqlDbType.DateTime, cur["TglTerima"].DateTimeValue(DBNull.Value)));
                        cmdl.Add(new Parameter("@TglSerahTerimaChecker", SqlDbType.DateTime, cur["TglSerahTerimaChecker"].DateTimeValue(DBNull.Value)));
                        cmdl.Add(new Parameter("@TglExpedisi", SqlDbType.DateTime, cur["TglExpedisi"].DateTimeValue(DBNull.Value)));
                        cmdl.Add(new Parameter("@AlamatKirim", SqlDbType.VarChar, cur["AlamatKirim"].StringValue));
                        cmdl.Add(new Parameter("@Kota", SqlDbType.VarChar, cur["Kota"].StringValue));
                        cmdl.Add(new Parameter("@isClosed", SqlDbType.Bit, 0));
                        cmdl.Add(new Parameter("@Catatan1", SqlDbType.VarChar, cur["Catatan1"].StringValue));
                        cmdl.Add(new Parameter("@Catatan2", SqlDbType.VarChar, cur["Catatan2"].StringValue));
                        cmdl.Add(new Parameter("@Catatan3", SqlDbType.VarChar, cur["Catatan3"].StringValue));
                        cmdl.Add(new Parameter("@Catatan4", SqlDbType.VarChar, cur["Catatan4"].StringValue));
                        cmdl.Add(new Parameter("@Catatan5", SqlDbType.VarChar, cur["Catatan5"].StringValue));
                        cmdl.Add(new Parameter("@SyncFlag", SqlDbType.Bit, 0));
                        //cmdl.Add(new Parameter("@LinkID", SqlDbType.VarChar, ""));
                        cmdl.Add(new Parameter("@NPrint", SqlDbType.Int, cur["NPrint"].NumberValue));
                        cmdl.Add(new Parameter("@TransactionType", SqlDbType.VarChar, cur["TransactionType"].StringValue));
                        cmdl.Add(new Parameter("@HariKredit", SqlDbType.Int, cur["HariKredit"].NumberValue));
                        cmdl.Add(new Parameter("@HariKirim", SqlDbType.Int, cur["HariKirim"].NumberValue));
                        cmdl.Add(new Parameter("@HariSales", SqlDbType.Int, cur["HariSales"].NumberValue));
                        cmdl.Add(new Parameter("@Checker1", SqlDbType.VarChar, cur["Checker1"].StringValue));
                        cmdl.Add(new Parameter("@Checker2", SqlDbType.VarChar, cur["Checker2"].StringValue));
                        cmdl.Add(new Parameter("@LastUpdatedBy", SqlDbType.VarChar, cur["LastUpdatedBy"].StringValue));

                        DateTime TglSuratJalan = new DateTime();
                        DateTime.TryParse(cur["TglNota"].DateTimeValue(DBNull.Value).ToString(), out TglSuratJalan);
                        string barcode = cur["NoNota"].StringValue + TglSuratJalan.ToString("yyyy").Substring(1, 3) + TglSuratJalan.ToString("MM");

                        cmdl.Add(new Parameter("@Barcode", SqlDbType.VarChar, barcode));
                        cmdl.Add(new Parameter("@WiserTag", SqlDbType.VarChar, "WISERDC"));
                        cmdl.Add(new Parameter("@wiserid", SqlDbType.Int, cur["wiserid"].NumberValue));

                        try
                        {
                            db.Commands.Clear();
                            db.Commands.Add(db.CreateCommand("usp_NotaPenjualan_WISERDC_SET"));
                            db.Commands[0].Parameters = cmdl;
                            DataTable dtbl = db.Commands[0].ExecuteDataTable();

                            if (dtbl.Rows.Count > 0)
                            {
                                if (dtbl.Rows[0]["Result"].ToString() == "1")
                                {
                                    Guid id = (Guid)dtbl.Rows[0]["RowID"];

                                    int  cid          = 0;
                                    bool itOk         = true;
                                    bool itTrasaction = false;

                                    try
                                    {
                                        db.Commands.Clear();
                                        itTrasaction = true;
                                        db.BeginTransaction();

                                        foreach (string ck in cur["Details"].ObjKeys)
                                        {
                                            _detailID = Guid.NewGuid();
                                            JSON curd = cur["Details"][ck];

                                            cmdl = new List <Parameter>();
                                            cmdl.Add(new Parameter("@RowID", SqlDbType.UniqueIdentifier, _detailID));
                                            cmdl.Add(new Parameter("@HeaderID", SqlDbType.UniqueIdentifier, id));
                                            cmdl.Add(new Parameter("@DOID", SqlDbType.UniqueIdentifier, curd["DOID"].GuidValue(DBNull.Value)));
                                            cmdl.Add(new Parameter("@DODetailID", SqlDbType.UniqueIdentifier, curd["DODetailID"].GuidValue(DBNull.Value)));
                                            cmdl.Add(new Parameter("@RecordID", SqlDbType.VarChar, FingerPrintWiser(curd["wiserid"].StringValue)));
                                            cmdl.Add(new Parameter("@HtrID", SqlDbType.VarChar, FingerPrintWiser(curd["wiserid"].StringValue)));
                                            cmdl.Add(new Parameter("@KodeGudang", SqlDbType.VarChar, curd["KodeGudang"].StringValue));
                                            cmdl.Add(new Parameter("@BarangID", SqlDbType.VarChar, curd["BarangID"].StringValue));
                                            cmdl.Add(new Parameter("@QtySuratJalan", SqlDbType.Int, curd["QtySuratJalan"].NumberValue));
                                            cmdl.Add(new Parameter("@QtyNota", SqlDbType.Int, curd["QtyNota"].NumberValue));
                                            cmdl.Add(new Parameter("@HrgJual", SqlDbType.Money, curd["HrgJual"].NumberValue));
                                            cmdl.Add(new Parameter("@Disc1", SqlDbType.Decimal, curd["Disc1"].NumberValue));
                                            cmdl.Add(new Parameter("@Disc2", SqlDbType.Decimal, curd["Disc2"].NumberValue));
                                            cmdl.Add(new Parameter("@Disc3", SqlDbType.Decimal, curd["Disc3"].NumberValue));
                                            cmdl.Add(new Parameter("@Pot", SqlDbType.Money, 0));
                                            cmdl.Add(new Parameter("@DiscFormula", SqlDbType.VarChar, ""));
                                            cmdl.Add(new Parameter("@QtyKoli", SqlDbType.Int, curd["QtyKoli"].NumberValue));
                                            cmdl.Add(new Parameter("@KoliAwal", SqlDbType.Int, curd["KoliAwal"].NumberValue));
                                            cmdl.Add(new Parameter("@KoliAkhir", SqlDbType.Int, curd["KoliAkhir"].NumberValue));
                                            cmdl.Add(new Parameter("@NoKoli", SqlDbType.VarChar, curd["NoKoli"].StringValue));
                                            cmdl.Add(new Parameter("@Catatan", SqlDbType.VarChar, curd["Catatan"].StringValue));
                                            cmdl.Add(new Parameter("@SyncFlag", SqlDbType.Bit, 0));
                                            cmdl.Add(new Parameter("@KetKoli", SqlDbType.VarChar, curd["KetKoli"].StringValue));
                                            cmdl.Add(new Parameter("@NPackingListPrint", SqlDbType.Int, curd["NPackingListPrint"].NumberValue));
                                            cmdl.Add(new Parameter("@LastUpdatedBy", SqlDbType.VarChar, curd["LastUpdatedBy"].StringValue));
                                            cmdl.Add(new Parameter("@CreatedBy", SqlDbType.VarChar, curd["LastUpdatedBy"].StringValue));
                                            cmdl.Add(new Parameter("@QtyStock", SqlDbType.Int, curd["QtyStock"].NumberValue));
                                            cmdl.Add(new Parameter("@WiserID", SqlDbType.Int, curd["wiserid"].NumberValue));
                                            cmdl.Add(new Parameter("@WiserHeaderID", SqlDbType.Int, cur["wiserid"].NumberValue));
                                            cmdl.Add(new Parameter("@WiserTag", SqlDbType.VarChar, "WISERDC"));

                                            db.Commands.Add(db.CreateCommand("[usp_NotaPenjualanDetail_WISERDC_SET]"));
                                            db.Commands[cid].Parameters = cmdl;
                                            cid += 1;
                                        }

                                        foreach (Command cmd in db.Commands)
                                        {
                                            try { cmd.ExecuteNonQuery(); }
                                            catch (Exception ex) { Error.LogError(ex); }
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        if (itTrasaction)
                                        {
                                            db.RollbackTransaction();
                                        }
                                        itOk = false;
                                    }
                                    if (itOk)
                                    {
                                        if (itTrasaction)
                                        {
                                            db.CommitTransaction();
                                        }
                                        try
                                        {
                                            //Link Ke Piutang
                                            GetDataNota(id);
                                            if ((new JSON(cur["TglTerima"].DateTimeValue(DBNull.Value))).DateTimeValue(null) != null)
                                            {
                                                IsiTglTerima((DateTime)cur["TglTerima"].DateTimeValue(DBNull.Value), (string)cur["TransactionType"].StringValue, (int)cur["HariKredit"].NumberValue, (int)cur["HariKirim"].NumberValue, (int)cur["HariSales"].NumberValue, (string)cur["LastUpdatedBy"].StringValue);
                                                GetDataNota(id);
                                                LinkKePiutang();
                                                successMessage = "DATA BERHASIL DITAMBAHKAN DAN SUDAH DILINK KE PIUTANG";
                                            }
                                            else
                                            {
                                                successMessage = "DATA BERHASIL DITAMBAHKAN TAPI BELUM DILINK KE PIUTANG KARENA TGL TERIMA MASIH KOSONG";
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            itOk = false;
                                        }
                                    }
                                    if (itOk)
                                    {
                                        //if (itTrasaction) db.CommitTransaction();
                                        scs.Add((int)cur["wiserid"].NumberValue);
                                    }
                                    else
                                    {
                                        try
                                        {
                                            db.Commands.Clear();
                                            db.Commands.Add(db.CreateCommand("[usp_NotaPenjualan_WISERDC_DELETE]"));
                                            db.Commands[0].Parameters.Add(new Parameter("@RowID", SqlDbType.UniqueIdentifier, id));
                                            db.Commands[0].Parameters.Add(new Parameter("@WiserID", SqlDbType.Int, cur["wiserid"].NumberValue));

                                            db.Commands[0].ExecuteNonQuery();
                                        }
                                        catch (Exception) { }
                                    }
                                }
                                else if (dtbl.Rows[0]["Result"].ToString() == "2")
                                {
                                    MessageBox.Show("Order Penjualan atas Nota " + cur["NoNota"].StringValue + " tidak ditemukan");
                                    //return;
                                }
                                else
                                {
                                    if (successMessage != "")
                                    {
                                        if (!successMessage.Contains("SURAT JALAN"))
                                        {
                                            successMessage = successMessage + " ADA NOTA YANG BELUM DIBUAT SURAT JALAN.";
                                        }
                                    }
                                    else
                                    {
                                        if (!successMessage.Contains("SURAT JALAN") && !successMessage.Contains("PIUTANG"))
                                        {
                                            successMessage = "ADA NOTA YANG BELUM DIBUAT SURAT JALAN.";
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error: " + ex);
                            return;
                        }
                    }

                    if (scs.Count > 0)
                    {
                        MessageBox.Show(successMessage);
                    }
                    else
                    {
                        MessageBox.Show("Synch gagal");
                    }

                    if (scs.Count > 0)
                    {
                        if (!hasSynch)
                        {
                            hasSynch = true;
                        }
                        MarkAsSuccess(scs.ToArray());

                        JSON opt = new JSON(JSONType.Object);
                        JSON lst = new JSON(JSONType.Array);
                        foreach (int ix in scs)
                        {
                            lst.ArrAdd(new JSON(ix));
                        }
                        opt.ObjAdd("mark", new JSON(true));
                        opt.ObjAdd("ids", lst);

                        string errm = "";

                        XNet       xn  = new XNet(host + "/api/notapenjualan/synch", XNetMethod.GET);
                        XNetThread xnt = xn.Send(opt, r =>
                        {
                            if (r.Error != null)
                            {
                                errm = r.Error.Message;
                            }
                            else if (r.Output.Length > 0)
                            {
                                JSON jres = JSON.Parse(r.Output);
                                if (jres.Type == JSONType.Object)
                                {
                                    if (jres.ObjExists("Result") && jres["Result"].BoolValue)
                                    {
                                        errm = "";
                                    }
                                    else if (jres.ObjExists("Msg"))
                                    {
                                        errm = jres["Msg"].StringValue;
                                    }
                                    else
                                    {
                                        errm = "Marking to server failed";
                                    }
                                    return;
                                }
                            }
                            errm = "Marking to server failed";
                        });

                        if (errm.Length > 0)
                        {
                            MessageBox.Show("Server message:\n" + errm);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Synch gagal");
                    }

                    b.Result = true;
                    return;
                }
                if (b.Result != null && !b.Result.Equals(true))
                {
                    MessageBox.Show(b.Result.ToString());
                    return;
                }
            };
            bgw.RunWorkerCompleted += (a, b) =>
            {
                bool r = false;
                if (b.Cancelled)
                {
                    MessageBox.Show(thisx, "Operasi di gagalkan");
                }
                else if (b.Error != null)
                {
                    MessageBox.Show(thisx, b.Error.Message);
                }
                else
                {
                    r = true;
                }

                ipProgress.Close(r);
                //if (scs.Count == mdat.Count) this.Close();
            };

            ipProgress.OpenDialog(this, a =>
            {
            }, () => bgw.CancelAsync());
            bgw.RunWorkerAsync();
        }
        private void ImportData(int[] ids)
        {
            using (var db = new Database())
            {
                db.Commands.Add(db.CreateCommand("[usp_AppSetting_LIST]"));
                db.Commands[0].Parameters.Add(new Parameter("@Key", SqlDbType.VarChar, "WiserDC_Host"));
                DataTable dtbl2 = db.Commands[0].ExecuteDataTable();

                string host = "http://devwiserdc.sas-autoparts.com:8000";
                if (dtbl2.Rows.Count > 0)
                {
                    host = dtbl2.Rows[0]["Value"].ToString();
                }
                else
                {
                    MessageBox.Show("Download gagal, Wiser belum di setting");
                    return;
                }

                Guid HeaderID, DetailID;

                List <int> scs = new List <int>();
                foreach (int i in ids)
                {
                    JSON             cur  = mdat[i.ToString()];
                    List <Parameter> cmdl = new List <Parameter>();
                    db.Commands.Clear();

                    if (cur["isarowid"].GuidValue(DBNull.Value) == DBNull.Value)
                    {
                        HeaderID = Guid.NewGuid();
                    }
                    else
                    {
                        HeaderID = (Guid)cur["isarowid"].GuidValue(DBNull.Value);
                    }

                    cmdl.Add(new Parameter("@RowID", SqlDbType.UniqueIdentifier, HeaderID));
                    cmdl.Add(new Parameter("@RecordID", SqlDbType.VarChar, FingerPrintWiser(cur["wiserid"].StringValue)));
                    cmdl.Add(new Parameter("@DrGudang", SqlDbType.VarChar, cur["DrGudang"].StringValue));
                    cmdl.Add(new Parameter("@KeGudang", SqlDbType.VarChar, cur["KeGudang"].StringValue));
                    cmdl.Add(new Parameter("@TglKirim", SqlDbType.DateTime, cur["TglKirim"].DateTimeValue(DBNull.Value)));
                    cmdl.Add(new Parameter("@TglTerima", SqlDbType.DateTime, cur["TglTerima"].DateTimeValue(DBNull.Value)));
                    cmdl.Add(new Parameter("@NoAG", SqlDbType.VarChar, cur["NoAG"].StringValue));
                    cmdl.Add(new Parameter("@Pengirim", SqlDbType.VarChar, cur["Pengirim"].StringValue));
                    cmdl.Add(new Parameter("@Penerima", SqlDbType.VarChar, cur["Penerima"].StringValue));
                    cmdl.Add(new Parameter("@DrCheck1", SqlDbType.VarChar, cur["DrCheck1"].StringValue));
                    cmdl.Add(new Parameter("@DrCheck2", SqlDbType.VarChar, cur["DrCheck2"].StringValue));
                    cmdl.Add(new Parameter("@KeCheck1", SqlDbType.VarChar, cur["KeCheck1"].StringValue));
                    cmdl.Add(new Parameter("@KeCheck2", SqlDbType.VarChar, cur["KeCheck2"].StringValue));
                    cmdl.Add(new Parameter("@Catatan", SqlDbType.VarChar, cur["Catatan"].StringValue));
                    cmdl.Add(new Parameter("@expedisi", SqlDbType.VarChar, cur["Expedisi"].StringValue));
                    cmdl.Add(new Parameter("@NoKendaraan", SqlDbType.VarChar, cur["NoKendaraan"].StringValue));
                    cmdl.Add(new Parameter("@NamaSopir", SqlDbType.VarChar, cur["NamaSopir"].StringValue));
                    cmdl.Add(new Parameter("@LastUpdatedBy", SqlDbType.VarChar, cur["LastUpdatedBy"].StringValue));
                    cmdl.Add(new Parameter("@wiserid", SqlDbType.Int, cur["wiserid"].NumberValue));
                    cmdl.Add(new Parameter("@WiserTag", SqlDbType.VarChar, "WISERDC"));

                    try
                    {
                        db.Commands.Clear();
                        db.Commands.Add(db.CreateCommand("[usp_AntarGudang_WISERDC_SET]"));
                        db.Commands[0].Parameters = cmdl;
                        DataTable dtbl = db.Commands[0].ExecuteDataTable();

                        if (dtbl.Rows.Count > 0)
                        {
                            if (dtbl.Rows[0]["Result"].ToString() == "1")
                            {
                                Guid id = (Guid)dtbl.Rows[0]["RowID"];

                                int  cid          = 0;
                                bool itOk         = true;
                                bool itTrasaction = false;

                                try
                                {
                                    db.Commands.Clear();
                                    foreach (string ck in cur["Details"].ObjKeys)
                                    {
                                        /*
                                         *
                                         *                          "wiserid"		=> $row2->id,
                                         *                          "KodeBarang"    => $row2->barang->kodebarang,
                                         *                          "QtyKirim"      => $row2->qtynotaag,
                                         *                          "QtyTerima"     => 0,
                                         *                          "Catatan"       => $row2->catatan,
                                         *                          "Ongkos"        => null,
                                         *                          "SyncFlag"      => 1,
                                         *                          "QtyDO"         => $row2->qtyrqag,
                                         *                          "isarowid"		=> $row2->isarowid,
                                         *                          "LastUpdatedBy" => $tmp["LastUpdatedBy"],
                                         *                          "LastUpdatedTime" => $tmp["LastUpdatedTime"],
                                         *                          "HrgBeli"       => 0
                                         */
                                        JSON curd = cur["Details"][ck];

                                        if (curd["isarowid"].GuidValue(DBNull.Value) == DBNull.Value)
                                        {
                                            DetailID = Guid.NewGuid();
                                        }
                                        else
                                        {
                                            DetailID = (Guid)curd["isarowid"].GuidValue(DBNull.Value);
                                        }

                                        cmdl = new List <Parameter>();
                                        cmdl.Add(new Parameter("@RowID", SqlDbType.UniqueIdentifier, DetailID));
                                        cmdl.Add(new Parameter("@HeaderID", SqlDbType.UniqueIdentifier, id));
                                        cmdl.Add(new Parameter("@RecordID", SqlDbType.VarChar, FingerPrintWiser(curd["wiserid"].StringValue)));
                                        cmdl.Add(new Parameter("@TransactionID", SqlDbType.VarChar, FingerPrintWiser(curd["wiserid"].StringValue)));
                                        cmdl.Add(new Parameter("@KodeBarang", SqlDbType.VarChar, curd["KodeBarang"].StringValue));
                                        cmdl.Add(new Parameter("@QtyKirim", SqlDbType.Int, curd["QtyKirim"].NumberValue));
                                        cmdl.Add(new Parameter("@QtyTerima", SqlDbType.Int, curd["QtyTerima"].NumberValue));
                                        cmdl.Add(new Parameter("@QtyDO", SqlDbType.Int, curd["QtyDO"].NumberValue));
                                        cmdl.Add(new Parameter("@Ongkos", SqlDbType.Money, curd["Ongkos"].NumberValue));
                                        cmdl.Add(new Parameter("@HrgBeli", SqlDbType.Money, curd["HrgBeli"].NumberValue));
                                        cmdl.Add(new Parameter("@Catatan", SqlDbType.VarChar, curd["Catatan"].StringValue));
                                        cmdl.Add(new Parameter("@SyncFlag", SqlDbType.Bit, 0));
                                        cmdl.Add(new Parameter("@LastUpdatedBy", SqlDbType.VarChar, curd["LastUpdatedBy"].StringValue));
                                        cmdl.Add(new Parameter("@WiserID", SqlDbType.Int, curd["wiserid"].NumberValue));
                                        cmdl.Add(new Parameter("@WiserHeaderID", SqlDbType.Int, curd["wiserheaderid"].NumberValue));
                                        cmdl.Add(new Parameter("@WiserTag", SqlDbType.VarChar, "WISERDC"));

                                        db.Commands.Add(db.CreateCommand("[usp_AntarGudangDetail_WISERDC_SET]"));
                                        db.Commands[cid].Parameters = cmdl;
                                        cid += 1;
                                    }

                                    itTrasaction = true;
                                    db.BeginTransaction();
                                    foreach (Command cmd in db.Commands)
                                    {
                                        try
                                        {
                                            cmd.ExecuteNonQuery();
                                        }
                                        catch (Exception ex)
                                        {
                                            Error.LogError(ex);
                                            if (itTrasaction)
                                            {
                                                db.RollbackTransaction();
                                            }
                                            itOk = false;
                                            break;
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    itOk = false;
                                }

                                if (itOk)
                                {
                                    if (itTrasaction)
                                    {
                                        db.CommitTransaction();
                                    }
                                    scs.Add((int)cur["wiserid"].NumberValue);
                                }
                                else
                                {
                                    try
                                    {
                                        db.Commands.Clear();
                                        db.Commands.Add(db.CreateCommand("[usp_AntarGudang_WISERDC_DELETE]"));
                                        db.Commands[0].Parameters.Add(new Parameter("@RowID", SqlDbType.UniqueIdentifier, id));
                                        db.Commands[0].Parameters.Add(new Parameter("@WiserID", SqlDbType.Int, cur["wiserid"].NumberValue));

                                        db.Commands[0].ExecuteNonQuery();
                                    }
                                    catch (Exception) { }
                                }
                            }
                        }
                    }
                    catch (Exception ex) {
                        MessageBox.Show("Error: " + ex);
                    }
                }

                if (scs.Count > 0)
                {
                    if (!hasSynch)
                    {
                        hasSynch = true;
                    }
                    MarkAsSuccess(scs.ToArray());

                    JSON opt = new JSON(JSONType.Object);
                    JSON lst = new JSON(JSONType.Array);
                    foreach (int ix in scs)
                    {
                        lst.ArrAdd(new JSON(ix));
                    }
                    opt.ObjAdd("mark", new JSON(true));
                    opt.ObjAdd("ids", lst);

                    string errm = "";

                    XNet       xn  = new XNet(host + "/api/antargudang/synch", XNetMethod.GET);
                    XNetThread xnt = xn.Get(opt, r =>
                    {
                        if (r.Error != null)
                        {
                            errm = r.Error.Message;
                        }
                        else if (r.Output.Length > 0)
                        {
                            JSON jres = JSON.Parse(r.Output);
                            if (jres.Type == JSONType.Object)
                            {
                                if (jres.ObjExists("Result") && jres["Result"].BoolValue)
                                {
                                    errm = "";
                                }
                                else if (jres.ObjExists("Msg"))
                                {
                                    errm = jres["Msg"].StringValue;
                                }
                                else
                                {
                                    errm = "Marking to server failed";
                                }
                                return;
                            }
                        }
                        errm = "Marking to server failed";
                    });

                    if (errm.Length > 0)
                    {
                        MessageBox.Show("Server message:\n" + errm);
                    }
                    if (scs.Count == mdat.Count)
                    {
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("Synch gagal");
                }
            }
        }
        private void ImportData(int[] ids)
        {
            Form thisx = this;

            if (ipProgress == null)
            {
                ipProgress = new InPopup(this, pnlProgress);
            }
            if (fpProgress == null)
            {
                fpProgress = new FakeProgress(progbProgress);
            }

            BackgroundWorker bgw = new BackgroundWorker();

            bgw.DoWork += (a, b) =>
            {
                fpProgress.Start();

                using (var db = new Database())
                {
                    if (bgw.CancellationPending)
                    {
                        return;
                    }

                    db.Commands.Add(db.CreateCommand("[usp_AppSetting_LIST]"));
                    db.Commands[0].Parameters.Add(new Parameter("@Key", SqlDbType.VarChar, "WiserDC_Host"));
                    DataTable dtbl2 = db.Commands[0].ExecuteDataTable();

                    string host = "http://devwiserdc.sas-autoparts.com:8000";
                    if (dtbl2.Rows.Count > 0)
                    {
                        host = dtbl2.Rows[0]["Value"].ToString();
                    }
                    else
                    {
                        MessageBox.Show("Download gagal, Wiser belum di setting");
                        return;
                    }

                    Guid       _headerRowID, _detailID;
                    List <int> scs = new List <int>();
                    foreach (int i in ids)
                    {
                        JSON             cur  = mdat[i.ToString()];
                        List <Parameter> cmdl = new List <Parameter>();
                        db.Commands.Clear();

                        //DateTime tTerima = DateTime.ParseExact(cur["TglTerima"].StringValue, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                        //MessageBox.Show(cur["NotaRowID"].GuidValue(DBNull.Value).ToString());
                        //return;
                        if (cur["RJRowID"].GuidValue(DBNull.Value) == DBNull.Value)
                        {
                            _headerRowID = Guid.NewGuid();
                        }
                        else
                        {
                            _headerRowID = (Guid)cur["RJRowID"].GuidValue(DBNull.Value);
                        }
                        cmdl.Add(new Parameter("@RowID", SqlDbType.UniqueIdentifier, _headerRowID));
                        cmdl.Add(new Parameter("@ReturID", SqlDbType.VarChar, FingerPrintWiser(cur["wiserid"].StringValue)));
                        cmdl.Add(new Parameter("@Cabang1", SqlDbType.VarChar, cur["Cabang1"].StringValue));
                        cmdl.Add(new Parameter("@Cabang2", SqlDbType.VarChar, cur["Cabang2"].StringValue));
                        cmdl.Add(new Parameter("@NoMPR", SqlDbType.VarChar, cur["NoMPR"].StringValue));
                        cmdl.Add(new Parameter("@NoNotaRetur", SqlDbType.VarChar, cur["NoNotaRetur"].StringValue));
                        cmdl.Add(new Parameter("@NoTolak", SqlDbType.VarChar, cur["NoTolak"].StringValue));
                        cmdl.Add(new Parameter("@TglMPR", SqlDbType.DateTime, cur["TglMPR"].DateTimeValue(DBNull.Value)));
                        cmdl.Add(new Parameter("@TglNotaRetur", SqlDbType.DateTime, cur["TglNotaRetur"].DateTimeValue(DBNull.Value)));
                        cmdl.Add(new Parameter("@TglTolak", SqlDbType.DateTime, cur["TglTolak"].DateTimeValue(DBNull.Value)));
                        cmdl.Add(new Parameter("@KodeToko", SqlDbType.VarChar, cur["KodeToko"].StringValue));
                        cmdl.Add(new Parameter("@Pengambilan", SqlDbType.VarChar, cur["Pengambilan"].StringValue));
                        cmdl.Add(new Parameter("@TglPengambilan", SqlDbType.DateTime, cur["TglPengambilan"].DateTimeValue(DBNull.Value)));
                        cmdl.Add(new Parameter("@TglGudang", SqlDbType.DateTime, cur["TglGudang"].DateTimeValue(DBNull.Value)));
                        cmdl.Add(new Parameter("@BagPenjualan", SqlDbType.VarChar, cur["BagPenjualan"].StringValue));
                        cmdl.Add(new Parameter("@Penerima", SqlDbType.VarChar, cur["Penerima"].StringValue));
                        cmdl.Add(new Parameter("@isClosed", SqlDbType.Bit, 0));
                        cmdl.Add(new Parameter("@SyncFlag", SqlDbType.Bit, 0));
                        //cmdl.Add(new Parameter("@LinkID", SqlDbType.VarChar, ""));
                        cmdl.Add(new Parameter("@NPrint", SqlDbType.Int, cur["NPrint"].NumberValue));
                        cmdl.Add(new Parameter("@TglRQRetur", SqlDbType.DateTime, cur["TglRQRetur"].DateTimeValue(DBNull.Value)));
                        cmdl.Add(new Parameter("@LastUpdatedBy", SqlDbType.VarChar, cur["LastUpdatedBy"].StringValue));
                        cmdl.Add(new Parameter("@ACCSPV", SqlDbType.VarChar, cur["ACCSPV"].StringValue));
                        cmdl.Add(new Parameter("@wiserid", SqlDbType.Int, cur["wiserid"].NumberValue));
                        cmdl.Add(new Parameter("@WiserTag", SqlDbType.VarChar, "WISERDC"));

                        try
                        {
                            db.Commands.Clear();
                            db.Commands.Add(db.CreateCommand("usp_ReturPenjualan_WISERDC_SET"));
                            db.Commands[0].Parameters = cmdl;
                            DataTable dtbl = db.Commands[0].ExecuteDataTable();

                            if (dtbl.Rows.Count > 0)
                            {
                                if (dtbl.Rows[0]["Result"].ToString() == "1")
                                {
                                    Guid id = (Guid)dtbl.Rows[0]["RowID"];

                                    int  cid          = 0;
                                    bool itOk         = true;
                                    bool itTrasaction = false;

                                    try
                                    {
                                        db.Commands.Clear();
                                        itTrasaction = true;
                                        db.BeginTransaction();

                                        foreach (string ck in cur["Details"].ObjKeys)
                                        {
                                            JSON curd = cur["Details"][ck];

                                            if (curd["RJDRowID"].GuidValue(DBNull.Value) == DBNull.Value)
                                            {
                                                _detailID = Guid.NewGuid();
                                            }
                                            else
                                            {
                                                _detailID = (Guid)curd["RJDRowID"].GuidValue(DBNull.Value);
                                            }
                                            cmdl = new List <Parameter>();
                                            cmdl.Add(new Parameter("@RowID", SqlDbType.UniqueIdentifier, _detailID));
                                            cmdl.Add(new Parameter("@HeaderID", SqlDbType.UniqueIdentifier, id));
                                            cmdl.Add(new Parameter("@NotaJualDetailWiserID", SqlDbType.Int, curd["NotaJualDetailWiserID"].NumberValue));
                                            cmdl.Add(new Parameter("@RecordID", SqlDbType.VarChar, FingerPrintWiser(curd["wiserid"].StringValue)));
                                            cmdl.Add(new Parameter("@KodeRetur", SqlDbType.VarChar, curd["KodeRetur"].StringValue));
                                            cmdl.Add(new Parameter("@KodeGudang", SqlDbType.VarChar, curd["KodeGudang"].StringValue));
                                            cmdl.Add(new Parameter("@BarangID", SqlDbType.VarChar, curd["BarangID"].StringValue));
                                            cmdl.Add(new Parameter("@QtyMemo", SqlDbType.Int, curd["QtyMemo"].NumberValue));
                                            cmdl.Add(new Parameter("@QtyTarik", SqlDbType.Int, curd["QtyTarik"].NumberValue));
                                            cmdl.Add(new Parameter("@QtyTerima", SqlDbType.Int, curd["QtyTerima"].NumberValue));
                                            cmdl.Add(new Parameter("@QtyGudang", SqlDbType.Int, curd["QtyGudang"].NumberValue));
                                            cmdl.Add(new Parameter("@QtyTolak", SqlDbType.Int, curd["QtyTolak"].NumberValue));
                                            cmdl.Add(new Parameter("@HrgJual", SqlDbType.Money, curd["HrgJual"].NumberValue));
                                            cmdl.Add(new Parameter("@Catatan1", SqlDbType.VarChar, curd["Catatan1"].StringValue));
                                            cmdl.Add(new Parameter("@Catatan2", SqlDbType.VarChar, curd["Catatan2"].StringValue));
                                            cmdl.Add(new Parameter("@SyncFlag", SqlDbType.Bit, 0));
                                            cmdl.Add(new Parameter("@Kategori", SqlDbType.VarChar, curd["Kategori"].StringValue));
                                            cmdl.Add(new Parameter("@NoACC", SqlDbType.VarChar, curd["NoACC"].StringValue));
                                            cmdl.Add(new Parameter("@LastUpdatedBy", SqlDbType.VarChar, curd["LastUpdatedBy"].StringValue));
                                            cmdl.Add(new Parameter("@WiserID", SqlDbType.Int, curd["wiserid"].NumberValue));
                                            cmdl.Add(new Parameter("@WiserHeaderID", SqlDbType.Int, cur["wiserid"].NumberValue));
                                            cmdl.Add(new Parameter("@WiserTag", SqlDbType.VarChar, "WISERDC"));

                                            db.Commands.Add(db.CreateCommand("[usp_ReturPenjualanDetail_WISERDC_SET]"));
                                            db.Commands[cid].Parameters = cmdl;
                                            cid += 1;
                                        }

                                        foreach (Command cmd in db.Commands)
                                        {
                                            try { cmd.ExecuteNonQuery(); }
                                            catch (Exception ex) { Error.LogError(ex); }
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        if (itTrasaction)
                                        {
                                            db.RollbackTransaction();
                                        }
                                        itOk = false;
                                    }

                                    if (itOk)
                                    {
                                        if (itTrasaction)
                                        {
                                            db.CommitTransaction();
                                        }
                                        scs.Add((int)cur["wiserid"].NumberValue);
                                    }
                                    else
                                    {
                                        try
                                        {
                                            db.Commands.Clear();
                                            db.Commands.Add(db.CreateCommand("[usp_ReturPenjualan_WISERDC_DELETE]"));
                                            db.Commands[0].Parameters.Add(new Parameter("@RowID", SqlDbType.UniqueIdentifier, id));
                                            db.Commands[0].Parameters.Add(new Parameter("@WiserID", SqlDbType.Int, cur["wiserid"].NumberValue));

                                            db.Commands[0].ExecuteNonQuery();
                                        }
                                        catch (Exception) { }
                                    }
                                    //Link Ke Piutang
                                    //GetDataNota(id);
                                    //LinkKePiutang();
                                }
                                else if (dtbl.Rows[0]["Result"].ToString() == "2")
                                {
                                    MessageBox.Show("Order Penjualan atas Nota " + cur["NoNota"].StringValue + " tidak ditemukan");
                                    return;
                                }
                                else
                                {
                                    MessageBox.Show("Unknown Error");
                                    return;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error: " + ex);
                        }
                    }

                    if (scs.Count > 0)
                    {
                        MessageBox.Show("DATA BERHASIL DITAMBAHKAN");
                    }
                    else
                    {
                        MessageBox.Show("Synch gagal");
                    }

                    if (scs.Count > 0)
                    {
                        if (!hasSynch)
                        {
                            hasSynch = true;
                        }
                        MarkAsSuccess(scs.ToArray());

                        JSON opt = new JSON(JSONType.Object);
                        JSON lst = new JSON(JSONType.Array);
                        foreach (int ix in scs)
                        {
                            lst.ArrAdd(new JSON(ix));
                        }
                        opt.ObjAdd("mark", new JSON(true));
                        opt.ObjAdd("ids", lst);

                        string errm = "";

                        XNet       xn  = new XNet(host + "/api/returpenjualan/synch", XNetMethod.GET);
                        XNetThread xnt = xn.Send(opt, r =>
                        {
                            if (r.Error != null)
                            {
                                errm = r.Error.Message;
                            }
                            else if (r.Output.Length > 0)
                            {
                                JSON jres = JSON.Parse(r.Output);
                                if (jres.Type == JSONType.Object)
                                {
                                    if (jres.ObjExists("Result") && jres["Result"].BoolValue)
                                    {
                                        errm = "";
                                    }
                                    else if (jres.ObjExists("Msg"))
                                    {
                                        errm = jres["Msg"].StringValue;
                                    }
                                    else
                                    {
                                        errm = "Marking to server failed";
                                    }
                                    return;
                                }
                            }
                            errm = "Marking to server failed";
                        });

                        if (errm.Length > 0)
                        {
                            MessageBox.Show("Server message:\n" + errm);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Synch gagal");
                    }

                    b.Result = true;
                    return;
                }
                if (b.Result != null && !b.Result.Equals(true))
                {
                    MessageBox.Show(b.Result.ToString());
                    return;
                }
            };
            bgw.RunWorkerCompleted += (a, b) =>
            {
                bool r = false;
                if (b.Cancelled)
                {
                    MessageBox.Show(thisx, "Operasi di gagalkan");
                }
                else if (b.Error != null)
                {
                    MessageBox.Show(thisx, b.Error.Message);
                }
                else
                {
                    r = true;
                }

                ipProgress.Close(r);
                this.Close();
            };

            ipProgress.OpenDialog(this, a =>
            {
            }, () => bgw.CancelAsync());
            bgw.RunWorkerAsync();
        }