public static function AssignPlan($user_id, $plan_id, $price = NULL)
 {
     DB::transaction(function () use($user_id, $plan_id, $price) {
         $oldPlan = APActivePlan::where('user_id', $user_id)->first();
         if (!is_null($oldPlan)) {
             self::updatePlanHistory($oldPlan);
         } else {
             $oldPlan = new ApActivePlan();
         }
         $plan = Plan::findOrFail($plan_id);
         $newPlan = ['user_id' => $user_id, 'plan_name' => $plan->name, 'plan_type' => $plan->plan_type, 'policy_type' => $plan->policy_type, 'sim_sessions' => $plan->sim_sessions, 'interim_updates' => $plan->interim_updates, 'aq_invocked' => 0, 'price' => $plan->price, 'time_balance' => NULL, 'data_balance' => NULL, 'assigned_on' => date("Y-m-d H:i:s"), 'validity' => $plan->validity, 'validity_unit' => $plan->validity_unit];
         if ($price != NULL) {
             $newPlan['price'] = $price;
         }
         if ($plan->plan_type == LIMITED) {
             $limit = $plan->limit;
             if ($limit->limit_type == TIME_LIMIT || $limit->limit_type == BOTH_LIMITS) {
                 $newPlan['time_balance'] = $limit->time_limit * constant($limit->time_unit);
             }
             if ($limit->limit_type == DATA_LIMIT || $limit->limit_type == BOTH_LIMITS) {
                 $newPlan['data_balance'] = $limit->data_limit * constant($limit->data_unit);
             }
             if ($limit->aq_access == ALLOWED) {
                 $aq_policy = Policy::findOrFail($limit->aq_policy);
                 $limit->aq_policy = mikrotikRateLimit($aq_policy->toArray());
             }
             $planLimit = APLimit::create($limit->toArray());
             $newPlan['limit_id'] = $planLimit->id;
         }
         if ($plan->policy_type == 'Policy') {
             $policy = APPolicy::create(['bw_policy' => mikrotikRateLimit($plan->policy->toArray())]);
         } elseif ($plan->policy_type == 'PolicySchema') {
             $days = ['mo' => 'monday', 'tu' => 'tuesday', 'we' => 'wednesday', 'th' => 'thursday', 'fr' => 'friday', 'sa' => 'saturday', 'su' => 'sunday'];
             foreach ($days as $d) {
                 $tpl = $plan->policy->{$day}->toArray();
                 $types = ['bw_policy', 'pr_policy', 'sec_policy'];
                 foreach ($types as $t) {
                     if (!is_null($tpl[$t])) {
                         $policy = Policy::find($tpl[$t])->toArray();
                         $tpl[$t] = mikrotikRateLimit($policy);
                     }
                 }
                 $template = APPolicySchemaTemplate::create($tpl);
                 $schema[$d] = $template->id;
             }
             $policy = APPolicySchema::create($schema);
         }
         $newPlan['policy_id'] = $policy->id;
         $oldPlan->fill($newPlan);
         $oldPlan->save();
         return TRUE;
     });
 }
Ejemplo n.º 2
0
 public function activeServicePlan()
 {
     $plan = APActivePlan::where('user_id', $this->account->id)->first();
     return $plan->plan_name;
 }