using Winbooks.TORM.DAL.Accounting;
using Winbooks.TORM.OM;
using Winbooks.TORM.DAL;

using Winbooks.TORM.OM.Accounting;

using Winbooks.Apis.Services.v2;

namespace Winbooks.Apis.Demo
{
    public class GetEntity
    {
        public static GLTransHeader GetGLTransHeader(EntitiesServices folder, string jnlCode, long docNumber)
        {
            string msg = "GLTransHeader of " + jnlCode + " " + docNumber.ToString();
            Tools.Log("Getting " + msg, true);

            ICriteria criteria1 = new GLTransHeaderDAO().CreateCriteria()
                .CreateAlias("jnl", typeof(Journal), Journal.Names.Id, GLTransHeader.Names.Journal_Id)
                .Add(Condition.Eq("jnl." + Journal.Names.Code, jnlCode))
                .Add(Condition.Eq(GLTransHeader.Names.DocNumber, docNumber));

            GLTransHeader header = folder.GetFilter<GLTransHeader>(criteria1);
            Tools.LogErrorOrOk(header, msg);
            return header;
        }

        public static GLTrans GetGLTrans(EntitiesServices folder, string jnlCode, long docNumber, int lineOrder = 0)
        {
            GLTransHeader header = GetGLTransHeader(folder, jnlCode, docNumber);
            if (header == null) return null;

            string msg = "GLTrans of " + jnlCode + " " + docNumber.ToString() + " with LineOrder = " + lineOrder.ToString();
            Tools.Log("Getting " + msg, true);

            ICriteria criteria2 = new GLTransDAO().CreateCriteria()
                .Add(Condition.Eq(GLTrans.Names.GLTransHeader_Id, header.Id))
                .Add(Condition.Eq(GLTrans.Names.LineOrder, lineOrder));

            GLTrans glTrans = folder.GetFilter<GLTrans>(criteria2);
            Tools.LogErrorOrOk(glTrans, msg);
            return glTrans;
        }

        public static BookPeriod GetBookPeriod(EntitiesServices folder, int relatedYear, int relatedMonth)
        {
            string msg = "Period " + relatedYear.ToString() + "." + relatedMonth.ToString();
            Tools.Log("Getting " + msg, true);

            var periodCriteria = new BookPeriodDAO().CreateCriteria()
                .Add(Condition.Eq(BookPeriod.Names.RelatedYear, relatedYear))
                .Add(Condition.Eq(BookPeriod.Names.RelatedMonth, relatedMonth))
                .Add(Condition.NotEq(BookPeriod.Names.Number, 0))                    // to avoid opening period
                .Order(BookPeriod.Names.Number, true)                                // to avoid the closing period
                .SetFirstResult(0).SetMaxResults(1);

            BookPeriod bookPeriod = folder.GetFilter<BookPeriod>(periodCriteria);
            Tools.LogErrorOrOk(bookPeriod, msg);
            return bookPeriod;
        }

        public static Journal GetJournal(EntitiesServices folder, string journalCode, BookPeriod bookPeriod)
        {
            string msg = "Journal " + journalCode;
            Tools.Log("Getting " + msg, true);

            var journalCriteria = new JournalDAO().CreateCriteria()
                .Add(Condition.Eq(Journal.Names.BookYear_Id, bookPeriod.BookYear_Id))
                .Add(Condition.Eq(Journal.Names.Code, journalCode))
                .SetFirstResult(0).SetMaxResults(1);

            Journal journal = folder.GetFilter<Journal>(journalCriteria);
            Tools.LogErrorOrOk(journal, msg);
            return journal;
        }

        public static Currency GetCurrency(EntitiesServices folder, string currencyCode)
        {
            string msg = "Currency " + currencyCode;
            Tools.Log("Getting " + msg, true);
            Currency currency = folder.GetByCode<Currency>(currencyCode);
            Tools.LogErrorOrOk(currency, msg);
            return currency;
        }

        public static Customer GetCustomer(EntitiesServices folder, string code)
        {
            string msg = "Customer " + code;
            Tools.Log("Getting " + msg, true);
            Customer customer = folder.GetByCode<Customer>(code, true);
            Tools.LogErrorOrOk(customer, msg);
            return customer;
        }

        public static GLAccount GetGLAccount(EntitiesServices folder, string glAccountCode, BookPeriod bookPeriod)
        {
            string msg = "Account " + glAccountCode;
            Tools.Log("Getting " + msg, true);

            var turnOverGLAccountCriteria = new GLAccountDAO().CreateCriteria()
                .CreateAlias("gLAccountBookYear", typeof(GLAccount_BookYear), GLAccount_BookYear.Names.GLAccount_Id, GLAccount.Names.Id)
                .Add(Condition.Eq("gLAccountBookYear." + GLAccount_BookYear.Names.BookYear_Id, bookPeriod.BookYear_Id))
                .Add(Condition.Eq(GLAccount.Names.Code, glAccountCode))
                .SetFirstResult(0).SetMaxResults(1);

            GLAccount glAccount = folder.GetFilter<GLAccount>(turnOverGLAccountCriteria);
            Tools.LogErrorOrOk(glAccount, msg);
            return glAccount;
        }

        public static Vat GetVat(EntitiesServices folder, string code)
        {
            string msg = "Vat " + code;
            Tools.Log("Getting " + msg, true);

            Vat vat = folder.GetByCode<Vat>(code, true);

            Tools.LogErrorOrOk(vat, msg);
            return vat;
        }

        public static GLAccount GetGLAccountByCentralAccountTypeId(EntitiesServices folder, System.Guid id)
        {
            if (id == System.Guid.Empty)
                return null;

            string msg = "GLAccount of CentralAccountType Id " + id.ToString();
            Tools.Log("Getting " + msg, true);

            var criteria = new GLAccount_CentralAccountTypeDAO().CreateCriteria()
                .Add(Condition.Eq(GLAccount_CentralAccountType.Names.CentralAccountType_Id, id))
                .SetFirstResult(0).SetMaxResults(1);

            GLAccount_CentralAccountType glAccCentType = folder.GetFilter<GLAccount_CentralAccountType>(criteria);
            Tools.LogErrorOrOk(glAccCentType, msg);
            if (glAccCentType == null)
                return null;
            if (glAccCentType.GLAccount_Id == System.Guid.Empty)
                return null;
            GLAccount glAccount = folder.GetById<GLAccount>(glAccCentType.GLAccount_Id);
            Tools.LogErrorOrOk(glAccount, msg);
            return glAccount;
        }

        public static Address GetDefaultAddressInvoicing(EntitiesServices folder, System.Guid thirdId)
        {
            Tools.Log("Getting the first address of third "+thirdId.ToString(), true);

            var crit = new AddressDAO().CreateCriteria()
                .CreateAlias("thirdaddr", typeof(Third_Address), Third_Address.Names.Address_Id, Address.Names.Id)
                .Add(Condition.Eq("thirdaddr."+Third_Address.Names.Third_Id, thirdId))
                .SetFirstResult(0).SetMaxResults(1);

            Address address = folder.GetFilter<Address>(crit);

            if (address == null)
                Tools.Log("=> no address found");
            else
                Tools.Log("=> address found: " + address.Address1);
            return address;
        }

    }
}
