Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Etablissement.php 7.5KB

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