Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

228 lines
7.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. 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'], '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'], 'string'],
  46. [['solde_negatif', 'credit_pain', 'actif'], 'boolean'],
  47. [['nom', 'siret', 'logo', 'photo', 'code_postal', 'ville', 'code'], '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. ];
  74. }
  75. public function getUserEtablissement() {
  76. return $this->hasMany(UserEtablissement::className(), ['id_etablissement' => 'id']);
  77. }
  78. public function getUser() {
  79. return $this->hasMany(User::className(), ['id_etablissement' => 'id']);
  80. }
  81. public static function getEtablissementsPopulateDropdown() {
  82. $etablissements_dispos = Etablissement::find()
  83. ->where(['actif' => 1])
  84. ->orderby('code_postal, ville ASC')
  85. ->all();
  86. $departements = Departements::get();
  87. $data_etablissements_dispos = [];
  88. $options_etablissements_dispos = [];
  89. foreach ($etablissements_dispos as $e) {
  90. if ($e->etatPaiement() == self::PAIEMENT_OK || $e->etatPaiement() == self::PAIEMENT_ESSAI) {
  91. if (!key_exists('d' . substr($e->code_postal, 0, 2), $data_etablissements_dispos)) {
  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. $date_limite = strtotime($this->date_creation) + 30 * 24 * 60 * 60;
  104. $date = time();
  105. $date_paiement = strtotime($this->date_paiement);
  106. if ($date < $date_paiement + 30 * 24 * 60 * 60 || $this->gratuit) {
  107. return 'ok';
  108. } else {
  109. if ($date < $date_limite) {
  110. return 'essai';
  111. } else {
  112. if (!$this->date_paiement)
  113. return 'essai-terminee';
  114. else
  115. return 'retard-paiement';
  116. }
  117. }
  118. }
  119. public function getCA($periode = '', $format = false) {
  120. if (!$periode)
  121. $periode = date('Y-m');
  122. $connection = Yii::$app->getDb();
  123. $command = $connection->createCommand('
  124. SELECT SUM(IF(produit.vrac,0,commande_produit.prix * commande_produit.quantite)) AS CA
  125. FROM commande, commande_produit, production, produit
  126. WHERE commande.id = commande_produit.id_commande
  127. AND production.id_etablissement = :id_etablissement
  128. AND commande.id_production = production.id
  129. AND commande_produit.id_produit = produit.id
  130. AND production.date > :date_debut
  131. AND production.date < :date_fin', [
  132. ':date_debut' => date('Y-m-31', strtotime("-1 month", strtotime($periode))),
  133. ':date_fin' => date('Y-m-01', strtotime("+1 month", strtotime($periode))),
  134. ':id_etablissement' => $this->id
  135. ]);
  136. $result = $command->queryOne();
  137. $ca = $result['CA'];
  138. if ($format)
  139. return number_format($ca, 2) . ' €';
  140. else
  141. return $ca;
  142. }
  143. public function getMontantFacturer($periode = '', $ca = 0, $format = false) {
  144. if (!$periode)
  145. $periode = date('Y-m');
  146. if (!$ca)
  147. $ca = $this->getCA($periode);
  148. if ($ca < 500) {
  149. $montant = 0;
  150. } else {
  151. $montant = $ca * 0.02;
  152. }
  153. if ($format) {
  154. return number_format($montant, 2) . ' €';
  155. } else {
  156. return $montant;
  157. }
  158. }
  159. public function getFacture($periode = '') {
  160. if (!$periode)
  161. $periode = date('Y-m', strtotime('-1 month'));
  162. $facture = Facture::find()
  163. ->where('id_etablissement = :id_etablissement')
  164. ->andWhere('periode = :periode')
  165. ->addParams([
  166. ':id_etablissement' => $this->id,
  167. ':periode' => $periode,
  168. ])
  169. ->one();
  170. return $facture;
  171. }
  172. public function factureMoisDernier() {
  173. return $this->getFacture(date('Y-m', strtotime('-1 month')));
  174. }
  175. public static function getConfig($config = '', $id_etablissement = 0) {
  176. if (strlen($config)) {
  177. if (!$id_etablissement)
  178. $id_etablissement = Yii::$app->user->identity->id_etablissement;
  179. $etablissement = self::findOne($id_etablissement);
  180. if ($etablissement) {
  181. return $etablissement->$config;
  182. }
  183. }
  184. return false;
  185. }
  186. public function getPrixLibre() {
  187. if (!is_null($this->prix_libre)) {
  188. return number_format($this->prix_libre, 2, ',', false) . ' €';
  189. }
  190. }
  191. }