/// <summary> /// 新增报价项 /// </summary> /// <param name="quote_item"></param> /// <param name="user_id"></param> /// <returns></returns> public ERROR_CODE Insert(crm_quote_item quote_item, Dictionary <long, int> wareDic, long user_id, bool isSaleOrder = false, long?saleOrderId = null) { if (quote_item.type_id != (int)DicEnum.QUOTE_ITEM_TYPE.DISCOUNT) { if (quote_item.unit_price == null || quote_item.unit_cost == null || quote_item.quantity == null || quote_item.unit_discount == null) { return(ERROR_CODE.PARAMS_ERROR); } if (string.IsNullOrEmpty(quote_item.name)) { return(ERROR_CODE.PARAMS_ERROR); } } else { if (string.IsNullOrEmpty(quote_item.name)) { return(ERROR_CODE.PARAMS_ERROR); } if (quote_item.discount_percent == null && quote_item.unit_discount == null) { return(ERROR_CODE.PARAMS_ERROR); } } var user = UserInfoBLL.GetUserInfo(user_id); if (user == null) { return(ERROR_CODE.USER_NOT_FIND); } quote_item.id = _dal.GetNextIdCom(); quote_item.tax_cate_id = quote_item.tax_cate_id == 0 ? null : quote_item.tax_cate_id; quote_item.period_type_id = quote_item.period_type_id == 0 ? null : quote_item.period_type_id; quote_item.create_time = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now); quote_item.update_time = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now); quote_item.create_user_id = user_id; quote_item.update_user_id = user_id; _dal.Insert(quote_item); 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.QUOTE_ITEM, oper_object_id = quote_item.id,// 操作对象id oper_type_id = (int)OPER_LOG_TYPE.ADD, oper_description = _dal.AddValue(quote_item), remark = "保存报价项信息" }); var oDal = new crm_opportunity_dal(); var oppo = oDal.GetOpByItemID(quote_item.id); if (oppo != null && oppo.use_quote == 1) { if (quote_item.optional != 1 && quote_item.type_id != (int)DicEnum.QUOTE_ITEM_TYPE.DISCOUNT && quote_item.type_id != (int)DicEnum.QUOTE_ITEM_TYPE.DISTRIBUTION_EXPENSES) { decimal?changeRevenue = quote_item.quantity * ((quote_item.unit_price ?? 0) - (quote_item.unit_discount ?? 0)); decimal?changeCost = quote_item.quantity * quote_item.unit_cost; switch (quote_item.period_type_id) { case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.ONE_TIME: oppo.one_time_revenue += (changeRevenue ?? 0); oppo.one_time_cost += (changeCost ?? 0); break; case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.MONTH: oppo.monthly_revenue += (changeRevenue ?? 0); oppo.monthly_cost += (changeCost ?? 0); break; case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.QUARTER: oppo.quarterly_revenue += (changeRevenue ?? 0); oppo.quarterly_cost += (changeCost ?? 0); break; case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.HALFYEAR: oppo.semi_annual_revenue += (changeRevenue ?? 0); oppo.semi_annual_cost += (changeCost ?? 0); break; case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.YEAR: oppo.yearly_revenue += (changeRevenue ?? 0); oppo.yearly_cost += (changeCost ?? 0); break; default: break; } // var udfDto = new UserDefinedFieldsBLL().GetUdf(DicEnum.UDF_CATE.OPPORTUNITY); // var udfValue = OpportunityAddOrUpdateDto param = new OpportunityAddOrUpdateDto() { general = oppo, udf = null, notify = null }; new OpportunityBLL().Update(param, user.id); } } if (quote_item.type_id == (int)QUOTE_ITEM_TYPE.PRODUCT) { if (wareDic != null && wareDic.Count > 0) { var irDal = new ivt_reserve_dal(); var iwDal = new ivt_warehouse_dal(); foreach (var thisPageWare in wareDic) { var thisWareHouse = iwDal.FindNoDeleteById(thisPageWare.Key); if (thisWareHouse != null && thisPageWare.Value != 0) { var thisReserve = new ivt_reserve() { id = irDal.GetNextIdCom(), create_time = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now), update_time = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now), create_user_id = user_id, update_user_id = user_id, quote_item_id = quote_item.id, warehouse_id = thisPageWare.Key, quantity = thisPageWare.Value, resource_id = thisWareHouse.resource_id, }; irDal.Insert(thisReserve); OperLogBLL.OperLogAdd <ivt_reserve>(thisReserve, thisReserve.id, user_id, OPER_LOG_OBJ_CATE.WAREHOUSE_RESERVE, "新增库存预留"); } } } } if (isSaleOrder && saleOrderId != null && (quote_item.type_id == (int)DicEnum.QUOTE_ITEM_TYPE.PRODUCT || quote_item.type_id == (int)DicEnum.QUOTE_ITEM_TYPE.DEGRESSION || quote_item.type_id == (int)DicEnum.QUOTE_ITEM_TYPE.DISTRIBUTION_EXPENSES)) { var cccDal = new ctt_contract_cost_dal(); var costList = cccDal.GetListBySaleOrderId((long)saleOrderId); if (costList != null && costList.Count > 0) { int status_id = 0; long cost_code_id = (long)quote_item.object_id; long?product_id = null; if (quote_item.type_id == (int)QUOTE_ITEM_TYPE.PRODUCT) { product_id = quote_item.object_id; status_id = (int)COST_STATUS.PENDING_PURCHASE; var thisProduct = new ivt_product_dal().FindNoDeleteById((long)quote_item.object_id); if (thisProduct != null) { cost_code_id = thisProduct.cost_code_id; } else { return(ERROR_CODE.ERROR); } var appSet = new SysSettingBLL().GetSetById(DTO.SysSettingEnum.CTT_COST_APPROVAL_VALUE); if (appSet != null && !string.IsNullOrEmpty(appSet.setting_value) && thisProduct.does_not_require_procurement == 0) // 该产品走采购流程,并且价格大于设置,则带审批 { if (((decimal)quote_item.quantity * (decimal)quote_item.unit_price) > decimal.Parse(appSet.setting_value)) // 金额超出(待审批) { status_id = (int)COST_STATUS.PENDING_APPROVAL; } } } else { status_id = (int)COST_STATUS.PENDING_DELIVERY; } ctt_contract_cost cost = new ctt_contract_cost() { id = _dal.GetNextIdCom(), opportunity_id = costList[0].opportunity_id, quote_item_id = quote_item.id, cost_code_id = cost_code_id, product_id = product_id, name = quote_item.name, description = quote_item.description, date_purchased = DateTime.Now, is_billable = 1, bill_status = 0, cost_type_id = (int)COST_TYPE.OPERATIONA, status_id = status_id, quantity = quote_item.quantity ?? 0, unit_price = (quote_item.unit_price ?? 0) - (quote_item.unit_discount ?? 0), unit_cost = quote_item.unit_cost, extended_price = ((quote_item.unit_price ?? 0) - (quote_item.unit_discount ?? 0)) * quote_item.quantity, 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), project_id = costList[0].project_id, contract_id = costList[0].contract_id, task_id = costList[0].task_id, sub_cate_id = costList[0].sub_cate_id, }; cccDal.Insert(cost); OperLogBLL.OperLogAdd <ctt_contract_cost>(cost, cost.id, user_id, OPER_LOG_OBJ_CATE.CONTRACT_COST, "新增成本"); if (quote_item.type_id == (int)QUOTE_ITEM_TYPE.PRODUCT) { var ccBll = new ContractCostBLL(); var irDal = new ivt_reserve_dal(); var thisPro = new ivt_product_dal().FindNoDeleteById((long)quote_item.object_id); var thisProResList = irDal.GetListByItemId(quote_item.id); if (thisProResList != null && thisProResList.Count > 0 && thisPro != null) { foreach (var thisProRes in thisProResList) { ccBll.AddCostProduct(cost.id, thisPro.id, thisProRes.warehouse_id, thisProRes.quantity, "", "", user.id); irDal.SoftDelete(thisProRes, user.id); OperLogBLL.OperLogDelete <ivt_reserve>(thisProRes, thisProRes.id, user.id, OPER_LOG_OBJ_CATE.WAREHOUSE_RESERVE, "删除产品预留"); } ccBll.ChangCostStatus(cost.id, user_id); } } } } return(ERROR_CODE.SUCCESS); }
/// <summary> /// 编辑报价项 /// </summary> /// <param name="quote_item"></param> /// <param name="user_id"></param> /// <returns></returns> public ERROR_CODE Update(crm_quote_item quote_item, Dictionary <long, int> wareDic, long user_id, bool isSaleOrder = false, long?saleOrderId = null) { if (quote_item.type_id != (int)DicEnum.QUOTE_ITEM_TYPE.DISCOUNT) { if (quote_item.unit_price == null || quote_item.unit_cost == null || quote_item.quantity == null || quote_item.unit_discount == null) { return(ERROR_CODE.PARAMS_ERROR); } if (string.IsNullOrEmpty(quote_item.name)) { return(ERROR_CODE.PARAMS_ERROR); } } else { if (string.IsNullOrEmpty(quote_item.name)) { return(ERROR_CODE.PARAMS_ERROR); } if (quote_item.discount_percent == null && quote_item.unit_discount == null) { return(ERROR_CODE.PARAMS_ERROR); } } var user = UserInfoBLL.GetUserInfo(user_id); if (user == null) { return(ERROR_CODE.USER_NOT_FIND); } var old_quote_item = _dal.GetQuoteItem(quote_item.id); int?oldPeriod = old_quote_item.period_type_id; quote_item.oid = old_quote_item.oid; quote_item.update_user_id = user_id; quote_item.update_time = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now); quote_item.tax_cate_id = quote_item.tax_cate_id == 0 ? null : quote_item.tax_cate_id; _dal.Update(quote_item); 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.QUOTE_ITEM, oper_object_id = quote_item.id,// 操作对象id oper_type_id = (int)OPER_LOG_TYPE.UPDATE, oper_description = _dal.CompareValue(old_quote_item, quote_item), remark = "编辑报价项信息" }); var oDal = new crm_opportunity_dal(); var oppo = oDal.GetOpByItemID(quote_item.id); if (oppo != null && oppo.use_quote == 1) { if (quote_item.optional != 1 && quote_item.type_id != (int)DicEnum.QUOTE_ITEM_TYPE.DISCOUNT && quote_item.type_id != (int)DicEnum.QUOTE_ITEM_TYPE.DISTRIBUTION_EXPENSES) { decimal?changeRevenue = quote_item.quantity * quote_item.unit_price; decimal?changeCost = quote_item.quantity * quote_item.unit_cost; decimal?oldChangeRevenue = old_quote_item.quantity * ((old_quote_item.unit_price ?? 0) - (old_quote_item.unit_discount ?? 0)); decimal?oldChangeCost = old_quote_item.quantity * old_quote_item.unit_cost; switch (oldPeriod) { case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.ONE_TIME: oppo.one_time_revenue = oppo.one_time_revenue - (oldChangeRevenue ?? 0); oppo.one_time_cost = oppo.one_time_cost - (oldChangeCost ?? 0); break; case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.MONTH: oppo.one_time_revenue = oppo.one_time_revenue - (oldChangeRevenue ?? 0); oppo.one_time_cost = oppo.one_time_cost - (oldChangeCost ?? 0); break; case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.QUARTER: oppo.one_time_revenue = oppo.one_time_revenue - (oldChangeRevenue ?? 0); oppo.one_time_cost = oppo.one_time_cost - (oldChangeCost ?? 0); break; case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.HALFYEAR: oppo.one_time_revenue = oppo.one_time_revenue - (oldChangeRevenue ?? 0); oppo.one_time_cost = oppo.one_time_cost - (oldChangeCost ?? 0); break; case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.YEAR: oppo.one_time_revenue = oppo.one_time_revenue - (oldChangeRevenue ?? 0); oppo.one_time_cost = oppo.one_time_cost - (oldChangeCost ?? 0); break; default: break; } switch (quote_item.period_type_id) { case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.ONE_TIME: oppo.one_time_revenue += (changeRevenue ?? 0); oppo.one_time_cost += (changeCost ?? 0); break; case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.MONTH: oppo.monthly_revenue += (changeRevenue ?? 0); oppo.monthly_cost += (changeCost ?? 0); break; case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.QUARTER: oppo.quarterly_revenue += (changeRevenue ?? 0); oppo.quarterly_cost += (changeCost ?? 0); break; case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.HALFYEAR: oppo.semi_annual_revenue += (changeRevenue ?? 0); oppo.semi_annual_cost += (changeCost ?? 0); break; case (int)DicEnum.QUOTE_ITEM_PERIOD_TYPE.YEAR: oppo.yearly_revenue += (changeRevenue ?? 0); oppo.yearly_cost += (changeCost ?? 0); break; default: break; } // var udfDto = new UserDefinedFieldsBLL().GetUdf(DicEnum.UDF_CATE.OPPORTUNITY); // var udfValue = OpportunityAddOrUpdateDto param = new OpportunityAddOrUpdateDto() { general = oppo, udf = null, notify = null }; new OpportunityBLL().Update(param, user.id); } } if (quote_item.type_id == (int)QUOTE_ITEM_TYPE.PRODUCT) { var irDal = new ivt_reserve_dal(); var iwDal = new ivt_warehouse_dal(); var oldReserList = irDal.GetListByItemId(quote_item.id); if (wareDic != null && wareDic.Count > 0) { if (oldReserList != null && oldReserList.Count > 0) { foreach (var thisPageWare in wareDic) { var thisWareHouse = iwDal.FindNoDeleteById(thisPageWare.Key); var thisOldReser = oldReserList.FirstOrDefault(_ => _.warehouse_id == thisPageWare.Key); if (thisOldReser != null) { oldReserList.Remove(thisOldReser); if (thisWareHouse != null) { if (thisPageWare.Value == 0) { irDal.SoftDelete(thisOldReser, user_id); OperLogBLL.OperLogDelete <ivt_reserve>(thisOldReser, thisOldReser.id, user_id, OPER_LOG_OBJ_CATE.WAREHOUSE_RESERVE, "删除库存预留"); continue; } if (thisOldReser.quantity != thisPageWare.Value || thisOldReser.resource_id != thisWareHouse.resource_id) { thisOldReser.quantity = thisPageWare.Value; thisOldReser.resource_id = thisWareHouse.resource_id; var thisOld = irDal.FindNoDeleteById(thisOldReser.id); irDal.Update(thisOldReser); OperLogBLL.OperLogUpdate <ivt_reserve>(thisOldReser, thisOld, thisOldReser.id, user_id, OPER_LOG_OBJ_CATE.WAREHOUSE_RESERVE, "修改库存预留"); } } } else { if (thisWareHouse != null && thisPageWare.Value != 0) { var thisReserve = new ivt_reserve() { id = irDal.GetNextIdCom(), create_time = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now), update_time = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now), create_user_id = user_id, update_user_id = user_id, quote_item_id = quote_item.id, warehouse_id = thisPageWare.Key, quantity = thisPageWare.Value, resource_id = thisWareHouse.resource_id, }; irDal.Insert(thisReserve); OperLogBLL.OperLogAdd <ivt_reserve>(thisReserve, thisReserve.id, user_id, OPER_LOG_OBJ_CATE.WAREHOUSE_RESERVE, "新增库存预留"); } } } } else { foreach (var thisPageWare in wareDic) { var thisWareHouse = iwDal.FindNoDeleteById(thisPageWare.Key); if (thisWareHouse != null) { var thisReserve = new ivt_reserve() { id = irDal.GetNextIdCom(), create_time = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now), update_time = Tools.Date.DateHelper.ToUniversalTimeStamp(DateTime.Now), create_user_id = user_id, update_user_id = user_id, quote_item_id = quote_item.id, warehouse_id = thisPageWare.Key, quantity = thisPageWare.Value, resource_id = thisWareHouse.resource_id, }; irDal.Insert(thisReserve); OperLogBLL.OperLogAdd <ivt_reserve>(thisReserve, thisReserve.id, user_id, OPER_LOG_OBJ_CATE.WAREHOUSE_RESERVE, "新增库存预留"); } } } } if (oldReserList != null && oldReserList.Count > 0) { oldReserList.ForEach(_ => { irDal.SoftDelete(_, user_id); OperLogBLL.OperLogDelete <ivt_reserve>(_, _.id, user_id, OPER_LOG_OBJ_CATE.WAREHOUSE_RESERVE, "删除库存预留"); }); } } if (isSaleOrder && saleOrderId != null && (quote_item.type_id == (int)DicEnum.QUOTE_ITEM_TYPE.PRODUCT || quote_item.type_id == (int)DicEnum.QUOTE_ITEM_TYPE.DEGRESSION || quote_item.type_id == (int)DicEnum.QUOTE_ITEM_TYPE.DISTRIBUTION_EXPENSES)) { var cccDal = new ctt_contract_cost_dal(); var thisCost = cccDal.GetSinBuQuoteItem(quote_item.id); if (thisCost != null) { long?product_id = thisCost.product_id; int status_id = thisCost.status_id; long cost_code_id = thisCost.cost_code_id; if (quote_item.type_id == (int)QUOTE_ITEM_TYPE.PRODUCT) { product_id = quote_item.object_id; status_id = thisCost.status_id; var thisProduct = new ivt_product_dal().FindNoDeleteById((long)quote_item.object_id); if (thisProduct != null) { cost_code_id = thisProduct.cost_code_id; } else { return(ERROR_CODE.ERROR); } if (status_id != (int)COST_STATUS.UNDETERMINED && status_id != (int)COST_STATUS.PENDING_APPROVAL && status_id != (int)COST_STATUS.CANCELED) { if (thisCost.quantity != quote_item.quantity) { status_id = (int)COST_STATUS.PENDING_PURCHASE; var appSet = new SysSettingBLL().GetSetById(DTO.SysSettingEnum.CTT_COST_APPROVAL_VALUE); if (appSet != null && !string.IsNullOrEmpty(appSet.setting_value) && thisProduct.does_not_require_procurement == 0) { if (((decimal)quote_item.quantity * (decimal)quote_item.unit_price) > decimal.Parse(appSet.setting_value)) // 金额超出(待审批) { status_id = (int)COST_STATUS.PENDING_APPROVAL; } } } } } thisCost.product_id = product_id; thisCost.cost_code_id = cost_code_id; thisCost.name = quote_item.name; thisCost.description = quote_item.description; thisCost.unit_price = (quote_item.unit_price ?? 0) - (quote_item.unit_discount ?? 0); thisCost.unit_cost = quote_item.unit_cost; thisCost.quantity = quote_item.quantity; thisCost.status_id = status_id; var olderCost = cccDal.FindNoDeleteById(thisCost.id); cccDal.Update(thisCost); OperLogBLL.OperLogUpdate <ctt_contract_cost>(thisCost, olderCost, thisCost.id, user_id, OPER_LOG_OBJ_CATE.CONTRACT_COST, "修改成本"); if (quote_item.type_id == (int)QUOTE_ITEM_TYPE.PRODUCT) { var ccBll = new ContractCostBLL(); var irDal = new ivt_reserve_dal(); var thisPro = new ivt_product_dal().FindNoDeleteById((long)quote_item.object_id); var thisProResList = irDal.GetListByItemId(quote_item.id); if (thisProResList != null && thisProResList.Count > 0 && thisPro != null) { foreach (var thisProRes in thisProResList) { ccBll.AddCostProduct(thisCost.id, thisPro.id, thisProRes.warehouse_id, thisProRes.quantity, "", "", user.id); irDal.SoftDelete(thisProRes, user.id); OperLogBLL.OperLogDelete <ivt_reserve>(thisProRes, thisProRes.id, user.id, OPER_LOG_OBJ_CATE.WAREHOUSE_RESERVE, "删除产品预留"); } ccBll.ChangCostStatus(thisCost.id, user_id); } } } } return(ERROR_CODE.SUCCESS); }