protected virtual void SOCreateFilter_RowSelected(PXCache sender, PXRowSelectedEventArgs e)
        {
            SOCreateFilter filter = Filter.Current;

            FixedDemand.SetProcessDelegate(list => SOCreateProc(list, filter.PurchDate));

            TimeSpan        span;
            Exception       message;
            PXLongRunStatus status = PXLongOperation.GetStatus(this.UID, out span, out message);

            PXUIFieldAttribute.SetVisible <SOLine.orderNbr>(Caches[typeof(SOLine)], null, (status == PXLongRunStatus.Completed || status == PXLongRunStatus.Aborted));

            if (PXAccess.FeatureInstalled <FeaturesSet.warehouse>())
            {
                INSite toxicsite = PXSelect <INSite,
                                             Where <INSite.siteID, Equal <Current <SOCreateFilter.siteID> >,
                                                    And <INSite.active, Equal <True>,
                                                         And <Where <INSite.addressID, IsNull, Or <INSite.contactID, IsNull> > > > > > .SelectSingleBound(this, new object[] { e.Row });

                if (toxicsite != null)
                {
                    throw new PXSetupNotEnteredException <INSite, INSite.siteCD>(Messages.WarehouseWithoutAddressAndContact, toxicsite.SiteCD, toxicsite.SiteCD);
                }
            }
        }
Exemple #2
0
        protected virtual void _(Events.FieldUpdated <InventoryItem, InventoryItem.dfltSiteID> eventArgs)
        {
            INSite site = INSite.PK.Find(this, ((InventoryItem)eventArgs.Row).DfltSiteID);

            eventArgs.Row.DfltShipLocationID    = site?.ShipLocationID;
            eventArgs.Row.DfltReceiptLocationID = site?.ReceiptLocationID;
        }
Exemple #3
0
        public virtual void FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
        {
            if (e.Cancel || e.Row == null)
            {
                return;
            }
            object obj = sender.GetValue(e.Row, this._inventoryType.Name);

            if (obj == null)
            {
                return;
            }
            InventoryItem inventoryItem = InventoryItem.PK.Find(sender.Graph, new int?((int)obj));

            if (!inventoryItem.DfltSiteID.HasValue)
            {
                object branchId = sender.GetValue(e.Row, this._branchType.Name);
                if (branchId == null)
                {
                    CROpportunityProducts current = (CROpportunityProducts)sender.Current;
                    if (current != null)
                    {
                        branchId = (object)SelectFrom <CROpportunity> .Where <CROpportunity.quoteNoteID.IsEqual <P.AsGuid> > .View.SelectSingleBound(sender.Graph, null, (object)current.QuoteID).TopFirst.BranchID;
                    }
                }
                PX.Objects.CR.Location location = (PX.Objects.CR.Location)SelectFrom <PX.Objects.CR.Location> .InnerJoin <PX.Objects.GL.Branch> .On <PX.Objects.GL.Branch.bAccountID.IsEqual <PX.Objects.CR.Location.bAccountID> >
                                                  .Where <PX.Objects.GL.Branch.branchID.IsEqual <P.AsInt> > .View.ReadOnly.Select(sender.Graph, branchId);

                if (location == null)
                {
                    return;
                }
                e.NewValue = (object)location.CSiteID;
            }
            else
            {
                INSite inSite = (INSite)PXSelectBase <INSite, PXSelectReadonly <INSite, Where <INSite.siteID, Equal <Required <INSite.siteID> >, And <Match <INSite, Current <AccessInfo.userName> > > > > .Config> .Select(sender.Graph, (object)inventoryItem.DfltSiteID);

                if (inSite == null)
                {
                    return;
                }
                e.NewValue = (object)inSite.SiteID;
            }
        }
        public KCSiteMultiSelectListAttribute() : base()
        {
            PXResultset <INSite> rslt = PXSelect <INSite, Where <INSite.siteID, NotEqual <SiteAttribute.transitSiteID> > > .Select(new PXGraph());

            List <string> values = new List <string>();
            List <string> labels = new List <string>();

            foreach (PXResult <INSite> item in rslt)
            {
                INSite e = (INSite)item;
                values.Add(e.SiteID.ToString().Trim());
                labels.Add(e.SiteCD);
            }

            this._AllowedValues = values.ToArray();
            this._AllowedLabels = labels.ToArray();
            MultiSelect         = true;
        }
        protected virtual CarrierRequest BuildRateRequest(Document doc)
        {
            if (string.IsNullOrEmpty(doc.ShipVia))
            {
                return(null);
            }

            Carrier carrier = Carrier.PK.Find(Base, doc.ShipVia);

            if (carrier == null)
            {
                throw new PXException(Messages.CarrierWithIdNotFound);
            }

            if (carrier.IsExternal != true)
            {
                return(null);
            }

            CarrierPlugin plugin = CarrierPlugin.PK.Find(Base, carrier.CarrierPluginID);

            ValidatePlugin(plugin);

            List <string> methods = GetCarrierMethods(plugin.CarrierPluginID);

            IList <SOPackageEngine.PackSet> packSets = GetPackages(doc, suppressRecalc: true);

            if (packSets.Count == 0)
            {
                throw new PXException(Messages.AtleastOnePackageIsRequired);
            }

            List <CarrierBoxEx> boxes = new List <CarrierBoxEx>();

            foreach (SOPackageEngine.PackSet packSet in packSets)
            {
                INSite  shipToWarehouse  = INSite.PK.Find(Base, packSet.SiteID);
                Address warehouseAddress = PXSelect <Address, Where <Address.addressID, Equal <Required <Address.addressID> > > > .Select(Base, shipToWarehouse.AddressID);

                boxes.Add(BuildCarrierBoxes(packSet.Packages, warehouseAddress, carrier.PluginMethod, plugin));
            }

            return(GetCarrierRequest(doc, GetUnitsType(plugin), methods, boxes));
        }
        protected virtual CarrierRequest BuildQuoteRequest(Document doc, CarrierPlugin plugin)
        {
            ValidatePlugin(plugin);

            List <string> methods = GetCarrierMethods(plugin.CarrierPluginID);

            List <CarrierBoxEx> boxes = new List <CarrierBoxEx>();

            foreach (string method in methods)
            {
                IList <SOPackageEngine.PackSet> packSets = GetPackages(doc);

                foreach (SOPackageEngine.PackSet packSet in packSets)
                {
                    INSite  shipToWarehouse  = INSite.PK.Find(Base, packSet.SiteID);
                    Address warehouseAddress = PXSelect <Address, Where <Address.addressID, Equal <Required <Address.addressID> > > > .Select(Base, shipToWarehouse.AddressID);

                    boxes.Add(BuildCarrierBoxes(packSet.Packages, warehouseAddress, method, plugin));
                }
            }

            return(GetCarrierRequest(doc, GetUnitsType(plugin), methods, boxes));
        }
        public virtual void CreateAdjustmentTran(List <AllocationServiceBase.POReceiptLineAdjustment> pOLinesToProcess, string ReasonCode)
        {
            foreach (AllocationServiceBase.POReceiptLineAdjustment poreceiptline in pOLinesToProcess)
            {
                INTran intran = new INTran();

                INTran origtran   = PurchasePriceVarianceAllocationService.Instance.GetOriginalInTran(_inGraph, poreceiptline.ReceiptLine.ReceiptNbr, poreceiptline.ReceiptLine.LineNbr);
                bool   isDropShip = (poreceiptline.ReceiptLine.LineType == POLineType.GoodsForDropShip || poreceiptline.ReceiptLine.LineType == POLineType.NonStockForDropShip);

                if (!isDropShip && origtran == null)
                {
                    throw new PXException(AP.Messages.CannotFindINReceipt, poreceiptline.ReceiptLine.ReceiptNbr);
                }

                //Drop-Ships are considered non-stocks
                if (poreceiptline.ReceiptLine.IsStockItem())
                {
                    intran.TranType = INTranType.ReceiptCostAdjustment;
                }
                else
                {
                    //Landed cost and PPV for non-stock items are handled in special way in the inventory.
                    //They should create a GL Batch, but for convinience and unforminty this functionality is placed into IN module
                    //Review this part when the functionality is implemented in IN module
                    intran.IsCostUnmanaged = true;
                    intran.TranType        = INTranType.Adjustment;
                    intran.InvtMult        = 0;
                }
                intran.InventoryID = poreceiptline.ReceiptLine.InventoryID;
                intran.SubItemID   = poreceiptline.ReceiptLine.SubItemID;
                intran.SiteID      = poreceiptline.ReceiptLine.SiteID;
                intran.BAccountID  = poreceiptline.ReceiptLine.VendorID;
                intran.BranchID    = poreceiptline.BranchID;


                if (isDropShip && intran.SiteID != null)
                {
                    INSite invSite = PXSelect <INSite, Where <INSite.siteID, Equal <Required <POReceiptLine.siteID> > > > .Select(_inGraph, intran.SiteID);

                    if (invSite.DropShipLocationID == null)
                    {
                        throw new PXException(SO.Messages.NoDropShipLocation, invSite.SiteCD);
                    }

                    intran.LocationID = invSite.DropShipLocationID;
                }
                else
                {
                    intran.LocationID = poreceiptline.ReceiptLine.LocationID ?? origtran.LocationID;
                }

                intran.LotSerialNbr     = poreceiptline.ReceiptLine.LotSerialNbr;
                intran.POReceiptType    = poreceiptline.ReceiptLine.ReceiptType;
                intran.POReceiptNbr     = poreceiptline.ReceiptLine.ReceiptNbr;
                intran.POReceiptLineNbr = poreceiptline.ReceiptLine.LineNbr;

                //tran.Qty = poreceiptline.ReceiptQty;
                intran.TranDesc = poreceiptline.ReceiptLine.TranDesc;
                //tran.UnitCost = PXDBPriceCostAttribute.Round(inGraph.transactions.Cache, (decimal)(poreceiptline.ExtCost / poreceiptline.ReceiptQty));
                intran.TranCost   = poreceiptline.QtyToAssign;
                intran.ReasonCode = ReasonCode;
                if (origtran != null && origtran.DocType == INDocType.Issue)
                {
                    intran.ARDocType = origtran.ARDocType;
                    intran.ARRefNbr  = origtran.ARRefNbr;
                    intran.ARLineNbr = origtran.ARLineNbr;
                }
                if (!isDropShip)
                {
                    intran.OrigTranType = origtran.DocType;
                    intran.OrigRefNbr   = origtran.RefNbr;
                }

                int?acctID = null;
                int?subID  = null;

                //Set AcctID and SubID = POAccrual Acct/Sub from orig. INTran
                if (origtran != null)
                {
                    intran.AcctID = origtran.AcctID;
                    intran.SubID  = origtran.SubID;
                }
                else if (isDropShip)
                {
                    intran.AcctID = poreceiptline.ReceiptLine.POAccrualAcctID;
                    intran.SubID  = poreceiptline.ReceiptLine.POAccrualSubID;
                }
                ReasonCode reasonCode = PXSelect <ReasonCode, Where <ReasonCode.reasonCodeID, Equal <Required <ReasonCode.reasonCodeID> > > > .Select(_inGraph, ReasonCode);

                if (reasonCode == null)
                {
                    throw new PXException(AP.Messages.ReasonCodeCannotNotFound, ReasonCode);
                }
                AP.APReleaseProcess.GetPPVAccountSub(ref acctID, ref subID, _inGraph, poreceiptline.ReceiptLine, reasonCode);

                intran.COGSAcctID = acctID;
                intran.COGSSubID  = subID;

                intran = _inGraph.transactions.Insert(intran);
            }
        }
        public void GenerateSalesOrders(string JsonPackage, string mailFrom, DateTime orderDate)
        {
            try
            {
                SOOrderEntry soGraph = PXGraph.CreateInstance <SOOrderEntry>();
                soGraph.Clear();

                PXTrace.WriteInformation("SO Order Graph instantiated.");

                #region Variables

                JObject package = JObject.Parse(JsonPackage);

                int     countLines = 0;
                decimal qty        = 0.0M;

                //Customer
                Customer customer = PXSelect <Customer,
                                              Where <Customer.acctCD, Equal <Customer.acctCD> > > .
                                    Select(soGraph, "ABCHOLDING");


                //Customer Default Location
                Location arCustomerLocation = PXSelect <Location,
                                                        Where <Location.isActive, Equal <Required <Location.isActive> >,
                                                               And <Location.bAccountID, Equal <Required <Location.bAccountID> >,
                                                                    And <Location.locationCD, Equal <Required <Location.locationCD> > > > > > .
                                              Select(soGraph, true, customer.BAccountID, "MAIN");

                PXTrace.WriteInformation("SO Order Graph variables set.");
                #endregion

                //Header
                bool    hasError = false;
                string  ms       = "";
                SOOrder order    = new SOOrder();
                order.OrderType = "QT";

                try
                {
                    order = soGraph.Document.Insert(order);

                    soGraph.Document.Cache.SetValueExt <SOOrder.customerID>(order, customer.BAccountID);
                    soGraph.Document.Cache.SetValueExt <SOOrder.customerLocationID>(order, arCustomerLocation.LocationID);
                    soGraph.Document.Cache.SetValueExt <SOOrder.customerOrderNbr>(order, "");
                    soGraph.Document.Cache.SetValueExt <SOOrder.orderDate>(order, DateTime.Now);
                    soGraph.Document.Cache.SetValueExt <SOOrder.requestDate>(order, orderDate);

                    //Update Order
                    order = soGraph.Document.Update(order);
                }
                catch (Exception er)
                {
                    hasError = true;
                    ms       = er.Message;
                }

                if (hasError == false)
                {
                    PXTrace.WriteInformation("SO Order Header added. Starting rows. ");

                    //Details

                    SuperJSon     superJSon     = JsonConvert.DeserializeObject <SuperJSon>(package.ToString());
                    List <string> inventorydesc = new List <string>();
                    int           Myqty         = 0;
                    foreach (var item in superJSon.compositeEntities)
                    {
                        if (item.children[0].type == "product")
                        {
                            inventorydesc.Add(item.children[0].value);
                        }
                        else
                        {
                            Myqty = Convert.ToInt32(item.children[0].value);
                        }
                    }

                    //    JObject saleLine = JObject.Parse(sl);

                    var inItem = new PXSelect <InventoryItem>(this).Select().AsQueryable();

                    foreach (string item in inventorydesc)
                    {
                        inItem = inItem.Where(i => i.GetItem <InventoryItem>().Descr.Contains(item));
                    }

                    InventoryItem ResultInventory = inItem.First();


                    INSite warehouse = PXSelectReadonly <INSite,
                                                         Where <INSite.siteCD, Equal <Required <INSite.siteCD> > > > .
                                       Select(soGraph, "WHOLESALE");


                    if (inItem != null)
                    {
                        try
                        {
                            SOLine orderLine = soGraph.Transactions.Insert();

                            soGraph.Transactions.Cache.SetValueExt <SOLine.inventoryID>(orderLine, ResultInventory.InventoryID);
                            soGraph.Transactions.Cache.SetValueExt <SOLine.siteID>(orderLine, warehouse.SiteID);

                            qty = Convert.ToDecimal(Myqty);

                            soGraph.Transactions.Cache.SetValueExt <SOLine.orderQty>(orderLine, qty);

                            orderLine = soGraph.Transactions.Update(orderLine);

                            countLines++;
                        }
                        catch (Exception)
                        {
                            hasError = true;  //Failed
                        }
                    }
                    else
                    {
                        hasError = true;  //Failed
                    }
                }

                PXTrace.WriteInformation("SO Order Details added. ");

                if (hasError == false)
                {
                    //ADD EMAIL


                    order = soGraph.Document.Update(order);
                    try
                    {
                        soGraph.Actions.PressSave();
                        PXTrace.WriteInformation("Order [" + soGraph.Document.Current.RefNbr + "] created ");
                    }
                    catch (PXException pEx)
                    {
                        PXTrace.WriteInformation(pEx.Message + " " + pEx.InnerException);
                    }
                }
                else
                {
                    PXTrace.WriteInformation("Errors on order, could not save. ");
                }
            }
            catch (Exception e)
            {
                PXTrace.WriteInformation(e.Message);
            }
        }
Exemple #9
0
        //Graph passed in function must be the one using newly created sub in details - otherwise save will fail (inserted subs will be created in other graph)
        protected static void GetLCVarianceAccountSub(ref int?aAccountID, ref int?aSubID, PXGraph aGraph, POReceiptLine aRow)
        {
            if (aRow.InventoryID.HasValue)
            {
                PXResult <InventoryItem, INPostClass> res = (PXResult <InventoryItem, INPostClass>) PXSelectJoin <InventoryItem,
                                                                                                                  LeftJoin <INPostClass, On <INPostClass.postClassID, Equal <InventoryItem.postClassID> > >,
                                                                                                                  Where <InventoryItem.inventoryID, Equal <Required <POLine.inventoryID> > > > .Select(aGraph, aRow.InventoryID);

                if (res != null)
                {
                    InventoryItem invItem   = (InventoryItem)res;
                    INPostClass   postClass = (INPostClass)res;
                    if ((bool)invItem.StkItem)
                    {
                        if (postClass == null)
                        {
                            throw new PXException(Messages.PostingClassIsNotDefinedForTheItemInReceiptRow, invItem.InventoryCD, aRow.ReceiptNbr, aRow.LineNbr);
                        }
                        INSite invSite = null;
                        if (aRow.SiteID != null)
                        {
                            invSite = PXSelect <INSite, Where <INSite.siteID, Equal <Required <POReceiptLine.siteID> > > > .Select(aGraph, aRow.SiteID);
                        }
                        if (aRow.LineType == POLineType.GoodsForDropShip)
                        {
                            aAccountID = INReleaseProcess.GetAcctID <INPostClass.cOGSAcctID>(aGraph, postClass.COGSAcctDefault, invItem, invSite, postClass);
                            if (aAccountID == null)
                            {
                                throw new PXException(Messages.COGSAccountCanNotBeFoundForItemInReceiptRow, invItem.InventoryCD, aRow.ReceiptNbr, aRow.LineNbr, postClass.PostClassID, invSite != null ? invSite.SiteCD : String.Empty);
                            }
                            try
                            {
                                aSubID = INReleaseProcess.GetSubID <INPostClass.cOGSSubID>(aGraph, postClass.COGSAcctDefault, postClass.COGSSubMask, invItem, invSite, postClass);
                            }
                            catch (PXException ex)
                            {
                                if (postClass.COGSSubID == null ||
                                    string.IsNullOrEmpty(postClass.COGSSubMask) ||
                                    invItem.COGSSubID == null || invSite == null || invSite.COGSSubID == null)
                                {
                                    throw new PXException(Messages.COGSSubAccountCanNotBeFoundForItemInReceiptRow, invItem.InventoryCD, aRow.ReceiptNbr, aRow.LineNbr, postClass.PostClassID, invSite != null ? invSite.SiteCD : String.Empty);
                                }
                                else
                                {
                                    throw ex;
                                }
                            }
                            if (aSubID == null)
                            {
                                throw new PXException(Messages.COGSSubAccountCanNotBeFoundForItemInReceiptRow, invItem.InventoryCD, aRow.ReceiptNbr, aRow.LineNbr, postClass.PostClassID, invSite != null ? invSite.SiteCD : String.Empty);
                            }
                        }
                        else
                        {
                            aAccountID = INReleaseProcess.GetAcctID <INPostClass.lCVarianceAcctID>(aGraph, postClass.LCVarianceAcctDefault, invItem, invSite, postClass);
                            if (aAccountID == null)
                            {
                                throw new PXException(Messages.LCVarianceAccountCanNotBeFoundForItemInReceiptRow, invItem.InventoryCD, aRow.ReceiptNbr, aRow.LineNbr, postClass.PostClassID, invSite != null ? invSite.SiteCD : String.Empty);
                            }
                            try
                            {
                                aSubID = INReleaseProcess.GetSubID <INPostClass.lCVarianceSubID>(aGraph, postClass.LCVarianceAcctDefault, postClass.LCVarianceSubMask, invItem, invSite, postClass);
                            }
                            catch (PXException ex)
                            {
                                if (postClass.LCVarianceSubID == null ||
                                    string.IsNullOrEmpty(postClass.LCVarianceSubMask) ||
                                    invItem.LCVarianceSubID == null || invSite == null || invSite.LCVarianceSubID == null)
                                {
                                    throw new PXException(Messages.LCVarianceSubAccountCanNotBeFoundForItemInReceiptRow, invItem.InventoryCD, aRow.ReceiptNbr, aRow.LineNbr, postClass.PostClassID, invSite != null ? invSite.SiteCD : String.Empty);
                                }
                                else
                                {
                                    throw ex;
                                }
                            }
                            if (aSubID == null)
                            {
                                throw new PXException(Messages.LCVarianceSubAccountCanNotBeFoundForItemInReceiptRow, invItem.InventoryCD, aRow.ReceiptNbr, aRow.LineNbr, postClass.PostClassID, invSite != null ? invSite.SiteCD : String.Empty);
                            }
                        }
                    }
                    else
                    {
                        aAccountID = aRow.ExpenseAcctID;
                        aSubID     = aRow.ExpenseSubID;
                    }
                }
                else
                {
                    throw
                        new PXException(Messages.LCInventoryItemInReceiptRowIsNotFound, aRow.InventoryID, aRow.ReceiptNbr, aRow.LineNbr);
                }
            }
            else
            {
                aAccountID = aRow.ExpenseAcctID;
                aSubID     = aRow.ExpenseSubID;
            }
        }
        protected virtual INTran[] CreateTransactions(POLandedCostDoc doc, POLandedCostDetail landedCostDetail, IEnumerable <LandedCostAllocationService.POLandedCostReceiptLineAdjustment> pOLinesToProcess)
        {
            var result = new List <INTran>();

            var landedCostCode = GetLandedCostCode(landedCostDetail.LandedCostCodeID);
            var reasonCode     = landedCostCode.ReasonCode;

            foreach (LandedCostAllocationService.POLandedCostReceiptLineAdjustment poreceiptline in pOLinesToProcess)
            {
                INTran intran = new INTran();

                INTran origtran   = LandedCostAllocationService.Instance.GetOriginalInTran(_pxGraph, poreceiptline.ReceiptLine.ReceiptNbr, poreceiptline.ReceiptLine.LineNbr);
                bool   isDropShip = (poreceiptline.ReceiptLine.LineType == POLineType.GoodsForDropShip || poreceiptline.ReceiptLine.LineType == POLineType.NonStockForDropShip);

                if (!isDropShip && origtran == null)
                {
                    throw new PXException(AP.Messages.CannotFindINReceipt, poreceiptline.ReceiptLine.ReceiptNbr);
                }

                //Drop-Ships are considered non-stocks
                if (poreceiptline.ReceiptLine.IsStockItem())
                {
                    intran.TranType = INTranType.ReceiptCostAdjustment;
                }
                else
                {
                    //Landed cost and PPV for non-stock items are handled in special way in the inventory.
                    //They should create a GL Batch, but for convinience and unforminty this functionality is placed into IN module
                    //Review this part when the functionality is implemented in IN module
                    intran.IsCostUnmanaged = true;
                    intran.TranType        = INTranType.Adjustment;
                    intran.InvtMult        = 0;
                }
                intran.InventoryID = poreceiptline.ReceiptLine.InventoryID;
                intran.SubItemID   = poreceiptline.ReceiptLine.SubItemID;
                intran.SiteID      = poreceiptline.ReceiptLine.SiteID;
                intran.BAccountID  = doc.VendorID;
                intran.BranchID    = landedCostDetail.BranchID;


                if (isDropShip && intran.SiteID != null)
                {
                    INSite invSite = PXSelect <INSite, Where <INSite.siteID, Equal <Required <POReceiptLine.siteID> > > > .Select(_pxGraph, intran.SiteID);

                    if (invSite.DropShipLocationID == null)
                    {
                        throw new PXException(SO.Messages.NoDropShipLocation, invSite.SiteCD);
                    }

                    intran.LocationID = invSite.DropShipLocationID;
                }
                else
                {
                    intran.LocationID = poreceiptline.ReceiptLine.LocationID ?? origtran.LocationID;
                }

                intran.LotSerialNbr     = poreceiptline.ReceiptLine.LotSerialNbr;
                intran.POReceiptType    = poreceiptline.ReceiptLine.ReceiptType;
                intran.POReceiptNbr     = poreceiptline.ReceiptLine.ReceiptNbr;
                intran.POReceiptLineNbr = poreceiptline.ReceiptLine.LineNbr;

                //tran.Qty = poreceiptline.ReceiptQty;
                intran.TranDesc = landedCostDetail.Descr;
                //tran.UnitCost = PXDBPriceCostAttribute.Round(inGraph.transactions.Cache, (decimal)(poreceiptline.ExtCost / poreceiptline.ReceiptQty));
                intran.TranCost   = poreceiptline.QtyToAssign;
                intran.ReasonCode = reasonCode;
                if (origtran != null && origtran.DocType == INDocType.Issue)
                {
                    intran.ARDocType = origtran.ARDocType;
                    intran.ARRefNbr  = origtran.ARRefNbr;
                    intran.ARLineNbr = origtran.ARLineNbr;
                }
                if (!isDropShip)
                {
                    intran.OrigTranType = origtran.DocType;
                    intran.OrigRefNbr   = origtran.RefNbr;
                }

                int?acctID = null;
                int?subID  = null;
                intran.AcctID = landedCostDetail.LCAccrualAcct;
                intran.SubID  = landedCostDetail.LCAccrualSub;
                GetLCVarianceAccountSub(ref acctID, ref subID, poreceiptline.ReceiptLine);
                intran.COGSAcctID = acctID;
                intran.COGSSubID  = subID;

                result.Add(intran);
            }

            return(result.ToArray());
        }
        //TODO:refactor
        public static void SOCreateProc(List <SOFixedDemand> list, DateTime?PurchDate)
        {
            SOOrderEntry           docgraph = PXGraph.CreateInstance <SOOrderEntry>();
            SOSetup                sosetup  = docgraph.sosetup.Current;
            DocumentList <SOOrder> created  = new DocumentList <SOOrder>(docgraph);

            docgraph.ExceptionHandling.AddHandler <SOLineSplit.qty>((cache, e) => { e.Cancel = true; });
            docgraph.ExceptionHandling.AddHandler <SOLineSplit.isAllocated>((cache, e) => { ((SOLineSplit)e.Row).IsAllocated = true; e.Cancel = true; });

            foreach (SOFixedDemand demand in list)
            {
                string OrderType =
                    sosetup.TransferOrderType ?? SOOrderTypeConstants.TransferOrder;

                string demandPlanType = demand.PlanType;

                try
                {
                    if (demand.SourceSiteID == null)
                    {
                        PXProcessing <SOFixedDemand> .SetWarning(list.IndexOf(demand), Messages.MissingSourceSite);

                        continue;
                    }

                    SOOrder      order;
                    SOLineSplit2 sosplit = PXSelect <SOLineSplit2, Where <SOLineSplit2.planID, Equal <Required <SOLineSplit2.planID> > > > .Select(docgraph, demand.PlanID);

                    if (sosplit != null)
                    {
                        order = created.Find <SOOrder.orderType, SOOrder.destinationSiteID, SOOrder.defaultSiteID>(OrderType, sosplit.ToSiteID, sosplit.SiteID);
                    }
                    else
                    {
                        if (demand.SourceSiteID == demand.SiteID)
                        {
                            PXProcessing <SOFixedDemand> .SetWarning(list.IndexOf(demand), Messages.EqualSourceDestinationSite);

                            continue;
                        }

                        order = created.Find <SOOrder.orderType, SOOrder.destinationSiteID, SOOrder.defaultSiteID>(OrderType, demand.SiteID, demand.SourceSiteID);
                    }

                    if (order == null)
                    {
                        order = new SOOrder();
                    }

                    if (order.OrderNbr == null)
                    {
                        docgraph.Clear();

                        if (sosplit != null)
                        {
                            INSite sourceSite = PXSelect <INSite, Where <INSite.siteID, Equal <Required <INSite.siteID> > > > .Select(docgraph, sosplit.SiteID);

                            order.BranchID  = sourceSite.BranchID;
                            order.OrderType = OrderType;
                            order           = PXCache <SOOrder> .CreateCopy(docgraph.Document.Insert(order));

                            order.DefaultSiteID     = sosplit.SiteID;
                            order.DestinationSiteID = sosplit.ToSiteID;
                            order.OrderDate         = PurchDate;

                            docgraph.Document.Update(order);
                        }
                        else
                        {
                            INSite sourceSite = PXSelect <INSite, Where <INSite.siteID, Equal <Required <INSite.siteID> > > > .Select(docgraph, demand.SourceSiteID);

                            order.BranchID  = sourceSite.BranchID;
                            order.OrderType = OrderType;
                            order           = PXCache <SOOrder> .CreateCopy(docgraph.Document.Insert(order));

                            order.DefaultSiteID     = demand.SourceSiteID;
                            order.DestinationSiteID = demand.SiteID;
                            order.OrderDate         = PurchDate;

                            docgraph.Document.Update(order);
                        }
                    }
                    else if (docgraph.Document.Cache.ObjectsEqual(docgraph.Document.Current, order) == false)
                    {
                        docgraph.Document.Current = docgraph.Document.Search <SOOrder.orderNbr>(order.OrderNbr, order.OrderType);
                    }

                    SOLine      newline;
                    SOLineSplit newsplit;
                    PXCache     cache = docgraph.Caches[typeof(INItemPlan)];
                    INItemPlan  rp;

                    if (sosplit != null)
                    {
                        docgraph.Transactions.Current = newline = docgraph.Transactions.Search <SOLine.inventoryID, SOLine.subItemID, SOLine.siteID, SOLine.pOCreate>(demand.InventoryID, demand.SubItemID, demand.SiteID, false);
                        if (newline == null)
                        {
                            newline = PXCache <SOLine> .CreateCopy(docgraph.Transactions.Insert());

                            newline.InventoryID = demand.InventoryID;
                            newline.SubItemID   = demand.SubItemID;
                            newline.SiteID      = demand.SiteID;
                            newline.UOM         = demand.UOM;
                            newline.OrderQty    = 0m;

                            newline = docgraph.Transactions.Update(newline);
                        }

                        newsplit = new SOLineSplit();
                        newsplit.LotSerialNbr = sosplit.LotSerialNbr;
                        newsplit.IsAllocated  = true;
                        newsplit.IsMergeable  = false;
                        newsplit.SiteID       = demand.SiteID; //SiteID should be explicitly set because of PXFormula
                        newsplit.Qty          = demand.OrderQty;
                        newsplit.RefNoteID    = demand.RefNoteID;

                        //we have to delete previous allocation and reinsert it after the newsplit for transfer-order allocation to work properly
                        rp = PXCache <INItemPlan> .CreateCopy(demand);

                        cache.RaiseRowDeleted(demand);

                        newsplit = docgraph.splits.Insert(newsplit);

                        sosplit.SOOrderType    = newsplit.OrderType;
                        sosplit.SOOrderNbr     = newsplit.OrderNbr;
                        sosplit.SOLineNbr      = newsplit.LineNbr;
                        sosplit.SOSplitLineNbr = newsplit.SplitLineNbr;
                        docgraph.sodemand.Update(sosplit);

                        rp.SiteID       = sosplit.ToSiteID;
                        rp.PlanType     = INPlanConstants.Plan93;
                        rp.FixedSource  = null;
                        rp.SupplyPlanID = newsplit.PlanID;
                        cache.RaiseRowInserted(rp);
                        cache.SetStatus(rp, PXEntryStatus.Updated);
                    }
                    else
                    {
                        docgraph.Transactions.Current = newline = docgraph.Transactions.Search <SOLine.inventoryID, SOLine.subItemID, SOLine.siteID, SOLine.pOCreate>(demand.InventoryID, demand.SubItemID, demand.SourceSiteID, demand.VendorID != null);
                        if (newline == null)
                        {
                            newline = PXCache <SOLine> .CreateCopy(docgraph.Transactions.Insert());

                            newline.InventoryID = demand.InventoryID;
                            newline.SubItemID   = demand.SubItemID;
                            newline.SiteID      = demand.SourceSiteID;
                            newline.UOM         = demand.UOM;
                            newline.OrderQty    = 0m;
                            //newline.POCreate = (demand.VendorID != null);
                            newline.POCreate = (demand.FixedSource == INReplenishmentSource.TransferToPurchase);
                            newline.VendorID = demand.VendorID;
                            //newline.VendorLocationID = demand.VendorLocationID;

                            newline = docgraph.Transactions.Update(newline);
                        }

                        newsplit             = new SOLineSplit();
                        newsplit.IsAllocated = false;
                        newsplit.Qty         = demand.OrderQty;
                        //newsplit.POCreate = (demand.VendorID != null);
                        newsplit.POCreate = (demand.FixedSource == INReplenishmentSource.TransferToPurchase);
                        newsplit.VendorID = demand.VendorID;
                        //newsplit.VendorLocationID = demand.VendorLocationID;
                        newsplit = docgraph.splits.Insert(newsplit) ?? docgraph.splits.Current;

                        rp = PXCache <INItemPlan> .CreateCopy(demand);

                        cache.RaiseRowDeleted(demand);
                        rp.SiteID       = demand.SiteID;
                        rp.PlanType     = INPlanConstants.Plan94;
                        rp.FixedSource  = null;
                        rp.SupplyPlanID = newsplit.PlanID;
                        cache.RaiseRowInserted(rp);
                        cache.SetStatus(rp, PXEntryStatus.Updated);
                    }

                    if (newsplit.PlanID == null)
                    {
                        throw new PXRowPersistedException(typeof(SOLine).Name, newline, RQ.Messages.UnableToCreateSOOrders);
                    }

                    if (docgraph.Transactions.Cache.IsInsertedUpdatedDeleted)
                    {
                        using (PXTransactionScope scope = new PXTransactionScope())
                        {
                            docgraph.Save.Press();
                            if (demandPlanType == INPlanConstants.Plan90)
                            {
                                docgraph.Replenihment.Current = docgraph.Replenihment.Search <INReplenishmentOrder.noteID>(demand.RefNoteID);
                                if (docgraph.Replenihment.Current != null)
                                {
                                    INReplenishmentLine rLine =
                                        PXCache <INReplenishmentLine> .CreateCopy(docgraph.ReplenishmentLines.Insert(new INReplenishmentLine()));

                                    rLine.InventoryID       = newsplit.InventoryID;
                                    rLine.SubItemID         = newsplit.SubItemID;
                                    rLine.UOM               = newsplit.UOM;
                                    rLine.Qty               = newsplit.Qty;
                                    rLine.SOType            = newsplit.OrderType;
                                    rLine.SONbr             = docgraph.Document.Current.OrderNbr;
                                    rLine.SOLineNbr         = newsplit.LineNbr;
                                    rLine.SOSplitLineNbr    = newsplit.SplitLineNbr;
                                    rLine.SiteID            = demand.SourceSiteID;
                                    rLine.DestinationSiteID = demand.SiteID;
                                    rLine.PlanID            = demand.PlanID;
                                    docgraph.ReplenishmentLines.Update(rLine);
                                    INItemPlan plan = PXSelect <INItemPlan,
                                                                Where <INItemPlan.planID, Equal <Required <INItemPlan.planID> > > > .SelectWindowed(docgraph, 0, 1,
                                                                                                                                                    demand.SupplyPlanID);

                                    if (plan != null)
                                    {
                                        //plan.SupplyPlanID = rp.PlanID;
                                        rp.SupplyPlanID = plan.PlanID;
                                        cache.SetStatus(rp, PXEntryStatus.Updated);
                                    }

                                    docgraph.Save.Press();
                                }
                            }
                            scope.Complete();
                        }

                        PXProcessing <SOFixedDemand> .SetInfo(list.IndexOf(demand), PXMessages.LocalizeFormatNoPrefixNLA(Messages.TransferOrderCreated, docgraph.Document.Current.OrderNbr));

                        if (created.Find(docgraph.Document.Current) == null)
                        {
                            created.Add(docgraph.Document.Current);
                        }
                    }
                }
                catch (Exception e)
                {
                    PXProcessing <SOFixedDemand> .SetError(list.IndexOf(demand), e);
                }
            }
            if (created.Count == 1)
            {
                using (new PXTimeStampScope(null))
                {
                    docgraph.Clear();
                    docgraph.Document.Current = docgraph.Document.Search <POOrder.orderNbr>(created[0].OrderNbr, created[0].OrderType);
                    throw new PXRedirectRequiredException(docgraph, Messages.SOOrder);
                }
            }
        }
Exemple #12
0
        protected virtual void GetLCVarianceAccountSub(ref int?aAccountID, ref int?aSubID, POReceiptLine receiptLine)
        {
            if (receiptLine.InventoryID.HasValue)
            {
                var invItem = InventoryItem.PK.Find(_pxGraph, receiptLine.InventoryID);
                if (invItem != null)
                {
                    INPostClass postClass = INPostClass.PK.Find(_pxGraph, invItem.PostClassID);

                    if ((bool)invItem.StkItem)
                    {
                        if (postClass == null)
                        {
                            throw new PXException(Messages.PostingClassIsNotDefinedForTheItemInReceiptRow, invItem.InventoryCD, receiptLine.ReceiptNbr, receiptLine.LineNbr);
                        }
                        INSite invSite = null;
                        if (receiptLine.SiteID != null)
                        {
                            invSite = INSite.PK.Find(_pxGraph, receiptLine.SiteID);
                        }
                        if (receiptLine.LineType == POLineType.GoodsForDropShip)
                        {
                            aAccountID = INReleaseProcess.GetAcctID <INPostClass.cOGSAcctID>(_pxGraph, postClass.COGSAcctDefault, invItem, invSite, postClass);
                            if (aAccountID == null)
                            {
                                throw new PXException(Messages.COGSAccountCanNotBeFoundForItemInReceiptRow, invItem.InventoryCD, receiptLine.ReceiptNbr, receiptLine.LineNbr, postClass.PostClassID, invSite != null ? invSite.SiteCD : String.Empty);
                            }
                            try
                            {
                                aSubID = INReleaseProcess.GetSubID <INPostClass.cOGSSubID>(_pxGraph, postClass.COGSAcctDefault, postClass.COGSSubMask, invItem, invSite, postClass);
                            }
                            catch (PXException ex)
                            {
                                if (postClass.COGSSubID == null ||
                                    string.IsNullOrEmpty(postClass.COGSSubMask) ||
                                    invItem.COGSSubID == null || invSite == null || invSite.COGSSubID == null)
                                {
                                    throw new PXException(Messages.COGSSubAccountCanNotBeFoundForItemInReceiptRow, invItem.InventoryCD, receiptLine.ReceiptNbr, receiptLine.LineNbr, postClass.PostClassID, invSite != null ? invSite.SiteCD : String.Empty);
                                }
                                else
                                {
                                    throw ex;
                                }
                            }
                            if (aSubID == null)
                            {
                                throw new PXException(Messages.COGSSubAccountCanNotBeFoundForItemInReceiptRow, invItem.InventoryCD, receiptLine.ReceiptNbr, receiptLine.LineNbr, postClass.PostClassID, invSite != null ? invSite.SiteCD : String.Empty);
                            }
                        }
                        else
                        {
                            aAccountID = INReleaseProcess.GetAcctID <INPostClass.lCVarianceAcctID>(_pxGraph, postClass.LCVarianceAcctDefault, invItem, invSite, postClass);
                            if (aAccountID == null)
                            {
                                throw new PXException(Messages.LCVarianceAccountCanNotBeFoundForItemInReceiptRow, invItem.InventoryCD, receiptLine.ReceiptNbr, receiptLine.LineNbr, postClass.PostClassID, invSite != null ? invSite.SiteCD : String.Empty);
                            }
                            try
                            {
                                aSubID = INReleaseProcess.GetSubID <INPostClass.lCVarianceSubID>(_pxGraph, postClass.LCVarianceAcctDefault, postClass.LCVarianceSubMask, invItem, invSite, postClass);
                            }
                            catch (PXException ex)
                            {
                                if (postClass.LCVarianceSubID == null ||
                                    string.IsNullOrEmpty(postClass.LCVarianceSubMask) ||
                                    invItem.LCVarianceSubID == null || invSite == null || invSite.LCVarianceSubID == null)
                                {
                                    throw new PXException(Messages.LCVarianceSubAccountCanNotBeFoundForItemInReceiptRow, invItem.InventoryCD, receiptLine.ReceiptNbr, receiptLine.LineNbr, postClass.PostClassID, invSite != null ? invSite.SiteCD : String.Empty);
                                }
                                else
                                {
                                    throw ex;
                                }
                            }
                            if (aSubID == null)
                            {
                                throw new PXException(Messages.LCVarianceSubAccountCanNotBeFoundForItemInReceiptRow, invItem.InventoryCD, receiptLine.ReceiptNbr, receiptLine.LineNbr, postClass.PostClassID, invSite != null ? invSite.SiteCD : String.Empty);
                            }
                        }
                    }
                    else
                    {
                        aAccountID = receiptLine.ExpenseAcctID;
                        aSubID     = receiptLine.ExpenseSubID;
                    }
                }
                else
                {
                    throw
                        new PXException(Messages.LCInventoryItemInReceiptRowIsNotFound, receiptLine.InventoryID, receiptLine.ReceiptNbr, receiptLine.LineNbr);
                }
            }
            else
            {
                aAccountID = receiptLine.ExpenseAcctID;
                aSubID     = receiptLine.ExpenseSubID;
            }
        }
Exemple #13
0
        public static void SOCreateProc(List <SOFixedDemand> list, DateTime?PurchDate)
        {
            SOOrderEntry           docgraph = PXGraph.CreateInstance <SOOrderEntry>();
            SOSetup                sosetup  = docgraph.sosetup.Current;
            DocumentList <SOOrder> created  = new DocumentList <SOOrder>(docgraph);

            foreach (SOFixedDemand demand in list)
            {
                string OrderType =
                    sosetup.TransferOrderType ?? SOOrderTypeConstants.TransferOrder;

                string ErrorText = string.Empty;

                try
                {
                    if (demand.ReplenishmentSourceSiteID == null)
                    {
                        PXProcessing <SOFixedDemand> .SetWarning(list.IndexOf(demand), Messages.MissingSourceSite);

                        continue;
                    }

                    if (demand.ReplenishmentSourceSiteID == demand.SiteID)
                    {
                        PXProcessing <SOFixedDemand> .SetWarning(list.IndexOf(demand), Messages.EqualSourceDestinationSite);

                        continue;
                    }
                    SOOrder order;
                    POLine  poline = PXSelect <POLine, Where <POLine.planID, Equal <Required <POLine.planID> > > > .Select(docgraph, demand.PlanID);

                    if (poline != null)
                    {
                        order =
                            created.Find <SOOrder.orderType, SOOrder.destinationSiteID, SOOrder.defaultSiteID, SOOrder.origPOType, SOOrder.origPONbr>(
                                OrderType, demand.SiteID, demand.ReplenishmentSourceSiteID, poline.OrderType, poline.OrderNbr);
                    }
                    else
                    {
                        order = created.Find <SOOrder.orderType, SOOrder.destinationSiteID, SOOrder.defaultSiteID>(OrderType, demand.SiteID, demand.ReplenishmentSourceSiteID);
                    }

                    if (order == null)
                    {
                        order = new SOOrder();
                    }

                    if (order.OrderNbr == null)
                    {
                        docgraph.Clear();

                        INSite sourceSite = PXSelect <INSite, Where <INSite.siteID, Equal <Required <INSite.siteID> > > > .Select(docgraph, demand.ReplenishmentSourceSiteID);

                        order.BranchID  = sourceSite.BranchID;
                        order.OrderType = OrderType;
                        order           = PXCache <SOOrder> .CreateCopy(docgraph.Document.Insert(order));

                        order.DefaultSiteID     = demand.ReplenishmentSourceSiteID;
                        order.DestinationSiteID = demand.SiteID;
                        order.Status            = SOOrderStatus.Open;
                        order.OrderDate         = PurchDate;
                        if (poline != null)
                        {
                            order.OrigPOType = poline.OrderType;
                            order.OrigPONbr  = poline.OrderNbr;
                        }
                        docgraph.Document.Update(order);
                    }
                    else if (docgraph.Document.Cache.ObjectsEqual(docgraph.Document.Current, order) == false)
                    {
                        docgraph.Document.Current = docgraph.Document.Search <SOOrder.orderNbr>(order.OrderNbr, order.OrderType);
                    }
                    SOLine line = new SOLine();
                    line = PXCache <SOLine> .CreateCopy(docgraph.Transactions.Insert(line));

                    line.InventoryID = demand.InventoryID;
                    line.SubItemID   = demand.SubItemID;
                    line.SiteID      = demand.ReplenishmentSourceSiteID;
                    line.UOM         = demand.UOM;
                    line.OrderQty    = demand.OrderQty;
                    if (poline != null)
                    {
                        line.OrigPOType    = poline.OrderType;
                        line.OrigPONbr     = poline.OrderNbr;
                        line.OrigPOLineNbr = poline.LineNbr;
                    }

                    line = docgraph.Transactions.Update(line);

                    if (line.PlanID == null)
                    {
                        throw new PXRowPersistedException(typeof(SOLine).Name, line, RQ.Messages.UnableToCreateSOOrders);
                    }

                    PXCache cache          = docgraph.Caches[typeof(INItemPlan)];
                    string  demandPlanType = demand.PlanType;
                    //cache.SetStatus(demand, PXEntryStatus.Updated);
                    //demand.SupplyPlanID = line.PlanID;
                    INItemPlan rp = PXCache <INItemPlan> .CreateCopy(demand);

                    cache.RaiseRowDeleted(demand);
                    rp.PlanType     = INPlanConstants.Plan94;
                    rp.FixedSource  = null;
                    rp.SupplyPlanID = line.PlanID;
                    cache.RaiseRowInserted(rp);
                    cache.SetStatus(rp, PXEntryStatus.Updated);

                    if (docgraph.Transactions.Cache.IsInsertedUpdatedDeleted)
                    {
                        using (PXTransactionScope scope = new PXTransactionScope())
                        {
                            docgraph.Save.Press();
                            if (demandPlanType == INPlanConstants.Plan90)
                            {
                                docgraph.Replenihment.Current = docgraph.Replenihment.Search <INReplenishmentOrder.noteID>(demand.RefNoteID);
                                if (docgraph.Replenihment.Current != null)
                                {
                                    INReplenishmentLine rLine =
                                        PXCache <INReplenishmentLine> .CreateCopy(docgraph.ReplenishmentLines.Insert(new INReplenishmentLine()));

                                    rLine.InventoryID       = line.InventoryID;
                                    rLine.SubItemID         = line.SubItemID;
                                    rLine.UOM               = line.UOM;
                                    rLine.Qty               = line.OrderQty;
                                    rLine.SOType            = line.OrderType;
                                    rLine.SONbr             = docgraph.Document.Current.OrderNbr;
                                    rLine.SOLineNbr         = line.LineNbr;
                                    rLine.SiteID            = demand.ReplenishmentSourceSiteID;
                                    rLine.DestinationSiteID = demand.SiteID;
                                    rLine.PlanID            = demand.PlanID;
                                    docgraph.ReplenishmentLines.Update(rLine);
                                    INItemPlan plan = PXSelect <INItemPlan,
                                                                Where <INItemPlan.planID, Equal <Required <INItemPlan.planID> > > > .SelectWindowed(docgraph, 0, 1,
                                                                                                                                                    demand.SupplyPlanID);

                                    if (plan != null)
                                    {
                                        //plan.SupplyPlanID = rp.PlanID;
                                        rp.SupplyPlanID = plan.PlanID;
                                        cache.SetStatus(rp, PXEntryStatus.Updated);
                                    }

                                    docgraph.Save.Press();
                                }
                            }
                            scope.Complete();
                        }

                        PXProcessing <SOFixedDemand> .SetInfo(list.IndexOf(demand), string.Format(Messages.TransferOrderCreated, docgraph.Document.Current.OrderNbr) + "\r\n" + ErrorText);


                        if (created.Find(docgraph.Document.Current) == null)
                        {
                            created.Add(docgraph.Document.Current);
                        }
                    }
                }
                catch (Exception e)
                {
                    PXProcessing <SOFixedDemand> .SetError(list.IndexOf(demand), e);
                }
            }
        }
        public virtual void PostShippedNotInvoiced(INTran intran, ARTran n, ARInvoice ardoc, JournalEntry je)
        {
            if (intran.UpdateShippedNotInvoiced != true)
            {
                return;
            }
            if (intran.Released != true)
            {
                throw new PXException(PXMessages.LocalizeFormatNoPrefixNLA(AR.Messages.ShippedNotInvoicedINtranNotReleased, intran.RefNbr));
            }
            var trancosts = PXSelect <INTranCost,
                                      Where <INTranCost.costDocType, Equal <Required <INTranCost.costDocType> >,
                                             And <INTranCost.costRefNbr, Equal <Required <INTranCost.costRefNbr> >,
                                                  And <INTranCost.lineNbr, Equal <Required <INTranCost.lineNbr> > > > > >
                            .Select(Base, intran.DocType, intran.RefNbr, intran.LineNbr);

            foreach (INTranCost trancost in trancosts)
            {
                var itemPostClassRes = (PXResult <InventoryItem, INPostClass>) PXSelectJoin <InventoryItem,
                                                                                             LeftJoin <INPostClass, On <INPostClass.postClassID, Equal <InventoryItem.postClassID> > >,
                                                                                             Where <InventoryItem.inventoryID, Equal <Required <InventoryItem.inventoryID> > > >
                                       .Select(Base, intran.InventoryID);

                ReasonCode reasoncode = INTran.FK.ReasonCode.FindParent(Base, intran);
                INSite     site       = INSite.PK.Find(Base, intran.SiteID);

                if (trancost != null && trancost.COGSAcctID != null && intran != null)
                {
                    //Credit shipped-not-invoiced account
                    GLTran tranFromIN = new GLTran();
                    tranFromIN.SummPost  = Base.SummPost;
                    tranFromIN.BranchID  = intran.BranchID;
                    tranFromIN.TranType  = trancost.TranType;
                    tranFromIN.TranClass = GLTran.tranClass.ShippedNotInvoiced;

                    tranFromIN.AccountID = intran.COGSAcctID ?? INReleaseProcess.GetAccountDefaults <INPostClass.cOGSAcctID>(Base, (InventoryItem)itemPostClassRes, site, (INPostClass)itemPostClassRes, intran);
                    tranFromIN.SubID     = intran.COGSSubID ?? INReleaseProcess.GetAccountDefaults <INPostClass.cOGSSubID>(Base, (InventoryItem)itemPostClassRes, site, (INPostClass)itemPostClassRes, intran);

                    tranFromIN.CuryDebitAmt  = (trancost.InvtMult == (short)1) ? trancost.TranCost : 0m;
                    tranFromIN.DebitAmt      = (trancost.InvtMult == (short)1) ? trancost.TranCost : 0m;
                    tranFromIN.CuryCreditAmt = (trancost.InvtMult == (short)1) ? 0m : trancost.TranCost;
                    tranFromIN.CreditAmt     = (trancost.InvtMult == (short)1) ? 0m : trancost.TranCost;

                    tranFromIN.RefNbr      = trancost.RefNbr;
                    tranFromIN.InventoryID = trancost.InventoryID;
                    tranFromIN.Qty         = (trancost.InvtMult == (short)1) ? trancost.Qty : -trancost.Qty;
                    tranFromIN.UOM         = intran.UOM;
                    tranFromIN.TranDesc    = intran.TranDesc;
                    tranFromIN.TranDate    = n.TranDate;
                    bool isNonProject = trancost.InvtMult == (short)0;
                    tranFromIN.ProjectID  = isNonProject ? PM.ProjectDefaultAttribute.NonProject() : intran.ProjectID;
                    tranFromIN.TaskID     = isNonProject ? null : intran.TaskID;
                    tranFromIN.CostCodeID = tranFromIN.CostCodeID;
                    tranFromIN.Released   = true;

                    Base.InsertInvoiceDetailsINTranCostTransaction(je, tranFromIN,
                                                                   new GLTranInsertionContext {
                        ARRegisterRecord = ardoc, ARTranRecord = n, INTranRecord = intran, INTranCostRecord = trancost
                    });

                    //Debit COGS account
                    tranFromIN          = new GLTran();
                    tranFromIN.SummPost = Base.SummPost;
                    tranFromIN.BranchID = n.BranchID;
                    if (reasoncode?.Usage == ReasonCodeUsages.Issue)
                    {
                        tranFromIN.AccountID = reasoncode.AccountID;
                        tranFromIN.SubID     = INReleaseProcess.GetReasonCodeSubID(Base, reasoncode, (InventoryItem)itemPostClassRes, site, (INPostClass)itemPostClassRes);
                    }
                    else
                    {
                        tranFromIN.AccountID = INReleaseProcess.GetAccountDefaults <INPostClass.cOGSAcctID>(Base, (InventoryItem)itemPostClassRes, site, (INPostClass)itemPostClassRes);
                        if (((INPostClass)itemPostClassRes) != null && ((INPostClass)itemPostClassRes).COGSSubFromSales != true)                         //we cannot use intran here to retrive cogs/sales sub
                        {
                            tranFromIN.SubID = INReleaseProcess.GetAccountDefaults <INPostClass.cOGSSubID>(Base, (InventoryItem)itemPostClassRes, site, (INPostClass)itemPostClassRes);
                        }
                        else
                        {
                            tranFromIN.SubID = n.SubID;
                        }
                    }

                    tranFromIN.CuryDebitAmt  = (trancost.InvtMult == (short)1) ? 0m : trancost.TranCost;
                    tranFromIN.DebitAmt      = (trancost.InvtMult == (short)1) ? 0m : trancost.TranCost;
                    tranFromIN.CuryCreditAmt = (trancost.InvtMult == (short)1) ? trancost.TranCost : 0m;
                    tranFromIN.CreditAmt     = (trancost.InvtMult == (short)1) ? trancost.TranCost : 0m;

                    tranFromIN.TranType    = trancost.TranType;
                    tranFromIN.TranClass   = GLTran.tranClass.ShippedNotInvoiced;
                    tranFromIN.RefNbr      = trancost.RefNbr;
                    tranFromIN.InventoryID = trancost.InventoryID;
                    tranFromIN.Qty         = (trancost.InvtMult == (short)1) ? -trancost.Qty : trancost.Qty;
                    tranFromIN.UOM         = intran.UOM;
                    tranFromIN.TranDesc    = intran.TranDesc;
                    tranFromIN.TranDate    = n.TranDate;
                    tranFromIN.ProjectID   = (trancost.InvtMult == (short)1) ? PM.ProjectDefaultAttribute.NonProject() : intran.ProjectID;
                    tranFromIN.TaskID      = (trancost.InvtMult == (short)1) ? null : intran.TaskID;
                    tranFromIN.CostCodeID  = intran.CostCodeID;
                    tranFromIN.Released    = true;
                    tranFromIN.TranLineNbr = (tranFromIN.SummPost == true) ? null : intran.LineNbr;

                    Base.InsertInvoiceDetailsINTranCostTransaction(je, tranFromIN,
                                                                   new GLTranInsertionContext {
                        ARRegisterRecord = ardoc, ARTranRecord = n, INTranRecord = intran, INTranCostRecord = trancost
                    });
                }
            }
        }
Exemple #15
0
        protected void ReceiptDetails_Insert(PXGraph graph, EntityImpl entity, EntityImpl targetEntity)
        {
            var lineNbr    = targetEntity.Fields.SingleOrDefault(f => f.Name == "OrigLineNbr") as EntityValueField;
            var receiptQty = targetEntity.Fields.SingleOrDefault(f => f.Name == "Quantity") as EntityValueField;
            var location   = targetEntity.Fields.SingleOrDefault(f => f.Name == "Location") as EntityValueField;

            var allocations    = (targetEntity.Fields.SingleOrDefault(f => string.Equals(f.Name, "Allocations")) as EntityListField).Value ?? new EntityImpl[0];
            var hasAllocations = allocations.Any(a => a.Fields != null && a.Fields.Length > 0);

            var receiptEntry = (INReceiptEntry)graph;

            string transferNbr = receiptEntry.receipt.Current.TransferNbr;

            var detailsCache = receiptEntry.transactions.Cache;

            if (lineNbr == null || transferNbr == null)
            {
                detailsCache.Current = detailsCache.Insert();
                return;
            }

            INTran  newtran     = null;
            decimal newtranqty  = Decimal.Parse(receiptQty.Value);
            decimal newtrancost = 0m;

            receiptEntry.ParseSubItemSegKeys();

            using (new PXReadBranchRestrictedScope())
            {
                foreach (PXResult <INTransitLine, INLocationStatus2, INTransitLineLotSerialStatus, INSite, InventoryItem, INTran> res in
                         PXSelectJoin <INTransitLine,
                                       InnerJoin <INLocationStatus2, On <INLocationStatus2.locationID, Equal <INTransitLine.costSiteID> >,
                                                  LeftJoin <INTransitLineLotSerialStatus,
                                                            On <INTransitLine.transferNbr, Equal <INTransitLineLotSerialStatus.transferNbr>,
                                                                And <INTransitLine.transferLineNbr, Equal <INTransitLineLotSerialStatus.transferLineNbr> > >,
                                                            InnerJoin <INSite, On <INSite.siteID, Equal <INTransitLine.toSiteID> >,
                                                                       InnerJoin <InventoryItem, On <InventoryItem.inventoryID, Equal <INLocationStatus2.inventoryID> >,
                                                                                  InnerJoin <INTran,
                                                                                             On <INTran.docType, Equal <INDocType.transfer>,
                                                                                                 And <INTran.refNbr, Equal <INTransitLine.transferNbr>,
                                                                                                      And <INTran.lineNbr, Equal <INTransitLine.transferLineNbr>,
                                                                                                           And <INTran.invtMult, Equal <shortMinus1> > > > > > > > > >,
                                       Where <INTransitLine.transferNbr, Equal <Required <INTransitLine.transferNbr> >, And <INTransitLine.transferLineNbr, Equal <Required <INTransitLine.transferLineNbr> > > >,
                                       OrderBy <Asc <INTransitLine.transferNbr, Asc <INTransitLine.transferLineNbr> > > >
                         .Select(receiptEntry, transferNbr, lineNbr.Value))
                {
                    INTransitLine                transitline = res;
                    INLocationStatus2            stat        = res;
                    INTransitLineLotSerialStatus lotstat     = res;
                    INSite        site = res;
                    InventoryItem item = res;
                    INTran        tran = res;

                    if (stat.QtyOnHand == 0m || (lotstat != null && lotstat.QtyOnHand == 0m))
                    {
                        continue;
                    }

                    if (newtran == null)
                    {
                        if (!object.Equals(receiptEntry.receipt.Current.BranchID, site.BranchID))
                        {
                            INRegister copy = PXCache <INRegister> .CreateCopy(receiptEntry.receipt.Current);

                            copy.BranchID = site.BranchID;
                            receiptEntry.receipt.Update(copy);
                        }

                        newtran = PXCache <INTran> .CreateCopy(tran);

                        newtran.OrigBranchID = newtran.BranchID;
                        newtran.OrigTranType = newtran.TranType;
                        newtran.OrigRefNbr   = transitline.TransferNbr;
                        newtran.OrigLineNbr  = transitline.TransferLineNbr;
                        newtran.BranchID     = site.BranchID;
                        newtran.DocType      = receiptEntry.receipt.Current.DocType;
                        newtran.RefNbr       = receiptEntry.receipt.Current.RefNbr;
                        newtran.LineNbr      = (int)PXLineNbrAttribute.NewLineNbr <INTran.lineNbr>(receiptEntry.transactions.Cache, receiptEntry.receipt.Current);
                        newtran.InvtMult     = (short)1;
                        newtran.SiteID       = transitline.ToSiteID;
                        newtran.LocationID   = transitline.ToLocationID;
                        newtran.ToSiteID     = null;
                        newtran.ToLocationID = null;
                        newtran.BaseQty      = 0m;
                        newtran.Qty          = 0m;
                        newtran.UnitCost     = 0m;
                        newtran.Released     = false;
                        newtran.InvtAcctID   = null;
                        newtran.InvtSubID    = null;
                        newtran.ReasonCode   = null;
                        newtran.ARDocType    = null;
                        newtran.ARRefNbr     = null;
                        newtran.ARLineNbr    = null;
                        newtran.ProjectID    = null;
                        newtran.TaskID       = null;
                        newtran.CostCodeID   = null;
                        newtran.TranCost     = 0m;

                        receiptEntry.splits.Current = null;

                        newtran = receiptEntry.transactions.Insert(newtran);

                        receiptEntry.transactions.Current = newtran;

                        if (receiptEntry.splits.Current != null)
                        {
                            receiptEntry.splits.Delete(receiptEntry.splits.Current);
                        }
                    }

                    if (hasAllocations)
                    {
                        newtranqty = 0m;

                        foreach (var allocation in allocations)
                        {
                            var newsplitqty      = allocation.Fields.SingleOrDefault(f => f.Name == "Quantity") as EntityValueField;
                            var newsplitlocation = allocation.Fields.SingleOrDefault(f => f.Name == "Location") as EntityValueField;

                            INTranSplit newsplit = this.addReceiptSplitLine(receiptEntry, stat, lotstat, transitline, newtran, item, Decimal.Parse(newsplitqty.Value), newsplitlocation.Value);

                            newtrancost += newsplit.BaseQty.Value * newsplit.UnitCost.Value;
                            newtranqty  += newsplit.BaseQty.Value;
                        }

                        break;
                    }
                    else
                    {
                        INTranSplit newsplit = this.addReceiptSplitLine(receiptEntry, stat, lotstat, transitline, tran, item, newtranqty, null);

                        newtrancost += newsplit.BaseQty.Value * newsplit.UnitCost.Value;
                        newtranqty  += newsplit.BaseQty.Value;
                    }
                }

                receiptEntry.UpdateTranCostQty(newtran, newtranqty, newtrancost);
            }
        }