123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650 |
- 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
- }
|