コード例 #1
0
        protected override bool BeforeSave(bool newRecord)
        {
            // Movement Quantity can not be less than 0 when planned is greater than 0
            if (GetPlannedQty() > 0 && GetMovementQty() < 0)
            {
                log.SaveWarning("Warning", Msg.GetMsg(GetCtx(), "MovementQtyCantbelesszero"));
                return(false);
            }

            // Movement Quantity can not be greater than 0 when planned is less than 0
            if (GetPlannedQty() < 0 && GetMovementQty() > 0)
            {
                log.SaveWarning("Warning", Msg.GetMsg(GetCtx(), "MovementQtyCantbegrtzero"));
                return(false);
            }

            MProduct product = new MProduct(GetCtx(), GetM_Product_ID(), Get_TrxName());

            if (newRecord && product != null)
            {
                if (Util.GetValueOfString(product.Get_Value("IsSlopeOil")) == "True" && GetMovementQty() < 0)
                {
                    log.SaveWarning("Warning", Msg.GetMsg(GetCtx(), "MovementQtyCantbelesszero"));
                    return(false);
                }
                if (Util.GetValueOfString(product.Get_Value("IsSlopeOil")) == "False" && GetMovementQty() > 0)
                {
                    log.SaveWarning("Warning", Msg.GetMsg(GetCtx(), "MovementQtyCantbegrtzero"));
                    return(false);
                }
            }

            X_M_ProductionPlan plan = new X_M_ProductionPlan(GetCtx(), GetM_ProductionPlan_ID(), Get_TrxName());

            if (Util.GetValueOfDecimal(plan.Get_Value("GOM01_ComponentLimit")) > 0 && GetPlannedQty() < 0)
            {
                decimal ComputedQty = (GetPlannedQty() * Util.GetValueOfDecimal(plan.Get_Value("GOM01_ComponentLimit"))) / 100;

                ComputedQty = decimal.Negate(GetPlannedQty() + ComputedQty);

                if (decimal.Negate(GetMovementQty()) > ComputedQty)
                {
                    log.SaveWarning("Warning", Msg.GetMsg(GetCtx(), "GOM01_QtyConsumption"));
                    return(false);
                }
            }

            // when warehouse disallow negative inventory is false then on hand qty can't be in negative
            wh = MWarehouse.Get(GetCtx(), GetM_Warehouse_ID());
            if (wh.IsDisallowNegativeInv() && GetM_Product_ID() > 0)
            {
                product = MProduct.Get(GetCtx(), GetM_Product_ID());
                string qry = "SELECT NVL(SUM(NVL(QtyOnHand,0)),0) AS QtyOnHand FROM M_Storage where m_locator_id=" + GetM_Locator_ID() + " and m_product_id=" + GetM_Product_ID();
                //if (GetM_AttributeSetInstance_ID() != 0)
                //{
                qry += " AND NVL(M_AttributeSetInstance_ID , 0) =" + GetM_AttributeSetInstance_ID();
                //}
                Decimal?OnHandQty = Convert.ToDecimal(DB.ExecuteScalar(qry));

                qry = @"SELECT NVL(SUM(MovementQty) , 0) FROM M_ProductionLine WHERE IsActive = 'Y' AND  M_Locator_ID=" + GetM_Locator_ID() + @" AND m_product_id=" + GetM_Product_ID() +
                      @" AND NVL(M_AttributeSetInstance_ID , 0) =" + GetM_AttributeSetInstance_ID() + @" AND M_Production_ID = " + GetM_Production_ID();
                if (!newRecord)
                {
                    qry += @" AND M_ProductionLine_ID <> " + GetM_ProductionLine_ID();
                }
                Decimal?moveQty = Convert.ToDecimal(DB.ExecuteScalar(qry));
                if ((OnHandQty + GetMovementQty() + moveQty) < 0)
                {
                    log.SaveError("", product.GetName() + ", " + Msg.GetMsg(GetCtx(), "VIS_InsufficientQty") + OnHandQty);
                    return(false);
                }
            }

            return(true);
        }