Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.31 KB | None | 0 0
  1. func requestReload(rs *reseller.Reseller, ch *reseller.Channel, resellerMsg *model.Message, req *message.Reload, resp *message.Response) {
  2.     checkResellerTrxID(ch.ID, req.ResellerTrxID)
  3.  
  4.     prod := product.GetProductByCode(req.ProductCode)
  5.     if prod == nil {
  6.         panic(`product not found`)
  7.     }
  8.  
  9.     customerNumber := req.CustomerNumber
  10.     if prod.OperatorID != 0 {
  11.         customerNumber = util.FormatPhonenumber(req.CustomerNumber)
  12.         op := db.Get(`SELECT * FROM operator WHERE id = ? AND (MATCH(prefix) AGAINST(?) OR prefix = '')`,
  13.             prod.OperatorID, customerNumber[:5])
  14.         if op == nil {
  15.             panic(`prefix salah: ` + customerNumber[:5])
  16.         }
  17.         customerNumber = req.CustomerNumber
  18.  
  19.         if op.GetStringD(`prefix`) != `` {
  20.             customerNumber = util.FormatPhonenumber(customerNumber)
  21.         }
  22.     }
  23.  
  24.     checkDuplicate(customerNumber, req.ProductCode)
  25.  
  26.     productSupplier := supplier.GetPriorityProductSupplier(prod)
  27.     if productSupplier == nil {
  28.         panic(`supplier not found`)
  29.     }
  30.     if productSupplier.Code != nil {
  31.         req.ProductCode = *productSupplier.Code
  32.     }
  33.     supp := supplier.GetByID(productSupplier.SupplierID)
  34.     if supp == nil {
  35.         panic(`supplier not found`)
  36.     } else if supp.Type == `` {
  37.         panic(`Incorrect supplier type`)
  38.     }
  39.  
  40.     connector := GetConnector(supp.Type)
  41.     if connector == nil {
  42.         logger.W(`Supplier`, supp.Type, `belum disupport.`)
  43.         panic(`supplier not found`)
  44.     }
  45.  
  46.     connector.setParameters(rs, supp, productSupplier)
  47.  
  48.     useStock, masterPrice, supplierPrice, resellerPrice, trx := useCredit(prod, productSupplier, rs)
  49.  
  50.     inv := invoice.NewInvoice(ch.ID, prod.ID, rs.ResellerGroupID, supp.ID, req.ResellerTrxID,
  51.         customerNumber, masterPrice, resellerPrice, supplierPrice)
  52.  
  53.     if trx != nil {
  54.         trx.UpdateRefID(inv.ID)
  55.     }
  56.     resellerMsg.UpdateInvoiceID(inv.ID)
  57.  
  58.     respCh := make(chan interface{})
  59.     timeoutCh := make(chan bool)
  60.  
  61.     go func() {
  62.         select {
  63.         case <-timeoutCh:
  64.             return
  65.         case <-time.After(30 * time.Second):
  66.             respCh <- &supplier.Response{ Status: supplier.ResponseTimeout }
  67.             return
  68.         }
  69.     }()
  70.  
  71.     go func() {
  72.         defer func() {
  73.             if r := recover(); r != nil {
  74.                 switch msg := r.(type) {
  75.                 case string:
  76.                     respCh <- msg
  77.                 case error:
  78.                     respCh <- msg.Error()
  79.                 case int:
  80.                     respCh <- strconv.Itoa(msg)
  81.                 }
  82.                 respCh <- nil
  83.             }
  84.         }()
  85.         resp := connector.Reload(req, inv)
  86.         inv.UpdateSupplierResponse(constant.LogTypeBuy, resp)
  87.         respCh <- resp
  88.         timeoutCh <- true
  89.     }()
  90.     receivedResp := <-respCh
  91.  
  92.     refund := false
  93.  
  94.     if val, ok := receivedResp.(string); ok {
  95.         refund = true
  96.         logger.E(val)
  97.     }
  98.     suppResp, ok := receivedResp.(*supplier.Response)
  99.  
  100.     msg := ``
  101.     resp.Status = 99
  102.     if !ok {
  103.         refund = true
  104.         logger.W(`no supplier response`)
  105.     } else {
  106.         switch suppResp.Status {
  107.         case supplier.ResponseDuplicate:
  108.             msg = formatMessage.Get(`MSG_RELOAD_DUPLICATE`)
  109.             refund = true
  110.         case supplier.ResponseFailed:
  111.             msg = formatMessage.Get(`MSG_RELOAD_FAILED`)
  112.             refund = true
  113.         case supplier.ResponseNoStock:
  114.             msg = formatMessage.Get(`MSG_RELOAD_NOSTOCK`)
  115.             refund = true
  116.         case supplier.ResponseTimeout:
  117.             msg = formatMessage.Get(`MSG_RELOAD_PENDING`)
  118.         case supplier.ResponseSuccess:
  119.             msg = formatMessage.Get(`MSG_RELOAD_OK`)
  120.         }
  121.         logger.D(`supp resp status:`, suppResp.Status)
  122.     }
  123.  
  124.     invID := strconv.Itoa(inv.ID)
  125.  
  126.     msg = strings.Replace(msg, `[serial_number]`, inv.SerialNumber, -1)
  127.     msg = strings.Replace(msg, `[customer]`, inv.CustomerNumber, -1)
  128.     msg = strings.Replace(msg, `[price]`, strconv.Itoa(int(resellerPrice)), -1)
  129.     msg = strings.Replace(msg, `[invoice_id]`, invID, -1)
  130.  
  131.     if prod.Code != `` {
  132.         msg = strings.Replace(msg, `[product]`, prod.Code, -1)
  133.     }
  134.     if useStock {
  135.         stock := db.Get(`SELECT quantity FROM stock WHERE product_id = ? AND reseller_id = ?`, prod.ID, rs.ID)
  136.         msg = strings.Replace(msg, `[balance]`, stock.GetStringD(`quantity`), -1)
  137.     } else {
  138.         msg = strings.Replace(msg, `[balance]`, strconv.Itoa(int(rs.GetBalance())), -1)
  139.     }
  140.  
  141.     resp.Message = msg
  142.     resp.PartnerTrxID = req.ResellerTrxID
  143.     if refund {
  144.         if useStock {
  145.             rs.AddStock(prod.ID, 1)
  146.         } else {
  147.             rs.AddBalance(constant.TransactionBuyRevert, resellerPrice, inv.ID)
  148.         }
  149.         inv.Failed()
  150.     } else {
  151.         if suppResp != nil && suppResp.Status == supplier.ResponseSuccess   {
  152.             inv.Success()
  153.             resp.Status = 0
  154.             resp.SerialNumber = inv.SerialNumber
  155.             resp.TrxID = &invID
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement