protected virtual IAddressBase GetFromAddress(APInvoice invoice, APTran tran)
 {
     return((IAddressBase)
            PXSelectJoin <PORemitAddress,
                          InnerJoin <POOrder, On <PORemitAddress.addressID, Equal <POOrder.remitAddressID> > >,
                          Where <POOrder.orderType, Equal <Current <APTran.pOOrderType> >,
                                 And <POOrder.orderNbr, Equal <Current <APTran.pONbr> > > > >
            .SelectSingleBound(Base, new[] { tran })
            .RowCast <PORemitAddress>()
            .FirstOrDefault()
            ??
            PXSelectJoin <Address,
                          InnerJoin <Location, On <Location.defAddressID, Equal <Address.addressID> >,
                                     InnerJoin <POReceipt, On <POReceipt.vendorLocationID, Equal <Location.locationID> >,
                                                InnerJoin <POReceiptLine, On <
                                                               POReceiptLine.receiptType, Equal <POReceipt.receiptType>,
                                                               And <POReceiptLine.receiptNbr, Equal <POReceipt.receiptNbr> > > > > >,
                          Where <POReceiptLine.receiptType, Equal <Current <APTran.receiptType> >,
                                 And <POReceiptLine.receiptNbr, Equal <Current <APTran.receiptNbr> >,
                                      And <POReceiptLine.lineNbr, Equal <Current <APTran.receiptLineNbr> > > > > >
            .SelectSingleBound(Base, new[] { tran })
            .RowCast <Address>()
            .FirstOrDefault()
            ??
            PXSelectJoin <Address,
                          InnerJoin <Location, On <Location.defAddressID, Equal <Address.addressID> > >,
                          Where <Location.locationID, Equal <Current <APInvoice.vendorLocationID> > > >
            .SelectSingleBound(Base, new[] { invoice })
            .RowCast <Address>()
            .FirstOrDefault());;
 }
예제 #2
0
        protected virtual IEnumerable documentList()
        {
            foreach (APInvoiceExt doc in PXSelect <APInvoiceExt> .Select(this))
            {
                bool hasUnreleasedDocument = false;

                foreach (PXResult <APRetainageInvoice, APTran> res in PXSelectJoin <APRetainageInvoice,
                                                                                    LeftJoin <APTran, On <APRetainageInvoice.paymentsByLinesAllowed, Equal <True>,
                                                                                                          And <APTran.tranType, Equal <APRetainageInvoice.docType>,
                                                                                                               And <APTran.refNbr, Equal <APRetainageInvoice.refNbr>,
                                                                                                                    And <APTran.origLineNbr, Equal <Required <APTran.origLineNbr> > > > > > >,
                                                                                    Where <APRetainageInvoice.isRetainageDocument, Equal <True>,
                                                                                           And <APRetainageInvoice.origDocType, Equal <Required <APInvoice.docType> >,
                                                                                                And <APRetainageInvoice.origRefNbr, Equal <Required <APInvoice.refNbr> >,
                                                                                                     And <APRetainageInvoice.released, NotEqual <True> > > > > >
                         .Select(this, doc.APTranLineNbr, doc.DocType, doc.RefNbr))
                {
                    APRetainageInvoice invoice = res;
                    APTran             tran    = res;

                    if (invoice.PaymentsByLinesAllowed != true ||
                        tran.LineNbr != null)
                    {
                        hasUnreleasedDocument = true;
                    }
                }

                if (!hasUnreleasedDocument)
                {
                    yield return(doc);
                }
            }
        }
 public virtual void SetReferenceKeyTo(APTran aTran)
 {
     aTran.POAccrualType      = this.POAccrualType;
     aTran.PONbr              = this.OrderNbr;
     aTran.POOrderType        = this.OrderType;
     aTran.POLineNbr          = this.LineNbr;
     aTran.POAccrualRefNoteID = ((IAPTranSource)this).POAccrualRefNoteID;
     aTran.POAccrualLineNbr   = ((IAPTranSource)this).POAccrualLineNbr;
 }
            public virtual void SetReferenceKeyTo(APTran aTran)
            {
                bool orderPOAccrual = (this.POAccrualType == Objects.PO.POAccrualType.Order);

                aTran.POAccrualType      = this.POAccrualType;
                aTran.POAccrualRefNoteID = this.POAccrualRefNoteID;
                aTran.POAccrualLineNbr   = this.POAccrualLineNbr;
                aTran.ReceiptType        = orderPOAccrual ? null : this.ReceiptType;
                aTran.ReceiptNbr         = orderPOAccrual ? null : this.ReceiptNbr;
                aTran.ReceiptLineNbr     = orderPOAccrual ? null : this.LineNbr;
                aTran.POOrderType        = this.POType;
                aTran.PONbr     = this.PONbr;
                aTran.POLineNbr = this.POLineNbr;
            }
        protected virtual IAddressBase GetToAddress(APInvoice invoice, APTran tran)
        {
            var poLine =
                PXSelectJoin <Address,
                              InnerJoin <INSite, On <INSite.addressID, Equal <Address.addressID> >,
                                         InnerJoin <POLine, On <POLine.siteID, Equal <INSite.siteID> > > >,
                              Where <POLine.orderType, Equal <Current <APTran.pOOrderType> >,
                                     And <POLine.orderNbr, Equal <Current <APTran.pONbr> >,
                                          And <POLine.lineNbr, Equal <Current <APTran.pOLineNbr> > > > > >
                .SelectSingleBound(Base, new[] { tran })
                .Cast <PXResult <Address, INSite, POLine> >()
                .FirstOrDefault();

            var poAddress =
                PXSelectJoin <POShipAddress,
                              InnerJoin <POOrder, On <POShipAddress.addressID, Equal <POOrder.shipAddressID> > >,
                              Where <POOrder.orderType, Equal <Current <APTran.pOOrderType> >,
                                     And <POOrder.orderNbr, Equal <Current <APTran.pONbr> > > > >
                .SelectSingleBound(Base, new[] { tran })
                .RowCast <POShipAddress>()
                .FirstOrDefault();

            if (poLine?.GetItem <POLine>()?.OrderType == POOrderType.DropShip)
            {
                return(poAddress);
            }

            return
                (PXSelectJoin <Address,
                               InnerJoin <INSite, On <INSite.addressID, Equal <Address.addressID> >,
                                          InnerJoin <POReceiptLine, On <POReceiptLine.siteID, Equal <INSite.siteID> > > >,
                               Where <POReceiptLine.receiptType, Equal <Current <APTran.receiptType> >,
                                      And <POReceiptLine.receiptNbr, Equal <Current <APTran.receiptNbr> >,
                                           And <POReceiptLine.lineNbr, Equal <Current <APTran.receiptLineNbr> > > > > >
                 .SelectSingleBound(Base, new[] { tran })
                 .RowCast <Address>()
                 .FirstOrDefault()
                 ?? poLine?.GetItem <Address>()
                 ?? poAddress
                 ?? GetToAddress(invoice));
        }
        public virtual void GenerateProc(Schedule schedule, short times, DateTime runDate)
        {
            IEnumerable <ScheduleDet> occurrences = new Scheduler(this).MakeSchedule(schedule, times, runDate);

            APInvoiceEntry invoiceEntry = CreateGraph();

            using (PXTransactionScope transactionScope = new PXTransactionScope())
            {
                foreach (ScheduleDet occurrence in occurrences)
                {
                    foreach (PXResult <APInvoice, Vendor, CurrencyInfo> scheduledInvoiceResult in PXSelectJoin <
                                 APInvoice,
                                 InnerJoin <Vendor, On <Vendor.bAccountID, Equal <APInvoice.vendorID> >,
                                            InnerJoin <CurrencyInfo, On <CurrencyInfo.curyInfoID, Equal <APInvoice.curyInfoID> > > >,
                                 Where <
                                     APInvoice.scheduleID, Equal <Required <APInvoice.scheduleID> >,
                                     And <APInvoice.scheduled, Equal <boolTrue> > > >
                             .Select(this, schedule.ScheduleID))
                    {
                        invoiceEntry.Clear();

                        invoiceEntry.vendor.Current = (Vendor)scheduledInvoiceResult;
                        APInvoice    scheduledInvoice             = (APInvoice)scheduledInvoiceResult;
                        CurrencyInfo scheduledInvoiceCurrencyInfo = (CurrencyInfo)scheduledInvoiceResult;

                        if (scheduledInvoice.Released == true)
                        {
                            throw new PXException(AR.Messages.ScheduledDocumentAlreadyReleased);
                        }

                        // Cloning currency info is required because we want to preserve
                        // (and not default) the currency rate type of the template document.
                        // -
                        CurrencyInfo newCurrencyInfo = PXCache <CurrencyInfo> .CreateCopy(scheduledInvoiceCurrencyInfo);

                        newCurrencyInfo.CuryInfoID  = null;
                        newCurrencyInfo.CuryRate    = null;
                        newCurrencyInfo.CuryEffDate = occurrence.ScheduledDate;

                        newCurrencyInfo = invoiceEntry.currencyinfo.Insert(newCurrencyInfo);

                        APInvoice newInvoice = PXCache <APInvoice> .CreateCopy(scheduledInvoice);

                        newInvoice.CuryInfoID          = newCurrencyInfo.CuryInfoID;
                        newInvoice.DocDate             = occurrence.ScheduledDate;
                        newInvoice.FinPeriodID         = occurrence.ScheduledPeriod;
                        newInvoice.TranPeriodID        = null;
                        newInvoice.DueDate             = null;
                        newInvoice.DiscDate            = null;
                        newInvoice.PayDate             = null;
                        newInvoice.CuryOrigDiscAmt     = null;
                        newInvoice.OrigDiscAmt         = null;
                        newInvoice.RefNbr              = null;
                        newInvoice.Scheduled           = false;
                        newInvoice.CuryLineTotal       = 0m;
                        newInvoice.CuryVatTaxableTotal = 0m;
                        newInvoice.CuryVatExemptTotal  = 0m;
                        newInvoice.NoteID              = null;
                        newInvoice.PaySel              = false;
                        newInvoice.IsTaxValid          = false;
                        newInvoice.IsTaxPosted         = false;
                        newInvoice.IsTaxSaved          = false;
                        newInvoice.OrigDocType         = scheduledInvoice.DocType;
                        newInvoice.OrigRefNbr          = scheduledInvoice.RefNbr;

                        newInvoice = invoiceEntry.Document.Insert(newInvoice);                         //we insert an item here because we need DontApprove field to be set in RowSelected
                        if (newInvoice.DontApprove != true)
                        {
                            // We always generate documents on hold
                            // if approval process is enabled in AP.
                            // -
                            newInvoice.Hold = true;
                        }

                        newInvoice = invoiceEntry.Document.Update(newInvoice);

                        CurrencyInfoAttribute.SetEffectiveDate <APInvoice.docDate>(
                            invoiceEntry.Document.Cache,
                            new PXFieldUpdatedEventArgs(newInvoice, null, false));

                        PXNoteAttribute.CopyNoteAndFiles(Caches[typeof(APInvoice)], scheduledInvoice, invoiceEntry.Document.Cache, newInvoice);

                        foreach (APTran originalLine in PXSelect <
                                     APTran,
                                     Where <
                                         APTran.tranType, Equal <Required <APTran.tranType> >,
                                         And <APTran.refNbr, Equal <Required <APTran.refNbr> >,
                                              And <Where <
                                                       APTran.lineType, IsNull,
                                                       Or <APTran.lineType, NotEqual <SOLineType.discount> > > > > > >
                                 .Select(invoiceEntry, scheduledInvoice.DocType, scheduledInvoice.RefNbr))
                        {
                            APTran newLine = PXCache <APTran> .CreateCopy(originalLine);

                            newLine.RefNbr      = null;
                            newLine.CuryInfoID  = null;
                            newLine.ManualPrice = true;
                            newLine.ManualDisc  = true;
                            newLine.NoteID      = null;
                            newLine             = invoiceEntry.Transactions.Insert(newLine);

                            PXNoteAttribute.CopyNoteAndFiles(Caches[typeof(APTran)], originalLine, invoiceEntry.Transactions.Cache, newLine);
                        }

                        foreach (APInvoiceDiscountDetail originalDiscountDetail in PXSelect <
                                     APInvoiceDiscountDetail,
                                     Where <
                                         APInvoiceDiscountDetail.docType, Equal <Required <APInvoiceDiscountDetail.docType> >,
                                         And <APInvoiceDiscountDetail.refNbr, Equal <Required <APInvoiceDiscountDetail.refNbr> > > > >
                                 .Select(invoiceEntry, scheduledInvoice.DocType, scheduledInvoice.RefNbr))
                        {
                            APInvoiceDiscountDetail newDiscountDetail =
                                PXCache <APInvoiceDiscountDetail> .CreateCopy(originalDiscountDetail);

                            newDiscountDetail.RefNbr     = null;
                            newDiscountDetail.CuryInfoID = null;
                            newDiscountDetail.IsManual   = true;

                            DiscountEngineProvider.GetEngineFor <APTran, APInvoiceDiscountDetail>().InsertDiscountDetail(invoiceEntry.DiscountDetails.Cache, invoiceEntry.DiscountDetails, newDiscountDetail);
                        }

                        BalanceCalculation.ForceDocumentControlTotals(invoiceEntry, newInvoice);

                        try
                        {
                            invoiceEntry.Save.Press();
                        }
                        catch
                        {
                            if (invoiceEntry.Document.Cache.IsInsertedUpdatedDeleted)
                            {
                                throw;
                            }
                        }
                    }

                    schedule.LastRunDate = occurrence.ScheduledDate;
                    Running_Schedule.Cache.Update(schedule);
                }

                transactionScope.Complete(this);
            }

            using (PXTransactionScope ts = new PXTransactionScope())
            {
                Running_Schedule.Cache.Persist(PXDBOperation.Update);
                ts.Complete(this);
            }

            Running_Schedule.Cache.Persisted(false);
        }
        public virtual APInvoice ReleaseRetainageProc(APInvoice doc, RetainageOptions retainageOpts, bool isAutoRelease = false)
        {
            Base.Clear(PXClearOption.PreserveTimeStamp);

            if (retainageOpts.CuryRetainageAmt <= 0 || retainageOpts.CuryRetainageAmt > doc.CuryRetainageUnreleasedAmt)
            {
                throw new PXException(Messages.IncorrectRetainageAmount);
            }

            // Magic. We need to prevent rewriting of CurrencyInfo.IsReadOnly
            // by true in CurrencyInfoView
            //
            Base.CurrentDocument.Cache.AllowUpdate = true;

            PXResult <APInvoice, CurrencyInfo, Terms, Vendor> resultDoc =
                APInvoice_CurrencyInfo_Terms_Vendor
                .SelectSingleBound(Base, null, doc.DocType, doc.RefNbr, doc.VendorID)
                .Cast <PXResult <APInvoice, CurrencyInfo, Terms, Vendor> >()
                .First();

            CurrencyInfo info        = resultDoc;
            APInvoice    origInvoice = resultDoc;
            Vendor       vendor      = resultDoc;

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

            new_info.CuryInfoID = null;
            new_info.IsReadOnly = false;
            new_info            = PXCache <CurrencyInfo> .CreateCopy(Base.currencyinfo.Insert(new_info));

            APInvoice invoice = PXCache <APInvoice> .CreateCopy(origInvoice);

            invoice.CuryInfoID = new_info.CuryInfoID;
            invoice.DocType    = APDocType.Invoice;
            invoice.RefNbr     = null;
            invoice.LineCntr   = null;
            invoice.InvoiceNbr = retainageOpts.InvoiceNbr;

            // Must be set for _RowSelected event handler
            //
            invoice.OpenDoc  = true;
            invoice.Released = false;

            Base.Document.Cache.SetDefaultExt <APInvoice.isMigratedRecord>(invoice);
            invoice.BatchNbr        = null;
            invoice.PrebookBatchNbr = null;
            invoice.Prebooked       = false;
            invoice.ScheduleID      = null;
            invoice.Scheduled       = false;
            invoice.NoteID          = null;

            invoice.DueDate         = null;
            invoice.DiscDate        = null;
            invoice.CuryOrigDiscAmt = 0m;
            invoice.OrigDocType     = origInvoice.DocType;
            invoice.OrigRefNbr      = origInvoice.RefNbr;
            invoice.OrigDocDate     = origInvoice.DocDate;

            invoice.PaySel              = false;
            invoice.CuryLineTotal       = 0m;
            invoice.IsTaxPosted         = false;
            invoice.IsTaxValid          = false;
            invoice.CuryVatTaxableTotal = 0m;
            invoice.CuryVatExemptTotal  = 0m;

            invoice.CuryDocBal     = 0m;
            invoice.CuryOrigDocAmt = retainageOpts.CuryRetainageAmt;
            invoice.Hold           = !isAutoRelease && Base.apsetup.Current.HoldEntry == true || Base.IsApprovalRequired(invoice, Base.Document.Cache);

            invoice.DocDate     = retainageOpts.DocDate;
            invoice.FinPeriodID = retainageOpts.FinPeriodID;

            Base.ClearRetainageSummary(invoice);
            invoice.RetainageApply      = false;
            invoice.IsRetainageDocument = true;

            invoice = Base.Document.Insert(invoice);

            if (new_info != null)
            {
                CurrencyInfo b_info = (CurrencyInfo)PXSelect <CurrencyInfo,
                                                              Where <CurrencyInfo.curyInfoID, Equal <Current <APInvoice.curyInfoID> > > > .Select(Base);

                b_info.CuryID         = new_info.CuryID;
                b_info.CuryEffDate    = new_info.CuryEffDate;
                b_info.CuryRateTypeID = new_info.CuryRateTypeID;
                b_info.CuryRate       = new_info.CuryRate;
                b_info.RecipRate      = new_info.RecipRate;
                b_info.CuryMultDiv    = new_info.CuryMultDiv;
                Base.currencyinfo.Update(b_info);
            }

            decimal retainagePercent = (decimal)(retainageOpts.CuryRetainageAmt / doc.CuryRetainageTotal);

            PXResultset <APTran> details = PXSelectGroupBy <APTran,
                                                            Where <APTran.tranType, Equal <Required <APTran.tranType> >,
                                                                   And <APTran.refNbr, Equal <Required <APTran.refNbr> >,
                                                                        And <APTran.curyRetainageAmt, NotEqual <decimal0> > > >,
                                                            Aggregate <
                                                                GroupBy <APTran.taxCategoryID,
                                                                         Sum <APTran.curyRetainageAmt> > > >
                                           .Select(Base, doc.DocType, doc.RefNbr);

            APTran  tranNew       = null;
            decimal prevCuryTotal = 0m;

            TaxCalc oldTaxCalc = TaxBaseAttribute.GetTaxCalc <APTran.taxCategoryID>(Base.Transactions.Cache, null);

            TaxBaseAttribute.SetTaxCalc <APTran.taxCategoryID>(Base.Transactions.Cache, null, TaxCalc.ManualCalc);

            foreach (APTran detail in details)
            {
                // Create APTran record for chosen retainage amount,
                // clear all required fields to prevent tax calculation,
                // discount calculation and retainage calculation.
                // CuryUnitCost = 0m and CuryLineAmt = 0m here to prevent their
                // FieldDefaulting events, because in our case default value
                // should be equal to zero.
                //
                tranNew = Base.Transactions.Insert(new APTran
                {
                    CuryUnitCost = 0m,
                    CuryLineAmt  = 0m
                });

                tranNew.BranchID      = origInvoice.BranchID;
                tranNew.TaxCategoryID = detail.TaxCategoryID;
                tranNew.AccountID     = origInvoice.RetainageAcctID;
                tranNew.SubID         = origInvoice.RetainageSubID;
                tranNew.ProjectID     = ProjectDefaultAttribute.NonProject();

                tranNew.Qty                  = 0m;
                tranNew.CuryUnitCost         = 0m;
                tranNew.ManualDisc           = true;
                tranNew.DiscPct              = 0m;
                tranNew.CuryDiscAmt          = 0m;
                tranNew.RetainagePct         = 0m;
                tranNew.CuryRetainageAmt     = 0m;
                tranNew.CuryTaxableAmt       = 0m;
                tranNew.CuryTaxAmt           = 0;
                tranNew.CuryExpenseAmt       = 0m;
                tranNew.GroupDiscountRate    = 1m;
                tranNew.DocumentDiscountRate = 1m;

                using (new PXLocaleScope(vendor.LocaleName))
                {
                    tranNew.TranDesc = PXMessages.LocalizeFormatNoPrefix(
                        Messages.RetainageForTransactionDescription,
                        APDocTypeDict[origInvoice.DocType],
                        origInvoice.RefNbr);
                }

                prevCuryTotal       = (retainageOpts.CuryRetainageAmt ?? 0m) - (invoice.CuryDocBal ?? 0m);
                tranNew.CuryLineAmt = PXCurrencyAttribute.RoundCury(Base.Transactions.Cache, tranNew, (detail.CuryRetainageAmt ?? 0m) * retainagePercent);
                tranNew             = Base.Transactions.Update(tranNew);
            }

            ClearCurrentDocumentDiscountDetails();

            // We should copy all taxes from the original document
            // because it is possible to add or delete them.
            //
            foreach (APTaxTran aptaxtran in PXSelect <APTaxTran,
                                                      Where <APTaxTran.module, Equal <BatchModule.moduleAP>,
                                                             And <APTaxTran.tranType, Equal <Required <APTaxTran.tranType> >,
                                                                  And <APTaxTran.refNbr, Equal <Required <APTaxTran.refNbr> > > > > >
                     .Select(Base, origInvoice.DocType, origInvoice.RefNbr)
                     .RowCast <APTaxTran>()
                     .Where(row => row.CuryRetainedTaxAmt != 0m))
            {
                APTaxTran new_aptaxtran = Base.Taxes.Insert(new APTaxTran
                {
                    TaxID = aptaxtran.TaxID
                });

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

                    new_aptaxtran.TaxRate = aptaxtran.TaxRate;
                    new_aptaxtran         = Base.Taxes.Update(new_aptaxtran);
                }
            }

            TaxBaseAttribute.SetTaxCalc <APTran.taxCategoryID>(Base.Transactions.Cache, null, oldTaxCalc);
            decimal diff = (retainageOpts.CuryRetainageAmt ?? 0m) - (invoice.CuryDocBal ?? 0m);

            if (tranNew != null && diff != 0m)
            {
                HashSet <string> taxList = PXSelectJoin <APTax,
                                                         InnerJoin <Tax, On <Tax.taxID, Equal <APTax.taxID> > >,
                                                         Where <APTax.tranType, Equal <Required <APTax.tranType> >,
                                                                And <APTax.refNbr, Equal <Required <APTax.refNbr> >,
                                                                     And <APTax.lineNbr, Equal <Required <APTax.lineNbr> >,
                                                                          And <Tax.taxType, NotEqual <CSTaxType.use> > > > > >
                                           .Select(Base, tranNew.TranType, tranNew.RefNbr, tranNew.LineNbr)
                                           .RowCast <APTax>()
                                           .Select(row => row.TaxID)
                                           .ToHashSet();

                // To guarantee correct document total amount
                // we should calculate last line total,
                // including its taxes.
                //
                TaxAttribute.CalcTaxable calcClass = new TaxAttribute.CalcTaxable(false, TaxAttribute.TaxCalcLevelEnforcing.None);
                decimal curyLineAmt = calcClass.CalcTaxableFromTotalAmount(
                    Base.Transactions.Cache,
                    tranNew,
                    taxList,
                    invoice.DocDate.Value,
                    prevCuryTotal);

                tranNew.CuryLineAmt = curyLineAmt;
                tranNew             = Base.Transactions.Update(tranNew);
            }

            APVendorRefNbrAttribute aPVendorRefNbrAttribute = Base.Document.Cache.GetAttributesReadonly <APInvoice.invoiceNbr>()
                                                              .OfType <APVendorRefNbrAttribute>().FirstOrDefault();

            if (aPVendorRefNbrAttribute != null)
            {
                var args = new PXFieldVerifyingEventArgs(invoice, invoice.InvoiceNbr, true);
                aPVendorRefNbrAttribute.FieldVerifying(Base.Document.Cache, args);
            }

            return(invoice);
        }
        protected virtual GetTaxRequest BuildTaxRequest(APInvoice invoice)
        {
            if (invoice == null)
            {
                throw new PXArgumentException(nameof(invoice), ErrorMessages.ArgumentNullException);
            }

            Vendor vend = (Vendor)Base.vendor.View.SelectSingleBound(new object[] { invoice });

            var request = new PX.TaxProvider.GetTaxRequest();

            request.CompanyCode  = CompanyCodeFromBranch(invoice.TaxZoneID, invoice.BranchID);
            request.CurrencyCode = invoice.CuryID;
            request.CustomerCode = vend.AcctCD;
            IAddressBase fromAddress = GetFromAddress(invoice);
            IAddressBase toAddress   = GetToAddress(invoice);

            if (fromAddress == null)
            {
                throw new PXException(Messages.FailedGetFrom);
            }

            if (toAddress == null)
            {
                throw new PXException(Messages.FailedGetTo);
            }

            request.OriginAddress      = AddressConverter.ConvertTaxAddress(fromAddress);
            request.DestinationAddress = AddressConverter.ConvertTaxAddress(toAddress);
            request.DocCode            = $"AP.{invoice.DocType}.{invoice.RefNbr}";
            request.DocDate            = invoice.DocDate.GetValueOrDefault();
            request.LocationCode       = GetExternalTaxProviderLocationCode(invoice);

            Location branchLoc = GetBranchLocation(invoice);

            if (branchLoc != null)
            {
                request.CustomerUsageType = branchLoc.CAvalaraCustomerUsageType;
                request.ExemptionNo       = branchLoc.CAvalaraExemptionNumber;
            }

            request.DocType = GetTaxDocumentType(invoice);
            Sign sign = GetDocumentSign(invoice);

            PXSelectBase <APTran> select = new PXSelectJoin <APTran,
                                                             LeftJoin <InventoryItem, On <InventoryItem.inventoryID, Equal <APTran.inventoryID> >,
                                                                       LeftJoin <Account, On <Account.accountID, Equal <APTran.accountID> > > >,
                                                             Where <APTran.tranType, Equal <Current <APInvoice.docType> >,
                                                                    And <APTran.refNbr, Equal <Current <APInvoice.refNbr> >,
                                                                         And <APTran.lineType, NotEqual <SOLineType.discount> > > >,
                                                             OrderBy <Asc <APTran.tranType, Asc <APTran.refNbr, Asc <APTran.lineNbr> > > > >(Base);

            request.Discount = GetDocDiscount().GetValueOrDefault();
            foreach (PXResult <APTran, InventoryItem, Account> res in select.View.SelectMultiBound(new object[] { invoice }))
            {
                APTran        tran         = (APTran)res;
                InventoryItem item         = (InventoryItem)res;
                Account       salesAccount = (Account)res;

                var line = new TaxCartItem();
                line.Index              = tran.LineNbr.GetValueOrDefault();
                line.Amount             = sign * tran.CuryTranAmt.GetValueOrDefault();
                line.Description        = tran.TranDesc;
                line.OriginAddress      = AddressConverter.ConvertTaxAddress(GetFromAddress(invoice, tran));
                line.DestinationAddress = AddressConverter.ConvertTaxAddress(GetToAddress(invoice, tran));
                line.ItemCode           = item.InventoryCD;
                line.Quantity           = Math.Abs(tran.Qty.GetValueOrDefault());
                line.Discounted         = request.Discount > 0;
                line.RevAcct            = salesAccount.AccountCD;

                line.TaxCode = tran.TaxCategoryID;

                request.CartItems.Add(line);
            }

            if (invoice.DocType == APDocType.DebitAdj && (invoice.OrigDocDate != null))
            {
                request.TaxOverride.Reason          = Messages.DebitAdjustmentReason;
                request.TaxOverride.TaxDate         = invoice.OrigDocDate.Value;
                request.TaxOverride.TaxOverrideType = PX.TaxProvider.TaxOverrideType.TaxDate;
            }

            return(request);
        }
 public virtual bool CompareReferenceKey(APTran aTran)
 {
     return(aTran.POAccrualType == this.POAccrualType &&
            aTran.POAccrualRefNoteID == this.POAccrualRefNoteID &&
            aTran.POAccrualLineNbr == this.POAccrualLineNbr);
 }
예제 #10
0
        public virtual void GenerateProc(Schedule s, short Times, DateTime runDate)
        {
            List <ScheduleDet> sd = GL.ScheduleProcess.MakeSchedule(this, s, Times, runDate);

            APInvoiceEntry docgraph = CreateGraph();

            foreach (ScheduleDet sdet in sd)
            {
                foreach (PXResult <APInvoice, Vendor, CurrencyInfo> res in PXSelectJoin <APInvoice, InnerJoin <Vendor, On <Vendor.bAccountID, Equal <APInvoice.vendorID> >, InnerJoin <CurrencyInfo, On <CurrencyInfo.curyInfoID, Equal <APInvoice.curyInfoID> > > >, Where <APInvoice.scheduleID, Equal <Required <APInvoice.scheduleID> >, And <APInvoice.scheduled, Equal <boolTrue> > > > .Select(this, s.ScheduleID))
                {
                    docgraph.Clear();
                    docgraph.vendor.Current = (Vendor)res;
                    APInvoice    apdoc = (APInvoice)res;
                    CurrencyInfo info  = (CurrencyInfo)res;

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

                    new_info.CuryInfoID = null;
                    new_info            = docgraph.currencyinfo.Insert(new_info);

                    APInvoice new_apdoc = PXCache <APInvoice> .CreateCopy(apdoc);

                    new_apdoc.CuryInfoID          = new_info.CuryInfoID;
                    new_apdoc.DocDate             = sdet.ScheduledDate;
                    new_apdoc.FinPeriodID         = sdet.ScheduledPeriod;
                    new_apdoc.TranPeriodID        = null;
                    new_apdoc.DueDate             = null;
                    new_apdoc.DiscDate            = null;
                    new_apdoc.PayDate             = null;
                    new_apdoc.CuryOrigDiscAmt     = null;
                    new_apdoc.OrigDiscAmt         = null;
                    new_apdoc.RefNbr              = null;
                    new_apdoc.Scheduled           = false;
                    new_apdoc.CuryLineTotal       = 0m;
                    new_apdoc.CuryVatTaxableTotal = 0m;
                    new_apdoc.CuryVatExemptTotal  = 0m;
                    new_apdoc.NoteID              = null;
                    new_apdoc = docgraph.Document.Insert(new_apdoc);

                    TaxAttribute.SetTaxCalc <APTran.taxCategoryID, APTaxAttribute>(docgraph.Transactions.Cache, null, TaxCalc.ManualCalc);
                    PXNoteAttribute.SetNote(docgraph.Document.Cache, new_apdoc, PXNoteAttribute.GetNote(Caches[typeof(APInvoice)], apdoc));
                    PXNoteAttribute.SetFileNotes(docgraph.Document.Cache, new_apdoc, PXNoteAttribute.GetFileNotes(Caches[typeof(APInvoice)], apdoc));

                    foreach (APTran aptran in PXSelect <APTran, Where <APTran.tranType, Equal <Required <APTran.tranType> >, And <APTran.refNbr, Equal <Required <APTran.refNbr> > > > > .Select(docgraph, apdoc.DocType, apdoc.RefNbr))
                    {
                        APTran new_aptran = PXCache <APTran> .CreateCopy(aptran);

                        new_aptran.RefNbr     = null;
                        new_aptran.CuryInfoID = null;
                        docgraph.Transactions.Insert(new_aptran);
                    }

                    foreach (APTaxTran tax in PXSelect <APTaxTran, Where <APTaxTran.tranType, Equal <Required <APTaxTran.tranType> >, And <APTaxTran.refNbr, Equal <Required <APTaxTran.refNbr> > > > > .Select(docgraph, apdoc.DocType, apdoc.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);
                        }
                    }
                    docgraph.Save.Press();
                }
                s.LastRunDate = sdet.ScheduledDate;
                Running_Schedule.Cache.Update(s);
            }

            using (PXTransactionScope ts = new PXTransactionScope())
            {
                Running_Schedule.Cache.Persist(PXDBOperation.Update);
                ts.Complete(this);
            }
            Running_Schedule.Cache.Persisted(false);
        }