amount_calculation.go 8.17 KB
Newer Older
haoyanbin's avatar
1  
haoyanbin committed
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
package repository

import (
	"database/sql"
	"fmt"
)

// 按已结账单计算金额
func BillOfSettlement(db *sql.DB, t, formatSQL string, hospitalLocalId int) (float64, error) {
	var money float64
	err := db.QueryRow(`select ifnull(convert(sum(b.paymoney) / 1000, decimal(11,3)), 0) 
from his_bill as b
where b.delflag = 0
and b.billtype in (1,4,6,7)
and DATE_FORMAT(b.billtime,?) = ? 
and b.sys_hospital_id in (0,?) `, formatSQL, t, hospitalLocalId).Scan(&money)

	return money, err
}

// 已结账单中所卖出的商品的 成本
func BillByCost(db *sql.DB, t, formatSQL string, hospitalLocalId int, chainCode string) (float64, error) {

	isNew, err := ISNewStock(chainCode)
	if err != nil {
		return 0, err
	}

	var money float64

	if isNew == 1 {
		err = db.QueryRow(`select 
ifnull(convert(sum(c.quantity*g.costmoney) / 1000000, decimal(11,3)), 0) as money
from his_consumption as c
left join his_bill as b on b.id = c.his_bill_id
LEFT JOIN g_new_stock_count count on count.his_consumption_id = c.id and count.his_consumption_id>0 and count.delflag = 0
LEFT JOIN g_new_stock g on g.lot_number = count.lot_number
where c.delflag = 0
and b.delflag = 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,?) `, formatSQL, t, hospitalLocalId).Scan(&money)
	} else {
		err = db.QueryRow(`select 
ifnull(convert(sum(c.quantity*g.costmoney) / 1000000, decimal(11,3)), 0) as money
from his_consumption as c
left join his_bill as b on b.id = c.his_bill_id
left join g_stock as g on g.con_commodity_id = c.con_commodity_id
where c.delflag = 0
and b.delflag = 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,?) `, formatSQL, t, hospitalLocalId).Scan(&money)
	}

	return money, err
}

// 次卡充值
func MeterCardRecharge(db *sql.DB, t, formatSQL string, employeeID, hospitalLocalId int) (float64, error) {

	sqlStr := `select ifnull(convert(sum(d.money) / 1000, decimal(11,3)), 0) 
from his_metercard_detail as d
where d.delflag = 0
and d.eventtype = 2
and DATE_FORMAT(d.eventtime,?) = ?
and d.sys_hospital_id in (0,?) `

	if employeeID != 0 {
		sqlStr += fmt.Sprintf(` and d.recharge_employee_id = %v`, employeeID)
	}

	var money float64
	err := db.QueryRow(sqlStr, formatSQL, t, hospitalLocalId).Scan(&money)

	return money, err
}

// 会员卡充值
func ConsumerCardRecharge(db *sql.DB, t, formatSQL string) (float64, error) {
	var money float64
	err := db.QueryRow(`select 
ifnull(convert(sum(d.money) / 1000, decimal(11,3)), 0) as money
from his_card_detail as d
where d.delflag = 0
and d.eventtype = 3
and DATE_FORMAT(d.eventtime, ?) = ?`, formatSQL, t).Scan(&money)

	return money, err
}

// 押金充值
func DepositMoneyRecharge(db *sql.DB, t, formatSQL string) (float64, error) {
	var money float64
	err := db.QueryRow(`select 
ifnull(convert(sum(d.money) / 1000, decimal(11,3)), 0) as money
from his_deposit_detail as d
where d.delflag = 0
and d.eventtype = 1
and DATE_FORMAT(d.eventtime, ?) = ?`, formatSQL, t).Scan(&money)

	return money, err
}

// 已结账单中: 现金、支付宝、微信、银行卡 (会员卡、次卡、押金结算 不算其中)
func BillOfSettlementIncome(db *sql.DB, t, formatSQL string) (float64, error) {
	var money float64
	err := db.QueryRow(`select ifnull(convert(sum(p.money) / 1000, decimal(11,3)), 0) 
from his_bill as b
left join his_bill_pay as p on p.his_bill_id = b.id
where b.delflag = 0
and p.delflag = 0
and b.billtype in (1,4,6,7)
and p.paytype in (1,2,3,4,10)
and DATE_FORMAT(b.billtime,?) = ?`, formatSQL, t).Scan(&money)

	return money, err
}

// 销售出库单结算的总金额
func StockOutBill(db *sql.DB, t, formatSQL string) (float64, error) {
	var money float64
	err := db.QueryRow(`select 
ifnull(convert(sum(b.paymoney) / 1000, decimal(11,3)), 0) as money
from g_stockout_bill as b
where b.delflag = 0
and DATE_FORMAT(b.eventtime, ?) = ?`, formatSQL, t).Scan(&money)

	return money, err
}

// 采购退货单结算的总金额
func StockRetreatBill(db *sql.DB, t, formatSQL string) (float64, error) {
	var money float64
	err := db.QueryRow(`select 
ifnull(convert(sum(b.paymoney) / 1000, decimal(11,3)), 0) as money
from g_stockretreat_bill as b
where b.delflag = 0
and DATE_FORMAT(b.eventtime, ?) = ?`, formatSQL, t).Scan(&money)

	return money, err
}

// 预收款
func PreInFundBill(db *sql.DB, t, formatSQL string) (float64, error) {
	var money float64
	err := db.QueryRow(`select 
ifnull(convert(sum(p.money) / 1000, decimal(11,3)), 0) as money
from c_prein_fund as p
where p.delflag = 0
and p.type = 0
and DATE_FORMAT(p.createtime, ?) = ?`, formatSQL, t).Scan(&money)

	return money, err
}

// 退预收款
func RetreatPreInFundBill(db *sql.DB, t, formatSQL string) (float64, error) {
	var money float64
	err := db.QueryRow(`select 
ifnull(convert(sum(p.money) / 1000, decimal(11,3)), 0) as money
from c_prein_fund as p
where p.delflag = 0
and p.type = 1
and DATE_FORMAT(p.createtime, ?) = ?`, formatSQL, t).Scan(&money)

	return money, err
}

// 支出管理 - 收入单
func InChargeBill(db *sql.DB, t, formatSQL string) (float64, error) {
	var money float64
	err := db.QueryRow(`select 
ifnull(convert(sum(i.money) / 1000, decimal(11,3)), 0) as money
from c_incharge as i
where i.delflag = 0
and DATE_FORMAT(i.happentime, ?) = ?`, formatSQL, t).Scan(&money)

	return money, err
}

// 退押金
func RetreatDepositMoney(db *sql.DB, t, formatSQL string, hospitalLocalId int) (float64, error) {
	var money float64
	err := db.QueryRow(`select 
ifnull(convert(sum(d.money) / 1000, decimal(11,3)), 0) as money
from his_deposit_detail as d
where d.delflag = 0
and d.eventtype in (3)
and DATE_FORMAT(d.eventtime, ?) = ? 
and d.sys_hospital_id in (0,?)  `, formatSQL, t, hospitalLocalId).Scan(&money)

	return money, err
}

// 退会员卡

func RetreatConsumerCard(db *sql.DB, t, formatSQL string, hospitalLocalId int) (float64, error) {
	var money float64
	err := db.QueryRow(`select 
ifnull(convert(sum(d.money) / 1000, decimal(11,3)), 0) as money
from his_card_detail as d
where d.delflag = 0
and d.eventtype in (7,13)
and DATE_FORMAT(d.eventtime, ?) = ?
and d.sys_hospital_id in (0,?)  `, formatSQL, t, hospitalLocalId).Scan(&money)

	return money, err
}

// 退次卡

func RetreatMeterCard(db *sql.DB, t, formatSQL string, hospitalLocalId int) (float64, error) {
	var money float64
	err := db.QueryRow(`select 
ifnull(convert(sum(d.money) / 1000, decimal(11,3)), 0) as money
from his_metercard_detail as d
where d.delflag = 0
and d.eventtype in (4)
and DATE_FORMAT(d.eventtime, ?) = ?
and d.sys_hospital_id in (0,?)  `, formatSQL, t, hospitalLocalId).Scan(&money)

	return money, err
}

// 支出管理 - 支出单
func OutChargeBill(db *sql.DB, t, formatSQL string, hospitalLocalId int) (float64, error) {
	var money float64
	err := db.QueryRow(`select 
ifnull(convert(sum(o.money) / 1000, decimal(11,3)), 0) as money
from c_outcharge as o
where o.delflag = 0
and DATE_FORMAT(o.happentime, ?) = ?
and o.sys_hospital_id in (0,?)  `, formatSQL, t, hospitalLocalId).Scan(&money)

	return money, err
}

// 采购入库单结算的总金额
func StockInBill(db *sql.DB, t, formatSQL string, hospitalLocalId int) (float64, error) {
	var money float64
	err := db.QueryRow(`select 
ifnull(convert(sum(b.paymoney) / 1000, decimal(11,3)), 0) as money
from g_stockin_bill as b
where b.delflag = 0
and DATE_FORMAT(b.eventtime, ?) = ?
and b.sys_hospital_id in (0,?) `, formatSQL, t, hospitalLocalId).Scan(&money)

	return money, err
}

// 预付款
func PreOutFundBill(db *sql.DB, t, formatSQL string, hospitalLocalId int) (float64, error) {
	var money float64
	err := db.QueryRow(`select 
ifnull(convert(sum(p.money) / 1000, decimal(11,3)), 0) as money
from c_preout_fund as p
where p.delflag = 0
and p.type = 0
and DATE_FORMAT(p.createtime, ?) = ?
and p.sys_hospital_id in (0,?)  `, formatSQL, t, hospitalLocalId).Scan(&money)

	return money, err
}

// 退预付款
func RetreatPreOutFundBill(db *sql.DB, t, formatSQL string, hospitalLocalId int) (float64, error) {
	var money float64
	err := db.QueryRow(`select 
ifnull(convert(sum(p.money) / 1000, decimal(11,3)), 0) as money
from c_preout_fund as p
where p.delflag = 0
and p.type = 1
and DATE_FORMAT(p.createtime, ?) = ?
and p.sys_hospital_id in (0,?)  `, formatSQL, t, hospitalLocalId).Scan(&money)

	return money, err
}