contract_invoice.go 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. package fms
  2. import (
  3. "fmt"
  4. "hongze/fms_api/global"
  5. "hongze/fms_api/models/base"
  6. "hongze/fms_api/utils"
  7. "time"
  8. )
  9. // ContractInvoice 合同开票表
  10. type ContractInvoice struct {
  11. ContractInvoiceId int `gorm:"primaryKey;column:contract_invoice_id" json:"contract_invoice_id" description:"开票ID"`
  12. ContractRegisterId int `gorm:"column:contract_register_id" json:"contract_register_id" description:"登记ID"`
  13. ContractCode string `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
  14. Amount float64 `gorm:"column:amount" json:"amount" description:"换算后的金额(人民币)"`
  15. OriginAmount float64 `gorm:"column:origin_amount" json:"origin_amount" description:"开票/到款金额"`
  16. CurrencyUnit string `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
  17. InvoiceType int `gorm:"column:invoice_type" json:"invoice_type" description:"类型: 1-开票登记; 2-到款登记"`
  18. InvoiceDate time.Time `gorm:"column:invoice_time" json:"invoice_time" description:"开票日期/到款月"`
  19. SellerId int `gorm:"column:seller_id" json:"seller_id" description:"销售ID"`
  20. SellerName string `gorm:"column:seller_name" json:"seller_name" description:"销售名称"`
  21. SellerGroupId int `gorm:"column:seller_group_id" json:"seller_group_id" description:"销售分组ID"`
  22. SellerGroupName string `gorm:"column:seller_group_name" json:"seller_group_name" description:"销售分组名称"`
  23. SellerTeamId int `gorm:"column:seller_team_id" json:"seller_team_id" description:"销售小组ID"`
  24. SellerTeamName string `gorm:"column:seller_team_name" json:"seller_team_name" description:"销售小组名称"`
  25. PayType int `gorm:"column:pay_type" json:"pay_type" description:"付款方式:0-无;1-年付;2-半年付;3-季付;4-次付;5-异常"`
  26. AdminId int `gorm:"column:admin_id" json:"admin_id" description:"操作人ID"`
  27. AdminName string `gorm:"column:admin_name" json:"admin_name" description:"操作人姓名"`
  28. Remark string `gorm:"column:remark" json:"remark" description:"备注信息"`
  29. IsDeleted int `gorm:"column:is_deleted" json:"is_deleted" description:"是否已删除: 0-正常; 1-已删除"`
  30. ServiceProductId int `gorm:"column:service_product_id" json:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
  31. IsPrePay int `gorm:"column:is_pre_pay" json:"is_pre_pay" description:"是否预付款: 0-不是; 1-是"`
  32. StartDate time.Time `gorm:"column:start_date" json:"start_date" description:"约定开始时间"`
  33. EndDate time.Time `gorm:"column:end_date" json:"end_date" description:"约定结束时间"`
  34. PreRegisterId int `gorm:"column:pre_register_id" json:"pre_register_id" description:"预登记ID"`
  35. base.TimeBase
  36. }
  37. func (c *ContractInvoice) TableName() string {
  38. return "contract_invoice"
  39. }
  40. // ContractInvoiceItem 合同开票/到款
  41. type ContractInvoiceItem struct {
  42. ContractInvoiceId int `gorm:"column:contract_invoice_id" json:"contract_invoice_id" description:"开票ID"`
  43. ContractRegisterId int `gorm:"column:contract_register_id" json:"contract_register_id" description:"登记ID"`
  44. ContractCode string `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
  45. ContractStatus int `json:"contract_status" description:"合同状态: 1-已审批; 2-单章寄出; 3-已签回"`
  46. Amount float64 `gorm:"column:amount" json:"amount" description:"换算金额"`
  47. OriginAmount float64 `gorm:"column:origin_amount" json:"origin_amount" description:"开票/到款金额"`
  48. CurrencyUnit string `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
  49. UnitName string `json:"unit_name" description:"货币单位名称"`
  50. InvoiceType int `gorm:"column:invoice_type" json:"invoice_type" description:"类型: 1-开票登记; 2-到款登记;3-开票预登记;4-到款预登记"`
  51. InvoiceDate string `gorm:"column:invoice_time" json:"invoice_time" description:"开票日期/到款月"`
  52. SellerId int `gorm:"column:seller_id" json:"seller_id" description:"销售ID"`
  53. SellerName string `gorm:"column:seller_name" json:"seller_name" description:"销售名称"`
  54. PayType int `gorm:"column:pay_type" json:"pay_type" description:"付款方式:0-无;1-年付;2-半年付;3-季付;4-次付;5-异常"`
  55. Remark string `gorm:"column:remark" json:"remark" description:"备注信息"`
  56. ServiceProductId int `gorm:"column:service_product_id" json:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
  57. IsPrePay int `gorm:"column:is_pre_pay" json:"is_pre_pay" description:"是否预付款: 0-不是; 1-是"`
  58. StartDate string `gorm:"column:start_date" json:"start_date" description:"约定开始时间"`
  59. EndDate string `gorm:"column:end_date" json:"end_date" description:"约定结束时间"`
  60. CreateTime string `gorm:"column:create_time" json:"create_time" description:"创建时间"`
  61. CompanyName string `gorm:"column:company_name" json:"company_name" description:"客户名称"`
  62. }
  63. func (c *ContractInvoice) Create() (err error) {
  64. err = global.DEFAULT_MYSQL.Create(c).Error
  65. return
  66. }
  67. func (c *ContractInvoice) AddInBatches(list []*ContractInvoice) (err error) {
  68. err = global.DEFAULT_MYSQL.CreateInBatches(list, len(list)).Error
  69. return
  70. }
  71. func (c *ContractInvoice) Update(updateCols []string) (err error) {
  72. err = global.DEFAULT_MYSQL.Model(c).Select(updateCols).Updates(c).Error
  73. return
  74. }
  75. func (c *ContractInvoice) Fetch(id int) (item *ContractInvoice, err error) {
  76. err = global.DEFAULT_MYSQL.Model(c).Where("is_deleted = 0 AND contract_invoice_id = ?", id).First(&item).Error
  77. return
  78. }
  79. // 删除
  80. func (c *ContractInvoice) Delete() (err error) {
  81. err = global.DEFAULT_MYSQL.Delete(c).Error
  82. return
  83. }
  84. func (c *ContractInvoice) Sum(field, condition string, pars []interface{}) (total float64, err error) {
  85. totalList := make([]float64, 0)
  86. err = global.DEFAULT_MYSQL.Table("contract_invoice AS a").Joins("INNER JOIN contract_register AS b ON a.contract_register_id = b.contract_register_id ").
  87. Where("a.is_deleted = 0").
  88. Where(condition, pars...).
  89. Pluck(field, &totalList).Error
  90. for i := range totalList {
  91. total += totalList[i]
  92. }
  93. return
  94. }
  95. func (c *ContractInvoice) List(condition string, pars []interface{}, orderRule string) (list []*ContractInvoice, err error) {
  96. list = make([]*ContractInvoice, 0)
  97. query := global.DEFAULT_MYSQL.Model(c).
  98. Where("is_deleted = 0").
  99. Where(condition, pars...)
  100. if orderRule != "" {
  101. query.Order(orderRule)
  102. } else {
  103. query.Order("contract_invoice_id ASC")
  104. }
  105. err = query.Find(&list).Error
  106. return
  107. }
  108. func (c *ContractInvoice) PageList(page base.IPage, condition string, pars []interface{}) (count int64, results []*ContractInvoice, err error) {
  109. results = make([]*ContractInvoice, 0)
  110. query := global.DEFAULT_MYSQL.Model(c).
  111. Where("is_deleted = 0").
  112. Where(condition, pars...)
  113. query.Count(&count)
  114. if len(page.GetOrderItemsString()) > 0 {
  115. query = query.Order(page.GetOrderItemsString())
  116. }
  117. err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  118. return
  119. }
  120. // DeleteAndCreateNewInvoice 删除并新增登记
  121. func (c *ContractInvoice) DeleteAndCreateNewInvoice(contractRegisterId, invoiceType int, deleteInvoiceIds []int, invoices []*ContractInvoice) (err error) {
  122. tx := global.DEFAULT_MYSQL.Begin()
  123. defer func() {
  124. if err != nil {
  125. tx.Rollback()
  126. } else {
  127. tx.Commit()
  128. }
  129. }()
  130. if len(deleteInvoiceIds) > 0 {
  131. err = tx.Model(c).
  132. Where("contract_register_id = ? AND invoice_type = ? AND contract_invoice_id IN (?)", contractRegisterId, invoiceType, deleteInvoiceIds).
  133. UpdateColumn("is_deleted", 1).Error
  134. if err != nil {
  135. return
  136. }
  137. }
  138. if len(invoices) > 0 {
  139. err = tx.CreateInBatches(invoices, len(invoices)).Error
  140. if err != nil {
  141. return
  142. }
  143. }
  144. return
  145. }
  146. // ContractInvoiceSaveReq 合同开票-请求体
  147. type ContractInvoiceSaveReq struct {
  148. ContractRegisterId int `json:"contract_register_id" binding:"required,gte=1" description:"登记ID"`
  149. InvoiceType int `json:"invoice_type" binding:"oneof=1 2 3" description:"类型: 1-开票登记; 2-到款登记; 3-预到款登记"`
  150. AmountList []*ContractInvoiceSaveItem `json:"amount_list"`
  151. }
  152. // ContractInvoiceSaveItem 合同开票数据
  153. type ContractInvoiceSaveItem struct {
  154. ServiceProductId int `json:"service_product_id" binding:"oneof=1 2" description:"套餐类型:1ficc套餐,2权益套餐"`
  155. InvoiceId int `json:"invoice_id" description:"开票ID"`
  156. OriginAmount float64 `json:"origin_amount" description:"开票(到款)金额"`
  157. Amount float64 `json:"amount" description:"换算后的金额"`
  158. InvoiceDate string `json:"invoice_date" description:"开票日期/到款月"`
  159. Remark string `json:"remark" description:"备注"`
  160. SellerId int `json:"seller_id" description:"销售ID"`
  161. }
  162. // GetContractInvoiceItemList 获取开票到款列表
  163. func GetContractInvoiceItemList(condition string, pars []interface{}) (itemList []*ContractInvoiceItem, err error) {
  164. list := make([]*ContractInvoice, 0)
  165. err = global.DEFAULT_MYSQL.Model(ContractInvoice{}).
  166. Where("is_deleted = 0").
  167. Where(condition, pars...).
  168. Order("contract_invoice_id ASC").
  169. Find(&list).Error
  170. if err != nil {
  171. return
  172. }
  173. itemList = formatContractInvoice2ItemList(list)
  174. return
  175. }
  176. // formatContractInvoice2ItemList 格式化ContractInvoice
  177. func formatContractInvoice2ItemList(list []*ContractInvoice) (itemList []*ContractInvoiceItem) {
  178. itemList = make([]*ContractInvoiceItem, 0)
  179. for i := range list {
  180. itemList = append(itemList, &ContractInvoiceItem{
  181. ContractInvoiceId: list[i].ContractInvoiceId,
  182. ContractRegisterId: list[i].ContractRegisterId,
  183. ContractCode: list[i].ContractCode,
  184. Amount: list[i].Amount,
  185. OriginAmount: list[i].OriginAmount,
  186. CurrencyUnit: list[i].CurrencyUnit,
  187. InvoiceType: list[i].InvoiceType,
  188. InvoiceDate: utils.TimeTransferString(utils.FormatDate, list[i].InvoiceDate),
  189. SellerId: list[i].SellerId,
  190. SellerName: list[i].SellerName,
  191. PayType: list[i].PayType,
  192. Remark: list[i].Remark,
  193. ServiceProductId: list[i].ServiceProductId,
  194. IsPrePay: list[i].IsPrePay,
  195. StartDate: list[i].StartDate.Format(utils.FormatDate),
  196. EndDate: list[i].StartDate.Format(utils.FormatDate),
  197. CreateTime: utils.TimeTransferString(utils.FormatDateTime, list[i].CreateTime),
  198. })
  199. }
  200. return
  201. }
  202. // ContractInvoiceListReq 合同开票/到款登记列表请求体
  203. type ContractInvoiceListReq struct {
  204. InvoiceType int `json:"invoice_type" form:"invoice_type" binding:"oneof=1 2" description:"类型: 1-开票登记; 2-到款登记"`
  205. SellerIds string `json:"seller_ids" form:"seller_ids" description:"销售ID, 多个id用逗号拼接"`
  206. Keyword string `json:"keyword" form:"keyword" binding:"omitempty" description:"搜索关键字"`
  207. StartDate string `json:"start_date" form:"start_date" binding:"omitempty,datetime=2006-01-02" description:"开始日期"`
  208. EndDate string `json:"end_date" form:"end_date" binding:"omitempty,datetime=2006-01-02" description:"结束日期"`
  209. MinAmount float64 `json:"min_amount" form:"min_amount" description:"开票金额区间-最小值"`
  210. MaxAmount float64 `json:"max_amount" form:"max_amount" description:"开票金额区间-最大值"`
  211. IsExport int `json:"is_export" form:"is_export" description:"是否导出: 0-否; 1-是"`
  212. ServiceProductId int `json:"service_product_id" form:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
  213. base.PageReq
  214. }
  215. // GetContractInvoiceItemPageList 获取合同开票/到款列表-分页
  216. func GetContractInvoiceItemPageList(page base.IPage, condition string, pars []interface{}) (count int64, results []*ContractInvoiceItem, err error) {
  217. list := make([]*ContractInvoiceRespItem, 0)
  218. query := global.DEFAULT_MYSQL.Table("contract_invoice AS a").
  219. Select("a.*, b.company_name").
  220. Joins("JOIN contract_register b ON a.contract_register_id = b.contract_register_id").
  221. Where("a.is_deleted = 0").
  222. Where(condition, pars...)
  223. query.Count(&count)
  224. if len(page.GetOrderItemsString()) > 0 {
  225. query = query.Order(page.GetOrderItemsString())
  226. }
  227. err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&list).Error
  228. if err != nil {
  229. return
  230. }
  231. for i := range list {
  232. results = append(results, formatContractInvoice2Item(list[i]))
  233. }
  234. return
  235. }
  236. // formatContractInvoice2Item 格式化ContractInvoiceItem
  237. func formatContractInvoice2Item(item *ContractInvoiceRespItem) (formatItem *ContractInvoiceItem) {
  238. formatItem = new(ContractInvoiceItem)
  239. formatItem.ContractInvoiceId = item.ContractInvoiceId
  240. formatItem.ContractRegisterId = item.ContractRegisterId
  241. formatItem.ContractCode = item.ContractCode
  242. formatItem.Amount = item.Amount
  243. formatItem.OriginAmount = item.OriginAmount
  244. formatItem.CurrencyUnit = item.CurrencyUnit
  245. formatItem.InvoiceType = item.InvoiceType
  246. formatItem.InvoiceDate = utils.TimeTransferString(utils.FormatDate, item.InvoiceDate)
  247. formatItem.SellerId = item.SellerId
  248. formatItem.SellerName = item.SellerName
  249. formatItem.PayType = item.PayType
  250. formatItem.Remark = item.Remark
  251. formatItem.IsPrePay = item.IsPrePay
  252. formatItem.ServiceProductId = item.ServiceProductId
  253. formatItem.CreateTime = utils.TimeTransferString(utils.FormatDateTime, item.CreateTime)
  254. formatItem.CompanyName = item.CompanyName
  255. return
  256. }
  257. // DeleteContractInvoicesByRegisterId 根据合同登记ID删除开票/到款记录
  258. func DeleteContractInvoicesByRegisterId(registerId int) (err error) {
  259. sql := `UPDATE contract_invoice SET is_deleted = 1 WHERE contract_register_id = ?`
  260. err = global.DEFAULT_MYSQL.Exec(sql, registerId).Error
  261. return
  262. }
  263. // InvoicePaymentCensusListReq 商品到款统计列表请求体
  264. type InvoicePaymentCensusListReq struct {
  265. Keyword string `json:"keyword" form:"keyword" binding:"omitempty" description:"关键词"`
  266. SellerIds string `json:"seller_ids" form:"seller_ids" description:"销售ID,多个ID之间用逗号拼接"`
  267. ServiceTypes string `json:"service_types" form:"service_types" description:"套餐类型, 多个套餐之间用逗号拼接"`
  268. StartDate string `json:"start_date" form:"start_date" binding:"omitempty,datetime=2006-01-02" description:"开始日期"`
  269. EndDate string `json:"end_date" form:"end_date" binding:"omitempty,datetime=2006-01-02" description:"结束日期"`
  270. HasInvoice string `json:"has_invoice" form:"has_invoice" description:"是否已开票: 0-否; 1-是; 空-全部"`
  271. HasPayment string `json:"has_payment" form:"has_payment" description:"是否已到款: 0-否; 1-是; 空-全部"`
  272. IsExport int `json:"is_export" form:"is_export" description:"是否导出: 0-否; 1-是"`
  273. ListParam int `json:"list_param" form:"list_param" description:"套餐类型: 0-全部; 1-ficc; 2-权益"`
  274. SortParam string `json:"sort_param" form:"sort_param" description:"排序字段参数,用来排序的字段, 枚举值:'invoice_time':开票日 、 'payment_date':到款日"`
  275. SortType string `json:"sort_type" form:"sort_type" description:"如何排序,是正序还是倒序,枚举值:asc 正序,desc 倒叙"`
  276. TimeType int `json:"time_type" form:"time_type" description:"时间类型: 1-开票时间; 2-到款时间;3-开票时间&到款时间"`
  277. base.PageReq
  278. }
  279. // GetInvoicePaymentCensusPageListV2 获取商品到款统计列表-分页
  280. func GetInvoicePaymentCensusPageListV2(page base.IPage, condition string, pars []interface{}) (results []*ContractRegister, registerIds []int, err error) {
  281. query := global.DEFAULT_MYSQL.Table("contract_invoice AS a").
  282. Select("b.*").
  283. Joins("JOIN contract_register AS b ON a.contract_register_id = b.contract_register_id").
  284. Where("a.is_deleted = 0 AND b.is_deleted = 0").
  285. Where(condition, pars...).
  286. Group("b.contract_register_id")
  287. if len(page.GetOrderItemsString()) > 0 {
  288. query = query.Order(page.GetOrderItemsString())
  289. }
  290. err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  291. if err != nil {
  292. return
  293. }
  294. // 计数
  295. queryCount := global.DEFAULT_MYSQL.Table("contract_invoice AS a").
  296. Select("b.contract_register_id").
  297. Joins("JOIN contract_register AS b ON a.contract_register_id = b.contract_register_id").
  298. Where("a.is_deleted = 0 AND b.is_deleted = 0").
  299. Where(condition, pars...).
  300. Group("b.contract_register_id")
  301. queryCount.Find(&registerIds)
  302. return
  303. }
  304. // InvoicePaymentCensusResp 商品到款统计响应体
  305. type InvoicePaymentCensusResp struct {
  306. DataList []*InvoicePaymentCensusItem `json:"data_list"`
  307. InvoiceTotal float64 `json:"invoice_total" description:"开票总金额(换算后)"`
  308. PaymentTotal float64 `json:"payment_total" description:"到款总金额(换算后)"`
  309. AmountTotal float64 `json:"amount_total" description:"合计总金额(换算后)"`
  310. InvoiceCurrencyTotal []*InvoiceListCurrencyTotal `json:"invoice_currency_total" description:"开票-分币种总金额"`
  311. PaymentCurrencyTotal []*InvoiceListCurrencyTotal `json:"payment_currency_total" description:"到款-分币种总金额"`
  312. }
  313. // InvoicePaymentCensusItem 商品到款统计信息
  314. type InvoicePaymentCensusItem struct {
  315. SummaryId int `json:"summary_id" description:"汇总ID"`
  316. ContractRegisterId int `json:"contract_register_id" description:"登记ID"`
  317. CompanyName string `json:"company_name" description:"客户名称"`
  318. NewCompany int `json:"new_company" description:"是否为新客户: 0-否; 1-是"`
  319. StartDate string `json:"start_date" description:"合同开始日期"`
  320. EndDate string `json:"end_date" description:"合同结束日期"`
  321. ServicesName string `json:"services_name" description:"套餐信息字符串拼接"`
  322. ContractType int `json:"contract_type" description:"合同类型: 0-无 1-新签; 2-续约"`
  323. ActualPayCompanies string `json:"actual_pay_companies" description:"该合同关联的所有代付方, 英文逗号拼接"`
  324. InvoicePaymentList []*InvoicePaymentCensusInfo `json:"invoice_payment_list" description:"开票到款列表"`
  325. ContractCode string `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
  326. ContractAmount float64 `gorm:"column:contract_amount" json:"contract_amount" description:"合同金额"`
  327. UnitName string `gorm:"unit_name" json:"unit_name" description:"单位名称"`
  328. ProductIds string `gorm:"column:product_ids" json:"product_ids" description:"产品ID:1-FICC; 2-权益, 如果两者都有,则用英文逗号拼接"`
  329. NotInvoiceTotal float64 `json:"not_invoice_total" description:"未开票总金额(换算后)"`
  330. }
  331. // InvoicePaymentCensusInfo 开票到款统计信息
  332. type InvoicePaymentCensusInfo struct {
  333. InvoiceId int `json:"invoice_id" description:"开票ID"`
  334. InvoiceDate string `json:"invoice_time" description:"开票日期"`
  335. InvoiceAmount float64 `json:"invoice_amount" description:"开票换算金额"`
  336. InvoiceOriginAmount float64 `json:"invoice_origin_amount" description:"开票原始金额"`
  337. SellerId int `json:"seller_id" description:"销售ID"`
  338. SellerName string `json:"seller_name" description:"销售名称"`
  339. SellerGroupId int `json:"seller_group_id" description:"销售组别ID"`
  340. SellerGroupName string `json:"seller_group_name" description:"销售组别名称"`
  341. PaymentId int `json:"payment_id" description:"到款ID"`
  342. PaymentDate string `json:"payment_date" description:"到款日期"`
  343. PaymentAmount float64 `json:"payment_amount" description:"到款换算金额"`
  344. PaymentOriginAmount float64 `json:"payment_origin_amount" description:"到款原始金额"`
  345. PayType int `json:"pay_type" description:"付款方式:0-无;1-年付;2-半年付;3-季付;4-次付;5-异常"`
  346. ServiceAmountList []*ContractPaymentServiceAmountItem `json:"service_amount_list" description:"到款套餐金额分配信息"`
  347. SellerType string `json:"seller_type" description:"销售类型:1ficc销售,2权益销售"`
  348. }
  349. // ContractInvoiceAmountTotal 开票到款金额合计信息
  350. type ContractInvoiceAmountTotal struct {
  351. InvoiceType int `json:"invoice_type" description:"类型: 1-开票; 2-到款"`
  352. TotalAmount float64 `json:"total_amount" description:"金额合计"`
  353. }
  354. // GetContractInvoiceAmountTotal 获取开票到款金额合计信息
  355. func GetContractInvoiceAmountTotal(condition string, pars []interface{}) (results []*ContractInvoiceAmountTotal, err error) {
  356. query := global.DEFAULT_MYSQL.Table("contract_invoice").
  357. Select("invoice_type, SUM(amount) AS total_amount").
  358. Where("is_deleted = 0").
  359. Where(condition, pars...).
  360. Group("invoice_type")
  361. err = query.Find(&results).Error
  362. return
  363. }
  364. // UpdatePaymentPayTypeReq 到款登记-修改付款方式请求体
  365. type UpdatePaymentPayTypeReq struct {
  366. ContractPaymentId int `json:"contract_payment_id" binding:"required,gte=1" description:"到款登记ID"`
  367. PayType int `json:"pay_type" binding:"oneof=1 2 3 4" description:"付款方式: 1-年付; 2-半年付; 3-季付; 4-次付"`
  368. }
  369. // CensusSellerGroupInvoiceListReq 销售组开票统计列表请求体
  370. type CensusSellerGroupInvoiceListReq struct {
  371. StartDate string `json:"start_date" form:"start_date" binding:"omitempty,datetime=2006-01-02" description:"开始日期"`
  372. EndDate string `json:"end_date" form:"end_date" binding:"omitempty,datetime=2006-01-02" description:"结束日期"`
  373. SortField int `json:"sort_field" form:"sort_field" description:"排序字段: 1-开票金额; 2-组别占比"`
  374. SortType int `json:"sort_type" form:"sort_type" description:"排序方式: 1-正序; 2-倒序"`
  375. IsExport int `json:"is_export" form:"is_export" description:"是否导出: 0-否; 1-是"`
  376. SellerType int `json:"seller_type" form:"seller_type" description:"销售类型:1ficc销售,2权益销售"`
  377. CompanyType int `json:"company_type" form:"company_type" description:"客户类型 0全部 1新客户 2老客户"`
  378. SellerIds string `json:"seller_ids" form:"seller_ids" description:"销售IDs"`
  379. base.PageReq
  380. }
  381. // CensusSellerGroupInvoiceItem 销售组开票统计数据
  382. type CensusSellerGroupInvoiceItem struct {
  383. GroupId int `json:"group_id" description:"分组ID"`
  384. GroupName string `json:"group_name" description:"分组名称"`
  385. InvoiceAmount float64 `json:"invoice_amount" description:"开票金额"`
  386. GroupRate float64 `json:"group_rate" description:"分组占比"`
  387. }
  388. // GetCensusSellerGroupInvoicePageList 获取销售组开票统计列表-分页
  389. func GetCensusSellerGroupInvoicePageList(page base.IPage, condition, outCond string, pars []interface{}, amountSum float64) (results []*CensusSellerGroupInvoiceItem, err error) {
  390. tableName := `weekly_report.sys_group`
  391. if global.CONFIG.Serve.RunMode == "debug" {
  392. tableName = `test_v2_weekly_report.sys_group`
  393. }
  394. sql := `SELECT a.group_id, a.group_name, b.invoice_amount, b.group_rate FROM %s AS a LEFT JOIN `
  395. sql += ` (
  396. SELECT
  397. seller_group_id,
  398. seller_group_name,
  399. SUM(amount) AS invoice_amount,
  400. SUM(amount) / %f AS group_rate
  401. FROM
  402. contract_invoice
  403. WHERE
  404. %s
  405. GROUP BY
  406. seller_group_id
  407. ) AS b ON a.group_id = b.seller_group_id`
  408. sql += ` WHERE %s ORDER BY %s LIMIT %d,%d`
  409. sql = fmt.Sprintf(sql, tableName, amountSum, condition, outCond, page.GetOrderItemsString(), page.Offset(), page.GetPageSize())
  410. err = global.DEFAULT_MYSQL.Raw(sql, pars...).Scan(&results).Error
  411. return
  412. }
  413. // CensusSellerInvoiceListReq 销售开票统计列表请求体
  414. type CensusSellerInvoiceListReq struct {
  415. SellerIds string `json:"seller_ids" form:"seller_ids" description:"销售IDs"`
  416. StartDate string `json:"start_date" form:"start_date" binding:"omitempty,datetime=2006-01-02" description:"开始日期"`
  417. EndDate string `json:"end_date" form:"end_date" binding:"omitempty,datetime=2006-01-02" description:"结束日期"`
  418. SortField int `json:"sort_field" form:"sort_field" description:"排序字段: 1-开票金额; 2-小组占比; 3-全员占比"`
  419. SortType int `json:"sort_type" form:"sort_type" description:"排序方式: 1-正序; 2-倒序"`
  420. IsExport int `json:"is_export" form:"is_export" description:"是否导出: 0-否; 1-是"`
  421. SellerType int `json:"seller_type" form:"seller_type" description:"销售类型:1ficc销售,2权益销售"`
  422. ShowResign bool `json:"show_resign" form:"show_resign" description:"是否显示离职销售"`
  423. CompanyType int `json:"company_type" form:"company_type" description:"客户类型 0全部 1新客户 2老客户"`
  424. base.PageReq
  425. }
  426. // CensusSellerInvoiceItem 销售组开票统计数据
  427. type CensusSellerInvoiceItem struct {
  428. SellerId int `json:"seller_id" description:"销售ID"`
  429. SellerName string `json:"seller_name" description:"销售名称"`
  430. GroupId int `json:"group_id" description:"分组ID"`
  431. GroupName string `json:"group_name" description:"分组名称"`
  432. InvoiceAmount float64 `json:"invoice_amount" description:"开票金额"`
  433. GroupRate float64 `json:"group_rate" description:"分组占比"`
  434. SellerRate float64 `json:"seller_rate" description:"全员占比"`
  435. }
  436. // GetCensusSellerInvoicePageList 获取销售开票统计列表-分页
  437. func GetCensusSellerInvoicePageList(page base.IPage, condition, outCond string, pars []interface{}, amountSum float64) (results []*CensusSellerInvoiceItem, err error) {
  438. tableName := `weekly_report.admin`
  439. if global.CONFIG.Serve.RunMode == "debug" {
  440. tableName = `test_v2_weekly_report.admin`
  441. }
  442. sql := `SELECT a.admin_id AS seller_id, a.real_name AS seller_name, a.group_id, b.invoice_amount, b.group_rate, b.seller_rate FROM %s AS a LEFT JOIN `
  443. sql += ` (
  444. SELECT c.seller_id, c.seller_group_id, SUM(c.amount) AS invoice_amount,
  445. SUM(c.amount) / (
  446. SELECT SUM(amount) FROM contract_invoice WHERE seller_group_id = c.seller_group_id AND %s
  447. ) AS group_rate,
  448. SUM(c.amount) / %f AS seller_rate
  449. FROM contract_invoice AS c
  450. WHERE %s
  451. GROUP BY c.seller_id
  452. ) AS b ON a.admin_id = b.seller_id `
  453. sql += ` WHERE %s ORDER BY %s LIMIT %d,%d`
  454. sql = fmt.Sprintf(sql, tableName, condition, amountSum, condition, outCond, page.GetOrderItemsString(), page.Offset(), page.GetPageSize())
  455. err = global.DEFAULT_MYSQL.Raw(sql, pars...).Scan(&results).Error
  456. return
  457. }
  458. // InvoiceListRespData 开票/到款列表响应体
  459. type InvoiceListRespData struct {
  460. Page *base.Page `json:"page"`
  461. List interface{} `json:"list"`
  462. AmountTotal float64 `json:"amount_total"`
  463. CurrencyTotal []*InvoiceListCurrencyTotal `json:"currency_total"`
  464. }
  465. // InvoiceListCurrencyTotal 开票/到款列表分币种合计信息
  466. type InvoiceListCurrencyTotal struct {
  467. Name string `json:"name" description:"货币名称"`
  468. UnitName string `json:"unit_name" description:"单位名称"`
  469. Code string `json:"code" description:"国际代码"`
  470. Amount float64 `json:"amount" description:"金额"`
  471. FlagImg string `json:"flag_img" description:"国旗图标"`
  472. }
  473. // InvoiceListCurrencySum 开票/到款列表分币种总和
  474. type InvoiceListCurrencySum struct {
  475. CurrencyUnit string `json:"currency_unit" description:"货币代码"`
  476. InvoiceType int `json:"invoice_type" description:"开票类型:1-开票; 2-到款"`
  477. AmountTotal float64 `json:"amount_total" description:"换算后合计金额"`
  478. OriginAmountTotal float64 `json:"origin_amount_total" description:"原合计金额"`
  479. }
  480. // GetInvoiceListCurrencySum 获取开票/到款分货币合计
  481. func GetInvoiceListCurrencySum(condition string, pars []interface{}, groupRule string) (results []*InvoiceListCurrencySum, err error) {
  482. query := global.DEFAULT_MYSQL.Table("contract_invoice AS a").
  483. Select("a.currency_unit, a.invoice_type, SUM(a.amount) AS amount_total, SUM(a.origin_amount) AS origin_amount_total, b.company_name").
  484. Joins("JOIN contract_register b ON a.contract_register_id = b.contract_register_id").
  485. Where("a.is_deleted = 0").
  486. Where(condition, pars...).
  487. Group(groupRule)
  488. err = query.Find(&results).Error
  489. return
  490. }
  491. // PreRegisterListReq 预登记列表请求体
  492. type PreRegisterListReq struct {
  493. Keyword string `json:"keyword" form:"keyword" binding:"omitempty" description:"关键词"`
  494. IsExport int `json:"is_export" form:"is_export" description:"是否导出: 0-否; 1-是"`
  495. base.PageReq
  496. }
  497. // ContractInvoiceDetailItem 预登记列表详情用-invoice_time换register_time
  498. type ContractInvoiceDetailItem struct {
  499. ContractInvoiceId int `gorm:"column:contract_invoice_id" json:"contract_invoice_id" description:"开票ID"`
  500. ContractRegisterId int `gorm:"column:contract_register_id" json:"contract_register_id" description:"登记ID"`
  501. ContractCode string `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
  502. ContractStatus int `json:"contract_status" description:"合同状态: 1-已审批; 2-单章寄出; 3-已签回"`
  503. OriginAmount float64 `gorm:"column:amount" json:"amount" description:"开票/到款金额"`
  504. CurrencyUnit string `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
  505. UnitName string `json:"unit_name" description:"货币单位名称"`
  506. InvoiceType int `gorm:"column:invoice_type" json:"invoice_type" description:"类型: 1-开票登记; 2-到款登记;3-开票预登记;4-到款预登记"`
  507. InvoiceDate string `gorm:"column:register_date" json:"register_date" description:"开票日期/到款月"`
  508. SellerId int `gorm:"column:seller_id" json:"seller_id" description:"销售ID"`
  509. SellerName string `gorm:"column:seller_name" json:"seller_name" description:"销售名称"`
  510. PayType int `gorm:"column:pay_type" json:"pay_type" description:"付款方式:0-无;1-年付;2-半年付;3-季付;4-次付;5-异常"`
  511. Remark string `gorm:"column:remark" json:"remark" description:"备注信息"`
  512. ServiceProductId int `gorm:"column:service_product_id" json:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
  513. IsPrePay int `gorm:"column:is_pre_pay" json:"is_pre_pay" description:"是否预付款: 0-不是; 1-是"`
  514. StartDate string `gorm:"column:start_date" json:"start_date" description:"约定开始时间"`
  515. EndDate string `gorm:"column:end_date" json:"end_date" description:"约定结束时间"`
  516. CreateTime string `gorm:"column:create_time" json:"create_time" description:"创建时间"`
  517. PreRegisterId int `gorm:"column:pre_register_id" json:"pre_register_id" description:"预登记ID"`
  518. HasInvoiceSeller int `json:"has_invoice_seller" description:"对应的开票记录有没有销售 0没有 1有"`
  519. }
  520. type InvoiceDetailItem struct {
  521. ContractInvoice
  522. HasInvoiceSeller int `json:"has_invoice_seller" description:"对应的开票记录有没有销售 0没有 1有"`
  523. }
  524. // GetContractInvoiceDetailItemList 预登记列表详情用-获取开票到款列表
  525. func GetContractInvoiceDetailItemList(condition string, pars []interface{}) (itemList []*ContractInvoiceDetailItem, err error) {
  526. list := make([]*InvoiceDetailItem, 0)
  527. err = global.DEFAULT_MYSQL.Table("contract_invoice AS a").
  528. Select("a.*,IF(p.seller_id > 0, 1, 0) AS has_invoice_seller ").
  529. Joins("JOIN contract_pre_register AS p ON p.invoice_id = a.contract_invoice_id OR p.arrive_id = a.contract_invoice_id ").
  530. Where("is_deleted = 0").
  531. Where(condition, pars...).
  532. Order("contract_invoice_id ASC").
  533. Scan(&list).Error
  534. if err != nil {
  535. return
  536. }
  537. itemList = formatContractInvoice2DetailItemList(list)
  538. return
  539. }
  540. // formatContractInvoice2ItemList 预登记列表详情用-格式化ContractInvoice
  541. func formatContractInvoice2DetailItemList(list []*InvoiceDetailItem) (itemList []*ContractInvoiceDetailItem) {
  542. itemList = make([]*ContractInvoiceDetailItem, 0)
  543. for i := range list {
  544. itemList = append(itemList, &ContractInvoiceDetailItem{
  545. ContractInvoiceId: list[i].ContractInvoiceId,
  546. ContractRegisterId: list[i].ContractRegisterId,
  547. ContractCode: list[i].ContractCode,
  548. OriginAmount: list[i].OriginAmount,
  549. CurrencyUnit: list[i].CurrencyUnit,
  550. InvoiceType: list[i].InvoiceType,
  551. InvoiceDate: utils.TimeTransferString(utils.FormatDate, list[i].InvoiceDate),
  552. SellerId: list[i].SellerId,
  553. SellerName: list[i].SellerName,
  554. PayType: list[i].PayType,
  555. Remark: list[i].Remark,
  556. ServiceProductId: list[i].ServiceProductId,
  557. IsPrePay: list[i].IsPrePay,
  558. StartDate: list[i].StartDate.Format(utils.FormatDate),
  559. EndDate: list[i].StartDate.Format(utils.FormatDate),
  560. CreateTime: utils.TimeTransferString(utils.FormatDateTime, list[i].CreateTime),
  561. PreRegisterId: list[i].PreRegisterId,
  562. HasInvoiceSeller: list[i].HasInvoiceSeller,
  563. })
  564. }
  565. return
  566. }
  567. type ContractInvoiceRespItem struct {
  568. ContractInvoice
  569. CompanyName string `gorm:"column:company_name" json:"company_name" description:"客户名称"`
  570. }
  571. func (c *ContractInvoice) FetchByRegisterId(registerId int) (items []*ContractInvoice, err error) {
  572. err = global.DEFAULT_MYSQL.Model(c).Where("is_deleted = 0 AND contract_register_id = ?", registerId).Scan(&items).Error
  573. return
  574. }
  575. // GetDuplicateContractInvoiceDetailItemListWithType 预登记列表详情用-获取开票到款列表
  576. func GetDuplicateContractInvoiceDetailItemListWithType(companyName, startDate, endDate string, contractType, hasPayment int) (itemList []*ContractInvoiceDetailItem, err error) {
  577. list := make([]*InvoiceDetailItem, 0)
  578. err = global.DEFAULT_MYSQL.Table("contract_invoice as a ").
  579. Select(" a.*,IF(p.seller_id > 0, 1, 0) AS has_invoice_seller ").
  580. Joins(" JOIN contract_register as b ON a.contract_register_id = b.contract_register_id ").
  581. Joins("JOIN contract_pre_register AS p ON p.invoice_id = a.contract_invoice_id OR p.arrive_id = a.contract_invoice_id ").
  582. Where(fmt.Sprintf("a.is_deleted = 0 AND b.company_name = '%s' AND b.start_date= '%s' AND b.end_date= '%s'"+
  583. " AND b.is_deleted=0 AND (a.invoice_type=3 OR a.invoice_type=4) "+
  584. "AND b.contract_type=? AND b.has_payment=?", companyName, startDate, endDate), contractType, hasPayment).
  585. Order("a.contract_invoice_id ASC").
  586. Find(&list).Error
  587. if err != nil {
  588. return
  589. }
  590. itemList = formatContractInvoice2DetailItemList(list)
  591. return
  592. }
  593. func GetContractInvoiceByRegisterIdAndType(registerId int) (items *ContractInvoice, err error) {
  594. err = global.DEFAULT_MYSQL.Model(ContractInvoice{}).Where("is_deleted = 0 AND contract_register_id = ? AND invoice_type = 2", registerId).Order("create_time DESC").First(&items).Error
  595. return
  596. }
  597. type DupInvoice struct {
  598. ContractInvoice
  599. TemplateIds string
  600. }
  601. // GetDuplicateContractInvoiceDetailItemList 预登记列表详情用-获取开票到款列表
  602. func GetDuplicateContractInvoiceDetailItemList(companyName, startDate, endDate string, contractType, hasPayment int) (list []*DupInvoice, err error) {
  603. err = global.DEFAULT_MYSQL.Table("contract_invoice as a ").
  604. Select(" a.*,GROUP_CONCAT(DISTINCT c.service_template_id ORDER BY c.service_template_id ) AS template_ids ").
  605. Joins(" JOIN contract_register as b ON a.contract_register_id = b.contract_register_id ").
  606. Joins("JOIN contract_service AS c ON a.contract_register_id = c.contract_register_id").
  607. Where(fmt.Sprintf("a.is_deleted = 0 AND b.company_name = '%s' AND b.start_date= '%s' AND b.end_date= '%s' AND b.is_deleted=0 AND (a.invoice_type=3 OR a.invoice_type=4) AND b.contract_type=? AND b.has_payment=?", companyName, startDate, endDate), contractType, hasPayment).
  608. Group("a.contract_invoice_id").
  609. Order("a.contract_invoice_id ASC").
  610. Find(&list).Error
  611. if err != nil {
  612. return
  613. }
  614. return
  615. }
  616. type IncomeChartListReq struct {
  617. SellerIds string `json:"seller_ids" form:"seller_ids" description:"销售ID,多个ID之间用逗号拼接"`
  618. StartDate string `json:"start_date" form:"start_date" binding:"omitempty,datetime=2006-01" description:"开始日期"`
  619. EndDate string `json:"end_date" form:"end_date" binding:"omitempty,datetime=2006-01" description:"结束日期"`
  620. IsExport int `json:"is_export" form:"is_export" description:"是否导出: 0-否; 1-是"`
  621. ListParam string `json:"list_param" form:"list_param" description:"套餐类型: 0-月度; 1-季度; 2-半年度;3-年度;4-月度累计"`
  622. CompanyType int `json:"company_type" form:"company_type" description:"客户类型 0全部 1新客户 2老客户 3未续约"`
  623. ServiceTypes string `json:"service_types" form:"service_types" description:"套餐类型"`
  624. IncomeType int `json:"income_type" form:"income_type" description:"收入类型 0开票收入 1到款收入"`
  625. }
  626. // 获取最新的开票或到款日期
  627. func GetLatestIncome(condition string, pars []interface{}) (result *ContractInvoice, err error) {
  628. sql := `SELECT
  629. a.*
  630. FROM
  631. contract_invoice AS a
  632. INNER JOIN contract_register AS b ON a.contract_register_id = b.contract_register_id
  633. WHERE
  634. a.is_deleted = 0 `
  635. sql += condition
  636. sql += `ORDER BY a.invoice_time DESC `
  637. err = global.DEFAULT_MYSQL.Raw(sql, pars...).First(&result).Error
  638. return
  639. }
  640. // NotPaymentCensusItem 商品到款统计信息
  641. type NotPaymentCensusItem struct {
  642. SummaryId int `json:"summary_id" description:"汇总ID"`
  643. ContractRegisterId int `json:"contract_register_id" description:"登记ID"`
  644. CompanyName string `json:"company_name" description:"客户名称"`
  645. NewCompany int `json:"new_company" description:"是否为新客户: 0-否; 1-是"`
  646. StartDate string `json:"start_date" description:"合同开始日期"`
  647. EndDate string `json:"end_date" description:"合同结束日期"`
  648. ServicesName string `json:"services_name" description:"套餐信息字符串拼接"`
  649. ContractType int `json:"contract_type" description:"合同类型: 0-无 1-新签; 2-续约"`
  650. ActualPayCompanies string `json:"actual_pay_companies" description:"该合同关联的所有代付方, 英文逗号拼接"`
  651. InvoicePaymentList []*NotPaymentCensusInfo `json:"invoice_payment_list" description:"开票到款列表"`
  652. ContractCode string `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
  653. ContractAmount float64 `gorm:"column:contract_amount" json:"contract_amount" description:"合同金额"`
  654. UnitName string `gorm:"unit_name" json:"unit_name" description:"单位名称"`
  655. ProductIds string `gorm:"column:product_ids" json:"product_ids" description:"产品ID:1-FICC; 2-权益, 如果两者都有,则用英文逗号拼接"`
  656. NotInvoiceTotal float64 `json:"not_invoice_total" description:"未开票总金额(换算后)"`
  657. NotInvoiceList []*NotInvoicePaymentSummaryItem `json:"not_invoice_list" description:"未开票invoice列表"`
  658. }
  659. // NotPaymentCensusInfo 开票到款统计信息
  660. type NotPaymentCensusInfo struct {
  661. InvoiceId int `json:"invoice_id" description:"开票ID"`
  662. InvoiceDate string `json:"invoice_time" description:"开票日期"`
  663. InvoiceAmount float64 `json:"invoice_amount" description:"开票换算金额"`
  664. InvoiceOriginAmount float64 `json:"invoice_origin_amount" description:"开票原始金额"`
  665. SellerId int `json:"seller_id" description:"销售ID"`
  666. SellerName string `json:"seller_name" description:"销售名称"`
  667. SellerGroupId int `json:"seller_group_id" description:"销售组别ID"`
  668. SellerGroupName string `json:"seller_group_name" description:"销售组别名称"`
  669. PaymentId int `json:"payment_id" description:"到款ID"`
  670. PaymentDate string `json:"payment_date" description:"到款日期"`
  671. PaymentAmount float64 `json:"payment_amount" description:"到款换算金额"`
  672. PaymentOriginAmount float64 `json:"payment_origin_amount" description:"到款原始金额"`
  673. PayType int `json:"pay_type" description:"付款方式:0-无;1-年付;2-半年付;3-季付;4-次付;5-异常"`
  674. ServiceAmountList []*ContractPaymentServiceAmountItem `json:"service_amount_list" description:"到款套餐金额分配信息"`
  675. SellerType int `json:"seller_type" description:"销售类型:1ficc销售,2权益销售"`
  676. NotPaymentOriginAmount float64 `json:"not_payment_origin_amount" description:"开票未到款金额"`
  677. NotPaymentAmount float64 `json:"not_payment_amount" description:"开票未到款换算金额"`
  678. ServiceProductId int `json:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
  679. UnitName string `json:"unit_name" description:"单位名称"`
  680. }
  681. // GetCensusSellerGroupInvoicePageListV2 获取销售组开票统计列表-分页
  682. func GetCensusSellerGroupInvoicePageListV2(page base.IPage, groupStr, condition string, pars []interface{}, amountSum float64) (count int, results []*CensusSellerGroupInvoiceItem, err error) {
  683. sql := `
  684. SELECT
  685. seller_group_id AS group_id,
  686. SUM(amount) AS invoice_amount,
  687. SUM(amount) / %f AS group_rate
  688. FROM
  689. contract_invoice AS a
  690. INNER JOIN contract_register AS b ON a.contract_register_id = b.contract_register_id
  691. WHERE
  692. %s
  693. AND seller_group_id IN (` + groupStr + `)
  694. GROUP BY
  695. seller_group_id ORDER BY %s LIMIT %d,%d
  696. `
  697. sqlCount := ` select count(1) as count from (SELECT *
  698. FROM
  699. contract_invoice AS a
  700. WHERE
  701. %s
  702. AND seller_group_id IN (` + groupStr + `)
  703. GROUP BY
  704. seller_group_id )as a
  705. `
  706. sqlCount = fmt.Sprintf(sqlCount, condition)
  707. err = global.DEFAULT_MYSQL.Raw(sqlCount, pars...).Find(&count).Error
  708. sql = fmt.Sprintf(sql, amountSum, condition, page.GetOrderItemsString(), page.Offset(), page.GetPageSize())
  709. err = global.DEFAULT_MYSQL.Raw(sql, pars...).Scan(&results).Error
  710. return
  711. }
  712. // GetCensusSellerInvoicePageList 获取销售开票统计列表-分页
  713. func GetCensusSellerInvoicePageListV2(page base.IPage, adminStr, inCond, condition string, pars []interface{}, amountSum float64) (count int, results []*CensusSellerInvoiceItem, err error) {
  714. //sql := `SELECT a.admin_id AS seller_id, a.real_name AS seller_name, a.group_id, b.invoice_amount, b.group_rate, b.seller_rate FROM %s AS a LEFT JOIN `
  715. sql := ` SELECT c.seller_id , SUM(c.amount) AS invoice_amount,
  716. SUM(c.amount) / (
  717. SELECT SUM(amount) FROM contract_invoice WHERE seller_group_id = c.seller_group_id AND %s
  718. ) AS group_rate,
  719. SUM(c.amount) / %f AS seller_rate
  720. FROM contract_invoice AS c
  721. INNER JOIN contract_register AS b ON c.contract_register_id = b.contract_register_id
  722. WHERE %s
  723. AND c.seller_id IN (` + adminStr + `)
  724. GROUP BY c.seller_id ORDER BY %s LIMIT %d,%d `
  725. sqlCount := ` select count(1) as count from (SELECT c.*
  726. FROM contract_invoice AS c
  727. INNER JOIN contract_register AS b ON c.contract_register_id = b.contract_register_id
  728. WHERE %s
  729. AND c.seller_id IN (` + adminStr + `) GROUP BY c.seller_id ) as a
  730. `
  731. sqlCount = fmt.Sprintf(sqlCount, condition)
  732. err = global.DEFAULT_MYSQL.Raw(sqlCount, pars...).Find(&count).Error
  733. //query := global.DEFAULT_MYSQL.Table("contract_invoice").
  734. // Where(condition, pars...).Where("AND seller_id IN (`+adminStr+`)")
  735. //query.Count(&count)
  736. sql = fmt.Sprintf(sql, inCond, amountSum, condition, page.GetOrderItemsString(), page.Offset(), page.GetPageSize())
  737. err = global.DEFAULT_MYSQL.Raw(sql, pars...).Scan(&results).Error
  738. return
  739. }
  740. // GetInvoiceListByFix
  741. func GetInvoiceListByFix() (results []*ContractInvoice, err error) {
  742. sql := `SELECT * FROM contract_invoice WHERE invoice_type = 2 AND seller_id = 0 `
  743. err = global.DEFAULT_MYSQL.Raw(sql).Find(&results).Error
  744. return
  745. }
  746. // GetDuplicateContractInvoiceDetailItemList 补录合同用-获取开票到款列表
  747. func GetDuplicateContractInvoiceDetailItemListForSupplement(companyName, startDate, endDate string, contractType, hasPayment int) (list []*DupInvoice, err error) {
  748. err = global.DEFAULT_MYSQL.Table("contract_invoice as a ").
  749. Select(" a.*,GROUP_CONCAT(DISTINCT c.service_template_id ORDER BY c.service_template_id ) AS template_ids ").
  750. Joins(" JOIN contract_register as b ON a.contract_register_id = b.contract_register_id ").
  751. Joins("JOIN contract_service AS c ON a.contract_register_id = c.contract_register_id").
  752. Where(fmt.Sprintf("a.is_deleted = 0 AND b.company_name = '%s' AND b.start_date= '%s' AND b.end_date= '%s' AND b.is_deleted=0 AND b.contract_type=? AND b.has_payment=?", companyName, startDate, endDate), contractType, hasPayment).
  753. Group("a.contract_register_id").
  754. Order("a.contract_invoice_id ASC").
  755. Find(&list).Error
  756. if err != nil {
  757. return
  758. }
  759. return
  760. }
  761. func GetInvoiceCountByRegisterId(registerId int) (total int64, err error) {
  762. err = global.DEFAULT_MYSQL.Table("contract_invoice as a ").
  763. Where("is_deleted = 0 AND contract_register_id = ?", registerId).Count(&total).Error
  764. return
  765. }
  766. type IncomeListReq struct {
  767. SellerIds string `json:"seller_ids" form:"seller_ids" description:"销售ID,多个ID之间用逗号拼接"`
  768. StartDate string `json:"start_date" form:"start_date" binding:"omitempty,datetime=2006-01" description:"开始日期"`
  769. EndDate string `json:"end_date" form:"end_date" binding:"omitempty,datetime=2006-01" description:"结束日期"`
  770. IsExport int `json:"is_export" form:"is_export" description:"是否导出: 0-否; 1-是"`
  771. ListParam string `json:"list_param" form:"list_param" description:"套餐类型: 0-月度; 1-季度; 2-半年度;3-年度;4-月度累计"`
  772. CompanyType int `json:"company_type" form:"company_type" description:"客户类型 0全部 1新客户 2老客户 3未续约"`
  773. ServiceTypes string `json:"service_types" form:"service_types" description:"套餐类型"`
  774. IncomeType int `json:"income_type" form:"income_type" description:"收入类型 0开票收入 1到款收入"`
  775. SortType string `json:"sort_type" form:"sort_type" description:"如何排序,是正序还是倒序,枚举值:asc 正序,desc 倒叙"`
  776. Keyword string `json:"keyword" form:"keyword" description:"搜索关键字"`
  777. base.PageReq
  778. }
  779. // 获取最新的到款日期
  780. func GetLatestPaymentIncome(condition string, pars []interface{}) (result *ContractInvoice, err error) {
  781. sql := `SELECT
  782. a.*
  783. FROM
  784. contract_invoice AS a
  785. INNER JOIN contract_register AS b ON a.contract_register_id = b.contract_register_id
  786. WHERE
  787. a.is_deleted = 0 AND a.invoice_type IN (2,4) `
  788. sql += condition
  789. sql += `ORDER BY a.invoice_time DESC `
  790. err = global.DEFAULT_MYSQL.Raw(sql, pars...).First(&result).Error
  791. return
  792. }
  793. // 获取最早的到款日期
  794. func GetEarliestPaymentIncome(condition string, pars []interface{}) (result *ContractInvoice, err error) {
  795. sql := `SELECT
  796. a.*
  797. FROM
  798. contract_invoice AS a
  799. INNER JOIN contract_register AS b ON a.contract_register_id = b.contract_register_id
  800. WHERE
  801. a.is_deleted = 0 AND a.invoice_type IN (2,4) `
  802. sql += condition
  803. sql += `ORDER BY a.invoice_time ASC `
  804. err = global.DEFAULT_MYSQL.Raw(sql, pars...).First(&result).Error
  805. return
  806. }