Пример #1
0
        //TODO: remove in 2018R2
        internal static void DeleteLegacyTaxRows(PXGraph graph, string refNbr)
        {
            if (!String.IsNullOrEmpty(refNbr))
            {
                PXCache        claimCache   = graph.Caches[typeof(EPExpenseClaim)];
                PXCache        taxTranCache = graph.Caches[typeof(EPTaxTran)];
                EPExpenseClaim claim        = PXSelect <EPExpenseClaim,
                                                        Where <EPExpenseClaim.refNbr, Equal <Required <EPExpenseClaim.refNbr> > > > .Select(graph, refNbr);

                bool taxTransDeleted = false;
                foreach (EPTaxAggregate taxTran in PXSelect <EPTaxAggregate,
                                                             Where <EPTaxAggregate.refNbr, Equal <Required <EPExpenseClaim.refNbr> > > > .Select(graph, refNbr))
                {
                    claim.TaxTotal     -= taxTran.TaxAmt;
                    claim.CuryTaxTotal -= taxTran.CuryTaxAmt;
                    taxTransDeleted     = true;
                    graph.Caches[typeof(EPTaxAggregate)].Delete(taxTran);
                }
                if (taxTransDeleted)
                {
                    claim.VatTaxableTotal     = 0;
                    claim.CuryVatTaxableTotal = 0;
                    claim.CuryVatExemptTotal  = 0;
                    claim.VatExemptTotal      = 0;
                    claimCache.Update(claim);
                }
            }
        }
Пример #2
0
        public static void RefNbrUpdated(PXCache receiptsCache, EPExpenseClaim claim, EPExpenseClaimDetails receipt)
        {
            if (claim != null)
            {
                receipt.ClaimCuryInfoID = claim?.CuryInfoID;
                receipt.SubmitedDate    = DateTime.Now;
                RecalcAmountInClaimCury(receiptsCache, receipt);
            }
            else
            {
                receipt.SubmitedDate    = null;
                receipt.ClaimCuryInfoID = null;

                receipt.ClaimCuryTranAmt          = 0;
                receipt.ClaimCuryTranAmtWithTaxes = 0;
                receipt.ClaimCuryTaxTotal         = 0;
                receipt.ClaimCuryTaxRoundDiff     = 0;
                receipt.ClaimCuryVatExemptTotal   = 0;
                receipt.ClaimCuryVatTaxableTotal  = 0;
            }
            foreach (EPTaxTran copy in PXSelect <EPTaxTran,
                                                 Where <EPTaxTran.claimDetailID, Equal <Required <EPExpenseClaimDetails.claimDetailID> > > > .Select(receiptsCache.Graph, receipt.ClaimDetailID))
            {
                copy.RefNbr = receipt.RefNbr;
                receiptsCache.Graph.Caches[typeof(EPTaxTran)].Update(copy);
            }
        }
Пример #3
0
        protected virtual IEnumerable expenseClaimRecords()
        {
            MyExpenseClaimsEnqFilter filter = Filter.Current as MyExpenseClaimsEnqFilter;

            PXSelectBase <EPExpenseClaim> select =
                new PXSelectJoin <EPExpenseClaim,
                                  LeftJoin <APInvoice, On <APInvoice.refNbr, Equal <EPExpenseClaim.aPRefNbr> >,
                                            InnerJoin <EPEmployee, On <EPEmployee.bAccountID, Equal <EPExpenseClaim.employeeID> > > >,
                                  Where <EPEmployee.userID, Equal <Current <AccessInfo.userID> > >,
                                  OrderBy <Desc <EPExpenseClaim.docDate,
                                                 Asc <EPExpenseClaim.refNbr> > > >(this);

            if (filter.StartDate.HasValue)
            {
                select.WhereAnd <Where <EPExpenseClaim.docDate, GreaterEqual <Current <MyExpenseClaimsEnqFilter.startDate> > > >();
            }
            if (filter.EndDate.HasValue)
            {
                select.WhereAnd <Where <EPExpenseClaim.docDate, LessEqual <Current <MyExpenseClaimsEnqFilter.endDate> > > >();
            }

            foreach (PXResult <EPExpenseClaim, APInvoice> res in select.Select())
            {
                EPExpenseClaim claim = (EPExpenseClaim)res;
                if ((((filter.Released ?? false) && claim.Status == EPClaimStatus.Released) ||
                     ((filter.Approved ?? false) && claim.Status == EPClaimStatus.Approved) ||
                     ((filter.Pending ?? false) && claim.Status == EPClaimStatus.Balanced) ||
                     ((filter.Voided ?? false) && claim.Status == EPClaimStatus.Voided) ||
                     ((filter.OnHold ?? false) && claim.Status == EPClaimStatus.Hold)))
                {
                    yield return(res);
                }
            }
        }
        public virtual void _(Events.FieldVerifying <EPExpenseClaimDetails.refNbr> e)
        {
            EPExpenseClaimDetails receipt = (EPExpenseClaimDetails)e.Row;
            string newClaimRefNbr         = (string)e.NewValue;

            object pendingCorpCard = e.Cache.GetValuePending <EPExpenseClaimDetails.corpCardID>(receipt);
            int?   corpCardID      = null;

            if (pendingCorpCard == PXCache.NotSetValue)
            {
                corpCardID = receipt.CorpCardID;
            }
            else
            {
                if (pendingCorpCard is string corpCardCD)
                {
                    corpCardID = CACorpCard.PKCD.Find(e.Cache.Graph, corpCardCD)?.CorpCardID;
                }
                else
                {
                    corpCardID = (int?)pendingCorpCard;
                }
            }

            EPExpenseClaim newClaim = GetParentClaim(newClaimRefNbr);

            VerifyClaimAndCorpCardCurrencies(corpCardID, newClaim);
            VerifyEmployeeAndClaimCurrenciesForCash(receipt, receipt.PaidWith, newClaim);
        }
Пример #5
0
 public static void SubmitReceipt(PXCache claimsCache, PXCache receiptsCache, EPExpenseClaim claim, EPExpenseClaimDetails receipt)
 {
     receipt        = receiptsCache.CreateCopy(receipt) as EPExpenseClaimDetails;
     receipt.RefNbr = claim.RefNbr;
     RefNbrUpdated(receiptsCache, claim, receipt);
     receiptsCache.Update(receipt);
 }
        public virtual void VerifyEmployeeAndClaimCurrenciesForCash(
            EPExpenseClaimDetails receipt,
            string receiptPaidWith,
            EPExpenseClaim claim,
            Action substituteNewValue = null)
        {
            if (claim == null)
            {
                return;
            }

            if (receiptPaidWith == EPExpenseClaimDetails.paidWith.PersonalAccount)
            {
                EPEmployee employee =
                    PXSelect <EPEmployee,
                              Where <EPEmployee.bAccountID, Equal <Required <EPEmployee.bAccountID> > > >
                    .Select(Base, receipt.EmployeeID);

                if (employee.AllowOverrideCury != true && employee.CuryID != null && employee.CuryID != claim.CuryID)
                {
                    substituteNewValue?.Invoke();

                    throw new PXSetPropertyException(Messages.ReceiptCannotBeAddedToClaimBecauseClaimCuryDiffersFromEmployeeCury);
                }
            }
        }
        public virtual IEnumerable editDetail(PXAdapter adapter)
        {
            if (Records.Current != null && Records.Current.RefNoteID != null)
            {
                bool navigate = true;

                EntityHelper helper = new EntityHelper(this);
                Note         note   = helper.SelectNote(Records.Current.RefNoteID);

                if (note != null && note.EntityType == typeof(EPExpenseClaim).FullName)
                {
                    EPExpenseClaim claim = PXSelect <EPExpenseClaim, Where <EPExpenseClaim.noteID, Equal <Required <EPExpenseClaim.noteID> > > > .Select(this, note.NoteID);

                    if (claim != null)
                    {
                        ExpenseClaimEntry graph  = PXGraph.CreateInstance <ExpenseClaimEntry>();
                        EPExpenseClaim    target = graph.ExpenseClaim.Search <EPExpenseClaim.refNbr>(claim.RefNbr);
                        if (target == null)
                        {
                            navigate = false;
                        }
                    }
                    else
                    {
                        using (new PXReadBranchRestrictedScope())
                        {
                            claim = PXSelect <EPExpenseClaim, Where <EPExpenseClaim.noteID, Equal <Required <EPExpenseClaim.noteID> > > > .Select(this, note.NoteID);

                            if (claim != null)
                            {
                                throw new PXException(Messages.YouAreNotAlowedToViewTheDetails);
                            }
                        }
                    }
                }

                if (note != null && note.EntityType == typeof(EPTimeCard).FullName)
                {
                    EPTimeCard timecard = PXSelect <EPTimeCard, Where <EPTimeCard.noteID, Equal <Required <EPTimeCard.noteID> > > > .Select(this, note.NoteID);

                    if (timecard != null)
                    {
                        TimeCardMaint graph  = PXGraph.CreateInstance <TimeCardMaint>();
                        EPTimeCard    target = graph.Document.Search <EPTimeCard.timeCardCD>(timecard.TimeCardCD);
                        if (target == null)
                        {
                            navigate = false;
                        }
                    }
                }

                if (navigate)
                {
                    helper.NavigateToRow(Records.Current.RefNoteID.Value, PXRedirectHelper.WindowMode.InlineWindow);
                }
            }
            return(adapter.Get());
        }
Пример #8
0
        protected virtual void editDetail()
        {
            if (Claim.Current == null)
            {
                return;
            }
            EPExpenseClaim row = PXSelect <EPExpenseClaim, Where <EPExpenseClaim.refNbr, Equal <Required <EPExpenseClaim.refNbr> > > > .SelectSingleBound(this, null, Claim.Current.RefNbr);

            PXRedirectHelper.TryRedirect(this, row, PXRedirectHelper.WindowMode.InlineWindow);
        }
Пример #9
0
        public void ReleaseDocProc(EPExpenseClaim claim, ReleaseDocProcDelegate baseMethod)
        {
            baseMethod(claim);

            if (TWNGUIValidation.ActivateTWGUI(Base) == true &&
                claim != null &&
                claim.Released == true)
            {
                ExpenseClaimEntry graph = PXGraph.CreateInstance <ExpenseClaimEntry>();
                TWNReleaseProcess rp    = PXGraph.CreateInstance <TWNReleaseProcess>();

                Vendor vendor = new Vendor();

                foreach (TWNManualGUIExpense manualGUIExp in SelectFrom <TWNManualGUIExpense> .Where <TWNManualGUIExpense.refNbr.IsEqual <@P.AsString> > .View.Select(Base, claim.RefNbr))
                {
                    if (PXCache <Tax> .GetExtension <TaxExt>(Tax.PK.Find(Base, manualGUIExp.TaxID)).UsrTWNGUI == false)
                    {
                        continue;
                    }

                    using (PXTransactionScope ts = new PXTransactionScope())
                    {
                        if (manualGUIExp.VendorID != null)
                        {
                            vendor = SelectFrom <Vendor> .Where <Vendor.bAccountID.IsEqual <@P.AsInt> > .View.SelectSingleBound(Base, null, manualGUIExp.VendorID);
                        }

                        rp.CreateGUITrans(new STWNGUITran()
                        {
                            VATCode       = manualGUIExp.VATInCode,
                            GUINbr        = manualGUIExp.GUINbr,
                            GUIStatus     = TWNGUIStatus.Used,
                            BranchID      = graph.ExpenseClaimDetailsCurrent.Current.BranchID,
                            GUIDirection  = TWNGUIDirection.Receipt,
                            GUIDate       = manualGUIExp.GUIDate,
                            GUITitle      = vendor.AcctName,
                            TaxZoneID     = manualGUIExp.TaxZoneID,
                            TaxCategoryID = manualGUIExp.TaxCategoryID,
                            TaxID         = manualGUIExp.TaxID,
                            TaxNbr        = manualGUIExp.TaxNbr,
                            OurTaxNbr     = manualGUIExp.OurTaxNbr,
                            NetAmount     = manualGUIExp.NetAmt,
                            TaxAmount     = manualGUIExp.TaxAmt,
                            AcctCD        = vendor.AcctCD,
                            AcctName      = vendor.AcctName,
                            DeductionCode = manualGUIExp.Deduction,
                            Remark        = manualGUIExp.Remark,
                            OrderNbr      = manualGUIExp.RefNbr
                        });

                        ts.Complete(Base);
                    }
                }
            }
        }
Пример #10
0
 protected virtual void CreateNew()
 {
     using (new PXPreserveScope())
     {
         ExpenseClaimEntry graph = (ExpenseClaimEntry)PXGraph.CreateInstance(typeof(ExpenseClaimEntry));
         graph.Clear(PXClearOption.ClearAll);
         EPExpenseClaim claim = (EPExpenseClaim)graph.ExpenseClaim.Cache.CreateInstance();
         graph.ExpenseClaim.Insert(claim);
         graph.ExpenseClaim.Cache.IsDirty = false;
         PXRedirectHelper.TryRedirect(graph, PXRedirectHelper.WindowMode.InlineWindow);
     }
 }
Пример #11
0
        protected virtual void EPExpenseClaimDetails_CuryID_FieldUpdating(PXCache cache, PXFieldUpdatingEventArgs e)
        {
            EPExpenseClaim claim = CurrentClaim.SelectSingle();
            var            row   = e.Row as EPExpenseClaimDetails;

            if (claim != null &&
                row != null &&
                claim.CuryInfoID == row.CuryInfoID)
            {
                row.CuryInfoID = null;
                ClaimDetails.Cache.Update(row);
            }
        }
Пример #12
0
        protected virtual void ViewClaim()
        {
            if (ClaimDetails.Current != null && ClaimDetails.Current.RefNbr != null)
            {
                EPExpenseClaim claim = PXSelect <EPExpenseClaim, Where <EPExpenseClaim.refNbr, Equal <Required <EPExpenseClaimDetails.refNbr> > > > .SelectSingleBound(this, null, ClaimDetails.Current.RefNbr);

                if (claim == null)
                {
                    return;
                }
                PXRedirectHelper.TryRedirect(this, claim, PXRedirectHelper.WindowMode.NewWindow);
            }
        }
        public virtual void SumClaimValues(EPExpenseClaim claim, EPExpenseClaimDetails receipt, EPExpenseClaimDetails oldReceipt)
        {
            EPExpenseClaim newClaim = (EPExpenseClaim)Claim.Cache.CreateCopy(claim);

            SumCalc sumCalculator = new SumCalc();

            newClaim.CuryDocBal          += (decimal?)sumCalculator.Calculate <EPExpenseClaimDetails.claimCuryTranAmtWithTaxes>(Receipts.Cache, receipt, oldReceipt);
            newClaim.CuryTaxTotal        += (decimal?)sumCalculator.Calculate <EPExpenseClaimDetails.claimCuryTaxTotal>(Receipts.Cache, receipt, oldReceipt);
            newClaim.CuryTaxRoundDiff    += (decimal?)sumCalculator.Calculate <EPExpenseClaimDetails.claimCuryTaxRoundDiff>(Receipts.Cache, receipt, oldReceipt);
            newClaim.CuryVatExemptTotal  += (decimal?)sumCalculator.Calculate <EPExpenseClaimDetails.claimCuryVatExemptTotal>(Receipts.Cache, receipt, oldReceipt);
            newClaim.CuryVatTaxableTotal += (decimal?)sumCalculator.Calculate <EPExpenseClaimDetails.claimCuryVatTaxableTotal>(Receipts.Cache, receipt, oldReceipt);

            Claim.Update(newClaim);
        }
        public virtual void VerifyClaimAndCorpCardCurrencies(int?corpCardID, EPExpenseClaim claim, Action substituteNewValue = null)
        {
            if (corpCardID != null && claim != null)
            {
                CashAccount cashAccount = CACorpCardsMaint.GetCardCashAccount(Base, corpCardID);

                if (cashAccount.CuryID != claim.CuryID)
                {
                    substituteNewValue?.Invoke();

                    throw new PXSetPropertyException(Messages.CardCurrencyAndTheClaimCurrencyMustBeEqual);
                }
            }
        }
Пример #15
0
        protected virtual void EPExpenseClaimDetails_RowPersisting(PXCache cache, PXRowPersistingEventArgs e)
        {
            EPExpenseClaimDetails row = (EPExpenseClaimDetails)e.Row;

            if (!string.IsNullOrEmpty(row.RefNbr) && e.Operation != PXDBOperation.Delete)
            {
                EPExpenseClaim claim = CurrentClaim.SelectSingle(row.RefNbr);
                if (claim != null &&
                    epsetup.Current.AllowMixedTaxSettingInClaims == false)
                {
                    if (claim.TaxZoneID != CurrentClaimDetails.Current.TaxZoneID)
                    {
                        cache.RaiseExceptionHandling <EPExpenseClaimDetails.taxZoneID>(row,
                                                                                       row.TaxZoneID,
                                                                                       new PXSetPropertyException(Messages.TaxZoneNotMatch, row.ClaimDetailID, claim.RefNbr));
                    }
                    if (claim.TaxCalcMode != row.TaxCalcMode)
                    {
                        cache.RaiseExceptionHandling <EPExpenseClaimDetails.taxCalcMode>(row,
                                                                                         row.TaxCalcMode,
                                                                                         new PXSetPropertyException(Messages.TaxCalcModeNotMatch, row.ClaimDetailID, claim.RefNbr));
                    }
                }
            }
            if (row != null && e.Operation != PXDBOperation.Delete)
            {
                if (row.Hold == false)
                {
                    if (!epsetup.Current.NonTaxableTipItem.HasValue && (row.CuryTipAmt ?? 0) != 0)
                    {
                        cache.RaiseExceptionHandling <EPExpenseClaimDetails.curyTipAmt>(row,
                                                                                        row.CuryTipAmt,
                                                                                        new PXSetPropertyException(Messages.TipItemIsNotDefined));
                    }
                    if (glsetup.Current.RoundingLimit < Math.Abs(row.TaxRoundDiff ?? 0m))
                    {
                        throw new PXException(AP.Messages.RoundingAmountTooBig, currencyinfo?.Current?.BaseCuryID, row.TaxRoundDiff,
                                              PXDBQuantityAttribute.Round(this.glsetup.Current.RoundingLimit));
                    }
                }
                if (row.CuryTipAmt > 0 && row.CuryExtCost < 0 || row.CuryTipAmt < 0 && row.CuryExtCost > 0)
                {
                    cache.RaiseExceptionHandling <EPExpenseClaimDetails.curyTipAmt>(row,
                                                                                    row.CuryTipAmt,
                                                                                    new PXSetPropertyException(Messages.TipSign));
                }
            }
        }
        public virtual void _(Events.RowPersisting <EPExpenseClaimDetails> e)
        {
            if (e.Operation != PXDBOperation.Delete)
            {
                if (e.Row.PaidWith != EPExpenseClaimDetails.paidWith.PersonalAccount && e.Row.CorpCardID == null)
                {
                    var fieldName = typeof(EPExpenseClaimDetails.corpCardID).Name;
                    throw new PXRowPersistingException(fieldName, e.Row.CorpCardID, ErrorMessages.FieldIsEmpty, fieldName);
                }

                EPExpenseClaim claim = GetParentClaim(e.Row.RefNbr);

                VerifyEmployeeAndClaimCurrenciesForCash(e.Row, e.Row.PaidWith, claim);
                VerifyClaimAndCorpCardCurrencies(e.Row.CorpCardID, claim);
                VerifyExpenseRefNbrIsNotEmpty(e.Row);
            }
        }
        public virtual void _(Events.RowSelected <EPExpenseClaimDetails> e)
        {
            if (e.Row is EPExpenseClaimDetails row)
            {
                EPExpenseClaim claim = GetParentClaim(row.RefNbr);

                bool enabledApprovalReceipt = PXAccess.FeatureInstalled <FeaturesSet.approvalWorkflow>() && epsetup.Current.ClaimDetailsAssignmentMapID != null;
                bool legacyClaim            = row.LegacyReceipt == true && !String.IsNullOrEmpty(row.RefNbr);
                bool enabledEditReceipt     = (row.Hold == true || !enabledApprovalReceipt || (UseClaimStatus && claim?.Hold == true)) && !legacyClaim;

                if (claim != null)
                {
                    bool enabledEditClaim = (claim.Hold == true);
                    enabledEditReceipt = enabledEditReceipt && enabledEditClaim;
                }

                bool notMatchedToBankTran = row.BankTranDate == null;

                e.Cache.Adjust <PXUIFieldAttribute>()
                .For <EPExpenseClaimDetails.expenseDate>(ui =>
                                                         ui.Enabled = notMatchedToBankTran && enabledEditReceipt)
                .SameFor <EPExpenseClaimDetails.qty>()
                .SameFor <EPExpenseClaimDetails.uOM>()
                .SameFor <EPExpenseClaimDetails.curyUnitCost>()
                .SameFor <EPExpenseClaimDetails.curyEmployeePart>()
                .SameFor <EPExpenseClaimDetails.curyExtCost>()
                .SameFor <EPExpenseClaimDetails.paidWith>();

                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.curyTipAmt>(e.Cache, row, notMatchedToBankTran &&
                                                                                 enabledEditReceipt &&
                                                                                 row.PaidWith != EPExpenseClaimDetails.paidWith.CardPersonalExpense);

                var corpCardIsEnabled = row.PaidWith != EPExpenseClaimDetails.paidWith.PersonalAccount && notMatchedToBankTran && row.Released != true;
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.corpCardID>(e.Cache, row, corpCardIsEnabled);

                bool isProjectEnabled = row.PaidWith != EPExpenseClaimDetails.paidWith.CardPersonalExpense && row.Released != true;
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.contractID>(e.Cache, row, isProjectEnabled);
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.taskID>(e.Cache, row, isProjectEnabled);
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.costCodeID>(e.Cache, row, isProjectEnabled);

                UIState.RaiseOrHideError <EPExpenseClaimDetails.claimDetailCD>(e.Cache, row, row.BankTranDate != null,
                                                                               Messages.SomeBoxesAndActionsOnTheFormAreUnavailableExpenseReceiptIsMatchedToBankStatement, PXErrorLevel.Warning,
                                                                               row.BankTranDate?.ToShortDateString());
            }
        }
        public virtual void RefNbrUpdated(PXCache receiptsCache, EPExpenseClaim claim, EPExpenseClaimDetails receipt, string oldRefNbr)
        {
            SetClaimCuryWhenNotInClaim(receipt, claim?.RefNbr, receipt.CorpCardID);

            if (claim != null)
            {
                receipt.ClaimCuryInfoID = claim?.CuryInfoID;
                receipt.SubmitedDate    = DateTime.Now;

                RecalcAmountInClaimCury(receipt);

                if (receipt.IsPaidWithCard && receipt.CorpCardID != null)
                {
                    SumClaimValues(claim, receipt, null);
                }
            }
            else
            {
                EPExpenseClaimDetails oldReceipt = (EPExpenseClaimDetails)receiptsCache.CreateCopy(receipt);

                receipt.SubmitedDate = null;

                RecalcAmountInClaimCury(receipt);

                if (oldRefNbr != null)
                {
                    EPExpenseClaim oldClaim = GetParentClaim(oldRefNbr);

                    if (oldClaim != null)
                    {
                        SumClaimValues(oldClaim, null, oldReceipt);
                    }
                }
            }
            foreach (EPTaxTran copy in PXSelect <EPTaxTran,
                                                 Where <EPTaxTran.claimDetailID, Equal <Required <EPExpenseClaimDetails.claimDetailID> > > > .Select(receiptsCache.Graph, receipt.ClaimDetailID))
            {
                copy.RefNbr = receipt.RefNbr;
                receiptsCache.Graph.Caches[typeof(EPTaxTran)].Update(copy);
            }
        }
        public virtual void _(Events.FieldDefaulting <EPExpenseClaimDetails.corpCardID> e)
        {
            if (e.Row is EPExpenseClaimDetails row)
            {
                if (row.EmployeeID != null)
                {
                    if (row.PaidWith != EPExpenseClaimDetails.paidWith.PersonalAccount)
                    {
                        var firstCreditCard = GetFirstCreditCardForEmployeeAlphabeticallySorted(row.EmployeeID.Value);
                        var thereIsCreditCardForEmployee = firstCreditCard != null;
                        if (thereIsCreditCardForEmployee)
                        {
                            var lastUsedCreditCard = GetLastUsedCreditCardForEmployee(row.EmployeeID.Value);

                            var corpCardId = lastUsedCreditCard != null
                                                                ? lastUsedCreditCard.CorpCardID
                                                                : firstCreditCard.CorpCardID;

                            if (row.RefNbr != null)
                            {
                                EPExpenseClaim claim = PXParentAttribute.SelectParent <EPExpenseClaim>(e.Cache, row);

                                CashAccount cashAccount = CACorpCardsMaint.GetCardCashAccount(Base, corpCardId);

                                if (cashAccount.CuryID != claim.CuryID)
                                {
                                    corpCardId = null;
                                }
                            }

                            e.Cache.SetValueExt <EPExpenseClaimDetails.corpCardID>(row, corpCardId);
                        }
                    }
                }
            }
        }
        public virtual void RecalcAmountInClaimCury(EPExpenseClaimDetails receipt)
        {
            if (receipt != null && receipt.TranAmt != null && receipt.TranAmtWithTaxes != null)
            {
                decimal curyClaim           = 0m;
                decimal curyClaimWithTaxes  = 0m;
                decimal curyClaimTax        = 0m;
                decimal curyTaxRoundDiff    = 0m;
                decimal curyVatExemptTotal  = 0m;
                decimal curyVatTaxableTotal = 0m;

                if (!receipt.IsPaidWithCard || receipt.IsPaidWithCard && receipt.CorpCardID != null)
                {
                    PXCache      taxTranCache    = Base.Caches[typeof(EPTaxTran)];
                    CurrencyInfo expenseCuriInfo = PXSelect <CurrencyInfo,
                                                             Where <CurrencyInfo.curyInfoID, Equal <Required <EPExpenseClaimDetails.curyInfoID> > > > .SelectSingleBound(Base, null, receipt.CuryInfoID);

                    CurrencyInfo currencyinfo = PXSelect <CurrencyInfo,
                                                          Where <CurrencyInfo.curyInfoID, Equal <Required <EPExpenseClaimDetails.curyInfoID> > > > .SelectSingleBound(Base, null, receipt.ClaimCuryInfoID);

                    if (CurrencyHelper.IsSameCury(receipt.CuryInfoID, receipt.ClaimCuryInfoID, expenseCuriInfo, currencyinfo))
                    {
                        curyClaim           = receipt.CuryTranAmt ?? 0m;
                        curyClaimWithTaxes  = receipt.CuryTranAmtWithTaxes ?? 0m;
                        curyClaimTax        = receipt.CuryTaxTotal ?? 0m;
                        curyTaxRoundDiff    = receipt.CuryTaxRoundDiff ?? 0m;
                        curyVatExemptTotal  = receipt.CuryVatExemptTotal ?? 0m;
                        curyVatTaxableTotal = receipt.CuryVatTaxableTotal ?? 0m;
                        foreach (EPTaxTran copy in PXSelect <EPTaxTran,
                                                             Where <EPTaxTran.claimDetailID, Equal <Required <EPExpenseClaimDetails.claimDetailID> > > > .Select(Base, receipt.ClaimDetailID))
                        {
                            if (taxTranCache.GetStatus(copy) != PXEntryStatus.Inserted)
                            {
                                taxTranCache.SetStatus(copy, PXEntryStatus.Updated);
                            }
                            taxTranCache.SetValue <EPTaxTran.claimCuryExpenseAmt>(copy, copy.CuryExpenseAmt ?? 0m);
                            taxTranCache.SetValue <EPTaxTran.claimCuryTaxableAmt>(copy, copy.CuryTaxableAmt ?? 0m);
                            taxTranCache.SetValue <EPTaxTran.claimCuryTaxAmt>(copy, copy.CuryTaxAmt ?? 0m);
                        }
                    }
                    else if (currencyinfo?.CuryRate != null)
                    {
                        PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(Receipts.Cache, receipt, receipt.TranAmt ?? 0m, out curyClaim);
                        PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(Receipts.Cache, receipt, receipt.TranAmtWithTaxes ?? 0m, out curyClaimWithTaxes);
                        PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(Receipts.Cache, receipt, receipt.TaxTotal ?? 0m, out curyClaimTax);
                        PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(Receipts.Cache, receipt, receipt.TaxRoundDiff ?? 0m, out curyTaxRoundDiff);
                        PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(Receipts.Cache, receipt, receipt.VatExemptTotal ?? 0m, out curyVatExemptTotal);
                        PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(Receipts.Cache, receipt, receipt.VatTaxableTotal ?? 0m, out curyVatTaxableTotal);
                        foreach (EPTaxTran copy in PXSelect <EPTaxTran,
                                                             Where <EPTaxTran.claimDetailID, Equal <Required <EPExpenseClaimDetails.claimDetailID> > > > .Select(Base, receipt.ClaimDetailID))
                        {
                            if (taxTranCache.GetStatus(copy) != PXEntryStatus.Inserted)
                            {
                                taxTranCache.SetStatus(copy, PXEntryStatus.Updated);
                            }
                            decimal newValue;
                            PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(Receipts.Cache, receipt, copy.ExpenseAmt ?? 0m, out newValue);
                            taxTranCache.SetValue <EPTaxTran.claimCuryExpenseAmt>(copy, newValue);

                            PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(Receipts.Cache, receipt, copy.TaxableAmt ?? 0m, out newValue);
                            taxTranCache.SetValue <EPTaxTran.claimCuryTaxableAmt>(copy, newValue);

                            PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(Receipts.Cache, receipt, copy.TaxAmt ?? 0m, out newValue);
                            taxTranCache.SetValue <EPTaxTran.claimCuryTaxAmt>(copy, newValue);
                        }
                    }
                }

                EPExpenseClaimDetails oldReceipt = (EPExpenseClaimDetails)Receipts.Cache.CreateCopy(receipt);

                receipt.ClaimCuryTranAmt          = curyClaim;
                receipt.ClaimCuryTranAmtWithTaxes = curyClaimWithTaxes;
                receipt.ClaimCuryTaxTotal         = curyClaimTax;
                receipt.ClaimCuryTaxRoundDiff     = curyTaxRoundDiff;
                receipt.ClaimCuryVatExemptTotal   = curyVatExemptTotal;
                receipt.ClaimCuryVatTaxableTotal  = curyVatTaxableTotal;

                if (Claim != null && receipt.RefNbr != null)
                {
                    EPExpenseClaim claim = GetParentClaim(receipt.RefNbr);

                    SumClaimValues(claim, receipt, oldReceipt);
                }
            }
        }
Пример #21
0
        public virtual void Bill(CustomersList customer, PX.Objects.EP.EPCustomerBilling.BillingFilter filter)
        {
            ARInvoiceEntry arGraph = PXGraph.CreateInstance <ARInvoiceEntry>();
            RegisterEntry  pmGraph = PXGraph.CreateInstance <RegisterEntry>();

            arGraph.Clear();
            pmGraph.Clear();

            PMRegister pmDoc = null;
            ARInvoice  arDoc = null;

            List <ARRegister>            doclist = new List <ARRegister>();
            List <EPExpenseClaimDetails> listOfDirectBilledClaims = new List <EPExpenseClaimDetails>();

            PXSelectBase <EPExpenseClaimDetails> select = new PXSelectJoin <EPExpenseClaimDetails,
                                                                            InnerJoin <EPExpenseClaim, On <EPExpenseClaimDetails.refNbr, Equal <EPExpenseClaim.refNbr> > >,
                                                                            Where <EPExpenseClaimDetails.released, Equal <boolTrue>,
                                                                                   And <EPExpenseClaimDetails.billable, Equal <boolTrue>,
                                                                                        And <EPExpenseClaimDetails.billed, Equal <boolFalse>,
                                                                                             And <EPExpenseClaimDetails.customerID, Equal <Required <EPExpenseClaimDetails.customerID> >,
                                                                                                  And <EPExpenseClaimDetails.customerLocationID, Equal <Required <EPExpenseClaimDetails.customerLocationID> >,
                                                                                                       And <EPExpenseClaimDetails.expenseDate, LessEqual <Required <EPExpenseClaimDetails.expenseDate> > > > > > > >,
                                                                            OrderBy <Asc <EPExpenseClaimDetails.customerID, Asc <EPExpenseClaimDetails.customerLocationID> > > >(this);

            foreach (PXResult <EPExpenseClaimDetails, EPExpenseClaim> res in select.Select(customer.CustomerID, customer.LocationID, filter.EndDate))
            {
                EPExpenseClaimDetails row = (EPExpenseClaimDetails)res;
                EPExpenseClaim        doc = (EPExpenseClaim)res;

                if (row.ContractID != null && !ProjectDefaultAttribute.IsNonProject(this, row.ContractID))
                {
                    if (pmDoc == null)
                    {
                        pmDoc             = (PMRegister)pmGraph.Document.Cache.Insert();
                        pmDoc.OrigDocType = PMOrigDocType.ExpenseClaim;
                        pmDoc.OrigDocNbr  = doc.RefNbr;
                    }

                    InsertPMTran(pmGraph, row, doc, Setup.Current.CopyNotesPM == true, Setup.Current.CopyFilesPM == true);
                }
                else
                {
                    if (arDoc == null ||
                        arDoc.CustomerID != row.CustomerID ||
                        arDoc.CustomerLocationID != row.CustomerLocationID)
                    {
                        if (arDoc != null)
                        {
                            arDoc.CuryOrigDocAmt = arDoc.CuryDocBal;
                            arGraph.Document.Update(arDoc);
                            arGraph.Save.Press();
                        }
                        arDoc                    = (ARInvoice)arGraph.Document.Cache.Insert();
                        arDoc.DocType            = AR.ARDocType.Invoice;
                        arDoc.CustomerID         = row.CustomerID;
                        arDoc.CustomerLocationID = row.CustomerLocationID;
                        arDoc                    = arGraph.Document.Update(arDoc);
                        arGraph.Document.Cache.RaiseFieldUpdated <AR.ARInvoice.customerID>(arDoc, null);
                        if (Setup.Current.AutomaticReleaseAR == true)
                        {
                            arDoc.Hold = false;
                        }
                        doclist.Add(arDoc);
                    }

                    //insert ARTran
                    InsertARTran(arGraph, row, Setup.Current.CopyNotesAR == true, Setup.Current.CopyFilesAR == true);
                    listOfDirectBilledClaims.Add(row);
                }

                row.Billed = true;
                Transactions.Update(row);
            }

            if (arDoc != null)
            {
                arDoc.CuryOrigDocAmt = arDoc.CuryDocBal;
                arGraph.Document.Update(arDoc);

                arGraph.RowPersisted.AddHandler <ARInvoice>(
                    delegate(PXCache sender, PXRowPersistedEventArgs e)
                {
                    if (e.TranStatus == PXTranStatus.Open)
                    {
                        foreach (EPExpenseClaimDetails claimdetail in listOfDirectBilledClaims)
                        {
                            EPExpenseClaimDetails row = Transactions.Locate(claimdetail);

                            row.ARDocType = ((ARInvoice)e.Row).DocType;
                            row.ARRefNbr  = ((ARInvoice)e.Row).RefNbr;
                        }
                    }
                });

                arGraph.Save.Press();
            }
            if (pmDoc != null)
            {
                pmGraph.Save.Press();
            }

            this.Persist(typeof(EPExpenseClaimDetails), PXDBOperation.Update);

            if (Setup.Current.AutomaticReleaseAR == true)
            {
                ARDocumentRelease.ReleaseDoc(doclist, false);
            }
        }
Пример #22
0
        public virtual void ReleaseDocProc(EPExpenseClaim claim)
        {
            ExpenseClaimEntry expenseClaimGraph = PXGraph.CreateInstance <ExpenseClaimEntry>();

            EPExpenseClaim checkClaim = PXSelectReadonly <EPExpenseClaim, Where <EPExpenseClaim.refNbr, Equal <Required <EPExpenseClaim.refNbr> > > > .Select(expenseClaimGraph, claim.RefNbr);

            if (checkClaim.Released == true)
            {
                throw new PXException(Messages.AlreadyReleased);
            }

            var receipts = PXSelect <EPExpenseClaimDetails,
                                     Where <EPExpenseClaimDetails.refNbr, Equal <Required <EPExpenseClaim.refNbr> >,
                                            And <EPExpenseClaimDetails.released, Equal <False> > > >
                           .Select(expenseClaimGraph, claim.RefNbr)
                           .RowCast <EPExpenseClaimDetails>()
                           .ToArray();

            IFinPeriodUtils finPeriodUtils = expenseClaimGraph.GetService <IFinPeriodUtils>();

            if (claim.FinPeriodID != null)
            {
                finPeriodUtils.ValidateFinPeriod <EPExpenseClaimDetails>(receipts, m => claim.FinPeriodID, m => m.BranchID.SingleToArray());
            }

            List <APRegister> apDocs = new List <APRegister>();

            using (var ts = new PXTransactionScope())
            {
                if (receipts.Any())
                {
                    var receiptsByPaidWithType = receipts.GroupBy(receipt => receipt.PaidWith);

                    foreach (var receiptGroup in receiptsByPaidWithType)
                    {
                        List <APRegister> res = null;

                        if (receiptGroup.Key == EPExpenseClaimDetails.paidWith.PersonalAccount)
                        {
                            res = ReleaseClaimDetails <
                                Invoice, InvoiceMapping, APInvoiceEntry, APInvoiceEntry.APInvoiceEntryDocumentExtension>(
                                expenseClaimGraph, claim, receiptGroup, receiptGroup.Key);
                        }
                        else if (receiptGroup.Key == EPExpenseClaimDetails.paidWith.CardCompanyExpense)
                        {
                            res = ReleaseClaimDetails <
                                PaidInvoice, PaidInvoiceMapping, APQuickCheckEntry, APQuickCheckEntry.APQuickCheckEntryDocumentExtension>(
                                expenseClaimGraph, claim, receiptGroup, receiptGroup.Key);
                        }
                        else if (receiptGroup.Key == EPExpenseClaimDetails.paidWith.CardPersonalExpense)
                        {
                            res = ReleaseClaimDetails <
                                Invoice, InvoiceMapping, APInvoiceEntry, APInvoiceEntry.APInvoiceEntryDocumentExtension>(
                                expenseClaimGraph, claim, receiptGroup, receiptGroup.Key);
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }

                        apDocs.AddRange(res);
                    }
                }
                else
                {
                    apDocs = ReleaseClaimDetails <Invoice, InvoiceMapping, APInvoiceEntry, APInvoiceEntry.APInvoiceEntryDocumentExtension>(
                        expenseClaimGraph, claim, new EPExpenseClaimDetails[0], EPExpenseClaimDetails.paidWith.PersonalAccount);
                }

                ts.Complete();
            }

            EPSetup epsetup = PXSelectReadonly <EPSetup> .Select(this);

            if (epsetup.AutomaticReleaseAP == true)
            {
                APDocumentRelease.ReleaseDoc(apDocs, false);
            }
        }
Пример #23
0
        public virtual List <APRegister> ReleaseClaimDetails
        <TAPDocument, TInvoiceMapping, TGraph, TAPDocumentGraphExtension>
            (ExpenseClaimEntry expenseClaimGraph, EPExpenseClaim claim, IEnumerable <EPExpenseClaimDetails> receipts, string receiptGroupPaidWithType)
            where TGraph : PXGraph, new()
            where TAPDocument : InvoiceBase, new()
            where TInvoiceMapping : IBqlMapping
            where TAPDocumentGraphExtension : PX.Objects.Common.GraphExtensions.Abstract.InvoiceBaseGraphExtension <TGraph, TAPDocument, TInvoiceMapping>
        {
            #region prepare required variable

            var docgraph = PXGraph.CreateInstance <TGraph>();

            EPSetup epsetup = PXSelectReadonly <EPSetup> .Select(docgraph);

            TAPDocumentGraphExtension apDocumentGraphExtension = docgraph.FindImplementation <TAPDocumentGraphExtension>();

            List <List <EPExpenseClaimDetails> > receiptsForDocument = new List <List <EPExpenseClaimDetails> >();

            if (receiptGroupPaidWithType == EPExpenseClaimDetails.paidWith.PersonalAccount)
            {
                receiptsForDocument = receipts.GroupBy(item => new { item.TaxZoneID, item.TaxCalcMode })
                                      .Select(group => group.ToList())
                                      .ToList();
            }
            else if (receiptGroupPaidWithType == EPExpenseClaimDetails.paidWith.CardCompanyExpense)
            {
                if (epsetup.PostSummarizedCorpCardExpenseReceipts == true)
                {
                    receiptsForDocument = receipts.GroupBy(item =>
                                                           new
                    {
                        item.TaxZoneID,
                        item.TaxCalcMode,
                        item.CorpCardID,
                        item.ExpenseDate,
                        item.ExpenseRefNbr
                    })
                                          .Select(group => group.ToList())
                                          .ToList();
                }
                else
                {
                    receiptsForDocument = receipts.Select(receipt => receipt.SingleToList()).ToList();
                }
            }
            else if (receiptGroupPaidWithType == EPExpenseClaimDetails.paidWith.CardPersonalExpense)
            {
                receiptsForDocument = new List <List <EPExpenseClaimDetails> >()
                {
                    receipts.ToList()
                };
            }
            else
            {
                throw new NotImplementedException();
            }

            if (!receiptsForDocument.Any())
            {
                receiptsForDocument.Add(new List <EPExpenseClaimDetails>());
            }

            APSetup apsetup = PXSelectReadonly <APSetup> .Select(docgraph);

            EPEmployee employee = PXSelect <EPEmployee, Where <EPEmployee.bAccountID, Equal <Required <EPExpenseClaim.employeeID> > > > .Select(docgraph, claim.EmployeeID);

            Location emplocation = PXSelect <Location, Where <Location.bAccountID, Equal <Required <EPExpenseClaim.employeeID> >, And <Location.locationID, Equal <Required <EPExpenseClaim.locationID> > > > > .Select(docgraph, claim.EmployeeID, claim.LocationID);

            List <APRegister> doclist = new List <APRegister>();
            expenseClaimGraph.SelectTimeStamp();


            if (claim.FinPeriodID != null)
            {
                FinPeriodUtils.ValidateFinPeriod(claim.SingleToArray());
            }
            #endregion

            foreach (var receiptGroup in receiptsForDocument)
            {
                if (receiptGroupPaidWithType == EPExpenseClaimDetails.paidWith.CardCompanyExpense &&
                    receiptGroup.Count > 1)
                {
                    EPExpenseClaimDetails[] matchedReceipts = receiptGroup.Where(receipt => receipt.BankTranDate != null).Take(11).ToArray();

                    if (matchedReceipts.Any())
                    {
                        PXResult <EPExpenseClaimDetails, CABankTranMatch, CABankTran>[] rows =
                            PXSelectJoin <EPExpenseClaimDetails,
                                          InnerJoin <CABankTranMatch,
                                                     On <CABankTranMatch.docModule, Equal <BatchModule.moduleEP>,
                                                         And <CABankTranMatch.docType, Equal <EPExpenseClaimDetails.docType>,
                                                              And <CABankTranMatch.docRefNbr, Equal <EPExpenseClaimDetails.claimDetailCD> > > >,
                                                     InnerJoin <CABankTran,
                                                                On <CABankTran.tranID, Equal <CABankTranMatch.tranID> > > >,
                                          Where <EPExpenseClaimDetails.claimDetailCD, In <Required <EPExpenseClaimDetails.claimDetailCD> > > >
                            .Select(expenseClaimGraph, matchedReceipts.Select(receipt => receipt.ClaimDetailCD).ToArray())
                            .Cast <PXResult <EPExpenseClaimDetails, CABankTranMatch, CABankTran> >()
                            .ToArray();

                        throw new PXException(Messages.ExpenseReceiptCannotBeSummarized,
                                              rows.Select(row => String.Concat(PXMessages.LocalizeNoPrefix(Messages.Receipt),
                                                                               " ",
                                                                               ((EPExpenseClaimDetails)row).ClaimDetailCD,
                                                                               " - ",
                                                                               ((CABankTran)row).GetFriendlyKeyImage(Caches[typeof(CABankTran)])))
                                              .ToArray()
                                              .JoinIntoStringForMessageNoQuotes(maxCount: 10));
                    }
                }

                docgraph.Clear(PXClearOption.ClearAll);
                docgraph.SelectTimeStamp();
                apDocumentGraphExtension.Contragent.Current = apDocumentGraphExtension.Contragent.Cache.GetExtension <Contragent>(employee);
                apDocumentGraphExtension.Location.Current   = emplocation;

                CurrencyInfo infoOriginal = PXSelect <CurrencyInfo,
                                                      Where <CurrencyInfo.curyInfoID, Equal <Required <EPExpenseClaim.curyInfoID> > > > .Select(docgraph, claim.CuryInfoID);

                CurrencyInfo info = PXCache <CurrencyInfo> .CreateCopy(infoOriginal);

                info.CuryInfoID = null;
                info            = apDocumentGraphExtension.CurrencyInfo.Insert(info);

                #region CreateInvoiceHeader

                var invoice = new TAPDocument();

                CABankTranMatch bankTranMatch = null;

                if (receiptGroupPaidWithType == EPExpenseClaimDetails.paidWith.PersonalAccount)
                {
                    invoice.DocType = receiptGroup.Sum(_ => _.ClaimCuryTranAmtWithTaxes) < 0
                        ? APInvoiceType.DebitAdj
                        : APInvoiceType.Invoice;
                }
                else if (receiptGroupPaidWithType == EPExpenseClaimDetails.paidWith.CardCompanyExpense)
                {
                    EPExpenseClaimDetails receipt = receiptGroup.First();

                    invoice.DocType = APDocType.QuickCheck;

                    CACorpCard card = CACorpCard.PKID.Find(this, receipt.CorpCardID);

                    PaymentMethodAccount paymentMethodAccount = PXSelect <PaymentMethodAccount,
                                                                          Where <PaymentMethodAccount.cashAccountID, Equal <Required <PaymentMethodAccount.cashAccountID> > > >
                                                                .Select(this, card.CashAccountID);

                    invoice.CashAccountID   = card.CashAccountID;
                    invoice.PaymentMethodID = paymentMethodAccount.PaymentMethodID;
                    invoice.ExtRefNbr       = receipt.ExpenseRefNbr;

                    if (receiptGroup.Count == 1)
                    {
                        bankTranMatch =
                            PXSelect <CABankTranMatch,
                                      Where <CABankTranMatch.docModule, Equal <BatchModule.moduleEP>,
                                             And <CABankTranMatch.docType, Equal <EPExpenseClaimDetails.docType>,
                                                  And <CABankTranMatch.docRefNbr, Equal <Required <CABankTranMatch.docRefNbr> > > > > >
                            .Select(expenseClaimGraph, receipt.ClaimDetailCD);

                        if (bankTranMatch != null)
                        {
                            CABankTran bankTran = CABankTran.PK.Find(expenseClaimGraph, bankTranMatch.TranID);

                            invoice.ClearDate = bankTran.ClearDate;
                            invoice.Cleared   = true;
                        }
                    }
                }
                else if (receiptGroupPaidWithType == EPExpenseClaimDetails.paidWith.CardPersonalExpense)
                {
                    invoice.DocType = APDocType.DebitAdj;
                }
                else
                {
                    throw new NotImplementedException();
                }

                invoice.CuryInfoID = info.CuryInfoID;

                invoice.Hold     = true;
                invoice.Released = false;
                invoice.Printed  = invoice.DocType == APDocType.QuickCheck;
                invoice.OpenDoc  = true;

                invoice.HeaderDocDate        = claim.DocDate;
                invoice.FinPeriodID          = claim.FinPeriodID;
                invoice.InvoiceNbr           = claim.RefNbr;
                invoice.DocDesc              = claim.DocDesc;
                invoice.ContragentID         = claim.EmployeeID;
                invoice.CuryID               = info.CuryID;
                invoice.ContragentLocationID = claim.LocationID;
                invoice.ModuleAccountID      = emplocation != null ? emplocation.APAccountID : null;
                invoice.ModuleSubID          = emplocation != null ? emplocation.APSubID : null;
                invoice.TaxCalcMode          = receiptGroup.Any() ? receiptGroup.First().TaxCalcMode: claim.TaxCalcMode;
                invoice.BranchID             = claim.BranchID;
                invoice.OrigModule           = BatchModule.EP;

                if (receiptGroupPaidWithType == EPExpenseClaimDetails.paidWith.CardCompanyExpense &&
                    receiptGroup.Count == 1)
                {
                    invoice.OrigDocType = EPExpenseClaimDetails.DocType;
                    invoice.OrigRefNbr  = receiptGroup.Single().ClaimDetailCD;
                }
                else
                {
                    invoice.OrigDocType = EPExpenseClaim.DocType;
                    invoice.OrigRefNbr  = claim.RefNbr;
                }

                bool reversedDocument = invoice.DocType == APInvoiceType.DebitAdj && receiptGroupPaidWithType == EPExpenseClaimDetails.paidWith.PersonalAccount;

                decimal signOperation = reversedDocument ? -1 : 1;

                invoice = apDocumentGraphExtension.Documents.Insert(invoice);
                (apDocumentGraphExtension.Documents.Cache as PXModelExtension <TAPDocument>)?.UpdateExtensionMapping(invoice, MappingSyncDirection.BaseToExtension);

                invoice.TaxZoneID = receiptGroup.Any() ? receiptGroup.First().TaxZoneID : claim.TaxZoneID;

                invoice = apDocumentGraphExtension.Documents.Update(invoice);

                PXCache <CurrencyInfo> .RestoreCopy(info, infoOriginal);

                info.CuryInfoID = invoice.CuryInfoID;

                PXCache claimcache       = docgraph.Caches[typeof(EPExpenseClaim)];
                PXCache claimdetailcache = docgraph.Caches[typeof(EPExpenseClaimDetails)];

                PXNoteAttribute.CopyNoteAndFiles(claimcache, claim, apDocumentGraphExtension.Documents.Cache, invoice, epsetup.GetCopyNoteSettings <PXModule.ap>());
                #endregion

                TaxAttribute.SetTaxCalc <InvoiceTran.taxCategoryID>(apDocumentGraphExtension.InvoiceTrans.Cache, null, TaxCalc.ManualCalc);

                decimal?claimCuryTaxRoundDiff = 0m;
                decimal?claimTaxRoundDiff     = 0m;
                foreach (EPExpenseClaimDetails claimdetail in receiptGroup)
                {
                    #region AddDetails

                    decimal tipQty;
                    if (reversedDocument == claimdetail.ClaimCuryTranAmtWithTaxes < 0)
                    {
                        tipQty = 1;
                    }
                    else
                    {
                        tipQty = -1;
                    }
                    Contract contract = PXSelect <Contract, Where <Contract.contractID, Equal <Required <EPExpenseClaimDetails.contractID> > > > .SelectSingleBound(docgraph, null, claimdetail.ContractID);

                    if (claimdetail.TaskID != null)
                    {
                        PMTask task = PXSelect <PMTask, Where <PMTask.taskID, Equal <Required <PMTask.taskID> > > > .Select(expenseClaimGraph, claimdetail.TaskID);

                        if (task != null && !(bool)task.VisibleInAP)
                        {
                            throw new PXException(PM.Messages.TaskInvisibleInModule, task.TaskCD, BatchModule.AP);
                        }
                    }

                    InvoiceTran tran = new InvoiceTran();
                    tran.InventoryID = claimdetail.InventoryID;
                    tran.TranDesc    = claimdetail.TranDesc;
                    decimal unitCost;
                    decimal amount;
                    decimal taxableAmt;
                    decimal taxAmt;
                    if (CurrencyHelper.IsSameCury(expenseClaimGraph, claimdetail.CuryInfoID, claimdetail.ClaimCuryInfoID))
                    {
                        unitCost   = claimdetail.CuryUnitCost ?? 0m;
                        amount     = claimdetail.CuryTaxableAmt ?? 0m;
                        taxableAmt = claimdetail.CuryTaxableAmtFromTax ?? 0m;
                        taxAmt     = claimdetail.CuryTaxAmt ?? 0m;
                    }
                    else
                    {
                        if (claimdetail.CuryUnitCost == null || claimdetail.CuryUnitCost == 0m)
                        {
                            unitCost = 0m;
                        }
                        else
                        {
                            PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(expenseClaimGraph.ExpenseClaimDetails.Cache, claimdetail, (decimal)claimdetail.UnitCost, out unitCost);
                        }
                        if (claimdetail.CuryTaxableAmt == null || claimdetail.CuryTaxableAmt == 0m)
                        {
                            amount = 0m;
                        }
                        else
                        {
                            PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(expenseClaimGraph.ExpenseClaimDetails.Cache, claimdetail, (decimal)claimdetail.TaxableAmt, out amount);
                        }
                        if (claimdetail.CuryTaxableAmtFromTax == null || claimdetail.CuryTaxableAmtFromTax == 0m)
                        {
                            taxableAmt = 0m;
                        }
                        else
                        {
                            PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(expenseClaimGraph.ExpenseClaimDetails.Cache, claimdetail, (decimal)claimdetail.TaxableAmtFromTax, out taxableAmt);
                        }
                        if (claimdetail.CuryTaxAmt == null || claimdetail.CuryTaxAmt == 0m)
                        {
                            taxAmt = 0m;
                        }
                        else
                        {
                            PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(expenseClaimGraph.ExpenseClaimDetails.Cache, claimdetail, (decimal)claimdetail.TaxAmt, out taxAmt);
                        }
                    }

                    tran.ManualPrice       = true;
                    tran.CuryUnitCost      = unitCost;
                    tran.Qty               = claimdetail.Qty * signOperation;
                    tran.UOM               = claimdetail.UOM;
                    tran.NonBillable       = claimdetail.Billable != true;
                    claimCuryTaxRoundDiff += (claimdetail.ClaimCuryTaxRoundDiff ?? 0m) * signOperation;
                    claimTaxRoundDiff     += (claimdetail.ClaimTaxRoundDiff ?? 0m) * signOperation;
                    tran.Date              = claimdetail.ExpenseDate;

                    if (contract.BaseType == CT.CTPRType.Project)
                    {
                        tran.ProjectID = claimdetail.ContractID;
                    }
                    else
                    {
                        tran.ProjectID = ProjectDefaultAttribute.NonProject();
                    }

                    tran.TaskID     = claimdetail.TaskID;
                    tran.CostCodeID = claimdetail.CostCodeID;

                    if (receiptGroupPaidWithType == EPExpenseClaimDetails.paidWith.CardPersonalExpense)
                    {
                        CACorpCard  card        = CACorpCard.PKID.Find(this, claimdetail.CorpCardID);
                        CashAccount cashAccount = CashAccount.PK.Find(this, card.CashAccountID);

                        tran.AccountID = cashAccount.AccountID;
                        tran.SubID     = cashAccount.SubID;
                    }
                    else
                    {
                        tran.AccountID = claimdetail.ExpenseAccountID;
                        tran.SubID     = claimdetail.ExpenseSubID;
                    }

                    tran.BranchID = claimdetail.BranchID;


                    tran = InsertInvoiceTransaction(apDocumentGraphExtension.InvoiceTrans.Cache, tran,
                                                    new InvoiceTranContext {
                        EPClaim = claim, EPClaimDetails = claimdetail
                    });

                    if (claimdetail.PaidWith == EPExpenseClaimDetails.paidWith.CardPersonalExpense)
                    {
                        claimdetail.APLineNbr = tran.LineNbr;
                    }

                    tran.CuryLineAmt    = amount * signOperation;
                    tran.CuryTaxAmt     = 0;
                    tran.CuryTaxableAmt = taxableAmt * signOperation;
                    tran.CuryTaxAmt     = taxAmt * signOperation;
                    tran.TaxCategoryID  = claimdetail.TaxCategoryID;


                    tran = UpdateInvoiceTransaction(apDocumentGraphExtension.InvoiceTrans.Cache, tran,
                                                    new InvoiceTranContext {
                        EPClaim = claim, EPClaimDetails = claimdetail
                    });


                    if ((claimdetail.CuryTipAmt ?? 0) != 0)
                    {
                        InvoiceTran tranTip = new InvoiceTran();
                        if (epsetup.NonTaxableTipItem == null)
                        {
                            throw new PXException(Messages.TipItemIsNotDefined);
                        }
                        IN.InventoryItem tipItem = PXSelect <IN.InventoryItem,
                                                             Where <IN.InventoryItem.inventoryID, Equal <Required <IN.InventoryItem.inventoryID> > > > .Select(docgraph, epsetup.NonTaxableTipItem);

                        if (tipItem == null)
                        {
                            string fieldname = PXUIFieldAttribute.GetDisplayName <EPSetup.nonTaxableTipItem>(docgraph.Caches[typeof(EPSetup)]);
                            throw new PXException(ErrorMessages.ValueDoesntExistOrNoRights, fieldname, epsetup.NonTaxableTipItem);
                        }
                        tranTip.InventoryID = epsetup.NonTaxableTipItem;
                        tranTip.TranDesc    = tipItem.Descr;
                        if (CurrencyHelper.IsSameCury(expenseClaimGraph, claimdetail.CuryInfoID, claimdetail.ClaimCuryInfoID))
                        {
                            tranTip.CuryUnitCost = Math.Abs(claimdetail.CuryTipAmt ?? 0m);
                            tranTip.CuryTranAmt  = claimdetail.CuryTipAmt * signOperation;
                        }
                        else
                        {
                            decimal tipAmt;
                            PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(expenseClaimGraph.ExpenseClaimDetails.Cache, claimdetail, (decimal)claimdetail.TipAmt, out tipAmt);
                            tranTip.CuryUnitCost = Math.Abs(tipAmt);
                            tranTip.CuryTranAmt  = tipAmt * signOperation;
                        }
                        tranTip.Qty         = tipQty;
                        tranTip.UOM         = tipItem.BaseUnit;
                        tranTip.NonBillable = claimdetail.Billable != true;
                        tranTip.Date        = claimdetail.ExpenseDate;

                        tranTip.BranchID = claimdetail.BranchID;

                        tranTip = InsertInvoiceTipTransaction(apDocumentGraphExtension.InvoiceTrans.Cache, tranTip,
                                                              new InvoiceTranContext {
                            EPClaim = claim, EPClaimDetails = claimdetail
                        });


                        if (epsetup.UseReceiptAccountForTips == true)
                        {
                            tranTip.AccountID = claimdetail.ExpenseAccountID;
                            tranTip.SubID     = claimdetail.ExpenseSubID;
                        }
                        else
                        {
                            tranTip.AccountID = tipItem.COGSAcctID;
                            Location companyloc = (Location)PXSelectJoin <Location,
                                                                          InnerJoin <BAccountR, On <Location.bAccountID, Equal <BAccountR.bAccountID>,
                                                                                                    And <Location.locationID, Equal <BAccountR.defLocationID> > >,
                                                                                     InnerJoin <GL.Branch, On <BAccountR.bAccountID, Equal <GL.Branch.bAccountID> > > >,
                                                                          Where <GL.Branch.branchID, Equal <Current <APInvoice.branchID> > > > .Select(docgraph);

                            PMTask task = PXSelect <PMTask,
                                                    Where <PMTask.projectID, Equal <Required <PMTask.projectID> >,
                                                           And <PMTask.taskID, Equal <Required <PMTask.taskID> > > > > .Select(docgraph, claimdetail.ContractID, claimdetail.TaskID);

                            Location customerLocation = (Location)PXSelectorAttribute.Select <EPExpenseClaimDetails.customerLocationID>(claimdetailcache, claimdetail);

                            int?employee_SubID = (int?)docgraph.Caches[typeof(EPEmployee)].GetValue <EPEmployee.expenseSubID>(employee);
                            int?item_SubID     = (int?)docgraph.Caches[typeof(IN.InventoryItem)].GetValue <IN.InventoryItem.cOGSSubID>(tipItem);
                            int?company_SubID  = (int?)docgraph.Caches[typeof(Location)].GetValue <Location.cMPExpenseSubID>(companyloc);
                            int?project_SubID  = (int?)docgraph.Caches[typeof(Contract)].GetValue <Contract.defaultSubID>(contract);
                            int?task_SubID     = (int?)docgraph.Caches[typeof(PMTask)].GetValue <PMTask.defaultSubID>(task);
                            int?location_SubID = (int?)docgraph.Caches[typeof(Location)].GetValue <Location.cSalesSubID>(customerLocation);

                            object value = SubAccountMaskAttribute.MakeSub <EPSetup.expenseSubMask>(docgraph, epsetup.ExpenseSubMask,
                                                                                                    new object[] { employee_SubID, item_SubID, company_SubID, project_SubID, task_SubID, location_SubID },
                                                                                                    new Type[] { typeof(EPEmployee.expenseSubID), typeof(IN.InventoryItem.cOGSSubID), typeof(Location.cMPExpenseSubID), typeof(Contract.defaultSubID), typeof(PMTask.defaultSubID), typeof(Location.cSalesSubID) });

                            docgraph.Caches[typeof(APTran)].RaiseFieldUpdating <APTran.subID>(tranTip, ref value);
                            tranTip.SubID = (int?)value;
                        }

                        tranTip = UpdateInvoiceTipTransactionAccounts(apDocumentGraphExtension.InvoiceTrans.Cache, tranTip,
                                                                      new InvoiceTranContext {
                            EPClaim = claim, EPClaimDetails = claimdetail
                        });

                        tranTip.TaxCategoryID = tipItem.TaxCategoryID;
                        tranTip.ProjectID     = tran.ProjectID;
                        tranTip.TaskID        = tran.TaskID;
                        tranTip = AddTaxes <TAPDocument, TInvoiceMapping, TGraph, TAPDocumentGraphExtension>(apDocumentGraphExtension, docgraph, expenseClaimGraph, invoice, signOperation, claimdetail, tranTip, true);


                        tranTip = UpdateInvoiceTipTransactionTaxesAndProject(apDocumentGraphExtension.InvoiceTrans.Cache, tranTip,
                                                                             new InvoiceTranContext {
                            EPClaim = claim, EPClaimDetails = claimdetail
                        });
                    }

                    PXNoteAttribute.CopyNoteAndFiles(claimdetailcache, claimdetail, apDocumentGraphExtension.InvoiceTrans.Cache, tran, epsetup.GetCopyNoteSettings <PXModule.ap>());
                    claimdetail.Released = true;
                    expenseClaimGraph.ExpenseClaimDetails.Update(claimdetail);
                    #endregion

                    if (receiptGroupPaidWithType != EPExpenseClaimDetails.paidWith.CardPersonalExpense)
                    {
                        tran = AddTaxes <TAPDocument, TInvoiceMapping, TGraph, TAPDocumentGraphExtension>(apDocumentGraphExtension, docgraph, expenseClaimGraph, invoice, signOperation, claimdetail, tran, false);
                    }
                }

                #region legacy taxes
                foreach (EPTaxAggregate tax in PXSelectReadonly <EPTaxAggregate,
                                                                 Where <EPTaxAggregate.refNbr, Equal <Required <EPExpenseClaim.refNbr> > > > .Select(docgraph, claim.RefNbr))
                {
                    #region Add taxes
                    GenericTaxTran new_aptax = apDocumentGraphExtension.TaxTrans.Search <GenericTaxTran.taxID>(tax.TaxID);

                    if (new_aptax == null)
                    {
                        new_aptax       = new GenericTaxTran();
                        new_aptax.TaxID = tax.TaxID;
                        new_aptax       = apDocumentGraphExtension.TaxTrans.Insert(new_aptax);
                        if (new_aptax != null)
                        {
                            new_aptax = (GenericTaxTran)apDocumentGraphExtension.TaxTrans.Cache.CreateCopy(new_aptax);
                            new_aptax.CuryTaxableAmt = 0m;
                            new_aptax.CuryTaxAmt     = 0m;
                            new_aptax.CuryExpenseAmt = 0m;
                            new_aptax = apDocumentGraphExtension.TaxTrans.Update(new_aptax);
                        }
                    }

                    if (new_aptax != null)
                    {
                        new_aptax                = (GenericTaxTran)apDocumentGraphExtension.TaxTrans.Cache.CreateCopy(new_aptax);
                        new_aptax.TaxRate        = tax.TaxRate;
                        new_aptax.CuryTaxableAmt = (new_aptax.CuryTaxableAmt ?? 0m) + tax.CuryTaxableAmt * signOperation;
                        new_aptax.CuryTaxAmt     = (new_aptax.CuryTaxAmt ?? 0m) + tax.CuryTaxAmt * signOperation;
                        new_aptax.CuryExpenseAmt = (new_aptax.CuryExpenseAmt ?? 0m) + tax.CuryExpenseAmt * signOperation;
                        new_aptax                = apDocumentGraphExtension.TaxTrans.Update(new_aptax);
                    }
                    #endregion
                }
                #endregion

                invoice.CuryOrigDocAmt = invoice.CuryDocBal;
                invoice.CuryTaxAmt     = invoice.CuryTaxTotal;
                invoice.Hold           = false;
                apDocumentGraphExtension.SuppressApproval();
                apDocumentGraphExtension.Documents.Update(invoice);

                if (receiptGroupPaidWithType != EPExpenseClaimDetails.paidWith.CardPersonalExpense)
                {
                    invoice.CuryTaxRoundDiff = invoice.CuryRoundDiff = invoice.CuryRoundDiff = claimCuryTaxRoundDiff;
                    invoice.TaxRoundDiff     = invoice.RoundDiff = claimTaxRoundDiff;
                    bool inclusive = PXSelectJoin <APTaxTran, InnerJoin <Tax,
                                                                         On <APTaxTran.taxID, Equal <Tax.taxID> > >,
                                                   Where <APTaxTran.refNbr, Equal <Required <APInvoice.refNbr> >,
                                                          And <APTaxTran.tranType, Equal <Required <APInvoice.docType> >,
                                                               And <Tax.taxCalcLevel, Equal <CSTaxCalcLevel.inclusive> > > > >
                                     .Select(docgraph, invoice.RefNbr, invoice.DocType).Count > 0;

                    if ((invoice.TaxCalcMode == TaxCalculationMode.Gross &&
                         PXSelectJoin <APTaxTran, InnerJoin <Tax,
                                                             On <APTaxTran.taxID, Equal <Tax.taxID> > >,
                                       Where <APTaxTran.refNbr, Equal <Required <APInvoice.refNbr> >,
                                              And <APTaxTran.tranType, Equal <Required <APInvoice.docType> >,
                                                   And <Tax.taxCalcLevel, Equal <CSTaxCalcLevel.calcOnItemAmt> > > > >
                         .Select(docgraph, invoice.RefNbr, invoice.DocType).Count > 0) ||
                        inclusive)
                    {
                        decimal curyAdditionalDiff = -(invoice.CuryTaxRoundDiff ?? 0m) + (invoice.CuryTaxAmt ?? 0m) - (invoice.CuryDocBal ?? 0m);
                        decimal additionalDiff     = -(invoice.TaxRoundDiff ?? 0m) + (invoice.TaxAmt ?? 0m) - (invoice.DocBal ?? 0m);
                        foreach (InvoiceTran line in apDocumentGraphExtension.InvoiceTrans.Select())
                        {
                            curyAdditionalDiff += (line.CuryTaxableAmt ?? 0m) == 0m ? (line.CuryTranAmt ?? 0m) : (line.CuryTaxableAmt ?? 0m);
                            additionalDiff     += (line.TaxableAmt ?? 0m) == 0m ? (line.TranAmt ?? 0m) : (line.TaxableAmt ?? 0m);
                        }

                        invoice.CuryTaxRoundDiff += curyAdditionalDiff;
                        invoice.TaxRoundDiff     += additionalDiff;
                    }
                }

                invoice = apDocumentGraphExtension.Documents.Update(invoice);
                docgraph.Actions.PressSave();

                if (receiptGroupPaidWithType == EPExpenseClaimDetails.paidWith.CardCompanyExpense &&
                    receiptGroup.Count == 1 &&
                    bankTranMatch != null)
                {
                    CABankTransactionsMaint.RematchFromExpenseReceipt(this, bankTranMatch, invoice.CATranID, invoice.ContragentID, receiptGroup.Single());
                }

                foreach (EPExpenseClaimDetails claimdetail in receiptGroup)
                {
                    claimdetail.APDocType = invoice.DocType;
                    claimdetail.APRefNbr  = invoice.RefNbr;
                    expenseClaimGraph.ExpenseClaimDetails.Update(claimdetail);
                }

                claim.Status   = EPExpenseClaimStatus.ReleasedStatus;
                claim.Released = true;

                expenseClaimGraph.ExpenseClaim.Update(claim);


                #region EP History Update
                EPHistory hist = new EPHistory();
                hist.EmployeeID  = invoice.ContragentID;
                hist.FinPeriodID = invoice.FinPeriodID;
                hist             = (EPHistory)expenseClaimGraph.Caches[typeof(EPHistory)].Insert(hist);

                hist.FinPtdClaimed += invoice.DocBal;
                hist.FinYtdClaimed += invoice.DocBal;
                if (invoice.FinPeriodID == invoice.HeaderTranPeriodID)
                {
                    hist.TranPtdClaimed += invoice.DocBal;
                    hist.TranYtdClaimed += invoice.DocBal;
                }
                else
                {
                    EPHistory tranhist = new EPHistory();
                    tranhist.EmployeeID      = invoice.ContragentID;
                    tranhist.FinPeriodID     = invoice.HeaderTranPeriodID;
                    tranhist                 = (EPHistory)expenseClaimGraph.Caches[typeof(EPHistory)].Insert(tranhist);
                    tranhist.TranPtdClaimed += invoice.DocBal;
                    tranhist.TranYtdClaimed += invoice.DocBal;
                }
                expenseClaimGraph.Views.Caches.Add(typeof(EPHistory));
                #endregion

                expenseClaimGraph.Save.Press();

                Actions.PressSave();

                doclist.Add((APRegister)apDocumentGraphExtension.Documents.Current.Base);
            }

            return(doclist);
        }
        private static void ClaimDetail(List <EPExpenseClaimDetails> details)
        {
            PXSetup <EPSetup> epsetup   = new PXSetup <EPSetup>(PXGraph.CreateInstance(typeof(ExpenseClaimDetailEntry)));
            bool enabledApprovalReceipt = PXAccess.FeatureInstalled <FeaturesSet.approvalWorkflow>() && epsetup.Current.ClaimDetailsAssignmentMapID != null;
            bool isError        = false;
            bool notAllApproved = false;
            Dictionary <string, EPExpenseClaim> result = new Dictionary <string, EPExpenseClaim>();

            IEnumerable <Receipts> List;

            if (epsetup.Current.AllowMixedTaxSettingInClaims == true)
            {
                List = details.Where(item => string.IsNullOrEmpty(item.RefNbr)).OrderBy(detail => detail.ClaimDetailID).GroupBy(
                    item => new
                {
                    item.EmployeeID,
                    item.BranchID,
                    item.CustomerID,
                    item.CustomerLocationID,
                },
                    (key, item) => new Receipts
                {
                    employee         = key.EmployeeID,
                    branch           = key.BranchID,
                    customer         = key.CustomerID,
                    customerLocation = key.CustomerLocationID,
                    details          = item
                }
                    );
            }
            else
            {
                List = details.Where(item => string.IsNullOrEmpty(item.RefNbr)).OrderBy(detail => detail.ClaimDetailID).GroupBy(
                    item => new
                {
                    item.EmployeeID,
                    item.BranchID,
                    item.CustomerID,
                    item.CustomerLocationID,
                    item.TaxZoneID,
                    item.TaxCalcMode
                },
                    (key, item) => new Receipts
                {
                    employee         = key.EmployeeID,
                    branch           = key.BranchID,
                    customer         = key.CustomerID,
                    customerLocation = key.CustomerLocationID,
                    details          = item
                }
                    );
            }

            foreach (Receipts item in List)
            {
                isError        = false;
                notAllApproved = false;
                using (PXTransactionScope ts = new PXTransactionScope())
                {
                    ExpenseClaimEntry expenseClaimEntry = CreateInstance <ExpenseClaimEntry>();
                    expenseClaimEntry.SelectTimeStamp();
                    EPExpenseClaim expenseClaim = (EPExpenseClaim)expenseClaimEntry.ExpenseClaim.Cache.CreateInstance();
                    expenseClaim.EmployeeID         = item.employee;
                    expenseClaim.BranchID           = item.branch;
                    expenseClaim.CustomerID         = item.customer;
                    expenseClaim.DocDesc            = EP.Messages.SubmittedReceipt;
                    expenseClaim                    = expenseClaimEntry.ExpenseClaim.Update(expenseClaim);
                    expenseClaim.CustomerLocationID = item.customerLocation;
                    expenseClaim.TaxCalcMode        = item.details.First().TaxCalcMode;
                    expenseClaim.TaxZoneID          = item.details.First().TaxZoneID;

                    foreach (EPExpenseClaimDetails detail in item.details)
                    {
                        PXProcessing <EPExpenseClaimDetails> .SetCurrentItem(detail);

                        if (detail.Approved ?? false)
                        {
                            try
                            {
                                if (detail.TipAmt != 0 && epsetup.Current.NonTaxableTipItem == null)
                                {
                                    PXProcessing <EPExpenseClaimDetails> .SetError(Messages.TipItemIsNotDefined);

                                    isError = true;
                                }
                                else
                                {
                                    EPClaimReceiptController.SubmitReceipt(expenseClaimEntry.ExpenseClaim.Cache,
                                                                           expenseClaimEntry.ExpenseClaimDetails.Cache, expenseClaimEntry.ExpenseClaim.Current, detail);

                                    PXProcessing <EPExpenseClaimDetails> .SetProcessed();
                                }


                                expenseClaimEntry.Save.Press();
                                if (!result.ContainsKey(expenseClaim.RefNbr))
                                {
                                    result.Add(expenseClaim.RefNbr, expenseClaim);
                                }
                                detail.RefNbr = expenseClaim.RefNbr;
                            }
                            catch (Exception ex)
                            {
                                PXProcessing <EPExpenseClaimDetails> .SetError(ex);

                                isError = true;
                            }
                        }
                        else
                        {
                            PXProcessing.SetError(enabledApprovalReceipt
                                                                ? Messages.ReceiptNotApproved
                                                                : Messages.ReceiptTakenOffHold);
                            notAllApproved = true;
                        }
                    }
                    if (!isError)
                    {
                        ts.Complete();
                    }
                }
            }



            if (!isError && !notAllApproved)
            {
                if (result.Count == 1)
                {
                    ExpenseClaimEntry expenseClaimEntry = CreateInstance <ExpenseClaimEntry>();
                    PXRedirectHelper.TryRedirect(expenseClaimEntry, result.First().Value, PXRedirectHelper.WindowMode.InlineWindow);
                }
            }
            else
            {
                PXProcessing <EPExpenseClaimDetails> .SetCurrentItem(null);

                throw new PXException(Messages.ErrorProcessingReceipts);
            }
        }
Пример #25
0
        protected virtual void InsertPMTran(RegisterEntry pmGraph, EPExpenseClaimDetails row, EPExpenseClaim doc, bool copyNotes, bool copyFiles)
        {
            PMProject project = PXSelect <PMProject, Where <PMProject.contractID, Equal <Required <PMProject.contractID> > > > .Select(pmGraph, row.ContractID);

            Account account = PXSelect <Account, Where <Account.accountID, Equal <Required <Account.accountID> > > > .Select(this, row.ExpenseAccountID);

            if (account.AccountGroupID == null && project.BaseType == PMProject.ProjectBaseType.Project)
            {
                throw new PXException(Messages.AccountGroupIsNotAssignedForAccount, account.AccountCD);
            }

            PMTran tran = (PMTran)pmGraph.Transactions.Cache.Insert();

            tran.AccountGroupID = account.AccountGroupID;
            tran.BAccountID     = row.CustomerID;
            tran.LocationID     = row.CustomerLocationID;
            tran.ProjectID      = row.ContractID;
            tran.TaskID         = row.TaskID;
            tran.InventoryID    = row.InventoryID;
            tran.Qty            = row.Qty;
            tran.Billable       = true;
            tran.BillableQty    = row.Qty;
            tran.UOM            = row.UOM;
            tran.UnitRate       = row.UnitCost;
            tran.Amount         = row.TranAmt;
            tran.AccountID      = row.ExpenseAccountID;
            tran.SubID          = row.ExpenseSubID;
            tran.StartDate      = row.ExpenseDate;
            tran.EndDate        = row.ExpenseDate;
            tran.Date           = row.ExpenseDate;
            tran.ResourceID     = doc.EmployeeID;
            tran.Released       = project.BaseType == PMProject.ContractBaseType.Contract;      //contract trans are created as released

            pmGraph.Transactions.Update(tran);

            pmGraph.Document.Current.Released = project.BaseType == PMProject.ContractBaseType.Contract;

            if (copyNotes)
            {
                string note = PXNoteAttribute.GetNote(Caches[typeof(EPExpenseClaimDetails)], row);
                if (note != null)
                {
                    PXNoteAttribute.SetNote(pmGraph.Transactions.Cache, tran, note);
                }
            }
            if (copyFiles)
            {
                Guid[] files = PXNoteAttribute.GetFileNotes(Caches[typeof(EPExpenseClaimDetails)], row);
                if (files != null && files.Length > 0)
                {
                    PXNoteAttribute.SetFileNotes(pmGraph.Transactions.Cache, tran, files);
                }
            }
        }
Пример #26
0
        protected virtual void EPExpenseClaimDetails_RowSelected(PXCache cache, PXRowSelectedEventArgs e)
        {
            EPExpenseClaimDetails row = (EPExpenseClaimDetails)e.Row;

            if (row != null)
            {
                EPExpenseClaim claim = (EPExpenseClaim)PXSelect <EPExpenseClaim, Where <EPExpenseClaim.refNbr, Equal <Required <EPExpenseClaimDetails.refNbr> > > > .SelectSingleBound(this, new object[] { null }, row.RefNbr);

                bool enabledApprovalReceipt   = PXAccess.FeatureInstalled <FeaturesSet.approvalWorkflow>() && epsetup.Current.ClaimDetailsAssignmentMapID != null;
                bool legacyClaim              = row.LegacyReceipt == true && !String.IsNullOrEmpty(row.RefNbr);
                bool enabledEditReceipt       = (row.Hold == true || !enabledApprovalReceipt) && !legacyClaim;
                bool enabledRefNbr            = true;
                bool enabledEmployeeAndBranch = enabledEditReceipt && !(row.ClaimCuryInfoID == null && cache.AllowUpdate && !string.IsNullOrEmpty(row.RefNbr));
                bool enabledFinancialDetails  = (row.Rejected != true) && (row.Released != true);
                bool NonProject    = (CurrentContract.SelectSingle()?.ContractCD ?? PMSetup.DefaultNonProjectCode).Trim() == PMSetup.DefaultNonProjectCode;
                bool claimReleased = false;
                if (claim != null)
                {
                    bool enabledEditClaim = (row.HoldClaim == true);
                    enabledEditReceipt       = enabledEditReceipt && enabledEditClaim;
                    enabledRefNbr            = enabledEditClaim;
                    enabledEmployeeAndBranch = false;
                    enabledFinancialDetails  = enabledFinancialDetails && enabledEditClaim;
                    claimReleased            = claim.Released == true;
                }
                enabledRefNbr = enabledRefNbr && row.LegacyReceipt == false;

                Approval.AllowSelect = enabledApprovalReceipt;
                Delete.SetEnabled(enabledEditReceipt && claim == null);
                PXUIFieldAttribute.SetEnabled(cache, row, enabledEditReceipt);
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.claimDetailID>(cache, row, true);
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.refNbr>(cache, row, enabledRefNbr);
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.employeeID>(cache, row, enabledEmployeeAndBranch);
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.branchID>(cache, row, enabledEmployeeAndBranch);
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.expenseAccountID>(cache, row, enabledFinancialDetails);
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.expenseSubID>(cache, row, enabledFinancialDetails);
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.salesAccountID>(cache, row, enabledFinancialDetails && (row.Billable == true));
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.salesSubID>(cache, row, enabledFinancialDetails && (row.Billable == true));
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.taxCategoryID>(cache, row, enabledFinancialDetails);
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.customerID>(cache, row, NonProject && !claimReleased);
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.customerLocationID>(cache, row, NonProject && !claimReleased);
                action.SetEnabled("Submit", cache.GetStatus(row) != PXEntryStatus.Inserted && row.Hold == true);

                if (row.ContractID != null && (bool)row.Billable && row.TaskID != null)
                {
                    PMTask task = PXSelect <PMTask, Where <PMTask.taskID, Equal <Required <PMTask.taskID> > > > .Select(this, row.TaskID);

                    if (task != null && !(bool)task.VisibleInAP)
                    {
                        cache.RaiseExceptionHandling <EPExpenseClaimDetails.taskID>(e.Row, task.TaskCD, new PXSetPropertyException(PM.Messages.TaskInvisibleInModule, task.TaskCD, GL.BatchModule.AP));
                    }
                }

                CurrencyInfo info = (CurrencyInfo)PXSelect <CurrencyInfo, Where <CurrencyInfo.curyInfoID, Equal <Current <EPExpenseClaimDetails.curyInfoID> > > > .SelectSingleBound(this, new object[] { row });

                if (info != null && info.CuryRateTypeID != null && info.CuryEffDate != null && row.ExpenseDate != null && info.CuryEffDate < row.ExpenseDate)
                {
                    CurrencyRateType ratetype = (CurrencyRateType)PXSelectorAttribute.Select <CurrencyInfo.curyRateTypeID>(currencyinfo.Cache, info);
                    if (ratetype != null && ratetype.RateEffDays > 0 &&
                        ((TimeSpan)(row.ExpenseDate - info.CuryEffDate)).Days > ratetype.RateEffDays)
                    {
                        PXRateIsNotDefinedForThisDateException exc = new PXRateIsNotDefinedForThisDateException(info.CuryRateTypeID, info.BaseCuryID, info.CuryID, (DateTime)row.ExpenseDate);
                        cache.RaiseExceptionHandling <EPExpenseClaimDetails.expenseDate>(e.Row, ((EPExpenseClaimDetails)e.Row).ExpenseDate, exc);
                    }
                }
                string message = PXUIFieldAttribute.GetError <CurrencyInfo.curyID>(currencyinfo.Cache, info);
                if (string.IsNullOrEmpty(message) && info != null && info.CuryRate == null)
                {
                    message = CM.Messages.RateNotFound;
                }
                if (string.IsNullOrEmpty(message))
                {
                    cache.RaiseExceptionHandling <EPExpenseClaimDetails.curyID>(e.Row, null, null);
                }
                else
                {
                    cache.RaiseExceptionHandling <EPExpenseClaimDetails.curyID>(e.Row, null, new PXSetPropertyException(message, PXErrorLevel.Warning));
                }

                bool allowEdit = this.Accessinfo.UserID == row.CreatedByID;

                if (Employee.Current != null)
                {
                    if (!allowEdit && this.Accessinfo.UserID == Employee.Current.UserID)
                    {
                        allowEdit = true;
                    }

                    if (!allowEdit)
                    {
                        EPWingman wingMan = PXSelectJoin <EPWingman,
                                                          InnerJoin <EPEmployee, On <EPWingman.wingmanID, Equal <EPEmployee.bAccountID> > >,
                                                          Where <EPWingman.employeeID, Equal <Required <EPWingman.employeeID> >,
                                                                 And <EPEmployee.userID, Equal <Required <EPEmployee.userID> > > > > .Select(this, row.EmployeeID, Accessinfo.UserID);

                        if (wingMan != null)
                        {
                            allowEdit = true;
                        }
                    }
                }

                //Another conditions in automation steps
                if (!allowEdit)
                {
                    action.SetEnabled(MsgNotLocalizable.PutOnHold, false);
                }

                bool taxSettingsEnabled = enabledEditReceipt && (epsetup.Current.AllowMixedTaxSettingInClaims == true || CurrentClaimDetails.Current.RefNbr == null);
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.taxZoneID>(cache, row, taxSettingsEnabled);
                ValidateProjectAndProjectTask(row);

                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.taxCalcMode>(cache, row, taxSettingsEnabled);
                PXUIFieldAttribute.SetEnabled <EPExpenseClaimDetails.taxCategoryID>(cache, row, enabledEditReceipt);

                PXUIFieldAttribute.SetVisible <EPExpenseClaimDetails.curyTipAmt>(ClaimDetails.Cache, null, epsetup.Current.NonTaxableTipItem.HasValue || (row.CuryTipAmt ?? 0) != 0);
                Taxes.Cache.SetAllEditPermissions(enabledEditReceipt);
                RaiseOrHideError <EPExpenseClaimDetails.refNbr>(cache, row, legacyClaim && row.Released == false, Messages.LegacyClaim, PXErrorLevel.Warning, row.RefNbr);
                RaiseOrHideError <EPExpenseClaimDetails.claimDetailID>(cache, row, row.LegacyReceipt == true && row.Released == false && !String.IsNullOrEmpty(row.TaxZoneID), Messages.LegacyReceipt, PXErrorLevel.Warning);

                EPEmployee employeeRow     = Employee.Select();
                string     taxZoneID       = EPClaimReceiptController.GetTaxZoneID(this, employeeRow);
                bool       notMatchtaxZone = String.IsNullOrEmpty(row.TaxZoneID) && !String.IsNullOrEmpty(taxZoneID);
                RaiseOrHideError <EPExpenseClaimDetails.taxZoneID>(cache, row, notMatchtaxZone && row.Released == false, Messages.TaxZoneEmpty, PXErrorLevel.Warning);
                if (UseTaxes.Select().Count != 0)
                {
                    cache.RaiseExceptionHandling <EPExpenseClaimDetails.curyTaxTotal>(row, row.CuryTaxTotal,
                                                                                      new PXSetPropertyException(TX.Messages.UseTaxExcludedFromTotals, PXErrorLevel.Warning));
                }
                else
                {
                    cache.RaiseExceptionHandling <EPExpenseClaimDetails.curyTaxTotal>(row, row.CuryTaxTotal, null);
                }
            }
        }
Пример #27
0
        public static void ReleaseDoc(EPExpenseClaim claim)
        {
            APInvoiceEntry    docgraph     = PXGraph.CreateInstance <APInvoiceEntry>();
            ExpenseClaimEntry expenseclaim = PXGraph.CreateInstance <ExpenseClaimEntry>();

            if (claim.FinPeriodID == null)
            {
                throw new PXException(Messages.ReleaseClaimWithoutFinPeriod);
            }
            //docgraph.FieldVerifying.AddHandler<APInvoice.vendorLocationID>(APInterceptor);

            EPEmployee employee = PXSelect <EPEmployee, Where <EPEmployee.bAccountID, Equal <Required <EPExpenseClaim.employeeID> > > > .Select(docgraph, claim.EmployeeID);

            Location emplocation = PXSelect <Location, Where <Location.bAccountID, Equal <Required <EPExpenseClaim.employeeID> >, And <Location.locationID, Equal <Required <EPExpenseClaim.locationID> > > > > .Select(docgraph, claim.EmployeeID, claim.LocationID);

            EPSetup epsetup = PXSelectReadonly <EPSetup> .Select(docgraph);

            APSetup apsetup = PXSelectReadonly <APSetup> .Select(docgraph);

            docgraph.vendor.Current   = employee;
            docgraph.location.Current = emplocation;

            CurrencyInfo infoOriginal = PXSelect <CurrencyInfo, Where <CurrencyInfo.curyInfoID, Equal <Required <EPExpenseClaim.curyInfoID> > > > .Select(docgraph, claim.CuryInfoID);

            CurrencyInfo info = PXCache <CurrencyInfo> .CreateCopy(infoOriginal);

            info.CuryInfoID = null;
            info            = docgraph.currencyinfo.Insert(info);
            APInvoice invoice = new APInvoice();

            invoice.CuryInfoID = info.CuryInfoID;

            invoice.Hold     = true;
            invoice.Released = false;
            invoice.Printed  = false;
            invoice.OpenDoc  = true;

            invoice.DocDate          = claim.DocDate;
            invoice.InvoiceNbr       = claim.RefNbr;
            invoice.DocDesc          = claim.DocDesc;
            invoice.VendorID         = claim.EmployeeID;
            invoice.CuryID           = info.CuryID;
            invoice.VendorLocationID = claim.LocationID;
            invoice.APAccountID      = emplocation != null ? emplocation.APAccountID : null;
            invoice.APSubID          = emplocation != null ? emplocation.APSubID : null;
            invoice.TaxZoneID        = claim.TaxZoneID;
            invoice = docgraph.Document.Insert(invoice);

            PXCache claimcache       = docgraph.Caches[typeof(EPExpenseClaim)];
            PXCache claimdetailcache = docgraph.Caches[typeof(EPExpenseClaimDetails)];

            if (epsetup.CopyNotesAP == true)
            {
                string note = PXNoteAttribute.GetNote(claimcache, claim);
                if (note != null)
                {
                    PXNoteAttribute.SetNote(docgraph.Document.Cache, invoice, note);
                }
            }
            if (epsetup.CopyFilesAP == true)
            {
                Guid[] files = PXNoteAttribute.GetFileNotes(claimcache, claim);
                if (files != null && files.Length > 0)
                {
                    PXNoteAttribute.SetFileNotes(docgraph.Document.Cache, invoice, files);
                }
            }

            using (PXConnectionScope cs = new PXConnectionScope())
            {
                using (PXTransactionScope ts = new PXTransactionScope())
                {
                    TaxAttribute.SetTaxCalc <APTran.taxCategoryID, APTaxAttribute>(docgraph.Transactions.Cache, null, TaxCalc.ManualCalc);

                    foreach (PXResult <EPExpenseClaimDetails, Contract> res in PXSelectJoin <EPExpenseClaimDetails,
                                                                                             LeftJoin <Contract, On <EPExpenseClaimDetails.contractID, Equal <Contract.contractID> > >,
                                                                                             Where <EPExpenseClaimDetails.refNbr, Equal <Required <EPExpenseClaim.refNbr> > > > .Select(docgraph, claim.RefNbr))
                    {
                        EPExpenseClaimDetails claimdetail = (EPExpenseClaimDetails)res;
                        Contract contract = (Contract)res;

                        if (claimdetail.TaskID != null)
                        {
                            PMTask task = PXSelect <PMTask, Where <PMTask.taskID, Equal <Required <PMTask.taskID> > > > .Select(expenseclaim, claimdetail.TaskID);

                            if (task != null && !(bool)task.VisibleInAP)
                            {
                                throw new PXException(PM.Messages.TaskInvisibleInModule, task.TaskCD, BatchModule.AP);
                            }
                        }

                        APTran tran = new APTran();
                        tran.InventoryID  = claimdetail.InventoryID;
                        tran.TranDesc     = claimdetail.TranDesc;
                        tran.CuryUnitCost = claimdetail.CuryUnitCost;
                        tran.Qty          = claimdetail.Qty;
                        tran.UOM          = claimdetail.UOM;
                        tran.NonBillable  = claimdetail.Billable != true;
                        tran.CuryLineAmt  = claimdetail.CuryTranAmt;
                        tran.Date         = claimdetail.ExpenseDate;

                        if (contract.BaseType == PM.PMProject.ProjectBaseType.Project)
                        {
                            tran.ProjectID = claimdetail.ContractID;
                        }
                        else
                        {
                            tran.ProjectID = PM.ProjectDefaultAttribute.NonProject(docgraph);
                        }

                        tran.TaskID        = claimdetail.TaskID;
                        tran.AccountID     = claimdetail.ExpenseAccountID;
                        tran.SubID         = claimdetail.ExpenseSubID;
                        tran.TaxCategoryID = claimdetail.TaxCategoryID;
                        tran = docgraph.Transactions.Insert(tran);
                        if (epsetup.CopyNotesAP == true)
                        {
                            string note = PXNoteAttribute.GetNote(claimdetailcache, claimdetail);
                            if (note != null)
                            {
                                PXNoteAttribute.SetNote(docgraph.Transactions.Cache, tran, note);
                            }
                        }
                        if (epsetup.CopyFilesAP == true)
                        {
                            Guid[] files = PXNoteAttribute.GetFileNotes(claimdetailcache, claimdetail);
                            if (files != null && files.Length > 0)
                            {
                                PXNoteAttribute.SetFileNotes(docgraph.Transactions.Cache, tran, files);
                            }
                        }
                        claimdetail.Released = true;
                        expenseclaim.ExpenseClaimDetails.Update(claimdetail);
                    }

                    foreach (EPTaxTran tax in PXSelect <EPTaxTran, Where <EPTaxTran.refNbr, Equal <Required <EPTaxTran.refNbr> > > > .Select(docgraph, claim.RefNbr))
                    {
                        APTaxTran new_aptax = new APTaxTran();
                        new_aptax.TaxID = tax.TaxID;

                        new_aptax = docgraph.Taxes.Insert(new_aptax);

                        if (new_aptax != null)
                        {
                            new_aptax = PXCache <APTaxTran> .CreateCopy(new_aptax);

                            new_aptax.TaxRate        = tax.TaxRate;
                            new_aptax.CuryTaxableAmt = tax.CuryTaxableAmt;
                            new_aptax.CuryTaxAmt     = tax.CuryTaxAmt;
                            new_aptax = docgraph.Taxes.Update(new_aptax);
                        }
                    }

                    invoice.CuryOrigDocAmt = invoice.CuryDocBal;
                    invoice.Hold           = false;
                    docgraph.Document.Update(invoice);
                    docgraph.Save.Press();
                    claim.Status    = EPClaimStatus.Released;
                    claim.Released  = true;
                    claim.APRefNbr  = invoice.RefNbr;
                    claim.APDocType = invoice.DocType;
                    expenseclaim.ExpenseClaim.Update(claim);

                    #region EP History Update
                    EPHistory hist = new EPHistory();
                    hist.EmployeeID  = invoice.VendorID;
                    hist.FinPeriodID = invoice.FinPeriodID;
                    hist             = (EPHistory)expenseclaim.Caches[typeof(EPHistory)].Insert(hist);

                    hist.FinPtdClaimed += invoice.DocBal;
                    hist.FinYtdClaimed += invoice.DocBal;
                    if (invoice.FinPeriodID == invoice.TranPeriodID)
                    {
                        hist.TranPtdClaimed += invoice.DocBal;
                        hist.TranYtdClaimed += invoice.DocBal;
                    }
                    else
                    {
                        EPHistory tranhist = new EPHistory();
                        tranhist.EmployeeID      = invoice.VendorID;
                        tranhist.FinPeriodID     = invoice.TranPeriodID;
                        tranhist                 = (EPHistory)expenseclaim.Caches[typeof(EPHistory)].Insert(tranhist);
                        tranhist.TranPtdClaimed += invoice.DocBal;
                        tranhist.TranYtdClaimed += invoice.DocBal;
                    }
                    expenseclaim.Views.Caches.Add(typeof(EPHistory));
                    #endregion

                    expenseclaim.Save.Press();

                    ts.Complete();
                }
            }
            if ((bool)epsetup.AutomaticReleaseAP == true)
            {
                List <APRegister> doclist = new List <APRegister>();
                doclist.Add(docgraph.Document.Current);
                APDocumentRelease.ReleaseDoc(doclist, false);
            }
        }
Пример #28
0
        public static void ReleaseDoc(EPExpenseClaim claim)
        {
            #region prepare required variables
            APInvoiceEntry    docgraph          = PXGraph.CreateInstance <APInvoiceEntry>();
            ExpenseClaimEntry expenseClaimGraph = PXGraph.CreateInstance <ExpenseClaimEntry>();


            var receipts = PXSelect <EPExpenseClaimDetails,
                                     Where <EPExpenseClaimDetails.refNbr, Equal <Required <EPExpenseClaim.refNbr> > > > .Select(expenseClaimGraph, claim.RefNbr).Select(
                result => (EPExpenseClaimDetails)result).GroupBy(
                item => Tuple.Create(
                    item.TaxZoneID,
                    item.TaxCalcMode
                    )).ToDictionary(x => x.Key, group => group.ToList());

            if (receipts.Count() == 0)
            {
                receipts.Add(Tuple.Create(claim.TaxZoneID, claim.TaxCalcMode), new List <EPExpenseClaimDetails>());
            }
            EPSetup epsetup = PXSelectReadonly <EPSetup> .Select(docgraph);

            APSetup apsetup = PXSelectReadonly <APSetup> .Select(docgraph);

            EPEmployee employee = PXSelect <EPEmployee, Where <EPEmployee.bAccountID, Equal <Required <EPExpenseClaim.employeeID> > > > .Select(docgraph, claim.EmployeeID);

            Location emplocation = PXSelect <Location, Where <Location.bAccountID, Equal <Required <EPExpenseClaim.employeeID> >, And <Location.locationID, Equal <Required <EPExpenseClaim.locationID> > > > > .Select(docgraph, claim.EmployeeID, claim.LocationID);

            List <APRegister> doclist = new List <APRegister>();
            expenseClaimGraph.SelectTimeStamp();
            #endregion

            using (PXTransactionScope ts = new PXTransactionScope())
            {
                foreach (var res in receipts)
                {
                    docgraph.Clear(PXClearOption.ClearAll);
                    docgraph.SelectTimeStamp();
                    docgraph.vendor.Current   = employee;
                    docgraph.location.Current = emplocation;

                    CurrencyInfo infoOriginal = PXSelect <CurrencyInfo,
                                                          Where <CurrencyInfo.curyInfoID, Equal <Required <EPExpenseClaim.curyInfoID> > > > .Select(docgraph, claim.CuryInfoID);

                    CurrencyInfo info = PXCache <CurrencyInfo> .CreateCopy(infoOriginal);

                    info.CuryInfoID = null;
                    info            = docgraph.currencyinfo.Insert(info);
                    #region CreateInoiceHeader
                    APInvoice invoice = new APInvoice();


                    bool reversedDocument = false;
                    if (res.Value.Sum(_ => _.ClaimCuryTranAmtWithTaxes) < 0)
                    {
                        invoice.DocType  = APInvoiceType.DebitAdj;
                        reversedDocument = true;
                    }
                    else
                    {
                        invoice.DocType = APInvoiceType.Invoice;
                    }
                    decimal signOperation;
                    if (reversedDocument)
                    {
                        signOperation = -1;
                    }
                    else
                    {
                        signOperation = 1;
                    }

                    invoice.CuryInfoID = info.CuryInfoID;

                    invoice.Hold     = true;
                    invoice.Released = false;
                    invoice.Printed  = false;
                    invoice.OpenDoc  = true;

                    invoice.DocDate          = claim.DocDate;
                    invoice.FinPeriodID      = claim.FinPeriodID;
                    invoice.InvoiceNbr       = claim.RefNbr;
                    invoice.DocDesc          = claim.DocDesc;
                    invoice.VendorID         = claim.EmployeeID;
                    invoice.CuryID           = info.CuryID;
                    invoice.VendorLocationID = claim.LocationID;
                    invoice.APAccountID      = emplocation != null ? emplocation.APAccountID : null;
                    invoice.APSubID          = emplocation != null ? emplocation.APSubID : null;
                    invoice.TaxZoneID        = res.Key.Item1;
                    invoice.TaxCalcMode      = res.Key.Item2;
                    invoice.BranchID         = claim.BranchID;
                    invoice.OrigModule       = BatchModule.EP;
                    invoice.OrigRefNbr       = claim.RefNbr;

                    invoice = docgraph.Document.Insert(invoice);

                    PXCache <CurrencyInfo> .RestoreCopy(info, infoOriginal);

                    info.CuryInfoID = invoice.CuryInfoID;

                    PXCache claimcache       = docgraph.Caches[typeof(EPExpenseClaim)];
                    PXCache claimdetailcache = docgraph.Caches[typeof(EPExpenseClaimDetails)];

                    PXNoteAttribute.CopyNoteAndFiles(claimcache, claim, docgraph.Document.Cache, invoice, epsetup.GetCopyNoteSettings <PXModule.ap>());
                    #endregion
                    TaxAttribute.SetTaxCalc <APTran.taxCategoryID, APTaxAttribute>(docgraph.Transactions.Cache, null, TaxCalc.ManualCalc);
                    decimal?claimCuryTaxRoundDiff = 0m;
                    decimal?claimTaxRoundDiff     = 0m;
                    foreach (EPExpenseClaimDetails claimdetail in res.Value)
                    {
                        #region AddDetails

                        decimal tipQty;
                        if (reversedDocument == claimdetail.ClaimCuryTranAmtWithTaxes < 0)
                        {
                            tipQty = 1;
                        }
                        else
                        {
                            tipQty = -1;
                        }
                        Contract contract = PXSelect <Contract, Where <Contract.contractID, Equal <Required <EPExpenseClaimDetails.contractID> > > > .SelectSingleBound(docgraph, null, claimdetail.ContractID);

                        if (claimdetail.TaskID != null)
                        {
                            PMTask task = PXSelect <PMTask, Where <PMTask.taskID, Equal <Required <PMTask.taskID> > > > .Select(expenseClaimGraph, claimdetail.TaskID);

                            if (task != null && !(bool)task.VisibleInAP)
                            {
                                throw new PXException(PM.Messages.TaskInvisibleInModule, task.TaskCD, BatchModule.AP);
                            }
                        }

                        APTran tran = new APTran();
                        tran.InventoryID = claimdetail.InventoryID;
                        tran.TranDesc    = claimdetail.TranDesc;
                        decimal unitCost;
                        decimal amount;
                        decimal taxableAmt;
                        decimal taxAmt;
                        if (EPClaimReceiptController.IsSameCury(expenseClaimGraph, claimdetail.CuryInfoID, claimdetail.ClaimCuryInfoID))
                        {
                            unitCost   = claimdetail.CuryUnitCost ?? 0m;
                            amount     = claimdetail.CuryTaxableAmt ?? 0m;
                            taxableAmt = claimdetail.CuryTaxableAmtFromTax ?? 0m;
                            taxAmt     = claimdetail.CuryTaxAmt ?? 0m;
                        }
                        else
                        {
                            if (claimdetail.CuryUnitCost == null || claimdetail.CuryUnitCost == 0m)
                            {
                                unitCost = 0m;
                            }
                            else
                            {
                                PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(expenseClaimGraph.ExpenseClaimDetails.Cache, claimdetail, (decimal)claimdetail.UnitCost, out unitCost);
                            }
                            if (claimdetail.CuryTaxableAmt == null || claimdetail.CuryTaxableAmt == 0m)
                            {
                                amount = 0m;
                            }
                            else
                            {
                                PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(expenseClaimGraph.ExpenseClaimDetails.Cache, claimdetail, (decimal)claimdetail.TaxableAmt, out amount);
                            }
                            if (claimdetail.CuryTaxableAmtFromTax == null || claimdetail.CuryTaxableAmtFromTax == 0m)
                            {
                                taxableAmt = 0m;
                            }
                            else
                            {
                                PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(expenseClaimGraph.ExpenseClaimDetails.Cache, claimdetail, (decimal)claimdetail.TaxableAmtFromTax, out taxableAmt);
                            }
                            if (claimdetail.CuryTaxAmt == null || claimdetail.CuryTaxAmt == 0m)
                            {
                                taxAmt = 0m;
                            }
                            else
                            {
                                PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(expenseClaimGraph.ExpenseClaimDetails.Cache, claimdetail, (decimal)claimdetail.TaxAmt, out taxAmt);
                            }
                        }

                        tran.ManualPrice       = true;
                        tran.CuryUnitCost      = unitCost;
                        tran.Qty               = claimdetail.Qty * signOperation;
                        tran.UOM               = claimdetail.UOM;
                        tran.NonBillable       = claimdetail.Billable != true;
                        claimCuryTaxRoundDiff += (claimdetail.ClaimCuryTaxRoundDiff ?? 0m) * signOperation;
                        claimTaxRoundDiff     += (claimdetail.ClaimTaxRoundDiff ?? 0m) * signOperation;
                        tran.Date              = claimdetail.ExpenseDate;

                        if (contract.BaseType == PMProject.ProjectBaseType.Project)
                        {
                            tran.ProjectID = claimdetail.ContractID;
                        }
                        else
                        {
                            tran.ProjectID = ProjectDefaultAttribute.NonProject();
                        }

                        tran.TaskID        = claimdetail.TaskID;
                        tran.CostCodeID    = claimdetail.CostCodeID;
                        tran.AccountID     = claimdetail.ExpenseAccountID;
                        tran.SubID         = claimdetail.ExpenseSubID;
                        tran.TaxCategoryID = claimdetail.TaxCategoryID;
                        tran.BranchID      = claimdetail.BranchID;
                        tran                = docgraph.Transactions.Insert(tran);
                        tran.CuryLineAmt    = amount * signOperation;
                        tran.CuryTaxAmt     = 0;
                        tran.CuryTaxableAmt = taxableAmt * signOperation;
                        tran.CuryTaxAmt     = taxAmt * signOperation;
                        tran                = docgraph.Transactions.Update(tran);


                        if ((claimdetail.CuryTipAmt ?? 0) != 0)
                        {
                            APTran tranTip = new APTran();
                            if (epsetup.NonTaxableTipItem == null)
                            {
                                throw new PXException(Messages.TipItemIsNotDefined);
                            }
                            IN.InventoryItem tipItem = PXSelect <IN.InventoryItem,
                                                                 Where <IN.InventoryItem.inventoryID, Equal <Required <IN.InventoryItem.inventoryID> > > > .Select(docgraph, epsetup.NonTaxableTipItem);

                            if (tipItem == null)
                            {
                                string fieldname = PXUIFieldAttribute.GetDisplayName <EPSetup.nonTaxableTipItem>(docgraph.Caches[typeof(EPSetup)]);
                                throw new PXException(ErrorMessages.ValueDoesntExistOrNoRights, fieldname, epsetup.NonTaxableTipItem);
                            }
                            tranTip.InventoryID = epsetup.NonTaxableTipItem;
                            tranTip.TranDesc    = tipItem.Descr;
                            if (EPClaimReceiptController.IsSameCury(expenseClaimGraph, claimdetail.CuryInfoID, claimdetail.ClaimCuryInfoID))
                            {
                                tranTip.CuryUnitCost = Math.Abs(claimdetail.CuryTipAmt ?? 0m);
                                tranTip.CuryTranAmt  = claimdetail.CuryTipAmt * signOperation;
                            }
                            else
                            {
                                decimal tipAmt;
                                PXCurrencyAttribute.CuryConvCury <EPExpenseClaimDetails.claimCuryInfoID>(expenseClaimGraph.ExpenseClaimDetails.Cache, claimdetail, (decimal)claimdetail.TipAmt, out tipAmt);
                                tranTip.CuryUnitCost = Math.Abs(tipAmt);
                                tranTip.CuryTranAmt  = tipAmt * signOperation;
                            }
                            tranTip.Qty         = tipQty;
                            tranTip.UOM         = tipItem.BaseUnit;
                            tranTip.NonBillable = claimdetail.Billable != true;
                            tranTip.Date        = claimdetail.ExpenseDate;

                            tranTip.BranchID = claimdetail.BranchID;
                            tranTip          = docgraph.Transactions.Insert(tranTip);

                            if (epsetup.UseReceiptAccountForTips == true)
                            {
                                tranTip.AccountID = claimdetail.ExpenseAccountID;
                                tranTip.SubID     = claimdetail.ExpenseSubID;
                            }
                            else
                            {
                                tranTip.AccountID = tipItem.COGSAcctID;
                                Location companyloc = (Location)PXSelectJoin <Location,
                                                                              InnerJoin <BAccountR, On <Location.bAccountID, Equal <BAccountR.bAccountID>,
                                                                                                        And <Location.locationID, Equal <BAccountR.defLocationID> > >,
                                                                                         InnerJoin <GL.Branch, On <BAccountR.bAccountID, Equal <GL.Branch.bAccountID> > > >,
                                                                              Where <GL.Branch.branchID, Equal <Current <APInvoice.branchID> > > > .Select(docgraph);

                                PMTask task = PXSelect <PMTask,
                                                        Where <PMTask.projectID, Equal <Required <PMTask.projectID> >,
                                                               And <PMTask.taskID, Equal <Required <PMTask.taskID> > > > > .Select(docgraph, claimdetail.ContractID, claimdetail.TaskID);

                                Location customerLocation = (Location)PXSelectorAttribute.Select <EPExpenseClaimDetails.customerLocationID>(claimdetailcache, claimdetail);

                                int?employee_SubID = (int?)docgraph.Caches[typeof(EPEmployee)].GetValue <EPEmployee.expenseSubID>(employee);
                                int?item_SubID     = (int?)docgraph.Caches[typeof(IN.InventoryItem)].GetValue <IN.InventoryItem.cOGSSubID>(tipItem);
                                int?company_SubID  = (int?)docgraph.Caches[typeof(Location)].GetValue <Location.cMPExpenseSubID>(companyloc);
                                int?project_SubID  = (int?)docgraph.Caches[typeof(Contract)].GetValue <Contract.defaultSubID>(contract);
                                int?task_SubID     = (int?)docgraph.Caches[typeof(PMTask)].GetValue <PMTask.defaultSubID>(task);
                                int?location_SubID = (int?)docgraph.Caches[typeof(Location)].GetValue <Location.cSalesSubID>(customerLocation);

                                object value = SubAccountMaskAttribute.MakeSub <EPSetup.expenseSubMask>(docgraph, epsetup.ExpenseSubMask,
                                                                                                        new object[] { employee_SubID, item_SubID, company_SubID, project_SubID, task_SubID, location_SubID },
                                                                                                        new Type[] { typeof(EPEmployee.expenseSubID), typeof(IN.InventoryItem.cOGSSubID), typeof(Location.cMPExpenseSubID), typeof(Contract.defaultSubID), typeof(PMTask.defaultSubID), typeof(Location.cSalesSubID) });

                                docgraph.Caches[typeof(APTran)].RaiseFieldUpdating <APTran.subID>(tranTip, ref value);
                                tranTip.SubID = (int?)value;
                            }
                            tranTip = docgraph.Transactions.Update(tranTip);
                            tranTip.TaxCategoryID = tipItem.TaxCategoryID;
                            tranTip.ProjectID     = tran.ProjectID;
                            tranTip.TaskID        = tran.TaskID;
                            tranTip = AddTaxes(docgraph, invoice, signOperation, claimdetail, tranTip, true);
                            tranTip = docgraph.Transactions.Update(tranTip);
                        }

                        PXNoteAttribute.CopyNoteAndFiles(claimdetailcache, claimdetail, docgraph.Transactions.Cache, tran, epsetup.GetCopyNoteSettings <PXModule.ap>());
                        claimdetail.Released = true;
                        expenseClaimGraph.ExpenseClaimDetails.Update(claimdetail);
                        #endregion


                        tran = AddTaxes(docgraph, invoice, signOperation, claimdetail, tran, false);
                    }

                    #region legacy taxes
                    foreach (EPTaxAggregate tax in PXSelectReadonly <EPTaxAggregate,
                                                                     Where <EPTaxAggregate.refNbr, Equal <Required <EPExpenseClaim.refNbr> > > > .Select(docgraph, claim.RefNbr))
                    {
                        #region Add taxes
                        APTaxTran new_aptax = docgraph.Taxes.Search <APTaxTran.taxID>(tax.TaxID);

                        if (new_aptax == null)
                        {
                            new_aptax       = new APTaxTran();
                            new_aptax.TaxID = tax.TaxID;
                            new_aptax       = docgraph.Taxes.Insert(new_aptax);
                            if (new_aptax != null)
                            {
                                new_aptax = PXCache <APTaxTran> .CreateCopy(new_aptax);

                                new_aptax.CuryTaxableAmt = 0m;
                                new_aptax.CuryTaxAmt     = 0m;
                                new_aptax.CuryExpenseAmt = 0m;
                                new_aptax = docgraph.Taxes.Update(new_aptax);
                            }
                        }

                        if (new_aptax != null)
                        {
                            new_aptax = PXCache <APTaxTran> .CreateCopy(new_aptax);

                            new_aptax.TaxRate        = tax.TaxRate;
                            new_aptax.CuryTaxableAmt = (new_aptax.CuryTaxableAmt ?? 0m) + tax.CuryTaxableAmt * signOperation;
                            new_aptax.CuryTaxAmt     = (new_aptax.CuryTaxAmt ?? 0m) + tax.CuryTaxAmt * signOperation;
                            new_aptax.CuryExpenseAmt = (new_aptax.CuryExpenseAmt ?? 0m) + tax.CuryExpenseAmt * signOperation;
                            new_aptax = docgraph.Taxes.Update(new_aptax);
                        }
                        #endregion
                    }
                    #endregion

                    invoice.CuryOrigDocAmt = invoice.CuryDocBal;
                    invoice.CuryTaxAmt     = invoice.CuryTaxTotal;
                    invoice.Hold           = false;
                    docgraph.Document.Update(invoice);
                    invoice.CuryTaxRoundDiff = invoice.CuryRoundDiff = invoice.CuryRoundDiff = claimCuryTaxRoundDiff;
                    invoice.TaxRoundDiff     = invoice.RoundDiff = claimTaxRoundDiff;
                    bool inclusive = PXSelectJoin <APTaxTran, InnerJoin <Tax,
                                                                         On <APTaxTran.taxID, Equal <Tax.taxID> > >,
                                                   Where <APTaxTran.refNbr, Equal <Required <APInvoice.refNbr> >,
                                                          And <APTaxTran.tranType, Equal <Required <APInvoice.docType> >,
                                                               And <Tax.taxCalcLevel, Equal <CSTaxCalcLevel.inclusive> > > > >
                                     .Select(docgraph, invoice.RefNbr, invoice.DocType).Count > 0;

                    if ((invoice.TaxCalcMode == TaxCalculationMode.Gross &&
                         PXSelectJoin <APTaxTran, InnerJoin <Tax,
                                                             On <APTaxTran.taxID, Equal <Tax.taxID> > >,
                                       Where <APTaxTran.refNbr, Equal <Required <APInvoice.refNbr> >,
                                              And <APTaxTran.tranType, Equal <Required <APInvoice.docType> >,
                                                   And <Tax.taxCalcLevel, Equal <CSTaxCalcLevel.calcOnItemAmt> > > > >
                         .Select(docgraph, invoice.RefNbr, invoice.DocType).Count > 0) ||
                        inclusive)
                    {
                        decimal curyAdditionalDiff = -(invoice.CuryTaxRoundDiff ?? 0m) + (invoice.CuryTaxAmt ?? 0m) - (invoice.CuryDocBal ?? 0m);
                        decimal additionalDiff     = -(invoice.TaxRoundDiff ?? 0m) + (invoice.TaxAmt ?? 0m) - (invoice.DocBal ?? 0m);
                        foreach (APTran line in docgraph.Transactions.Select())
                        {
                            curyAdditionalDiff += (line.CuryTaxableAmt ?? 0m) == 0m ? (line.CuryTranAmt ?? 0m) : (line.CuryTaxableAmt ?? 0m);
                            additionalDiff     += (line.TaxableAmt ?? 0m) == 0m ? (line.TranAmt ?? 0m) : (line.TaxableAmt ?? 0m);
                        }

                        invoice.CuryTaxRoundDiff += curyAdditionalDiff;
                        invoice.TaxRoundDiff     += additionalDiff;
                    }

                    docgraph.Document.Update(invoice);
                    docgraph.Save.Press();
                    foreach (EPExpenseClaimDetails claimdetail in res.Value)
                    {
                        claimdetail.APDocType = invoice.DocType;
                        claimdetail.APRefNbr  = invoice.RefNbr;
                        expenseClaimGraph.ExpenseClaimDetails.Update(claimdetail);
                    }
                    claim.Status   = EPExpenseClaimStatus.ReleasedStatus;
                    claim.Released = true;
                    expenseClaimGraph.ExpenseClaim.Update(claim);

                    #region EP History Update
                    EPHistory hist = new EPHistory();
                    hist.EmployeeID  = invoice.VendorID;
                    hist.FinPeriodID = invoice.FinPeriodID;
                    hist             = (EPHistory)expenseClaimGraph.Caches[typeof(EPHistory)].Insert(hist);

                    hist.FinPtdClaimed += invoice.DocBal;
                    hist.FinYtdClaimed += invoice.DocBal;
                    if (invoice.FinPeriodID == invoice.TranPeriodID)
                    {
                        hist.TranPtdClaimed += invoice.DocBal;
                        hist.TranYtdClaimed += invoice.DocBal;
                    }
                    else
                    {
                        EPHistory tranhist = new EPHistory();
                        tranhist.EmployeeID      = invoice.VendorID;
                        tranhist.FinPeriodID     = invoice.TranPeriodID;
                        tranhist                 = (EPHistory)expenseClaimGraph.Caches[typeof(EPHistory)].Insert(tranhist);
                        tranhist.TranPtdClaimed += invoice.DocBal;
                        tranhist.TranYtdClaimed += invoice.DocBal;
                    }
                    expenseClaimGraph.Views.Caches.Add(typeof(EPHistory));
                    #endregion

                    expenseClaimGraph.Save.Press();

                    doclist.Add(docgraph.Document.Current);
                }

                ts.Complete();
            }

            if ((bool)epsetup.AutomaticReleaseAP == true)
            {
                APDocumentRelease.ReleaseDoc(doclist, false);
            }
        }
Пример #29
0
 public static void ReleaseDoc(EPExpenseClaim claim)
 {
     PXGraph.CreateInstance <EPReleaseProcess>().ReleaseDocProc(claim);
 }
Пример #30
0
        private static void ClaimDetail(List <EPExpenseClaimDetails> details)
        {
            PXSetup <EPSetup> epsetup   = new PXSetup <EPSetup>(PXGraph.CreateInstance(typeof(ExpenseClaimDetailEntry)));
            bool enabledApprovalReceipt = PXAccess.FeatureInstalled <FeaturesSet.approvalWorkflow>() && epsetup.Current.ClaimDetailsAssignmentMapID != null;
            bool isError        = false;
            bool notAllApproved = false;
            bool emptyClaim     = true;
            var  List           = details.Where(item => string.IsNullOrEmpty(item.RefNbr)).OrderBy(detail => detail.ClaimDetailID).GroupBy(
                item => new { item.EmployeeID, item.BranchID, item.CustomerID, item.CustomerLocationID }
                , (key, item) => new { employee = key.EmployeeID, branch = key.BranchID, customer = key.CustomerID, customerLocation = key.CustomerLocationID, details = item }
                );

            List <EPExpenseClaim> result = new List <EPExpenseClaim>();

            foreach (var item in List)
            {
                isError        = false;
                notAllApproved = false;
                emptyClaim     = true;
                using (PXTransactionScope ts = new PXTransactionScope())
                {
                    ExpenseClaimEntry expenseClaimEntry = CreateInstance <ExpenseClaimEntry>();
                    EPExpenseClaim    expenseClaim      = (EPExpenseClaim)expenseClaimEntry.ExpenseClaim.Cache.CreateInstance();
                    expenseClaim.EmployeeID         = item.employee;
                    expenseClaim.BranchID           = item.branch;
                    expenseClaim.CustomerID         = item.customer;
                    expenseClaim.DocDesc            = EP.Messages.SubmittedReceipt;
                    expenseClaim                    = expenseClaimEntry.ExpenseClaim.Update(expenseClaim);
                    expenseClaim.CustomerLocationID = item.customerLocation;

                    foreach (EPExpenseClaimDetails detail in item.details)
                    {
                        PXProcessing <EPExpenseClaimDetails> .SetCurrentItem(detail);

                        if (detail.Approved ?? false)
                        {
                            try
                            {
                                expenseClaimEntry.SubmitDetail(detail);
                                PXProcessing <EPExpenseClaimDetails> .SetProcessed();

                                emptyClaim = false;
                            }
                            catch (Exception ex)
                            {
                                PXProcessing <EPExpenseClaimDetails> .SetError(ex);

                                isError = true;
                            }
                        }
                        else
                        {
                            PXProcessing.SetError(enabledApprovalReceipt
                                ? Messages.ReceiptNotApproved
                                : Messages.ReceiptTakenOffHold);
                            notAllApproved = true;
                        }
                    }

                    if (!emptyClaim)
                    {
                        try
                        {
                            expenseClaimEntry.Actions.PressSave();
                            result.Add(expenseClaim);
                        }
                        catch (Exception ex)
                        {
                            foreach (EPExpenseClaimDetails detail in item.details)
                            {
                                PXProcessing <EPExpenseClaimDetails> .SetCurrentItem(detail);

                                PXProcessing <EPExpenseClaimDetails> .SetError(ex);
                            }
                            isError = true;
                        }
                    }
                    if (!isError)
                    {
                        ts.Complete();
                    }
                }
            }

            if (!isError && !notAllApproved)
            {
                if (result.Count == 1)
                {
                    ExpenseClaimEntry expenseClaimEntry = CreateInstance <ExpenseClaimEntry>();
                    PXRedirectHelper.TryRedirect(expenseClaimEntry, result[0], PXRedirectHelper.WindowMode.InlineWindow);
                }
            }
            else
            {
                PXProcessing <EPExpenseClaimDetails> .SetCurrentItem(null);

                throw new PXException(Messages.ErrorProcessingReceipts);
            }
        }