No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

258 líneas
8.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','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 function getContact() {
  84. return $this->hasMany(User::className(), ['id_etablissement' => 'id'])->where(['status' => User::STATUS_BOULANGER]);
  85. }
  86. public static function getEtablissementsPopulateDropdown() {
  87. $etablissements_dispos = Etablissement::find()
  88. ->where(['actif' => 1])
  89. ->orderby('code_postal, ville ASC')
  90. ->all();
  91. $departements = Departements::get();
  92. $data_etablissements_dispos = [];
  93. $options_etablissements_dispos = [];
  94. foreach ($etablissements_dispos as $e) {
  95. if ($e->etatPaiement() == self::PAIEMENT_OK || $e->etatPaiement() == self::PAIEMENT_ESSAI) {
  96. if (!key_exists('d' . substr($e->code_postal, 0, 2), $data_etablissements_dispos)) {
  97. $data_etablissements_dispos['d' . substr($e->code_postal, 0, 2)] = '<strong>' . $departements[substr($e->code_postal, 0, 2)] . '</strong>';
  98. $options_etablissements_dispos['d' . substr($e->code_postal, 0, 2)] = ['disabled' => true];
  99. }
  100. $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>';
  101. if (strlen($e->code))
  102. $options_etablissements_dispos[$e->id] = ['class' => 'lock'];
  103. }
  104. }
  105. return ['data' => $data_etablissements_dispos, 'options' => $options_etablissements_dispos];
  106. }
  107. public function etatPaiement() {
  108. $date_limite = strtotime($this->date_creation) + 30 * 24 * 60 * 60;
  109. $date = time();
  110. $date_paiement = strtotime($this->date_paiement);
  111. if ($date < $date_paiement + 30 * 24 * 60 * 60 || $this->gratuit) {
  112. return 'ok';
  113. } else {
  114. if ($date < $date_limite) {
  115. return 'essai';
  116. } else {
  117. if (!$this->date_paiement)
  118. return 'essai-terminee';
  119. else
  120. return 'retard-paiement';
  121. }
  122. }
  123. }
  124. public function getCA($periode = '', $format = false) {
  125. if (!$periode)
  126. $periode = date('Y-m');
  127. $connection = Yii::$app->getDb();
  128. $command = $connection->createCommand('
  129. SELECT SUM(IF(produit.vrac,0,commande_produit.prix * commande_produit.quantite)) AS CA
  130. FROM commande, commande_produit, production, produit
  131. WHERE commande.id = commande_produit.id_commande
  132. AND production.id_etablissement = :id_etablissement
  133. AND commande.id_production = production.id
  134. AND commande_produit.id_produit = produit.id
  135. AND production.date > :date_debut
  136. AND production.date < :date_fin', [
  137. ':date_debut' => date('Y-m-31', strtotime("-1 month", strtotime($periode))),
  138. ':date_fin' => date('Y-m-01', strtotime("+1 month", strtotime($periode))),
  139. ':id_etablissement' => $this->id
  140. ]);
  141. $result = $command->queryOne();
  142. $ca = $result['CA'];
  143. if ($format)
  144. return number_format($ca, 2) . ' €';
  145. else
  146. return $ca;
  147. }
  148. public function getMontantFacturer($periode = '', $ca = 0, $format = false) {
  149. if (!$periode)
  150. $periode = date('Y-m');
  151. if (!$ca)
  152. $ca = $this->getCA($periode);
  153. if ($ca < 500) {
  154. $montant = 0;
  155. } else {
  156. $montant = $ca * 0.02;
  157. }
  158. if ($format) {
  159. return number_format($montant, 2) . ' €';
  160. } else {
  161. return $montant;
  162. }
  163. }
  164. public function getFacture($periode = '') {
  165. if (!$periode)
  166. $periode = date('Y-m', strtotime('-1 month'));
  167. $facture = Facture::find()
  168. ->where('id_etablissement = :id_etablissement')
  169. ->andWhere('periode = :periode')
  170. ->addParams([
  171. ':id_etablissement' => $this->id,
  172. ':periode' => $periode,
  173. ])
  174. ->one();
  175. return $facture;
  176. }
  177. public function factureMoisDernier() {
  178. return $this->getFacture(date('Y-m', strtotime('-1 month')));
  179. }
  180. public static function getConfig($config = '', $id_etablissement = 0) {
  181. if (strlen($config)) {
  182. if (!$id_etablissement)
  183. $id_etablissement = Yii::$app->user->identity->id_etablissement;
  184. $etablissement = self::findOne($id_etablissement);
  185. if ($etablissement) {
  186. return $etablissement->$config;
  187. }
  188. }
  189. return false;
  190. }
  191. public function getPrixLibre() {
  192. if (!is_null($this->prix_libre)) {
  193. return number_format($this->prix_libre, 2, ',', false) . ' €';
  194. }
  195. }
  196. public static function addUser($id_user, $id_producer) {
  197. $user_producer = UserEtablissement::find()
  198. ->where([
  199. 'id_user' => $id_user,
  200. 'id_etablissement' => $id_producer
  201. ])->one();
  202. if (!$user_producer) {
  203. $new_user_producer = new UserEtablissement;
  204. $new_user_producer->id_etablissement = $id_producer;
  205. $new_user_producer->id_user = $id_user;
  206. $new_user_producer->credit = 0;
  207. $new_user_producer->actif = 1;
  208. $new_user_producer->save();
  209. } else {
  210. if (!$user_producer->actif) {
  211. $user_producer->actif = 1;
  212. $user_producer->save();
  213. }
  214. }
  215. return $user_producer ;
  216. }
  217. }