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.

128 lines
3.6KB

  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','code'], 'required'],
  38. ['code', function($attribute, $params)
  39. {
  40. $code = $this->$attribute ;
  41. $etablissement = Etablissement::findOne(['code' => $code]) ;
  42. if($etablissement && $etablissement->id != $this->id)
  43. {
  44. $this->addError($attribute, 'Ce code est déjà utilisé par une autre boulangerie.');
  45. }
  46. }],
  47. [['description'], 'string'],
  48. [['nom', 'siret', 'logo', 'photo', 'code_postal', 'ville','code'], 'string', 'max' => 255],
  49. ];
  50. }
  51. /**
  52. * @inheritdoc
  53. */
  54. public function attributeLabels()
  55. {
  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. ];
  66. }
  67. public static function getEtablissementsPopulateDropdown()
  68. {
  69. $etablissements_dispos = Etablissement::find()
  70. ->orderby('code_postal, ville ASC')
  71. ->all() ;
  72. $departements = Departements::get() ;
  73. $data_etablissements_dispos = [] ;
  74. $options_etablissements_dispos = [] ;
  75. foreach($etablissements_dispos as $e)
  76. {
  77. if($e->etatPaiement() == self::PAIEMENT_OK || $e->etatPaiement() == self::PAIEMENT_ESSAI)
  78. {
  79. if(!key_exists('d'. substr($e->code_postal, 0, 2), $data_etablissements_dispos))
  80. {
  81. $data_etablissements_dispos['d'. substr($e->code_postal, 0, 2)] = '<strong>'.$departements[substr($e->code_postal, 0, 2)].'</strong>' ;
  82. $options_etablissements_dispos['d'. substr($e->code_postal, 0, 2)] = ['disabled' => true] ;
  83. }
  84. $data_etablissements_dispos[$e->id] = Html::encode($e->nom).' - '.Html::encode($e->code_postal).' '.Html::encode($e->ville) ;
  85. }
  86. }
  87. return ['data' => $data_etablissements_dispos, 'options' => $options_etablissements_dispos] ;
  88. }
  89. public function etatPaiement()
  90. {
  91. $date_limite = strtotime($this->date_creation) + 30*24*60*60 ;
  92. $date = time() ;
  93. $date_paiement = strtotime($this->date_paiement) ;
  94. if($date < $date_paiement + 30*24*60*60 || $this->gratuit)
  95. {
  96. return 'ok' ;
  97. }
  98. else {
  99. if($date < $date_limite)
  100. {
  101. return 'essai' ;
  102. }
  103. else {
  104. if(!$this->date_paiement)
  105. return 'essai-terminee' ;
  106. else
  107. return 'retard-paiement' ;
  108. }
  109. }
  110. }
  111. }