private void GetWhereClause(VAdvantage.Model.MOrder ord)
 {
     //  StringBuilder where = new StringBuilder();
     //if (ord.GetC_BPartner_ID() != 0)
     //{
     //    whereClause.Append(" AND fa.C_BPartner_ID = " + ord.GetC_BPartner_ID());
     //}
     //else
     //{
     //    whereClause.Append(" AND fa.C_BPartner_ID is null ");
     //}
     if (ord.GetC_Campaign_ID() != 0)
     {
         whereClause.Append(" AND fa.C_Campaign_ID = " + ord.GetC_Campaign_ID());
     }
     else
     {
         whereClause.Append(" AND fa.C_Campaign_ID is null ");
     }
     if (ord.GetC_Activity_ID() != 0)
     {
         whereClause.Append(" AND fa.C_Activity_ID = " + ord.GetC_Activity_ID());
     }
     else
     {
         whereClause.Append(" AND fa.C_Activity_ID is null ");
     }
     if (ord.GetC_Project_ID() != 0)
     {
         whereClause.Append(" AND fa.C_Project_ID = " + ord.GetC_Project_ID());
     }
     else
     {
         whereClause.Append(" AND fa.C_Project_ID is null ");
     }
     //if (ord.GetUser1_ID() != 0)
     //{
     //    whereClause.Append(" AND fa.User1_ID = " + ord.GetUser1_ID());
     //}
     //else
     //{
     //    whereClause.Append(" AND fa.User1_ID is null ");
     //}
     //if (ord.GetUser2_ID() != 0)
     //{
     //    whereClause.Append(" AND fa.User2_ID = " + ord.GetUser2_ID());
     //}
     //else
     //{
     //    whereClause.Append(" AND fa.User2_ID is null ");
     //}
     //if (ord.GetAD_OrgTrx_ID() != 0)
     //{
     //    whereClause.Append(" AND fa.AD_OrgTrx_ID = " + ord.GetAD_OrgTrx_ID());
     //}
     //else
     //{
     //    whereClause.Append(" AND fa.AD_OrgTrx_ID is null ");
     //}
     //if (ord.GetC_BPartner_ID() != 0)
     //{
     //    where.Append("C_BPartner_ID = " + ord.GetC_BPartner_ID());
     //}
 }
예제 #2
0
        protected override string DoIt()
        {
            // Int32 value = 0;
            string msg = "";

            log.Info("C_Project_ID=" + _C_Project_ID);
            if (_C_Project_ID == 0)
            {
                throw new ArgumentException("C_Project_ID == 0");
            }

            VAdvantage.Model.MProject fromProject = new VAdvantage.Model.MProject(GetCtx(), _C_Project_ID, null);

            if (fromProject.GetGenerate_Quotation() == null)
            {
                throw new ArgumentException("No Generate Quotation found on Project.");
            }
            if (fromProject.GetGenerate_Quotation().Trim() == "Y")
            {
                throw new ArgumentException("Sales Quotation already generated");
            }
            VAdvantage.Model.MOrder order = new VAdvantage.Model.MOrder(GetCtx(), 0, null);
            C_Bpartner_id          = fromProject.GetC_BPartner_ID();
            C_Bpartner_Location_id = fromProject.GetC_BPartner_Location_ID();
            C_BPartnerSR_ID        = fromProject.GetC_BPartnerSR_ID();
            //MBPartner bp = new MBPartner(GetCtx(), C_Bpartner_id, null);
            VAdvantage.Model.MBPartnerLocation bpartnerloc = new VAdvantage.Model.MBPartnerLocation(GetCtx(), C_Bpartner_Location_id, null);
            String currentdate = DateTime.Now.ToString();
            String sqlprjln    = " select c_projectline_id from c_projectline where c_project_id=" + _C_Project_ID + "";

            C_ProjectLine_ID = VAdvantage.Utility.Util.GetValueOfInt(DB.ExecuteScalar(sqlprjln));
            if (C_ProjectLine_ID != 0)
            {
                order.SetDateOrdered(Convert.ToDateTime(currentdate));
                order.SetDatePromised(Convert.ToDateTime(currentdate));
                if (C_Bpartner_id != 0)
                {
                    order.SetC_BPartner_ID(fromProject.GetC_BPartner_ID());
                    if (bpartnerloc.IsShipTo() == true)
                    {
                        order.SetC_BPartner_Location_ID(fromProject.GetC_BPartner_Location_ID());
                        order.SetAD_User_ID(fromProject.GetAD_User_ID());
                    }
                    if (bpartnerloc.IsBillTo() == true)
                    {
                        order.SetBill_Location_ID(fromProject.GetC_BPartner_Location_ID());
                        order.SetBill_User_ID(fromProject.GetAD_User_ID());
                    }
                }
                if (C_BPartnerSR_ID != 0)
                {
                    //String sqlcust = "update c_bpartner set iscustomer='Y', isprospect='N' where c_bpartner_id=" + C_BPartnerSR_ID + "";
                    //value = DB.ExecuteQuery(sqlcust, null, null);
                    //if (value == -1)
                    //{

                    //}
                    //bp.SetIsCustomer(true);
                    //bp.SetIsProspect(false);

                    order.SetC_BPartner_ID(fromProject.GetC_BPartnerSR_ID());
                    if (bpartnerloc.IsShipTo() == true)
                    {
                        order.SetC_BPartner_Location_ID(fromProject.GetC_BPartner_Location_ID());
                        order.SetAD_User_ID(fromProject.GetAD_User_ID());
                    }
                    if (bpartnerloc.IsBillTo() == true)
                    {
                        order.SetBill_Location_ID(fromProject.GetC_BPartner_Location_ID());
                        order.SetBill_User_ID(fromProject.GetAD_User_ID());
                    }
                }
                // String sql = "select c_doctype_id from c_doctype where docbasetype= 'SOO' and  = 'Sales Quotation'";
                String sql           = "select c_doctype_id from c_doctype where docbasetype = 'SOO' and docsubtypeso = 'ON' and isreturntrx = 'N' and ad_client_id = " + GetCtx().GetAD_Client_ID();
                int    Doctype_id    = VAdvantage.Utility.Util.GetValueOfInt(DB.ExecuteScalar(sql));
                int    MPriceList_id = Util.GetValueOfInt(fromProject.GetM_PriceList_ID());
                order.SetM_PriceList_ID(MPriceList_id);
                ////String sqlmpricelist = "select m_pricelist_id from m_pricelist where name='Export'";
                ////int MPriceList_id = VAdvantage.Utility.Util.GetValueOfInt(DB.ExecuteScalar(sqlmpricelist));
                //if (MPriceList_id == order.GetM_PriceList_ID())
                //{
                //    String sqlconversiontype = "select c_conversiontype_id from c_conversiontype where value = 'C'";
                //    int C_ConversionType_id = VAdvantage.Utility.Util.GetValueOfInt(DB.ExecuteScalar(sqlconversiontype));
                //    order.SetC_ConversionType_ID(C_ConversionType_id);
                //}
                order.SetC_Project_ID(GetRecord_ID());
                if (fromProject.GetSalesRep_ID() > 0)
                {
                    order.SetSalesRep_ID(fromProject.GetSalesRep_ID());
                }
                order.SetC_Currency_ID(fromProject.GetC_Currency_ID());
                if (C_Bpartner_id != 0)
                {
                    VAdvantage.Model.MBPartner bp = new VAdvantage.Model.MBPartner(GetCtx(), C_Bpartner_id, null);
                    if (bp.GetC_Campaign_ID() == 0 && fromProject.GetC_Campaign_ID() > 0)
                    {
                        bp.SetC_Campaign_ID(fromProject.GetC_Campaign_ID());
                    }
                    bp.SetAD_Client_ID(fromProject.GetAD_Client_ID());
                    bp.SetAD_Org_ID(fromProject.GetAD_Org_ID());
                    if (bp.GetC_PaymentTerm_ID() != 0)
                    {
                        order.SetPaymentMethod(bp.GetPaymentRule());
                        order.SetC_PaymentTerm_ID(bp.GetC_PaymentTerm_ID());
                    }

                    if (!bp.Save())
                    {
                        log.SaveError("CampaignIDNotSaved", "");
                        return(Msg.GetMsg(GetCtx(), "CampaignIDtNotSaved"));
                    }
                }
                else
                {
                    VAdvantage.Model.MBPartner bp = new VAdvantage.Model.MBPartner(GetCtx(), C_BPartnerSR_ID, null);
                    if (bp.GetC_Campaign_ID() == 0 && fromProject.GetC_Campaign_ID() > 0)
                    {
                        bp.SetC_Campaign_ID(fromProject.GetC_Campaign_ID());
                    }
                    bp.SetAD_Client_ID(fromProject.GetAD_Client_ID());
                    bp.SetAD_Org_ID(fromProject.GetAD_Org_ID());
                    if (bp.GetC_PaymentTerm_ID() != 0)
                    {
                        order.SetPaymentMethod(bp.GetPaymentRule());
                        order.SetC_PaymentTerm_ID(bp.GetC_PaymentTerm_ID());
                    }

                    if (!bp.Save())
                    {
                        log.SaveError("CampaignIDtNotSaved", "");
                        return(Msg.GetMsg(GetCtx(), "CampaignIDtNotSaved"));
                    }
                }
                //if (bp.GetC_PaymentTerm_ID() != 0)
                //{
                //    order.SetPaymentMethod(bp.GetPaymentRule());
                //    order.SetC_PaymentTerm_ID(bp.GetC_PaymentTerm_ID());

                //}
                order.SetFreightCostRule("I");
                if (order.GetC_Campaign_ID() == 0 && fromProject.GetC_Campaign_ID() > 0)
                {
                    order.SetC_Campaign_ID(fromProject.GetC_Campaign_ID());
                }
                order.SetDocStatus("IP");
                order.SetC_DocType_ID(Doctype_id);
                order.SetC_DocTypeTarget_ID(Doctype_id);
                order.SetIsSOTrx(true);
                if (!order.Save())
                {
                    log.SaveError("SaleOrdertNotSaved", "");
                    return(Msg.GetMsg(GetCtx(), "SaleOrdertNotSaved"));
                }
                //Order Lines
                int count = 0;
                VAdvantage.Model.MProjectLine[] lines = fromProject.GetLines();
                for (int i = 0; i < lines.Length; i++)
                {
                    VAdvantage.Model.MOrderLine ol = new VAdvantage.Model.MOrderLine(order);
                    ol.SetLine(lines[i].GetLine());
                    ol.SetDescription(lines[i].GetDescription());
                    ol.SetM_Product_ID(lines[i].GetM_Product_ID(), true);
                    ol.SetQtyEntered(lines[i].GetPlannedQty());
                    ol.SetQtyOrdered(lines[i].GetPlannedQty());
                    ol.SetPriceEntered(lines[i].GetPlannedPrice());
                    ol.SetPriceActual(lines[i].GetPlannedPrice());
                    ol.SetPriceList(lines[i].GetPriceList());
                    if (ol.Save())
                    {
                        count++;
                    }
                }

                fromProject.SetRef_Order_ID(order.GetC_Order_ID());
                fromProject.SetGenerate_Quotation("Y");
                if (!fromProject.Save())
                {
                    log.SaveError("ProjectNotSaved", "");
                    return(Msg.GetMsg(GetCtx(), "ProjectNotSaved"));;
                }
                msg = Msg.GetMsg(GetCtx(), "QuotationGenerated");
            }
            else
            {
                msg = Msg.GetMsg(GetCtx(), "No Lines");
            }
            return(msg);
        }
        //
        private int GetBudgetAmount(int AccID, DateTime?startDate, DateTime?endDate, VAdvantage.Model.MOrder ord)
        {
            int B_ID      = 0;
            int budget_ID = 0;

            bgtAmount = 0;
            sql.Clear();
            int         C_Project_ID  = Util.GetValueOfInt(ord.GetC_Project_ID());
            int         C_Campaign_ID = Util.GetValueOfInt(ord.GetC_Campaign_ID());
            int         C_Activity_ID = Util.GetValueOfInt(ord.GetC_Activity_ID());
            IDataReader idr           = null;

            if (C_Project_ID != 0 && C_Campaign_ID != 0 && C_Activity_ID != 0)
            {
                // find Budget against all three Parameters
                budget_ID = GetBudgetAgainstProjectCampaignActivity(AccID, startDate, endDate, C_Project_ID, C_Campaign_ID, C_Activity_ID);
                // if not found then find against Project and Campaign
                if (budget_ID == 0)
                {
                    budget_ID = GetBudgetAgainstProjectCampaign(AccID, startDate, endDate, C_Project_ID, C_Campaign_ID);
                }
                // Then Against Project and Activity
                if (budget_ID == 0)
                {
                    budget_ID = GetBudgetAgainstProjectActivity(AccID, startDate, endDate, C_Project_ID, C_Activity_ID);
                }
                // Then Against Campaign and Activity
                if (budget_ID == 0)
                {
                    budget_ID = GetBudgetAgainstCampaignActivity(AccID, startDate, endDate, C_Activity_ID, C_Campaign_ID);
                }
                // Against Project
                if (budget_ID == 0)
                {
                    budget_ID = GetBudgetAgainstProject(AccID, startDate, endDate, C_Project_ID);
                }
                // Against Project
                if (budget_ID == 0)
                {
                    budget_ID = GetBudgetAgainstCampaign(AccID, startDate, endDate, C_Campaign_ID);
                }
                // Against Activity
                if (budget_ID == 0)
                {
                    budget_ID = GetBudgetAgainstActivity(AccID, startDate, endDate, C_Activity_ID);
                }
            }
            else if (C_Project_ID != 0 && C_Campaign_ID != 0)
            {
                budget_ID = GetBudgetAgainstProjectCampaign(AccID, startDate, endDate, C_Project_ID, C_Campaign_ID);
                if (budget_ID == 0)
                {
                    budget_ID = GetBudgetAgainstProject(AccID, startDate, endDate, C_Project_ID);
                }
                if (budget_ID == 0)
                {
                    budget_ID = GetBudgetAgainstCampaign(AccID, startDate, endDate, C_Campaign_ID);
                }
            }
            else if (C_Campaign_ID != 0 && C_Activity_ID != 0)
            {
                budget_ID = GetBudgetAgainstCampaignActivity(AccID, startDate, endDate, C_Campaign_ID, C_Activity_ID);
                if (budget_ID == 0)
                {
                    budget_ID = GetBudgetAgainstCampaign(AccID, startDate, endDate, C_Campaign_ID);
                }
                if (budget_ID == 0)
                {
                    budget_ID = GetBudgetAgainstActivity(AccID, startDate, endDate, C_Activity_ID);
                }
            }
            else if (C_Project_ID != 0 && C_Activity_ID != 0)
            {
                budget_ID = GetBudgetAgainstProjectActivity(AccID, startDate, endDate, C_Project_ID, C_Activity_ID);
                if (budget_ID == 0)
                {
                    budget_ID = GetBudgetAgainstProject(AccID, startDate, endDate, C_Project_ID);
                }
                if (budget_ID == 0)
                {
                    budget_ID = GetBudgetAgainstActivity(AccID, startDate, endDate, C_Activity_ID);
                }
            }
            else if (C_Project_ID != 0)
            {
                budget_ID = GetBudgetAgainstProject(AccID, startDate, endDate, C_Project_ID);
            }
            else if (C_Campaign_ID != 0)
            {
                budget_ID = GetBudgetAgainstCampaign(AccID, startDate, endDate, C_Campaign_ID);
            }
            else if (C_Activity_ID != 0)
            {
                budget_ID = GetBudgetAgainstActivity(AccID, startDate, endDate, C_Activity_ID);
            }

            // If Budget Not Found For Any Dimension Then look for Account + Organization
            if (budget_ID == 0)
            {
                try
                {
                    sql.Clear();
                    sql.Append("SELECT fa.AmtAcctDr, b.GL_Budget_ID FROM Fact_Acct fa inner join GL_Budget b ON (b.GL_Budget_ID = fa.GL_Budget_ID) WHERE fa.Account_ID = " + AccID + " AND fa.DateAcct >= " + GlobalVariable.TO_DATE(startDate, false) + " AND fa.DateAcct <= " + GlobalVariable.TO_DATE(endDate, false) + " AND fa.AD_Org_ID = " + _AD_Org_ID + " AND fa.AD_Client_ID = " + _AD_Client_ID + " AND fa.GL_Budget_ID IS Not null AND b.BudgetControlBasis = '" + controlBasis + "' ORDER BY fa.dateacct DESC");
                    idr = DB.ExecuteReader(sql.ToString(), null, null);
                    while (idr.Read())
                    {
                        if (B_ID == 0)
                        {
                            B_ID = Util.GetValueOfInt(idr[1]);
                        }
                        if (B_ID.Equals(Util.GetValueOfInt(idr[1])))
                        {
                            budget_ID = Util.GetValueOfInt(idr[1]);
                            bgtAmount = Util.GetValueOfDecimal(idr[0]);
                        }
                    }
                    idr.Close();
                    idr = null;
                }
                catch
                {
                    idr.Close();
                    idr = null;
                }
            }

            #region
            //while (!gotID)
            //{
            //    finalWhereClause.Clear();
            //    if (ord.GetC_Campaign_ID() != 0)
            //    {
            //        finalWhereClause.Append(" AND fa.C_Campaign_ID = " + ord.GetC_Campaign_ID());
            //    }
            //    else
            //    {
            //        finalWhereClause.Append(" AND fa.C_Campaign_ID is null ");
            //    }
            //    if (ord.GetC_Activity_ID() != 0)
            //    {
            //        finalWhereClause.Append(" AND fa.C_Activity_ID = " + ord.GetC_Activity_ID());
            //    }
            //    else
            //    {
            //        finalWhereClause.Append(" AND fa.C_Activity_ID is null ");
            //    }
            //    if (ord.GetC_Project_ID() != 0)
            //    {
            //        finalWhereClause.Append(" AND fa.C_Project_ID = " + ord.GetC_Project_ID());
            //    }
            //    else
            //    {
            //        finalWhereClause.Append(" AND fa.C_Project_ID is null ");
            //    }

            //    sql.Append("SELECT fa.AmtAcctDr, b.GL_Budget_ID FROM Fact_Acct fa inner join GL_Budget b ON (b.GL_Budget_ID = fa.GL_Budget_ID) " + whereClause + " AND fa.GL_Budget_ID IS Not null AND b.BudgetControlBasis = '" + controlBasis + "' ORDER BY fa.DateAcct DESC");
            //    // IDataReader idr = null;
            //    try
            //    {
            //        // Amount = Util.GetValueOfDecimal(DB.ExecuteScalar(sql.ToString(), null, null));
            //        idr = DB.ExecuteReader(sql.ToString(), null, null);
            //        while (idr.Read())
            //        {
            //            gotID = true;
            //            if (B_ID == 0)
            //            {
            //                B_ID = Util.GetValueOfInt(idr[1]);
            //            }
            //            if (B_ID.Equals(Util.GetValueOfInt(idr[1])))
            //            {
            //                budget_ID = Util.GetValueOfInt(idr[1]);
            //                bgtAmount = Decimal.Add(bgtAmount.Value, Util.GetValueOfDecimal(idr[0]));
            //            }
            //        }

            //        idr.Close();
            //        idr = null;

            //        //if (budget_ID.Equals(0))
            //        //{
            //        //    sql.Clear();
            //        //    sql.Append("SELECT fa.AmtAcctDr, b.GL_Budget_ID FROM Fact_Acct fa inner join GL_Budget b ON (b.GL_Budget_ID = fa.GL_Budget_ID) WHERE fa.Account_ID = " + AccID + " AND fa.DateAcct >= " + GlobalVariable.TO_DATE(startDate, false) + " AND fa.DateAcct <= " + GlobalVariable.TO_DATE(endDate, false) + " AND fa.AD_Org_ID = " + _AD_Org_ID + " AND fa.AD_Client_ID = " + _AD_Client_ID + " AND fa.GL_Budget_ID IS Not null AND b.BudgetControlBasis = '" + controlBasis + "' AND RowNum = 1 ORDER BY fa.dateacct DESC");
            //        //    idr = DB.ExecuteReader(sql.ToString(), null, null);
            //        //    while (idr.Read())
            //        //    {
            //        //        budget_ID = Util.GetValueOfInt(idr[1]);
            //        //        bgtAmount = Util.GetValueOfDecimal(idr[0]);
            //        //        break;
            //        //    }

            //        //    idr.Close();
            //        //    idr = null;
            //        //}
            //    }
            //    catch
            //    {
            //        idr.Close();
            //        idr = null;
            //    }

            //}

            //sql.Append("SELECT GL_Budget_ID FROM GL_Budget WHERE " + GlobalVariable.TO_DATE(dateAcct, false) + " >= FromDate AND " + GlobalVariable.TO_DATE(dateAcct, false) + " <= ToDate AND IsActive = 'Y' AND AD_Client_ID = " + _AD_Client_ID);
            // sql.Append("SELECT GL_Budget_ID FROM GL_Budget WHERE AD_Client_ID = " + _AD_Client_ID + " AND BudgetControlBasis = '" + controlBasis + "'");
            // int GL_Budget_ID = Util.GetValueOfInt(DB.ExecuteScalar(sql.ToString(), null, null));
            // sql.Clear();
            //sql.Append("SELECT GL_Budget_ID FROM GL_Budget WHERE " + GlobalVariable.TO_DATE(dateAcct, false) + " >= FromDate AND " + GlobalVariable.TO_DATE(dateAcct, false) + " <= ToDate AND IsActive = 'Y' AND AD_Client_ID = " + _AD_Client_ID);
            #endregion
            return(budget_ID);
        }