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.

141 line
4.5KB

  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. {
  20. const PAIEMENT_OK = 'ok' ;
  21. const PAIEMENT_ESSAI = 'essai' ;
  22. const PAIEMENT_ESSAI_TERMINE = 'essai-terminee' ;
  23. const PAIEMENT_RETARD = 'retard-paiement' ;
  24. /**
  25. * @inheritdoc
  26. */
  27. public static function tableName()
  28. {
  29. return 'etablissement';
  30. }
  31. /**
  32. * @inheritdoc
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['nom', 'siret', 'heure_limite_commande','delai_commande'], 'required'],
  38. [['heure_limite_commande','delai_commande'],'integer'],
  39. ['heure_limite_commande','in', 'range' => [18, 19, 20, 21, 22, 23, 24]],
  40. ['delai_commande','in', 'range' => [1,2,3,4,5,6,7]],
  41. ['code', function($attribute, $params)
  42. {
  43. $code = $this->$attribute ;
  44. $etablissement = Etablissement::findOne(['code' => $code]) ;
  45. if($etablissement && $etablissement->id != $this->id)
  46. {
  47. $this->addError($attribute, 'Ce code est déjà utilisé par une autre boulangerie.');
  48. }
  49. }],
  50. [['description'], 'string'],
  51. [['solde_negatif', 'credit_pain','actif'], 'boolean'],
  52. [['nom', 'siret', 'logo', 'photo', 'code_postal', 'ville','code'], 'string', 'max' => 255],
  53. ];
  54. }
  55. /**
  56. * @inheritdoc
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'nom' => 'Nom',
  63. 'siret' => 'Siret',
  64. 'logo' => 'Logo',
  65. 'photo' => 'Photo',
  66. 'description' => 'Description',
  67. 'code_postal' => 'Code postal',
  68. 'ville' => 'Ville',
  69. 'code' => 'Code',
  70. 'heure_limite_commande' => 'Heure limite de commande',
  71. 'delai_commande' => 'Délai de commande',
  72. 'solde_negatif' => 'Solde négatif',
  73. 'credit_pain' => 'Crédit pain',
  74. 'actif' => 'Actif'
  75. ];
  76. }
  77. public static function getEtablissementsPopulateDropdown()
  78. {
  79. $etablissements_dispos = Etablissement::find()
  80. ->where(['actif' => 1])
  81. ->orderby('code_postal, ville ASC')
  82. ->all() ;
  83. $departements = Departements::get() ;
  84. $data_etablissements_dispos = [] ;
  85. $options_etablissements_dispos = [] ;
  86. foreach($etablissements_dispos as $e)
  87. {
  88. if($e->etatPaiement() == self::PAIEMENT_OK || $e->etatPaiement() == self::PAIEMENT_ESSAI)
  89. {
  90. if(!key_exists('d'. substr($e->code_postal, 0, 2), $data_etablissements_dispos))
  91. {
  92. $data_etablissements_dispos['d'. substr($e->code_postal, 0, 2)] = '<strong>'.$departements[substr($e->code_postal, 0, 2)].'</strong>' ;
  93. $options_etablissements_dispos['d'. substr($e->code_postal, 0, 2)] = ['disabled' => true] ;
  94. }
  95. $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>' ;
  96. if(strlen($e->code))
  97. $options_etablissements_dispos[$e->id] = ['class' => 'lock'] ;
  98. }
  99. }
  100. return ['data' => $data_etablissements_dispos, 'options' => $options_etablissements_dispos] ;
  101. }
  102. public function etatPaiement()
  103. {
  104. $date_limite = strtotime($this->date_creation) + 30*24*60*60 ;
  105. $date = time() ;
  106. $date_paiement = strtotime($this->date_paiement) ;
  107. if($date < $date_paiement + 30*24*60*60 || $this->gratuit)
  108. {
  109. return 'ok' ;
  110. }
  111. else {
  112. if($date < $date_limite)
  113. {
  114. return 'essai' ;
  115. }
  116. else {
  117. if(!$this->date_paiement)
  118. return 'essai-terminee' ;
  119. else
  120. return 'retard-paiement' ;
  121. }
  122. }
  123. }
  124. }