1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657 |
- package service
-
- import (
- "XT_New/models"
- "XT_New/utils"
- "errors"
- "fmt"
- "github.com/jinzhu/gorm"
- "math"
- "strconv"
- "strings"
- "time"
- )
-
- func GetPharmacyDrugList(storehouse_id int64, orgid int64, keywords string) (drug []*models.PharmacyBaseDrug, err error) {
-
- db := XTReadDB().Model(&drug).Where("org_id=? and status=1 and drug_status<>'停用 and is_pharmacy = 1' ", orgid)
- if len(keywords) > 0 {
- likekey := "%" + keywords + "%"
- err = db.Where("(drug_name LIKE ?)", likekey).Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
- return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
- }).Find(&drug).Error
- } else {
- err = db.Preload("DrugWarehouseInfo", func(db *gorm.DB) *gorm.DB {
- return XTReadDB().Where("org_id=? and status = 1 and warehousing_count <> 0 and storehouse_id = ?", orgid, storehouse_id).Group("id")
- }).Find(&drug).Error
- }
- return
- }
-
- func GetBloodAdviceList(advice_date int64, drug_id int64, org_id int64) (advice []*models.PharmacyDoctorAdvice, err error) {
-
- err = XTReadDB().Where("advice_date = ? and drug_id = ? and user_org_id = ? and status = 1 and advice_type =2 and is_medicine= 0", advice_date, drug_id, org_id).Find(&advice).Error
- return advice, err
- }
-
- func GetHisAdviceList(advice_date int64, drug_id int64, org_id int64) (advice []*models.PharmacyHisDoctorAdviceInfo, err error) {
-
- err = XTReadDB().Where("advice_date = ? and drug_id =? and user_org_id =? and status = 1 and is_medicine = 0", advice_date, drug_id, org_id).Order("prescription_id asc").Find(&advice).Error
- return advice, err
- }
-
- func UpdatePharmacyAdviceBaseDrug(ids []string, orgid int64) (models.PharmacyDoctorAdvice, error) {
-
- drug := models.PharmacyDoctorAdvice{}
-
- err := XTWriteDB().Where("id in(?) and user_org_id = ? and status = 1", ids, orgid).Update(map[string]interface{}{"is_medicine": 1}).Error
-
- return drug, err
- }
-
- func UpdatePharmacyHisAdviceBaseDrug(ids []string, orgid int64) (models.PharmacyHisDoctorAdviceInfo, error) {
-
- info := models.PharmacyHisDoctorAdviceInfo{}
- err := XTWriteDB().Where("id in(?) and user_org_id = ? and status = 1", ids, orgid).Update(map[string]interface{}{"is_medicine": 1}).Error
- return info, err
- }
-
- func GetPharmacyAdviceList(ids []string, orgid int64) (advice []*models.PharmacyDoctorAdvice, err error) {
-
- err = XTReadDB().Where("id in(?) and user_org_id = ?", ids, orgid).Find(&advice).Error
- return advice, err
- }
-
- func GetPharmacyHisAdviceList(ids []string, orgid int64) (advice []*models.PharmacyHisDoctorAdviceInfo, err error) {
-
- err = XTReadDB().Where("id in(?) and user_org_id = ?", ids, orgid).Find(&advice).Error
- return advice, err
- }
-
- func FindPharmacyConfig(orgid int64) (models.PharmacyConfig, error) {
-
- config := models.PharmacyConfig{}
- err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
- return config, err
- }
-
- // 药品出库
-
- func PharmacyDrugsDelivery(orgID int64, creater int64, advice *models.PharmacyDoctorAdvice, id int64) (err error) {
- // 1.判断药品是否来自专用字典的药品库
- // 2.判断当天当前机构有没有创建出库单,没有则创建
- // 3.创建出库流程
- // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
- isHasWay := false
- record_time := int64(0)
- if advice.Way == 1 {
- isHasWay = true
- record_time = advice.RecordDate
- }
-
- if isHasWay {
- //判断当天当前机构有没有创建出库单,没有则创建
- out, err := FindDrugStockOutByIsSys(orgID, 15, record_time)
- if err == gorm.ErrRecordNotFound {
- timeStr := time.Now().Format("2006-01-02")
- timeArr := strings.Split(timeStr, "-")
- total, _ := FindAllDrugWarehouseOut(orgID)
- total = total + 1
- warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
- number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
- number = number + total
- warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
- storeConfig, _ := GetAllStoreHouseConfig(orgID)
- warehouseOut := models.DrugWarehouseOut{
- WarehouseOutOrderNumber: warehousing_out_order,
- OperationTime: time.Now().Unix(),
- OrgId: orgID,
- Creater: creater,
- Ctime: time.Now().Unix(),
- Status: 1,
- WarehouseOutTime: record_time,
- Dealer: 0,
- Manufacturer: 0,
- Type: 1,
- IsSys: 15,
- StorehouseId: storeConfig.DrugStorehouseOut,
- }
- err := AddSigleDrugWarehouseOut(&warehouseOut)
- if err != nil {
- utils.TraceLog("创建出库单失败 err = %v", err)
- return err
- } else {
- out = warehouseOut
- }
- }
-
- // 出库流程
- // 1.查询该药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
- drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
- if drup.ID > 0 {
- prescribingNumber := advice.PrescribingNumber
-
- PharmacyDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice, id)
-
- } else {
- return errors.New("药品信息不存在")
- }
-
- }
-
- return
- }
-
- // 药品自动出库 递归方式
- func PharmacyDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.PharmacyDoctorAdvice, id int64) (err error) {
-
- // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
- var deliver_number int64 = 0
- var stock_number int64 = 0
-
- prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
- count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
-
- var drug_price float64
- if advice.PrescribingNumberUnit == drup.MaxUnit {
- deliver_number = count * drup.MinNumber
-
- } else {
- deliver_number = count
-
- }
-
- if advice.PrescribingNumberUnit == drup.MaxUnit {
- drug_price = drup.RetailPrice
- }
- fmt.Println(drug_price)
-
- if advice.PrescribingNumberUnit == drup.MinUnit {
- drug_price = drup.MinPrice
- }
-
- if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
- drug_price = drup.RetailPrice
- }
-
- // 根据先进先出原则,查询最先入库的批次,进行出库
- // 如果没有对应的库存,则报错
- storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
- warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
-
- if err != nil {
-
- return err
- }
-
- // 将该批次的剩余库存数量转换为拆零数量
-
- stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
-
- // 当库存数量大于或等于出库数量的话,则正常出库该批次
- if stock_number >= deliver_number {
-
- warehouseOutInfo := &models.DrugWarehouseOutInfo{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- Status: 1,
- Ctime: time.Now().Unix(),
- Remark: warehouse.Remark,
- OrgId: orgID,
- Type: 1,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- IsSys: 15,
- SysRecordTime: advice.RecordDate,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- BatchNumber: warehouse.BatchNumber,
- Price: warehouse.RetailPrice,
- CountUnit: drup.MinUnit,
- RetailPrice: warehouse.RetailPrice,
- ProductDate: warehouse.ProductDate,
- ExpiryDate: warehouse.ExpiryDate,
- PatientId: advice.PatientId,
- Count: deliver_number, //出最小单位
- WarehouseInfoId: warehouse.ID,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- PharmacyId: id,
- }
-
- drugflow := models.DrugFlow{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- ProductDate: warehouse.ProductDate,
- ExpireDate: warehouse.ExpiryDate,
- Count: deliver_number, //按最小单位计算,
- Price: warehouse.RetailPrice,
- Status: 1,
- Ctime: time.Now().Unix(),
- UserOrgId: orgID,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- BatchNumber: warehouse.BatchNumber,
- MaxUnit: drup.MinUnit,
- ConsumableType: 3,
- IsEdit: 1,
- Creator: advice.ExecutionStaff,
- IsSys: 15,
- PatientId: advice.PatientId,
- SystemTime: advice.AdviceDate,
- WarehousingDetailId: warehouse.ID,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- PharmacyId: id,
- }
-
- CreateDrugFlowOne(drugflow)
-
- errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
- fmt.Println(errOne)
- //查询是否存在数据
-
- details := &models.DrugAutomaticReduceDetail{
- WarehouseOutId: warehouseout.ID,
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- PatientId: advice.PatientId,
- Ctime: time.Now().Unix(),
- Mtime: time.Now().Unix(),
- Status: 1,
- RecordTime: advice.RecordDate,
- OrgId: orgID,
- DrugId: advice.DrugId,
- Count: count,
- CountUnit: advice.PrescribingNumberUnit,
- WarehouseInfoId: warehouse.ID,
- StorehouseId: storeConfig.DrugStorehouseOut,
- }
- errTwo := AddSigleDrugAutoReduceRecordInfo(details)
- if errTwo != nil {
- return errTwo
- }
-
- var maxNumber int64 = 0
- var minNumber int64 = 0
- var stock_max_number int64 = 0
- stock_max_number = warehouse.StockMaxNumber
-
- maxNumber = deliver_number / drup.MinNumber
- minNumber = deliver_number % drup.MinNumber
-
- if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
- minNumber = maxNumber
- }
-
- if drup.MaxUnit != drup.MinUnit {
- if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
- return errors.New("库存数量不足")
- }
- }
-
- warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
-
- if warehouse.StockMaxNumber < 0 {
- warehouse.StockMaxNumber = 0
- }
- if warehouse.StockMinNumber < 0 {
- warehouse.StockMinNumber = 0
- }
-
- warehouse.Mtime = time.Now().Unix()
-
- if warehouse.StockMinNumber < minNumber {
-
- warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
- warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
- } else {
- if minNumber > 0 {
- warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
- }
-
- if minNumber == 0 && maxNumber != 1 {
- warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
- }
-
- }
-
- if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
- warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
- if warehouse.StockMinNumber == 0 {
- warehouse.StockMaxNumber = stock_max_number
- }
- }
-
- if drup.MaxUnit != drup.MinUnit {
- if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
- return errors.New("库存数量不足")
- }
- }
- if warehouse.StockMinNumber <= 0 {
- warehouse.StockMinNumber = 0
- }
-
- errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
- if errThree != nil {
- return errThree
- }
-
- return nil
- } else {
- // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
- warehouseOutInfo := &models.DrugWarehouseOutInfo{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- Status: 1,
- Ctime: time.Now().Unix(),
- Remark: warehouse.Remark,
- OrgId: orgID,
- Type: 1,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- IsSys: 15,
- SysRecordTime: advice.RecordDate,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- BatchNumber: warehouse.BatchNumber,
- Price: warehouse.RetailPrice,
- CountUnit: drup.MinUnit,
- ProductDate: warehouse.ProductDate,
- ExpiryDate: warehouse.ExpiryDate,
- PatientId: advice.PatientId,
- Count: deliver_number,
- WarehouseInfoId: warehouse.ID,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- PharmacyId: id,
- }
- errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
- if errOne != nil {
- return errOne
- }
-
- details := &models.DrugAutomaticReduceDetail{
- WarehouseOutId: warehouseout.ID,
- WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
- PatientId: advice.PatientId,
- Ctime: time.Now().Unix(),
- Mtime: time.Now().Unix(),
- Status: 1,
- RecordTime: advice.RecordDate,
- OrgId: orgID,
- DrugId: advice.DrugId,
- Count: deliver_number,
- CountUnit: drup.MinUnit,
- WarehouseInfoId: warehouse.ID,
- StorehouseId: storeConfig.DrugStorehouseOut,
- }
- errTwo := AddSigleDrugAutoReduceRecordInfo(details)
- if errTwo != nil {
- return errTwo
- }
-
- info := models.XtDrugWarehouseInfo{
- ID: warehouse.ID,
- WarehousingId: warehouse.WarehousingId,
- DrugId: warehouse.DrugId,
- Number: warehouse.Number,
- ProductDate: warehouse.ProductDate,
- ExpiryDate: warehouse.ExpiryDate,
- WarehousingCount: warehouse.WarehousingCount,
- Price: warehouse.RetailPrice,
- TotalPrice: warehouse.TotalPrice,
- Dealer: warehouse.Dealer,
- Manufacturer: warehouse.Manufacturer,
- Remark: warehouse.Remark,
- Ctime: warehouse.Ctime,
- Mtime: warehouse.Mtime,
- Status: 1,
- OrgId: warehouse.OrgId,
- IsReturn: warehouse.IsReturn,
- WarehousingOrder: warehouse.WarehousingOrder,
- Type: warehouse.Type,
- RetailPrice: warehouse.RetailPrice,
- RetailTotalPrice: warehouse.RetailPrice,
- StockMaxNumber: 0,
- StockMinNumber: 0,
- BatchNumber: warehouse.BatchNumber,
- MaxUnit: warehouse.MaxUnit,
- WarehousingInfoId: warehouse.WarehousingInfoId,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- }
- //扣减库存
- errThree := UpDateDrugWarehouseInfoByStock(&info)
-
- if errThree != nil {
- return errThree
- }
-
- drugflow := models.DrugFlow{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- ProductDate: warehouse.ProductDate,
- ExpireDate: warehouse.ExpiryDate,
- Count: stock_number,
- Price: warehouse.RetailPrice,
- Status: 1,
- Ctime: time.Now().Unix(),
- UserOrgId: orgID,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- BatchNumber: warehouse.BatchNumber,
- MaxUnit: drup.MinUnit,
- ConsumableType: 3,
- IsEdit: 1,
- Creator: advice.ExecutionStaff,
- IsSys: 15,
- PatientId: advice.PatientId,
- WarehousingDetailId: warehouse.ID,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- PharmacyId: id,
- }
-
- CreateDrugFlowOne(drugflow)
-
- // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
- prescribingNumber_two_temp := deliver_number - stock_number
-
- overPlusNumber := float64(prescribingNumber_two_temp)
-
- advice.PrescribingNumber = float64(prescribingNumber_two_temp)
- advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
-
- NewPharmacyDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, id)
- }
-
- return
- }
-
- // 药品出库 递归方式
- func NewPharmacyDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.PharmacyDoctorAdvice, id int64) (err error) {
-
- // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
- var deliver_number int64 = 0
- var stock_number int64 = 0
-
- prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
- count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
-
- var drug_price float64
-
- //按最小单位数据计算
- deliver_number = count
-
- if advice.PrescribingNumberUnit == drup.MaxUnit {
- drug_price = drup.RetailPrice
- }
-
- if advice.PrescribingNumberUnit == drup.MinUnit {
- drug_price = drup.MinPrice
- }
-
- if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
- drug_price = drup.RetailPrice
- }
-
- // 根据先进先出原则,查询最先入库的批次,进行出库
- // 如果没有对应的库存,则报错
- //查询默认仓库
- storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
- warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
-
- if err != nil {
-
- return err
- }
-
- // 将该批次的剩余库存数量转换为拆零数量
-
- stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
-
- // 当库存数量大于或等于出库数量的话,则正常出库该批次
- if stock_number >= deliver_number {
- warehouseOutInfo := &models.DrugWarehouseOutInfo{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- Status: 1,
- Ctime: time.Now().Unix(),
- Remark: warehouse.Remark,
- OrgId: orgID,
- Type: 1,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- IsSys: 15,
- SysRecordTime: advice.RecordDate,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- BatchNumber: warehouse.BatchNumber,
- Price: warehouse.RetailPrice,
- CountUnit: drup.MinUnit,
- RetailPrice: drug_price,
- ProductDate: warehouse.ProductDate,
- ExpiryDate: warehouse.ExpiryDate,
- PatientId: advice.PatientId,
- Count: deliver_number, //出最小单位
- WarehouseInfoId: warehouse.ID,
- AdviceId: advice.ID,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- PharmacyId: id,
- }
-
- drugflow := models.DrugFlow{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- ProductDate: warehouse.ProductDate,
- ExpireDate: warehouse.ExpiryDate,
- Count: deliver_number, //按最小单位计算,
- Price: warehouse.RetailPrice,
- Status: 1,
- Ctime: time.Now().Unix(),
- UserOrgId: orgID,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- BatchNumber: warehouse.BatchNumber,
- MaxUnit: drup.MinUnit,
- ConsumableType: 3,
- IsEdit: 1,
- Creator: advice.ExecutionStaff,
- IsSys: 15,
- PatientId: advice.PatientId,
- WarehousingDetailId: warehouse.ID,
- AdviceId: advice.ID,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- PharmacyId: id,
- }
-
- CreateDrugFlowOne(drugflow)
- errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
- if errOne != nil {
- return errOne
- }
-
- details := &models.DrugAutomaticReduceDetail{
- WarehouseOutId: warehouseout.ID,
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- PatientId: advice.PatientId,
- Ctime: time.Now().Unix(),
- Mtime: time.Now().Unix(),
- Status: 1,
- RecordTime: advice.RecordDate,
- OrgId: orgID,
- DrugId: advice.DrugId,
- Count: count,
- CountUnit: advice.PrescribingNumberUnit,
- WarehouseInfoId: warehouse.ID,
- StorehouseId: storeConfig.DrugStorehouseOut,
- }
- errTwo := AddSigleDrugAutoReduceRecordInfo(details)
- if errTwo != nil {
- return errTwo
- }
-
- // 出库完成后,要减去对应批次的库存数量
- // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
- var maxNumber int64 = 0
- var minNumber int64 = 0
- var stock_max_number int64 = 0
- stock_max_number = warehouse.StockMaxNumber
-
- maxNumber = deliver_number / drup.MinNumber
- minNumber = deliver_number % drup.MinNumber
-
- if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
- minNumber = maxNumber
- }
-
- if drup.MaxUnit != drup.MinUnit {
- if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
- return errors.New("库存数量不足")
- }
- }
-
- warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
-
- if warehouse.StockMaxNumber < 0 {
- warehouse.StockMaxNumber = 0
- }
- if warehouse.StockMinNumber < 0 {
- warehouse.StockMinNumber = 0
- }
-
- warehouse.Mtime = time.Now().Unix()
-
- if warehouse.StockMinNumber < minNumber {
-
- warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
- warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
- } else {
-
- if minNumber > 0 {
- warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
- }
-
- if minNumber == 0 && maxNumber != 1 {
- warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
- }
- }
-
- if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
-
- warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
- if warehouse.StockMinNumber == 0 {
- warehouse.StockMaxNumber = stock_max_number
- }
- }
-
- if drup.MaxUnit != drup.MinUnit {
- if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
- return errors.New("库存数量不足")
- }
- }
- if warehouse.StockMinNumber <= 0 {
- warehouse.StockMinNumber = 0
- }
-
- errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
-
- if errThree != nil {
- return errThree
- }
-
- return nil
- } else {
- // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
- warehouseOutInfo := &models.DrugWarehouseOutInfo{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- Status: 1,
- Ctime: time.Now().Unix(),
- Remark: warehouse.Remark,
- OrgId: orgID,
- Type: 1,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- IsSys: 15,
- SysRecordTime: advice.RecordDate,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- BatchNumber: warehouse.BatchNumber,
- Price: warehouse.RetailPrice,
- CountUnit: drup.MinUnit,
- ProductDate: warehouse.ProductDate,
- ExpiryDate: warehouse.ExpiryDate,
- PatientId: advice.PatientId,
- Count: deliver_number,
- WarehouseInfoId: warehouse.ID,
- AdviceId: advice.ID,
- StorehouseId: storeConfig.DrugStorehouseOut,
- PharmacyId: id,
- }
- errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
- if errOne != nil {
- return errOne
- }
- //查询是否存在数据
-
- details := &models.DrugAutomaticReduceDetail{
- WarehouseOutId: warehouseout.ID,
- WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
- PatientId: advice.PatientId,
- Ctime: time.Now().Unix(),
- Mtime: time.Now().Unix(),
- Status: 1,
- RecordTime: advice.RecordDate,
- OrgId: orgID,
- DrugId: advice.DrugId,
- Count: deliver_number,
- CountUnit: drup.MinUnit,
- WarehouseInfoId: warehouse.ID,
- StorehouseId: storeConfig.DrugStorehouseOut,
- }
- errTwo := AddSigleDrugAutoReduceRecordInfo(details)
- if errTwo != nil {
- return errTwo
- }
-
- // 出库完成后,要将该批次库存清零
- //warehouse.StockMaxNumber = 0
- //warehouse.StockMinNumber = 0
- //warehouse.Mtime = time.Now().Unix()
-
- info := models.XtDrugWarehouseInfo{
- ID: warehouse.ID,
- WarehousingId: warehouse.WarehousingId,
- DrugId: warehouse.DrugId,
- Number: warehouse.Number,
- ProductDate: warehouse.ProductDate,
- ExpiryDate: warehouse.ExpiryDate,
- WarehousingCount: warehouse.WarehousingCount,
- Price: warehouse.Price,
- TotalPrice: warehouse.TotalPrice,
- Dealer: warehouse.Dealer,
- Manufacturer: warehouse.Manufacturer,
- Remark: warehouse.Remark,
- Ctime: warehouse.Ctime,
- Mtime: warehouse.Mtime,
- Status: 1,
- OrgId: warehouse.OrgId,
- IsReturn: warehouse.IsReturn,
- WarehousingOrder: warehouse.WarehousingOrder,
- Type: warehouse.Type,
- RetailPrice: warehouse.RetailPrice,
- RetailTotalPrice: warehouse.RetailPrice,
- StockMaxNumber: 0,
- StockMinNumber: 0,
- BatchNumber: warehouse.BatchNumber,
- MaxUnit: warehouse.MaxUnit,
- WarehousingInfoId: warehouse.WarehousingInfoId,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- }
- //扣减库存
- errThree := UpDateDrugWarehouseInfoByStock(&info)
-
- if errThree != nil {
- return errThree
- }
-
- drugflow := models.DrugFlow{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- ProductDate: warehouse.ProductDate,
- ExpireDate: warehouse.ExpiryDate,
- Count: deliver_number,
- Price: warehouse.RetailPrice,
- Status: 1,
- Ctime: time.Now().Unix(),
- UserOrgId: orgID,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- BatchNumber: warehouse.BatchNumber,
- MaxUnit: drup.MinUnit,
- ConsumableType: 3,
- IsEdit: 1,
- Creator: advice.ExecutionStaff,
- IsSys: 15,
- PatientId: advice.PatientId,
- WarehousingDetailId: warehouse.ID,
- AdviceId: advice.ID,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- PharmacyId: id,
- }
-
- CreateDrugFlowOne(drugflow)
-
- // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
- prescribingNumber_two_temp := deliver_number - stock_number
-
- overPlusNumber := float64(prescribingNumber_two_temp)
-
- advice.PrescribingNumber = float64(prescribingNumber_two_temp)
- advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
-
- NewPharmacyDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, id)
- }
-
- return
- }
-
- func FindAllPharmacyDrugWarehouseOut(org_id int64) (total int64, err error) {
- err = readDb.Model(&models.DrugWarehouseOut{}).Where("org_id = ? and is_sys = 15", org_id).Count(&total).Error
- return total, err
-
- }
-
- func CreatePharmacy(pharmacy models.Pharmacy) error {
-
- err := XTWriteDB().Create(&pharmacy).Error
- return err
- }
-
- // 药品出库
-
- func PharmacyHisDrugsDelivery(orgID int64, creater int64, advice *models.PharmacyHisDoctorAdviceInfo, id int64) (err error) {
- // 1.判断药品是否来自专用字典的药品库
- // 2.判断当天当前机构有没有创建出库单,没有则创建
- // 3.创建出库流程
- // 3.1 实现先进先出逻辑 由于药品执行后,不可以修改和删除,所以不考虑出库后的退库和修改出库数量等
- isHasWay := false
- record_time := int64(0)
- if advice.Way == 1 {
- isHasWay = true
- record_time = advice.RecordDate
- }
-
- if isHasWay {
- //判断当天当前机构有没有创建出库单,没有则创建
- out, err := FindDrugStockOutByIsSys(orgID, 15, record_time)
- if err == gorm.ErrRecordNotFound {
- timeStr := time.Now().Format("2006-01-02")
- timeArr := strings.Split(timeStr, "-")
- total, _ := FindAllDrugWarehouseOut(orgID)
- total = total + 1
- warehousing_out_order := strconv.FormatInt(orgID, 10) + timeArr[0] + timeArr[1] + timeArr[2] + "000"
- number, _ := strconv.ParseInt(warehousing_out_order, 10, 64)
- number = number + total
- warehousing_out_order = "YPCKD" + strconv.FormatInt(number, 10)
- storeConfig, _ := GetAllStoreHouseConfig(orgID)
- warehouseOut := models.DrugWarehouseOut{
- WarehouseOutOrderNumber: warehousing_out_order,
- OperationTime: time.Now().Unix(),
- OrgId: orgID,
- Creater: creater,
- Ctime: time.Now().Unix(),
- Status: 1,
- WarehouseOutTime: record_time,
- Dealer: 0,
- Manufacturer: 0,
- Type: 1,
- IsSys: 15,
- StorehouseId: storeConfig.DrugStorehouseOut,
- }
- err := AddSigleDrugWarehouseOut(&warehouseOut)
- if err != nil {
- utils.TraceLog("创建出库单失败 err = %v", err)
- return err
- } else {
- out = warehouseOut
- }
- }
-
- // 出库流程
- // 1.查询该药品在药品库的规格信息,并将处方里的规格进行换算(尽量将拆零单位转换成包装单位)
- drup, _ := FindBaseDrugLibRecord(orgID, advice.DrugId)
- if drup.ID > 0 {
- prescribingNumber := advice.PrescribingNumber
-
- PharmacyHisDrugDeliverInfo(orgID, prescribingNumber, &out, &drup, advice, id)
-
- } else {
- return errors.New("药品信息不存在")
- }
-
- }
-
- return
- }
-
- // 药品自动出库 递归方式
- func PharmacyHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.PharmacyHisDoctorAdviceInfo, id int64) (err error) {
-
- // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
- var deliver_number int64 = 0
- var stock_number int64 = 0
-
- prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
- count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
-
- var drug_price float64
- if advice.PrescribingNumberUnit == drup.MaxUnit {
- deliver_number = count * drup.MinNumber
-
- } else {
- deliver_number = count
-
- }
-
- if advice.PrescribingNumberUnit == drup.MaxUnit {
- drug_price = drup.RetailPrice
- }
- fmt.Println(drug_price)
-
- if advice.PrescribingNumberUnit == drup.MinUnit {
- drug_price = drup.MinPrice
- }
-
- if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
- drug_price = drup.RetailPrice
- }
-
- // 根据先进先出原则,查询最先入库的批次,进行出库
- // 如果没有对应的库存,则报错
- storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
- warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
-
- if err != nil {
-
- return err
- }
-
- // 将该批次的剩余库存数量转换为拆零数量
-
- stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
-
- // 当库存数量大于或等于出库数量的话,则正常出库该批次
- if stock_number >= deliver_number {
-
- warehouseOutInfo := &models.DrugWarehouseOutInfo{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- Status: 1,
- Ctime: time.Now().Unix(),
- Remark: warehouse.Remark,
- OrgId: orgID,
- Type: 1,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- IsSys: 15,
- SysRecordTime: advice.RecordDate,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- BatchNumber: warehouse.BatchNumber,
- Price: warehouse.RetailPrice,
- CountUnit: drup.MinUnit,
- RetailPrice: warehouse.RetailPrice,
- ProductDate: warehouse.ProductDate,
- ExpiryDate: warehouse.ExpiryDate,
- PatientId: advice.PatientId,
- Count: deliver_number, //出最小单位
- WarehouseInfoId: warehouse.ID,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- ID: id,
- }
-
- drugflow := models.DrugFlow{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- ProductDate: warehouse.ProductDate,
- ExpireDate: warehouse.ExpiryDate,
- Count: deliver_number, //按最小单位计算,
- Price: warehouse.RetailPrice,
- Status: 1,
- Ctime: time.Now().Unix(),
- UserOrgId: orgID,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- BatchNumber: warehouse.BatchNumber,
- MaxUnit: drup.MinUnit,
- ConsumableType: 3,
- IsEdit: 1,
- Creator: advice.ExecutionStaff,
- IsSys: 15,
- PatientId: advice.PatientId,
- SystemTime: advice.AdviceDate,
- WarehousingDetailId: warehouse.ID,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- ID: id,
- }
-
- CreateDrugFlowOne(drugflow)
-
- errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
- fmt.Println(errOne)
- //查询是否存在数据
-
- details := &models.DrugAutomaticReduceDetail{
- WarehouseOutId: warehouseout.ID,
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- PatientId: advice.PatientId,
- Ctime: time.Now().Unix(),
- Mtime: time.Now().Unix(),
- Status: 1,
- RecordTime: advice.RecordDate,
- OrgId: orgID,
- DrugId: advice.DrugId,
- Count: count,
- CountUnit: advice.PrescribingNumberUnit,
- WarehouseInfoId: warehouse.ID,
- StorehouseId: storeConfig.DrugStorehouseOut,
- }
- errTwo := AddSigleDrugAutoReduceRecordInfo(details)
- if errTwo != nil {
- return errTwo
- }
-
- var maxNumber int64 = 0
- var minNumber int64 = 0
- var stock_max_number int64 = 0
- stock_max_number = warehouse.StockMaxNumber
-
- maxNumber = deliver_number / drup.MinNumber
- minNumber = deliver_number % drup.MinNumber
-
- if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
- minNumber = maxNumber
- }
-
- if drup.MaxUnit != drup.MinUnit {
- if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
- return errors.New("库存数量不足")
- }
- }
-
- warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
-
- if warehouse.StockMaxNumber < 0 {
- warehouse.StockMaxNumber = 0
- }
- if warehouse.StockMinNumber < 0 {
- warehouse.StockMinNumber = 0
- }
-
- warehouse.Mtime = time.Now().Unix()
-
- if warehouse.StockMinNumber < minNumber {
-
- warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
- warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
- } else {
- if minNumber > 0 {
- warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
- }
-
- if minNumber == 0 && maxNumber != 1 {
- warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
- }
-
- }
-
- if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
- warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
- if warehouse.StockMinNumber == 0 {
- warehouse.StockMaxNumber = stock_max_number
- }
- }
-
- if drup.MaxUnit != drup.MinUnit {
- if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
- return errors.New("库存数量不足")
- }
- }
- if warehouse.StockMinNumber <= 0 {
- warehouse.StockMinNumber = 0
- }
-
- errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
- if errThree != nil {
- return errThree
- }
-
- return nil
- } else {
- // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
- warehouseOutInfo := &models.DrugWarehouseOutInfo{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- Status: 1,
- Ctime: time.Now().Unix(),
- Remark: warehouse.Remark,
- OrgId: orgID,
- Type: 1,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- IsSys: 15,
- SysRecordTime: advice.RecordDate,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- BatchNumber: warehouse.BatchNumber,
- Price: warehouse.RetailPrice,
- CountUnit: drup.MinUnit,
- ProductDate: warehouse.ProductDate,
- ExpiryDate: warehouse.ExpiryDate,
- PatientId: advice.PatientId,
- Count: deliver_number,
- WarehouseInfoId: warehouse.ID,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- ID: id,
- }
- errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
- if errOne != nil {
- return errOne
- }
-
- details := &models.DrugAutomaticReduceDetail{
- WarehouseOutId: warehouseout.ID,
- WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
- PatientId: advice.PatientId,
- Ctime: time.Now().Unix(),
- Mtime: time.Now().Unix(),
- Status: 1,
- RecordTime: advice.RecordDate,
- OrgId: orgID,
- DrugId: advice.DrugId,
- Count: deliver_number,
- CountUnit: drup.MinUnit,
- WarehouseInfoId: warehouse.ID,
- StorehouseId: storeConfig.DrugStorehouseOut,
- }
- errTwo := AddSigleDrugAutoReduceRecordInfo(details)
- if errTwo != nil {
- return errTwo
- }
-
- info := models.XtDrugWarehouseInfo{
- ID: warehouse.ID,
- WarehousingId: warehouse.WarehousingId,
- DrugId: warehouse.DrugId,
- Number: warehouse.Number,
- ProductDate: warehouse.ProductDate,
- ExpiryDate: warehouse.ExpiryDate,
- WarehousingCount: warehouse.WarehousingCount,
- Price: warehouse.RetailPrice,
- TotalPrice: warehouse.TotalPrice,
- Dealer: warehouse.Dealer,
- Manufacturer: warehouse.Manufacturer,
- Remark: warehouse.Remark,
- Ctime: warehouse.Ctime,
- Mtime: warehouse.Mtime,
- Status: 1,
- OrgId: warehouse.OrgId,
- IsReturn: warehouse.IsReturn,
- WarehousingOrder: warehouse.WarehousingOrder,
- Type: warehouse.Type,
- RetailPrice: warehouse.RetailPrice,
- RetailTotalPrice: warehouse.RetailPrice,
- StockMaxNumber: 0,
- StockMinNumber: 0,
- BatchNumber: warehouse.BatchNumber,
- MaxUnit: warehouse.MaxUnit,
- WarehousingInfoId: warehouse.WarehousingInfoId,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- }
- //扣减库存
- errThree := UpDateDrugWarehouseInfoByStock(&info)
-
- if errThree != nil {
- return errThree
- }
-
- drugflow := models.DrugFlow{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- ProductDate: warehouse.ProductDate,
- ExpireDate: warehouse.ExpiryDate,
- Count: stock_number,
- Price: warehouse.RetailPrice,
- Status: 1,
- Ctime: time.Now().Unix(),
- UserOrgId: orgID,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- BatchNumber: warehouse.BatchNumber,
- MaxUnit: drup.MinUnit,
- ConsumableType: 3,
- IsEdit: 1,
- Creator: advice.ExecutionStaff,
- IsSys: 15,
- PatientId: advice.PatientId,
- WarehousingDetailId: warehouse.ID,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- ID: id,
- }
-
- CreateDrugFlowOne(drugflow)
-
- // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
- prescribingNumber_two_temp := deliver_number - stock_number
-
- overPlusNumber := float64(prescribingNumber_two_temp)
-
- advice.PrescribingNumber = float64(prescribingNumber_two_temp)
- advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
-
- NewPharmacyHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, id)
- }
-
- return
- }
-
- // 药品出库 递归方式
- func NewPharmacyHisDrugDeliverInfo(orgID int64, prescribingNumber float64, warehouseout *models.DrugWarehouseOut, drup *models.BaseDrugLib, advice *models.PharmacyHisDoctorAdviceInfo, id int64) (err error) {
-
- // 判断处方里药品单位是拆零单位还是包装单位, 如果是包装单位,则根据规格,将包装数量转为拆零数量
- var deliver_number int64 = 0
- var stock_number int64 = 0
-
- prescribingNumber_temp := strconv.FormatFloat(math.Abs(prescribingNumber), 'f', 0, 64)
- count, _ := strconv.ParseInt(prescribingNumber_temp, 10, 64)
-
- var drug_price float64
-
- //按最小单位数据计算
- deliver_number = count
-
- if advice.PrescribingNumberUnit == drup.MaxUnit {
- drug_price = drup.RetailPrice
- }
-
- if advice.PrescribingNumberUnit == drup.MinUnit {
- drug_price = drup.MinPrice
- }
-
- if advice.PrescribingNumberUnit != drup.MaxUnit || advice.PrescribingNumberUnit != drup.MinUnit {
- drug_price = drup.RetailPrice
- }
-
- // 根据先进先出原则,查询最先入库的批次,进行出库
- // 如果没有对应的库存,则报错
- //查询默认仓库
- storeConfig, _ := GetAllStoreHouseConfig(advice.UserOrgId)
- warehouse, err := FindLastDrugWarehousingInfoByID(advice.DrugId, storeConfig.DrugStorehouseOut)
-
- if err != nil {
-
- return err
- }
-
- // 将该批次的剩余库存数量转换为拆零数量
-
- stock_number = warehouse.StockMaxNumber*drup.MinNumber + warehouse.StockMinNumber
-
- // 当库存数量大于或等于出库数量的话,则正常出库该批次
- if stock_number >= deliver_number {
- warehouseOutInfo := &models.DrugWarehouseOutInfo{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- Status: 1,
- Ctime: time.Now().Unix(),
- Remark: warehouse.Remark,
- OrgId: orgID,
- Type: 1,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- IsSys: 15,
- SysRecordTime: advice.RecordDate,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- BatchNumber: warehouse.BatchNumber,
- Price: warehouse.RetailPrice,
- CountUnit: drup.MinUnit,
- RetailPrice: drug_price,
- ProductDate: warehouse.ProductDate,
- ExpiryDate: warehouse.ExpiryDate,
- PatientId: advice.PatientId,
- Count: deliver_number, //出最小单位
- WarehouseInfoId: warehouse.ID,
- AdviceId: advice.ID,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- ID: id,
- }
-
- drugflow := models.DrugFlow{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- ProductDate: warehouse.ProductDate,
- ExpireDate: warehouse.ExpiryDate,
- Count: deliver_number, //按最小单位计算,
- Price: warehouse.RetailPrice,
- Status: 1,
- Ctime: time.Now().Unix(),
- UserOrgId: orgID,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- BatchNumber: warehouse.BatchNumber,
- MaxUnit: drup.MinUnit,
- ConsumableType: 3,
- IsEdit: 1,
- Creator: advice.ExecutionStaff,
- IsSys: 15,
- PatientId: advice.PatientId,
- WarehousingDetailId: warehouse.ID,
- AdviceId: advice.ID,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- ID: id,
- }
-
- CreateDrugFlowOne(drugflow)
- errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
- if errOne != nil {
- return errOne
- }
-
- details := &models.DrugAutomaticReduceDetail{
- WarehouseOutId: warehouseout.ID,
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- PatientId: advice.PatientId,
- Ctime: time.Now().Unix(),
- Mtime: time.Now().Unix(),
- Status: 1,
- RecordTime: advice.RecordDate,
- OrgId: orgID,
- DrugId: advice.DrugId,
- Count: count,
- CountUnit: advice.PrescribingNumberUnit,
- WarehouseInfoId: warehouse.ID,
- StorehouseId: storeConfig.DrugStorehouseOut,
- }
- errTwo := AddSigleDrugAutoReduceRecordInfo(details)
- if errTwo != nil {
- return errTwo
- }
-
- // 出库完成后,要减去对应批次的库存数量
- // 判断处方里药品单位是拆零单位还是包装单位, 如果是拆零单位,则根据规格,将拆零数量转为包装数量
- var maxNumber int64 = 0
- var minNumber int64 = 0
- var stock_max_number int64 = 0
- stock_max_number = warehouse.StockMaxNumber
-
- maxNumber = deliver_number / drup.MinNumber
- minNumber = deliver_number % drup.MinNumber
-
- if warehouse.StockMaxNumber == 0 && drup.MaxUnit == drup.MinUnit {
- minNumber = maxNumber
- }
-
- if drup.MaxUnit != drup.MinUnit {
- if warehouse.StockMaxNumber < maxNumber && warehouse.StockMinNumber < minNumber {
- return errors.New("库存数量不足")
- }
- }
-
- warehouse.StockMaxNumber = warehouse.StockMaxNumber - maxNumber
-
- if warehouse.StockMaxNumber < 0 {
- warehouse.StockMaxNumber = 0
- }
- if warehouse.StockMinNumber < 0 {
- warehouse.StockMinNumber = 0
- }
-
- warehouse.Mtime = time.Now().Unix()
-
- if warehouse.StockMinNumber < minNumber {
-
- warehouse.StockMaxNumber = warehouse.StockMaxNumber - 1
- warehouse.StockMinNumber = warehouse.StockMinNumber + drup.MinNumber - minNumber
- } else {
-
- if minNumber > 0 {
- warehouse.StockMinNumber = warehouse.StockMinNumber - minNumber
- }
-
- if minNumber == 0 && maxNumber != 1 {
- warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
- }
- }
-
- if maxNumber == 1 && minNumber == 0 && drup.MaxUnit != drup.MinUnit {
-
- warehouse.StockMinNumber = warehouse.StockMinNumber - deliver_number
- if warehouse.StockMinNumber == 0 {
- warehouse.StockMaxNumber = stock_max_number
- }
- }
-
- if drup.MaxUnit != drup.MinUnit {
- if warehouse.StockMaxNumber < 0 && warehouse.StockMinNumber < 0 {
- return errors.New("库存数量不足")
- }
- }
- if warehouse.StockMinNumber <= 0 {
- warehouse.StockMinNumber = 0
- }
-
- errThree := UpDateDrugWarehouseInfoByStock(&warehouse)
-
- if errThree != nil {
- return errThree
- }
-
- return nil
- } else {
- // 当该批次的库存数量小于出库数量的话,则先把该批次出库完后,再进行递归出库
- warehouseOutInfo := &models.DrugWarehouseOutInfo{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- Status: 1,
- Ctime: time.Now().Unix(),
- Remark: warehouse.Remark,
- OrgId: orgID,
- Type: 1,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- IsSys: 15,
- SysRecordTime: advice.RecordDate,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- BatchNumber: warehouse.BatchNumber,
- Price: warehouse.RetailPrice,
- CountUnit: drup.MinUnit,
- ProductDate: warehouse.ProductDate,
- ExpiryDate: warehouse.ExpiryDate,
- PatientId: advice.PatientId,
- Count: deliver_number,
- WarehouseInfoId: warehouse.ID,
- AdviceId: advice.ID,
- StorehouseId: storeConfig.DrugStorehouseOut,
- ID: id,
- }
- errOne := AddSigleDrugWarehouseOutInfo(warehouseOutInfo)
- if errOne != nil {
- return errOne
- }
- //查询是否存在数据
-
- details := &models.DrugAutomaticReduceDetail{
- WarehouseOutId: warehouseout.ID,
- WarehouseOutOrderNumber: warehouseOutInfo.WarehouseOutOrderNumber,
- PatientId: advice.PatientId,
- Ctime: time.Now().Unix(),
- Mtime: time.Now().Unix(),
- Status: 1,
- RecordTime: advice.RecordDate,
- OrgId: orgID,
- DrugId: advice.DrugId,
- Count: deliver_number,
- CountUnit: drup.MinUnit,
- WarehouseInfoId: warehouse.ID,
- StorehouseId: storeConfig.DrugStorehouseOut,
- }
- errTwo := AddSigleDrugAutoReduceRecordInfo(details)
- if errTwo != nil {
- return errTwo
- }
-
- // 出库完成后,要将该批次库存清零
- //warehouse.StockMaxNumber = 0
- //warehouse.StockMinNumber = 0
- //warehouse.Mtime = time.Now().Unix()
-
- info := models.XtDrugWarehouseInfo{
- ID: warehouse.ID,
- WarehousingId: warehouse.WarehousingId,
- DrugId: warehouse.DrugId,
- Number: warehouse.Number,
- ProductDate: warehouse.ProductDate,
- ExpiryDate: warehouse.ExpiryDate,
- WarehousingCount: warehouse.WarehousingCount,
- Price: warehouse.Price,
- TotalPrice: warehouse.TotalPrice,
- Dealer: warehouse.Dealer,
- Manufacturer: warehouse.Manufacturer,
- Remark: warehouse.Remark,
- Ctime: warehouse.Ctime,
- Mtime: warehouse.Mtime,
- Status: 1,
- OrgId: warehouse.OrgId,
- IsReturn: warehouse.IsReturn,
- WarehousingOrder: warehouse.WarehousingOrder,
- Type: warehouse.Type,
- RetailPrice: warehouse.RetailPrice,
- RetailTotalPrice: warehouse.RetailPrice,
- StockMaxNumber: 0,
- StockMinNumber: 0,
- BatchNumber: warehouse.BatchNumber,
- MaxUnit: warehouse.MaxUnit,
- WarehousingInfoId: warehouse.WarehousingInfoId,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- }
- //扣减库存
- errThree := UpDateDrugWarehouseInfoByStock(&info)
-
- if errThree != nil {
- return errThree
- }
-
- drugflow := models.DrugFlow{
- WarehouseOutOrderNumber: warehouseout.WarehouseOutOrderNumber,
- WarehouseOutId: warehouseout.ID,
- DrugId: advice.DrugId,
- Number: warehouse.Number,
- ProductDate: warehouse.ProductDate,
- ExpireDate: warehouse.ExpiryDate,
- Count: deliver_number,
- Price: warehouse.RetailPrice,
- Status: 1,
- Ctime: time.Now().Unix(),
- UserOrgId: orgID,
- Manufacturer: warehouse.Manufacturer,
- Dealer: warehouse.Dealer,
- BatchNumber: warehouse.BatchNumber,
- MaxUnit: drup.MinUnit,
- ConsumableType: 3,
- IsEdit: 1,
- Creator: advice.ExecutionStaff,
- IsSys: 15,
- PatientId: advice.PatientId,
- WarehousingDetailId: warehouse.ID,
- AdviceId: advice.ID,
- SupplyWarehouseId: warehouse.SupplyWarehouseId,
- StorehouseId: storeConfig.DrugStorehouseOut,
- ID: id,
- }
-
- CreateDrugFlowOne(drugflow)
-
- // 清零完该库存后,还有剩余出库未出完,进行对应的递归操作
- prescribingNumber_two_temp := deliver_number - stock_number
-
- overPlusNumber := float64(prescribingNumber_two_temp)
-
- advice.PrescribingNumber = float64(prescribingNumber_two_temp)
- advice.PrescribingNumberUnit = advice.PrescribingNumberUnit
-
- NewPharmacyHisDrugDeliverInfo(orgID, overPlusNumber, warehouseout, drup, advice, id)
- }
- return
- }
-
- func GetUseredBloodAdviceList(advice_date int64, drug_id int64, org_id int64) (advice []*models.PharmacyDoctorAdvice, err error) {
-
- err = XTReadDB().Where("advice_date = ? and drug_id = ? and user_org_id = ? and status = 1 and advice_type =2 and is_medicine= 1", advice_date, drug_id, org_id).Find(&advice).Error
- return advice, err
- }
-
- func GetUseredHisAdviceList(advice_date int64, drug_id int64, org_id int64) (advice []*models.PharmacyHisDoctorAdviceInfo, err error) {
-
- err = XTReadDB().Where("advice_date = ? and drug_id =? and user_org_id =? and status = 1 and is_medicine = 1", advice_date, drug_id, org_id).Order("prescription_id asc").Find(&advice).Error
- return advice, err
- }
-
- func UpdateReturnPharmacyAdviceBaseDrug(ids []string, orgid int64) (models.PharmacyDoctorAdvice, error) {
-
- advice := models.PharmacyDoctorAdvice{}
-
- err := XTWriteDB().Where("id in(?) and user_org_id = ? and status = 1", ids, orgid).Update(map[string]interface{}{"is_medicine": 0}).Error
- return advice, err
- }
-
- func UpdateReturnPharmacyHisAdviceBaseDrug(ids []string, orgid int64) (models.PharmacyHisDoctorAdviceInfo, error) {
-
- advice := models.PharmacyHisDoctorAdviceInfo{}
-
- err := XTWriteDB().Where("id in(?) and user_org_id = ? and status = 1", ids, orgid).Update(map[string]interface{}{"is_medicine": 0}).Error
- return advice, err
- }
-
- func GetLastPharmary(orgid int64, advice_id int64, record_date int64) (models.Pharmacy, error) {
-
- pharmacy := models.Pharmacy{}
-
- err := XTReadDB().Where("user_org_id = ? and advice_id = ? and record_date =? and status =1 ", orgid, advice_id, record_date).Find(&pharmacy).Error
-
- return pharmacy, err
- }
-
- func GetLastHisPharmary(orgid int64, advice_id int64, record_date int64) (models.Pharmacy, error) {
-
- pharmacy := models.Pharmacy{}
-
- err := XTReadDB().Where("user_org_id = ? and his_advice_id = ? and record_date =? and status =1 ", orgid, advice_id, record_date).Find(&pharmacy).Error
-
- return pharmacy, err
- }
-
- func GetConfigSettingIsExsit(orgid int64) (*models.PharmacyConfig, error) {
-
- var pharmacy models.PharmacyConfig
- var err error
- err = readDb.Model(&models.PharmacyConfig{}).Where("user_org_id=? and status = 1", orgid).First(&pharmacy).Error
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- }
- if err != nil {
- return nil, err
- }
- return &pharmacy, nil
- }
-
- func CreatePharmacyConfig(config *models.PharmacyConfig) error {
-
- err := XTWriteDB().Create(&config).Error
- return err
- }
-
- func UpdatePharmacyConfig(orgid int64, config *models.PharmacyConfig) error {
-
- err := XTWriteDB().Model(&models.PharmacyConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"is_open": config.IsOpen, "mtime": time.Now().Unix()}).Error
- return err
- }
-
- func GetPharmacyConfig(orgid int64) (models.PharmacyConfig, error) {
-
- config := models.PharmacyConfig{}
- err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&config).Error
- if err == gorm.ErrRecordNotFound {
- tmp := models.PharmacyConfig{
- UserOrgId: orgid,
- IsOpen: 2,
- Status: 1,
- Ctime: time.Now().Unix(),
- Mtime: time.Now().Unix(),
- }
- errs := XTWriteDB().Create(&tmp).Error
- if errs != nil {
- err = errs
- } else {
- err = nil
- }
- return tmp, err
- }
-
- return config, err
- }
-
- func CreateDrugError(drugError models.XtDrugError) error {
-
- err := XTWriteDB().Create(&drugError).Error
-
- return err
- }
|