You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 satır
8.4KB

  1. <?php
  2. namespace domain\Producer\Producer;
  3. use common\helpers\GlobalParam;
  4. use domain\User\User\User;
  5. use domain\_\AbstractService;
  6. use domain\_\SolverInterface;
  7. use yii\helpers\Html;
  8. class ProducerSolver extends AbstractService implements SolverInterface
  9. {
  10. /**
  11. * Retourne true si le compte est un compte de démo.
  12. */
  13. public function isDemo(Producer $producer): bool
  14. {
  15. if (strpos($producer->name, 'Démo') !== false) {
  16. return true;
  17. }
  18. return false;
  19. }
  20. public function getFullAddressAsHtml(Producer $producer): string
  21. {
  22. $address = '<div class="name">'.$producer->name . '</div>';
  23. if (strlen($producer->address)) {
  24. $address .= $producer->address . '<br>';
  25. }
  26. if (strlen($producer->postcode) || strlen($producer->city)) {
  27. $address .= $producer->postcode . ' ' . $producer->city;
  28. }
  29. return $address;
  30. }
  31. public function getHtmlLogo(Producer $producer)
  32. {
  33. $html = '';
  34. if (strlen($producer->logo)) {
  35. $html = '<img src="' . $this->getUrlLogo($producer) . '" class="producer-logo" />';
  36. }
  37. return $html;
  38. }
  39. public function getUrlLogo(Producer $producer): string
  40. {
  41. return \Yii::$app->urlManagerProducer->getHostInfo() . '/' . \Yii::$app->urlManagerProducer->baseUrl . '/uploads/' . $producer->logo;
  42. }
  43. /**
  44. * @deprecated Déplacé dans MailerService
  45. */
  46. public function getEmailOpendistrib(Producer $producer): string
  47. {
  48. return $producer->slug . '@opendistrib.net';
  49. }
  50. public function getMainContact(Producer $producer): ?User
  51. {
  52. if ($producer->contact) {
  53. foreach ($producer->contact as $contact) {
  54. if ($contact->is_main_contact) {
  55. return $contact;
  56. }
  57. }
  58. }
  59. return null;
  60. }
  61. public function getMainContactEmail(Producer $producer): ?string
  62. {
  63. $contact = $this->getMainContact($producer);
  64. if ($contact) {
  65. return $contact->email;
  66. }
  67. return null;
  68. }
  69. public function isUpToDateWithOpendistribVersion(Producer $producer): bool
  70. {
  71. return $producer->latest_version_opendistrib == GlobalParam::getOpendistribVersion();
  72. }
  73. public function isBillingTypeClassic(Producer $producer): bool
  74. {
  75. return $producer->option_billing_type == Producer::BILLING_TYPE_CLASSIC;
  76. }
  77. public function isBillingTypeFreePrice(Producer $producer): bool
  78. {
  79. return $producer->option_billing_type == Producer::BILLING_TYPE_FREE_PRICE;
  80. }
  81. /**
  82. * Retourne le montant de l'abonnement à prix libre définit par
  83. * le producteur.
  84. */
  85. public function getFreePrice(Producer $producer, bool $format = true)
  86. {
  87. if (!is_null($producer->free_price)) {
  88. if ($format) {
  89. return number_format($producer->free_price, 2, ',', false) . ' €';
  90. } else {
  91. return $producer->free_price;
  92. }
  93. }
  94. }
  95. public function getSpecificDelays(Producer $producer): array
  96. {
  97. $array = [];
  98. $daysArray = [
  99. 'monday',
  100. 'tuesday',
  101. 'wednesday',
  102. 'thursday',
  103. 'friday',
  104. 'saturday',
  105. 'sunday'
  106. ];
  107. foreach ($daysArray as $day) {
  108. $fieldDelay = 'order_delay_' . $day;
  109. $fieldDeadline = 'order_deadline_' . $day;
  110. $delay = $producer->order_delay;
  111. $deadline = $producer->order_deadline;
  112. if ($producer->$fieldDelay) {
  113. $delay = $producer->$fieldDelay;
  114. }
  115. if ($producer->$fieldDeadline) {
  116. $deadline = $producer->$fieldDeadline;
  117. }
  118. $array[$day] = [
  119. 'order_delay' => $delay,
  120. 'order_deadline' => $deadline,
  121. ];
  122. }
  123. return $array;
  124. }
  125. public function hasSpecificDelays(Producer $producer): bool
  126. {
  127. $daysArray = [
  128. 'monday',
  129. 'tuesday',
  130. 'wednesday',
  131. 'thursday',
  132. 'friday',
  133. 'saturday',
  134. 'sunday'
  135. ];
  136. foreach ($daysArray as $day) {
  137. $fieldDelay = 'order_delay_' . $day;
  138. $fieldDeadline = 'order_deadline_' . $day;
  139. if ($producer->$fieldDelay || $producer->$fieldDeadline) {
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. public function getFilenamePrivateKeyApiStripe(Producer $producer): string
  146. {
  147. return '../../common/config/stripe/api-' . $producer->id . '.key';
  148. }
  149. public function getFilenamePrivateKeyEndpointStripe(Producer $producer): string
  150. {
  151. return '../../common/config/stripe/endpoint-' . $producer->id . '.key';
  152. }
  153. /**
  154. * Retourne la clé secrète d'API de Stripe.
  155. */
  156. public function getPrivateKeyStripe(string $filename): string
  157. {
  158. if (file_exists($filename)) {
  159. $handle = fopen($filename, "r");
  160. $filesize = filesize($filename);
  161. if ($handle && $filesize) {
  162. $secretKey = fread($handle, $filesize);
  163. fclose($handle);
  164. return $secretKey;
  165. }
  166. }
  167. return '';
  168. }
  169. public function getPrivateKeyApiStripe(Producer $producer): string
  170. {
  171. return $this->getPrivateKeyStripe($this->getFilenamePrivateKeyApiStripe($producer));
  172. }
  173. public function getPrivateKeyEndpointStripe(Producer $producer): string
  174. {
  175. return $this->getPrivateKeyStripe($this->getFilenamePrivateKeyEndpointStripe($producer));
  176. }
  177. public function isOnlinePaymentActive(Producer $producer): bool
  178. {
  179. return $producer->online_payment
  180. || ($producer->option_stripe_mode_test
  181. && !\Yii::$app->user->isGuest
  182. && \Yii::$app->user->identity->status > 10);
  183. }
  184. public function isOnlinePaymentActiveAndTypeOrder(Producer $producer): bool
  185. {
  186. return $this->isOnlinePaymentActive($producer)
  187. && $producer->option_online_payment_type == 'order';
  188. }
  189. public function isBillingFrequencyMonthly(Producer $producer): bool
  190. {
  191. return $producer->option_billing_frequency == Producer::BILLING_FREQUENCY_MONTHLY;
  192. }
  193. public function isBillingFrequencyQuarterly(Producer $producer): bool
  194. {
  195. return $producer->option_billing_frequency == Producer::BILLING_FREQUENCY_QUARTERLY;
  196. }
  197. public function isBillingFrequencyBiannual(Producer $producer): bool
  198. {
  199. return $producer->option_billing_frequency == Producer::BILLING_FREQUENCY_BIANNUAL;
  200. }
  201. public function getPointSaleWording(Producer $producer)
  202. {
  203. if($producer->option_point_sale_wording && strlen($producer->option_point_sale_wording)) {
  204. return Html::encode($producer->option_point_sale_wording);
  205. }
  206. return 'Points de vente';
  207. }
  208. public function getConfig(string $config, Producer $producer = null): ?string
  209. {
  210. if(!$producer) {
  211. $producer = $this->getProducerContext();
  212. }
  213. return $producer->$config;
  214. }
  215. public function getLeavePeriodStartDateTime(Producer $producer): ?\DateTime
  216. {
  217. if(!$producer->option_leave_period_start) {
  218. return null;
  219. }
  220. return \DateTime::createFromFormat('Y-m-d', $producer->option_leave_period_start)->setTime(0,0);;
  221. }
  222. public function getLeavePeriodEndDateTime(Producer $producer): ?\DateTime
  223. {
  224. if(!$producer->option_leave_period_end) {
  225. return null;
  226. }
  227. return \DateTime::createFromFormat('Y-m-d', $producer->option_leave_period_end)->setTime(23,59);;
  228. }
  229. public function hasLeavePeriodDefined(Producer $producer): bool
  230. {
  231. return $producer->option_leave_period_start && $producer->option_leave_period_end;
  232. }
  233. public function isOnLeavePeriod(Producer $producer, \DateTime $date = null): bool
  234. {
  235. if(!$date) {
  236. $date = new \DateTime();
  237. }
  238. if(!$producer->option_leave_period_start || !$producer->option_leave_period_end) {
  239. return false;
  240. }
  241. $leavePeriodStart = $this->getLeavePeriodStartDateTime($producer);
  242. $leavePeriodEnd = $this->getLeavePeriodEndDateTime($producer);
  243. if($date >= $leavePeriodStart && $date <= $leavePeriodEnd) {
  244. return true;
  245. }
  246. return false;
  247. }
  248. }