Пример #1
0
        /// <summary>
        ///
        /// </summary>
        private void UpdateParty()
        {
            IList <crmCParty> partys = ObjectSpace.GetObjects <crmCParty>(CriteriaOperator.Parse("Person is Null"));

            foreach (crmCParty party in partys)
            {
                if (party.ComponentType == typeof(crmCLegalPerson))
                {
                    crmCLegalPerson lp = (crmCLegalPerson)party.ComponentObject;
                    party.Person = lp.Person;
                }
                if (party.ComponentType == typeof(crmCLegalPersonUnit))
                {
                    crmCLegalPersonUnit lpu = (crmCLegalPersonUnit)party.ComponentObject;
                    if (lpu.LegalPerson != null)
                    {
                        party.Person = lpu.LegalPerson.Person;
                    }
                }
                if (party.ComponentType == typeof(crmCPhysicalParty))
                {
                    crmCPhysicalParty php = (crmCPhysicalParty)party.ComponentObject;
                    if (php.Person != null)
                    {
                        party.Person = php.Person.Person;
                    }
                }
                if (party.ComponentType == typeof(crmCBusinessman))
                {
                    crmCBusinessman bm = (crmCBusinessman)party.ComponentObject;
                    if (bm.Person != null)
                    {
                        party.Person = bm.Person.Person;
                    }
                }
            }
        }
        private void ProcessRecord(fmCAVTBookBuhStruct struct_book, IObjectSpace os, fmCAVTBookBuhStructRecord record, InvoiceType invoice_type)
        {
            crmCParty party;

            if (String.IsNullOrEmpty(record.PartnerInn))
            {
                throw new InvalidDataException("Незаполнен ИНН Рег: " + record.InvoiceRegNumber);
            }
            //                    crmCLegalPerson leg_person;
            //                    crmCLegalPersonUnit legunit_person;
            //                    person = os.GetObjects<crmCPerson>(new BinaryOperator("INN", record.PartnerInn)).FirstOrDefault();
            if (invoice_type == InvoiceType.InvoiceOut && record.SaleDate < new DateTime(1990, 01, 01) && record.SaleSummVAT != 0)
            {
                record.SaleDate = record.TransferDate;
            }
            if (record.PartnerParty == null)
            {
                if (record.InvoiceType == "ГТД")
                {
                    party = os.GetObjects <crmCParty>(new BinaryOperator("Code", record.PartnerInn)).FirstOrDefault();
                }
                else
                if (record.PartnerInn == "-")
                {
                    if (record.InvoiceType == "СФЗ" || record.PartnerName.Trim().ToUpper() == "НАСЕЛЕНИЕ")
                    {
                        party = os.GetObjects <crmCParty>(new BinaryOperator("Code", "2706")).FirstOrDefault();
                        //                    record.InvoiceType = "СФЗ";
                    }
                    else
                    {
                        String[] fio = record.PartnerName.Split(' ');
                        if (fio.Length != 3)
                        {
                            throw new InvalidDataException("Незаполнен ФИО физического лица '" + fio + "' Рег: " + record.InvoiceRegNumber);
                        }
                        crmCPhysicalParty person = os.GetObjects <crmCPhysicalParty>(
                            new BinaryOperator("LastName", fio[0]) &
                            new BinaryOperator("FirstName", fio[1]) &
                            new BinaryOperator("MiddleName", fio[2])
                            , true).FirstOrDefault();
                        if (person == null)
                        {
                            person                              = os.CreateObject <crmCPhysicalParty>();
                            person.INN                          = "-";
                            person.Name                         = record.PartnerName;
                            person.NameHandmake                 = record.PartnerName;
                            person.LastName                     = fio[0];
                            person.FirstName                    = fio[1];
                            person.MiddleName                   = fio[2];
                            person.AddressLegal.City            = record.PartnerSity;
                            person.AddressLegal.AddressHandmake = "РФ " + record.PartnerSity + " " + record.PartnerAddress;
                            //                        person.AddressFact.City = record.PartnerSity;
                            //                        person.AddressFact.AddressHandmake = "РФ " + record.PartnerSity + " " + record.PartnerAddress;
                            //                        party = person.Party;
                        }

                        party = person.Party;
                    }
                }
                else
                {
                    if (String.IsNullOrEmpty(record.PartnerKpp) || record.PartnerKpp == "-")
                    {
                        if (record.PartnerInn.Length != 12)
                        {
                            throw new FormatException("Некорректная длина ИНН Рег: " + record.InvoiceRegNumber);
                        }
                        crmCBusinessman person = os.GetObjects <crmCBusinessman>(new BinaryOperator("INN", record.PartnerInn), true).FirstOrDefault();
                        //                         phys_person =
                        if (person == null)
                        {
                            person     = os.CreateObject <crmCBusinessman>();
                            person.INN = record.PartnerInn;
                            String[] name_comps = record.PartnerName.Split(' ');
                            if (name_comps.Length >= 2)
                            {
                                person.LastName = name_comps[1];
                            }
                            else
                            {
                                person.LastName = "-";
                            }
                            if (name_comps.Length >= 3)
                            {
                                person.FirstName = name_comps[2];
                            }
                            else
                            {
                                person.FirstName = "-";
                            }
                            if (name_comps.Length == 4)
                            {
                                person.MiddleName = name_comps[3];
                            }
                            else
                            {
                                person.MiddleName = "-";
                            }
                            person.Name                         = record.PartnerName;
                            person.NameHandmake                 = record.PartnerName;
                            person.AddressLegal.City            = record.PartnerSity;
                            person.AddressLegal.AddressHandmake = "РФ " + record.PartnerSity + " " + record.PartnerAddress;
                            person.AddressFact.City             = record.PartnerSity;
                            person.AddressFact.AddressHandmake  = "РФ " + record.PartnerSity + " " + record.PartnerAddress;
                        }
                        party = person.Party;
                    }
                    else
                    {
                        if (record.PartnerInn.Length != 10)
                        {
                            throw new FormatException("Некорректная длина ИНН Рег: " + record.InvoiceRegNumber);
                        }
                        if (record.PartnerKpp.Length != 9)
                        {
                            throw new FormatException("Некорректная длина КПП Рег: " + record.InvoiceRegNumber);
                        }
                        party = os.GetObjects <crmCParty>(new BinaryOperator("INN", record.PartnerInn) &
                                                          new BinaryOperator("KPP", record.PartnerKpp), true).FirstOrDefault();
                        if (party == null)
                        {
                            crmCLegalPerson person;
                            person = os.GetObjects <crmCLegalPerson>(new BinaryOperator("INN", record.PartnerInn), true).FirstOrDefault();
                            if (person == null)
                            {
                                person      = os.CreateObject <crmCLegalPerson>();
                                person.INN  = record.PartnerInn;
                                person.KPP  = record.PartnerKpp;
                                person.Name = record.PartnerName;
                                person.AddressLegal.City            = record.PartnerSity;
                                person.AddressLegal.AddressHandmake = "РФ " + record.PartnerSity + " " + record.PartnerAddress;
                                person.AddressFact.City             = record.PartnerSity;
                                person.AddressFact.AddressHandmake  = "РФ " + record.PartnerSity + " " + record.PartnerAddress;
                                party = person.Party;
                            }
                            if (person.Party.KPP == record.PartnerKpp)
                            {
                                party = person.Party;
                            }
                            else
                            {
                                crmCLegalPersonUnit unit = person.LegalPersonUnits.FirstOrDefault(x => x.KPP == record.PartnerKpp);
                                if (unit == null)
                                {
                                    unit = os.CreateObject <crmCLegalPersonUnit>();
                                    person.LegalPersonUnits.Add(unit);
                                    unit.KPP                         = record.PartnerKpp;
                                    unit.Name                        = record.PartnerName;
                                    unit.AddressFact.City            = record.PartnerSity;
                                    unit.AddressFact.AddressHandmake = "РФ " + record.PartnerSity + " " + record.PartnerAddress;
                                }
                                party = unit.Party;
                            }
                        }
                    }
                    //                        person = os.GetObjects<crmCPerson>(new BinaryOperator("INN", record.PartnerInn)).FirstOrDefault()
                }
                record.PartnerParty = party;
            }
            if (record.InvoiceType == "СЧФ" || record.InvoiceType == "УПД" || record.InvoiceType == "СФА")
            {
                fmCAVTInvoiceBase invoice;

                if (invoice_type == InvoiceType.InvoiceOut)
                {
                    invoice = os.FindObject <fmCAVTInvoiceBase>(
                        CriteriaOperator.And(new BinaryOperator("RegNumber", record.InvoiceRegNumber),
                                             new BinaryOperator("Date", record.InvoiceDate.Date, BinaryOperatorType.GreaterOrEqual),
                                             new BinaryOperator("Date", record.InvoiceDate.Date.AddDays(1), BinaryOperatorType.Less),
                                             new BinaryOperator("Supplier", struct_book.Party)
                                             //                                         new BinaryOperator("Customer", record.PartnerParty)
                                             ), true
                        );
                    if (invoice == null)
                    {
                        invoice           = os.CreateObject <fmCAVTInvoiceBase>();
                        invoice.RegNumber = record.InvoiceRegNumber;
                        invoice.Number    = record.InvoiceNumber;
                        invoice.Date      = record.InvoiceDate;
                        invoice.Supplier  = struct_book.Party;
                        invoice.Customer  = record.PartnerParty;
                    }
                    if (invoice != null)
                    {
                        if (invoice.Customer != record.PartnerParty ||
                            invoice.Supplier != struct_book.Party)
                        {
                            throw new InvalidDataException("Счет фактура с регистрационным номером " + record.InvoiceRegNumber + " уже зарегистрирован с другим партнером " + invoice.Customer.Name);
                        }
                        if (invoice.InvoiceType == null && invoice.Number != null && invoice.Number.Length > 0)
                        {
                            foreach (fmCAVTInvoiceType inv_type in _InvoiceTypes)
                            {
                                if (inv_type.InvoiceDirection == fmAVTInvoiceDirection.AVTInvoiceOut &&
                                    inv_type.Prefix == invoice.Number.Substring(0, 1))
                                {
                                    invoice.InvoiceType = inv_type;
                                }
                            }
                        }
                        invoice.SummCost = record.SummCost;
                        invoice.SummAVT  = record.SummVAT;
                        record.Invoice   = invoice;
                    }
                }
                if (invoice_type == InvoiceType.InvoiceIn)
                {
                    invoice = os.FindObject <fmCAVTInvoiceBase>(
                        CriteriaOperator.And(new BinaryOperator("RegNumber", record.InvoiceRegNumber),
                                             new BinaryOperator("Date", record.InvoiceDate.Date, BinaryOperatorType.GreaterOrEqual),
                                             new BinaryOperator("Date", record.InvoiceDate.Date.AddDays(1), BinaryOperatorType.Less),
                                             //                                         new BinaryOperator("Supplier", record.PartnerParty),
                                             new BinaryOperator("Customer", struct_book.Party)
                                             ), true
                        );
                    if (invoice == null)
                    {
                        invoice           = os.CreateObject <fmCAVTInvoiceBase>();
                        invoice.RegNumber = record.InvoiceRegNumber;
                        invoice.Number    = record.InvoiceNumber;
                        invoice.Date      = record.InvoiceDate;
                        invoice.Supplier  = record.PartnerParty;
                        invoice.Customer  = struct_book.Party;
                    }
                    if (invoice != null)
                    {
                        if (invoice.Supplier != record.PartnerParty ||
                            invoice.Customer != struct_book.Party)
                        {
                            throw new InvalidDataException("Счет фактура с регистрационным номером " + record.InvoiceRegNumber + " уже зарегистрирован с другим партнером " + invoice.Supplier.Name);
                        }
                        invoice.SummCost = record.SummCost;
                        invoice.SummAVT  = record.SummVAT;
                        record.Invoice   = invoice;
                    }
                }
            }
        }
Пример #3
0
        public void import(String filename)
        {
//            CsvEngine engine = new CsvEngine(new CsvOptions("ImportPartyRecord", ';',6));
//            engine.Options.IgnoreFirstLines = 1;
//            Npgsql.NpgsqlConnection con = new Npgsql.NpgsqlConnection("Server=npomash;User Id=pg_adm;Password='******';Database=ermdev;Encoding=UNICODE;");
//            IXpoDataStoreProvider dsp = new ConnectionDataStoreProvider(con);

//            IDbConnection con = PostgreSqlConnectionProvider.CreateConnection("Server=npomash;User Id=pg_adm;Password='******';Database=ermdev;Encoding=UNICODE;");
            IDbConnection con = PostgreSqlConnectionProvider.CreateConnection("Server=alt-dev;User Id=pg_adm;Password='******';Database=ermprod;Encoding=UNICODE;");
//
            IDataLayer   data_layer   = XpoDefault.GetDataLayer(con, AutoCreateOption.None);
            IObjectLayer object_layer = new SimpleObjectLayer(data_layer);
            Int32        count        = 0;

//            ObjectSpaceProvider osp = new ObjectSpaceProvider(dsp);
            //
//            XafApplication application = new ImportApplication();
//            ModuleBase testModule = new ModuleBase();
//            testModule.AdditionalExportedTypes.Add(typeof(crmCParty));
//            testModule.AdditionalExportedTypes.Add(typeof(crmCBusinessman));
//            testModule.AdditionalExportedTypes.Add(typeof(crmCPhysicalParty));
//            testModule.AdditionalExportedTypes.Add(typeof(crmCLegalPerson));
//            application.Modules.Add(testModule);
//            application.Setup("ImportApplication", osp);
//            using (IObjectSpace os = application.CreateObjectSpace()) {
            using (UnitOfWork uow_base = new UnitOfWork(object_layer)) {
                DataTable tbl = (new CsvEngine("ImportPartyRecord", ';', 6)).ReadFileAsDT(filename);
                foreach (DataRow row in tbl.Rows)
                {
                    using (UnitOfWork uow = uow_base.BeginNestedUnitOfWork()) {
                        ImportPartyRecord rec = new ImportPartyRecord();
                        rec.Type = ((String)row.ItemArray[0]).Trim();
                        rec.Code = ((String)row.ItemArray[1]).Trim();
                        rec.INN  = ((String)row.ItemArray[2]).Trim();
                        rec.KPP  = ((String)row.ItemArray[3]).Trim();
                        rec.Name = ((String)row.ItemArray[4]).Trim();
                        rec.Addr = ((String)row.ItemArray[5]).Trim();
                        System.Console.WriteLine(rec.Type + "@" + rec.Code + "@" + rec.INN + "@" + rec.KPP + "@" + rec.Name + "@" + rec.Addr);
                        XPQuery <crmCParty> q_party = new XPQuery <crmCParty>(uow, true);
                        crmCParty           party   = q_party.FirstOrDefault(obj => obj.Code == rec.Code);
                        if (party != null)
                        {
                            continue;
                        }
                        switch (rec.Type)
                        {
                        case "ИП":
                            crmCBusinessman bm = new crmCBusinessman(uow);
                            bm.Code = rec.Code;
                            bm.Name = rec.Name;
                            bm.INN  = rec.INN;
                            bm.AddressLegal.AddressHandmake = rec.Addr;
                            bm.AddressFact.AddressHandmake  = rec.Addr;
                            break;

                        case "ФЛ":
                            crmCPhysicalParty phl = new crmCPhysicalParty(uow);
                            phl.Code = rec.Code;
                            phl.Name = rec.Name;
                            phl.INN  = rec.INN;
                            phl.AddressLegal.AddressHandmake = rec.Addr;
                            phl.AddressFact.AddressHandmake  = rec.Addr;
                            break;

                        default:
                            crmCLegalPerson lp = new crmCLegalPerson(uow);
                            lp.Code = rec.Code;
                            lp.Name = rec.Name;
                            lp.INN  = rec.INN;
                            lp.KPP  = rec.KPP;
                            lp.AddressLegal.AddressHandmake = rec.Addr;
                            lp.AddressFact.AddressHandmake  = rec.Addr;
                            break;
                        }
                        uow.CommitChanges();
                        count++;
                    }
                }
                uow_base.CommitChanges();
                System.Console.WriteLine("All count: " + count);
            }
        }
Пример #4
0
        //
        static void Main(string[] args)
        {
            Session bses = GetSession();
            int     i    = 1;

            if (args[0] == "city")
            {
                using (UnitOfWork ses = bses.BeginNestedUnitOfWork()) {
                    XPQuery <csAddress> aq = new XPQuery <csAddress>(ses);
                    var q = from address in aq
                            where (address.City == null ||
                                   address.City == "" ||
                                   address.City == " ") &&
                            !(address.AddressString == null ||
                              address.AddressString == "" ||
                              address.AddressString == " ")
                            select address;
                    foreach (csAddress address in q)
                    {
                        System.Console.WriteLine(address.AddressString + " : " +
                                                 address.AddressHandmake + " : " +
                                                 address.City);
                        address.City = "Не задан";
                    }
                    System.Console.WriteLine(q.Count());
                    ses.CommitChanges();
                }
            }
            if (args[0] == "party")
            {
                Excel.Application xlApp       = null;
                Excel.Workbook    xlWorkBook  = null;
                Excel.Worksheet   xlWorkSheet = null;

                xlApp = new Excel.ApplicationClass();

                // Открытие документа (книги) excel
                xlWorkBook = xlApp.Workbooks.Open("e:\\party2.xls");

                // Выбирается Лист (если существует)
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                //
                //try {
                while (true)
                {
                    using (UnitOfWork ses = bses.BeginNestedUnitOfWork()) {
                        i++;
                        ExcelParty ep = new ExcelParty();
                        //                    Excel.Range range = xlWorkSheet.get_Range("A" + i, "P" + i);
                        //                    Excel.Range range2 = xlWorkSheet.get_Range("A1");
                        if (xlWorkSheet.get_Range("A" + i).Value2 == null)
                        {
                            break;
                        }
                        ep.code = xlWorkSheet.get_Range("A" + i).Value2.ToString();
                        ep.type = xlWorkSheet.get_Range("B" + i).Value2.ToString();
                        if (xlWorkSheet.get_Range("C" + i).Value2 != null)
                        {
                            ep.close = xlWorkSheet.get_Range("C" + i).Value2.ToString();
                        }
                        else
                        {
                            ep.close = String.Empty;
                        }
                        if (xlWorkSheet.get_Range("E" + i).Value2 != null)
                        {
                            ep.country = UpFirstCase(xlWorkSheet.get_Range("E" + i).Value2.ToString());
                        }
                        else
                        {
                            ep.country = String.Empty;
                        }
                        if (xlWorkSheet.get_Range("F" + i).Value2 != null)
                        {
                            ep.city = UpFirstCase(xlWorkSheet.get_Range("F" + i).Value2.ToString());
                        }
                        else
                        {
                            ep.city = "-";
                        }
                        if (xlWorkSheet.get_Range("G" + i).Value2 != null)
                        {
                            ep.legal = xlWorkSheet.get_Range("G" + i).Value2.ToString();
                        }
                        else
                        {
                            ep.legal = String.Empty;
                        }
                        if (xlWorkSheet.get_Range("H" + i).Value2 != null)
                        {
                            ep.lpt = xlWorkSheet.get_Range("H" + i).Value2.ToString();
                        }
                        else
                        {
                            ep.lpt = "-";
                        }
                        ep.inn = xlWorkSheet.get_Range("I" + i).Value2.ToString();
                        if (xlWorkSheet.get_Range("J" + i).Value2 != null)
                        {
                            ep.kpp = xlWorkSheet.get_Range("J" + i).Value2.ToString();
                        }
                        else
                        {
                            ep.kpp = String.Empty;
                        }
                        ep.name = xlWorkSheet.get_Range("K" + i).Value2.ToString();
                        ep.addr = xlWorkSheet.get_Range("L" + i).Value2.ToString();
                        //                    party.NameFull = party.Name;
                        //                    party.AddressFact.AddressHandmake = xlWorkSheet.get_Range("L" + i, "L" + i).Value2.ToString();
                        //                    party.INN = xlWorkSheet.get_Range("I" + i, "I" + i).Value2.ToString();
                        //                    party.KPP = xlWorkSheet.get_Range("J" + i, "J" + i).Value2.ToString();
                        switch (ep.type)
                        {
                        case "ЛЮ":
                            crmCLegalPerson lp = NewLegalPerson(ses, ep);
                            //lp.Save();
                            break;

                        case "ЛФ":
                            crmCPhysicalParty php = new crmCPhysicalParty(ses);
                            php.Code                         = ep.code;
                            php.NameHandmake                 = ep.name;
                            php.INN                          = ep.inn;
                            php.AddressLegal.Country         = GetCountry(ses, ep.country);
                            php.AddressLegal.City            = ep.city;
                            php.AddressLegal.AddressHandmake = ep.addr;
                            php.AddressFact.Country          = php.AddressLegal.Country;
                            php.AddressFact.City             = ep.city;
                            php.AddressFact.AddressHandmake  = ep.addr;
                            if (!String.IsNullOrEmpty(ep.close.Trim()))
                            {
                                php.IsClosed = true;
                            }
                            //php.Save();
                            break;

                        case "ИП":
                            crmCBusinessman bp = new crmCBusinessman(ses);
                            bp.Code                         = ep.code;
                            bp.NameHandmake                 = ep.name;
                            bp.INN                          = ep.inn;
                            bp.PersonType                   = GetPersonType(ses, ep.lpt);
                            bp.AddressLegal.Country         = GetCountry(ses, ep.country);
                            bp.AddressLegal.City            = ep.city;
                            bp.AddressLegal.AddressHandmake = ep.addr;
                            bp.AddressFact.Country          = bp.AddressLegal.Country;
                            bp.AddressFact.City             = ep.city;
                            bp.AddressFact.AddressHandmake  = ep.addr;
                            if (!String.IsNullOrEmpty(ep.close.Trim()))
                            {
                                bp.IsClosed = true;
                            }
                            //bp.Save();
                            break;

                        case "ФИЛИАЛ":
                            crmCLegalPersonUnit lpu = UpdateLegalPersonUnit(ses, ep);
                            //lpu.Save();
                            break;

                        case "ПРОЧИЕ":
                            crmCParty party2 = new crmCParty(ses);
                            party2.Code = ep.code;
                            party2.Name = ep.name;
                            party2.INN  = ep.inn;
                            party2.AddressFact.Country         = GetCountry(ses, ep.country);
                            party2.AddressFact.City            = ep.city;
                            party2.AddressFact.AddressHandmake = ep.addr;
                            if (!String.IsNullOrEmpty(ep.close.Trim()))
                            {
                                party2.IsClosed = true;
                            }
                            //party2.Save();
                            break;

                        case "ДОМ":
                            crmCParty party = new crmCParty(ses);
                            party.Code = ep.code;
                            party.Name = ep.name;
                            party.INN  = ep.inn;
                            party.AddressFact.Country         = GetCountry(ses, ep.country);
                            party.AddressFact.City            = ep.city;
                            party.AddressFact.AddressHandmake = ep.addr;
                            if (!String.IsNullOrEmpty(ep.close.Trim()))
                            {
                                party.IsClosed = true;
                            }
                            //party.Save();
                            break;

                        default:
                            throw new NotSupportedException();
                        }

                        //MessageBox.Show(xlWorkSheet.get_Range("B1", "B1").Value2.ToString());

                        System.Console.WriteLine(ep.type + ' ' + ep.code + ' ' + ep.name + ' ' + ep.addr);
                        ses.CommitChanges();
                    }
                }
                //}
                //catch (Exception e) {
                //        if (xlWorkSheet != null) releaseObject(xlWorkSheet);
                //        if (xlWorkBook != null) releaseObject(xlWorkBook);
                //        if (xlApp != null) releaseObject(xlApp);
                //        xlWorkSheet = null;
                //        xlWorkBook = null;
                //        xlApp = null;
                //        GC.Collect();
                //        System.Console.WriteLine(e);
                //        throw new Exception("Error clear", e);
                //    }

                // Закрытие книги
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();
            }
            if (args[0] == "dogovor")
            {
                Excel.Application xlApp       = null;
                Excel.Workbook    xlWorkBook  = null;
                Excel.Worksheet   xlWorkSheet = null;

                xlApp = new Excel.ApplicationClass();

                // Открытие документа (книги) excel
                xlWorkBook = xlApp.Workbooks.Open("e:\\Договора.xlsx");

                // Выбирается Лист (если существует)
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                //
                try {
                    while (true)
                    {
                        using (UnitOfWork ses = bses.BeginNestedUnitOfWork()) {
                            i++;
                            ExcelDogovor dog = new ExcelDogovor();
                            //                    Excel.Range range = xlWorkSheet.get_Range("A" + i, "P" + i);
                            //                    Excel.Range range2 = xlWorkSheet.get_Range("A1");
                            if (xlWorkSheet.get_Range("A" + i).Value2 == null)
                            {
                                break;
                            }
                            dog.staff_code = xlWorkSheet.get_Range("A" + i).Value2.ToString();
                            dog.dep_code   = xlWorkSheet.get_Range("B" + i).Value2.ToString();
                            dog.type       = xlWorkSheet.get_Range("C" + i).Value2.ToString();

                            dog.dog_id  = xlWorkSheet.get_Range("F" + i).Value2.ToString();
                            dog.dog_num = xlWorkSheet.get_Range("G" + i).Value2.ToString();
                            String str_date = xlWorkSheet.get_Range("H" + i).Value2.ToString();
                            dog.dog_date = DateTime.Parse(str_date.Substring(0, 4) + "." +
                                                          str_date.Substring(4, 2) + "." +
                                                          str_date.Substring(6, 2));
                            if (xlWorkSheet.get_Range("I" + i).Value2 != null)
                            {
                                dog.dop_id   = xlWorkSheet.get_Range("I" + i).Value2.ToString();
                                dog.dop_num  = xlWorkSheet.get_Range("J" + i).Value2.ToString();
                                str_date     = xlWorkSheet.get_Range("K" + i).Value2.ToString();
                                dog.dop_date = DateTime.Parse(str_date.Substring(0, 4) + "." +
                                                              str_date.Substring(4, 2) + "." +
                                                              str_date.Substring(6, 2));
                                //dog.dop_date = (DateTime)xlWorkSheet.get_Range("K" + i).Value2;
                            }

                            dog.customer_code = xlWorkSheet.get_Range("L" + i).Value2.ToString();
                            dog.supplier_code = xlWorkSheet.get_Range("P" + i).Value2.ToString();

                            crmCParty cust = GetParty(ses, dog.customer_code);
                            if (cust == null)
                            {
                                new KeyNotFoundException(dog.customer_code);
                            }
                            crmCParty supl = GetParty(ses, dog.supplier_code);
                            if (supl == null)
                            {
                                new KeyNotFoundException(dog.supplier_code);
                            }

                            dog.dep = GetDepartment(ses, dog.dep_code);
                            if (dog.dep == null)
                            {
                                new KeyNotFoundException(dog.dep_code);
                            }

                            dog.reg_dep = GetDepartment(ses, "00-056");
                            if (dog.reg_dep == null)
                            {
                                new KeyNotFoundException("00-056");
                            }

                            dog.reg_staff = GetStaff(ses, dog.staff_code);
                            if (dog.reg_staff == null)
                            {
                                new KeyNotFoundException(dog.staff_code);
                            }

                            crmContract     contract = GetContract(ses, dog);
                            crmContractDeal deal     = new crmDealWithStage(ses);

                            deal.State = DealStates.DEAL_FORMATION;

                            deal.CuratorDepartment     = dog.dep;
                            deal.UserRegistrator       = dog.reg_staff;
                            deal.DepartmentRegistrator = dog.reg_dep;
                            deal.Customer = cust;
                            deal.Supplier = supl;

                            contract.ContractDeals.Add(deal);
                            if (dog.type == "ДОГ")
                            {
                                deal.ContractKind     = ContractKind.CONTRACT;
                                deal.ContractDocument = contract.ContractDocument;
                            }
                            else
                            {
                                deal.ContractKind = ContractKind.ADDENDUM;
                                crmContractDocument contract_document = new crmContractDocument(ses);
                                contract_document.DocumentCategory = GetDocumentCategory(ses, "ДС");
                                contract_document.Number           = dog.dop_num;
                                contract_document.Date             = dog.dop_date;
                                deal.ContractDocument = contract_document;
                                contract.ContractDocuments.Add(contract_document);
                            }

                            System.Console.WriteLine(dog.type + ' ' + dog.dog_id + ' ' + dog.dog_num + ' ' + dog.dog_date + ' ' + dog.customer_code + ' ' + dog.supplier_code);
                            ses.CommitChanges();
                        }
                    }
                }
                catch (Exception e) {
                    if (xlWorkSheet != null)
                    {
                        releaseObject(xlWorkSheet);
                    }
                    if (xlWorkBook != null)
                    {
                        releaseObject(xlWorkBook);
                    }
                    if (xlApp != null)
                    {
                        releaseObject(xlApp);
                    }
                    xlWorkSheet = null;
                    xlWorkBook  = null;
                    xlApp       = null;
                    GC.Collect();
                    System.Console.WriteLine(e);
                    throw e;
                }

                // Закрытие книги
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();
            }
            if (args[0] == "subject")
            {
                Excel.Application xlApp       = null;
                Excel.Workbook    xlWorkBook  = null;
                Excel.Worksheet   xlWorkSheet = null;

                xlApp = new Excel.ApplicationClass();

                // Открытие документа (книги) excel
                xlWorkBook = xlApp.Workbooks.Open("e:\\subject.xlsx");

                // Выбирается Лист (если существует)
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                //
                try {
                    while (true)
                    {
                        using (UnitOfWork ses = bses.BeginNestedUnitOfWork()) {
                            i++;
                            FinanceTema ft = new FinanceTema();
                            if (xlWorkSheet.get_Range("A" + i).Value2 == null)
                            {
                                break;
                            }
                            ft.dir  = xlWorkSheet.get_Range("A" + i).Value2.ToString();
                            ft.code = xlWorkSheet.get_Range("B" + i).Value2.ToString();
                            ft.name = xlWorkSheet.get_Range("C" + i).Value2.ToString();
                            if (xlWorkSheet.get_Range("D" + i).Value2 != null)
                            {
                                ft.desc = xlWorkSheet.get_Range("D" + i).Value2.ToString();
                            }
                            else
                            {
                                ft.desc = ft.name;
                            }
                            fmCDirection  dir  = GetObject <fmCDirection>(ses, ft.dir);
                            fmCSubjectExt subj = GetObject <fmCSubjectExt>(ses, ft.code);
                            if (subj == null)
                            {
                                subj = new fmCSubjectExt(ses);
                            }
                            subj.Code        = ft.code;
                            subj.Direction   = dir;
                            subj.Name        = ft.name;
                            subj.NameFull    = ft.name;
                            subj.Description = ft.desc;
                            if (subj.Name.Length > 80)
                            {
                                subj.Name = subj.Name.Substring(0, 80);
                            }
                            subj.Save();
                            System.Console.WriteLine(ft.dir + ' ' + ft.code + ' ' + ft.name);
                            ses.CommitChanges();
                        }
                    }
                }
                catch (Exception e) {
                    if (xlWorkSheet != null)
                    {
                        releaseObject(xlWorkSheet);
                    }
                    if (xlWorkBook != null)
                    {
                        releaseObject(xlWorkBook);
                    }
                    if (xlApp != null)
                    {
                        releaseObject(xlApp);
                    }
                    xlWorkSheet = null;
                    xlWorkBook  = null;
                    xlApp       = null;
                    GC.Collect();
                    System.Console.WriteLine(e);
                    throw e;
                }

                // Закрытие книги
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();
            }
            if (args[0] == "order")
            {
                Excel.Application xlApp       = null;
                Excel.Workbook    xlWorkBook  = null;
                Excel.Worksheet   xlWorkSheet = null;

                xlApp = new Excel.ApplicationClass();

                // Открытие документа (книги) excel
                xlWorkBook = xlApp.Workbooks.Open("e:\\order2.xlsx");

                // Выбирается Лист (если существует)
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                //
                try {
                    while (true)
                    {
                        using (UnitOfWork ses = bses.BeginNestedUnitOfWork()) {
                            i++;
                            FinanceOrder fo = new FinanceOrder();
                            if (xlWorkSheet.get_Range("A" + i).Value2 == null)
                            {
                                break;
                            }
                            if (xlWorkSheet.get_Range("A" + i).Value2 is Double)
                            {
                                fo.buh_int_num = (Int32)((Double)xlWorkSheet.get_Range("A" + i).Value2);
                            }
                            if (xlWorkSheet.get_Range("B" + i).Value2 != null)
                            {
                                fo.buh_account = xlWorkSheet.get_Range("B" + i).Value2.ToString();
                            }
                            else
                            {
                                fo.buh_account = "";
                            }
                            fo.code = xlWorkSheet.get_Range("C" + i).Value2.ToString();
                            if (xlWorkSheet.get_Range("D" + i).Value2 != null)
                            {
                                fo.is_closed = true;
                            }
                            else
                            {
                                fo.is_closed = false;
                            }
                            if (xlWorkSheet.get_Range("E" + i).Value2 != null)
                            {
                                fo.subj = xlWorkSheet.get_Range("E" + i).Value2.ToString();
                            }
                            else
                            {
                                fo.subj = "";
                            }
                            if (xlWorkSheet.get_Range("F" + i).Value2 != null)
                            {
                                fo.name_short = xlWorkSheet.get_Range("F" + i).Value2.ToString();
                            }
                            else
                            {
                                fo.name_short = "";
                            }
                            if (xlWorkSheet.get_Range("G" + i).Value2 != null)
                            {
                                fo.name_full = xlWorkSheet.get_Range("G" + i).Value2.ToString();
                            }
                            else
                            {
                                fo.name_full = "";
                            }
                            if (xlWorkSheet.get_Range("H" + i).Value2 != null)
                            {
                                fo.desc = xlWorkSheet.get_Range("H" + i).Value2.ToString();
                            }
                            else
                            {
                                fo.desc = "";
                            }
                            if (xlWorkSheet.get_Range("I" + i).Value2 != null)
                            {
                                fo.data_from = xlWorkSheet.get_Range("I" + i).Value2.ToString();
                            }
                            else
                            {
                                fo.data_from = "";
                            }
                            if (xlWorkSheet.get_Range("J" + i).Value2 != null)
                            {
                                fo.data_to = xlWorkSheet.get_Range("J" + i).Value2.ToString();
                            }
                            else
                            {
                                fo.data_to = "";
                            }
                            if (xlWorkSheet.get_Range("K" + i).Value2 != null)
                            {
                                fo.base_doc = xlWorkSheet.get_Range("K" + i).Value2.ToString();
                            }
                            else
                            {
                                fo.base_doc = "";
                            }
                            if (xlWorkSheet.get_Range("L" + i).Value2 != null)
                            {
                                fo.work_type = xlWorkSheet.get_Range("L" + i).Value2.ToString();
                            }
                            else
                            {
                                fo.work_type = "";
                            }
                            if (xlWorkSheet.get_Range("M" + i).Value2 != null)
                            {
                                fo.finans = xlWorkSheet.get_Range("M" + i).Value2.ToString();
                            }
                            else
                            {
                                fo.finans = "";
                            }
                            if (xlWorkSheet.get_Range("N" + i).Value2 != null)
                            {
                                fo.source = xlWorkSheet.get_Range("N" + i).Value2.ToString();
                            }
                            else
                            {
                                fo.source = "";
                            }
                            if (xlWorkSheet.get_Range("O" + i).Value2 != null)
                            {
                                fo.army = xlWorkSheet.get_Range("O" + i).Value2.ToString();
                            }
                            else
                            {
                                fo.army = "";
                            }
                            if (xlWorkSheet.get_Range("P" + i).Value2 != null)
                            {
                                fo.nds_mode = xlWorkSheet.get_Range("P" + i).Value2.ToString();
                            }
                            else
                            {
                                fo.nds_mode = "";
                            }
                            if (xlWorkSheet.get_Range("Q" + i).Value2 != null)
                            {
                                fo.acc_mode = xlWorkSheet.get_Range("Q" + i).Value2.ToString();
                            }
                            else
                            {
                                fo.acc_mode = "";
                            }
                            if (xlWorkSheet.get_Range("R" + i).Value2 != null)
                            {
                                if (xlWorkSheet.get_Range("R" + i).Value2 is Double)
                                {
                                    fo.koeff_ozm = (Decimal)((Double)xlWorkSheet.get_Range("R" + i).Value2);
                                }
                            }
                            if (xlWorkSheet.get_Range("S" + i).Value2 != null)
                            {
                                if (xlWorkSheet.get_Range("S" + i).Value2 is Double)
                                {
                                    fo.koeff_kb = (Decimal)((Double)xlWorkSheet.get_Range("S" + i).Value2);
                                }
                            }
                            fmCSubjectExt subj = GetObject <fmCSubjectExt>(ses, fo.subj);
                            fmCOrderExt   ord  = GetObject <fmCOrderExt>(ses, fo.code);
                            if (ord == null)
                            {
                                ord        = new fmCOrderExt(ses);
                                ord.Status = fmIOrderStatus.Loaded;
                            }
                            ord.Code        = fo.code;
                            ord.Subject     = subj;
                            ord.IsClosed    = fo.is_closed;
                            ord.Name        = fo.name_short;
                            ord.NameFull    = fo.name_full;
                            ord.Description = fo.desc;
                            if (!String.IsNullOrEmpty(fo.data_from))
                            {
                                ord.DateBegin = new DateTime(Int32.Parse(fo.data_from.Substring(0, 4)),
                                                             Int32.Parse(fo.data_from.Substring(4, 2)),
                                                             Int32.Parse(fo.data_from.Substring(6, 2)));
                            }
                            if (!String.IsNullOrEmpty(fo.data_to))
                            {
                                ord.DateEnd = new DateTime(Int32.Parse(fo.data_to.Substring(0, 4)),
                                                           Int32.Parse(fo.data_to.Substring(4, 2)),
                                                           Int32.Parse(fo.data_to.Substring(6, 2)));
                            }
                            if (!String.IsNullOrEmpty(fo.acc_mode))
                            {
                                ord.AnalitycAccouterType = GetObject <fmСOrderAnalitycAccouterType>(ses, fo.acc_mode);
                            }
                            if (!String.IsNullOrEmpty(fo.nds_mode))
                            {
                                ord.AnalitycAVT = GetObject <fmСOrderAnalitycAVT>(ses, fo.nds_mode);
                            }
                            ord.BuhAccount  = fo.buh_account;
                            ord.BuhIntNum   = fo.buh_int_num;
                            ord.KoeffKB     = fo.koeff_kb;
                            ord.KoeffOZM    = fo.koeff_ozm;
                            ord.SourceOther = fo.base_doc;
                            //                                ord.
                            if (!String.IsNullOrEmpty(fo.work_type))
                            {
                                ord.AnalitycWorkType = GetObject <fmСOrderAnalitycWorkType>(ses, fo.work_type);
                            }
                            if (!String.IsNullOrEmpty(fo.acc_mode))
                            {
                                ord.AnalitycFinanceSource = GetObject <fmСOrderAnalitycFinanceSource>(ses, fo.finans);
                            }
                            if (!String.IsNullOrEmpty(fo.acc_mode))
                            {
                                ord.AnalitycOrderSource = GetObject <fmСOrderAnalitycOrderSource>(ses, fo.source);
                            }
                            if (!String.IsNullOrEmpty(fo.acc_mode))
                            {
                                ord.AnalitycMilitary = GetObject <fmСOrderAnalitycMilitary>(ses, fo.army);
                            }
                            System.Console.WriteLine(fo.subj + ' ' + fo.code + ' ' + fo.name_short);
                            ses.CommitChanges();
                        }
                    }
                }
                catch (Exception e) {
                    if (xlWorkSheet != null)
                    {
                        releaseObject(xlWorkSheet);
                    }
                    if (xlWorkBook != null)
                    {
                        releaseObject(xlWorkBook);
                    }
                    if (xlApp != null)
                    {
                        releaseObject(xlApp);
                    }
                    xlWorkSheet = null;
                    xlWorkBook  = null;
                    xlApp       = null;
                    GC.Collect();
                    System.Console.WriteLine(e);
                    throw e;
                }
                // Закрытие книги
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();
            }
        }