static public fmCAVTBookVAT LocateBook(IObjectSpace os, crmCParty organ, fmCAVTBookVAT.fmCAVTBookVATType type, String period)
        {
            IList <fmCAVTBookVAT> books = os.GetObjects <fmCAVTBookVAT>(
                XPQuery <fmCAVTBookVAT> .TransformExpression(((ObjectSpace)os).Session,
                                                             b => b.Party == organ &&
                                                             b.Period == period), true).ToList();
            fmCAVTBookVAT book_main = null;

            foreach (fmCAVTBookVAT book in books)
            {
                if ((type == fmCAVTBookVAT.fmCAVTBookVATType.PAY_MAIN ||
                     type == fmCAVTBookVAT.fmCAVTBookVATType.PAY_ADD) &&
                    book.BookVATType == fmCAVTBookVAT.fmCAVTBookVATType.PAY_MAIN)
                {
                    book_main = book;
                    break;
                }
                if ((type == fmCAVTBookVAT.fmCAVTBookVATType.BAY_MAIN ||
                     type == fmCAVTBookVAT.fmCAVTBookVATType.BAY_ADD) &&
                    book.BookVATType == fmCAVTBookVAT.fmCAVTBookVATType.BAY_MAIN)
                {
                    book_main = book;
                    break;
                }
            }
            if (book_main == null)
            {
                if (type == fmCAVTBookVAT.fmCAVTBookVATType.PAY_MAIN ||
                    type == fmCAVTBookVAT.fmCAVTBookVATType.PAY_ADD)
                {
                }
            }
            return(book_main);
        }
        public override void UpdateDatabaseAfterUpdateSchema()
        {
            base.UpdateDatabaseAfterUpdateSchema();
            // Disable version
            if (this.CurrentDBVersion != new Version("0.0.0.0"))
            {
                return;
            }
            //
            using (IObjectSpace os = ObjectSpace.CreateNestedObjectSpace()) {
                csValuta val_rub = os.FindObject <csValuta>(
                    XPQuery <csValuta> .TransformExpression(((ObjectSpace)os).Session,
                                                            val => val.Code == "RUB"));
//                fmCAVTInvoiceVersion ver;
                IList <fmCAVTInvoiceVersion> ver_list = os.GetObjects <fmCAVTInvoiceVersion>(
                    XPQuery <fmCAVTInvoiceVersion> .TransformExpression(((ObjectSpace)os).Session,
                                                                        inv => inv.Valuta == null));
                foreach (fmCAVTInvoiceVersion ver in ver_list)
                {
                    ver.Valuta = val_rub;
                }
                os.CommitChanges();
            }
            using (IObjectSpace os = ObjectSpace.CreateNestedObjectSpace()) {
                IList <fmCAVTInvoiceBase> inv_list = os.GetObjects <fmCAVTInvoiceBase>(new UnaryOperator(UnaryOperatorType.IsNull, "InvoiceIntType"));
                foreach (fmCAVTInvoiceBase inv in inv_list)
                {
                    inv.InvoiceIntType = fmCAVTInvoiceIntType.NORMAL;
                }
                os.CommitChanges();
            }
            ObjectSpace.CommitChanges();
        }
Example #3
0
        /// <summary>
        /// Преобразование выражения Linq для XPO в выражение поискового критерия XPO
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="os"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        static public CriteriaOperator LinqToXPOCriteria <T>(IObjectSpace os, System.Linq.Expressions.Expression <Func <T, bool> > expression)
        {
            // Ограничения на тип T пока не накладывал
            CriteriaOperator criteria = XPQuery <T> .TransformExpression(((ObjectSpace)os).Session, expression);

            return(criteria);
        }
Example #4
0
 protected void ОбновитьСчетФактуру()
 {
     if (ТипИсточника == Основание.ТипИсточника.ВХОДЯЩИЙ || String.IsNullOrEmpty(Номер) || Дата < new DateTime(2000, 1, 1) ||
         СчетФактура != null && СчетФактура.Number == Номер &&
         СчетФактура.Date >= Дата && СчетФактура.Date < Дата.AddDays(1))
     {
         return;
     }
     СчетФактураУст(Session.FindObject <fmCAVTInvoiceBase>(
                        XPQuery <fmCAVTInvoiceBase> .TransformExpression(Session,
                                                                         rec =>
                                                                         rec.Supplier.INN == ИННПродавца &&
                                                                         rec.Number == Номер &&
                                                                         rec.Date >= Дата &&
                                                                         rec.Date < Дата.AddDays(1)
                                                                         ))
                    );
 }
        static public fmCAVTBookVAT LocateBookMain(IObjectSpace os, crmCParty organ, fmCAVTBookVAT.fmCAVTBookVATType type, String period)
        {
            if (type != fmCAVTBookVAT.fmCAVTBookVATType.PAY_MAIN &&
                type != fmCAVTBookVAT.fmCAVTBookVATType.BAY_MAIN)
            {
                throw new ArgumentException();
            }
            fmCAVTBookVAT book = os.FindObject <fmCAVTBookVAT>(
                XPQuery <fmCAVTBookVAT> .TransformExpression(((ObjectSpace)os).Session,
                                                             b => b.Party == organ &&
                                                             b.BookVATType == type &&
                                                             b.Period == period), true);

            if (book == null)
            {
                book             = os.CreateObject <fmCAVTBookVAT>();
                book.Party       = organ;
                book.BookVATType = type;
                book.Period      = period;
            }
            return(book);
        }
        static public void ImportBookBayMain2014(IObjectSpace os, fmCAVTBookVAT book)
        {
            Int32  kvartal   = Int32.Parse(book.PeriodKV);
            String period_m1 = book.PeriodYYYY + ((kvartal - 1) * 3 + 1).ToString("00");
            String period_m2 = book.PeriodYYYY + ((kvartal - 1) * 3 + 2).ToString("00");
            String period_m3 = book.PeriodYYYY + ((kvartal - 1) * 3 + 3).ToString("00");
            IList <fmCAVTInvoiceOperationType> oper_types = os.GetObjects <fmCAVTInvoiceOperationType>();

            os.Delete(book.BookVATRecords);
            UInt32 seq_num = 1;
            IList <fmCAVTBookBuhRecord> buhrecs = os.GetObjects <fmCAVTBookBuhRecord>(
                XPQuery <fmCAVTBookBuhRecord> .TransformExpression(((ObjectSpace)os).Session,
                                                                   rec => rec.BookBuhImport.IsNotUse == false && rec.BookType == "B" && (
                                                                       rec.PeriodOtchet == period_m1 ||
                                                                       rec.PeriodOtchet == period_m2 ||
                                                                       rec.PeriodOtchet == period_m3
                                                                       //&& (rec.RecordType != "AON" ||
                                                                       // rec.RecordType == "AON" &&
                                                                       // rec.RecordSummType == "NDS" &&
                                                                       // rec.SummVAT != 0)
                                                                       )
                                                                   ));
            var invoice_buhrecs = buhrecs.GroupBy(rec => new {
//                rec.RecordType,
                rec.AVTInvoicePartyCode,
                rec.AVTInvoiceType,
                rec.AVTInvoiceRegNumber,
                rec.AVTInvoiceNumber,
                rec.AVTInvoiceDate
            }).OrderBy(key => key.Key.AVTInvoicePartyCode);

            //
            foreach (var invoice_buhrec in invoice_buhrecs)
            {
                if (String.IsNullOrEmpty(invoice_buhrec.Key.AVTInvoiceType.Trim()))
                {
                    continue;
                }
                if (invoice_buhrec.Key.AVTInvoiceType.Trim() == "рд")
                {
                    continue;
                }
                fmCAVTBookVATRecord rec   = os.CreateObject <fmCAVTBookVATRecord>();
                crmCParty           party = os.GetObjects <crmCParty>(new BinaryOperator("Code", invoice_buhrec.Key.AVTInvoicePartyCode)).FirstOrDefault();
                rec.BuhRecordType =
                    invoice_buhrec.Select(x => x.RecordType).Distinct().OrderBy(x => x).Aggregate((x, y) => x + ";" + y);
                rec.Party               = party;
                rec.VATInvoiceType      = invoice_buhrec.Key.AVTInvoiceType;
                rec.VATInvoiceRegNumber = invoice_buhrec.Key.AVTInvoiceRegNumber;
                rec.VATInvoiceNumber    = invoice_buhrec.Key.AVTInvoiceNumber;
                rec.VATInvoiceDate      = invoice_buhrec.Key.AVTInvoiceDate;
                rec.Invoice             = os.FindObject <fmCAVTInvoiceBase>(
                    CriteriaOperator.And(new BinaryOperator("Number", invoice_buhrec.Key.AVTInvoiceNumber),
                                         new BinaryOperator("Date", invoice_buhrec.Key.AVTInvoiceDate.Date, BinaryOperatorType.GreaterOrEqual),
                                         new BinaryOperator("Date", invoice_buhrec.Key.AVTInvoiceDate.Date.AddDays(1), BinaryOperatorType.Less),
                                         new BinaryOperator("Supplier", party)));
                if (invoice_buhrec.Key.AVTInvoiceType == "ятю" && rec.Invoice == null)
                {
                    rec.Invoice = os.FindObject <fmCAVTInvoiceBase>(
                        CriteriaOperator.And(new BinaryOperator("Number", invoice_buhrec.Key.AVTInvoiceNumber),
                                             new BinaryOperator("Date", invoice_buhrec.Key.AVTInvoiceDate.Date, BinaryOperatorType.GreaterOrEqual),
                                             new BinaryOperator("Date", invoice_buhrec.Key.AVTInvoiceDate.Date.AddDays(1), BinaryOperatorType.Less),
                                             new BinaryOperator("Customer", party)));
                }
                rec.PayDate = invoice_buhrec.Select(buhrec => buhrec.PayDocDate).Max();
                rec.BuhDate = invoice_buhrec.Select(buhrec => buhrec.BuhDocDate).Min();
                //Decimal SummNoVAT_18;
                //Decimal SummNoVAT_10;
                //rec.SummVAT_18 = invoice_buhrec.Where(buhrec => buhrec.NDSRate == "2").Sum(buhrec => buhrec.SummVAT);
                //SummNoVAT_18 = invoice_buhrec.Where(buhrec => buhrec.NDSRate == "2").Sum(buhrec => buhrec.SummVATCost);
                //rec.SummVAT_10 = invoice_buhrec.Where(buhrec => buhrec.NDSRate == "3").Sum(buhrec => buhrec.SummVAT);
                //SummNoVAT_10 = invoice_buhrec.Where(buhrec => buhrec.NDSRate == "3").Sum(buhrec => buhrec.SummVATCost);
                //rec.SummCost_NoVAT = invoice_buhrec.Where(buhrec => buhrec.NDSRate == "5").Sum(buhrec => buhrec.SummAll);
                //rec.SummCost_18 = Decimal.Round(rec.SummVAT_18 * 100m / 18m, 2);
                //rec.SummCost_10 = Decimal.Round(rec.SummVAT_10 * 100m / 10m, 2);
                //rec.SummCost_NoVAT = rec.SummCost_NoVAT +
                //    Decimal.Round(SummNoVAT_18 * 118m / 18m, 2) +
                //    Decimal.Round(SummNoVAT_10 * 110m / 10m, 2);
                rec.SummBayCost            = invoice_buhrec.Sum(buhrec => buhrec.SummCost);
                rec.SummBayVatCharge       = invoice_buhrec.Sum(buhrec => buhrec.SummVATIn);
                rec.SummBayVatDeduction    = invoice_buhrec.Sum(buhrec => buhrec.SummVAT);
                rec.SummBayVatInCost       = invoice_buhrec.Sum(buhrec => buhrec.SummVATCost);
                rec.SummBayVatExp          = invoice_buhrec.Sum(buhrec => buhrec.SummVATExp);
                rec.SummBayVatOtherCredit  = invoice_buhrec.Sum(buhrec => buhrec.SummVATCrdOther);
                rec.SummBayVatOtherCredit += invoice_buhrec.Sum(buhrec => buhrec.SummVATNoInvoice);

                if (rec.SummBayVatDeduction == 0)
                {
                    os.Delete(rec);
                    continue;
                }
                if (rec.SummBayCost == 0)
                {
                    rec.SummBayCost = Decimal.Round((rec.SummBayVatDeduction + rec.SummBayVatInCost) * 100 / 18, 2);
                }
                rec.SequenceNumber = seq_num++;
                switch (rec.BuhRecordType)
                {
                case "PAY":
                    rec.OperationType = oper_types.FirstOrDefault(x => x.Code == "01");
                    break;

                case "BAY":
                    rec.OperationType = oper_types.FirstOrDefault(x => x.Code == "01");
                    break;

                case "AIN":
                    rec.OperationType = oper_types.FirstOrDefault(x => x.Code == "02");
                    break;

                case "AON":
                    rec.OperationType = oper_types.FirstOrDefault(x => x.Code == "02");
                    break;

                case "EAT":
                    rec.OperationType = oper_types.FirstOrDefault(x => x.Code == "01");
                    break;

                case "EXP":
                    rec.OperationType = oper_types.FirstOrDefault(x => x.Code == "01");
                    break;

                case "SMN":
                    rec.OperationType = oper_types.FirstOrDefault(x => x.Code == "01");
                    break;

                case "SPC":
                    rec.OperationType = oper_types.FirstOrDefault(x => x.Code == "01");
                    break;

                default:
                    rec.OperationType = oper_types.FirstOrDefault(x => x.Code == "01");
                    break;
                }
                book.BookVATRecords.Add(rec);
            }
            IList <fmCAVTBookBuhStruct> book_structs = os.GetObjects <fmCAVTBookBuhStruct>();

            foreach (var book_struct in book_structs)
            {
                foreach (var record in book_struct.InInvoiceRecords)
                {
                    if (book.DatePeriodStart.Date <= record.BayDate.Date && record.BayDate.Date <= book.DatePeriodStop.Date)
                    {
                        if (record.BaySummAll == 0 || record.BaySummVAT == 0 && record.BayVATRate.Code != "0%")
                        {
                            continue;
                        }
                        fmCAVTBookVATRecord book_rec = os.CreateObject <fmCAVTBookVATRecord>();
                        //                        rec.BuhRecordType = invoice_buhrec.Key.RecordType;
                        book_rec.BookBuhStruct = book_struct;
                        book_rec.OperationType = record.OperationType;
                        if (record.InvoiceType == "ятю")
                        {
                            book_rec.BuhRecordType = "AIN";
                        }
                        else
                        {
                            book_rec.BuhRecordType = "BAY";
                        }
                        book_rec.Party = record.PartnerParty;
                        book_rec.VATInvoiceRegNumber = record.InvoiceRegNumber;
                        book_rec.VATInvoiceType      = record.InvoiceType;
                        book_rec.VATInvoiceNumber    = record.InvoiceNumber;
                        book_rec.VATInvoiceDate      = record.InvoiceDate;
                        book_rec.Invoice             = record.Invoice;
                        if (record.Invoice != null)
                        {
                            book_rec.InvoiceVersion = record.Invoice.Current;
                        }
                        book_rec.PayDate = record.SaleDate;
                        book_rec.BuhDate = record.SaleDate;

                        book_rec.SummBayVatCharge    = record.BaySummVAT;
                        book_rec.SummBayVatDeduction = Decimal.Round(book_rec.SummBayVatCharge * book_struct.BayNorma, 2);
                        book_rec.SummBayVatInCost    = book_rec.SummBayVatCharge - book_rec.SummBayVatDeduction;

                        book_rec.SummBayCost = record.BaySummAll - record.BaySummVAT;
                        //                        if (record.InvoiceType == "ятг") book_rec.PayDate = book_rec.BuhDate;
                        //                        if (record.Nds)
                        //                        book_rec.SummVAT_18 = invoice_buhrec.Where(buhrec => buhrec.NDSRate == "2").Sum(buhrec => buhrec.SummVAT);
                        book_rec.SequenceNumber = seq_num++;
                        book.BookVATRecords.Add(book_rec);
                    }
                }
            }
            ReNumber(os, book);
        }
        static public void ImportBookBayMain(IObjectSpace os, fmCAVTBookVAT book)
        {
            Int32  kvartal   = Int32.Parse(book.PeriodKV);
            String period_m1 = book.PeriodYYYY + ((kvartal - 1) * 3 + 1).ToString("00");
            String period_m2 = book.PeriodYYYY + ((kvartal - 1) * 3 + 2).ToString("00");
            String period_m3 = book.PeriodYYYY + ((kvartal - 1) * 3 + 3).ToString("00");

            os.Delete(book.BookVATRecords);
            IList <fmCAVTBookBuhRecord> buhrecs = os.GetObjects <fmCAVTBookBuhRecord>(
                XPQuery <fmCAVTBookBuhRecord> .TransformExpression(((ObjectSpace)os).Session,
                                                                   rec => rec.BookType == "B" && (
                                                                       rec.PeriodOtchet == period_m1 ||
                                                                       rec.PeriodOtchet == period_m2 ||
                                                                       rec.PeriodOtchet == period_m3
                                                                       //&& (rec.RecordType != "AON" ||
                                                                       // rec.RecordType == "AON" &&
                                                                       // rec.RecordSummType == "NDS" &&
                                                                       // rec.SummVAT != 0)
                                                                       )
                                                                   ));
            var invoice_buhrecs = buhrecs.GroupBy(rec => new {
                rec.RecordType,
                rec.AVTInvoicePartyCode,
                rec.AVTInvoiceType,
                rec.AVTInvoiceRegNumber,
                rec.AVTInvoiceNumber,
                rec.AVTInvoiceDate
            }).OrderBy(key => key.Key.AVTInvoicePartyCode);
            //
            UInt32 seq_num = 1;

            foreach (var invoice_buhrec in invoice_buhrecs)
            {
                if (String.IsNullOrEmpty(invoice_buhrec.Key.AVTInvoiceType.Trim()))
                {
                    continue;
                }
                if (invoice_buhrec.Key.AVTInvoiceType.Trim() == "рд")
                {
                    continue;
                }
                fmCAVTBookVATRecord rec   = os.CreateObject <fmCAVTBookVATRecord>();
                crmCParty           party = os.GetObjects <crmCParty>(new BinaryOperator("Code", invoice_buhrec.Key.AVTInvoicePartyCode)).FirstOrDefault();
                rec.BuhRecordType       = invoice_buhrec.Key.RecordType;
                rec.Party               = party;
                rec.VATInvoiceType      = invoice_buhrec.Key.AVTInvoiceType;
                rec.VATInvoiceRegNumber = invoice_buhrec.Key.AVTInvoiceRegNumber;
                rec.VATInvoiceNumber    = invoice_buhrec.Key.AVTInvoiceNumber;
                rec.VATInvoiceDate      = invoice_buhrec.Key.AVTInvoiceDate;
                rec.Invoice             = os.FindObject <fmCAVTInvoiceBase>(
                    CriteriaOperator.And(new BinaryOperator("Number", invoice_buhrec.Key.AVTInvoiceNumber),
                                         new BinaryOperator("Date", invoice_buhrec.Key.AVTInvoiceDate.Date, BinaryOperatorType.GreaterOrEqual),
                                         new BinaryOperator("Date", invoice_buhrec.Key.AVTInvoiceDate.Date.AddDays(1), BinaryOperatorType.Less),
                                         new BinaryOperator("Supplier", party)));
                rec.PayDate = invoice_buhrec.Select(buhrec => buhrec.PayDocDate).Max();
                rec.BuhDate = invoice_buhrec.Select(buhrec => buhrec.BuhDocDate).Min();
                Decimal SummNoVAT_18;
                Decimal SummNoVAT_10;
                rec.SummVAT_18     = invoice_buhrec.Where(buhrec => buhrec.NDSRate == "2").Sum(buhrec => buhrec.SummVAT);
                SummNoVAT_18       = invoice_buhrec.Where(buhrec => buhrec.NDSRate == "2").Sum(buhrec => buhrec.SummVATCost);
                rec.SummVAT_10     = invoice_buhrec.Where(buhrec => buhrec.NDSRate == "3").Sum(buhrec => buhrec.SummVAT);
                SummNoVAT_10       = invoice_buhrec.Where(buhrec => buhrec.NDSRate == "3").Sum(buhrec => buhrec.SummVATCost);
                rec.SummCost_NoVAT = invoice_buhrec.Where(buhrec => buhrec.NDSRate == "5").Sum(buhrec => buhrec.SummAll);
                rec.SummCost_18    = Decimal.Round(rec.SummVAT_18 * 100m / 18m, 2);
                rec.SummCost_10    = Decimal.Round(rec.SummVAT_10 * 100m / 10m, 2);
                rec.SummCost_NoVAT = rec.SummCost_NoVAT +
                                     Decimal.Round(SummNoVAT_18 * 118m / 18m, 2) +
                                     Decimal.Round(SummNoVAT_10 * 110m / 10m, 2);
                if (rec.SummAll == 0 && rec.SummVAT_18 == 0 && rec.SummVAT_10 == 0)
                {
                    os.Delete(rec);
                    continue;
                }
                rec.SequenceNumber = seq_num++;
                book.BookVATRecords.Add(rec);

                switch (invoice_buhrec.Key.RecordType)
                {
                case "PAY":
                    break;

                case "AIN":
                    break;

                case "AON":
                    break;

                case "EAT":
                    break;

                case "EXP":
                    break;

                case "SMN":
                    break;

                case "SPC":
                    break;

                default:
                    break;
                }
            }
        }
Example #8
0
        public static ModificationResult DeleteWhere <T>(this Session session, Expression <Func <T, bool> > exp) where T : IXPObject
        {
            CriteriaOperator filter = XPQuery <T> .TransformExpression(session, exp);

            return(DeleteWhere <T>(session, filter));
        }
Example #9
0
        static public void ImportInvoices(IObjectSpace os, String file_name)
        {
            //            OpenFileDialog dialog = new OpenFileDialog();
            //            if (dialog.ShowDialog() == DialogResult.OK) {
            FixedFileEngine engine = new FixedFileEngine(typeof(InvoiceImport));

            InvoiceImport[]                    imp_res            = (InvoiceImport[])engine.ReadFile(file_name);
            IList <fmCAVTInvoiceType>          inv_types          = os.GetObjects <fmCAVTInvoiceType>();
            IList <fmCAVTInvoiceTransferType>  inv_transfer_types = os.GetObjects <fmCAVTInvoiceTransferType>();
            IList <fmCAVTInvoiceOperationType> inv_oper_types     = os.GetObjects <fmCAVTInvoiceOperationType>();
            Int32             count       = 0;
            fmCAVTInvoiceType sf_sfz_type = os.GetObjects <fmCAVTInvoiceType>().First(x => x.Prefix == "Z");

            foreach (InvoiceImport imp_rec in imp_res)
            {
                imp_rec.SF_VO_CODE    = imp_rec.SF_VO_CODE.Trim();
                imp_rec.SF_INT_NUMBER = imp_rec.SF_INT_NUMBER.Trim();
                imp_rec.SF_NUMBER     = imp_rec.SF_NUMBER.Trim();
                Decimal  summ_cost     = Decimal.Parse(imp_rec.SUMM_COST.Trim().Replace('.', ','));
                Decimal  summ_nds      = Decimal.Parse(imp_rec.SUMM_NDS.Trim().Replace('.', ','));
                Decimal  summ_sub_cost = Decimal.Parse(imp_rec.SUMM_SUB_COST.Trim().Replace('.', ','));
                Decimal  summ_sub_nds  = Decimal.Parse(imp_rec.SUMM_SUB_NDS.Trim().Replace('.', ','));
                DateTime sf_date       = default(DateTime);
                DateTime.TryParseExact(imp_rec.SF_DATE.Trim(), "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out sf_date);
                crmCParty party = os.GetObjects <crmCParty>(new BinaryOperator("Code", imp_rec.SF_VO_CODE)).FirstOrDefault();
                if (party == null)
                {
                    System.Console.WriteLine("SF " + imp_rec.SF_NUMBER + " party not found (" + imp_rec.SF_VO_CODE + ")");
                    continue;
                }
                Основание.ТипИсточника ts;
                if (imp_rec.SF_IO_TYPE == "I")
                {
                    ts = Основание.ТипИсточника.ВХОДЯЩИЙ;
                }
                else if (imp_rec.SF_IO_TYPE == "O")
                {
                    ts = Основание.ТипИсточника.ИСХОДЯЩИЙ;
                }
                else
                {
                    throw new ArgumentOutOfRangeException("SF " + imp_rec.SF_NUMBER + " неопределен тип входящий/исходящий");
                }
                Основание.ТипОснования tsf;
                switch (imp_rec.SF_TYPE)
                {
                case "СЧФ":
                    tsf = Основание.ТипОснования.СЧФ;
                    break;

                case "УПД":
                    tsf = Основание.ТипОснования.УПД;
                    break;

                case "СФА":
                    tsf = Основание.ТипОснования.СФА;
                    break;

                case "СФЗ":
                    tsf = Основание.ТипОснования.СФЗ;
                    break;

                case "СЧГ":
                    tsf = Основание.ТипОснования.СЧГ;
                    break;

                case "БЖД":
                    tsf = Основание.ТипОснования.БЖД;
                    break;

                case "СФВ":
                    tsf = Основание.ТипОснования.СФВ;
                    break;

                case "БСО":
                    tsf = Основание.ТипОснования.БСО;
                    break;

                case "ЧЕК":
                    tsf = Основание.ТипОснования.ЧЕК;
                    break;

                default:
                    System.Console.WriteLine("SF: " + imp_rec.SF_NUMBER + " странный тип (" + imp_rec.SF_TYPE + ")");
                    continue;
                }
                String  inn        = "";
                String  kpp        = "";
                ЛицоТип party_type = ЛицоТип.НЕЗАДАН;
                if (party.Person != null)
                {
                    if (party.Person.Address.Country.CodeAlfa2 == "RU")
                    {
//                        Type party.ComponentTypeComponentObject.GetType();
                        if (party.ComponentType == typeof(crmCLegalPerson) ||
                            party.ComponentType == typeof(crmCLegalPersonUnit))
                        {
                            party_type = ЛицоТип.ЮР_ЛИЦО;
                            inn        = party.INN;
                            if (inn.Length == 9)
                            {
                                inn = "0" + inn;
                            }
                            kpp = party.KPP;
                            if (inn.Length == 8)
                            {
                                kpp = "0" + kpp;
                            }
                            if (inn.Length != 10)
                            {
                                System.Console.WriteLine("Party: " + party.Code + " fail INN (" + inn + ")");
                                continue;
                            }
                            if (kpp.Length != 9)
                            {
                                System.Console.WriteLine("Party: " + party.Code + " fail KPP (" + kpp + ")");
                                continue;
                            }
                        }
                        else
                        {
                            if (party.ComponentType == typeof(crmCBusinessman))
                            {
                                party_type = ЛицоТип.ПРЕДПРИНИМАТЕЛЬ;
                                inn        = party.INN;
                                if (inn.Length == 11)
                                {
                                    inn = "0" + inn;
                                }
                                if (inn.Length != 12)
                                {
                                    System.Console.WriteLine("Party: " + party.Code + " fail INN (" + inn + ")");
                                    continue;
                                }
                            }
                            else
                            if (party.ComponentType == typeof(crmCPhysicalParty))
                            {
                                party_type = ЛицоТип.ФИЗ_ЛИЦО;
                            }
                        }
                    }
                    else
                    {
                        party_type = ЛицоТип.ИНО_ПАРТНЕР;
                        System.Console.WriteLine("Party: " + party.Code + " инопартнер ");
                    }
                }
                if (party.Code == "2706")
                {
                    party_type = ЛицоТип.ОЗНИЦА;
                }
                //
                String sale_inn = "5012039795";
                if (ts == Основание.ТипИсточника.ВХОДЯЩИЙ)
                {
                    sale_inn = inn;
                }
                // !!!!!!!!!!!!!!!!!!!!!!!!!!
                // Выбраковываем СФ
                if (String.IsNullOrEmpty(sale_inn) || String.IsNullOrEmpty(imp_rec.SF_REGNUM.Trim()) && tsf != Основание.ТипОснования.СФЗ)
                {
                    continue;
                }
                // !!!!!!!!!!!!!!!!!!!!!!!!!!
                Основание sf = os.FindObject <Основание>(
                    XPQuery <Основание> .TransformExpression(
                        ((ObjectSpace)os).Session,
                        rec =>
                        rec.ИннПродавца == sale_inn &&
                        rec.Номер == imp_rec.SF_NUMBER &&
                        rec.Дата >= sf_date &&
                        rec.Дата < sf_date.AddDays(1)
                        ));
                if (sf == null)
                {
                    sf          = os.CreateObject <Основание>();
                    sf.Источник = ts;
                    sf.ИНН      = inn;
                    sf.Номер    = imp_rec.SF_NUMBER;
                    sf.Дата     = sf_date;
                    sf.КПП      = kpp;
                }
                sf.Корректировка = Основание.ТипПодчиненности.ОСНОВНОЙ;
                sf.Источник      = ts;
                sf.Тип           = tsf;
                sf.ЛицоТип       = party_type;
                ОснованиеДокумент sfdoc      = null;
                String            sfdoc_sver = imp_rec.SF_PRAV_NUMBER.Trim();
                if (String.IsNullOrEmpty(sfdoc_sver))
                {
                    sfdoc_sver = "0";
                }
                UInt16 sfdoc_ver = 0;
                UInt16.TryParse(sfdoc_sver, out sfdoc_ver);
                DateTime sfdoc_date = default(DateTime);
                DateTime.TryParseExact(imp_rec.SF_DATE.Trim(), "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out sfdoc_date);
                foreach (ОснованиеДокумент doc in sf.Документы)
                {
                    if (doc.НомерИсправления == sfdoc_ver)
                    {
                        sfdoc = doc;
                        break;
                    }
                }
                if (sfdoc == null)
                {
                    sfdoc = os.CreateObject <ОснованиеДокумент>();
                    sf.Документы.Add(sfdoc);
                    sfdoc.НомерИсправления = sfdoc_ver;
                    if (sf.ДействующийДокумент.НомерИсправления < sfdoc.НомерИсправления)
                    {
                        sf.ДействующийДокумент = sfdoc;
                    }
                }
                sfdoc.ДатаИсправления = sfdoc_date;
                sfdoc.егНомер         = imp_rec.SF_REGNUM.Trim();
                if (sf.Тип == Основание.ТипОснования.СФЗ && String.IsNullOrEmpty(sfdoc.егНомер))
                {
                    Int32 IntNumber = fmCAVTInvoiceNumberGenerator.GenerateNumber(((ObjectSpace)os).Session, sf.ДействующийДокумент.CID, sf_sfz_type, sf.Дата, 0);
                    sfdoc.егНомер = sf_sfz_type.Prefix + sf.Дата.ToString("yyyyMM").Substring(2, 4) + IntNumber.ToString("00000");
                }
                sfdoc.КодПартнера       = party.Code;
                sfdoc.НаименКонтрагента = party.Name;
                sfdoc.СуммаВсего        = summ_cost + summ_nds;
                sfdoc.СуммаНДС          = summ_nds;
                sfdoc.СуммаВсегоУвел    = sfdoc.СуммаВсего + summ_sub_cost;
                sfdoc.СуммаНДСУвел      = sfdoc.СуммаНДС + summ_sub_nds;
                //fmCAVTInvoiceBase invoice = os.FindObject<fmCAVTInvoiceBase>(
                //    XPQuery<fmCAVTInvoiceBase>.TransformExpression(
                //    ((ObjectSpace)os).Session,
                //    rec => rec.Supplier.Code == imp_rec.SF_VO_CODE &&
                //           rec.RegNumber == imp_rec.SF_INT_NUMBER &&
                //           rec.Date >= sf_date &&
                //           rec.Date < sf_date.AddDays(1)
                //    ));
                ////if (invoice == null) {
                ////    count++;
                ////    System.Console.WriteLine(imp_rec.SF_INT_NUMBER + " " + imp_rec.SF_NUMBER + " " + imp_rec.SF_DATE + " " + summ_cost + " " + summ_nds);
                ////}
                //if (invoice == null) {
                //    crmCParty party = os.GetObjects<crmCParty>(new BinaryOperator("Code", imp_rec.SF_VO_CODE)).FirstOrDefault();
                //    invoice = os.CreateObject<fmCAVTInvoiceBase>();
                //    invoice.RegNumber = imp_rec.SF_INT_NUMBER;
                //    invoice.Number = imp_rec.SF_NUMBER;
                //    invoice.Date = sf_date;
                //    invoice.Supplier = party;
                //    invoice.Customer = register.Party;
                //    invoice.SummAVT = Decimal.Parse(imp_rec.SUMM_NDS.Trim().Replace('.', ','));
                //    invoice.SummCost = Decimal.Parse(imp_rec.SUMM_COST.Trim().Replace('.', ','));
                //}
                //else {
                //    invoice.SummAVT = Decimal.Parse(imp_rec.SUMM_NDS.Trim().Replace('.', ','));
                //    invoice.SummCost = Decimal.Parse(imp_rec.SUMM_COST.Trim().Replace('.', ','));
                //    fmCAVTInvoiceRegisterLine line_check = os.FindObject<fmCAVTInvoiceRegisterLine>(
                //            CriteriaOperator.And(new BinaryOperator("InvoiceVersion", invoice.Current)), true);
                //    if (line_check != null) continue;
                //}
                //fmCAVTInvoiceRegisterLine line = register.InLines.Where(rec => rec.Invoice == invoice).FirstOrDefault();
                //if (line == null) {
                //    line = os.CreateObject<fmCAVTInvoiceRegisterLine>();
                //    register.InLines.Add(line);
                //}
                //line.SequenceNumber = seq_num++;
                ////                line_doc.DateTransfer = invoice.Date;
                //line.Invoice = invoice;
                //line.TransferType = inv_transfer_types.Where(rec => rec.Code == "1").FirstOrDefault();
                //if (String.IsNullOrEmpty(imp_rec.SF_TRANS_DATE.Trim())) {
                //    DateTime trans_date = default(DateTime);
                //    DateTime.TryParseExact(imp_rec.SF_TRANS_DATE.Trim(), "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out trans_date);
                //    line.DateTransfer = trans_date;
                //}
                //if (line.DateTransfer < sf_date)
                //    line.DateTransfer = sf_date;
                //line.OperationType = inv_oper_types.Where(rec => rec.Code == imp_rec.SF_OPER_TYPE.Trim() ||
                //                                               rec.Code == '0' + imp_rec.SF_OPER_TYPE.Trim()).FirstOrDefault();
            }
            System.Console.WriteLine("All " + count);
        }
Example #10
0
        static public void ImportInvoices(IObjectSpace os, fmCAVTInvoiceRegister register, String file_name)
        {
//            OpenFileDialog dialog = new OpenFileDialog();
//            if (dialog.ShowDialog() == DialogResult.OK) {
            FixedFileEngine engine = new FixedFileEngine(typeof(fmCAVTInvoiceImport));

            fmCAVTInvoiceImport[]              imp_res            = (fmCAVTInvoiceImport[])engine.ReadFile(file_name);
            IList <fmCAVTInvoiceType>          inv_types          = os.GetObjects <fmCAVTInvoiceType>();
            IList <fmCAVTInvoiceTransferType>  inv_transfer_types = os.GetObjects <fmCAVTInvoiceTransferType>();
            IList <fmCAVTInvoiceOperationType> inv_oper_types     = os.GetObjects <fmCAVTInvoiceOperationType>();
            Int32  count   = 0;
            UInt32 seq_num = 0;

            foreach (fmCAVTInvoiceImport imp_rec in imp_res)
            {
                imp_rec.SF_VO_CODE    = imp_rec.SF_VO_CODE.Trim();
                imp_rec.SF_INT_NUMBER = imp_rec.SF_INT_NUMBER.Trim();
                imp_rec.SF_NUMBER     = imp_rec.SF_NUMBER.Trim();
                Decimal  summ_cost     = Decimal.Parse(imp_rec.SUMM_COST.Trim().Replace('.', ','));
                Decimal  summ_nds      = Decimal.Parse(imp_rec.SUMM_NDS.Trim().Replace('.', ','));
                Decimal  summ_sub_cost = Decimal.Parse(imp_rec.SUMM_SUB_COST.Trim().Replace('.', ','));
                Decimal  summ_sub_nds  = Decimal.Parse(imp_rec.SUMM_SUB_NDS.Trim().Replace('.', ','));
                DateTime sf_date       = default(DateTime);
                DateTime.TryParseExact(imp_rec.SF_DATE.Trim(), "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out sf_date);
                fmCAVTInvoiceBase invoice = os.FindObject <fmCAVTInvoiceBase>(
                    XPQuery <fmCAVTInvoiceBase> .TransformExpression(
                        ((ObjectSpace)os).Session,
                        rec => rec.Supplier.Code == imp_rec.SF_VO_CODE &&
                        rec.RegNumber == imp_rec.SF_INT_NUMBER &&
                        rec.Date >= sf_date &&
                        rec.Date < sf_date.AddDays(1)
                        ));
                //if (invoice == null) {
                //    count++;
                //    System.Console.WriteLine(imp_rec.SF_INT_NUMBER + " " + imp_rec.SF_NUMBER + " " + imp_rec.SF_DATE + " " + summ_cost + " " + summ_nds);
                //}
                if (invoice == null)
                {
                    crmCParty party = os.GetObjects <crmCParty>(new BinaryOperator("Code", imp_rec.SF_VO_CODE)).FirstOrDefault();
                    invoice           = os.CreateObject <fmCAVTInvoiceBase>();
                    invoice.RegNumber = imp_rec.SF_INT_NUMBER;
                    invoice.Number    = imp_rec.SF_NUMBER;
                    invoice.Date      = sf_date;
                    invoice.Supplier  = party;
                    invoice.Customer  = register.Party;
                    invoice.SummAVT   = Decimal.Parse(imp_rec.SUMM_NDS.Trim().Replace('.', ','));
                    invoice.SummCost  = Decimal.Parse(imp_rec.SUMM_COST.Trim().Replace('.', ','));
                }
                else
                {
                    invoice.SummAVT  = Decimal.Parse(imp_rec.SUMM_NDS.Trim().Replace('.', ','));
                    invoice.SummCost = Decimal.Parse(imp_rec.SUMM_COST.Trim().Replace('.', ','));
                    fmCAVTInvoiceRegisterLine line_check = os.FindObject <fmCAVTInvoiceRegisterLine>(
                        CriteriaOperator.And(new BinaryOperator("InvoiceVersion", invoice.Current)), true);
                    if (line_check != null)
                    {
                        continue;
                    }
                }
                fmCAVTInvoiceRegisterLine line = register.InLines.Where(rec => rec.Invoice == invoice).FirstOrDefault();
                if (line == null)
                {
                    line = os.CreateObject <fmCAVTInvoiceRegisterLine>();
                    register.InLines.Add(line);
                }
                line.SequenceNumber = seq_num++;
                //                line_doc.DateTransfer = invoice.Date;
                line.Invoice      = invoice;
                line.TransferType = inv_transfer_types.Where(rec => rec.Code == "1").FirstOrDefault();
                if (String.IsNullOrEmpty(imp_rec.SF_TRANS_DATE.Trim()))
                {
                    DateTime trans_date = default(DateTime);
                    DateTime.TryParseExact(imp_rec.SF_TRANS_DATE.Trim(), "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out trans_date);
                    line.DateTransfer = trans_date;
                }
                if (line.DateTransfer < sf_date)
                {
                    line.DateTransfer = sf_date;
                }
                line.OperationType = inv_oper_types.Where(rec => rec.Code == imp_rec.SF_OPER_TYPE.Trim() ||
                                                          rec.Code == '0' + imp_rec.SF_OPER_TYPE.Trim()).FirstOrDefault();
            }
            System.Console.WriteLine("All " + count);
        }
Example #11
0
        static public void ImportAvansData(IObjectSpace os, String file_name)
        {
            fmCAVTInvoiceType inv_type = os.GetObjects <fmCAVTInvoiceType>().FirstOrDefault(x => x.Prefix == "ю");
            FileHelperEngine <fmCAVTInvoiceAvansImport> engine = new FileHelperEngine <fmCAVTInvoiceAvansImport>();

            engine.Options.IgnoreFirstLines = 1;
            fmCAVTInvoiceAvansImport[] avans = engine.ReadFile(file_name);
            foreach (fmCAVTInvoiceAvansImport avan in avans)
            {
                avan.VO_CODE   = avan.VO_CODE.Trim();
                avan.SF_NUMBER = avan.SF_NUMBER.Trim();
                avan.PP_NUMBER = avan.PP_NUMBER.Trim();
                avan.TEXT      = avan.TEXT.Trim();
                fmCAVTInvoiceBase invoice = os.FindObject <fmCAVTInvoiceBase>(
                    XPQuery <fmCAVTInvoiceBase> .TransformExpression(
                        ((ObjectSpace)os).Session,
                        rec => rec.Customer.Code == avan.VO_CODE &&
                        rec.Number == avan.SF_NUMBER &&
                        rec.Date >= avan.SF_DATE.Date &&
                        rec.Date <= avan.SF_DATE.Date.AddDays(1)
                        ));
                fmCAVTInvoiceLine line = null;
                if (invoice == null)
                {
                    invoice          = os.CreateObject <fmCAVTInvoiceBase>();
                    invoice.Number   = avan.SF_NUMBER;
                    invoice.Date     = avan.SF_DATE.Date;
                    invoice.Customer = os.GetObjects <crmCParty>(new BinaryOperator("Code", avan.VO_CODE)).FirstOrDefault();
                    invoice.Supplier = os.GetObjects <crmCParty>(new BinaryOperator("Code", "2518")).FirstOrDefault();
                }
                if (invoice != null)
                {
                    invoice.InvoiceType = inv_type;
                    if (invoice.Lines.Count > 0)
                    {
                        line = invoice.Lines[0];
                    }
                    if (line == null)
                    {
                        line = os.CreateObject <fmCAVTInvoiceLine>();
                        invoice.Lines.Add(line);
                    }
                    line.NomenclatureText = avan.TEXT;
                    line.AVTSumm          = avan.SUMM_VAT;
                    line.Cost             = avan.SUMM_ALL - avan.SUMM_VAT;
                    //                    fmCAVTInvoiceVersion.Payment pay = new fmCAVTInvoiceVersion.Payment() {
                    fmCAVTInvoiceVersion.fmCAVTInvoicePayment pay = null;
                    if (invoice.PaymentsList.Count > 0)
                    {
                        pay = invoice.PaymentsList[0];
                    }
                    if (pay == null)
                    {
                        pay = os.CreateObject <fmCAVTInvoiceVersion.fmCAVTInvoicePayment>();
                        invoice.PaymentsList.Add(pay);
                    }
                    pay.Number = avan.PP_NUMBER;
                    pay.Date   = avan.PP_DATE;
                }
            }
        }