Пример #1
0
        protected List <crm_quote_item> degressionItem    = null; // 成本的配置项
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region  拉框赋值
                stage_id.DataTextField  = "show";
                stage_id.DataValueField = "val";
                stage_id.DataSource     = dic.FirstOrDefault(_ => _.Key == "opportunity_stage").Value;
                stage_id.DataBind();

                win_reason_type_id.DataTextField  = "show";
                win_reason_type_id.DataValueField = "val";
                win_reason_type_id.DataSource     = dic.FirstOrDefault(_ => _.Key == "oppportunity_win_reason_type").Value;
                win_reason_type_id.DataBind();
                win_reason_type_id.Items.Insert(0, new ListItem()
                {
                    Value = "0", Text = "   ", Selected = true
                });

                resource_id.DataTextField  = "show";
                resource_id.DataValueField = "val";
                resource_id.DataSource     = dic.FirstOrDefault(_ => _.Key == "sys_resource").Value;
                resource_id.DataBind();

                competitor_id.DataTextField  = "show";
                competitor_id.DataValueField = "val";
                competitor_id.DataSource     = dic.FirstOrDefault(_ => _.Key == "competition").Value;
                competitor_id.DataBind();
                competitor_id.Items.Insert(0, new ListItem()
                {
                    Value = "0", Text = "   ", Selected = true
                });

                // period_type
                period_type.DataTextField  = "show";
                period_type.DataValueField = "val";
                period_type.DataSource     = dic.FirstOrDefault(_ => _.Key == "period_type").Value;
                period_type.DataBind();
                period_type.Items.Insert(0, new ListItem()
                {
                    Value = "0", Text = "   ", Selected = true
                });

                // notifi_temp 通知模板
                var tempList = new sys_notify_tmpl_dal().GetTempByEvent(DicEnum.NOTIFY_EVENT.OPPORTUNITY_CLOSED);
                notifi_temp.DataTextField  = "name";
                notifi_temp.DataValueField = "id";
                notifi_temp.DataSource     = tempList;
                notifi_temp.DataBind();
                #endregion
                var id         = Request.QueryString["id"];
                var account_id = Request.QueryString["account_id"];
                if (!string.IsNullOrEmpty(id))
                {
                    if (string.IsNullOrEmpty(account_id))
                    {
                        if (!IsPostBack)
                        {
                            opportunity_id.Enabled = false;
                        }
                    }
                    opportunity = new crm_opportunity_dal().GetOpportunityById(long.Parse(id));
                    if (opportunity != null)
                    {
                        account    = new CompanyBLL().GetCompany(opportunity.account_id);
                        account_id = account.id.ToString();
                    }
                }
                if (!string.IsNullOrEmpty(account_id))
                {
                    account = new CompanyBLL().GetCompany(long.Parse(account_id));
                    var oppoList = new crm_opportunity_dal().FindOpHistoryByAccountId(long.Parse(account_id));
                    if (oppoList != null && oppoList.Count > 0)
                    {
                        opportunity_id.DataTextField  = "name";
                        opportunity_id.DataValueField = "id";
                        opportunity_id.DataSource     = oppoList;
                        opportunity_id.DataBind();
                    }
                    else
                    {
                        Response.Write("<script>alert('该客户还没有商机!');window.close();</script>");
                    }

                    //opportunity_id.Items.Insert(0, new ListItem() { Value = "0", Text = "   ", Selected = true });
                    if (string.IsNullOrEmpty(id))
                    {
                        opportunity = oppoList[0];
                    }
                    opportunity_id.SelectedValue = opportunity.id.ToString();
                }

                if (!IsPostBack)
                {
                    if (opportunity != null)
                    {
                        if (opportunity.status_id == (int)OPPORTUNITY_STATUS.CLOSED)
                        {
                            Response.Write("<script>if(!confirm('商机已被关闭,如果继续,系统会重复创建计费项和合同?')){window.close();}</script>");
                        }
                    }
                    if (opportunity.stage_id != null)
                    {
                        stage_id.SelectedValue = opportunity.stage_id.ToString();
                    }
                    else
                    {
                        var stageList    = new d_general_dal().GetGeneralByTableId((int)GeneralTableEnum.OPPORTUNITY_STATUS);
                        var defaultStage = stageList.FirstOrDefault(_ => _.ext1 == "1");
                        if (defaultStage != null)
                        {
                            stage_id.SelectedValue = defaultStage.id.ToString();
                        }
                    }
                    // stage_id.SelectedValue = opportunity.stage_id == null ? "0" : opportunity.stage_id.ToString();
                    resource_id.SelectedValue        = opportunity.resource_id.ToString();
                    competitor_id.SelectedValue      = opportunity.competitor_id == null ? "0" : opportunity.competitor_id.ToString();
                    win_reason_type_id.SelectedValue = opportunity.win_reason_type_id == null ? "0" : opportunity.win_reason_type_id.ToString();
                }
                // 折扣的下拉框不同于别的下拉框-- 需要去两个数据源去赋值
                StringBuilder text     = new StringBuilder();
                var           costCode = new d_cost_code_dal().GetListCostCode((int)COST_CODE_CATE.MATERIAL_COST_CODE);
                text.Append($"<option value='0'>   </option>");
                if (costCode != null && costCode.Count > 0)
                {
                    foreach (var item in costCode)
                    {
                        text.Append($"<option value='{item.id}'>{item.name}</option>");
                    }
                }
                codeSelect.Value = text.ToString();
                var disSource   = "";
                var disCostCode = costCode.Where(_ => _.id == (int)CostCode.NOTAXDISCOUNT || _.id == (int)CostCode.DISCOUNT).ToList();
                if (disCostCode != null && disCostCode.Count > 0)
                {
                    foreach (var item in disCostCode)
                    {
                        disSource += $"<option value='{item.id}'>{item.name}</option>";
                    }
                }
                disCodeSelct.Value        = disSource;
                period_type.SelectedValue = ((int)QUOTE_ITEM_PERIOD_TYPE.MONTH).ToString();
                primaryQuote = new QuoteBLL().GetPrimaryQuote(opportunity.id);


                if (primaryQuote != null)
                {
                    if (!IsPostBack)
                    {
                        if (primaryQuote.project_id != null) // 判断该报价是否关联项目提案,如果关联,默认选中,不关联,灰掉,不可选
                        {
                            activeproject.Checked = true;
                            activeproject.Enabled = true;
                            addRequest.Enabled    = false;
                            isaddRequest.Value    = "1";
                        }
                        else
                        {
                            activeproject.Checked = false;
                            addRequest.Enabled    = true;

                            activeproject.Enabled = false;
                            isactiveproject.Value = "1";
                        }
                    }
                    quoteItemList = new crm_quote_item_dal().GetQuoteItems($" and quote_id = {primaryQuote.id}");
                    if (quoteItemList != null && quoteItemList.Count > 0)
                    {
                        // 如果报价项中包含了服务 / 服务包、初始费用,则可以选中(默认不选)
                        // 此功能需要具有合同模块权限以及修改合同服务/包权限

                        var isServiceChargeItem = quoteItemList.Where(_ => _.type_id == (int)QUOTE_ITEM_TYPE.SERVICE || _.type_id == (int)QUOTE_ITEM_TYPE.SERVICE_PACK || _.type_id == (int)QUOTE_ITEM_TYPE.START_COST).ToList();
                        if (!IsPostBack)
                        {
                            if (isServiceChargeItem != null && isServiceChargeItem.Count > 0)
                            {
                                // todo--需要判断用户权限
                            }
                            else
                            {
                                addContractRequest.Enabled  = false;
                                isAddContractRequest.Value  = "1";
                                addContractServices.Enabled = false;
                                isaddContractServices.Value = "1";
                            }
                        }
                        proAndOneTimeItem = quoteItemList.Where(_ => _.optional != 1).Where(_ => _.type_id == (int)QUOTE_ITEM_TYPE.PRODUCT || _.type_id == (int)QUOTE_ITEM_TYPE.DISCOUNT).ToList();
                        if (!IsPostBack)
                        {
                            if (proAndOneTimeItem != null && proAndOneTimeItem.Count > 0)
                            {
                                isIncludePO.Checked = true;
                            }
                            else
                            {
                                IncludePO.Value     = "1";
                                isIncludePO.Enabled = false;
                            }
                        }
                        shipItem = quoteItemList.Where(_ => _.type_id == (int)QUOTE_ITEM_TYPE.DISTRIBUTION_EXPENSES && _.optional != 1).ToList();
                        if (!IsPostBack)
                        {
                            if (shipItem != null && shipItem.Count > 0)
                            {
                                isIncludeShip.Checked = true;
                            }
                            else
                            {
                                IncludeShip.Value     = "1";
                                isIncludeShip.Enabled = false;
                            }
                        }
                        degressionItem = quoteItemList.Where(_ => _.type_id == (int)QUOTE_ITEM_TYPE.DEGRESSION & _.optional != 1).ToList();
                        if (!IsPostBack)
                        {
                            if (degressionItem != null && degressionItem.Count > 0)
                            {
                                isIncludeCharges.Checked = true;
                            }
                            else
                            {
                                IncludeCharges.Value     = "1";
                                isIncludeCharges.Enabled = false;
                            }
                        }
                        jqueryCode.Value = ReturnJquery();
                    }
                    else
                    {
                        // activeproject.Enabled = false;
                        //isactiveproject.Value = "1";
                        addContractRequest.Enabled  = false;
                        isAddContractRequest.Value  = "1";
                        addContractServices.Enabled = false;
                        isaddContractServices.Value = "1";
                        IncludePO.Value             = "1";
                        isIncludePO.Enabled         = false;
                        IncludeShip.Value           = "1";
                        isIncludeShip.Enabled       = false;
                        IncludeCharges.Value        = "1";
                        isIncludeCharges.Enabled    = false;
                    }
                }
                else
                {
                    activeproject.Enabled       = false;
                    isactiveproject.Value       = "1";
                    addContractRequest.Enabled  = false;
                    isAddContractRequest.Value  = "1";
                    addContractServices.Enabled = false;
                    isaddContractServices.Value = "1";
                    IncludePO.Value             = "1";
                    isIncludePO.Enabled         = false;
                    IncludeShip.Value           = "1";
                    isIncludeShip.Enabled       = false;
                    IncludeCharges.Value        = "1";
                    isIncludeCharges.Enabled    = false;
                }
            }
            catch (Exception msg)
            {
                Response.End();
            }
        }
Пример #2
0
        public ERROR_CODE CloseQuote(long user_id, QuoteCloseDto param)
        {
            // 此向导将会把产品、一次性折扣、配送、成本转为计费项。可选项和费用不会被转换。如果有产品,会生成销售订单。
            // 报价中如果有服务 / 包或初始费用,将不会被转换为计费项,也不会创建定期服务合同
            // 如果商机状态已经是“关闭”或“已实施”,将会为此商机生成重复的计费项。


            // 报价项中如果有物料代码为空的,则需要设置。如果没有需要配置的,则此界面不显示

            // 计费项将会生成,是否需要创建发票

            // 打开新建的销售订单(链接)——如果创建了销售单才显示



            // 关闭报价是关闭商机的另一种方式,

            // -- 必填项校验-- 根据系统设置来判断 TODO
            var user = UserInfoBLL.GetUserInfo(user_id);

            // 1.更新商机信息

            #region 1.更新商机信息
            // 根据系统设置来选择商机的阶段-- todo
            param.opportunity.status_id = (int)DicEnum.OPPORTUNITY_STATUS.CLOSED;

            var stageList    = new d_general_dal().GetGeneralByTableId((int)GeneralTableEnum.OPPORTUNITY_STATUS);
            var defaultStage = stageList.FirstOrDefault(_ => _.ext1 == "1");
            if (defaultStage != null)
            {
                param.opportunity.stage_id = defaultStage.id;
            }
            var old_opportunity = new crm_opportunity_dal().GetOpportunityById(param.opportunity.id);
            new crm_opportunity_dal().Update(param.opportunity);
            new sys_oper_log_dal().Insert(new sys_oper_log()
            {
                user_cate           = "用户",
                user_id             = user_id,
                name                = user.name,
                phone               = user.mobile == null ? "" : user.mobile,
                oper_time           = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now),
                oper_object_cate_id = (int)OPER_LOG_OBJ_CATE.OPPORTUNITY,
                oper_object_id      = param.opportunity.id,// 操作对象id
                oper_type_id        = (int)OPER_LOG_TYPE.UPDATE,
                oper_description    = _dal.CompareValue(old_opportunity, param.opportunity),
                remark              = "修改商机信息"
            });

            #endregion

            // 2.更新客户信息
            #region 2.更新客户信息
            var account = new CompanyBLL().GetCompany(param.quote.account_id);
            if (account.type_id != (int)DicEnum.ACCOUNT_TYPE.CUSTOMER)
            {
                account.type_id = (int)DicEnum.ACCOUNT_TYPE.CUSTOMER;
                new crm_account_dal().Update(account);
                new sys_oper_log_dal().Insert(new sys_oper_log()
                {
                    user_cate           = "用户",
                    user_id             = user_id,
                    name                = user.name,
                    phone               = user.mobile == null ? "" : user.mobile,
                    oper_time           = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now),
                    oper_object_cate_id = (int)OPER_LOG_OBJ_CATE.CUSTOMER,
                    oper_object_id      = param.quote.account_id,// 操作对象id
                    oper_type_id        = (int)OPER_LOG_TYPE.UPDATE,
                    oper_description    = _dal.CompareValue(new CompanyBLL().GetCompany(param.quote.account_id), account),
                    remark              = "修改客户信息"
                });
            }

            #endregion

            long?contact_id = null;
            if (param.opportunity.contact_id != null)
            {
                crm_contact contact = new ContactBLL().GetContact((long)param.opportunity.contact_id);
                if (contact.is_active == 1)
                {
                    contact_id = contact.id;
                }
            }



            // 3.保存项目信息
            #region 3.如果项目关联了项目提案,修改项目提案信息
            if (param.quote.project_id != null)
            {
                var project = new pro_project_dal().GetProjectById((long)param.quote.project_id);
                if (project != null)
                {
                    if (project.type_id != (int)PROJECT_TYPE.ACCOUNT_PROJECT)
                    {
                        project.type_id        = (int)PROJECT_TYPE.ACCOUNT_PROJECT;
                        project.update_time    = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now);
                        project.update_user_id = user.id;
                        new sys_oper_log_dal().Insert(new sys_oper_log()
                        {
                            user_cate           = "用户",
                            user_id             = (int)user.id,
                            name                = user.name,
                            phone               = user.mobile == null ? "" : user.mobile,
                            oper_time           = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now),
                            oper_object_cate_id = (int)OPER_LOG_OBJ_CATE.PROJECT,
                            oper_object_id      = project.id,// 操作对象id
                            oper_type_id        = (int)OPER_LOG_TYPE.UPDATE,
                            oper_description    = _dal.CompareValue(new pro_project_dal().GetProjectById((long)param.quote.project_id), project),
                            remark              = "修改项目提案类型"
                        });
                        new pro_project_dal().Update(project);
                    }
                    param.project_id = project.id;
                }
                string costIds = "";
                new OpportunityBLL().InsertContract(param.dic, param.opportunity, user, null, out costIds, param.project_id, null);

                if (!string.IsNullOrEmpty(costIds))
                {
                    var aapBLL = new ApproveAndPostBLL();
                    costIds = costIds.Substring(0, costIds.Length - 1);
                    var costList = costIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var costId in costList)
                    {
                        // 审批并提交
                        aapBLL.Post_Charges(int.Parse(costId), int.Parse(DateTime.Now.ToString("yyyyMMdd")), user);
                    }
                }
            }
            #endregion

            // 4.新增工单信息
            #region 4.如果报价未关联项目提案,有需要转换的计费项
            // todo 关联sdk_ticket

            #endregion


            // 5.转换为工单/项目成本
            #region 5.转换为工单/项目成本
            // todo 关联sdk_ticket_charge
            // 一次性折扣根据需要拆分为两行——收税的、不收税的,分别计算折扣额。计算时仍然按照全部周期为一次性的报价项,而不是排除了服务和工时等报价项。


            #endregion

            // 将报价项转换为计费项
            #region  将报价项转换为计费项并审批提交 todo-需要项目ID 或者工单ID才可以进行操作
            //string costIds ="";
            //new OpportunityBLL().InsertContract(param.dic,param.opportunity,user,null,out costIds, param.project_id,null);

            //if (!string.IsNullOrEmpty(costIds))
            //{
            //    var aapBLL = new ApproveAndPostBLL();
            //    costIds = costIds.Substring(0,costIds.Length-1);
            //    var costList = costIds.Split(new char[] { ','},StringSplitOptions.RemoveEmptyEntries);
            //    foreach (var costId in costList)
            //    {
            //        // 审批并提交
            //        aapBLL.Post_Charges(int.Parse(costId),int.Parse(DateTime.Now.ToString("yyyyMMdd")),user);
            //    }
            //}
            #endregion

            // 6.新增销售订单
            #region 6.当有产品/一次性折扣、成本、配送转为计费项时,销售订单就会自动生成。Crm_sales_order
            if (param.dic != null && param.dic.Count > 0)
            {
                var saleOrder = new crm_sales_order_dal().GetSingleSalesOrderByWhere($" and opportunity_id = {param.opportunity.id} ");
                if (saleOrder == null)
                {
                    saleOrder = new crm_sales_order()
                    {
                        id = _dal.GetNextIdCom(),

                        opportunity_id    = param.opportunity.id,
                        status_id         = (int)SALES_ORDER_STATUS.OPEN,
                        contact_id        = contact_id,
                        owner_resource_id = param.opportunity.resource_id,
                        begin_date        = DateTime.Now,
                        create_user_id    = user.id,
                        update_user_id    = user.id,
                        create_time       = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now),
                        update_time       = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now),
                    };
                    new crm_sales_order_dal().Insert(saleOrder);
                    new sys_oper_log_dal().Insert(new sys_oper_log()
                    {
                        user_cate           = "用户",
                        user_id             = (int)user.id,
                        name                = user.name,
                        phone               = user.mobile == null ? "" : user.mobile,
                        oper_time           = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now),
                        oper_object_cate_id = (int)OPER_LOG_OBJ_CATE.SALE_ORDER,
                        oper_object_id      = saleOrder.id,// 操作对象id
                        oper_type_id        = (int)OPER_LOG_TYPE.ADD,
                        oper_description    = _dal.AddValue(saleOrder),
                        remark              = "关闭报价,新增销售订单"
                    });
                    param.saleOrderId = saleOrder.id;
                }
            }


            #endregion

            // 7.新增项目备注
            #region 7.转为项目计费项时,会生成备注
            if (param.quote.project_id != null)
            {
                com_activity addActivity = new com_activity()
                {
                    id             = _dal.GetNextIdCom(),
                    cate_id        = (int)ACTIVITY_CATE.PROJECT_NOTE,
                    action_type_id = (int)ACTIVITY_TYPE.PROJECT_NOTE,
                    parent_id      = null,
                    object_id      = (long)param.quote.project_id,
                    object_type_id = (int)OBJECT_TYPE.PROJECT,
                    // todo发布范围
                    account_id     = param.opportunity.account_id,
                    contact_id     = contact_id,
                    resource_id    = param.opportunity.resource_id,
                    contract_id    = contact_id,
                    opportunity_id = param.opportunity.id,
                    ticket_id      = null,
                    // todo 标题
                    description          = $"", // todo 内容描述拼接
                    status_id            = null,
                    complete_description = null,
                    complete_time        = null,
                    create_user_id       = user.id,
                    create_time          = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now),
                    update_user_id       = user.id,
                    update_time          = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now),
                    is_system_generate   = 1,
                };
                new com_activity_dal().Insert(addActivity);
                new sys_oper_log_dal().Insert(new sys_oper_log()
                {
                    user_cate           = "用户",
                    user_id             = (int)user.id,
                    name                = user.name,
                    phone               = user.mobile == null ? "" : user.mobile,
                    oper_time           = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now),
                    oper_object_cate_id = (int)OPER_LOG_OBJ_CATE.ACTIVITY,
                    oper_object_id      = addActivity.id,// 操作对象id
                    oper_type_id        = (int)OPER_LOG_TYPE.ADD,
                    oper_description    = _dal.AddValue(addActivity),
                    remark              = "商机关闭,新增项目备注"
                });
            }

            #endregion

            // 8.新增备注(商机关闭)
            #region 8.商机关闭时,会自动生成备注

            com_activity closeOppoActivity = new com_activity()
            {
                id                   = _dal.GetNextIdCom(),
                cate_id              = (int)ACTIVITY_CATE.NOTE,
                action_type_id       = (int)ACTIVITY_TYPE.OPPORTUNITYUPDATE,
                parent_id            = null,
                object_id            = param.opportunity.id,
                object_type_id       = (int)OBJECT_TYPE.OPPORTUNITY,
                account_id           = param.opportunity.account_id,
                contact_id           = contact_id,
                resource_id          = param.opportunity.resource_id,
                contract_id          = null, // todo 如果转为合同成本,则为“合同”;否则为空
                opportunity_id       = param.opportunity.id,
                ticket_id            = null,
                start_date           = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Parse(DateTime.Now.ToShortDateString() + " 12:00:00")),    // todo 从页面获取时间,去页面时间的12:00:00
                end_date             = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Parse(DateTime.Now.ToShortDateString() + " 12:00:00")),
                description          = $"关闭时间:{DateTime.Now.ToString("dd/MM/yyyy")}/r通知人:{user.email}/r主题:{param.opportunity.name}已经关闭/r内容:商机关闭向导定义", // todo 内容描述拼接
                status_id            = null,
                complete_description = null,
                complete_time        = null,
                create_user_id       = user.id,
                create_time          = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now),
                update_user_id       = user.id,
                update_time          = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now),
                is_system_generate   = 1,
            };

            new com_activity_dal().Insert(closeOppoActivity);
            new sys_oper_log_dal().Insert(new sys_oper_log()
            {
                user_cate           = "用户",
                user_id             = (int)user.id,
                name                = user.name,
                phone               = user.mobile == null ? "" : user.mobile,
                oper_time           = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now),
                oper_object_cate_id = (int)OPER_LOG_OBJ_CATE.ACTIVITY,
                oper_object_id      = closeOppoActivity.id,// 操作对象id
                oper_type_id        = (int)OPER_LOG_TYPE.ADD,
                oper_description    = _dal.AddValue(closeOppoActivity),
                remark              = "新增关闭商机的备注"
            });

            #endregion

            return(ERROR_CODE.SUCCESS);
        }