Пример #1
0
        /// <summary>
        /// 移库调拨申请
        /// </summary>
        /// <param name="User"></param>
        /// <param name="DeliveryInfo">移库单信息</param>
        /// <param name="ErrorInfo">出错消息</param>
        /// <returns></returns>
        public static int TransWithVehicle(UserInfo User, Delivery DeliveryInfo, out string ErrorInfo)
        {
            ErrorInfo = "";
            LogWriter.WriteLog("PBMIFService.TransWithVehicle:UserName="******",DeliveryInfo=" + JsonConvert.SerializeObject(DeliveryInfo));

            //经销商级人员,供货商只能是自己所属的经销商
            if (User.OwnerType == 3)
            {
                DeliveryInfo.Supplier = User.ClientID;
                DeliveryInfo.Client = User.ClientID;
            }

            if (DeliveryInfo.Classify != 5 && DeliveryInfo.Classify != 6)
            {
                ErrorInfo = "单据类别无效";
                return -1;
            }

            #region 根据车辆设置调入或调出车仓库
            //如果有送货车辆,默认出货仓库为该车辆所关联仓库
            if (DeliveryInfo.DeliveryVehicle != 0)
            {
                CM_Vehicle v = new CM_VehicleBLL(DeliveryInfo.DeliveryVehicle).Model;
                if (v == null) { ErrorInfo = "送货车辆无效!"; return -1; }

                if (DeliveryInfo.Classify == 5)
                {
                    //车销移库
                    DeliveryInfo.ClientWareHouse = v.RelateWareHouse;
                }
                else if (DeliveryInfo.Classify == 6)
                {
                    //车销退库
                    DeliveryInfo.SupplierWareHouse = v.RelateWareHouse;
                }
            }
            #endregion

            //默认业务人员为当前员工
            if (DeliveryInfo.SalesMan == 0) DeliveryInfo.SalesMan = User.StaffID;

            #region 必填字段校验
            if (DeliveryInfo.Supplier == 0 && DeliveryInfo.SupplierWareHouse != 0)
            {
                DeliveryInfo.Supplier = new CM_WareHouseBLL(DeliveryInfo.SupplierWareHouse).Model.Client;
            }
            if (DeliveryInfo.Supplier == 0) { ErrorInfo = "无效的客户!"; return -2; }
            if (DeliveryInfo.SupplierWareHouse == 0) { ErrorInfo = "无效的调出仓库!"; return -2; }
            if (DeliveryInfo.Client == 0) { DeliveryInfo.Client = DeliveryInfo.Supplier; }
            if (DeliveryInfo.ClientWareHouse == 0) { ErrorInfo = "无效的调入仓库!"; return -2; }
            if (DeliveryInfo.Items == null || DeliveryInfo.Items.Count == 0) { ErrorInfo = "无调拨产品明细!"; return -10; }
            #endregion

            PBM_DeliveryBLL bll = new PBM_DeliveryBLL();

            #region 保存发货单头信息
            bll.Model.SheetCode = "";
            bll.Model.Supplier = DeliveryInfo.Supplier;
            bll.Model.SupplierWareHouse = DeliveryInfo.SupplierWareHouse;
            bll.Model.Client = DeliveryInfo.Client;
            bll.Model.ClientWareHouse = DeliveryInfo.ClientWareHouse;
            bll.Model.Classify = DeliveryInfo.Classify;              //5:车销移库 6:车销退库
            bll.Model.SalesMan = User.StaffID;
            bll.Model.PrepareMode = 1;           //1:快捷模式
            bll.Model.State = 1;                 //默认制单状态
            bll.Model.DeliveryVehicle = DeliveryInfo.DeliveryVehicle;
            bll.Model.Remark = DeliveryInfo.Remark;
            bll.Model.ApproveFlag = 2;
            bll.Model.InsertStaff = User.StaffID;
            #endregion

            #region 循环处理每个发货单明细
            foreach (Delivery.DeliveryDetail item in DeliveryInfo.Items)
            {
                if (item.Product == 0) continue;
                if (item.DeliveryQuantity <= 0 && item.SignInQuantity <= 0) continue;

                string lotnumber = item.LotNumber.Trim();
                int quantity = item.DeliveryQuantity <= 0 ? item.SignInQuantity : item.DeliveryQuantity;

                PDT_ProductBLL productbll = new PDT_ProductBLL(item.Product);
                if (productbll.Model == null) { ErrorInfo = "无效产品项,产品ID:" + item.Product; return -11; }
                PDT_ProductExtInfo extinfo = productbll.GetProductExtInfo(bll.Model.Supplier);
                if (productbll.Model == null) { ErrorInfo = "产品不在销售商的经营目录中," + productbll.Model.FullName; return -11; }

                //判断库存是否够销售
                int inv_quantity = INV_InventoryBLL.GetProductQuantity(bll.Model.SupplierWareHouse, item.Product, lotnumber);
                if (quantity > inv_quantity)
                {
                    ErrorInfo = "产品[" + productbll.Model.FullName + "]库存不足,当前库存:" + inv_quantity.ToString();
                    return -11;
                }

                #region 新增库存明细
                PBM_DeliveryDetail d = new PBM_DeliveryDetail();

                d.Product = item.Product;
                d.LotNumber = lotnumber;
                d.SalesMode = item.SalesMode == 0 ? 1 : item.SalesMode;     //默认为“销售”

                d.Price = extinfo.SalesPrice;       //默认销售价
                d.DiscountRate = 1;

                d.ConvertFactor = productbll.Model.ConvertFactor == 0 ? 1 : productbll.Model.ConvertFactor;
                d.DeliveryQuantity = quantity;
                d.SignInQuantity = d.DeliveryQuantity;
                d.Remark = item.Remark;

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

            //计算折扣金额
            bll.Model.DiscountAmount = 0;
            bll.Model.WipeAmount = 0;

            //计算实际销售金额
            bll.Model.ActAmount = Math.Round(bll.Items.Sum(p => Math.Round(p.Price * p.ConvertFactor, 2) * p.SignInQuantity / p.ConvertFactor), 2);

            int ret = bll.Add();
            if (ret <= 0) { ErrorInfo = "销售单保存失败!"; return ret; }
            return ret;
        }
    protected void bt_AddDetail_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ID"] == 0) return;
        PBM_DeliveryBLL _bll = new PBM_DeliveryBLL((int)ViewState["ID"]);

        if (ViewState["Details"] == null) return;
        ListTable<PBM_DeliveryDetail> Details = (ListTable<PBM_DeliveryDetail>)ViewState["Details"];

        int product = 0;
        int.TryParse(select_Product.SelectValue, out product);
        //int.TryParse(RadComboBox1.SelectedValue, out product);
        if (product != 0)
        {
            PDT_ProductBLL productbll = new PDT_ProductBLL(product, true);
            if (productbll.Model == null) return;
            if (productbll.Model.ConvertFactor == 0)
            {
                productbll.Model.ConvertFactor = 1;
                productbll.Update();
            }

            int quantity = 0;
            decimal price = 0;

            int.TryParse(tbx_Quantity.Text, out quantity);
            decimal.TryParse(tbx_Price.Text, out price);

            if (ddl_Unit.SelectedValue == "T")
            {
                //整件单位
                quantity = quantity * productbll.Model.ConvertFactor;
                price = price / productbll.Model.ConvertFactor;
            }

            if (quantity == 0)
            {
                MessageBox.Show(this, "请填写数量!");
                return;
            }

            if ((int)ViewState["Classify"] != 2)
            {
                #region 销售时判断库存数量是否够退货
                int inv_quantity = INV_InventoryBLL.GetProductQuantity((int)ViewState["WareHouse"], product, ddl_LotNumber.SelectedValue);
                if (quantity > inv_quantity)
                {
                    MessageBox.Show(this, "库存不足,不可销售出库!");
                    return;
                }
                #endregion
            }

            INV_Inventory inv = INV_InventoryBLL.GetInventoryByProductAndLotNumber((int)ViewState["WareHouse"],
                product, ddl_LotNumber.SelectedValue);
            if (inv == null)
            {
                MessageBox.Show(this, "请正确选择销售批号!");
                return;
            }

            PBM_DeliveryDetail d = null;

            if (ViewState["SelectedDetail"] != null)
            {
                d = (PBM_DeliveryDetail)ViewState["SelectedDetail"];
            }
            else
            {
                d = new PBM_DeliveryDetail();
                d.DeliveryID = (int)ViewState["ID"];

                if (Details.GetListItem().Count == 0)
                    d.ID = 1;
                else
                    d.ID = Details.GetListItem().Max(p => p.ID) + 1;
            }

            d.Product = product;
            d.LotNumber = ddl_LotNumber.SelectedValue;
            d.CostPrice = inv.Price;            //成本价取当前库存表的价格
            d.Price = price;                    //实际销售价
            d.ConvertFactor = productbll.Model.ConvertFactor;
            d.DiscountRate = 1;                 //默认全价
            d.DeliveryQuantity = quantity;
            d.SignInQuantity = d.DeliveryQuantity;
            d.LostQuantity = 0;
            d.BadQuantity = 0;
            d.SalesMode = 1;
            if (d.SalesMode == 2)
            { d.Price = 0; }

            if (ViewState["SelectedDetail"] != null)
            {
                Details.Update(d);
                //_bll.UpdateDetail(d);
                ViewState["SelectedDetail"] = null;
                bt_AddDetail.Text = "新 增";
                gv_List.SelectedIndex = -1;
            }
            else
            {
                Details.Add(d);
                //_bll.AddDetail(d);
            }

            tbx_Quantity.Text = "0";

            BindGrid();
        }
    }
Пример #3
0
        /// <summary>
        /// 验证销售单,当在执行预售送货时,或在收款单变更了销售单明细时,需要调用此接口更新销售单
        /// </summary>
        /// <param name="User"></param>
        /// <param name="DeliveryInfo"></param>
        /// <param name="ErrorInfo"></param>
        /// <returns></returns>
        public static int SaleOut_Update(UserInfo User, Delivery DeliveryInfo, out string ErrorInfo)
        {
            ErrorInfo = "";
            LogWriter.WriteLog("PBMIFService.SaleOut_Update:UserName="******",DeliveryInfo=" + JsonConvert.SerializeObject(DeliveryInfo));

            if (DeliveryInfo.ID == 0) { ErrorInfo = "销售单不存在,请先新增销售单!"; return -1; }

            PBM_DeliveryBLL bll = new PBM_DeliveryBLL(DeliveryInfo.ID);
            if (bll.Model == null) { ErrorInfo = "销售单不存在,请先新增销售单!"; return -1; }
            if (bll.Model.State > 3 || bll.Model.ApproveFlag == 1) { ErrorInfo = "销售单状态不允许执行此操作!"; return -1; }

            if (bll.Model.Supplier == 0) bll.Model.Supplier = DeliveryInfo.Supplier;
            if (bll.Model.Client == 0) bll.Model.Client = DeliveryInfo.Client;

            //如果有送货车辆,默认出货仓库为该车辆所关联仓库
            if (DeliveryInfo.DeliveryVehicle != 0 && DeliveryInfo.SupplierWareHouse == 0)
            {
                CM_Vehicle v = new CM_VehicleBLL(DeliveryInfo.DeliveryVehicle).Model;
                if (v == null) { ErrorInfo = "送货车辆无效!"; return -1; }

                DeliveryInfo.SupplierWareHouse = v.RelateWareHouse;
            }

            //默认业务人员为当前员工
            if (DeliveryInfo.SalesMan == 0) DeliveryInfo.SalesMan = User.StaffID;

            #region 必填字段校验
            if (bll.Model.Supplier == 0) { ErrorInfo = "无效的供货客户!"; return -2; }
            if (User.OwnerType == 3 && bll.Model.Supplier != User.ClientID) { ErrorInfo = "无效的供货客户!"; return -2; }
            if (bll.Model.Client == 0) { ErrorInfo = "无效的购买客户!"; return -2; }

            if (DeliveryInfo.SupplierWareHouse == 0) { ErrorInfo = "无效的供货仓库!"; return -2; }
            if (DeliveryInfo.Items == null || DeliveryInfo.Items.Count == 0) { ErrorInfo = "无销售产品明细!"; return -10; }
            #endregion

            #region 保存销售单头信息
            bll.Model.SalesMan = DeliveryInfo.SalesMan;
            bll.Model.DeliveryVehicle = DeliveryInfo.DeliveryVehicle;
            bll.Model.SupplierWareHouse = DeliveryInfo.SupplierWareHouse;
            bll.Model.WipeAmount = DeliveryInfo.WipeAmount;

            bll.Model.WorkList = DeliveryInfo.WorkList;
            bll.Model.Remark = DeliveryInfo.Remark;
            #endregion

            #region 循环处理每个订单明细
            foreach (Delivery.DeliveryDetail item in DeliveryInfo.Items)
            {
                if (item.Product == 0) continue;
                if (item.DeliveryQuantity <= 0 && item.SignInQuantity <= 0)
                {
                    if (item.DetailID == 0)
                        continue;
                    else
                        bll.DeleteDetail(item.DetailID);
                }

                string lotnumber = item.LotNumber.Trim();

                int quantity = 0;
                if (bll.Model.State == 1)
                    quantity = item.DeliveryQuantity == 0 ? item.SignInQuantity : item.DeliveryQuantity;
                else
                    quantity = item.SignInQuantity;

                string remark = item.Remark;

                PDT_ProductBLL productbll = new PDT_ProductBLL(item.Product);
                if (productbll.Model == null) { ErrorInfo = "无效产品项,产品ID:" + item.Product; return -11; }
                PDT_ProductExtInfo extinfo = productbll.GetProductExtInfo(bll.Model.Supplier);
                if (productbll.Model == null) { ErrorInfo = "产品不在销售商的经营目录中," + productbll.Model.FullName; return -11; }

                if (bll.Model.Classify != 2)
                {
                    //不为退货单时,判断车库存是否够销售
                    int inv_quantity = INV_InventoryBLL.GetProductQuantity(bll.Model.SupplierWareHouse, item.Product, lotnumber);
                    if (quantity > inv_quantity)
                    {
                        ErrorInfo = "产品[" + productbll.Model.FullName + "]库存不足,当前库存:" + inv_quantity.ToString();
                        return -11;
                    }
                }

                if (item.DetailID > 0)
                {
                    PBM_DeliveryDetail d = bll.GetDetailModel(item.DetailID);

                    if (bll.Model.State == 1) d.DeliveryQuantity = quantity;
                    d.SignInQuantity = item.SignInQuantity;
                    d.LotNumber = lotnumber;
                    d.Remark = item.Remark;
                    bll.UpdateDetail(d);
                }
                else
                {
                    #region 新增商品明细品项
                    PBM_DeliveryDetail d = new PBM_DeliveryDetail();

                    d.Product = item.Product;
                    d.LotNumber = lotnumber;
                    d.SalesMode = item.SalesMode == 0 ? 1 : item.SalesMode;     //默认为“销售”

                    d.Price = PDT_StandardPriceBLL.GetSalePrice(bll.Model.Client, bll.Model.Supplier, d.Product);
                    if (d.SalesMode == 1)
                        d.DiscountRate = (item.DiscountRate <= 0 || item.DiscountRate > 1) ? 1 : item.DiscountRate;
                    else
                        d.DiscountRate = 0;

                    d.ConvertFactor = productbll.Model.ConvertFactor == 0 ? 1 : productbll.Model.ConvertFactor;
                    d.DeliveryQuantity = item.DeliveryQuantity;
                    d.SignInQuantity = d.DeliveryQuantity;
                    d.Remark = item.Remark;

                    bll.AddDetail(d);
                    #endregion
                }
            }
            #endregion

            //计算折扣金额
            bll.Model.DiscountAmount = bll.Items.Sum(p => (1 - p.DiscountRate) *
                Math.Round(p.Price * p.ConvertFactor, 2) * p.SignInQuantity / p.ConvertFactor);

            //计算实际销售金额
            bll.Model.ActAmount = Math.Round((bll.Model.Classify == 2 ? -1 : 1) *
                bll.Items.Sum(p => p.DiscountRate * Math.Round(p.Price * p.ConvertFactor, 2) * p.SignInQuantity / p.ConvertFactor)
                - bll.Model.WipeAmount, 2);

            int ret = bll.Update();
            if (ret < 0) { ErrorInfo = "销售单保存失败!"; return ret; }

            return 0;
        }
Пример #4
0
        /// <summary>
        /// 新增销售发货单
        /// </summary>
        /// <param name="User"></param>
        /// <param name="DeliveryInfo">发货单信息</param>
        /// <param name="ErrorInfo">出错消息</param>
        /// <returns></returns>
        public static int SaleOut_Add(UserInfo User, Delivery DeliveryInfo, out string ErrorInfo)
        {
            ErrorInfo = "";
            LogWriter.WriteLog("PBMIFService.SaleOut_Add:UserName="******",DeliveryInfo=" + JsonConvert.SerializeObject(DeliveryInfo));

            //经销商级人员,供货商只能是自己所属的经销商
            if (User.OwnerType == 3) DeliveryInfo.Supplier = User.ClientID;

            //如果有送货车辆,默认出货仓库为该车辆所关联仓库
            if (DeliveryInfo.DeliveryVehicle != 0 && DeliveryInfo.SupplierWareHouse == 0)
            {
                CM_Vehicle v = new CM_VehicleBLL(DeliveryInfo.DeliveryVehicle).Model;
                if (v == null) { ErrorInfo = "送货车辆无效!"; return -1; }

                DeliveryInfo.SupplierWareHouse = v.RelateWareHouse;
            }

            //默认业务人员为当前员工
            if (DeliveryInfo.SalesMan == 0) DeliveryInfo.SalesMan = User.StaffID;

            #region 必填字段校验
            if (DeliveryInfo.Supplier == 0) { ErrorInfo = "无效的供货客户!"; return -2; }
            if (DeliveryInfo.Client == 0) { ErrorInfo = "无效的购买客户!"; return -2; }
            if (DeliveryInfo.SupplierWareHouse == 0) { ErrorInfo = "无效的供货仓库!"; return -2; }
            if (DeliveryInfo.Items == null || DeliveryInfo.Items.Count == 0) { ErrorInfo = "无销售产品明细!"; return -10; }
            #endregion

            PBM_DeliveryBLL bll = new PBM_DeliveryBLL();

            #region 保存销售单头信息
            bll.Model.SheetCode = "";
            bll.Model.Supplier = DeliveryInfo.Supplier;
            bll.Model.SupplierWareHouse = DeliveryInfo.SupplierWareHouse;
            bll.Model.Client = DeliveryInfo.Client;
            bll.Model.SalesMan = DeliveryInfo.SalesMan;
            bll.Model.Classify = (DeliveryInfo.Classify == 0 ? 1 : DeliveryInfo.Classify);              //默认销售单
            bll.Model.PrepareMode = (DeliveryInfo.PrepareMode == 0 ? 2 : DeliveryInfo.PrepareMode);     //默认车销模式
            bll.Model.State = 1;        //默认制单状态
            bll.Model.StandardPrice = DeliveryInfo.StandardPrice;
            bll.Model.OrderId = DeliveryInfo.OrderID;
            bll.Model.WipeAmount = DeliveryInfo.WipeAmount;
            bll.Model.DeliveryVehicle = DeliveryInfo.DeliveryVehicle;
            bll.Model.WorkList = DeliveryInfo.WorkList;
            bll.Model.Remark = DeliveryInfo.Remark;
            bll.Model.ApproveFlag = 2;
            bll.Model.InsertStaff = User.StaffID;
            #endregion

            #region 循环处理每个订单明细
            foreach (Delivery.DeliveryDetail item in DeliveryInfo.Items)
            {
                if (item.Product == 0) continue;
                if (item.DeliveryQuantity <= 0 && item.SignInQuantity <= 0) continue;

                string lotnumber = item.LotNumber.Trim();
                int deliveryquantity = item.DeliveryQuantity <= 0 ? item.SignInQuantity : item.DeliveryQuantity;

                PDT_ProductBLL productbll = new PDT_ProductBLL(item.Product);
                if (productbll.Model == null) { ErrorInfo = "无效产品项,产品ID:" + item.Product; return -11; }
                PDT_ProductExtInfo extinfo = productbll.GetProductExtInfo(bll.Model.Supplier);
                if (productbll.Model == null) { ErrorInfo = "产品不在销售商的经营目录中," + productbll.Model.FullName; return -11; }

                if (bll.Model.Classify != 2)
                {
                    //不为退货单时,判断车库存是否够销售
                    int inv_quantity = INV_InventoryBLL.GetProductQuantity(bll.Model.SupplierWareHouse, item.Product, lotnumber);
                    if (deliveryquantity > inv_quantity)
                    {
                        ErrorInfo = "产品[" + productbll.Model.FullName + "]库存不足,当前库存:" + inv_quantity.ToString();
                        return -11;
                    }
                }

                #region 新增销售明细
                PBM_DeliveryDetail d = new PBM_DeliveryDetail();

                d.Product = item.Product;
                d.LotNumber = lotnumber;
                d.SalesMode = item.SalesMode == 0 ? 1 : item.SalesMode;     //默认为“销售”

                if (item.Price > 0)
                    d.Price = item.Price;
                else
                    d.Price = PDT_StandardPriceBLL.GetSalePrice(bll.Model.Client, bll.Model.Supplier, d.Product);       //默认销售价

                if (d.SalesMode == 1)
                    d.DiscountRate = (item.DiscountRate <= 0 || item.DiscountRate > 1) ? 1 : item.DiscountRate;
                else
                    d.DiscountRate = 0;     //非销售时,0折销售

                d.ConvertFactor = productbll.Model.ConvertFactor == 0 ? 1 : productbll.Model.ConvertFactor;
                d.DeliveryQuantity = deliveryquantity;
                d.SignInQuantity = d.DeliveryQuantity;
                d.Remark = item.Remark;

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

            //计算折扣金额
            bll.Model.DiscountAmount = bll.Items.Sum(p => (1 - p.DiscountRate) *
                Math.Round(p.Price * p.ConvertFactor, 2) * p.DeliveryQuantity / p.ConvertFactor);

            //计算实际销售金额
            bll.Model.ActAmount = Math.Round((bll.Model.Classify == 2 ? -1 : 1) *
                bll.Items.Sum(p => p.DiscountRate * Math.Round(p.Price * p.ConvertFactor, 2) * p.SignInQuantity / p.ConvertFactor)
                - bll.Model.WipeAmount, 2);

            int deliveryid = bll.Add();
            if (deliveryid <= 0) { ErrorInfo = "销售单保存失败!"; return deliveryid; }

            #region 销售单直接完成
            if (DeliveryInfo.State == 4 && DeliveryInfo.PayInfos != null && DeliveryInfo.PayInfos.Count > 0)
            {
                LogWriter.WriteLog("PBMIFService.SaleOut_Add:UserName="******",DeliveryID=" + deliveryid.ToString() + ",Auto submit order!");
                int ret = SaleOut_Confirm(User, deliveryid, DeliveryInfo.WipeAmount, DeliveryInfo.PayInfos, out ErrorInfo);
                if (ret < 0)
                {
                    LogWriter.WriteLog("PBMIFService.SaleOut_Add:UserName="******",DeliveryID=" + deliveryid.ToString() +
                        ",Auto confirm failed!ErrorInfo=" + ErrorInfo);
                    return ret;
                }
            }
            #endregion

            return deliveryid;
        }
Пример #5
0
            public DeliveryDetail(PBM_DeliveryDetail m)
            {
                DetailID = m.ID;
                Product = m.Product;
                LotNumber = m.LotNumber;
                Price = m.Price;
                DeliveryQuantity = m.DeliveryQuantity;
                SignInQuantity = m.SignInQuantity;
                SalesMode = m.SalesMode;
                DiscountRate = m.DiscountRate;
                Remark = m.Remark;

                PDT_Product p = new PDT_ProductBLL(Product).Model;
                if (p == null) return;

                ProductName = p.ShortName;

                if (p.ConvertFactor == 0) p.ConvertFactor = 1;
                ConvertFactor = p.ConvertFactor;

                if (p.Category > 0)
                {
                    CategoryName = PDT_CategoryBLL.GetFullCategoryName(p.Category);
                }

                #region 获取字典表名称
                try
                {
                    if (m.SalesMode > 0)
                    {
                        Dictionary_Data dic = DictionaryBLL.GetDicCollections("PBM_SalseMode")[m.SalesMode.ToString()];
                        if (dic != null) SalesModeName = dic.Name;
                    }
                    if (p.TrafficPackaging > 0)
                    {
                        PackingName_T = DictionaryBLL.GetDicCollections("PDT_Packaging")[p.TrafficPackaging.ToString()].ToString();
                    }
                    if (p.Packaging > 0)
                    {
                        PackingName_P = DictionaryBLL.GetDicCollections("PDT_Packaging")[p.Packaging.ToString()].ToString();
                    }
                }
                catch (System.Exception err)
                {
                    LogWriter.WriteLog("MCSFramework.WSI.DeliveryDetail", err);
                }
                #endregion
            }
Пример #6
0
        public void ImportSAPService()
        {
            while (bIsRunning)
            {
                try
                {
                    ImportExcelTDP.SAP_ZSD.Z_SD_SD_SHService sd = new ImportExcelTDP.SAP_ZSD.Z_SD_SD_SHService();

                    NetworkCredential c = new NetworkCredential();
                    c.UserName = ConfigHelper.GetConfigString("SAP_ZSD_User");
                    c.Password = ConfigHelper.GetConfigString("SAP_ZSD_Password");
                    sd.Credentials = c;

                    ImportExcelTDP.SAP_ZSD.Ztdplog[] results1 = new ImportExcelTDP.SAP_ZSD.Ztdplog[1024];
                    ImportExcelTDP.SAP_ZSD.Ztdplog[] results2 = new ImportExcelTDP.SAP_ZSD.Ztdplog[1024];
                    sd.ZSdSdSh("1", "5200", DateTime.Today.AddDays(-1).ToString("yyyyMMdd"), ref results1);
                    sd.ZSdSdSh("2", "5200", DateTime.Today.AddDays(-1).ToString("yyyyMMdd"), ref results1);
                    sd.ZSdSdSh("1", "5200", DateTime.Today.ToString("yyyyMMdd"), ref results1);
                    sd.ZSdSdSh("2", "5200", DateTime.Today.ToString("yyyyMMdd"), ref results2);

                    List<ImportExcelTDP.SAP_ZSD.Ztdplog> results = new List<ImportExcelTDP.SAP_ZSD.Ztdplog>();
                    results.AddRange(results1);
                    results.AddRange(results2);
                    SendMessage("ImportSAPService", "获取到" + results.Count().ToString() + "条发货数量!");

                    if (results.Count() == 0) { Thread.Sleep(3600 * 1000); continue; }

                    PBM_DeliveryBLL bll = new PBM_DeliveryBLL();
                    decimal totalamount = 0;
                    foreach (ImportExcelTDP.SAP_ZSD.Ztdplog item in results.OrderBy(p => p.Vgbel))
                    {
                        if (item.Vgbel != bll.Model.SheetCode)
                        {
                            #region 更新发货单
                            if (bll.Model.SheetCode != "" && bll.Items.Count > 0)
                            {
                                //计算折扣金额
                                bll.Model.DiscountAmount = bll.Items.Sum(p => (1 - p.DiscountRate) *
                                    Math.Round(p.Price * p.ConvertFactor, 2) * p.DeliveryQuantity / p.ConvertFactor);

                                //明细合计金额
                                decimal actamount = Math.Round(bll.Items.Sum(p => p.DiscountRate * Math.Round(p.Price * p.ConvertFactor, 2) * p.SignInQuantity / p.ConvertFactor), 2);

                                //优惠金额(明细合计金额与实际采购金额的差额)
                                bll.Model.WipeAmount = actamount - totalamount;

                                //计算实际销售金额
                                bll.Model.ActAmount = (bll.Model.Classify == 12 ? -1 : 1) * (actamount - bll.Model.WipeAmount);

                                int ret = bll.Add();
                                if (ret > 0)
                                    SendMessage("ImportSAPService", "单据号" + item.Vgbel + ",正在提交,总金额" + bll.Model.ActAmount.ToString("0.##") + "!");
                                else
                                    SendMessage("ImportSAPService", "单据号" + item.Vgbel + ",提交失败!Ret=" + ret.ToString());

                                bll = null;
                                totalamount = 0;
                            }
                            #endregion

                            #region 新增采购单据

                            //判断单号有没有导入过
                            if (PBM_DeliveryBLL.GetModelList("SheetCode='" + item.Vgbel + "' AND Supplier=1000 AND Classify IN (11,12)").Count > 0)
                            {
                                SendMessage("ImportSAPService", "单据号" + item.Vgbel + ",之前已存在!");
                                continue;
                            }

                            bll = new PBM_DeliveryBLL();
                            bll.Model.Classify = 11;

                            #region 判断TDP编码Kunage
                            string tdpcode = item.Kunag.Substring(3);
                            IList<CM_ClientManufactInfo> _clients = CM_ClientManufactInfoBLL.GetModelList("Code='" + tdpcode + "' AND Manufacturer=1000 AND State = 1");
                            if (_clients.Count == 0)
                            {
                                SendMessage("ImportSAPService", "门店编码无效,单据号" + item.Vgbel + ",客户编号:" + tdpcode);
                                continue;
                            }
                            #endregion

                            bll.Model.SheetCode = item.Vgbel;
                            bll.Model.Supplier = 1000;
                            bll.Model.Client = _clients[0].Client;

                            #region 获取经销商主仓库
                            IList<CM_WareHouse> _warehouses = CM_WareHouseBLL.GetEnbaledByClient(bll.Model.Client).Where(p => p.Classify == 1).OrderBy(p => p.ID).ToList();
                            if (_warehouses.Count == 0)
                            {
                                SendMessage("ImportSAPService", "未找到客户主仓库,单据号" + item.Vgbel + ",客户编号:" + tdpcode);
                                continue;
                            }
                            #endregion

                            bll.Model.ClientWareHouse = _warehouses[0].ID;
                            bll.Model.PrepareMode = 1;
                            bll.Model.State = 1;        //默认制单状态
                            bll.Model.WipeAmount = 0;
                            bll.Model.Remark = "SAP导入";
                            bll.Model.ApproveFlag = 2;
                            bll.Model.InsertStaff = 1;
                            DateTime _prearrivaldate = DateTime.Today;
                            DateTime.TryParse(item.WadatIst, out _prearrivaldate);
                            bll.Model.PreArrivalDate = _prearrivaldate;
                            #endregion
                        }

                        #region 新增商品明细
                        string productcode = item.Matnr.Substring(item.Matnr.Length - 6);
                        IList<PDT_Product> _products = PDT_ProductBLL.GetModelList("FactoryCode='" + productcode + "' AND State=1");
                        if (_products.Count == 0)
                        {
                            SendMessage("ImportSAPService", "商品编码无效,单据号" + item.Vgbel + ",商品编号:" + productcode);
                            continue;
                        }
                        int productid = _products[0].ID;
                        PDT_ProductBLL productbll = new PDT_ProductBLL(productid);

                        PBM_DeliveryDetail d = new PBM_DeliveryDetail();
                        d.Product = productid;
                        d.ConvertFactor = _products[0].ConvertFactor == 0 ? 1 : _products[0].ConvertFactor;
                        d.SalesMode = 1;
                        d.DeliveryQuantity = (int)Math.Round(item.Fkimg * d.ConvertFactor, 0);

                        if (d.DeliveryQuantity < 0)
                        {
                            bll.Model.Classify = 12;        //采购退库单
                            d.DeliveryQuantity = 0 - d.DeliveryQuantity;
                        }

                        d.SignInQuantity = d.DeliveryQuantity;
                        d.CostPrice = Math.Round(item.Kzwi / d.DeliveryQuantity, 4);
                        d.Price = d.CostPrice;
                        d.DiscountRate = Math.Round(item.Kzwi5 / item.Kzwi, 4);
                        d.LotNumber = item.Charg;
                        totalamount += item.Kzwi5;

                        DateTime _productdate = new DateTime(1900, 1, 1);
                        DateTime.TryParse(item.Hsdat, out _productdate);

                        if (_productdate.Year < 1900) _productdate = new DateTime(1900, 1, 1);
                        d.ProductDate = _productdate;
                        d.Remark = item.Fkart;

                        bll.Items.Add(d);
                        #endregion

                        #region 判断是否在默认价表中
                        PDT_ProductExtInfo extinfo = productbll.GetProductExtInfo(bll.Model.Client);
                        if (extinfo == null)
                        {
                            extinfo = new PDT_ProductExtInfo();
                            extinfo.Supplier = bll.Model.Client;
                            extinfo.Product = productid;
                            extinfo.Category = productbll.Model.Category;
                            extinfo.Code = productbll.Model.FactoryCode;
                            extinfo.SalesState = 1;
                            extinfo.BuyPrice = d.Price;
                            extinfo.SalesPrice = d.Price;
                            new PDT_ProductBLL(productid).SetProductExtInfo(extinfo);
                        }
                        #endregion
                    }

                    #region 更新发货单
                    if (bll.Model.SheetCode != "" && bll.Items.Count > 0)
                    {
                        //计算折扣金额
                        bll.Model.DiscountAmount = bll.Items.Sum(p => (1 - p.DiscountRate) *
                            Math.Round(p.Price * p.ConvertFactor, 2) * p.DeliveryQuantity / p.ConvertFactor);

                        //计算实际销售金额
                        bll.Model.ActAmount = Math.Round((bll.Model.Classify == 12 ? -1 : 1) *
            bll.Items.Sum(p => p.DiscountRate * Math.Round(p.Price * p.ConvertFactor, 2) * p.SignInQuantity / p.ConvertFactor)
            - bll.Model.WipeAmount, 2);

                        int ret = bll.Add();
                        if (ret > 0)
                            SendMessage("ImportSAPService", "单据号" + bll.Model.SheetCode + ",提交成功,总金额" + bll.Model.ActAmount.ToString("0.##") + "!");
                        else
                            SendMessage("ImportSAPService", "单据号" + bll.Model.SheetCode + ",提交失败!Ret=" + ret.ToString());
                    }
                    #endregion

                    Thread.Sleep(3600 * 1000);
                }
                catch (ThreadAbortException e)
                {
                    this.SendMessage("ImportSAPService.ThreadAbortException", e.Message);
                    Thread.Sleep(10000);
                    continue;
                }
                catch (Exception ex)
                {
                    this.SendMessage("ImportSAPService.Exception", ex.Message);
                    Thread.Sleep(10000);
                    continue;
                }
            }
        }
    protected void bt_AddDetail_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ID"] == 0) return;
        PBM_DeliveryBLL _bll = new PBM_DeliveryBLL((int)ViewState["ID"]);

        if (ViewState["Details"] == null) return;
        ListTable<PBM_DeliveryDetail> Details = (ListTable<PBM_DeliveryDetail>)ViewState["Details"];

        int product = 0;
        int.TryParse(select_Product.SelectValue, out product);
        //int.TryParse(RadComboBox1.SelectedValue, out product);
        if (product != 0)
        {
            PDT_ProductBLL productbll = new PDT_ProductBLL(product, true);
            if (productbll.Model == null) return;
            if (productbll.Model.ConvertFactor == 0)
            {
                productbll.Model.ConvertFactor = 1;
                productbll.Update();
            }

            string lotnumber = "";
            int quantity = 0;
            decimal price = 0;
            DateTime producedate = new DateTime(1900, 1, 1);

            int.TryParse(tbx_Quantity.Text, out quantity);
            decimal.TryParse(tbx_Price.Text, out price);
            DateTime.TryParse(tbx_ProductDate.Text, out producedate);
            if (producedate.Year < 1900) producedate = new DateTime(1900, 1, 1);

            if (ddl_Unit.SelectedValue == "T")
            {
                //整件单位
                quantity = quantity * productbll.Model.ConvertFactor;
                price = price / productbll.Model.ConvertFactor;
            }

            if (quantity == 0)
            {
                //MessageBox.Show(this, "请选择采购数量!");
                return;
            }

            lotnumber = rbl_ln.SelectedValue == "N" ? tbx_LotNumber.Text : ddl_LotNumber.SelectedValue;

            if (lotnumber == "" && producedate.Year > 1900) lotnumber = producedate.ToString("yyyyMMdd");

            if ((int)ViewState["Classify"] == 12)
            {
                #region 判断库存数量是否够退货
                int inv_quantity = INV_InventoryBLL.GetProductQuantity((int)ViewState["WareHouse"], product, lotnumber);
                if (quantity > inv_quantity)
                {
                    MessageBox.Show(this, "库存不足,不可采购退货!");
                    return;
                }
                #endregion
            }
            PBM_DeliveryDetail d = null;

            if (ViewState["SelectedDetail"] != null)
            {
                d = (PBM_DeliveryDetail)ViewState["SelectedDetail"];
            }
            else
            {
                d = new PBM_DeliveryDetail();
                d.DeliveryID = (int)ViewState["ID"];

                if (Details.GetListItem().Count == 0)
                    d.ID = 1;
                else
                    d.ID = Details.GetListItem().Max(p => p.ID) + 1;
            }

            decimal discountrate = 100;
            decimal.TryParse(tbx_DiscountRate.Text, out discountrate);

            d.Product = product;
            d.LotNumber = lotnumber;
            d.ProductDate = producedate.Year < 1900 ? new DateTime(1900, 1, 1) : producedate;
            d.CostPrice = price;
            d.Price = d.CostPrice;
            d.DiscountRate = discountrate / 100;     //默认100为全价
            d.ConvertFactor = productbll.Model.ConvertFactor;
            d.DeliveryQuantity = quantity;
            d.SignInQuantity = d.DeliveryQuantity;
            d.LostQuantity = 0;
            d.BadQuantity = 0;
            d.SalesMode = int.Parse(ddl_SalesMode.SelectedValue);
            if (d.SalesMode == 2) { d.Price = 0; }      //赠送模式采购价自动设为0

            if (ViewState["SelectedDetail"] != null)
            {
                Details.Update(d);
                ViewState["SelectedDetail"] = null;
                bt_AddDetail.Text = "新 增";
                gv_List.SelectedIndex = -1;
            }
            else
            {
                Details.Add(d);
                //_bll.AddDetail(d);
            }
            tbx_LotNumber.Text = "";
            tbx_Quantity.Text = "0";

            BindGrid();
        }
    }