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.

PointVente.php 4.8KB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\helpers\Html ;
  5. use common\models\PointVenteUser ;
  6. /**
  7. * This is the model class for table "point_vente".
  8. *
  9. * @property integer $id
  10. * @property string $nom
  11. * @property string $adresse
  12. * @property integer $id_boulangerie
  13. */
  14. class PointVente extends \yii\db\ActiveRecord
  15. {
  16. var $commandes = [] ;
  17. var $recettes = 0 ;
  18. var $recettes_pain = 0 ;
  19. var $recettes_vrac = 0 ;
  20. var $data_select_commandes ;
  21. var $data_options_commandes ;
  22. var $users = [] ;
  23. var $users_commentaire = [] ;
  24. /**
  25. * @inheritdoc
  26. */
  27. public static function tableName()
  28. {
  29. return 'point_vente';
  30. }
  31. /**
  32. * @inheritdoc
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['nom'], 'required'],
  38. [['acces_restreint'], 'boolean'],
  39. [['nom'], 'string', 'max' => 255],
  40. [['adresse','localite','horaires_lundi','horaires_mardi','horaires_mercredi','horaires_jeudi','horaires_vendredi','horaires_samedi','horaires_dimanche'], 'string'],
  41. [['point_fabrication','vrac','pain'], 'boolean'],
  42. ['point_fabrication', 'default','value'=>0],
  43. ['id_etablissement','integer'],
  44. ['id_etablissement','required'],
  45. [['users','users_commentaire'],'safe']
  46. ];
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. public function attributeLabels()
  52. {
  53. return [
  54. 'id' => 'ID',
  55. 'nom' => 'Nom',
  56. 'adresse' => 'Adresse',
  57. 'localite' => 'Localité',
  58. 'point_fabrication' => 'Point de fabrication',
  59. 'horaires_lundi' => 'Lundi',
  60. 'horaires_mardi' => 'Mardi',
  61. 'horaires_mercredi' => 'Mercredi',
  62. 'horaires_jeudi' => 'Jeudi',
  63. 'horaires_vendredi' => 'Vendredi',
  64. 'horaires_samedi' => 'Samedi',
  65. 'horaires_dimanche' => 'Dimanche',
  66. 'vrac' => 'Livraison de vrac',
  67. 'pain' => 'Livraison de pain',
  68. 'acces_restreint' => 'Accès restreint'
  69. ];
  70. }
  71. public function getPointVenteUser()
  72. {
  73. return $this->hasMany(PointVenteUser::className(), ['id_point_vente'=>'id']) ;
  74. }
  75. public function initCommandes($commandes) {
  76. $this->commandes = [] ;
  77. $this->recettes = 0 ;
  78. $this->recettes_pain = 0 ;
  79. $this->recettes_vrac = 0 ;
  80. foreach($commandes as $c) {
  81. if($this->id == $c->id_point_vente) {
  82. $this->commandes[] = $c ;
  83. $this->recettes += (float) $c->montant ;
  84. $this->recettes_pain += (float) $c->montant_pain ;
  85. $this->recettes_vrac += (float) $c->montant_vrac ;
  86. }
  87. }
  88. }
  89. public function strListeVrac() {
  90. $str = '' ;
  91. $produits = Produit::find()->orderBy('order ASC')->all() ;
  92. foreach($produits as $p) {
  93. if($p->vrac) {
  94. $quantite = Commande::getQuantiteProduit($p->id, $this->commandes) ;
  95. if($quantite) {
  96. $str .= $quantite.' '.Html::encode($p->diminutif).', ' ;
  97. }
  98. }
  99. }
  100. return substr($str, 0, strlen($str) - 2) ;
  101. }
  102. public function save($runValidation = true, $attributeNames = NULL)
  103. {
  104. $this->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  105. $this->pain = 1 ;
  106. return parent::save($runValidation, $attributeNames) ;
  107. }
  108. public function gestionPointFabrication()
  109. {
  110. if($this->point_fabrication)
  111. {
  112. PointVente::updateAll(
  113. ['point_fabrication' => 0],
  114. ['id_etablissement' => $this->id_etablissement]
  115. ) ;
  116. $this->point_fabrication = 1 ;
  117. $this->save() ;
  118. }
  119. }
  120. public function gestionAccesRestreint()
  121. {
  122. PointVenteUser::deleteAll(['id_point_vente' => $this->id]) ;
  123. if(is_array($this->users) && count($this->users))
  124. {
  125. foreach($this->users as $key => $val)
  126. {
  127. $user = User::findOne($val) ;
  128. if($user)
  129. {
  130. $point_vente_user = new PointVenteUser ;
  131. $point_vente_user->id_user = $val ;
  132. $point_vente_user->id_point_vente = $this->id ;
  133. if(isset($this->users_commentaire[$val]) && strlen($this->users_commentaire[$val]))
  134. $point_vente_user->commentaire = $this->users_commentaire[$val] ;
  135. $point_vente_user->save() ;
  136. }
  137. }
  138. }
  139. }
  140. public function getCommentaire()
  141. {
  142. if(isset($this->pointVenteUser))
  143. {
  144. foreach($this->pointVenteUser as $pvu)
  145. {
  146. if($pvu->id_user == Yii::$app->user->identity->id)
  147. {
  148. return $pvu->commentaire ;
  149. }
  150. }
  151. }
  152. }
  153. }