Beispiel #1
0
        /// <summary>
        /// Load Cash Line
        /// </summary>
        /// <param name="cash">journal</param>
        /// <param name="cb">cash book</param>
        /// <returns>DocLine Array</returns>
        private DocLine[] LoadLines(MCash cash, MCashBook cb)
        {
            List <DocLine> list = new List <DocLine>();

            MCashLine[] lines = cash.GetLines(false);
            for (int i = 0; i < lines.Length; i++)
            {
                MCashLine    line    = lines[i];
                DocLine_Cash docLine = new DocLine_Cash(line, this);
                //
                list.Add(docLine);
            }

            //	Return Array
            DocLine[] dls = new DocLine[list.Count];
            dls = list.ToArray();
            return(dls);
        }
Beispiel #2
0
        /// <summary>
        /// Create Facts (the accounting logic) for
        /// CMC.
        /// <pre>
        /// Expense
        /// CashExpense     DR
        ///        CashAsset               CR
        ///Receipt
        ///        CashAsset       DR
        ///        CashReceipt             CR
        ///  Charge
        ///        Charge          DR
        ///          CashAsset               CR
        ///  Difference
        ///          CashDifference  DR
        ///          CashAsset               CR
        ///  Invoice
        ///          CashAsset       DR
        ///          CashTransfer            CR
        ///  Transfer
        ///          BankInTransit   DR
        ///          CashAsset               CR
        ///  </pre>
        /// </summary>
        /// <param name="?"></param>
        /// <returns>Fact</returns>
        public override List <Fact> CreateFacts(MAcctSchema as1)
        {
            //  Need to have CashBook
            if (GetC_CashBook_ID() == 0)
            {
                _error = "C_CashBook_ID not set";
                log.Log(Level.SEVERE, _error);
                return(null);
            }

            //  create Fact Header
            Fact fact = new Fact(this, as1, Fact.POST_Actual);

            //  Header posting amt as1 Invoices and Transfer could be differenet currency
            //  CashAsset Total
            Decimal assetAmt           = Env.ZERO;
            Decimal TotalCurrLoss      = Env.ZERO;
            Decimal TotalCurrGain      = Env.ZERO;
            int     headerCashCurrency = 0;
            int     childCashCurrency  = 0;
            int     headerCashOrg      = 0;
            int     childCashOrg       = 0;



            //  Lines
            for (int i = 0; i < _lines.Length; i++)
            {
                DocLine_Cash line     = (DocLine_Cash)_lines[i];
                String       CashType = line.GetCashType();

                if (CashType.Equals(DocLine_Cash.CASHTYPE_EXPENSE))
                {   //  amount is negative
                    //  CashExpense     DR
                    //  CashAsset               CR
                    fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashExpense, as1),
                                    GetC_Currency_ID(), Decimal.Negate(line.GetAmount()), null);
                    //	fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                    //		p_vo.C_Currency_ID, null, line.GetAmount().negate());
                    assetAmt = Decimal.Subtract(assetAmt, Decimal.Negate(line.GetAmount()));
                }
                else if (CashType.Equals(DocLine_Cash.CASHTYPE_RECEIPT))
                {   //  amount is positive
                    //  CashAsset       DR
                    //  CashReceipt             CR
                    //	fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                    //		p_vo.C_Currency_ID, line.GetAmount(), null);
                    assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                    fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashReceipt, as1),
                                    GetC_Currency_ID(), null, line.GetAmount());
                }
                else if (CashType.Equals(DocLine_Cash.CASHTYPE_CHARGE))
                {   //  amount is negative
                    //  Charge          DR
                    //  CashAsset               CR
                    fact.CreateLine(line, line.GetChargeAccount(as1, GetAmount()),
                                    GetC_Currency_ID(), Decimal.Negate(line.GetAmount()), null);
                    //	fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                    //		p_vo.C_Currency_ID, null, line.GetAmount().negate());
                    assetAmt = Decimal.Subtract(assetAmt, Decimal.Negate(line.GetAmount()));
                }
                else if (CashType.Equals(DocLine_Cash.CASHTYPE_DIFFERENCE))
                {   //  amount is pos/neg
                    //  CashDifference  DR
                    //  CashAsset               CR
                    fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashDifference, as1),
                                    GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                    //	fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                    //		p_vo.C_Currency_ID, line.GetAmount());
                    assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                }
                else if (CashType.Equals(DocLine_Cash.CASHTYPE_INVOICE))
                {   //  amount is pos/neg
                    //  CashAsset       DR      dr      --   Invoice is in Invoice Currency !
                    //  CashTransfer    cr      CR
                    if (line.GetC_Currency_ID() == GetC_Currency_ID())
                    {
                        assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                    }
                    else
                    {
                        fact.CreateLine(line,
                                        GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                                        line.GetC_Currency_ID(), line.GetAmount());
                    }
                    fact.CreateLine(line,
                                    GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                                    line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                }
                else if (CashType.Equals(DocLine_Cash.CASHTYPE_TRANSFER))
                {   //  amount is pos/neg
                    //  BankInTransit   DR      dr      --  Transfer is in Bank Account Currency
                    //  CashAsset       dr      CR
                    int temp = GetC_BankAccount_ID();
                    SetC_BankAccount_ID(line.GetC_BankAccount_ID());
                    fact.CreateLine(line,
                                    GetAccount(Doc.ACCTTYPE_BankInTransit, as1),
                                    line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                    SetC_BankAccount_ID(temp);
                    if (line.GetC_Currency_ID() == GetC_Currency_ID())
                    {
                        assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                    }
                    else
                    {
                        fact.CreateLine(line,
                                        GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                                        line.GetC_Currency_ID(), line.GetAmount());
                    }
                }
                // Change to Apply Posting Logic against BusinessPartner
                else if (CashType.Equals(DocLine_Cash.CASHTYPE_BUSINESSPARTNER))
                {   //  amount is pos/neg
                    //  CashAsset       DR      dr      --   Invoice is in Invoice Currency !
                    //  CashTransfer    cr      CR

                    MBPartner bPartner = new MBPartner(GetCtx(), line.GetC_BPartner_ID(), null);
                    if (bPartner != null)
                    {
                        if (bPartner.IsEmployee())
                        {
                            if (line.GetC_Currency_ID() == GetC_Currency_ID())
                            {
                                assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                            }
                            else
                            {
                                fact.CreateLine(line,
                                                GetAccount(Doc.ACCTTYPE_E_Prepayment, as1, line.GetC_BPartner_ID()),
                                                line.GetC_Currency_ID(), line.GetAmount());
                            }
                            fact.CreateLine(line,
                                            GetAccount(Doc.ACCTTYPE_E_Prepayment, as1, line.GetC_BPartner_ID()),
                                            line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                        }
                        else if (bPartner.IsVendor())
                        {
                            if (line.GetC_Currency_ID() == GetC_Currency_ID())
                            {
                                assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                            }
                            else
                            {
                                fact.CreateLine(line,
                                                GetAccount(Doc.ACCTTYPE_V_Prepayment, as1, line.GetC_BPartner_ID()),
                                                line.GetC_Currency_ID(), line.GetAmount());
                            }
                            fact.CreateLine(line,
                                            GetAccount(Doc.ACCTTYPE_V_Prepayment, as1, line.GetC_BPartner_ID()),
                                            line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                        }
                        else if (bPartner.IsCustomer())
                        {
                            if (line.GetC_Currency_ID() == GetC_Currency_ID())
                            {
                                assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                            }
                            else
                            {
                                fact.CreateLine(line,
                                                GetAccount(Doc.ACCTTYPE_C_Prepayment, as1, line.GetC_BPartner_ID()),
                                                line.GetC_Currency_ID(), line.GetAmount());
                            }
                            fact.CreateLine(line,
                                            GetAccount(Doc.ACCTTYPE_C_Prepayment, as1, line.GetC_BPartner_ID()),
                                            line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                        }
                    }
                    else
                    {
                        if (line.GetC_Currency_ID() == GetC_Currency_ID())
                        {
                            assetAmt = Decimal.Add(assetAmt, line.GetAmount());
                        }
                        else
                        {
                            fact.CreateLine(line,
                                            GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                                            line.GetC_Currency_ID(), line.GetAmount());
                        }
                        fact.CreateLine(line,
                                        GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                                        line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                    }
                }


                //Code Added to CashBookTransfer

                else if (CashType.Equals(DocLine_Cash.CASHTYPE_CASHBOOKTRANSFER))
                {
                    ////  amount is negative
                    ////  Charge          DR
                    ////  CashAsset               CR
                    //fact.CreateLine(line, line.GetChargeAccount(as1, GetAmount()),
                    //    GetC_Currency_ID(), Decimal.Negate(line.GetAmount()), null);
                    ////	fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                    ////		p_vo.C_Currency_ID, null, line.GetAmount().negate());
                    //assetAmt = Decimal.Subtract(assetAmt, Decimal.Negate(line.GetAmount()));
                    int temp = GetC_CashBook_ID();
                    SetC_CashBook_ID(line.Get_C_CashBook_ID());

                    if (BeginningBalance > 0)
                    {
                        fact.CreateLine(line,
                                        GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                                        line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                        SetC_CashBook_ID(temp);
                        //if (line.GetC_Currency_ID() == (new MCashBook(GetCtx(), GetC_CashBook_ID(), null)).GetC_Currency_ID())// GetC_Currency_ID())
                        //{
                        //if (BeginningBalance > assetAmt)
                        //if (BeginningBalance >= Math.Abs(line.GetAmount()))
                        //if (assetAmt <= Math.Abs(line.GetAmount()))
                        //{
                        assetAmt = (Decimal.Subtract(assetAmt, Decimal.Negate(line.GetAmount())));
                        //}
                        //else
                        //{
                        //    assetAmt = (Decimal.Subtract(assetAmt, Decimal.Negate(line.GetAmount())));
                        //}
                        //}
                        //else
                        //{
                        //    fact.CreateLine(line,
                        //        GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                        //        line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                        //}
                    }
                    else
                    {
                        //fact.CreateLine(line,
                        //    GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                        //    line.GetC_Currency_ID(), decimal.Negate(line.GetAmount()));
                        fact.CreateLine(line,
                                        GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                                        (new MCashBook(GetCtx(), HeaderCasbookID, null)).GetC_Currency_ID(), decimal.Negate(line.GetAmount()));
                        SetC_CashBook_ID(temp);
                        //if (line.GetC_Currency_ID() == (new MCashBook(GetCtx(), GetC_CashBook_ID(), null)).GetC_Currency_ID())// GetC_Currency_ID())
                        //{
                        assetAmt = (Decimal.Add(assetAmt, line.GetAmount()));
                        //}
                        //else
                        //{
                        //    fact.CreateLine(line,
                        //        GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                        //        line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                        //}
                    }
                }

                else if (CashType.Equals(DocLine_Cash.CASHTYPE_CASHRECIEVEDFROM))
                {
                    int temp = GetC_CashBook_ID();
                    //SetC_CashBook_ID(line.Get_C_CashBook_ID());
                    //fact.CreateLine(line,
                    //    GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                    //    line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                    //SetC_CashBook_ID(temp);
                    //if (line.GetC_Currency_ID() == GetC_Currency_ID())
                    //{
                    //assetAmt = (Decimal.Add(assetAmt, line.GetAmount()));
                    //}
                    //else
                    //{
                    //    fact.CreateLine(line,
                    //        GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                    //        line.GetC_Currency_ID(), line.GetAmount());
                    //}
                    headerCashCurrency = (new MCashBook(GetCtx(), HeaderCasbookID, null).GetC_Currency_ID());
                    childCashCurrency  = (new MCashBook(GetCtx(), line.Get_C_CashBook_ID(), null).GetC_Currency_ID());
                    headerCashOrg      = (new MCashBook(GetCtx(), HeaderCasbookID, null).GetAD_Org_ID());
                    childCashOrg       = (new MCashBook(GetCtx(), line.Get_C_CashBook_ID(), null).GetAD_Org_ID());

                    //else
                    //{
                    if (headerCashCurrency != childCashCurrency)
                    {
                        Decimal transferdAmt = Util.GetValueOfDecimal(DB.ExecuteScalar("SELECT Amount FROM C_CashLine WHERE C_CashLine_ID =" + line.Get_C_CashLine_Ref_ID()));
                        Decimal recievedAmt  = MConversionRate.Convert(GetCtx(), line.GetAmount(), headerCashCurrency, childCashCurrency, GetAD_Client_ID(), GetAD_Org_ID());
                        Decimal res          = Decimal.Subtract(recievedAmt, Math.Abs(transferdAmt));
                        if (res > 0)
                        {
                            TotalCurrGain = Decimal.Add(TotalCurrGain, res);
                        }
                        else if (res < 0)
                        {
                            TotalCurrLoss = Decimal.Add(TotalCurrLoss, Decimal.Negate(res));
                        }

                        SetC_CashBook_ID(line.Get_C_CashBook_ID());

                        int OrgID = line.GetAD_Org_ID();
                        if (headerCashOrg != childCashOrg)
                        {
                            OrgID = childCashOrg;
                        }
                        //else
                        //{
                        transferdAmt = MConversionRate.Convert(GetCtx(), transferdAmt, childCashCurrency, headerCashCurrency, GetAD_Client_ID(), GetAD_Org_ID());
                        //}
                        fact.CreateLine(line,
                                        GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                                        headerCashCurrency, transferdAmt, OrgID);
                        SetC_CashBook_ID(temp);
                    }
                    else
                    {
                        SetC_CashBook_ID(line.Get_C_CashBook_ID());
                        if (headerCashOrg != childCashOrg)
                        {
                            fact.CreateLine(line,
                                            GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                                            line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()), childCashOrg);
                        }
                        else
                        {
                            fact.CreateLine(line,
                                            GetAccount(Doc.ACCTTYPE_CashTransfer, as1),
                                            line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                        }
                        SetC_CashBook_ID(temp);
                    }


                    //if (headerCashOrg != childCashOrg)
                    //{
                    //    DataSet ds = DB.ExecuteDataset("SELECT INTERCOMPANYDUETO_ACCT,INTERCOMPANYDUEFROM_ACCT FROM C_AcctSchema_GL WHERE AD_Client_ID=" + GetAD_Client_ID());
                    //    int dueFrom = 0;
                    //    int dueTo = 0;
                    //    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    //    {
                    //        dueTo = Util.GetValueOfInt(ds.Tables[0].Rows[0]["INTERCOMPANYDUETO_ACCT"]);
                    //        dueFrom = Util.GetValueOfInt(ds.Tables[0].Rows[0]["INTERCOMPANYDUEFROM_ACCT"]);
                    //        if (dueFrom > 0 && dueTo > 0)
                    //        {
                    //            fact.CreateLine(line, MAccount.Get(GetCtx(), dueFrom),line.GetC_Currency_ID(), line.GetAmount());
                    //            fact.CreateLine(line, MAccount.Get(GetCtx(), dueTo), line.GetC_Currency_ID(), Decimal.Negate(line.GetAmount()));
                    //        }
                    //    }

                    //}
                    //}
                    assetAmt = (Decimal.Add(assetAmt, line.GetAmount()));
                }
            }   //  lines

            if (TotalCurrGain != Env.ZERO)
            {
                int      validComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT C_ValidCombination_ID FROM C_ValidCombination WHERE Account_ID= ( SELECT C_ElementValue_ID FROM C_ElementValue WHERE Value='80540' AND AD_Client_ID = " + GetAD_Client_ID() + " )"));
                MAccount acct       = MAccount.Get(GetCtx(), validComID);
                TotalCurrGain = MConversionRate.Convert(GetCtx(), TotalCurrGain, childCashCurrency, headerCashCurrency, GetAD_Client_ID(), GetAD_Org_ID());


                fact.CreateLine(null, acct,
                                GetC_Currency_ID(), Decimal.Negate(TotalCurrGain));
            }
            if (TotalCurrLoss != Env.ZERO)
            {
                int      validComID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT C_ValidCombination_ID FROM C_ValidCombination WHERE Account_ID= ( SELECT C_ElementValue_ID FROM C_ElementValue WHERE Value='82540' AND AD_Client_ID = " + GetAD_Client_ID() + " )"));
                MAccount acct       = MAccount.Get(GetCtx(), validComID);
                TotalCurrLoss = MConversionRate.Convert(GetCtx(), TotalCurrLoss, childCashCurrency, headerCashCurrency, GetAD_Client_ID(), GetAD_Org_ID());


                fact.CreateLine(null, acct,
                                GetC_Currency_ID(), (TotalCurrLoss));
            }
            //

            //  Cash Asset
            fact.CreateLine(null, GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                            GetC_Currency_ID(), assetAmt);
            List <Fact> facts = new List <Fact>();

            facts.Add(fact);
            return(facts);
        }
Beispiel #3
0
 private void CreateFactLine(Fact fact, DocLine_Cash line, MAcctSchema as1, decimal assetAmt)
 {
     fact.CreateLine(line, GetAccount(Doc.ACCTTYPE_CashAsset, as1),
                     GetC_Currency_ID(), assetAmt);
 }