Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func requestReload(rs *reseller.Reseller, ch *reseller.Channel, resellerMsg *model.Message, req *message.Reload, resp *message.Response) {
- checkResellerTrxID(ch.ID, req.ResellerTrxID)
- prod := product.GetProductByCode(req.ProductCode)
- if prod == nil {
- panic(`product not found`)
- }
- customerNumber := req.CustomerNumber
- if prod.OperatorID != 0 {
- customerNumber = util.FormatPhonenumber(req.CustomerNumber)
- op := db.Get(`SELECT * FROM operator WHERE id = ? AND (MATCH(prefix) AGAINST(?) OR prefix = '')`,
- prod.OperatorID, customerNumber[:5])
- if op == nil {
- panic(`prefix salah: ` + customerNumber[:5])
- }
- customerNumber = req.CustomerNumber
- if op.GetStringD(`prefix`) != `` {
- customerNumber = util.FormatPhonenumber(customerNumber)
- }
- }
- checkDuplicate(customerNumber, req.ProductCode)
- productSupplier := supplier.GetPriorityProductSupplier(prod)
- if productSupplier == nil {
- panic(`supplier not found`)
- }
- if productSupplier.Code != nil {
- req.ProductCode = *productSupplier.Code
- }
- supp := supplier.GetByID(productSupplier.SupplierID)
- if supp == nil {
- panic(`supplier not found`)
- } else if supp.Type == `` {
- panic(`Incorrect supplier type`)
- }
- connector := GetConnector(supp.Type)
- if connector == nil {
- logger.W(`Supplier`, supp.Type, `belum disupport.`)
- panic(`supplier not found`)
- }
- connector.setParameters(rs, supp, productSupplier)
- useStock, masterPrice, supplierPrice, resellerPrice, trx := useCredit(prod, productSupplier, rs)
- inv := invoice.NewInvoice(ch.ID, prod.ID, rs.ResellerGroupID, supp.ID, req.ResellerTrxID,
- customerNumber, masterPrice, resellerPrice, supplierPrice)
- if trx != nil {
- trx.UpdateRefID(inv.ID)
- }
- resellerMsg.UpdateInvoiceID(inv.ID)
- respCh := make(chan interface{})
- timeoutCh := make(chan bool)
- go func() {
- select {
- case <-timeoutCh:
- return
- case <-time.After(30 * time.Second):
- respCh <- &supplier.Response{ Status: supplier.ResponseTimeout }
- return
- }
- }()
- go func() {
- defer func() {
- if r := recover(); r != nil {
- switch msg := r.(type) {
- case string:
- respCh <- msg
- case error:
- respCh <- msg.Error()
- case int:
- respCh <- strconv.Itoa(msg)
- }
- respCh <- nil
- }
- }()
- resp := connector.Reload(req, inv)
- inv.UpdateSupplierResponse(constant.LogTypeBuy, resp)
- respCh <- resp
- timeoutCh <- true
- }()
- receivedResp := <-respCh
- refund := false
- if val, ok := receivedResp.(string); ok {
- refund = true
- logger.E(val)
- }
- suppResp, ok := receivedResp.(*supplier.Response)
- msg := ``
- resp.Status = 99
- if !ok {
- refund = true
- logger.W(`no supplier response`)
- } else {
- switch suppResp.Status {
- case supplier.ResponseDuplicate:
- msg = formatMessage.Get(`MSG_RELOAD_DUPLICATE`)
- refund = true
- case supplier.ResponseFailed:
- msg = formatMessage.Get(`MSG_RELOAD_FAILED`)
- refund = true
- case supplier.ResponseNoStock:
- msg = formatMessage.Get(`MSG_RELOAD_NOSTOCK`)
- refund = true
- case supplier.ResponseTimeout:
- msg = formatMessage.Get(`MSG_RELOAD_PENDING`)
- case supplier.ResponseSuccess:
- msg = formatMessage.Get(`MSG_RELOAD_OK`)
- }
- logger.D(`supp resp status:`, suppResp.Status)
- }
- invID := strconv.Itoa(inv.ID)
- msg = strings.Replace(msg, `[serial_number]`, inv.SerialNumber, -1)
- msg = strings.Replace(msg, `[customer]`, inv.CustomerNumber, -1)
- msg = strings.Replace(msg, `[price]`, strconv.Itoa(int(resellerPrice)), -1)
- msg = strings.Replace(msg, `[invoice_id]`, invID, -1)
- if prod.Code != `` {
- msg = strings.Replace(msg, `[product]`, prod.Code, -1)
- }
- if useStock {
- stock := db.Get(`SELECT quantity FROM stock WHERE product_id = ? AND reseller_id = ?`, prod.ID, rs.ID)
- msg = strings.Replace(msg, `[balance]`, stock.GetStringD(`quantity`), -1)
- } else {
- msg = strings.Replace(msg, `[balance]`, strconv.Itoa(int(rs.GetBalance())), -1)
- }
- resp.Message = msg
- resp.PartnerTrxID = req.ResellerTrxID
- if refund {
- if useStock {
- rs.AddStock(prod.ID, 1)
- } else {
- rs.AddBalance(constant.TransactionBuyRevert, resellerPrice, inv.ID)
- }
- inv.Failed()
- } else {
- if suppResp != nil && suppResp.Status == supplier.ResponseSuccess {
- inv.Success()
- resp.Status = 0
- resp.SerialNumber = inv.SerialNumber
- resp.TrxID = &invID
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement