protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            //Leave UserCode region to enable user code saving
            #region UserCode
            var returnValue = new FR_Guid();

            // Get shopping cart status - Ordered
            var statusOrdered = CL1_ORD_PRC.ORM_ORD_PRC_ShoppingCart_Status.Query.Search(Connection, Transaction,
                                                                                         new CL1_ORD_PRC.ORM_ORD_PRC_ShoppingCart_Status.Query()
            {
                GlobalPropertyMatchingID = DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(DLCore_DBCommons.APODemand.EShoppingCartStatus.Ordered),
                Tenant_RefID             = securityTicket.TenantID,
                IsDeleted = false
            }).Single();

            var tenant = securityTicket.TenantID;

            #region Find All Shopping Carts in Approved state for TenantID

            var statusApproved = CL1_ORD_PRC.ORM_ORD_PRC_ShoppingCart_Status.Query.Search(Connection, Transaction,
                                                                                          new CL1_ORD_PRC.ORM_ORD_PRC_ShoppingCart_Status.Query
            {
                Tenant_RefID             = securityTicket.TenantID,
                GlobalPropertyMatchingID = DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(DLCore_DBCommons.APODemand.EShoppingCartStatus.Approved),
                IsDeleted = false
            }).Single();

            var approvedShoppingCarts = CL1_ORD_PRC.ORM_ORD_PRC_ShoppingCart.Query.Search(Connection, Transaction,
                                                                                          new CL1_ORD_PRC.ORM_ORD_PRC_ShoppingCart.Query
            {
                ShoppingCart_CurrentStatus_RefID = statusApproved.ORD_PRC_ShoppingCart_StatusID,
                Tenant_RefID = securityTicket.TenantID,
                IsDeleted    = false
            });

            if (approvedShoppingCarts == null || approvedShoppingCarts.Count() == 0)
            {
                returnValue.Result = Guid.Empty;
                return(returnValue);
            }
            #endregion

            #region Procurement Order
            // Get procurement order status
            var procurementOrderStatus = CL1_ORD_PRC.ORM_ORD_PRC_ProcurementOrder_Status.Query.Search(Connection, Transaction,
                                                                                                      new CL1_ORD_PRC.ORM_ORD_PRC_ProcurementOrder_Status.Query()
            {
                GlobalPropertyMatchingID = DLCore_DBCommons.Utils.EnumUtils.GetEnumDescription(DLCore_DBCommons.APODemand.EProcurementStatus.Active),
                Tenant_RefID             = securityTicket.TenantID,
                IsDeleted = false
            }).Single();

            var account = new CL1_USR.ORM_USR_Account();
            account.Load(Connection, Transaction, securityTicket.AccountID);

            #region

            var incrNumberParam = new P_L2NR_GaIINfUA_1454()
            {
                GlobalStaticMatchingID = EnumUtils.GetEnumDescription(ENumberRangeUsageAreaType.ProcurementOrderNumber)
            };
            var procurementNumber = cls_Get_and_Increase_IncreasingNumber_for_UsageArea.Invoke(Connection, Transaction, incrNumberParam, securityTicket).Result.Current_IncreasingNumber;

            #endregion

            // Create procurement order header
            var procurementOrderHeader = new CL1_ORD_PRC.ORM_ORD_PRC_ProcurementOrder_Header();
            procurementOrderHeader.Tenant_RefID = securityTicket.TenantID;
            procurementOrderHeader.CreatedBy_BusinessParticipant_RefID  = account.BusinessParticipant_RefID;
            procurementOrderHeader.Current_ProcurementOrderStatus_RefID = procurementOrderStatus.ORD_PRC_ProcurementOrder_StatusID;
            procurementOrderHeader.ProcurementOrder_Supplier_RefID      = Guid.Empty;
            procurementOrderHeader.ProcurementOrder_Date   = DateTime.Now;
            procurementOrderHeader.TotalValue_BeforeTax    = 0;
            procurementOrderHeader.ProcurementOrder_Number = procurementNumber;
            procurementOrderHeader.Save(Connection, Transaction);

            // Create procurement order status history
            var procurementOrderStatusHistory = new CL1_ORD_PRC.ORM_ORD_PRC_ProcurementOrder_StatusHistory();
            procurementOrderStatusHistory.Tenant_RefID = securityTicket.TenantID;
            procurementOrderStatusHistory.ProcurementOrder_Header_RefID = procurementOrderHeader.ORD_PRC_ProcurementOrder_HeaderID;
            procurementOrderStatusHistory.ProcurementOrder_Status_RefID = procurementOrderStatus.ORD_PRC_ProcurementOrder_StatusID;
            procurementOrderStatusHistory.Save(Connection, Transaction);

            // Set return value
            returnValue.Result = procurementOrderHeader.ORD_PRC_ProcurementOrder_HeaderID;
            #endregion

            foreach (var item in approvedShoppingCarts)
            {
                #region Procurement Order Shopping Products

                var param = new P_L5AWSSC_GSPfSC_1650();
                param.ShoppingCartID = item.ORD_PRC_ShoppingCartID;
                var shoppingProducts = cls_Get_ShoppingProducts_for_ShoppingCartID.Invoke(Connection, Transaction, param, securityTicket).Result;

                //when office is deleted but there is ordered items for it
                if (shoppingProducts == null)
                {
                    continue;
                }

                // Create and save procurement order for shopping products
                foreach (var shoppingProduct in shoppingProducts.Products)
                {
                    #region Skip if Product is not good

                    if (shoppingProduct.IsProductCanceled || shoppingProduct.IsProductDeleted)
                    {
                        continue;
                    }

                    #endregion

                    var procurementOrderPosition = new CL1_ORD_PRC.ORM_ORD_PRC_ProcurementOrder_Position();
                    procurementOrderPosition.ORD_PRC_ProcurementOrder_PositionID = Guid.NewGuid();
                    procurementOrderPosition.ProcurementOrder_Header_RefID       = procurementOrderHeader.ORD_PRC_ProcurementOrder_HeaderID;
                    procurementOrderPosition.Position_Quantity                = shoppingProduct.Quantity;
                    procurementOrderPosition.Position_ValuePerUnit            = shoppingProduct.Price;
                    procurementOrderPosition.Position_ValueTotal              = shoppingProduct.Price * (Decimal)shoppingProduct.Quantity;
                    procurementOrderPosition.Position_Unit_RefID              = Guid.Empty;
                    procurementOrderPosition.Position_RequestedDateOfDelivery = new DateTime();
                    procurementOrderPosition.CMN_PRO_Product_RefID            = shoppingProduct.CMN_PRO_Product_RefID;
                    procurementOrderPosition.CMN_PRO_Product_Release_RefID    = shoppingProduct.CMN_PRO_Product_Release_RefID;
                    procurementOrderPosition.CMN_PRO_Product_Variant_RefID    = shoppingProduct.CMN_PRO_Product_Variant_RefID;
                    procurementOrderPosition.Creation_Timestamp               = DateTime.Now;
                    procurementOrderPosition.Tenant_RefID = securityTicket.TenantID;
                    procurementOrderPosition.IsProductReplacementAllowed = shoppingProduct.IsProductReplacementAllowed;
                    procurementOrderPosition.Save(Connection, Transaction);

                    var shoppingCart2ProcurementOrder = new CL1_ORD_PRC.ORM_ORD_PRC_ShoppingCart_2_ProcurementOrderPosition();
                    shoppingCart2ProcurementOrder.AssignmentID = Guid.NewGuid();
                    shoppingCart2ProcurementOrder.ORD_PRC_ShoppingCart_Product_RefID      = shoppingProduct.ORD_PRC_ShoppingCart_ProductID;
                    shoppingCart2ProcurementOrder.ORD_PRC_ProcurementOrder_Position_RefID = procurementOrderPosition.ORD_PRC_ProcurementOrder_PositionID;
                    shoppingCart2ProcurementOrder.Tenant_RefID = securityTicket.TenantID;
                    shoppingCart2ProcurementOrder.Save(Connection, Transaction);

                    procurementOrderHeader.TotalValue_BeforeTax += procurementOrderPosition.Position_ValueTotal;
                }
                #endregion

                #region ShoppingCartNotes

                var notesParam = new P_L5AWSAR_GSCNfSC_1454()
                {
                    ShoppingCartID = item.ORD_PRC_ShoppingCartID
                };
                var scNotes = cls_Get_ShoppingCart_Notes_for_ShoppingCartID.Invoke(Connection, Transaction, notesParam, securityTicket).Result;

                var count = 1;
                foreach (var scNote in scNotes)
                {
                    if (!scNote.IsNoteForProcurementOrder)
                    {
                        continue;
                    }

                    var officeInfo = ORM_ORD_PRC_Office_ShoppingCart.Query.Search(Connection, Transaction, new ORM_ORD_PRC_Office_ShoppingCart.Query()
                    {
                        ORD_PRC_ShoppingCart_RefID = item.ORD_PRC_ShoppingCartID,
                        IsDeleted = false
                    }).Single();

                    var note = new ORM_ORD_PRC_ProcurementOrder_Note();
                    note.ORD_PRC_ProcurementOrder_NoteID         = Guid.NewGuid();
                    note.ORD_PRC_ProcurementOrder_Header_RefID   = procurementOrderHeader.ORD_PRC_ProcurementOrder_HeaderID;
                    note.ORD_PRC_ProcurementOrder_Position_RefID = Guid.Empty;
                    note.CMN_STR_Office_RefID = officeInfo.CMN_STR_Office_RefID;
                    note.Comment             = scNote.Memo_Text;
                    note.Title               = "";
                    note.NotePublishDate     = scNote.UpdatedOn;
                    note.SequenceOrderNumber = count;
                    note.Tenant_RefID        = securityTicket.TenantID;

                    note.Save(Connection, Transaction);

                    count++;
                }

                #endregion

                #region Shopping Cart

                // Change shopping cart status
                item.ShoppingCart_CurrentStatus_RefID = statusOrdered.ORD_PRC_ShoppingCart_StatusID;
                item.IsProcurementOrderCreated        = true;
                item.Save(Connection, Transaction);

                // Create and save new shopping cart history
                var shoppingHistory = new CL1_ORD_PRC.ORM_ORD_PRC_ShoppingCartStatus_History();
                shoppingHistory.ORD_PRC_ShoppingCart_RefID        = item.ORD_PRC_ShoppingCartID;
                shoppingHistory.ORD_PRC_ShoppingCart_Status_RefID = statusOrdered.ORD_PRC_ShoppingCart_StatusID;
                shoppingHistory.PerformedBy_Account_RefID         = securityTicket.AccountID;
                shoppingHistory.Tenant_RefID = securityTicket.TenantID;
                shoppingHistory.Save(Connection, Transaction);

                #endregion
            }

            //Save TotalValue_BeforeTax after updates
            procurementOrderHeader.Save(Connection, Transaction);

            return(returnValue);

            #endregion UserCode
        }
예제 #2
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L5AWSAR_CAPCfSCBO_1349 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();

            // Find priviges for current user
            var checkParam = new P_L5AWSSC_CUPfSCBO_1414();
            checkParam.ORD_PRC_ShoppingCartID = Parameter.ShoppingCartID;
            var checkResult = cls_Check_User_Permissions_for_ShoppingCart_Before_Order.Invoke(Connection, Transaction, checkParam, securityTicket).Result;

            if (!checkResult.HasPrivileges)
            {
                throw new Exception("User doesn't have privileges to order!");
            }

            if (checkResult.CreateApprovalShoppingCart)
            {
                // Check does user have ABDA articles
                var paramShoppingProducts = new P_L5AWSSC_GSPfSC_1650 {
                    ShoppingCartID = Parameter.ShoppingCartID
                };

                var shoppingProducts            = cls_Get_ShoppingProducts_for_ShoppingCartID.Invoke(Connection, Transaction, paramShoppingProducts, securityTicket).Result;
                var shoppingProductsForApproval = new List <CL1_ORD_PRC.ORM_ORD_PRC_ShoppingCart_Product>();

                foreach (var p in shoppingProducts.Products)
                {
                    if (p.Groups.Count() == 1 &&
                        p.Groups.Select(i => i.Group_GlobalPropertyMatchingID).Contains(EnumUtils.GetEnumDescription(EProductGroup.ABDA)))
                    {
                        var product = new CL1_ORD_PRC.ORM_ORD_PRC_ShoppingCart_Product();
                        product.Load(Connection, Transaction, p.ORD_PRC_ShoppingCart_ProductID);
                        shoppingProductsForApproval.Add(product);
                    }
                }

                // Create new approval shopping cart
                if (shoppingProductsForApproval.Count > 0)
                {
                    #region Create approval shopping cart
                    // Find office for current shopping cart
                    var officeShoppingCart = CL1_ORD_PRC.ORM_ORD_PRC_Office_ShoppingCart.Query.Search(Connection, Transaction,
                                                                                                      new CL1_ORD_PRC.ORM_ORD_PRC_Office_ShoppingCart.Query
                    {
                        ORD_PRC_ShoppingCart_RefID = Parameter.ShoppingCartID,
                        Tenant_RefID = securityTicket.TenantID,
                        IsDeleted    = false
                    }).Single();

                    // Create new shopping cart that will contain ABDA articles
                    var paramCreateShoppingCart = new P_L5AWSAR_CSC_1809();
                    paramCreateShoppingCart.IsWaitingForApproval = true;
                    paramCreateShoppingCart.OfficeID             = officeShoppingCart.CMN_STR_Office_RefID;
                    var newShoppingCartId = cls_Create_ShoppingChart_for_CurrentOffice.Invoke(Connection, Transaction, paramCreateShoppingCart, securityTicket).Result;

                    // Move products from current shopping cart to new shopping cart
                    foreach (var p in shoppingProductsForApproval)
                    {
                        p.ORD_PRC_ShoppingCart_RefID = newShoppingCartId;
                        p.Save(Connection, Transaction);
                    }
                    #endregion

                    #region Copy Comments to new shopping cart
                    // Copy comments from current cart to new cart
                    var comments = CL1_ORD_PRC.ORM_ORD_PRC_ShoppingCart_Note.Query.Search(Connection, Transaction,
                                                                                          new CL1_ORD_PRC.ORM_ORD_PRC_ShoppingCart_Note.Query
                    {
                        ORD_PRC_ShoppingCart_RefID = Parameter.ShoppingCartID,
                        Tenant_RefID = securityTicket.TenantID,
                        IsDeleted    = false
                    });

                    foreach (var comment in comments)
                    {
                        var memo = new CL1_CMN_BPT.ORM_CMN_BPT_Memo();
                        memo.Load(Connection, Transaction, comment.CMN_BPT_Memo_RefID);

                        var newMemo = new CL1_CMN_BPT.ORM_CMN_BPT_Memo();
                        newMemo.CreatedBy_Account_RefID       = memo.CreatedBy_Account_RefID;
                        newMemo.DocumentStructureHeader_RefID = memo.DocumentStructureHeader_RefID;
                        newMemo.Memo_Abbreviation             = memo.Memo_Abbreviation;
                        newMemo.Memo_Date               = memo.Memo_Date;
                        newMemo.Memo_Text               = memo.Memo_Text;
                        newMemo.Memo_Title              = memo.Memo_Title;
                        newMemo.Tenant_RefID            = securityTicket.TenantID;
                        newMemo.UpdatedBy_Account_RefID = memo.UpdatedBy_Account_RefID;
                        newMemo.UpdatedOn               = memo.UpdatedOn;
                        newMemo.Save(Connection, Transaction);

                        var newNote = new CL1_ORD_PRC.ORM_ORD_PRC_ShoppingCart_Note();
                        newNote.CMN_BPT_Memo_RefID         = newMemo.CMN_BPT_MemoID;
                        newNote.ORD_PRC_ShoppingCart_RefID = newShoppingCartId;
                        newNote.Tenant_RefID = securityTicket.TenantID;
                        newNote.Save(Connection, Transaction);
                    }
                    #endregion
                }
            }

            return(returnValue);

            #endregion UserCode
        }
예제 #3
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L5AWSSC_CSC_1653 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            //Leave UserCode region to enable user code saving
            #region UserCode
            var returnValue = new FR_Guid();

            // Shopping Cart for Current Office
            #region Shopping Cart for Current Office
            // Create new or get existing shopping cart for this office
            var officeShoppingCart = CL1_ORD_PRC.ORM_ORD_PRC_Office_ShoppingCart.Query.Search(Connection, Transaction,
                                                                                              new CL1_ORD_PRC.ORM_ORD_PRC_Office_ShoppingCart.Query
            {
                ORD_PRC_ShoppingCart_RefID = Parameter.ORD_PRC_ShoppingCartID,
                Tenant_RefID = securityTicket.TenantID,
                IsDeleted    = false
            }).Single();

            var getParamShoppingCartForCurrentOffice = new P_L5AWSSC_GoCSCfCO_1105();
            getParamShoppingCartForCurrentOffice.OfficeID = officeShoppingCart.CMN_STR_Office_RefID;

            var shoppingCartForCurrentOffice = cls_Get_or_Create_ShoppingCart_for_CurrentOffice.Invoke(Connection, Transaction, getParamShoppingCartForCurrentOffice, securityTicket).Result.Single();

            if (shoppingCartForCurrentOffice.ORD_PRC_Office_ShoppingCartID == Parameter.ORD_PRC_ShoppingCartID)
            {
                throw new Exception("Cannot copy shopping cart because is active shopping cart");
            }
            #endregion

            // Get shopping products for shopping cart id in param
            #region Get shopping products for shopping cart id in param
            var getParam = new P_L5AWSSC_GSPfSC_1650();
            getParam.ShoppingCartID = Parameter.ORD_PRC_ShoppingCartID;

            // Get shopping products for referent shopping cart
            var shoppingProducts = cls_Get_ShoppingProducts_for_ShoppingCartID.Invoke(Connection, Transaction, getParam, securityTicket).Result;
            #endregion

            // Check is product valid
            #region Check is product valid
            foreach (var shopProduct in shoppingProducts.Products)
            {
                // Get product by shopping product
                var product = CL1_CMN_PRO.ORM_CMN_PRO_Product.Query.Search(Connection, Transaction,
                                                                           new CL1_CMN_PRO.ORM_CMN_PRO_Product.Query
                {
                    CMN_PRO_ProductID = shopProduct.CMN_PRO_Product_RefID,
                    Tenant_RefID      = securityTicket.TenantID,
                    IsDeleted         = false,
                }).SingleOrDefault();

                // Remove product
                if (product == null)
                {
                    shopProduct.CMN_PRO_Product_RefID = Guid.Empty;
                }
                // Check is product valid
                else if (!product.IsProductAvailableForOrdering || !product.IsProduct_Article)
                {
                    // Find a duplicate product
                    var duplicateProduct = CL1_CMN_PRO.ORM_CMN_PRO_Product.Query.Search(Connection, Transaction,
                                                                                        new CL1_CMN_PRO.ORM_CMN_PRO_Product.Query
                    {
                        ProductITL                    = product.ProductITL,
                        Tenant_RefID                  = securityTicket.TenantID,
                        IsProduct_Article             = true,
                        IsProductAvailableForOrdering = true,
                        IsDeleted = false
                    }).SingleOrDefault();

                    // Remove product
                    if (duplicateProduct == null)
                    {
                        shopProduct.CMN_PRO_Product_RefID = Guid.Empty;
                    }
                    // Change product with a valid product
                    else
                    {
                        shopProduct.CMN_PRO_Product_RefID         = duplicateProduct.CMN_PRO_ProductID;
                        shopProduct.CMN_PRO_Product_Release_RefID = Guid.Empty;
                        shopProduct.CMN_PRO_Product_Variant_RefID = Guid.Empty;
                    }
                }
            }
            #endregion

            // Saving Products for current shopping cart
            #region Saving Products for current shopping cart
            var saveParam    = new P_L5AWSAR_SSCP_1653();
            var shoppingCart = new List <P_L5AWSAR_SSCP_1653_ShoppingCart>();

            foreach (var item in shoppingProducts.Products)
            {
                // If product is removed
                if (item.CMN_PRO_Product_RefID == Guid.Empty)
                {
                    continue;
                }

                var shoppProductParam = new P_L5AWSAR_SSCP_1653_ShoppingCart();
                var existProduct      = shoppingCartForCurrentOffice.Products.FirstOrDefault(x => x.CMN_PRO_Product_RefID == item.CMN_PRO_Product_RefID);

                if (existProduct != null)
                {
                    shoppProductParam.ORD_PRC_ShoppingCart_ProductID = existProduct.ORD_PRC_ShoppingCart_ProductID;
                    shoppProductParam.Quantity = existProduct.Quantity;
                }

                // Add products to a new shopping cart
                shoppProductParam.ORD_PRC_ShoppingCart_RefID    = shoppingCartForCurrentOffice.ORD_PRC_ShoppingCartID;
                shoppProductParam.CMN_PRO_Product_RefID         = item.CMN_PRO_Product_RefID;
                shoppProductParam.CMN_PRO_Product_Release_RefID = item.CMN_PRO_Product_Release_RefID;
                shoppProductParam.CMN_PRO_Product_Variant_RefID = item.CMN_PRO_Product_Variant_RefID;
                shoppProductParam.Quantity += item.Quantity;
                shoppingCart.Add(shoppProductParam);
            }

            saveParam.ShoppingCart = shoppingCart.ToArray();

            // Save this shopping products
            cls_Save_ShoppingCart_Product.Invoke(Connection, Transaction, saveParam, securityTicket);
            #endregion

            return(new FR_Guid(shoppingCartForCurrentOffice.ORD_PRC_ShoppingCartID));

            #endregion UserCode
        }