| 
									
										
										
										
											2022-03-06 02:02:12 +11:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace App\Models; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-27 21:56:29 +08:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2022-03-06 02:02:12 +11:00
										 |  |  | use Illuminate\Database\Eloquent\Factories\HasFactory; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Model; | 
					
						
							| 
									
										
										
										
											2022-05-15 00:24:05 +10:00
										 |  |  | use Illuminate\Support\Facades\Cache; | 
					
						
							| 
									
										
										
										
											2022-03-06 02:02:12 +11:00
										 |  |  | use Illuminate\Support\Facades\DB; | 
					
						
							| 
									
										
										
										
											2022-06-27 21:56:29 +08:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2022-03-06 02:02:12 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Pricing extends Model | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     use HasFactory; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 23:49:18 +10:00
										 |  |  |     protected $table = 'pricings'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public $incrementing = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected $keyType = 'string'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-06 02:02:12 +11:00
										 |  |  |     protected $fillable = ['service_id', 'service_type', 'currency', 'price', 'term', 'as_usd', 'usd_per_month', 'next_due_date']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-27 21:56:29 +08:00
										 |  |  |     private static function refreshRates(): object | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (Cache::has("currency_rates")) { | 
					
						
							|  |  |  |             return Cache::get("currency_rates"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $response_json = file_get_contents("https://open.er-api.com/v6/latest/USD"); | 
					
						
							|  |  |  |         if (false === $response_json) { | 
					
						
							|  |  |  |             Log::error("do file_get_contents failed"); | 
					
						
							|  |  |  |             return (object)null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $response = json_decode($response_json); | 
					
						
							|  |  |  |             if ('success' === $response->result) { | 
					
						
							|  |  |  |                 return Cache::remember("currency_rates", now()->addWeek(1), function () use ($response) { | 
					
						
							|  |  |  |                     return $response->rates; | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             Log::error("server response is " . $response->result . ", expecting success"); | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             Log::error("failed to request v6.exchangerate-api.com", ['err' => $e]); | 
					
						
							| 
									
										
										
										
											2022-05-29 16:46:55 +10:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-06-27 21:56:29 +08:00
										 |  |  |         return (object)null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static function getRates($currency): float | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $rate = self::refreshRates()->$currency; | 
					
						
							| 
									
										
										
										
											2022-10-20 11:12:53 +11:00
										 |  |  |         return $rate ?? 1.00; | 
					
						
							| 
									
										
										
										
											2022-06-27 21:56:29 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function getCurrencyList(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return array_keys((array)self::refreshRates()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function convertFromUSD(string $amount, string $convert_to): float | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $amount * self::getRates($convert_to); | 
					
						
							| 
									
										
										
										
											2022-05-29 16:46:55 +10:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-06 02:02:12 +11:00
										 |  |  |     public function convertToUSD(string $amount, string $convert_from): float | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-06-27 21:56:29 +08:00
										 |  |  |         return $amount / self::getRates($convert_from); | 
					
						
							| 
									
										
										
										
											2022-03-06 02:02:12 +11:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function costAsPerMonth(string $cost, int $term): float | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($term === 1) { | 
					
						
							|  |  |  |             return $cost; | 
					
						
							|  |  |  |         } elseif ($term === 2) { | 
					
						
							|  |  |  |             return ($cost / 3); | 
					
						
							|  |  |  |         } elseif ($term === 3) { | 
					
						
							|  |  |  |             return ($cost / 6); | 
					
						
							|  |  |  |         } elseif ($term === 4) { | 
					
						
							|  |  |  |             return ($cost / 12); | 
					
						
							|  |  |  |         } elseif ($term === 5) { | 
					
						
							|  |  |  |             return ($cost / 24); | 
					
						
							|  |  |  |         } elseif ($term === 6) { | 
					
						
							|  |  |  |             return ($cost / 36); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return $cost; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function termAsMonths(int $term): int | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($term === 1) { | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         } elseif ($term === 2) { | 
					
						
							|  |  |  |             return 3; | 
					
						
							|  |  |  |         } elseif ($term === 3) { | 
					
						
							|  |  |  |             return 6; | 
					
						
							|  |  |  |         } elseif ($term === 4) { | 
					
						
							|  |  |  |             return 12; | 
					
						
							|  |  |  |         } elseif ($term === 5) { | 
					
						
							|  |  |  |             return 24; | 
					
						
							|  |  |  |         } elseif ($term === 6) { | 
					
						
							|  |  |  |             return 36; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return 62; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function deletePricing($id): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-18 19:33:26 +10:00
										 |  |  |         DB::table('pricings')->where('service_id', $id)->delete(); | 
					
						
							| 
									
										
										
										
											2022-03-06 02:02:12 +11:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-05-15 00:24:05 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-05 11:58:19 +11:00
										 |  |  |     public function insertPricing(int $type, string $service_id, string $currency, float $price, int $term, string $next_due_date, int $is_active = 1): Pricing | 
					
						
							| 
									
										
										
										
											2022-05-15 00:24:05 +10:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-08-30 23:37:05 +10:00
										 |  |  |         $as_usd = $this->convertToUSD($price, $currency); | 
					
						
							| 
									
										
										
										
											2022-05-15 00:24:05 +10:00
										 |  |  |         return self::create([ | 
					
						
							|  |  |  |             'service_type' => $type, | 
					
						
							| 
									
										
										
										
											2022-05-15 00:43:36 +10:00
										 |  |  |             'service_id' => $service_id, | 
					
						
							| 
									
										
										
										
											2022-05-15 00:24:05 +10:00
										 |  |  |             'currency' => $currency, | 
					
						
							|  |  |  |             'price' => $price, | 
					
						
							|  |  |  |             'term' => $term, | 
					
						
							|  |  |  |             'as_usd' => $as_usd, | 
					
						
							|  |  |  |             'usd_per_month' => $this->costAsPerMonth($as_usd, $term), | 
					
						
							|  |  |  |             'next_due_date' => $next_due_date, | 
					
						
							| 
									
										
										
										
											2022-08-30 23:37:05 +10:00
										 |  |  |             'active' => $is_active | 
					
						
							| 
									
										
										
										
											2022-05-15 00:24:05 +10:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-20 11:17:34 +11:00
										 |  |  |     public function updatePricing(string $service_id, string $currency, float $price, int $term, string $next_due_date, int $is_active = 1): int | 
					
						
							| 
									
										
										
										
											2022-05-15 00:43:36 +10:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-10-20 11:17:34 +11:00
										 |  |  |         $as_usd = $this->convertToUSD($price, $currency); | 
					
						
							| 
									
										
										
										
											2022-05-15 00:43:36 +10:00
										 |  |  |         return DB::table('pricings') | 
					
						
							|  |  |  |             ->where('service_id', $service_id) | 
					
						
							|  |  |  |             ->update([ | 
					
						
							|  |  |  |                 'currency' => $currency, | 
					
						
							|  |  |  |                 'price' => $price, | 
					
						
							|  |  |  |                 'term' => $term, | 
					
						
							|  |  |  |                 'as_usd' => $as_usd, | 
					
						
							|  |  |  |                 'usd_per_month' => $this->costAsPerMonth($as_usd, $term), | 
					
						
							|  |  |  |                 'next_due_date' => $next_due_date, | 
					
						
							| 
									
										
										
										
											2022-10-20 11:17:34 +11:00
										 |  |  |                 'active' => $is_active | 
					
						
							| 
									
										
										
										
											2022-05-15 00:43:36 +10:00
										 |  |  |             ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-15 00:24:05 +10:00
										 |  |  |     public static function allPricing() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-15 19:04:56 +10:00
										 |  |  |         return Cache::remember('all_active_pricing', now()->addWeek(1), function () { | 
					
						
							|  |  |  |             return Pricing::where('active', 1)->get(); | 
					
						
							| 
									
										
										
										
											2022-05-15 00:24:05 +10:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-05-15 01:21:51 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-06 02:02:12 +11:00
										 |  |  | } |