/// <summary>
        /// 月结对账算法
        /// </summary>
        /// <param name="accountList">月末未结算的各药品最后一笔台帐记录</param>
        protected DataTable CheckAccount(List <YP_Account> accountList)
        {
            DataTable wrongDt    = base.BuildWrongDataTable();
            Hashtable makerdicDt = new Hashtable();

            foreach (YP_Account act in accountList)
            {
                if (!makerdicDt.ContainsKey(act.MakerDicID))
                {
                    makerdicDt.Add(act.MakerDicID, act);
                }
                else if (act.MAccountID > ((YP_Account)(makerdicDt[act.MakerDicID])).MAccountID)
                {
                    makerdicDt[act.MakerDicID] = act;
                }
            }
            foreach (object makerDicId in makerdicDt)
            {
                YP_Dal dal = new YP_Dal();
                dal._oleDb = oleDb;
                YP_Account hashValue = (YP_Account)((DictionaryEntry)makerDicId).Value;
                _makerDicId = hashValue.MakerDicID;
                List <YP_Account> actList    = accountList.FindAll(match);
                decimal           balanceFee = 0;
                decimal           storeFee   = dal.YF_GetDrugFee(hashValue.DeptID, hashValue.MakerDicID);
                decimal           storeNum   = StoreFactory.GetQuery(ConfigManager.YF_SYSTEM).QueryNum(hashValue.MakerDicID,
                                                                                                       hashValue.DeptID);
                foreach (YP_Account act in actList)
                {
                    if (act.AccountType == 0)
                    {
                        balanceFee = act.BalanceFee;
                    }
                }
                foreach (YP_Account act in actList)
                {
                    balanceFee += act.LenderFee;
                    balanceFee -= act.DebitFee;
                }

                if (Convert.ToDecimal(Math.Abs(hashValue.BalanceFee - balanceFee)) > 2 ||
                    Convert.ToDecimal(Math.Abs(hashValue.BalanceFee - storeFee)) > 2 ||
                    Math.Abs(hashValue.OverNum - storeNum) > 0)
                {
                    string  drugName = DrugBaseDataBll.GetDurgName(hashValue.MakerDicID);
                    DataRow errorRow = wrongDt.NewRow();
                    errorRow["CHEMNAME"]      = drugName;
                    errorRow["MAKERDICID"]    = hashValue.MakerDicID;
                    errorRow["WRONGFEE"]      = hashValue.BalanceFee - balanceFee;
                    errorRow["STOREWRONGFEE"] = storeFee - hashValue.BalanceFee;
                    errorRow["WRONGNUM"]      = storeNum - hashValue.OverNum;
                    errorRow["BALANCEFEE"]    = hashValue.BalanceFee;
                    errorRow["BALANCENUM"]    = hashValue.OverNum;
                    errorRow["UNIT"]          = hashValue.LeastUnit;
                    errorRow["UNITNUM"]       = hashValue.UnitNum;
                    wrongDt.Rows.Add(errorRow);
                }
            }
            return(wrongDt);
        }
        /// <summary>
        /// 将多张药库盘点单明细汇总到一张盘点明细表中
        /// </summary>
        /// <param name="totalListOrder">汇总明细表</param>
        /// <param name="listOrder">药库盘点单明细列表</param>
        private void AddOrderListByMakerId(List <BillOrder> totalListOrder, List <YP_CheckOrder> listOrder)
        {
            StoreQuery _storeQuery = StoreFactory.GetQuery(ConfigManager.YK_SYSTEM);

            foreach (YP_CheckOrder orderCheck in listOrder)
            {
                bool isFind = false;
                foreach (YP_CheckOrder totalOrder in totalListOrder)
                {
                    if (totalOrder.MakerDicID == orderCheck.MakerDicID)
                    {
                        totalOrder.CheckNum += orderCheck.CheckNum;
                        totalOrder.FactNum  += orderCheck.FactNum;
                        isFind = true;
                        break;
                    }
                }
                if (isFind == false)
                {
                    totalListOrder.Add((YP_CheckOrder)(orderCheck.Clone()));
                }
            }
            foreach (YP_CheckOrder totalOrder in totalListOrder)
            {
                decimal differentNum = totalOrder.CheckNum - totalOrder.FactNum;
                totalOrder.FactNum     = _storeQuery.QueryNum(totalOrder.MakerDicID, totalOrder.DeptID);
                totalOrder.CheckNum    = totalOrder.FactNum + differentNum;
                totalOrder.CKRetailFee = totalOrder.CheckNum * totalOrder.RetailPrice;
                totalOrder.CKTradeFee  = totalOrder.CheckNum * totalOrder.TradePrice;
                totalOrder.FTRetailFee = totalOrder.FactNum * totalOrder.RetailPrice;
                totalOrder.FTTradeFee  = totalOrder.FactNum * totalOrder.TradePrice;
            }
        }
        /// <summary>
        /// 审核调价单
        /// </summary>
        /// <param name="billMaster">调价单头表</param>
        /// <param name="auditerID">审核人员ID</param>
        /// <param name="auditDeptID">审核科室ID</param>
        public override void AuditBill(BillMaster billMaster, long auditerID, long auditDeptID)
        {
            try
            {
                HIS.DAL.YP_Dal ypDal = new YP_Dal();
                ypDal._oleDb = oleDb;
                string       strWhere;
                YP_AdjMaster masterAdj    = (YP_AdjMaster)billMaster;
                string       belongSystem = (masterAdj.OpType == ConfigManager.OP_YF_ADJPRICE ? ConfigManager.YF_SYSTEM : ConfigManager.YK_SYSTEM);
                if (ConfigManager.IsChecking((long)masterAdj.DeptID))
                {
                    string deptName = BaseData.GetDeptName(masterAdj.DeptID.ToString());
                    throw new Exception("[" + deptName + "]" + "库房药品正在盘点中....");
                }
                masterAdj.Audit_Flag = 1;
                masterAdj.Over_Flag  = 1;
                BindEntity <HIS.Model.YP_AdjMaster> .CreateInstanceDAL(oleDb).Update(masterAdj);

                strWhere = Tables.yp_adjorder.MASTERIADJPRICED + oleDb.EuqalTo() + masterAdj.MasterAdjPriceID;
                List <YP_AdjOrder> orderList = BindEntity <HIS.Model.YP_AdjOrder> .CreateInstanceDAL(oleDb).GetListArray(strWhere);

                List <BillOrder> billOrderList = new List <BillOrder>();
                Hashtable        storeTable    = new Hashtable();
                foreach (YP_AdjOrder adjOrder in orderList)
                {
                    YP_StoreNum yp_StoreNum = new YP_StoreNum();
                    decimal     storeNum    = StoreFactory.GetQuery(belongSystem).QueryNum(adjOrder.MakerDicID, adjOrder.DeptID);
                    adjOrder.Audit_Flag    = 1;
                    adjOrder.AdjNum        = storeNum;
                    yp_StoreNum.makerDicId = adjOrder.MakerDicID;
                    if (belongSystem == ConfigManager.YK_SYSTEM)
                    {
                        yp_StoreNum.smallUnit = adjOrder.LeastUnit;
                    }
                    else
                    {
                        yp_StoreNum.smallUnit = ypDal.Unit_GetSmallUnit(adjOrder.MakerDicID);
                    }
                    yp_StoreNum.storeNum = storeNum;
                    storeTable.Add(yp_StoreNum.makerDicId, yp_StoreNum);
                    //计算调赢/亏金额
                    ComputeAdjFee(adjOrder, belongSystem);
                    billOrderList.Add(adjOrder);
                    BindEntity <HIS.Model.YP_AdjOrder> .CreateInstanceDAL(oleDb).Update(adjOrder);
                }
                //记账
                AccountFactory.GetWriter(masterAdj.OpType).WriteAccount(masterAdj, billOrderList, storeTable);
                //更新药典价格
                foreach (YP_AdjOrder adjOrder in orderList)
                {
                    ChangePrice(adjOrder.NewRetailPrice, true, adjOrder.MakerDicID);
                    ChangePrice(adjOrder.NewTradePrice, false, adjOrder.MakerDicID);
                }
            }
            catch (Exception error)
            {
                throw error;
            }
        }
Exemple #4
0
 /// <summary>
 /// 加载采购计划单据明细
 /// </summary>
 /// <param name="billMaster">单据头表</param>
 /// <returns></returns>
 public override DataTable LoadOrder(BillMaster billMaster)
 {
     if (billMaster != null)
     {
         YP_PlanMaster master = (YP_PlanMaster)billMaster;
         if (master.PlanMasterId != -1)
         {
             string strWhere = HIS.BLL.Tables.yk_planmaster.PLANMASTERID + oleDb.EuqalTo()
                               + master.PlanMasterId;
             YP_Dal dal = new YP_Dal();
             dal._oleDb = oleDb;
             return(dal.YK_PlanOrder_GetList(strWhere));
         }
         else
         {
             string strWhere = HIS.BLL.Tables.yk_planmaster.PLANMASTERID + oleDb.EuqalTo()
                               + master.PlanMasterId;
             YP_Dal dal = new YP_Dal();
             dal._oleDb = oleDb;
             DataTable  orderDt        = dal.YK_PlanOrder_GetList(strWhere);
             StoreQuery storeQuery     = StoreFactory.GetQuery(ConfigManager.YK_SYSTEM);
             DataTable  lowerStoreDrug = storeQuery.GetDrugForStoreLimit("", false,
                                                                         (int)master.DeptId, 0);
             for (int index = 0; index < lowerStoreDrug.Rows.Count; index++)
             {
                 DataRow limitRow = lowerStoreDrug.Rows[index];
                 decimal stockNum = Convert.ToDecimal(limitRow["UPPERLIMIT"]) -
                                    Convert.ToDecimal(limitRow["CURRENTNUM"]);
                 if (stockNum > 0)
                 {
                     DataRow newRow = orderDt.NewRow();
                     newRow["MAKERDICID"]   = limitRow["MAKERDICID"];
                     newRow["CHEMNAME"]     = limitRow["CHEMNAME"];
                     newRow["SPEC"]         = limitRow["SPEC"];
                     newRow["PRODUCTNAME"]  = limitRow["PRODUCTNAME"];
                     newRow["PLANMASTERID"] = 0;
                     newRow["PLANORDERID"]  = 0;
                     newRow["RETAILPRICE"]  = limitRow["RETAILPRICE"];
                     newRow["TRADEPRICE"]   = limitRow["TRADEPRICE"];
                     newRow["UNIT"]         = limitRow["LEASTUNIT"];
                     newRow["UNITNAME"]     = limitRow["PACKUNITNAME"];
                     newRow["STOCKNUM"]     = stockNum;
                     newRow["RETAILFEE"]    = stockNum * Convert.ToDecimal(newRow["RETAILPRICE"]);
                     newRow["TRADEFEE"]     = stockNum * Convert.ToDecimal(newRow["TRADEPRICE"]);
                     orderDt.Rows.Add(newRow);
                 }
             }
             return(orderDt);
         }
     }
     else
     {
         return(null);
     }
 }
        /// <summary>
        /// 保存调价单
        /// </summary>
        /// <param name="billMaster">调价单表头</param>
        /// <param name="listOrder">调价单明细列表</param>
        /// <param name="deptId">药剂科室ID</param>
        public override void SaveBill(BillMaster billMaster, List <BillOrder> listOrder, long deptId)
        {
            try
            {
                int          count     = 0;
                YP_AdjMaster masterAdj = (YP_AdjMaster)billMaster;
                oleDb.BeginTransaction();
                //声明操作对象
                YP_AdjOrder    adjOrder = new YP_AdjOrder();
                HIS.DAL.YP_Dal ypDal    = new YP_Dal();
                ypDal._oleDb = oleDb;
                List <YP_DeptDic> deptList = new List <YP_DeptDic>();
                //获取调价药剂科室
                deptList = BindEntity <HIS.Model.YP_DeptDic> .CreateInstanceDAL(oleDb).GetListArray("");

                foreach (YP_DeptDic dept in deptList)
                {
                    count = 0;
                    if (dept.DeptType1 == "药房")
                    {
                        if (dept.Use_Flag == 1)
                        {
                            masterAdj.OpType = ConfigManager.OP_YF_ADJPRICE;
                        }
                    }
                    else
                    {
                        if (dept.Use_Flag == 1)
                        {
                            masterAdj.OpType = ConfigManager.OP_YK_ADJPRICE;
                        }
                    }
                    masterAdj.Over_Flag = 0;
                    masterAdj.AdjCode   = GWIString.FilterSpecial(masterAdj.AdjCode);
                    masterAdj.Remark    = GWIString.FilterSpecial(masterAdj.Remark);
                    masterAdj.DeptID    = dept.DeptID;
                    BindEntity <HIS.Model.YP_AdjMaster> .CreateInstanceDAL(oleDb).Add(masterAdj);

                    foreach (BillOrder order in listOrder)
                    {
                        adjOrder = (YP_AdjOrder)order;
                        adjOrder.MasterIAdjPriceD = masterAdj.MasterAdjPriceID;
                        adjOrder.DeptID           = masterAdj.DeptID;
                        string belongSystem = (dept.DeptType1 == "药房" ? ConfigManager.YF_SYSTEM : ConfigManager.YK_SYSTEM);
                        if (StoreFactory.GetQuery(belongSystem).QueryNum(adjOrder.MakerDicID, adjOrder.DeptID) < 0)
                        {
                            continue;
                        }
                        count++;
                        BindEntity <HIS.Model.YP_AdjOrder> .CreateInstanceDAL(oleDb).Add(adjOrder);
                    }
                    if (count > 0)
                    {
                        masterAdj.BillNum = (ypDal.YP_Bill_GetBillNum(masterAdj.OpType, dept.DeptID)).BillNum;
                        if (masterAdj.BillNum == 0)
                        {
                            throw new Exception("药剂科室设置错误,请确认当前科室是药剂科室");
                        }
                        BindEntity <HIS.Model.YP_AdjMaster> .CreateInstanceDAL(oleDb).Update(masterAdj);

                        AuditBill(masterAdj, (long)(masterAdj.RegPeople), (long)(masterAdj.DeptID));
                    }
                    else
                    {
                        string strwhere = Tables.yp_adjmaster.DEPTID + oleDb.EuqalTo() + masterAdj.DeptID
                                          + oleDb.And() + Tables.yp_adjmaster.MASTERADJPRICEID + oleDb.EuqalTo() + masterAdj.MasterAdjPriceID;
                        BindEntity <HIS.Model.YP_AdjMaster> .CreateInstanceDAL(oleDb).Delete(strwhere);

                        strwhere = Tables.yp_adjorder.DEPTID + oleDb.EuqalTo() + masterAdj.DeptID
                                   + oleDb.And() + Tables.yp_adjorder.MASTERIADJPRICED + oleDb.EuqalTo() + masterAdj.MasterAdjPriceID;
                        BindEntity <YP_AdjOrder> .CreateInstanceDAL(oleDb).Delete(strwhere);
                    }
                }
                oleDb.CommitTransaction();
            }
            catch (Exception error)
            {
                oleDb.RollbackTransaction();
                throw error;
            }
        }
 /// <summary>
 /// 按药房库存上下限构建入库申请单
 /// </summary>
 /// <param name="newMaster">申请单表头</param>
 /// <param name="orderDt">申请单明细列表</param>
 /// <param name="_currentDeptId">申请科室</param>
 /// <param name="_currentUserId">申请人员ID</param>
 /// <param name="applyDept">出库科室</param>
 /// <param name="lowerStoreDrug">低于库存下限药品</param>
 public void BuildApplyInByStoreLimit(out YP_InMaster newMaster, out DataTable orderDt, int _currentDeptId,
                                      int _currentUserId, int applyDept, DataTable lowerStoreDrug)
 {
     try
     {
         if (lowerStoreDrug == null)
         {
             newMaster = null;
             orderDt   = null;
             return;
         }
         decimal    drugStoreNum;      //药品库存数量
         decimal    lessNum;           //还需申请数量
         decimal    batchStoreNum;     //批次库存量
         decimal    applyNum;          //总共需申请数量
         int        currentMakerDicId; //当前药品编码
         StoreQuery _storeQuery          = StoreFactory.GetQuery(ConfigManager.YF_SYSTEM);
         BillQuery  _billQuery           = BillFactory.GetQuery(ConfigManager.OP_YF_APPLYIN);
         StoreQuery _applyDeptStoreQuery = StoreFactory.GetQuery(ConfigManager.YK_SYSTEM);
         //构建一个新的入库申请单表头
         newMaster = (YP_InMaster)(this.BuildNewMaster(_currentDeptId, _currentUserId));
         orderDt   = _billQuery.LoadOrder(newMaster);
         #region 循环低于下限的药品
         for (int index = 0; index < lowerStoreDrug.Rows.Count; index++)
         {
             DataRow limitRow = lowerStoreDrug.Rows[index];
             //获取当前药品编码
             currentMakerDicId = Convert.ToInt32(limitRow["MAKERDICID"]);
             //计算需要申请入库药品数量
             applyNum = (Convert.ToDecimal(limitRow["UPPERLIMIT"]) -
                         Convert.ToDecimal(limitRow["CURRENTNUM"])) / Convert.ToDecimal(limitRow["PUNITNUM"]);
             applyNum = Math.Ceiling(applyNum);
             //按药品编码加载批次
             DataTable batchDt = _applyDeptStoreQuery.LoadBatch(currentMakerDicId, applyDept);
             if (batchDt == null)
             {
                 break;
             }
             drugStoreNum = 0;
             //判断批次总量是否满足申请要求
             for (int temp = 0; temp < batchDt.Rows.Count; temp++)
             {
                 drugStoreNum += Convert.ToDecimal(batchDt.Rows[temp]["CURRENTNUM"]);
             }
             if (drugStoreNum > applyNum)
             {
                 #region 如果满足,循环批次生成入库单明细
                 for (int temp = 0; temp < batchDt.Rows.Count; temp++)
                 {
                     lessNum       = applyNum;
                     batchStoreNum = Convert.ToDecimal(batchDt.Rows[temp]["CURRENTNUM"]);
                     if (batchStoreNum != 0)
                     {
                         DataRow newRow = orderDt.NewRow();
                         newRow["MAKERDICID"]        = limitRow["MAKERDICID"];         //厂家ID
                         newRow["CHEMNAME"]          = limitRow["CHEMNAME"];           //化学名
                         newRow["SPEC"]              = limitRow["SPEC"];               //规格
                         newRow["PRODUCTNAME"]       = limitRow["PRODUCTNAME"];        //生产厂家名
                         newRow["RETAILPRICE"]       = limitRow["RETAILPRICE"];        //零售价
                         newRow["TRADEPRICE"]        = limitRow["TRADEPRICE"];         //批发价
                         newRow["STOCKPRICE"]        = limitRow["TRADEPRICE"];         //进价
                         newRow["LEASTUNIT"]         = limitRow["PACKUNIT"];           //包装单位编码
                         newRow["UNITNAME"]          = limitRow["PACKUNITNAME"];       //包装单位
                         newRow["BATCHNUM"]          = batchDt.Rows[temp]["BATCHNUM"]; //批次号
                         newRow["DEPTID"]            = limitRow["DEPTID"];             //部门ID
                         newRow["UNITNUM"]           = limitRow["PUNITNUM"];           //单位数量
                         newRow["VALIDITYDATE"]      = Convert.ToDateTime(batchDt.Rows[temp]["VALIDITYDATE"]);
                         newRow["AUDIT_FLAG"]        = 0;
                         newRow["BILLNUM"]           = 0;
                         newRow["DELIVERNUM"]        = "";
                         newRow["InStorageID"]       = 0;
                         newRow["MasterInStorageID"] = 0;
                         newRow["REMARK"]            = "";
                         newRow["DEFSTOCKPRICE"]     = limitRow["TRADEPRICE"];
                         newRow["RecScale"]          = 0.0;
                         //批次数量大于还需申请数量
                         if (batchStoreNum >= lessNum)
                         {
                             if (lessNum < 1)
                             {
                                 break;
                             }
                             else
                             {
                                 //lessNum = Math.Ceiling(lessNum);
                                 newRow["INNUM"]     = lessNum;                                            //入库数量
                                 newRow["TRADEFEE"]  = lessNum * Convert.ToDecimal(newRow["TRADEPRICE"]);  //批发金额
                                 newRow["RETAILFEE"] = lessNum * Convert.ToDecimal(newRow["RETAILPRICE"]); //零售金额
                                 newRow["STOCKFEE"]  = newRow["TRADEFEE"];                                 //进货金额
                                 orderDt.Rows.Add(newRow);
                                 break;
                             }
                         }
                         //批次数量小于还需申请数量
                         else
                         {
                             newRow["INNUM"]     = batchStoreNum;
                             newRow["TRADEFEE"]  = lessNum * Convert.ToDecimal(newRow["TRADEPRICE"]);  //批发金额
                             newRow["RETAILFEE"] = lessNum * Convert.ToDecimal(newRow["RETAILPRICE"]); //零售金额
                             newRow["STOCKFEE"]  = newRow["TRADEFEE"];
                             lessNum             = applyNum - batchStoreNum;                           //进货金额
                             orderDt.Rows.Add(newRow);
                         }
                     }
                 }
                 #endregion 如果满足,循环批次生成入库单明细
             }
         }
         #endregion 循环低于下限的药品
     }
     catch (Exception error)
     {
         throw error;
     }
 }