Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Etablissement.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use common\helpers\Departements;
  5. use yii\helpers\Html;
  6. /**
  7. * This is the model class for table "etablissement".
  8. *
  9. * @property integer $id
  10. * @property string $nom
  11. * @property string $siret
  12. * @property string $logo
  13. * @property string $photo
  14. * @property string $description
  15. * @property string $code_postal
  16. * @property string $ville
  17. */
  18. class Etablissement extends \yii\db\ActiveRecord {
  19. const PAIEMENT_OK = 'ok';
  20. const PAIEMENT_ESSAI = 'essai';
  21. const PAIEMENT_ESSAI_TERMINE = 'essai-terminee';
  22. const PAIEMENT_RETARD = 'retard-paiement';
  23. /**
  24. * @inheritdoc
  25. */
  26. public static function tableName() {
  27. return 'etablissement';
  28. }
  29. /**
  30. * @inheritdoc
  31. */
  32. public function rules() {
  33. return [
  34. [['nom', 'siret', 'heure_limite_commande', 'delai_commande','type'], 'required'],
  35. [['heure_limite_commande', 'delai_commande'], 'integer'],
  36. ['heure_limite_commande', 'in', 'range' => [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]],
  37. ['delai_commande', 'in', 'range' => [1, 2, 3, 4, 5, 6, 7]],
  38. ['code', function($attribute, $params) {
  39. $code = $this->$attribute;
  40. $etablissement = Etablissement::findOne(['code' => $code]);
  41. if ($etablissement && $etablissement->id != $this->id) {
  42. $this->addError($attribute, 'Ce code est déjà utilisé par un autre producteur.');
  43. }
  44. }],
  45. [['description', 'infos_commande','slug'], 'string'],
  46. [['solde_negatif', 'credit_pain', 'actif'], 'boolean'],
  47. [['nom', 'siret', 'logo', 'photo', 'code_postal', 'ville', 'code','type'], 'string', 'max' => 255],
  48. ['prix_libre', 'double'],
  49. ['prix_libre', 'compare', 'compareValue' => 0, 'operator' => '>=', 'type' => 'number', 'message' => 'Prix libre doit être supérieur ou égal à 0'],
  50. ];
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function attributeLabels() {
  56. return [
  57. 'id' => 'ID',
  58. 'nom' => 'Nom',
  59. 'siret' => 'Siret',
  60. 'logo' => 'Logo',
  61. 'photo' => 'Photo',
  62. 'description' => 'Description',
  63. 'code_postal' => 'Code postal',
  64. 'ville' => 'Ville',
  65. 'code' => 'Code',
  66. 'heure_limite_commande' => 'Heure limite de commande',
  67. 'delai_commande' => 'Délai de commande',
  68. 'solde_negatif' => 'Solde négatif',
  69. 'credit_pain' => 'Crédit pain',
  70. 'actif' => 'Actif',
  71. 'date_creation' => 'Date de création',
  72. 'infos_commande' => 'Informations',
  73. 'slug' => 'Slug',
  74. 'type' => 'Type d\'établissement'
  75. ];
  76. }
  77. public function getUserEtablissement() {
  78. return $this->hasMany(UserEtablissement::className(), ['id_etablissement' => 'id']);
  79. }
  80. public function getUser() {
  81. return $this->hasMany(User::className(), ['id_etablissement' => 'id']);
  82. }
  83. public static function getEtablissementsPopulateDropdown() {
  84. $etablissements_dispos = Etablissement::find()
  85. ->where(['actif' => 1])
  86. ->orderby('code_postal, ville ASC')
  87. ->all();
  88. $departements = Departements::get();
  89. $data_etablissements_dispos = [];
  90. $options_etablissements_dispos = [];
  91. foreach ($etablissements_dispos as $e) {
  92. if ($e->etatPaiement() == self::PAIEMENT_OK || $e->etatPaiement() == self::PAIEMENT_ESSAI) {
  93. if (!key_exists('d' . substr($e->code_postal, 0, 2), $data_etablissements_dispos)) {
  94. $data_etablissements_dispos['d' . substr($e->code_postal, 0, 2)] = '<strong>' . $departements[substr($e->code_postal, 0, 2)] . '</strong>';
  95. $options_etablissements_dispos['d' . substr($e->code_postal, 0, 2)] = ['disabled' => true];
  96. }
  97. $data_etablissements_dispos[$e->id] = '<span class="glyphicon glyphicon-lock"></span> ' . Html::encode($e->nom) . ' - ' . Html::encode($e->code_postal) . ' ' . Html::encode($e->ville) . ' <span class="glyphicon glyphicon-lock"></span>';
  98. if (strlen($e->code))
  99. $options_etablissements_dispos[$e->id] = ['class' => 'lock'];
  100. }
  101. }
  102. return ['data' => $data_etablissements_dispos, 'options' => $options_etablissements_dispos];
  103. }
  104. public function etatPaiement() {
  105. $date_limite = strtotime($this->date_creation) + 30 * 24 * 60 * 60;
  106. $date = time();
  107. $date_paiement = strtotime($this->date_paiement);
  108. if ($date < $date_paiement + 30 * 24 * 60 * 60 || $this->gratuit) {
  109. return 'ok';
  110. } else {
  111. if ($date < $date_limite) {
  112. return 'essai';
  113. } else {
  114. if (!$this->date_paiement)
  115. return 'essai-terminee';
  116. else
  117. return 'retard-paiement';
  118. }
  119. }
  120. }
  121. public function getCA($periode = '', $format = false) {
  122. if (!$periode)
  123. $periode = date('Y-m');
  124. $connection = Yii::$app->getDb();
  125. $command = $connection->createCommand('
  126. SELECT SUM(IF(produit.vrac,0,commande_produit.prix * commande_produit.quantite)) AS CA
  127. FROM commande, commande_produit, production, produit
  128. WHERE commande.id = commande_produit.id_commande
  129. AND production.id_etablissement = :id_etablissement
  130. AND commande.id_production = production.id
  131. AND commande_produit.id_produit = produit.id
  132. AND production.date > :date_debut
  133. AND production.date < :date_fin', [
  134. ':date_debut' => date('Y-m-31', strtotime("-1 month", strtotime($periode))),
  135. ':date_fin' => date('Y-m-01', strtotime("+1 month", strtotime($periode))),
  136. ':id_etablissement' => $this->id
  137. ]);
  138. $result = $command->queryOne();
  139. $ca = $result['CA'];
  140. if ($format)
  141. return number_format($ca, 2) . ' €';
  142. else
  143. return $ca;
  144. }
  145. public function getMontantFacturer($periode = '', $ca = 0, $format = false) {
  146. if (!$periode)
  147. $periode = date('Y-m');
  148. if (!$ca)
  149. $ca = $this->getCA($periode);
  150. if ($ca < 500) {
  151. $montant = 0;
  152. } else {
  153. $montant = $ca * 0.02;
  154. }
  155. if ($format) {
  156. return number_format($montant, 2) . ' €';
  157. } else {
  158. return $montant;
  159. }
  160. }
  161. public function getFacture($periode = '') {
  162. if (!$periode)
  163. $periode = date('Y-m', strtotime('-1 month'));
  164. $facture = Facture::find()
  165. ->where('id_etablissement = :id_etablissement')
  166. ->andWhere('periode = :periode')
  167. ->addParams([
  168. ':id_etablissement' => $this->id,
  169. ':periode' => $periode,
  170. ])
  171. ->one();
  172. return $facture;
  173. }
  174. public function factureMoisDernier() {
  175. return $this->getFacture(date('Y-m', strtotime('-1 month')));
  176. }
  177. public static function getConfig($config = '', $id_etablissement = 0) {
  178. if (strlen($config)) {
  179. if (!$id_etablissement)
  180. $id_etablissement = Yii::$app->user->identity->id_etablissement;
  181. $etablissement = self::findOne($id_etablissement);
  182. if ($etablissement) {
  183. return $etablissement->$config;
  184. }
  185. }
  186. return false;
  187. }
  188. public function getPrixLibre() {
  189. if (!is_null($this->prix_libre)) {
  190. return number_format($this->prix_libre, 2, ',', false) . ' €';
  191. }
  192. }
  193. }