protected virtual ARInvoice InsertDocument(
            ARInvoiceEntry invoiceEntry,
            ScheduleDet occurrence,
            Customer customer,
            ARInvoice scheduledInvoice,
            CurrencyInfo scheduledInvoiceCurrencyInfo)
        {
            if (scheduledInvoice.Released == true)
            {
                throw new PXException(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);

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

            newInvoice.CuryInfoID = newCurrencyInfo.CuryInfoID;
            newInvoice.DocDate    = occurrence.ScheduledDate;

            FinPeriod finPeriod =
                FinPeriodRepository.GetFinPeriodByMasterPeriodID(PXAccess.GetParentOrganizationID(newInvoice.BranchID), occurrence.ScheduledPeriod)
                .GetValueOrRaiseError();

            newInvoice.FinPeriodID = finPeriod.FinPeriodID;

            newInvoice.TranPeriodID        = null;
            newInvoice.DueDate             = null;
            newInvoice.DiscDate            = 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.IsTaxValid          = false;
            newInvoice.IsTaxPosted         = false;
            newInvoice.IsTaxSaved          = false;
            newInvoice.OrigDocType         = scheduledInvoice.DocType;
            newInvoice.OrigRefNbr          = scheduledInvoice.RefNbr;

            invoiceEntry.Document.Cache.SetDefaultExt <ARInvoice.printed>(newInvoice);
            invoiceEntry.Document.Cache.SetDefaultExt <ARInvoice.emailed>(newInvoice);

            bool forceClear         = false;
            bool clearPaymentMethod = false;

            if (newInvoice.PMInstanceID.HasValue)
            {
                PXResult <CustomerPaymentMethod, PaymentMethod> paymentMethodResult = (PXResult <CustomerPaymentMethod, PaymentMethod>)
                                                                                      PXSelectJoin <
                    CustomerPaymentMethod,
                    InnerJoin <PaymentMethod,
                               On <PaymentMethod.paymentMethodID, Equal <CustomerPaymentMethod.paymentMethodID> > >,
                    Where <
                        CustomerPaymentMethod.pMInstanceID, Equal <Required <CustomerPaymentMethod.pMInstanceID> > > >
                                                                                      .Select(invoiceEntry, newInvoice.PMInstanceID);

                if (paymentMethodResult != null)
                {
                    CustomerPaymentMethod customerPaymentMethod = paymentMethodResult;
                    PaymentMethod         paymentMethod         = paymentMethodResult;
                    if (customerPaymentMethod == null || customerPaymentMethod.IsActive != true || paymentMethod.IsActive != true || paymentMethod.UseForAR != true)
                    {
                        clearPaymentMethod = true;
                        forceClear         = true;
                    }
                }
                else
                {
                    clearPaymentMethod = true;
                    forceClear         = true;
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(newInvoice.PaymentMethodID))
                {
                    PaymentMethod paymentMethod = PXSelect <
                        PaymentMethod,
                        Where <PaymentMethod.paymentMethodID, Equal <Required <PaymentMethod.paymentMethodID> > > >
                                                  .Select(invoiceEntry, newInvoice.PaymentMethodID);

                    if (paymentMethod == null || paymentMethod.IsActive != true || paymentMethod.UseForAR != true)
                    {
                        clearPaymentMethod = true;
                        forceClear         = true;
                    }
                }
            }

            if (clearPaymentMethod)
            {
                newInvoice.PMInstanceID    = null;
                newInvoice.PaymentMethodID = null;
                newInvoice.CashAccountID   = null;
            }

            invoiceEntry.ClearRetainageSummary(newInvoice);

            newInvoice = invoiceEntry.Document.Insert(newInvoice);

            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);

            // Force credit rule back
            // -
            invoiceEntry.customer.Current = customer;

            if (forceClear == true)
            {
                ARInvoice copy = PXCache <ARInvoice> .CreateCopy(newInvoice);

                copy.PMInstanceID    = null;
                copy.PaymentMethodID = null;
                copy.CashAccountID   = null;
                newInvoice           = invoiceEntry.Document.Update(copy);
            }

            AddressAttribute.CopyRecord <ARInvoice.billAddressID>(invoiceEntry.Document.Cache, newInvoice, scheduledInvoice, false);
            ContactAttribute.CopyRecord <ARInvoice.billContactID>(invoiceEntry.Document.Cache, newInvoice, scheduledInvoice, false);

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

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

            return(newInvoice);
        }
Exemple #2
0
        public virtual void GenerateProc(Schedule s, short Times, DateTime runDate)
        {
            List <ScheduleDet> sd = GL.ScheduleProcess.MakeSchedule(this, s, Times, runDate);

            ARInvoiceEntry docgraph = CreateGraph();

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

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

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

                    ARInvoice new_ardoc = PXCache <ARInvoice> .CreateCopy(apdoc);

                    new_ardoc.CuryInfoID          = new_info.CuryInfoID;
                    new_ardoc.DocDate             = sdet.ScheduledDate;
                    new_ardoc.FinPeriodID         = sdet.ScheduledPeriod;
                    new_ardoc.TranPeriodID        = null;
                    new_ardoc.DueDate             = null;
                    new_ardoc.DiscDate            = null;
                    new_ardoc.CuryOrigDiscAmt     = null;
                    new_ardoc.OrigDiscAmt         = null;
                    new_ardoc.RefNbr              = null;
                    new_ardoc.Scheduled           = false;
                    new_ardoc.CuryLineTotal       = 0m;
                    new_ardoc.CuryVatTaxableTotal = 0m;
                    new_ardoc.CuryVatExemptTotal  = 0m;
                    new_ardoc.NoteID              = null;
                    bool forceClear = false;
                    bool clearPM    = false;
                    if (new_ardoc.PMInstanceID.HasValue)
                    {
                        PXResult <CustomerPaymentMethod, CA.PaymentMethod> pmiResult = (PXResult <CustomerPaymentMethod, CA.PaymentMethod>) PXSelectJoin <CustomerPaymentMethod, InnerJoin <CA.PaymentMethod, On <CA.PaymentMethod.paymentMethodID, Equal <CustomerPaymentMethod.paymentMethodID> > >, Where <CustomerPaymentMethod.pMInstanceID, Equal <Required <CustomerPaymentMethod.pMInstanceID> > > > .Select(docgraph, new_ardoc.PMInstanceID);

                        if (pmiResult != null)
                        {
                            CustomerPaymentMethod pmInstance    = pmiResult;
                            CA.PaymentMethod      paymentMethod = pmiResult;
                            if (pmInstance == null || pmInstance.IsActive != true || paymentMethod.IsActive != true || paymentMethod.UseForAR != true)
                            {
                                clearPM    = true;
                                forceClear = true;
                            }
                        }
                        else
                        {
                            clearPM    = true;
                            forceClear = true;
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(new_ardoc.PaymentMethodID) == false)
                        {
                            CA.PaymentMethod pm = PXSelect <CA.PaymentMethod, Where <CA.PaymentMethod.paymentMethodID, Equal <Required <CA.PaymentMethod.paymentMethodID> > > > .Select(docgraph, new_ardoc.PaymentMethodID);

                            if (pm == null || pm.IsActive != true || pm.UseForAR != true)
                            {
                                clearPM    = true;
                                forceClear = true;
                            }
                        }
                    }

                    if (clearPM)
                    {
                        new_ardoc.PMInstanceID    = null;
                        new_ardoc.PaymentMethodID = null;
                        new_ardoc.CashAccountID   = null;
                    }

                    new_ardoc = docgraph.Document.Insert(new_ardoc);

                    //force creditrule back
                    docgraph.customer.Current = (Customer)res;

                    if (forceClear == true)
                    {
                        ARInvoice copy = PXCache <ARInvoice> .CreateCopy(new_ardoc);

                        copy.PMInstanceID    = null;
                        copy.PaymentMethodID = null;
                        copy.CashAccountID   = null;
                        new_ardoc            = docgraph.Document.Update(copy);
                    }
                    AddressAttribute.CopyRecord <ARInvoice.billAddressID>(docgraph.Document.Cache, new_ardoc, apdoc, false);
                    ContactAttribute.CopyRecord <ARInvoice.billContactID>(docgraph.Document.Cache, new_ardoc, apdoc, false);

                    TaxAttribute.SetTaxCalc <ARTran.taxCategoryID, ARTaxAttribute>(docgraph.Transactions.Cache, null, TaxCalc.ManualCalc);
                    PXNoteAttribute.SetNote(docgraph.Document.Cache, new_ardoc, PXNoteAttribute.GetNote(Caches[typeof(ARInvoice)], apdoc));
                    PXNoteAttribute.SetFileNotes(docgraph.Document.Cache, new_ardoc, PXNoteAttribute.GetFileNotes(Caches[typeof(ARInvoice)], apdoc));

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

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

                    foreach (ARTaxTran tax in PXSelect <ARTaxTran, Where <ARTaxTran.tranType, Equal <Required <ARTaxTran.tranType> >, And <ARTaxTran.refNbr, Equal <Required <ARTaxTran.refNbr> > > > > .Select(docgraph, apdoc.DocType, apdoc.RefNbr))
                    {
                        ARTaxTran new_artax = new ARTaxTran();
                        new_artax.TaxID = tax.TaxID;

                        new_artax = docgraph.Taxes.Insert(new_artax);

                        if (new_artax != null)
                        {
                            new_artax = PXCache <ARTaxTran> .CreateCopy(new_artax);

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

                    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);
        }