Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Jul 19th, 2018 66 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. func (t *TrackManagementChain) CreateScheme(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  2.     //args[0]=schemeName
  3.     //args[1]=startDate
  4.     //args[2]=endDate
  5.     //args[3]=payoutType
  6.     //args[4]=sizeType
  7.     //args[5]=slabCount
  8.     //args[slabcount+6->(slabcount*3)+6]=payoutStartLimit
  9.     //args[slabcount+7->(slabcount*3)+7]=payoutEndLimit
  10.     //args[(slabcount*3)+8]=affectingUsersCount(let 3)
  11.     //args[(slabcount*3)+9]=usertype1
  12.     //args[(slabcount*3)+10]=userType2
  13.     //args[(slabcount*3)+11]=userType3
  14.     OemAddress, err := getAccountAddress(stub)
  15.     if err != nil {
  16.         return shim.Error("Chaincode:scheme:CreateScheme::Couldnt get address of the OEM")
  17.     }
  18.     if len(OemAddress) == 0 {
  19.         return shim.Error("Chaincode:scheme:CreateScheme:: get address id null of the OEM")
  20.     }
  21.  
  22.     Scheme := scheme{}
  23.     txid := stub.GetTxID()
  24.     Scheme.SchemeId = txid
  25.     if len(args[0]) == 0 {
  26.         return shim.Error("Chaincode:scheme:CreateScheme:: Name of the scheme can't be empty")
  27.     }
  28.     Scheme.Name = args[0]
  29.  
  30.     if len(args[1]) == 0 || len(args[2]) == 0 {
  31.         return shim.Error("Chaincode:scheme:CreateScheme:: Tenure can't be blank")
  32.     }
  33.  
  34.     var str = "T23:59:59+00:00"
  35.     time1 := args[1] + str
  36.     time2 := args[2] + str
  37.     tenureEnd, err := time.Parse(time.RFC3339, time2)
  38.     if err != nil {
  39.         return shim.Error("Chaincode:scheme:CreateScheme:: Couldnt Parse Date")
  40.     }
  41.     tenureStart, err := time.Parse(time.RFC3339, time1)
  42.     if err != nil {
  43.         return shim.Error("Chaincode:scheme:CreateScheme:: Couldnt Parse Date")
  44.     }
  45.  
  46.     diff := tenureStart.Before(tenureEnd)
  47.     if diff {
  48.  
  49.         tenure := tenure{}
  50.         tenure.StartDate = args[1]
  51.         tenure.EndDate = args[2]
  52.         Scheme.Tenure = tenure
  53.  
  54.         if len(args[3]) == 0 {
  55.             return shim.Error("Chaincode:scheme:CreateScheme::Couldnt read payout type")
  56.         }
  57.         Scheme.PayoutType = args[3]
  58.  
  59.         if len(args[4]) == 0 {
  60.             return shim.Error("Chaincode:scheme:CreateScheme::Couldnt read Sizetype")
  61.         }
  62.  
  63.         Scheme.Sizetype = args[4]
  64.  
  65.         if len(args[5]) == 0 || len(args[6]) == 0 || len(args[7]) == 0 {
  66.             return shim.Error("Chaincode:scheme:CreateScheme::Couldnt read Sizetype")
  67.         }
  68.  
  69.        
  70.  
  71.         slabCount,err:=strconv.Atoi(args[5])
  72.         if err!=nil {
  73.             return shim.Error("Chaincode:scheme:CreateScheme::Couldnt typecaste")
  74.         }
  75.         var i int
  76.         for i=6;i<6+slabCount*3; {
  77.  
  78.        
  79.             Slab := slab{}
  80.             payOutAmount, err := strconv.ParseFloat(args[i], 64)
  81.             if err!=nil {
  82.                 return shim.Error("Chaincode:scheme:CreateScheme::Expecting amount float")
  83.             }
  84.             i++
  85.             payoutStartLimit, err := strconv.Atoi(args[i])
  86.             if err!=nil {
  87.                 return shim.Error("Chaincode:scheme:CreateScheme::Expecting payout start limit")
  88.             }
  89.             i++
  90.             payoutEndLimit, err := strconv.Atoi(args[i])
  91.             if err!=nil {
  92.                 return shim.Error("Chaincode:scheme:CreateScheme::Expecting payout end limit")
  93.             }
  94.             i++
  95.  
  96.             if payoutEndLimit <= payoutStartLimit {
  97.                 return shim.Error("Chaincode:scheme:CreateScheme::ENTER CORRECT LIMIT")
  98.             }
  99.             Slab.Payout = payOutAmount
  100.             Slab.StartLimit = payoutStartLimit
  101.             Slab.EndLimit = payoutEndLimit
  102.             Scheme.Slabs =append(Scheme.Slabs,Slab)
  103.         }
  104.  
  105.        
  106.         userCount, err := strconv.Atoi(args[i])
  107.         if err != nil {
  108.             return shim.Error("Chaincode:scheme:CreateScheme::Couldnt type cast count")
  109.         }
  110.         i++
  111.         if userCount == 0 {
  112.             return shim.Error("Chaincode:scheme:CreateScheme::Affecting users can't be blank")
  113.         }
  114.         var arr []string
  115.         j := i
  116.         for j = i; j < i+userCount; j++ {
  117.             arr = append(arr, args[j])
  118.         }
  119.        
  120.  
  121.         Scheme.ApplicableFor = arr
  122.  
  123.         SchemeAsBytes, err := json.Marshal(Scheme)
  124.         if err != nil {
  125.             return shim.Error("Chaincode:scheme:CreateScheme::Couldnt marshal scheme")
  126.         }
  127.         err = stub.PutState(Scheme.SchemeId, SchemeAsBytes)
  128.         if err != nil {
  129.             return shim.Error("Chaincode:scheme:CreateScheme::Couldnt putstate scheme")
  130.         }
  131.  
  132.         OEMIndexKey, err := stub.CreateCompositeKey(OEMkey, []string{"OEM", Scheme.SchemeId})
  133.         if err != nil {
  134.             return shim.Error(err.Error())
  135.         }
  136.         value := []byte(Scheme.SchemeId)
  137.         err = stub.PutState(OEMIndexKey, value)
  138.         if err != nil {
  139.             return shim.Error("Chaincode:scheme:CreateScheme::Couldnt putstate warehouse scheme")
  140.         }
  141.  
  142.         for k := 0; k < len(arr); k++ {
  143.             if arr[k] == "distributor" {
  144.  
  145.                 userNameIndexKey, err := stub.CreateCompositeKey(DistributerKey, []string{arr[k], Scheme.SchemeId})
  146.                 if err != nil {
  147.                     return shim.Error(err.Error())
  148.                 }
  149.  
  150.                 value := []byte(Scheme.SchemeId)
  151.                 err = stub.PutState(userNameIndexKey, value)
  152.                 if err != nil {
  153.                     return shim.Error("Chaincode:scheme:CreateScheme::Couldnt putstate Distributer scheme")
  154.                 }
  155.  
  156.             } else if arr[k] == "subdistributor" {
  157.                 userNameIndexKey, err := stub.CreateCompositeKey(SubDistributerKey, []string{arr[k], Scheme.SchemeId})
  158.                 if err != nil {
  159.                     return shim.Error(err.Error())
  160.                 }
  161.                 value := []byte(Scheme.SchemeId)
  162.                 err = stub.PutState(userNameIndexKey, value)
  163.                 if err != nil {
  164.                     return shim.Error("Chaincode:scheme:CreateScheme::Couldnt putstate SubDistributer scheme")
  165.                 }
  166.  
  167.             } else if arr[k] == "wholeseller" {
  168.                 userNameIndexKey, err := stub.CreateCompositeKey(WholeSellerKey, []string{arr[k], Scheme.SchemeId})
  169.                 if err != nil {
  170.                     return shim.Error(err.Error())
  171.                 }
  172.                 value := []byte(Scheme.SchemeId)
  173.                 err = stub.PutState(userNameIndexKey, value)
  174.                 if err != nil {
  175.                     return shim.Error("Chaincode:scheme:CreateScheme::Couldnt putstate WholeSeller scheme")
  176.                 }
  177.  
  178.             } else if arr[k] == "retailer" {
  179.                 userNameIndexKey, err := stub.CreateCompositeKey(Retailerkey, []string{arr[k], Scheme.SchemeId})
  180.                 if err != nil {
  181.                     return shim.Error(err.Error())
  182.                 }
  183.                 value := []byte(Scheme.SchemeId)
  184.                 err = stub.PutState(userNameIndexKey, value)
  185.                 if err != nil {
  186.                     return shim.Error("Chaincode:scheme:CreateScheme::Couldnt putstate Retailer scheme")
  187.                 }
  188.  
  189.             }else {
  190.                 return shim.Error("Chaincode:scheme:CreateScheme::No Proper Participant Type Given.")
  191.             }
  192.         }
  193.  
  194.     } else {
  195.         return shim.Error("Chaincode:scheme:CreateScheme::Start Date Can't be before End-date")
  196.     }
  197.  
  198.     return shim.Success(nil)
  199. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top