• haoyanbin's avatar
    1 · 9bf9e037
    haoyanbin authored
    9bf9e037
revenue_today.go 12.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
package repository

import (
	"database/sql"
	"errors"
	"fmt"
	"gin-vue-admin/models"
)

func RevenueTodayListTemp(db *sql.DB, t string, employeeID int) (interface{}, error) {

	hospitalLocalId, err := QueryHospitalLocalIDByEmployeeLocalID(db, employeeID)
	if err != nil {
		return nil, err
	}

	// 计算时间 及格式化的模板
	var formatSQL string

	if len(t) == 10 {
		formatSQL = "%Y-%m-%d"
	} else if len(t) == 7 {
		formatSQL = "%Y-%m"
	} else if len(t) == 4 {
		formatSQL = "%Y"
	} else {
		return nil, errors.New("time error")
	}

	rows, err := db.Query(`select c.bussinesstype,
convert(ifnull(sum(c.quantity*c.payprice), 0) / 1000000, decimal(11,3))  
from his_consumption as c
left join his_bill as b on b.id = c.his_bill_id
where c.delflag = 0 
and c.his_retreatbill_id = 0 
and c.commodity_ismeal = 0
and c.meter_num = 0
and b.billtype in (1,4,6,7)
and DATE_FORMAT(b.paytime, ?) = ?
GROUP BY c.bussinesstype`, formatSQL, t)
	if err != nil {
		return nil, err
	}

	// DB:
	// 1:商品销售 2:挂号 3:住院每日消费 4.住院门诊 41:住院门诊化验  42:住院门诊影像 43:住院门诊处方
	// 44:住院门诊手术 45:住院门诊处置  5:住院商品销售 6:普通门诊 61:普通门诊化验  62:普通门诊影像
	// 63:普通门诊处方 64:普通门诊手术 65:普通门诊处置 7:美容 8:寄养每日消费 9:寄养商品销售 10:疫苗 11:驱虫

	// Map
	// 1 商品销售
	// 2 挂号
	// 3 住院每日消费
	// 4 化验
	// 5 影像
	// 6 处方
	// 7 手术
	// 8 处置
	// 9 美容
	// 10 寄养
	// 11 疫苗
	// 12 驱虫

	tempRes := map[int]float64{
		1:  0,
		2:  0,
		3:  0,
		4:  0,
		5:  0,
		6:  0,
		7:  0,
		8:  0,
		9:  0,
		10: 0,
		11: 0,
		12: 0,
	}
	for rows.Next() {
		var businessType int
		var money float64
		rows.Scan(&businessType, &money)

		switch businessType {
		case 1:
			tempRes[1] += money
		case 2:
			tempRes[2] += money
		case 3:
			tempRes[3] += money
		case 4:
			tempRes[3] += money
		case 41:
			tempRes[4] += money
		case 42:
			tempRes[5] += money
		case 43:
			tempRes[6] += money
		case 44:
			tempRes[7] += money
		case 45:
			tempRes[8] += money
		case 5:
			tempRes[3] += money
		case 6:
			tempRes[6] += money
		case 61:
			tempRes[4] += money
		case 62:
			tempRes[5] += money
		case 63:
			tempRes[6] += money
		case 64:
			tempRes[7] += money
		case 65:
			tempRes[8] += money
		case 7:
			tempRes[9] += money
		case 8:
			tempRes[10] += money
		case 9:
			tempRes[10] += money
		case 10:
			tempRes[11] += money
		case 11:
			tempRes[12] += money
		}
	}

	var r models.Today

	response := make([]models.RevenueToday, 0)

	business := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

	for _, v := range business {
		var t1 models.RevenueToday
		var t2 models.RevenueToday
		switch v {
		case 1:
			t1.Type = "收入"
			t1.Name = "商品销售"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "商品销售"
			t2.Value = 0
		case 2:
			t1.Type = "收入"
			t1.Name = "挂号"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "挂号"
			t2.Value = 0
		case 3:
			t1.Type = "收入"
			t1.Name = "住院"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "住院"
			t2.Value = 0
		case 4:
			t1.Type = "收入"
			t1.Name = "化验"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "化验"
			t2.Value = 0
		case 5:
			t1.Type = "收入"
			t1.Name = "影像"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "影像"
			t2.Value = 0
		case 6:
			t1.Type = "收入"
			t1.Name = "处方"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "处方"
			t2.Value = 0
		case 7:
			t1.Type = "收入"
			t1.Name = "手术"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "手术"
			t2.Value = 0
		case 8:
			t1.Type = "收入"
			t1.Name = "处置"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "处置"
			t2.Value = 0
		case 9:
			t1.Type = "收入"
			t1.Name = "美容"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "美容"
			t2.Value = 0
		case 10:
			t1.Type = "收入"
			t1.Name = "寄养"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "寄养"
			t2.Value = 0
		case 11:
			t1.Type = "收入"
			t1.Name = "疫苗"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "疫苗"
			t2.Value = 0
		case 12:
			t1.Type = "收入"
			t1.Name = "驱虫"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "驱虫"
			t2.Value = 0
		}

		if _, ok := tempRes[v]; ok {
			r.Income += tempRes[v]
		}

		response = append(response, t1, t2)
	}

	// 会员卡 收入 与 支出
	consumerCardRecharge, err := ConsumerCardRecharge(db, t, formatSQL)
	if err != nil {
		return nil, err
	}
	retreatConsumerCard, err := RetreatConsumerCard(db, t, formatSQL, hospitalLocalId)
	if err != nil {
		return nil, err
	}

	var card1 models.RevenueToday
	var card2 models.RevenueToday
	card1.Type = "收入"
	card1.Name = "会员卡"
	card1.Value = consumerCardRecharge
	card2.Type = "支出"
	card2.Name = "会员卡"
	card2.Value = retreatConsumerCard
	response = append(response, card1)
	response = append(response, card2)
	r.Income += consumerCardRecharge
	r.Expend += retreatConsumerCard

	// 押金收入与支出
	depositMoneyRecharge, err := DepositMoneyRecharge(db, t, formatSQL)
	if err != nil {
		return nil, err
	}
	retreatDepositMoney, err := RetreatDepositMoney(db, t, formatSQL, hospitalLocalId)
	if err != nil {
		return nil, err
	}

	var deposit1 models.RevenueToday
	var deposit2 models.RevenueToday
	deposit1.Type = "收入"
	deposit1.Name = "押金"
	deposit1.Value = depositMoneyRecharge
	deposit2.Type = "支出"
	deposit2.Name = "押金"
	deposit2.Value = retreatDepositMoney
	response = append(response, deposit1)
	response = append(response, deposit2)
	r.Income += depositMoneyRecharge
	r.Expend += retreatDepositMoney

	// 次卡收入与支出
	meterCardRecharge, err := MeterCardRecharge(db, t, formatSQL, 0, hospitalLocalId)
	if err != nil {
		return nil, err
	}
	retreatMeterCard, err := RetreatMeterCard(db, t, formatSQL, hospitalLocalId)
	if err != nil {
		return nil, err
	}

	var meterCard1 models.RevenueToday
	var meterCard2 models.RevenueToday
	meterCard1.Type = "收入"
	meterCard1.Name = "次卡"
	meterCard1.Value = meterCardRecharge
	meterCard2.Type = "支出"
	meterCard2.Name = "次卡"
	meterCard2.Value = retreatMeterCard
	response = append(response, meterCard1)
	response = append(response, meterCard2)
	r.Income += meterCardRecharge
	r.Expend += retreatMeterCard

	//  4. 已结账单中: 现金、支付宝、微信、银行卡 (会员卡、次卡、押金结算 不算其中)
	//	5. 销售出库-现金结算
	//	6. 采购退货-现金结算
	//	7. 预收款
	//	8. 收入单
	billOfSettlementIncome, err := BillOfSettlementIncome(db, t, formatSQL)
	if err != nil {
		return nil, err
	}
	stockOutBill, err := StockOutBill(db, t, formatSQL)
	if err != nil {
		return nil, err
	}

	stockRetreatBill, err := StockRetreatBill(db, t, formatSQL)
	if err != nil {
		return nil, err
	}

	preInFundBill, err := PreInFundBill(db, t, formatSQL)
	if err != nil {
		return nil, err
	}
	retreatPreInFundBill, err := RetreatPreInFundBill(db, t, formatSQL)
	if err != nil {
		return nil, err
	}
	inChargeBill, err := InChargeBill(db, t, formatSQL)
	if err != nil {
		return nil, err
	}

	var other1 models.RevenueToday
	other1.Type = "收入"
	other1.Name = "其它"
	other1.Value = billOfSettlementIncome + stockOutBill + stockRetreatBill + preInFundBill - retreatPreInFundBill + inChargeBill
	response = append(response, other1)

	//4. 支出管理中的
	//5. 采购入库-现金结算
	//6.  预付款

	outChargeBill, err := OutChargeBill(db, t, formatSQL, hospitalLocalId)
	if err != nil {
		return nil, err
	}

	stockInBill, err := StockInBill(db, t, formatSQL, hospitalLocalId)
	if err != nil {
		return nil, err
	}

	preOutFundBill, err := PreOutFundBill(db, t, formatSQL, hospitalLocalId)
	if err != nil {
		return nil, err
	}
	retreatPreOutFundBill, err := RetreatPreOutFundBill(db, t, formatSQL, hospitalLocalId)
	if err != nil {
		return nil, err
	}

	var other2 models.RevenueToday
	other2.Type = "支出"
	other2.Name = "其它"
	other2.Value = outChargeBill + stockInBill + preOutFundBill - retreatPreOutFundBill
	response = append(response, other2)

	r.Income += other1.Value
	r.Expend += other2.Value

	r.RevenueTodayList = response

	return r, nil
}

func RevenueTodayList(db *sql.DB, t string, employeeId int) (interface{}, error) {

	hospitalLocalId, err := QueryHospitalLocalIDByEmployeeLocalID(db, employeeId)
	if err != nil {
		return nil, err
	}

	// 计算时间 及格式化的模板
	var formatSQL string

	if len(t) == 10 {
		formatSQL = "%Y-%m-%d"
	} else if len(t) == 7 {
		formatSQL = "%Y-%m"
	} else if len(t) == 4 {
		formatSQL = "%Y"
	} else {
		return nil, errors.New("time error")
	}

	rows, err := db.Query(fmt.Sprintf(`select c.bussinesstype,
convert(ifnull(sum(c.quantity*c.payprice), 0) / 1000000, decimal(11,3))  
from his_consumption as c
left join his_bill as b on b.id = c.his_bill_id
where c.delflag = 0 
and c.his_retreatbill_id = 0 
and c.commodity_ismeal = 0
and c.meter_num = 0
and b.billtype in (1,4,6,7)
and DATE_FORMAT(b.paytime, ?) = ?
and  c.sys_hospital_id in (0,%v) 
GROUP BY c.bussinesstype`, hospitalLocalId), formatSQL, t)
	if err != nil {
		return nil, err
	}

	// DB:
	// 1:商品销售 2:挂号 3:住院每日消费 4.住院门诊 41:住院门诊化验  42:住院门诊影像 43:住院门诊处方
	// 44:住院门诊手术 45:住院门诊处置  5:住院商品销售 6:普通门诊 61:普通门诊化验  62:普通门诊影像
	// 63:普通门诊处方 64:普通门诊手术 65:普通门诊处置 7:美容 8:寄养每日消费 9:寄养商品销售 10:疫苗 11:驱虫

	// Map
	// 1 商品销售
	// 2 挂号
	// 3 住院每日消费
	// 4 化验
	// 5 影像
	// 6 处方
	// 7 手术
	// 8 处置
	// 9 美容
	// 10 寄养
	// 11 疫苗
	// 12 驱虫

	tempRes := map[int]float64{
		1:  0,
		2:  0,
		3:  0,
		4:  0,
		5:  0,
		6:  0,
		7:  0,
		8:  0,
		9:  0,
		10: 0,
		11: 0,
		12: 0,
	}
	for rows.Next() {
		var businessType int
		var money float64
		rows.Scan(&businessType, &money)

		switch businessType {
		case 1:
			tempRes[1] += money
		case 2:
			tempRes[2] += money
		case 3:
			tempRes[3] += money
		case 4:
			tempRes[3] += money
		case 41:
			tempRes[4] += money
		case 42:
			tempRes[5] += money
		case 43:
			tempRes[6] += money
		case 44:
			tempRes[7] += money
		case 45:
			tempRes[8] += money
		case 5:
			tempRes[3] += money
		case 6:
			tempRes[6] += money
		case 61:
			tempRes[4] += money
		case 62:
			tempRes[5] += money
		case 63:
			tempRes[6] += money
		case 64:
			tempRes[7] += money
		case 65:
			tempRes[8] += money
		case 7:
			tempRes[9] += money
		case 8:
			tempRes[10] += money
		case 9:
			tempRes[10] += money
		case 10:
			tempRes[11] += money
		case 11:
			tempRes[12] += money
		}
	}

	var r models.Today

	response := make([]models.RevenueToday, 0)

	business := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

	for _, v := range business {
		var t1 models.RevenueToday
		var t2 models.RevenueToday
		switch v {
		case 1:
			t1.Type = "营业额"
			t1.Name = "商品销售"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "商品销售"
			t2.Value = 0
		case 2:
			t1.Type = "营业额"
			t1.Name = "挂号"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "挂号"
			t2.Value = 0
		case 3:
			t1.Type = "营业额"
			t1.Name = "住院"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "住院"
			t2.Value = 0
		case 4:
			t1.Type = "营业额"
			t1.Name = "化验"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "化验"
			t2.Value = 0
		case 5:
			t1.Type = "营业额"
			t1.Name = "影像"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "影像"
			t2.Value = 0
		case 6:
			t1.Type = "营业额"
			t1.Name = "处方"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "处方"
			t2.Value = 0
		case 7:
			t1.Type = "营业额"
			t1.Name = "手术"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "手术"
			t2.Value = 0
		case 8:
			t1.Type = "营业额"
			t1.Name = "处置"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "处置"
			t2.Value = 0
		case 9:
			t1.Type = "营业额"
			t1.Name = "美容"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "美容"
			t2.Value = 0
		case 10:
			t1.Type = "营业额"
			t1.Name = "寄养"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "寄养"
			t2.Value = 0
		case 11:
			t1.Type = "营业额"
			t1.Name = "疫苗"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "疫苗"
			t2.Value = 0
		case 12:
			t1.Type = "营业额"
			t1.Name = "驱虫"
			t1.Value = tempRes[v]
			t2.Type = "支出"
			t2.Name = "驱虫"
			t2.Value = 0
		}

		if _, ok := tempRes[v]; ok {
			r.Income += tempRes[v]
		}

		response = append(response, t1)
	}

	r.RevenueTodayList = response

	return r, nil
}