Exemplo n.º 1
0
        //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        //Used to Remove Articles from DocumentOrder ex when we Delete Article From TicketList.OrderMain Details

        public decimal DeleteFromDocumentOrder(ArticleBagKey pKey, decimal pRemoveQuantity)
        {
            bool    isDone = false;
            decimal resultRemainQuantity = 0;

            string where = string.Empty;
            //Store Reference to Future delete Object (After foreach Loop)
            fin_documentordermain   deleteOrderMain   = null;
            fin_documentorderticket deleteOrderTicket = null;
            fin_documentorderdetail deleteOrderDetail = null;
            string articleDesignation = string.Empty;

            //Start UnitOfWork
            using (UnitOfWork uowSession = new UnitOfWork())
            {
                OrderMain             orderMain          = GlobalFramework.SessionApp.OrdersMain[GlobalFramework.SessionApp.CurrentOrderMainOid];
                fin_documentordermain xDocumentOrderMain = (fin_documentordermain)FrameworkUtils.GetXPGuidObject(uowSession, typeof(fin_documentordermain), orderMain.PersistentOid);

                if (xDocumentOrderMain != null && xDocumentOrderMain.OrderTicket != null)
                {
                    foreach (fin_documentorderticket ticket in xDocumentOrderMain.OrderTicket)
                    {
                        foreach (fin_documentorderdetail detail in ticket.OrderDetail)
                        {
                            try
                            {
                                //Check Equal Key
                                if (pKey.ArticleOid == detail.Article.Oid && pKey.Price == detail.Price && pKey.Discount == detail.Discount && pKey.Vat == detail.Vat)
                                {
                                    articleDesignation    = pKey.Designation;
                                    resultRemainQuantity += detail.Quantity;
                                    if (!isDone)
                                    {
                                        detail.Quantity -= pRemoveQuantity;
                                        //Assign references to Future Deletes
                                        if (detail.Quantity <= 0)
                                        {
                                            deleteOrderDetail = detail;
                                        }
                                        isDone = true;
                                    }
                                    else
                                    {
                                        where += string.Format(" OR Oid = '{0}'", detail.Oid);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                _log.Error(ex.Message, ex);
                            }
                        }
                    }
                }

                //Debug
                //string sql = @"SELECT * FROM fin_documentorderdetail WHERE 1=0{0};";
                //_log.Debug(string.Format("Delete(): sql [{0}]", string.Format(sql, where)));

                //Audit
                FrameworkUtils.Audit("ORDER_ARTICLE_REMOVED", string.Format(
                                         resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "audit_message_order_article_removed"),
                                         articleDesignation,
                                         1,
                                         resultRemainQuantity - 1,
                                         GlobalFramework.LoggedUser.Name
                                         )
                                     );

                if (isDone)
                {
                    //Update xDocumentOrderMain UpdatedAt, Required for RealTime Update
                    xDocumentOrderMain.UpdatedAt = FrameworkUtils.CurrentDateTimeAtomic();

                    //Remove Quantity
                    resultRemainQuantity -= pRemoveQuantity;

                    //Delete Records, OrderMain, OrderTicket and OrderDetails
                    if (deleteOrderDetail != null)
                    {
                        deleteOrderTicket = deleteOrderDetail.OrderTicket;
                        deleteOrderMain   = deleteOrderTicket.OrderMain;

                        //Delete Details
                        deleteOrderDetail.Delete();

                        //Check if OrderTicket in Empty, If so Delete it, its not required anymore
                        if (deleteOrderTicket.OrderDetail.Count <= 0)
                        {
                            deleteOrderTicket.Delete();
                        }
                        ;

                        //Check if OrderMain in Empty, If so Delete it, its not required anymore
                        if (deleteOrderMain.OrderTicket.Count <= 0)
                        {
                            //Before Delete OrderMain, we must UnAssign DocumentMaster SourceOrderMain else we have a CONSTRAINT ERROR on FK_DocumentFinanceMaster_SourceOrderMain trying to delete used OrderMain
                            string sql = string.Format(@"UPDATE fin_documentfinancemaster SET SourceOrderMain = NULL WHERE SourceOrderMain = '{0}';", deleteOrderMain.Oid);
                            uowSession.ExecuteScalar(sql);
                            //Open Table
                            deleteOrderMain.PlaceTable.TableStatus = TableStatus.Free;
                            //Audit
                            FrameworkUtils.Audit("TABLE_OPEN", string.Format(resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "audit_message_table_open"), deleteOrderMain.PlaceTable.Designation));
                            //Delete OrderMain
                            deleteOrderMain.Delete();
                        }
                        ;
                    }
                    ;
                }
                ;

                try
                {
                    //Commit UOW Changes
                    uowSession.CommitChanges();
                    //Update OrderMain UpdatedAt, Required to Sync Terminals
                    orderMain.UpdatedAt = FrameworkUtils.CurrentDateTimeAtomic();

                    //Update ArticleBag Price Properties
                    this[pKey].Quantity = resultRemainQuantity;
                    UpdateKeyProperties(pKey);

                    //SEARCH#001
                    //Require to Remove PartialPayed Items Quantity
                    return(resultRemainQuantity - FrameworkUtils.GetPartialPaymentPayedItems(uowSession, xDocumentOrderMain.Oid, pKey.ArticleOid));
                }
                catch (Exception ex)
                {
                    _log.Error(ex.Message, ex);
                    uowSession.RollbackTransaction();
                    return(-1);
                }
            }
        }