Exemplo n.º 1
0
        void StoreStock()
        {
            //var prodDS = GetProductsDS();
            var prodDS = UniqueStocks();

            if (prodDS == null)
                return;

            var outDir = txtOutDir.Text + "/" + StockDir + "/";
            if (!Directory.Exists(outDir))
                Directory.CreateDirectory(outDir);

            dataPackType dp = new dataPackType();
            dp.version = dataPackVersionType.Item20;
            dp.note = "Export ActiveStyle";
            dp.id = "stock_" + DateTime.Now.Ticks + ".xml";
            dp.ico = Properties.Settings.Default.ActiveStyle_ICO;
            dp.application = "MessageImporter";

            List<dataPackItemType> dataPacks = new List<dataPackItemType>();
            List<dataPackItemType> prijemky = new List<dataPackItemType>();
            List<dataPackItemType> invoices = new List<dataPackItemType>();

            List<prijemkaItemType> prijItems = new List<prijemkaItemType>();
            List<invoiceItemType> invItems = new List<invoiceItemType>();

            bool GBP_part = false;
            // referencna polozka
            StockItem refProd = null;

            var allProds = new List<StockItem>();
            //allProds.AddRange(WaitingToUpdate); // waiting nepojdu do stock, iba invoice..
            allProds.AddRange(prodDS);

            /////////////////////////////////////////////// UPDATE POLOZIEK
            /*foreach (var prod in toUpdate)
            {
                if (!prod.EquippedInv && prod.State != StockItemState.Waiting) // do exportu len produkty z vybavenych objednavok
                    continue;

                var code = prod.ProductCode;

                if (string.IsNullOrEmpty(prod.Sklad) || string.IsNullOrEmpty(prod.FictivePrice))
                {
                    MessageBox.Show(this, "Not all 'Sklad' and/or 'Fiktívna cena' are filled! Missing in product with code: " + code, "Missing fields", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                dataPackItemType newDatapack = new dataPackItemType();
                newDatapack.id = code+"_update";
                newDatapack.ItemElementName = ItemChoiceType4.stock;
                newDatapack.version = dataPackItemVersionType.Item20;

                stockType stock = new stockType();
                stock.version = stkVersionType.Item20;

                // header
                stock.stockHeader = new stockHeaderType();
                // zakomentovane kvoli waiting produktom z DB, ktore tuto cenu nemaju
                //stock.stockHeader.purchasingPriceSpecified = true;
                //stock.stockHeader.purchasingPrice = prod.PriceEURnoTax;
                stock.stockHeader.sellingPrice = Common.GetPrice(prod.FictivePrice);
                stock.stockHeader.name = prod.Description;
                stock.stockHeader.nameComplement = prod.SizeInv;
                stock.stockHeader.isSalesSpecified = true;
                stock.stockHeader.isSales = boolean.@true;
                stock.stockHeader.orderName = prod.OrderDate.ToString("dd.MM.yyyy ") + prod.FromFile.ToString();
                stock.stockHeader.isInternetSpecified = true;
                stock.stockHeader.isInternet = boolean.@true;
                stock.stockHeader.sellingRateVAT = vatRateType.high;
                stock.stockHeader.stockTypeSpecified = true;
                stock.stockHeader.stockType = stockTypeType.card;
                stock.stockHeader.code = code;
                stock.stockHeader.typePrice = new refType();
                stock.stockHeader.typePrice.ids = Properties.Settings.Default.TypePrice;
                stock.stockHeader.storage = new refTypeStorage();
                stock.stockHeader.storage.ids = prod.Sklad;

                // action type - update
                stock.actionType = new actionTypeType1();
                stock.actionType.Item = new requestStockType();
                stock.actionType.ItemElementName = ItemChoiceType3.update;
                stock.actionType.Item.filter = new filterStocksType();
                stock.actionType.Item.filter.code = code;

                newDatapack.Item = stock;
                dataPacks.Add(newDatapack);
            }*/
            /////////////////////////////////////////////// STORE POLOZIEK

            /*Fix pre opakujuce sa order number (alternativa by boli maily)*/
            // MiKo zakomentovane 29.3.14 - waiting po novom..
            /*foreach (var inv in AllInvoices)
            {
                foreach (var invItem in inv.InvoiceItems)
                {
                    if (invItem.FromDB)
                    {
                      //  var orderNum = (string.IsNullOrEmpty(invItem.Parent.OrderNumber.WaitingOrderNum) ? Common.ModifyOrderNumber2(prod.PairProduct.Parent.OrderNumber) : prod.WaitingOrderNum);
                        var update = string.Format("UPDATE {0} SET VALID = \"-1\" WHERE ORDER_NUMBER=\"{1}\" AND INV_SKU = \"{2}\"", DBProvider.T_WAIT_STOCK, invItem.Parent.OrderNumber,invItem.invSKU);//prod.PairProduct.invSKU
                        DBProvider.ExecuteNonQuery(update);
                    }
                }
            }*/

            foreach (var prod in allProds)
            {
                // referencny produkt bude prvy korektny
                if (refProd == null)
                    refProd = prod;

                if (!prod.EquippedInv && prod.State != StockItemState.Waiting) // do exportu len produkty z vybavenych objednavok
                    continue;

                /////////////////////////////////////////////////// stock item

                //var code = prod.ProductCode.Replace("/", "");
                var code = prod.ProductCode;

                if (string.IsNullOrEmpty(prod.Sklad) || string.IsNullOrEmpty(prod.FictivePrice))
                {
                    MessageBox.Show(this, "Not all 'Sklad' and/or 'Fiktívna cena' are filled! Missing in product with code: " + code, "Missing fields", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                dataPackItemType newDatapack = new dataPackItemType();
                newDatapack.id = code;
                newDatapack.ItemElementName = ItemChoiceType4.stock;
                newDatapack.version = dataPackItemVersionType.Item20;

                stockType stock = new stockType();
                stock.version = stkVersionType.Item20;

                // defaultna akcia add
                stock.actionType = new actionTypeType1();
                stock.actionType.Item = new requestStockType();
                stock.actionType.Item.add = boolean.@true;
                stock.actionType.ItemElementName = ItemChoiceType3.update;
                // filter pridany 13.3.2014
                stock.actionType.Item.filter = new filterStocksType();
                stock.actionType.Item.filter.code = code;
                stock.actionType.Item.filter.store = new refType();
                stock.actionType.Item.filter.store.ids = prod.Sklad;

                // header
                stock.stockHeader = new stockHeaderType();
                stock.stockHeader.stockTypeSpecified = true;
                stock.stockHeader.stockType = stockTypeType.card;
                stock.stockHeader.code = code;
                stock.stockHeader.EAN = code;
                stock.stockHeader.isSalesSpecified = true;
                stock.stockHeader.isSales = boolean.@true;
                stock.stockHeader.isInternetSpecified = true;
                stock.stockHeader.isInternet = boolean.@true;
                stock.stockHeader.name = prod.Description;
                stock.stockHeader.nameComplement = prod.ItemNameInv;
                stock.stockHeader.unit = "ks";
                stock.stockHeader.description = prod.Description;
                stock.stockHeader.description2 = prod.ItemNameInv;

                stock.stockHeader.storage = new refTypeStorage();
                stock.stockHeader.storage.ids = prod.Sklad;

                // MiKo 29.3.14 - waiting sa ukladaju hned pri kliknuti na Waiting
                /*if (prod.State == StockItemState.Waiting)
                {
                    try
                    {
                        var orderNum = (string.IsNullOrEmpty(prod.WaitingOrderNum) ? Common.ModifyOrderNumber2(prod.PairProduct.Parent.OrderNumber) : prod.WaitingOrderNum);
                        var update = string.Format("UPDATE {0} SET ORDER_NUMBER = \"{1}\" WHERE INV_SKU=\"{2}\"", DBProvider.T_WAIT_STOCK, orderNum, prod.PairProduct.invSKU);
                        DBProvider.ExecuteNonQuery(update);

                        // ulozenie produktu do DB
                        var insert = string.Format("INSERT INTO " + DBProvider.T_WAIT_STOCK + " VALUES ({0},\"{1}\",\"{2}\",\"{3}\",\"{4}\",{5})", "null", orderNum, prod.PairProduct.invSKU, prod.ProductCode, prod.Description, 1);
                        log(insert);
                        DBProvider.ExecuteNonQuery(insert);
                        if (prod.PairProduct != null)
                            DBProvider.InsertWaitingInvoice(prod.PairProduct);
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(this, "Exception during inserting waiting product into database: " + ex.ToString(), "Execute insert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }*/

                stock.stockHeader.typePrice = new refType();
                stock.stockHeader.typePrice.ids = Properties.Settings.Default.TypePrice;

                stock.stockHeader.purchasingPriceSpecified = true;
                stock.stockHeader.purchasingPrice = prod.PriceEURnoTax;
                stock.stockHeader.sellingPrice = Common.GetPrice(prod.SellPriceEUR);
                stock.stockHeader.limitMin = 0;
                stock.stockHeader.limitMax = 0;
                stock.stockHeader.orderName = prod.OrderDate.ToString("dd.MM.yyyy ") + prod.FromFile.ToString();
                stock.stockHeader.orderQuantitySpecified = true;
                stock.stockHeader.orderQuantity = prod.Ord_Qty;
                stock.stockHeader.shortName = code;
                stock.stockHeader.guaranteeType = guaranteeTypeType.year;
                stock.stockHeader.guaranteeTypeSpecified = true;
                stock.stockHeader.guarantee = "2";

                stock.stockHeader.yield = "604000";

                //stock.stockHeader.note = prod.FictivePrice;
                stock.stockHeader.sellingPrice = Common.GetPrice(prod.FictivePrice);

                stock.stockHeader.nameComplement = prod.SizeInv;

                stock.stockHeader.sellingRateVAT = vatRateType.high;

                stock.stockHeader.acc = "132100";

                newDatapack.Item = stock;
                dataPacks.Add(newDatapack);

                /////////////////////////////////////////////////// prijemky polozky
                prijemkaItemType prijItem = new prijemkaItemType();
                prijItem.code = code;
                /*prijItem.quantitySpecified = true;
                prijItem.quantity = prod.Disp_Qty;
                prijItem.unit = "ks";
                prijItem.stockItem = new stockItemType();
                prijItem.stockItem.stockItem = new stockRefType();
                prijItem.stockItem.stockItem.ids = code;*/
                prijItems.Add(prijItem);

                /////////////////////////////////////////////////// faktura polozky
                invoiceItemType xmlItem = new invoiceItemType();
                xmlItem.code = code;
                xmlItem.text = code;
                xmlItem.stockItem = new stockItemType();
                xmlItem.stockItem.stockItem = new stockRefType();
                xmlItem.stockItem.stockItem.ids = code;
                xmlItem.quantitySpecified = true;
                xmlItem.quantity = prod.Disp_Qty;

                invItems.Add(xmlItem);

                // GBP cast pojde ak existuje subor MSG s kurzom inym ako 1
                if (!GBP_part && prod.FromFile.ExchRate != 1.0)
                    GBP_part = true;
            }

            var ticks = DateTime.Now.Ticks;

            // zabalenie prijemok
            dataPackItemType prijmekaDatapack = new dataPackItemType();
            prijmekaDatapack.id = "prijemka_" + ticks;
            prijmekaDatapack.ItemElementName = ItemChoiceType4.prijemka;
            prijmekaDatapack.version = dataPackItemVersionType.Item20;
            prijemkaType prijemka = new prijemkaType();
            prijemka.version = priVersionType.Item20;
            prijemka.prijemkaHeader = new prijemkaHeaderType();
            prijemka.prijemkaDetail = prijItems.ToArray();
            prijemka.prijemkaSummary = new prijemkaSummaryType();
            prijmekaDatapack.Item = prijemka;
            prijemky.Add(prijmekaDatapack);

            /////////////////////////////////////////////////// invoice
            dataPackItemType invDatapack = new dataPackItemType();
            invDatapack.id = "invoice_" + ticks;
            invDatapack.ItemElementName = ItemChoiceType4.invoice;
            invDatapack.version = dataPackItemVersionType.Item20;
            invoiceType newInv = new invoiceType();
            newInv.version = invVersionType.Item20;
            newInv.invoiceHeader = new invoiceHeaderType();
            newInv.invoiceHeader.invoiceType = invoiceTypeType.receivedInvoice;
            newInv.invoiceHeader.dateAccounting = dtInvDate.Value;
            newInv.invoiceHeader.dateAccountingSpecified = true;
            newInv.invoiceHeader.dateOrder = DateTime.Now;
            newInv.invoiceHeader.dateOrderSpecified = true;
            newInv.invoiceHeader.dateTax = dtInvDate.Value;
            newInv.invoiceHeader.dateTaxSpecified = true;
            newInv.invoiceHeader.dateDue = dtInvDate.Value;// DateTime.Now.AddDays(Properties.Settings.Default.DueDateAdd);
            newInv.invoiceHeader.dateDueSpecified = true;
            newInv.invoiceHeader.dateDeliverySpecified = true;
            newInv.invoiceHeader.dateDelivery = dtInvDate.Value;
            newInv.invoiceHeader.accounting = new accountingType();
            newInv.invoiceHeader.accounting.ids = "1 GBP";
            newInv.invoiceHeader.classificationVAT = new classificationVATType();
            newInv.invoiceHeader.classificationVAT.ids = "PN";
            newInv.invoiceHeader.classificationVAT.classificationVATType1 = classificationVATTypeClassificationVATType.inland;
            newInv.invoiceHeader.text = refProd.FromFile.Type.ToString() + "_" + (allMessages.Count > 0 ? allMessages[0].OrderReference : "<err>");
            newInv.invoiceHeader.partnerIdentity = new address();
            switch (refProd.FromFile.Type)
            {
                case MSG_TYPE.SPORTS_DIRECT:
                    newInv.invoiceHeader.partnerIdentity.id = Properties.Settings.Default.PartnerSports;
                    break;
                case MSG_TYPE.MANDM_DIRECT:
                    newInv.invoiceHeader.partnerIdentity.id = Properties.Settings.Default.PartnerMandM;
                    break;
                case MSG_TYPE.GETTHELABEL:
                    newInv.invoiceHeader.partnerIdentity.id = Properties.Settings.Default.PartnerLabel;
                    break;
                default:
                    newInv.invoiceHeader.partnerIdentity.id = "24";
                    break;
            }

            // polozky z faktury.. zatial fiktivne
            if (refProd.FromFile != null)
            {
                newInv.invoiceHeader.symVar = refProd.FromFile.OrderNumber;
                newInv.invoiceHeader.symPar = refProd.FromFile.OrderNumber;
            }
            newInv.invoiceHeader.numberOrder = "numOrder";
            newInv.invoiceHeader.dateSpecified = true;
            newInv.invoiceHeader.date = dtInvDate.Value;
            newInv.invoiceHeader.paymentType = new paymentType();
            newInv.invoiceHeader.paymentType.ids = "cashondelivery";

            newInv.invoiceDetail = invItems.ToArray();

            if (GBP_part)
            {
                newInv.invoiceSummary = new invoiceSummaryType();
                newInv.invoiceSummary.foreignCurrency = new typeCurrencyForeign();
                newInv.invoiceSummary.foreignCurrency.currency = new refType();
                newInv.invoiceSummary.foreignCurrency.currency.ids = "GBP";
            }

            // naplnenie cudzej meny ak je zadana
            if (refProd.FromFile != null && !string.IsNullOrEmpty(refProd.FromFile.Currency))
            {
                if (newInv.invoiceSummary == null)
                {
                    newInv.invoiceSummary = new invoiceSummaryType();
                    newInv.invoiceSummary.foreignCurrency = new typeCurrencyForeign();
                    newInv.invoiceSummary.foreignCurrency.currency = new refType();
                }
                newInv.invoiceSummary.foreignCurrency.currency.ids = refProd.FromFile.Currency;
            }

            invDatapack.Item = newInv;
            invoices.Add(invDatapack);

            // polozky do xml
            //dataPacks.AddRange(prijemky); // 6.11.2012 prijemky nejdu do exportu
            dataPacks.AddRange(invoices);

            // datapack doprava pre faktury zo sportsdirect
            dataPackItemType shippingDatapack = null;
            if (refProd.FromFile.Type == MSG_TYPE.SPORTS_DIRECT)
            {
                shippingDatapack = new dataPackItemType();
                shippingDatapack.id = "shipping_" + ticks;
                shippingDatapack.ItemElementName = ItemChoiceType4.invoice;
                shippingDatapack.version = dataPackItemVersionType.Item20;
                newInv = new invoiceType();
                newInv.version = invVersionType.Item20;
                newInv.invoiceHeader = new invoiceHeaderType();
                newInv.invoiceHeader.invoiceType = invoiceTypeType.receivedInvoice;
                newInv.invoiceHeader.dateAccounting = dtInvDate.Value;
                newInv.invoiceHeader.dateAccountingSpecified = true;
                newInv.invoiceHeader.dateOrder = dtInvDate.Value;
                newInv.invoiceHeader.dateOrderSpecified = true;
                newInv.invoiceHeader.dateTax = dtInvDate.Value;
                newInv.invoiceHeader.dateTaxSpecified = true;
                newInv.invoiceHeader.dateDue = DateTime.Now.AddDays(Properties.Settings.Default.DueDateAdd);
                newInv.invoiceHeader.dateDueSpecified = true;
                newInv.invoiceHeader.dateDeliverySpecified = true;
                newInv.invoiceHeader.dateDelivery = dtInvDate.Value;
                newInv.invoiceHeader.accounting = new accountingType();
                newInv.invoiceHeader.accounting.ids = "1 GBP";
                newInv.invoiceHeader.classificationVAT = new classificationVATType();
                newInv.invoiceHeader.classificationVAT.ids = "PN";
                newInv.invoiceHeader.classificationVAT.classificationVATType1 = classificationVATTypeClassificationVATType.inland;
                newInv.invoiceHeader.text = "SportsDirect_doprava_" + (allMessages.Count > 0 ? allMessages[0].OrderReference : "<err>");
                newInv.invoiceHeader.partnerIdentity = new address();
                newInv.invoiceHeader.partnerIdentity.id = "23";

                // polozky z faktury.. zatial fiktivne
                if (refProd.FromFile != null)
                {
                    newInv.invoiceHeader.symVar = refProd.FromFile.OrderNumber;
                    newInv.invoiceHeader.symPar = refProd.FromFile.OrderNumber;
                }
                newInv.invoiceHeader.numberOrder = "numOrder";
                newInv.invoiceHeader.dateSpecified = true;
                newInv.invoiceHeader.date = dtInvDate.Value;
                newInv.invoiceHeader.paymentType = new paymentType();
                newInv.invoiceHeader.paymentType.ids = "cashondelivery";
                // summary
                newInv.invoiceSummary = new invoiceSummaryType();
                newInv.invoiceSummary.foreignCurrency = new typeCurrencyForeign();
                if (refProd != null && refProd.FromFile != null)
                {
                    newInv.invoiceSummary.foreignCurrency.currency = new refType();
                    newInv.invoiceSummary.foreignCurrency.currency.ids = refProd.FromFile.Currency;
                }

                // detail
                List<invoiceItemType> details = new List<invoiceItemType>();
                invoiceItemType xmlItem = new invoiceItemType();
                xmlItem.text = "doprava";
                xmlItem.quantity = 1;
                xmlItem.quantitySpecified = true;
                /* presunute do summary ako foreign currency
                xmlItem.homeCurrency = new typeCurrencyHomeItem();
                xmlItem.homeCurrency.unitPriceSpecified = true;
                xmlItem.homeCurrency.unitPrice = refProd.FromFile.Delivery;
                 */
                details.Add(xmlItem);
                newInv.invoiceDetail = details.ToArray();

                shippingDatapack.Item = newInv;
            }
            if (shippingDatapack != null)
                dataPacks.Add(shippingDatapack);
            // datapack doprava

            dp.dataPackItem = dataPacks.ToArray();

            try
            {
                // ulozenie zmien do xml
                var fname = outDir + dp.id;
                dp.SaveToFile(fname);

                if (!ValidateXML(fname))
                {
                    //File.Delete(fname);
                    MessageBox.Show(this, "Validation failed! Generated xml file is not valid!", "Stock generation", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }

            MessageBox.Show("Stock XML(s) generated!", "Save XML", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Exemplo n.º 2
0
        void StoreInvoice()
        {
            var invDS = GetInvoiceDS();
            var invItemsDS = GetInvoiceItemsDS();

            if (invDS == null || invItemsDS == null)
                return;

            var outDir = txtOutDir.Text + "/" + InvoiceDir + "/";
            if (!Directory.Exists(outDir))
                Directory.CreateDirectory(outDir);

            dataPackType dp = new dataPackType();
            dp.version = dataPackVersionType.Item20;
            dp.note = "Export ActiveStyle";
            dp.id = "inv_" + DateTime.Now.Ticks + ".xml";
            dp.ico = Properties.Settings.Default.ActiveStyle_ICO;
            dp.application = "MessageImporter";

            List<dataPackItemType> items = new List<dataPackItemType>();
            foreach (var inv in invDS)
            {
                // do exportu pojdu len vybavene objednavky, nestornovane
                if (!inv.Equipped || inv.Cancelled)
                    continue;

                var orderNr = Common.ModifyOrderNumber(inv.OrderNumber);

                dataPackItemType newDatapack = new dataPackItemType();
                newDatapack.ItemElementName = ItemChoiceType4.invoice;
                newDatapack.id = orderNr;
                newDatapack.version = dataPackItemVersionType.Item20;

                // faktura
                invoiceType newInv = new invoiceType();
                newInv.version = invVersionType.Item20;

                // header
                newInv.invoiceHeader = new invoiceHeaderType();
                newInv.invoiceHeader.symVar = orderNr;
                newInv.invoiceHeader.symPar = orderNr;
                newInv.invoiceHeader.invoiceType = invoiceTypeType.issuedInvoice;
                newInv.invoiceHeader.dateAccounting = DateTime.Now;
                newInv.invoiceHeader.dateAccountingSpecified = true;
                newInv.invoiceHeader.dateOrder = DateTime.Parse(inv.OrderDate);
                newInv.invoiceHeader.dateOrderSpecified = true;
                newInv.invoiceHeader.dateTax = DateTime.Now;
                newInv.invoiceHeader.dateTaxSpecified = true;
                newInv.invoiceHeader.dateDue = DateTime.Now.AddDays(Properties.Settings.Default.DueDateAdd);
                newInv.invoiceHeader.dateDueSpecified = true;
                newInv.invoiceHeader.accounting = new accountingType();
                newInv.invoiceHeader.accounting.ids = Properties.Settings.Default.Accounting + GetAccountingSuffix(inv.Country);
                newInv.invoiceHeader.classificationVAT = new classificationVATType();
                newInv.invoiceHeader.classificationVAT.ids = Properties.Settings.Default.ClasifficationVAT;
                newInv.invoiceHeader.classificationVAT.classificationVATType1 = classificationVATTypeClassificationVATType.inland;
                newInv.invoiceHeader.text = inv.TrashNumber + " " + inv.OrderGrandTotal;
                // KV DPH pre SK faktury
                if (inv.Country == Country.Slovakia)
                {
                    newInv.invoiceHeader.classificationKVDPH = new refType();
                    newInv.invoiceHeader.classificationKVDPH.ids = "D2";
                }

                // header->identity
                newInv.invoiceHeader.partnerIdentity = new address();
                newInv.invoiceHeader.partnerIdentity.address1 = new addressType();
                newInv.invoiceHeader.partnerIdentity.address1.name = inv.BillingName;

                newInv.invoiceHeader.partnerIdentity.address1.ico = inv.icoNumber;
                newInv.invoiceHeader.partnerIdentity.address1.dic = inv.dicNumber;

                newInv.invoiceHeader.partnerIdentity.address1.street = inv.BillingStreet;
                newInv.invoiceHeader.partnerIdentity.address1.zip = inv.BillingZip;
                newInv.invoiceHeader.partnerIdentity.address1.phone = inv.BillingPhoneNumber;
                newInv.invoiceHeader.partnerIdentity.address1.city = inv.BillingCity;
                newInv.invoiceHeader.partnerIdentity.address1.company = inv.BillingCompany;
                newInv.invoiceHeader.partnerIdentity.address1.country = new refType();
                newInv.invoiceHeader.partnerIdentity.address1.country.ids = inv.BillingCountry;
                newInv.invoiceHeader.partnerIdentity.address1.email = inv.CustomerEmail;
                newInv.invoiceHeader.partnerIdentity.shipToAddress = new shipToAddressType[1];
                newInv.invoiceHeader.partnerIdentity.shipToAddress[0] = new shipToAddressType();
                newInv.invoiceHeader.partnerIdentity.shipToAddress[0].name = inv.ShippingName;
                newInv.invoiceHeader.partnerIdentity.shipToAddress[0].street = inv.ShippingStreet;
                newInv.invoiceHeader.partnerIdentity.shipToAddress[0].city = inv.ShippingCity;
                newInv.invoiceHeader.partnerIdentity.shipToAddress[0].zip = inv.ShippingZip;
                newInv.invoiceHeader.partnerIdentity.shipToAddress[0].company = inv.ShippingCompany;

                newInv.invoiceHeader.numberOrder = orderNr;
                newInv.invoiceHeader.dateSpecified = true;
                newInv.invoiceHeader.date = DateTime.Now;
                newInv.invoiceHeader.symConst = Properties.Settings.Default.ConstSym;
                newInv.invoiceHeader.note = inv.CustomerEmail;
                newInv.invoiceHeader.intNote = inv.ShippingPhoneNumber;

                newInv.invoiceHeader.paymentType = new paymentType();
                newInv.invoiceHeader.paymentType.ids = inv.OrderPaymentMethod;

                if (inv.Country == Country.Hungary)
                {
                    newInv.invoiceHeader.regVATinEU = new refTypeRegVATinEU();
                    newInv.invoiceHeader.regVATinEU.ids = "HU26912248251";
                }
                newInv.invoiceHeader.account = new accountType();
                newInv.invoiceHeader.account.bankCode = Properties.Settings.Default.BankCode;
                newInv.invoiceHeader.account.ids = Properties.Settings.Default.Bank;

                newInv.invoiceHeader.number = new numberType();
                if (Properties.Settings.Default.UseSkkSerie && inv.Country == Country.Slovakia)
                    newInv.invoiceHeader.number.ids = Properties.Settings.Default.SkkSerie;
                else if (Properties.Settings.Default.UseCzkSerie && inv.Country == Country.CzechRepublic)
                    newInv.invoiceHeader.number.ids = Properties.Settings.Default.CzkSerie;
                else if (Properties.Settings.Default.UseHufSerie && inv.Country == Country.Hungary)
                    newInv.invoiceHeader.number.ids = Properties.Settings.Default.HufSerie;
                else if (Properties.Settings.Default.UsePlnSerie && inv.Country == Country.Poland)
                    newInv.invoiceHeader.number.ids = Properties.Settings.Default.PlnSerie;
                else
                    newInv.invoiceHeader.number = null;

                // polozky faktury
                var invItems = new List<invoiceItemType>();
                foreach (var invItem in inv.InvoiceItems)
                {
                    invoiceItemType xmlItem = new invoiceItemType();

                    var code = "";
                    if (invItem.PairCode != null)
                        code = invItem.PairCode;

                    // zlava
                    var zlava = Common.GetPrice(invItem.Zlava_Pohoda);
                    if (double.IsNaN(zlava))
                        zlava = 0;
                    xmlItem.discountPercentage = (float)zlava;

                    // specialna polozka "cena za dopravu"
                    if (code == Properties.Settings.Default.ShippingCode)
                    {
                        xmlItem.text = invItem.MSG_SKU;
                        xmlItem.quantitySpecified = true;
                        xmlItem.quantity = 1;
                        if (inv.Country == Country.Slovakia)
                        {
                            xmlItem.homeCurrency = new typeCurrencyHomeItem();
                            xmlItem.homeCurrency.unitPriceSpecified = true;
                            xmlItem.homeCurrency.unitPrice = Common.GetPrice(invItem.ItemPrice);
                        }
                        else
                        {
                            xmlItem.foreignCurrency = new typeCurrencyForeignItem();
                            xmlItem.foreignCurrency.unitPriceSpecified = true;
                            xmlItem.foreignCurrency.unitPrice = Common.GetPrice(invItem.ItemPrice);
                        }
                        xmlItem.accounting = new refType();
                        xmlItem.accounting.ids = "2" + GetAccountingSuffix(inv.Country);

                        xmlItem.payVAT = boolean.@true;
                        if (inv.Country == Country.Hungary)
                        {
                            xmlItem.rateVAT = vatRateType.historyHigh;
                            xmlItem.percentVATSpecified = true;
                            xmlItem.percentVAT = 27;
                        }
                        else
                        {
                            xmlItem.rateVAT = vatRateType.high;
                            xmlItem.percentVATSpecified = true;
                            xmlItem.percentVAT = 20;
                        }
                    }
                    else
                    {
                        xmlItem.code = code;
                        xmlItem.text = code;// invItem.ItemName;
                        xmlItem.quantitySpecified = true;
                        float qty = 1;
                        if (!float.TryParse(invItem.ItemQtyOrdered, out qty))
                            qty = 1;
                        xmlItem.quantity = qty;
                        xmlItem.unit = "ks";
                        /*xmlItem.homeCurrency = new typeCurrencyHomeItem();
                        xmlItem.homeCurrency.unitPriceSpecified = true;
                        xmlItem.homeCurrency.unitPrice = Common.GetPrice(invItem.ItemPrice);
                        xmlItem.homeCurrency.priceVATSpecified = true;
                        xmlItem.homeCurrency.priceVAT = Common.GetPrice(invItem.ItemTax);
                        xmlItem.homeCurrency.priceSpecified = true;
                        xmlItem.homeCurrency.price = Common.GetPrice(invItem.ItemTotal) - Common.GetPrice(invItem.ItemDiscount) / 1.2;
                        xmlItem.homeCurrency.priceSumSpecified = true;
                        xmlItem.homeCurrency.priceSum = xmlItem.homeCurrency.price;*/
                        xmlItem.percentVATSpecified = true;
                        xmlItem.percentVAT = Properties.Settings.Default.DPH_percent;

                        xmlItem.payVAT = boolean.@true;
                        if (inv.Country == Country.Hungary)
                        {
                            xmlItem.rateVAT = vatRateType.historyHigh;
                            xmlItem.percentVATSpecified = true;
                            xmlItem.percentVAT = 27;
                        }

                        // stock item
                        xmlItem.stockItem = new stockItemType();
                        xmlItem.stockItem.stockItem = new stockRefType();
                        xmlItem.stockItem.stockItem.ids = code;
                    }

                    // ak su ine krajiny ako SK, vyplnime predajnu cenu do foreignCurrency
                    if (!double.IsNaN(invItem.PredajnaCena))
                    {
                        if (inv.Country != Country.Slovakia)
                        {
                            xmlItem.foreignCurrency = new typeCurrencyForeignItem();
                            xmlItem.foreignCurrency.unitPriceSpecified = true;
                            xmlItem.foreignCurrency.unitPrice = invItem.PredajnaCena;
                        }
                        else
                        {
                            xmlItem.homeCurrency = new typeCurrencyHomeItem();
                            xmlItem.homeCurrency.unitPriceSpecified = true;
                            xmlItem.homeCurrency.unitPrice = invItem.PredajnaCena;
                        }
                    }

                    invItems.Add(xmlItem);
                }
                /*if (inv.OrderShippingMethod.Contains("freeshipping"))
                {
                    // za dopravu sa neplati
                }
                else
                {
                    var shipping = new invoiceItemType();
                    shipping.text = "Cena za dopravu";
                    shipping.quantitySpecified = true;
                    shipping.quantity = 1;
                    shipping.rateVAT = vatRateType.high;
                    shipping.homeCurrency = new typeCurrencyHomeItem();
                    shipping.homeCurrency.unitPriceSpecified = true;
                    shipping.homeCurrency.unitPrice = (Math.Ceiling(Common.GetPrice(inv.OrderShipping) * 1.2 * 100)-1) / 100;

                    invItems.Add(shipping);
                }*/

                newInv.invoiceDetail = invItems.ToArray();

                // specialita pre madarsko
                if (inv.Country == Country.Hungary)
                {
                    newInv.invoiceSummary = new invoiceSummaryType();
                    newInv.invoiceSummary.foreignCurrency = new typeCurrencyForeign();
                    newInv.invoiceSummary.foreignCurrency.currency = new refType();
                    newInv.invoiceSummary.foreignCurrency.currency.ids = "HUF";
                }
                if (inv.Country == Country.CzechRepublic)
                {
                    newInv.invoiceSummary = new invoiceSummaryType();
                    newInv.invoiceSummary.foreignCurrency = new typeCurrencyForeign();
                    newInv.invoiceSummary.foreignCurrency.currency = new refType();
                    newInv.invoiceSummary.foreignCurrency.currency.ids = "CZK";
                }
                if (inv.Country == Country.Poland)
                {
                    newInv.invoiceSummary = new invoiceSummaryType();
                    newInv.invoiceSummary.foreignCurrency = new typeCurrencyForeign();
                    newInv.invoiceSummary.foreignCurrency.currency = new refType();
                    newInv.invoiceSummary.foreignCurrency.currency.ids = "PLN";
                }

                // invoice do datapacku a datapack do vysledneho pola
                newDatapack.Item = newInv;
                items.Add(newDatapack);
            }

            // polozky do xml
            dp.dataPackItem = items.ToArray();

            try
            {
                // ulozenie zmien do xml
                var fname = outDir + dp.id;
                dp.SaveToFile(fname);

                if (!ValidateXML(fname))
                {
                    //File.Delete(fname);
                    MessageBox.Show(this, "Validation failed! Generated xml file is not valid!", "Invoice generation", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }

            ///////////////////////////////////////////////////////////////////////////////////////////
            // export pre citacku
            ReaderExport(invDS, outDir);

            MessageBox.Show("Invoice XML generated!", "Save XML", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }