Beispiel #1
0
        private List <JPKFakturaWiersz> CreateFakturaWiersz(BackgroundWorker worker, ref List <JPKFaktura> tabDoc, List <Product> products)
        {
            List <JPKFakturaWiersz> tab = new List <JPKFakturaWiersz>();

            var db     = new DbfFile(CodingName);
            int wzSize = 0;

            string fail = "";

            try
            {
                double index = 0;
                db.Open(Path.Combine(DbPath, "WZA.DBF"), FileMode.Open);
                JPKGenerator.SizeDB(db, ref wzSize);
                var dbRow = new DbfRecord(db.Header);

                if (tab.Count == 0)
                {
                    db.Read(0, dbRow);
                    do
                    {
                        string failNr = dbRow["RF"].Trim() + "/" + dbRow["NRDOK"].Trim();
                        try
                        {
                            if (tabDoc.Exists(d => d.P_2A == failNr) && !dbRow.IsDeleted)
                            {
                                int indexDoc         = tabDoc.FindIndex(d => d.P_2A == failNr);
                                JPKFakturaWiersz row = new JPKFakturaWiersz();
                                fail     = "P_2B";
                                row.P_2B = failNr; // numer faktury
                                fail     = "P_12";
                                row.P_12 = JPKGenerator.GetVat
                                               (dbRow["PTU"]); // stawka vat
                                fail = "P_7";
                                Product prod = products.Find(pr => pr.Code == dbRow["SYMB"].Trim());
                                row.P_7  = prod.Name;
                                fail     = "P_8A";
                                row.P_8A = prod.Quantity;
                                fail     = "P_8B";
                                row.P_8B = JPKGenerator.GetDecimal(dbRow["ILOSC"]); // ilosc

                                decimal brutto = JPKGenerator.GetDecimal(dbRow["WARTOSC"]);
                                decimal netto  = 0;
                                decimal vat    = 0;

                                if (dbRow["RF"].Trim() == "F")                         // od netto
                                {
                                    row.P_9A = JPKGenerator.GetDecimal(dbRow["CENA"]); // cena jednostkowa netto
                                    netto    = decimal.Parse((row.P_9A * row.P_8B).ToString("0.00"));
                                    vat      = brutto - netto;
                                }
                                else // od brutto
                                {
                                    decimal stawkaVat = 0;
                                    try { stawkaVat = JPKGenerator.GetDecimal(dbRow["PTU"]); } catch { }
                                    netto = stawkaVat != 0 ? (brutto * 100) / (stawkaVat + 100) : brutto;
                                    if (netto != 0)
                                    {
                                        row.P_9A = netto / row.P_8B;
                                    }
                                    vat = brutto - netto;
                                }
                                fail     = "P_11";
                                row.P_11 = row.P_8B * row.P_9A;
                                if (dbRow["RF"].Trim() != "F")
                                {
                                    row.P_11A = brutto;                            // wartosc brutto, gdy faktura liczona jest od brutto
                                }
                                fail = "vaty";
                                switch (row.P_12)
                                {
                                case JPKFakturaWierszP_12.Item22:
                                case JPKFakturaWierszP_12.Item23:
                                    tabDoc[indexDoc].P_13_1 = tabDoc[indexDoc].P_13_1 + netto;
                                    tabDoc[indexDoc].P_14_1 = tabDoc[indexDoc].P_14_1 + vat;
                                    break;

                                case JPKFakturaWierszP_12.Item8:
                                case JPKFakturaWierszP_12.Item7:
                                    tabDoc[indexDoc].P_13_2 = tabDoc[indexDoc].P_13_2 + netto;
                                    tabDoc[indexDoc].P_14_2 = tabDoc[indexDoc].P_14_1 + vat;
                                    break;

                                case JPKFakturaWierszP_12.Item5:
                                    tabDoc[indexDoc].P_13_3 = tabDoc[indexDoc].P_13_3 + netto;
                                    tabDoc[indexDoc].P_14_3 = tabDoc[indexDoc].P_14_3 + vat;
                                    break;

                                case JPKFakturaWierszP_12.Item0:
                                    tabDoc[indexDoc].P_13_6 = tabDoc[indexDoc].P_13_6 + netto;
                                    break;

                                case JPKFakturaWierszP_12.zw:
                                    tabDoc[indexDoc].P_13_7 = tabDoc[indexDoc].P_13_7 + netto;
                                    tabDoc[indexDoc].P_19   = true;
                                    tabDoc[indexDoc].P_19A  = @"Ustawa z dnia 11.03.2004 o podatku od towarów i usług, art. 43 ust. 1";
                                    break;

                                case JPKFakturaWierszP_12.np:
                                    tabDoc[indexDoc].P_13_4 = tabDoc[indexDoc].P_13_4 + netto;
                                    tabDoc[indexDoc].P_18   = true;
                                    break;
                                }

                                tab.Add(row);
                            }
                        }
                        catch (Exception ex)
                        {
                            MainStatic.ShowException(ex, "Bład pozycji faktury nr: " + failNr + "\n" + fail);
                        }
                        index++;
                        worker.ReportProgress((int)Math.Ceiling((index / wzSize) * 100), string.Format("Przetwarzanie pozycji faktur: {0}%", (int)Math.Ceiling((index / wzSize) * 100)));
                    }while (db.ReadNext(dbRow));
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Błędne kodowanie"))
                {
                    throw new Exception("Zmień kodowanie");
                }
                throw ex;
            }
            finally
            {
                db.Close();
            }

            return(tab);
        }
Beispiel #2
0
        private void CreateFakturaWierszKorekta(BackgroundWorker worker, List <Product> products, ref List <JPKFaktura> tabDoc, ref List <JPKFakturaWiersz> tab)
        {
            var db     = new DbfFile(CodingName);
            int wzSize = 0;

            string fail = "";

            try
            {
                double index = 0;
                db.Open(Path.Combine(DbPath, "ZWA.DBF"), FileMode.Open);
                JPKGenerator.SizeDB(db, ref wzSize);
                var dbRow = new DbfRecord(db.Header);

                db.Read(0, dbRow);
                do
                {
                    string failNr = "";
                    try
                    {
                        if (tabDoc.Exists(f => f.P_2A == dbRow["NRDOK"].Trim() && f.RodzajFaktury == JPKFakturaRodzajFaktury.KOREKTA) && !dbRow.IsDeleted)
                        {
                            failNr = dbRow["NRDOK"].Trim();
                            int indexKor         = tabDoc.FindIndex(f => f.P_2A == dbRow["NRDOK"].Trim() && f.RodzajFaktury == JPKFakturaRodzajFaktury.KOREKTA);
                            int typ              = int.Parse(dbRow["TYP"].Trim());
                            JPKFakturaWiersz row = new JPKFakturaWiersz();
                            fail     = "P_2B";
                            row.P_2B = dbRow["NRDOK"].Trim();                                    // numer faktury
                            fail     = "P_12";
                            row.P_12 = JPKGenerator.GetVat(dbRow["PTU"].Trim());                 // stawka vat
                            fail     = "P_7";
                            Product prod = products.Find(pr => pr.Code == dbRow["SYMB"].Trim()); // towar / usługa
                            row.P_7  = prod.Name;
                            fail     = "P_8A";
                            row.P_8A = prod.Quantity;
                            fail     = "ilosc";
                            row.P_8B = JPKGenerator.GetDecimal(dbRow["ILOSC"]); // ilosc

                            fail     = "nettoU";
                            row.P_9A = JPKGenerator.GetDecimal(dbRow["CENA"]); // cena jednostkowa netto
                            fail     = "stawkaVAT";
                            decimal stawkaVat = 0;
                            try { stawkaVat = JPKGenerator.GetDecimal(dbRow["PTU"]); } catch { }
                            if (dbRow["RF"].Trim() != "F")
                            {
                                row.P_9A = (stawkaVat != 0 ? (row.P_9A * 100) / (stawkaVat + 100) : row.P_9A) / row.P_8B;
                            }

                            switch (typ)
                            {
                            case 1:
                                fail     = "typ1";
                                row.P_8B = -row.P_8B;
                                row.P_11 = row.P_9A * -row.P_8B;
                                if (dbRow["RF"].Trim() != "F")
                                {
                                    row.P_11A = JPKGenerator.GetDecimal(dbRow["WARTOSC"]);
                                }
                                break;

                            case 2:
                                fail = "typ2";
                                if ((decimal.Parse(dbRow["WARTKOR"].Trim().Replace(".", ",")) * stawkaVat * (decimal)0.01).ToString("0.00").Replace(",", ".") == dbRow["WARTKORV"].Trim())
                                {
                                    row.P_11 = -JPKGenerator.GetDecimal(dbRow["WARTKOR"]);
                                    if (dbRow["RF"].Trim() != "F")
                                    {
                                        row.P_11A = -JPKGenerator.GetDecimal(dbRow["WARTKOR"]) + JPKGenerator.GetDecimal(dbRow["WARTKORV"]);
                                    }
                                }
                                else
                                {
                                    row.P_11 = -JPKGenerator.GetDecimal(dbRow["WARTKOR"]) - JPKGenerator.GetDecimal(dbRow["WARTKORV"]);
                                    if (dbRow["RF"].Trim() != "F")
                                    {
                                        row.P_11A = -JPKGenerator.GetDecimal(dbRow["WARTKOR"]);
                                    }
                                }
                                break;

                            case 3:
                                fail = "typ3";
                                if (JPKGenerator.GetDecimal(dbRow["WARTKOR"]) * stawkaVat * (decimal)0.01 == JPKGenerator.GetDecimal(dbRow["WARTKORV"]))
                                {
                                    row.P_11 = JPKGenerator.GetDecimal(dbRow["WARTKOR"]);
                                    if (dbRow["RF"].Trim() != "F")
                                    {
                                        row.P_11A = JPKGenerator.GetDecimal(dbRow["WARTKOR"]) + JPKGenerator.GetDecimal(dbRow["WARTKORV"]);
                                    }
                                }
                                else
                                {
                                    row.P_11  = JPKGenerator.GetDecimal(dbRow["WARTKOR"]) - JPKGenerator.GetDecimal(dbRow["WARTKORV"]);
                                    row.P_11A = JPKGenerator.GetDecimal(dbRow["WARTKOR"]);
                                }
                                break;

                            case 4:
                                fail     = "typ4";
                                row.P_11 = 0;
                                break;
                            }
                            fail = "stawka";
                            switch (row.P_12)
                            {
                            case JPKFakturaWierszP_12.Item22:
                            case JPKFakturaWierszP_12.Item23:
                                tabDoc[indexKor].P_13_1 = tabDoc[indexKor].P_13_1 + row.P_11;
                                tabDoc[indexKor].P_14_1 = row.P_11 * JPKGenerator.GetVatValue(row.P_12);
                                break;

                            case JPKFakturaWierszP_12.Item8:
                            case JPKFakturaWierszP_12.Item7:
                                tabDoc[indexKor].P_13_2 = tabDoc[indexKor].P_13_2 + row.P_11;
                                tabDoc[indexKor].P_14_1 = row.P_11 * JPKGenerator.GetVatValue(row.P_12);
                                break;

                            case JPKFakturaWierszP_12.Item5:
                                tabDoc[indexKor].P_13_3 = tabDoc[indexKor].P_13_3 + row.P_11;
                                tabDoc[indexKor].P_14_1 = row.P_11 * JPKGenerator.GetVatValue(row.P_12);
                                break;

                            case JPKFakturaWierszP_12.Item0:
                                tabDoc[indexKor].P_13_6 = tabDoc[indexKor].P_13_6 + row.P_11;
                                break;

                            case JPKFakturaWierszP_12.zw:
                                tabDoc[indexKor].P_13_7 = tabDoc[indexKor].P_13_7 + row.P_11;
                                tabDoc[indexKor].P_19   = true;
                                tabDoc[indexKor].P_19A  = @"Ustawa z dnia 11.03.2004 o podatku od towarów i usług, art. 43 ust. 1";
                                break;
                            }
                            tab.Add(row);
                        }
                    }
                    catch (Exception ex)
                    {
                        MainStatic.ShowException(ex, "Bład pozycji faktury nr: " + failNr + "\n" + fail);
                    }
                    index++;
                    worker.ReportProgress((int)Math.Ceiling((index / wzSize) * 100), string.Format("Przetwarzanie pozycji faktur: {0} %", (int)Math.Ceiling((index / wzSize) * 100)));
                }while (db.ReadNext(dbRow));
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Błędne kodowanie"))
                {
                    throw new Exception("Zmień kodowanie", ex);
                }
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }