Example #1
0
        /// <summary>
        /// 同步经销商进货单
        /// </summary>
        /// <param name="begindate"></param>
        /// <param name="enddate"></param>
        /// <param name="clientcode"></param>
        private void SyncSellOut(DateTime begindate, DateTime enddate, string clientcode)
        {
            try
            {
                int success = 0;

                //从进销存数据库中读取出货数据
                DataTable dtShipHeader = null;

                if (string.IsNullOrEmpty(clientcode))
                {
                    dtShipHeader = ERPIF.GetShipHeader(begindate, enddate);
                }
                else
                {
                    SendMessage(0, "经销商代码:" + clientcode);
                    dtShipHeader = ERPIF.GetShipHeader(begindate, enddate, clientcode);
                }
                SendMessage(0, "本次共获取到" + dtShipHeader.Rows.Count.ToString() + "条发货单待同步!" + _sClientCode);

                for (int i = 0; i < dtShipHeader.Rows.Count; i++)
                {
                    DataRow dr = dtShipHeader.Rows[i];

                    if (_bCancelSync)
                    {
                        SendMessage(0, "同步进行中时,被用户取消!");
                        break;
                    }
                    #region 逐条同步发货单
                    try
                    {
                        string   SheetCode  = dr["DELIVERY_ID"].ToString().Trim();
                        string   ClientCode = dr["CUST_NUMBER"].ToString().Trim();
                        DateTime SalesDate  = DateTime.Parse(dr["CARRY_SDATE"].ToString().Trim());
                        string   DJType     = dr["ORDER_TYPE"].ToString().Trim();
                        decimal  discount   = 0;
                        decimal.TryParse(dr["DISCOUNT"].ToString().Trim(), out discount);

                        if (DJType == "R")
                        {
                            continue;
                        }

                        if (ORD_OrderDeliveryBLL.GetModelList(" SheetCode = '" + SheetCode + "'").Count > 0)
                        {
                            continue;
                        }

                        #region 判断客户号有效否
                        IList <CM_Client> clients = CM_ClientBLL.GetModelList(" Code= '" + ClientCode.ToString() + "' AND ActiveFlag=1 AND ApproveFlag=1");
                        if (clients.Count == 0)
                        {
                            SendMessage(-11, "经销商编号为" + ClientCode.ToString() + "的匹配错误!");
                            continue;
                        }
                        CM_Client client = clients[0];
                        if (client["DIClassify"] == "3")
                        {
                            //经销商子户头
                            client = new CM_ClientBLL(client.Supplier).Model;
                            if (client == null)
                            {
                                SendMessage(-12, "经销商编号为" + ClientCode.ToString() + "对应的经销商为子户头,但未关联正确的主户头!");
                                continue;
                            }
                        }

                        if (client["DIClassify"] != "1")
                        {
                            SendMessage(-13, "经销商编号为" + ClientCode.ToString() + "对应的经销商不是主户头!");
                            continue;
                        }
                        #endregion

                        #region 生成销量单头
                        ORD_OrderDeliveryBLL bll = new ORD_OrderDeliveryBLL();
                        bll.Model.SheetCode    = SheetCode;
                        bll.Model.AccountMonth = AC_AccountMonthBLL.GetMonthByDate(SalesDate);
                        bll.Model.OrganizeCity = client.OrganizeCity;
                        bll.Model.State        = 1; //未发货
                        bll.Model.Client       = client.ID;
                        bll.Model.Store        = client.Supplier;
                        if (dr["CARRY_EDATE"].ToString() != "")
                        {
                            bll.Model.PreArrivalDate = DateTime.Parse(dr["CARRY_EDATE"].ToString().Trim());
                        }
                        else
                        {
                            bll.Model.PreArrivalDate = SalesDate.AddDays(7);      //默认7天后到货
                        }
                        bll.Model.ApproveFlag       = 1;
                        bll.Model.InsertStaff       = 1;
                        bll.Model["DeliveryTime"]   = SalesDate.ToString("yyyy-MM-dd HH:mm");
                        bll.Model["TruckNo"]        = dr["CARRY_CAR"].ToString().Trim();
                        bll.Model["DeliveryStaff"]  = "1";
                        bll.Model["DeliveryRemark"] = "ERP发货单提取";
                        bll.Model["Discount"]       = discount.ToString("0.###");
                        bll.Model["CarrySheetCode"] = dr["REQUEST_NUMBER"].ToString().Trim();
                        #endregion

                        //获取进销存明细
                        DataTable dt_detail = ERPIF.GetShipDetail(SheetCode);

                        #region 生成销量明细
                        foreach (DataRow dr_detail in dt_detail.Rows)
                        {
                            string  productcode = dr_detail["ITEM_NO"].ToString();
                            decimal price       = decimal.Parse(dr_detail["PRICE"].ToString());
                            int     quantity    = (int)decimal.Parse(dr_detail["SHIP_QTY"].ToString());
                            string  packaging   = dr_detail["UOM"].ToString();

                            if (quantity == 0)
                            {
                                continue;
                            }

                            #region 验证产品信息
                            PDT_Product product = null;
                            if (price == 0)
                            {
                                #region 价格为0,表示为赠品,取赠品目录中的物料
                                IList <PDT_Product> _products = PDT_ProductBLL.GetModelList("Code='" + productcode + "' AND Brand IN (SELECT ID FROM MCS_Pub.dbo.PDT_Brand WHERE IsOpponent=9)");
                                if (_products.Count > 0)
                                {
                                    product = _products[0];
                                }
                                else
                                {
                                    _products = PDT_ProductBLL.GetModelList("Code='" + productcode + "' AND Brand IN (SELECT ID FROM MCS_Pub.dbo.PDT_Brand WHERE IsOpponent <> 9)");
                                    if (_products.Count == 0)
                                    {
                                        SendMessage(-21, string.Format("发货单号为{0}内,产品编号{1}未在产品目录库中未找到!", SheetCode, productcode));
                                        continue;
                                    }
                                    else
                                    {
                                        //在成品目录里找到,但未在赠品目录中,自动在赠品目录中增加一条本品赠品目录
                                        PDT_ProductBLL productbll = new PDT_ProductBLL();
                                        productbll.Model             = _products[0];
                                        productbll.Model.Brand       = 3;   //本品赠品
                                        productbll.Model.Classify    = 100; //其他品类
                                        productbll.Model.State       = 2;   //停用
                                        productbll.Model.ApproveFlag = 2;   //未审核
                                        int productid = productbll.Add();

                                        product = new PDT_ProductBLL(productid).Model;
                                        SendMessage(0, string.Format("发货单号为{0}内,产品编号{1}未在本品赠品目录库中未找到,自动从产品库中增加该赠品信息!", SheetCode, productcode));
                                    }
                                }
                                #endregion
                            }
                            else
                            {
                                #region 从产品目录中取该产品信息
                                IList <PDT_Product> _products = PDT_ProductBLL.GetModelList("Code='" + productcode + "' AND Brand IN (SELECT ID FROM MCS_Pub.dbo.PDT_Brand WHERE IsOpponent<>9)");
                                if (_products.Count > 0)
                                {
                                    product = _products[0];
                                }
                                else
                                {
                                    _products = PDT_ProductBLL.GetModelList("Code='" + productcode + "' AND Brand IN (SELECT ID FROM MCS_Pub.dbo.PDT_Brand WHERE IsOpponent = 9)");
                                    if (_products.Count == 0)
                                    {
                                        SendMessage(-22, string.Format("发货单号为{0}内,产品编号{1}未在产品物料目录库中未找到!", SheetCode, productcode));
                                        continue;
                                    }
                                    else
                                    {
                                        //在赠品目录中找到,则使用赠品目录的产品ID
                                        //不在产品中增加该成品 2012-04-25
                                        product = _products[0];

                                        ////在赠品目录里找到,但未在成品目录中,自动在成品目录中增加一条本品成品目录
                                        //PDT_ProductBLL productbll = new PDT_ProductBLL();
                                        //productbll.Model = _products[0];
                                        //productbll.Model.Brand = 1;
                                        //productbll.Model.Classify = 1;
                                        //productbll.Model.State = 2;         //停用
                                        //productbll.Model.ApproveFlag = 2;   //未审核
                                        //int productid = productbll.Add();

                                        //product = new PDT_ProductBLL(productid).Model;
                                        //SendMessage(0, string.Format("发货单号为{0}内,产品编号{1}未在成品目录库中未找到,自动从赠品库中增加该产品信息!", SheetCode, productcode));
                                    }
                                }
                                #endregion
                            }

                            if (product.State == 2 && product.ApproveFlag == 1)
                            {
                                //确认被停用的产品,不再同步
                                continue;
                            }

                            if (product.State == 3 && !string.IsNullOrEmpty(product["MasterProduct"]) && product["MasterProduct"] != "0")
                            {
                                //是子产品,自动归属到主产品
                                PDT_Product _p = new PDT_ProductBLL(int.Parse(product["MasterProduct"])).Model;
                                if (_p != null)
                                {
                                    product = _p;
                                }
                            }

                            if (product == null)
                            {
                                SendMessage(-23, productcode + "为无效产品编号!");
                                continue;
                            }
                            #endregion

                            if (packaging == "Ea" && product.ConvertFactor > 1)
                            {
                                //整箱单位,转换为零售单位
                                quantity = quantity * product.ConvertFactor;
                                price    = price / product.ConvertFactor;
                            }

                            ORD_OrderDeliveryDetail item = bll.Items.FirstOrDefault
                                                               (p => p.Product == product.ID && p.Client == client.ID);

                            if (item != null)
                            {
                                item.DeliveryQuantity += quantity;
                            }
                            else
                            {
                                item                  = new ORD_OrderDeliveryDetail();
                                item.Client           = client.ID;
                                item.Product          = product.ID;
                                item.Price            = price;
                                item.FactoryPrice     = price == 0 ? product.FactoryPrice : price;
                                item.DeliveryQuantity = quantity;
                                item.SignInQuantity   = 0;
                                item.BadQuantity      = 0;
                                item.LostQuantity     = 0;

                                bll.Items.Add(item);
                            }
                        }
                        #endregion

                        if (bll.Items.Count == 0)
                        {
                            SendMessage(0, "发货单号" + bll.Model.SheetCode + ",未获取到发货单产品明细");
                            continue;
                        }

                        #region 写入发货单
                        if (bll.Add() > 0)
                        {
                            //bll.Delivery(1);        //设为发货状态(20140708修改为数据库作业执行)
                            SendMessage(0, "成功导入发货数据!单号:" + bll.Model.SheetCode + ",发生日期:" + SalesDate.ToString("yyyy-MM-dd") + ",产品明细条数:" + bll.Items.Count.ToString() + ",同步进度:" + (i + 1).ToString() + "/" + dtShipHeader.Rows.Count.ToString());
                        }
                        else
                        {
                            SendMessage(-40, "导入发货数据失败!单号" + bll.Model.SheetCode);
                            continue;
                        }
                        #endregion
                        success += 1;
                    }
                    catch (Exception err)
                    {
                        SendMessage(0, err.StackTrace + err.Source + err.Message);
                        continue;
                    }
                    #endregion
                }

                SendMessage(0, "------" + begindate.ToString("yyyy-MM-dd HH:mm") +
                            "至" + enddate.ToString("yyyy-MM-dd HH:mm") +
                            "。总共同步完成" + success.ToString() + "条发货单记录!-------");
            }
            catch (Exception err)
            {
                SendMessage(0, "SyncSellOut()" + err.StackTrace + err.Source + err.Message);
            }
        }
        /// <summary>
        /// 同步经销商进货单
        /// </summary>
        /// <param name="begindate"></param>
        /// <param name="enddate"></param>
        /// <param name="clientcode"></param>
        private void SyncSellOut(DateTime begindate, DateTime enddate, string clientcode)
        {
            try
            {
                int success = 0;

                //从进销存数据库中读取出货数据
                DataTable dtShipHeader = null;

                if (string.IsNullOrEmpty(clientcode))
                {
                    dtShipHeader = ERPIF.GetShipHeader(begindate, enddate);
                }
                else
                {
                    SendMessage(0, "经销商代码:" + clientcode);
                    dtShipHeader = ERPIF.GetShipHeader(begindate, enddate, clientcode);
                }
                SendMessage(0, "本次共获取到" + dtShipHeader.Rows.Count.ToString() + "条发货单待同步!" + _sClientCode);

                for (int i = 0; i < dtShipHeader.Rows.Count; i++)
                {
                    DataRow dr = dtShipHeader.Rows[i];

                    if (_bCancelSync)
                    {
                        SendMessage(0, "同步进行中时,被用户取消!");
                        break;
                    }
                    #region 逐条同步发货单
                    try
                    {
                        string SheetCode = dr["DELIVERY_ID"].ToString().Trim();
                        string ClientCode = dr["CUST_NUMBER"].ToString().Trim();
                        DateTime SalesDate = DateTime.Parse(dr["CARRY_SDATE"].ToString().Trim());
                        string DJType = dr["ORDER_TYPE"].ToString().Trim();
                        decimal discount = 0;
                        decimal.TryParse(dr["DISCOUNT"].ToString().Trim(), out discount);

                        if (DJType == "R") continue;

                        if (ORD_OrderDeliveryBLL.GetModelList(" SheetCode = '" + SheetCode + "'").Count > 0)
                        {
                            continue;
                        }

                        #region 判断客户号有效否
                        IList<CM_Client> clients = CM_ClientBLL.GetModelList(" Code= '" + ClientCode.ToString() + "' AND ActiveFlag=1 AND ApproveFlag=1");
                        if (clients.Count == 0)
                        {
                            SendMessage(-11, "经销商编号为" + ClientCode.ToString() + "的匹配错误!");
                            continue;
                        }
                        CM_Client client = clients[0];
                        if (client["DIClassify"] == "3")
                        {
                            //经销商子户头
                            client = new CM_ClientBLL(client.Supplier).Model;
                            if (client == null)
                            {
                                SendMessage(-12, "经销商编号为" + ClientCode.ToString() + "对应的经销商为子户头,但未关联正确的主户头!");
                                continue;
                            }
                        }

                        if (client["DIClassify"] != "1")
                        {
                            SendMessage(-13, "经销商编号为" + ClientCode.ToString() + "对应的经销商不是主户头!");
                            continue;
                        }
                        #endregion

                        #region 生成销量单头
                        ORD_OrderDeliveryBLL bll = new ORD_OrderDeliveryBLL();
                        bll.Model.SheetCode = SheetCode;
                        bll.Model.AccountMonth = AC_AccountMonthBLL.GetMonthByDate(SalesDate);
                        bll.Model.OrganizeCity = client.OrganizeCity;
                        bll.Model.State = 1;        //未发货
                        bll.Model.Client = client.ID;
                        bll.Model.Store = client.Supplier;
                        if (dr["CARRY_EDATE"].ToString() != "")
                            bll.Model.PreArrivalDate = DateTime.Parse(dr["CARRY_EDATE"].ToString().Trim());
                        else
                            bll.Model.PreArrivalDate = SalesDate.AddDays(7);      //默认7天后到货
                        bll.Model.ApproveFlag = 1;
                        bll.Model.InsertStaff = 1;
                        bll.Model["DeliveryTime"] = SalesDate.ToString("yyyy-MM-dd HH:mm");
                        bll.Model["TruckNo"] = dr["CARRY_CAR"].ToString().Trim();
                        bll.Model["DeliveryStaff"] = "1";
                        bll.Model["DeliveryRemark"] = "ERP发货单提取";
                        bll.Model["Discount"] = discount.ToString("0.###");
                        bll.Model["CarrySheetCode"] = dr["REQUEST_NUMBER"].ToString().Trim();
                        #endregion

                        //获取进销存明细
                        DataTable dt_detail = ERPIF.GetShipDetail(SheetCode);

                        #region 生成销量明细
                        foreach (DataRow dr_detail in dt_detail.Rows)
                        {
                            string productcode = dr_detail["ITEM_NO"].ToString();
                            decimal price = decimal.Parse(dr_detail["PRICE"].ToString());
                            int quantity = (int)decimal.Parse(dr_detail["SHIP_QTY"].ToString());
                            string packaging = dr_detail["UOM"].ToString();

                            if (quantity == 0) continue;

                            #region 验证产品信息
                            PDT_Product product = null;
                            if (price == 0)
                            {
                                #region 价格为0,表示为赠品,取赠品目录中的物料
                                IList<PDT_Product> _products = PDT_ProductBLL.GetModelList("Code='" + productcode + "' AND Brand IN (SELECT ID FROM MCS_Pub.dbo.PDT_Brand WHERE IsOpponent=9)");
                                if (_products.Count > 0)
                                {
                                    product = _products[0];
                                }
                                else
                                {
                                    _products = PDT_ProductBLL.GetModelList("Code='" + productcode + "' AND Brand IN (SELECT ID FROM MCS_Pub.dbo.PDT_Brand WHERE IsOpponent <> 9)");
                                    if (_products.Count == 0)
                                    {
                                        SendMessage(-21, string.Format("发货单号为{0}内,产品编号{1}未在产品目录库中未找到!", SheetCode, productcode));
                                        continue;
                                    }
                                    else
                                    {
                                        //在成品目录里找到,但未在赠品目录中,自动在赠品目录中增加一条本品赠品目录
                                        PDT_ProductBLL productbll = new PDT_ProductBLL();
                                        productbll.Model = _products[0];
                                        productbll.Model.Brand = 3;         //本品赠品
                                        productbll.Model.Classify = 100;    //其他品类
                                        productbll.Model.State = 2;         //停用
                                        productbll.Model.ApproveFlag = 2;   //未审核
                                        int productid = productbll.Add();

                                        product = new PDT_ProductBLL(productid).Model;
                                        SendMessage(0, string.Format("发货单号为{0}内,产品编号{1}未在本品赠品目录库中未找到,自动从产品库中增加该赠品信息!", SheetCode, productcode));
                                    }
                                }
                                #endregion
                            }
                            else
                            {
                                #region 从产品目录中取该产品信息
                                IList<PDT_Product> _products = PDT_ProductBLL.GetModelList("Code='" + productcode + "' AND Brand IN (SELECT ID FROM MCS_Pub.dbo.PDT_Brand WHERE IsOpponent<>9)");
                                if (_products.Count > 0)
                                {
                                    product = _products[0];
                                }
                                else
                                {
                                    _products = PDT_ProductBLL.GetModelList("Code='" + productcode + "' AND Brand IN (SELECT ID FROM MCS_Pub.dbo.PDT_Brand WHERE IsOpponent = 9)");
                                    if (_products.Count == 0)
                                    {
                                        SendMessage(-22, string.Format("发货单号为{0}内,产品编号{1}未在产品物料目录库中未找到!", SheetCode, productcode));
                                        continue;
                                    }
                                    else
                                    {
                                        //在赠品目录中找到,则使用赠品目录的产品ID
                                        //不在产品中增加该成品 2012-04-25
                                        product = _products[0];

                                        ////在赠品目录里找到,但未在成品目录中,自动在成品目录中增加一条本品成品目录
                                        //PDT_ProductBLL productbll = new PDT_ProductBLL();
                                        //productbll.Model = _products[0];
                                        //productbll.Model.Brand = 1;
                                        //productbll.Model.Classify = 1;
                                        //productbll.Model.State = 2;         //停用
                                        //productbll.Model.ApproveFlag = 2;   //未审核
                                        //int productid = productbll.Add();

                                        //product = new PDT_ProductBLL(productid).Model;
                                        //SendMessage(0, string.Format("发货单号为{0}内,产品编号{1}未在成品目录库中未找到,自动从赠品库中增加该产品信息!", SheetCode, productcode));
                                    }

                                }
                                #endregion
                            }

                            if (product.State == 2 && product.ApproveFlag == 1)
                            {
                                //确认被停用的产品,不再同步
                                continue;
                            }

                            if (product.State == 3 && !string.IsNullOrEmpty(product["MasterProduct"]) && product["MasterProduct"] != "0")
                            {
                                //是子产品,自动归属到主产品
                                PDT_Product _p = new PDT_ProductBLL(int.Parse(product["MasterProduct"])).Model;
                                if (_p != null) product = _p;
                            }

                            if (product == null)
                            {
                                SendMessage(-23, productcode + "为无效产品编号!");
                                continue;
                            }
                            #endregion

                            if (packaging == "Ea" && product.ConvertFactor > 1)
                            {
                                //整箱单位,转换为零售单位
                                quantity = quantity * product.ConvertFactor;
                                price = price / product.ConvertFactor;
                            }

                            ORD_OrderDeliveryDetail item = bll.Items.FirstOrDefault
                                (p => p.Product == product.ID && p.Client == client.ID);

                            if (item != null)
                            {
                                item.DeliveryQuantity += quantity;
                            }
                            else
                            {
                                item = new ORD_OrderDeliveryDetail();
                                item.Client = client.ID;
                                item.Product = product.ID;
                                item.Price = price;
                                item.FactoryPrice = price == 0 ? product.FactoryPrice : price;
                                item.DeliveryQuantity = quantity;
                                item.SignInQuantity = 0;
                                item.BadQuantity = 0;
                                item.LostQuantity = 0;

                                bll.Items.Add(item);
                            }
                        }
                        #endregion

                        if (bll.Items.Count == 0)
                        {
                            SendMessage(0, "发货单号" + bll.Model.SheetCode + ",未获取到发货单产品明细");
                            continue;
                        }

                        #region 写入发货单
                        if (bll.Add() > 0)
                        {
                            //bll.Delivery(1);        //设为发货状态(20140708修改为数据库作业执行)
                            SendMessage(0, "成功导入发货数据!单号:" + bll.Model.SheetCode + ",发生日期:" + SalesDate.ToString("yyyy-MM-dd") + ",产品明细条数:" + bll.Items.Count.ToString() + ",同步进度:" + (i + 1).ToString() + "/" + dtShipHeader.Rows.Count.ToString());
                        }
                        else
                        {
                            SendMessage(-40, "导入发货数据失败!单号" + bll.Model.SheetCode);
                            continue;
                        }
                        #endregion
                        success += 1;
                    }
                    catch (Exception err)
                    {
                        SendMessage(0, err.StackTrace + err.Source + err.Message);
                        continue;
                    }
                    #endregion
                }

                SendMessage(0, "------" + begindate.ToString("yyyy-MM-dd HH:mm") +
                    "至" + enddate.ToString("yyyy-MM-dd HH:mm") +
                    "。总共同步完成" + success.ToString() + "条发货单记录!-------");
            }
            catch (Exception err)
            {
                SendMessage(0, "SyncSellOut()" + err.StackTrace + err.Source + err.Message);
            }
        }
        public string DoImportProduct(int TemplateID, int Client, ISheet Sheet, out int State)
        {
            string ImportInfo = "【商品资料】Excel表:";
            State = 0;
            IPT_UploadTemplateBLL _template = new IPT_UploadTemplateBLL(TemplateID);

            List<string> listPDT = new List<string>() { "序号", "大类", "小类", "商品编码", "产品名称", "规格型号", "大单位", "小单位", "整零换算系数", "整件重量", "销售价", "采购价", "保质期", "整件条码", "零售条码" };

            DataTable dt = null;
            bool flag = VertifySheet(Sheet, listPDT, out dt, ref ImportInfo);
            if (!flag) { State = 4; return ImportInfo; }

            foreach (DataRow dr in dt.Rows)//循环导入数据
            {
                try
                {
                    decimal _pdtBuyPrice = (!string.IsNullOrEmpty(dr["采购价"].ToString())) ? Convert.ToDecimal(dr["采购价"].ToString()) : 0;
                    decimal _pdtSalesPrice = (!string.IsNullOrEmpty(dr["销售价"].ToString())) ? Convert.ToDecimal(dr["销售价"].ToString()) : 0;
                    if (_pdtSalesPrice == 0) continue;//销售价格围标表示门店不经营

                    int _pdtID = 0;
                    string _pdtCode = dr["商品编码"].ToString();

                    IList<PDT_ProductExtInfo> _listPDTExtInfo = PDT_ProductExtInfoBLL.GetProductExtInfoList_BySupplier(_template.Model.ClientID).Where(m => m.Code == _pdtCode).ToList();
                    if (_listPDTExtInfo != null && _listPDTExtInfo.Count > 0)
                    {
                        ImportInfo += string.Format("序号为{0},产品编码为{1}的产品与现有产品编码重复,跳过此行信息\r\n", dr["序号"], _pdtCode);
                        continue;
                    }

                    int _pdtCategory = 0;//类别
                    IList<PDT_Product> listPdt = PDT_ProductBLL.GetModelList(string.Format(" OwnerType IN (1,2) AND FactoryCode='{0}' ", _pdtCode));
                    if (listPdt == null || listPdt.Count == 0)//添加新产品
                    {
                        #region 获取产品类别
                        IList<PDT_Category> _listCategory = PDT_CategoryBLL.GetModelList(" SuperID=1 AND ApproveFlag=1 AND(OwnerType IN(1,2) OR (OwnerType=3 AND OwnerClient=" + _template.Model.ClientID.ToString() + " )) AND Name='" + dr["大类"].ToString() + "'");//获取大类(大类可能属于平台级和厂商级,也有可能属于经销商自己)
                        if (_listCategory == null || _listCategory.Count == 0)
                        {
                            PDT_CategoryBLL _bllCategory = new PDT_CategoryBLL();
                            _bllCategory.Model.Name = dr["大类"].ToString();
                            _bllCategory.Model.SuperID = 1;
                            _bllCategory.Model.EnabledFlag = "Y";
                            _bllCategory.Model.OwnerType = 3;
                            _bllCategory.Model.OwnerClient = _template.Model.ClientID;
                            _bllCategory.Model.Remark = "Excel批量导入";
                            _bllCategory.Model.ApproveFlag = 1;
                            _bllCategory.Model.InsertTime = DateTime.Now;
                            _bllCategory.Model.InsertStaff = _template.Model.InsertStaff;
                            _pdtCategory = _bllCategory.Add();
                        }
                        else { _pdtCategory = _listCategory[0].ID; }
                        IList<PDT_Category> _listCategory2 = PDT_CategoryBLL.GetModelList(" ApproveFlag=1 AND SuperID=" + _pdtCategory.ToString() + " AND (OwnerType IN(1,2) OR (OwnerType=3 AND OwnerClient=" + _template.Model.ClientID.ToString() + ")) AND Name='" + dr["小类"].ToString() + "'");//获取小类
                        if (_listCategory2 == null || _listCategory2.Count == 0)
                        {
                            PDT_CategoryBLL _bllCategory = new PDT_CategoryBLL();
                            _bllCategory.Model.Name = dr["小类"].ToString();
                            _bllCategory.Model.SuperID = _pdtCategory;
                            _bllCategory.Model.EnabledFlag = "Y";
                            _bllCategory.Model.OwnerType = 3;
                            _bllCategory.Model.OwnerClient = _template.Model.ClientID;
                            _bllCategory.Model.Remark = "Excel批量导入";
                            _bllCategory.Model.ApproveFlag = 1;
                            _bllCategory.Model.InsertTime = DateTime.Now;
                            _bllCategory.Model.InsertStaff = _template.Model.InsertStaff;
                            _pdtCategory = _bllCategory.Add();
                        }
                        else { _pdtCategory = _listCategory2[0].ID; }
                        #endregion

                        string _pdtName = dr["产品名称"].ToString();

                        if (string.IsNullOrEmpty(_pdtCode) && string.IsNullOrEmpty(_pdtName))
                        {
                            ImportInfo += string.Format("序号为{0}的行产品编码和名称均为空,跳过此行信息\r\n", dr["序号"]);
                            continue;
                        }
                        string _pdtSpec = dr["规格型号"].ToString();
                        IList<Dictionary_Data> _listData = DictionaryBLL.Dictionary_Data_GetAlllList(" TableName='PDT_Packaging' ");
                        string _strPdtTrafficPackaging = _listData.First(m => m.Name == dr["大单位"].ToString()).Code;
                        int _pdtTrafficPackaging = 0; int.TryParse(_strPdtTrafficPackaging, out _pdtTrafficPackaging);//整件单位
                        string _strPdtPackaging = _listData.First(m => m.Name == dr["小单位"].ToString()).Code;
                        int _pdtPackaging = 0; int.TryParse(_strPdtPackaging, out _pdtPackaging);//零售包装

                        int _pdtConvertFactor = (!string.IsNullOrEmpty(dr["整零换算系数"].ToString())) ? Convert.ToInt32(dr["整零换算系数"].ToString()) : 0;//重要信息强制显示,出错时直接跳过此行
                        decimal _pdtWeight = 0; decimal.TryParse(dr["整件重量"].ToString(), out _pdtWeight);
                        int _pdtExpiry = 0; int.TryParse(dr["保质期"].ToString(), out _pdtExpiry);
                        string _pdtBoxBarCode = dr["整件条码"].ToString();
                        string _pdtBarCode = dr["零售条码"].ToString();

                        PDT_ProductBLL _bllProduct = new PDT_ProductBLL();
                        _bllProduct.Model.FullName = _pdtName;
                        _bllProduct.Model.ShortName = _pdtName;
                        _bllProduct.Model.Spec = _pdtSpec;
                        _bllProduct.Model.TrafficPackaging = _pdtTrafficPackaging;
                        _bllProduct.Model.Packaging = _pdtPackaging;
                        _bllProduct.Model.ConvertFactor = _pdtConvertFactor;
                        _bllProduct.Model.BoxBarCode = _pdtBoxBarCode;
                        _bllProduct.Model.BarCode = _pdtBarCode;
                        _bllProduct.Model.Weight = _pdtWeight;
                        _bllProduct.Model.Expiry = _pdtExpiry;
                        _bllProduct.Model.Category = _pdtCategory;
                        _bllProduct.Model.State = 1;//在用产品
                        _bllProduct.Model.OwnerClient = _template.Model.ClientID;//所属经销商
                        _bllProduct.Model.OwnerType = 3;//经销商级
                        _bllProduct.Model.ApproveFlag = 1;
                        _bllProduct.Model.InsertTime = DateTime.Now;
                        _bllProduct.Model.InsertStaff = _template.Model.InsertStaff;
                        _bllProduct.Model.Remark = "Excel批量导入";

                        _pdtID = _bllProduct.Add();
                    }
                    else _pdtID = listPdt[0].ID;//已存在的厂商或平台级产品

                    IList<PDT_ProductExtInfo> _listPdtExtInfo = PDT_ProductExtInfoBLL.GetModelList(" Product=" + _pdtID.ToString() + " AND Supplier=" + _template.Model.ClientID.ToString() + " AND ApproveFlag=1 ");
                    if (_listPdtExtInfo == null || _listPdtExtInfo.Count == 0)
                    {
                        PDT_ProductExtInfoBLL _pdtExtInfoBLL = new PDT_ProductExtInfoBLL();
                        _pdtExtInfoBLL.Model.Supplier = _template.Model.ClientID;
                        _pdtExtInfoBLL.Model.Product = _pdtID;
                        _pdtExtInfoBLL.Model.Code = _pdtCode;
                        _pdtExtInfoBLL.Model.BuyPrice = _pdtBuyPrice;
                        _pdtExtInfoBLL.Model.SalesPrice = _pdtSalesPrice;
                        _pdtExtInfoBLL.Model.SalesState = 1;
                        _pdtExtInfoBLL.Model.Category = _pdtCategory;
                        _pdtExtInfoBLL.Model.Remark = "Excel批量导入";
                        _pdtExtInfoBLL.Model.ApproveFlag = 1;
                        _pdtExtInfoBLL.Model.InsertStaff = _template.Model.InsertStaff;
                        _pdtExtInfoBLL.Model.InsertTime = DateTime.Now;
                        _pdtExtInfoBLL.Add();
                    }
                    else
                    {
                        PDT_ProductExtInfoBLL _pdtExtInfoBLL = new PDT_ProductExtInfoBLL(_listPdtExtInfo[0].ID);
                        _pdtExtInfoBLL.Model.BuyPrice = _pdtBuyPrice;
                        _pdtExtInfoBLL.Model.SalesPrice = _pdtSalesPrice;
                        _pdtExtInfoBLL.Model.Remark = "Excel批量导入时修改";
                        _pdtExtInfoBLL.Model.UpdateStaff = _template.Model.InsertStaff;
                        _pdtExtInfoBLL.Model.UpdateTime = DateTime.Now;
                        _pdtExtInfoBLL.Update();
                    }
                }
                catch (Exception ex)
                {
                    ImportInfo += "导入序列号为" + dr["序号"].ToString() + "的数据行时出现错误,错误说明:" + ex.Message + "\r\n";
                    State = 5;
                    continue;
                }
            }
            if (State == 0) State = 3;
            ImportInfo += (State == 3 ? "导入完成!\r\n" : "");
            IPT_UploadTemplateMessageBLL _bllUploadTemplateMessage = new IPT_UploadTemplateMessageBLL();
            _bllUploadTemplateMessage.Model.TemplateID = TemplateID;
            _bllUploadTemplateMessage.Model.MessageType = State;
            _bllUploadTemplateMessage.Model.Content = ImportInfo;
            _bllUploadTemplateMessage.Add();
            return ImportInfo;
        }
Example #4
0
        public string DoImportProduct(int TemplateID, int Client, ISheet Sheet, out int State)
        {
            string ImportInfo = "【商品资料】Excel表:";

            State = 0;
            IPT_UploadTemplateBLL _template = new IPT_UploadTemplateBLL(TemplateID);

            List <string> listPDT = new List <string>()
            {
                "序号", "大类", "小类", "商品编码", "产品名称", "规格型号", "大单位", "小单位", "整零换算系数", "整件重量", "销售价", "采购价", "保质期", "整件条码", "零售条码"
            };

            DataTable dt   = null;
            bool      flag = VertifySheet(Sheet, listPDT, out dt, ref ImportInfo);

            if (!flag)
            {
                State = 4; return(ImportInfo);
            }

            foreach (DataRow dr in dt.Rows)//循环导入数据
            {
                try
                {
                    decimal _pdtBuyPrice   = (!string.IsNullOrEmpty(dr["采购价"].ToString())) ? Convert.ToDecimal(dr["采购价"].ToString()) : 0;
                    decimal _pdtSalesPrice = (!string.IsNullOrEmpty(dr["销售价"].ToString())) ? Convert.ToDecimal(dr["销售价"].ToString()) : 0;
                    if (_pdtSalesPrice == 0)
                    {
                        continue;                     //销售价格围标表示门店不经营
                    }
                    int    _pdtID   = 0;
                    string _pdtCode = dr["商品编码"].ToString();

                    IList <PDT_ProductExtInfo> _listPDTExtInfo = PDT_ProductExtInfoBLL.GetProductExtInfoList_BySupplier(_template.Model.ClientID).Where(m => m.Code == _pdtCode).ToList();
                    if (_listPDTExtInfo != null && _listPDTExtInfo.Count > 0)
                    {
                        ImportInfo += string.Format("序号为{0},产品编码为{1}的产品与现有产品编码重复,跳过此行信息\r\n", dr["序号"], _pdtCode);
                        continue;
                    }

                    int _pdtCategory            = 0;           //类别
                    IList <PDT_Product> listPdt = PDT_ProductBLL.GetModelList(string.Format(" OwnerType IN (1,2) AND FactoryCode='{0}' ", _pdtCode));
                    if (listPdt == null || listPdt.Count == 0) //添加新产品
                    {
                        #region 获取产品类别
                        IList <PDT_Category> _listCategory = PDT_CategoryBLL.GetModelList(" SuperID=1 AND ApproveFlag=1 AND(OwnerType IN(1,2) OR (OwnerType=3 AND OwnerClient=" + _template.Model.ClientID.ToString() + " )) AND Name='" + dr["大类"].ToString() + "'");//获取大类(大类可能属于平台级和厂商级,也有可能属于经销商自己)
                        if (_listCategory == null || _listCategory.Count == 0)
                        {
                            PDT_CategoryBLL _bllCategory = new PDT_CategoryBLL();
                            _bllCategory.Model.Name        = dr["大类"].ToString();
                            _bllCategory.Model.SuperID     = 1;
                            _bllCategory.Model.EnabledFlag = "Y";
                            _bllCategory.Model.OwnerType   = 3;
                            _bllCategory.Model.OwnerClient = _template.Model.ClientID;
                            _bllCategory.Model.Remark      = "Excel批量导入";
                            _bllCategory.Model.ApproveFlag = 1;
                            _bllCategory.Model.InsertTime  = DateTime.Now;
                            _bllCategory.Model.InsertStaff = _template.Model.InsertStaff;
                            _pdtCategory = _bllCategory.Add();
                        }
                        else
                        {
                            _pdtCategory = _listCategory[0].ID;
                        }
                        IList <PDT_Category> _listCategory2 = PDT_CategoryBLL.GetModelList(" ApproveFlag=1 AND SuperID=" + _pdtCategory.ToString() + " AND (OwnerType IN(1,2) OR (OwnerType=3 AND OwnerClient=" + _template.Model.ClientID.ToString() + ")) AND Name='" + dr["小类"].ToString() + "'");//获取小类
                        if (_listCategory2 == null || _listCategory2.Count == 0)
                        {
                            PDT_CategoryBLL _bllCategory = new PDT_CategoryBLL();
                            _bllCategory.Model.Name        = dr["小类"].ToString();
                            _bllCategory.Model.SuperID     = _pdtCategory;
                            _bllCategory.Model.EnabledFlag = "Y";
                            _bllCategory.Model.OwnerType   = 3;
                            _bllCategory.Model.OwnerClient = _template.Model.ClientID;
                            _bllCategory.Model.Remark      = "Excel批量导入";
                            _bllCategory.Model.ApproveFlag = 1;
                            _bllCategory.Model.InsertTime  = DateTime.Now;
                            _bllCategory.Model.InsertStaff = _template.Model.InsertStaff;
                            _pdtCategory = _bllCategory.Add();
                        }
                        else
                        {
                            _pdtCategory = _listCategory2[0].ID;
                        }
                        #endregion

                        string _pdtName = dr["产品名称"].ToString();

                        if (string.IsNullOrEmpty(_pdtCode) && string.IsNullOrEmpty(_pdtName))
                        {
                            ImportInfo += string.Format("序号为{0}的行产品编码和名称均为空,跳过此行信息\r\n", dr["序号"]);
                            continue;
                        }
                        string _pdtSpec = dr["规格型号"].ToString();
                        IList <Dictionary_Data> _listData = DictionaryBLL.Dictionary_Data_GetAlllList(" TableName='PDT_Packaging' ");
                        string _strPdtTrafficPackaging    = _listData.First(m => m.Name == dr["大单位"].ToString()).Code;
                        int    _pdtTrafficPackaging       = 0; int.TryParse(_strPdtTrafficPackaging, out _pdtTrafficPackaging);                      //整件单位
                        string _strPdtPackaging           = _listData.First(m => m.Name == dr["小单位"].ToString()).Code;
                        int    _pdtPackaging = 0; int.TryParse(_strPdtPackaging, out _pdtPackaging);                                                 //零售包装

                        int     _pdtConvertFactor = (!string.IsNullOrEmpty(dr["整零换算系数"].ToString())) ? Convert.ToInt32(dr["整零换算系数"].ToString()) : 0; //重要信息强制显示,出错时直接跳过此行
                        decimal _pdtWeight        = 0; decimal.TryParse(dr["整件重量"].ToString(), out _pdtWeight);
                        int     _pdtExpiry        = 0; int.TryParse(dr["保质期"].ToString(), out _pdtExpiry);
                        string  _pdtBoxBarCode    = dr["整件条码"].ToString();
                        string  _pdtBarCode       = dr["零售条码"].ToString();

                        PDT_ProductBLL _bllProduct = new PDT_ProductBLL();
                        _bllProduct.Model.FullName         = _pdtName;
                        _bllProduct.Model.ShortName        = _pdtName;
                        _bllProduct.Model.Spec             = _pdtSpec;
                        _bllProduct.Model.TrafficPackaging = _pdtTrafficPackaging;
                        _bllProduct.Model.Packaging        = _pdtPackaging;
                        _bllProduct.Model.ConvertFactor    = _pdtConvertFactor;
                        _bllProduct.Model.BoxBarCode       = _pdtBoxBarCode;
                        _bllProduct.Model.BarCode          = _pdtBarCode;
                        _bllProduct.Model.Weight           = _pdtWeight;
                        _bllProduct.Model.Expiry           = _pdtExpiry;
                        _bllProduct.Model.Category         = _pdtCategory;
                        _bllProduct.Model.State            = 1;                        //在用产品
                        _bllProduct.Model.OwnerClient      = _template.Model.ClientID; //所属经销商
                        _bllProduct.Model.OwnerType        = 3;                        //经销商级
                        _bllProduct.Model.ApproveFlag      = 1;
                        _bllProduct.Model.InsertTime       = DateTime.Now;
                        _bllProduct.Model.InsertStaff      = _template.Model.InsertStaff;
                        _bllProduct.Model.Remark           = "Excel批量导入";

                        _pdtID = _bllProduct.Add();
                    }
                    else
                    {
                        _pdtID = listPdt[0].ID; //已存在的厂商或平台级产品
                    }
                    IList <PDT_ProductExtInfo> _listPdtExtInfo = PDT_ProductExtInfoBLL.GetModelList(" Product=" + _pdtID.ToString() + " AND Supplier=" + _template.Model.ClientID.ToString() + " AND ApproveFlag=1 ");
                    if (_listPdtExtInfo == null || _listPdtExtInfo.Count == 0)
                    {
                        PDT_ProductExtInfoBLL _pdtExtInfoBLL = new PDT_ProductExtInfoBLL();
                        _pdtExtInfoBLL.Model.Supplier    = _template.Model.ClientID;
                        _pdtExtInfoBLL.Model.Product     = _pdtID;
                        _pdtExtInfoBLL.Model.Code        = _pdtCode;
                        _pdtExtInfoBLL.Model.BuyPrice    = _pdtBuyPrice;
                        _pdtExtInfoBLL.Model.SalesPrice  = _pdtSalesPrice;
                        _pdtExtInfoBLL.Model.SalesState  = 1;
                        _pdtExtInfoBLL.Model.Category    = _pdtCategory;
                        _pdtExtInfoBLL.Model.Remark      = "Excel批量导入";
                        _pdtExtInfoBLL.Model.ApproveFlag = 1;
                        _pdtExtInfoBLL.Model.InsertStaff = _template.Model.InsertStaff;
                        _pdtExtInfoBLL.Model.InsertTime  = DateTime.Now;
                        _pdtExtInfoBLL.Add();
                    }
                    else
                    {
                        PDT_ProductExtInfoBLL _pdtExtInfoBLL = new PDT_ProductExtInfoBLL(_listPdtExtInfo[0].ID);
                        _pdtExtInfoBLL.Model.BuyPrice    = _pdtBuyPrice;
                        _pdtExtInfoBLL.Model.SalesPrice  = _pdtSalesPrice;
                        _pdtExtInfoBLL.Model.Remark      = "Excel批量导入时修改";
                        _pdtExtInfoBLL.Model.UpdateStaff = _template.Model.InsertStaff;
                        _pdtExtInfoBLL.Model.UpdateTime  = DateTime.Now;
                        _pdtExtInfoBLL.Update();
                    }
                }
                catch (Exception ex)
                {
                    ImportInfo += "导入序列号为" + dr["序号"].ToString() + "的数据行时出现错误,错误说明:" + ex.Message + "\r\n";
                    State       = 5;
                    continue;
                }
            }
            if (State == 0)
            {
                State = 3;
            }
            ImportInfo += (State == 3 ? "导入完成!\r\n" : "");
            IPT_UploadTemplateMessageBLL _bllUploadTemplateMessage = new IPT_UploadTemplateMessageBLL();
            _bllUploadTemplateMessage.Model.TemplateID  = TemplateID;
            _bllUploadTemplateMessage.Model.MessageType = State;
            _bllUploadTemplateMessage.Model.Content     = ImportInfo;
            _bllUploadTemplateMessage.Add();
            return(ImportInfo);
        }