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.

176 lines
4.9KB

  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', 'credit_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. 'credit_pain' => 'Activer le Crédit Pain'
  70. ];
  71. }
  72. public function getPointVenteUser()
  73. {
  74. return $this->hasMany(PointVenteUser::className(), ['id_point_vente'=>'id']) ;
  75. }
  76. public function initCommandes($commandes) {
  77. $this->commandes = [] ;
  78. $this->recettes = 0 ;
  79. $this->recettes_pain = 0 ;
  80. $this->recettes_vrac = 0 ;
  81. foreach($commandes as $c) {
  82. if($this->id == $c->id_point_vente) {
  83. $this->commandes[] = $c ;
  84. $this->recettes += (float) $c->montant ;
  85. $this->recettes_pain += (float) $c->montant_pain ;
  86. $this->recettes_vrac += (float) $c->montant_vrac ;
  87. }
  88. }
  89. }
  90. public function strListeVrac() {
  91. $str = '' ;
  92. $produits = Produit::find()->orderBy('order ASC')->all() ;
  93. foreach($produits as $p) {
  94. if($p->vrac) {
  95. $quantite = Commande::getQuantiteProduit($p->id, $this->commandes) ;
  96. if($quantite) {
  97. $str .= $quantite.' '.Html::encode($p->diminutif).', ' ;
  98. }
  99. }
  100. }
  101. return substr($str, 0, strlen($str) - 2) ;
  102. }
  103. public function save($runValidation = true, $attributeNames = NULL)
  104. {
  105. $this->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  106. $this->pain = 1 ;
  107. return parent::save($runValidation, $attributeNames) ;
  108. }
  109. public function gestionPointFabrication()
  110. {
  111. if($this->point_fabrication)
  112. {
  113. PointVente::updateAll(
  114. ['point_fabrication' => 0],
  115. ['id_etablissement' => $this->id_etablissement]
  116. ) ;
  117. $this->point_fabrication = 1 ;
  118. $this->save() ;
  119. }
  120. }
  121. public function gestionAccesRestreint()
  122. {
  123. PointVenteUser::deleteAll(['id_point_vente' => $this->id]) ;
  124. if(is_array($this->users) && count($this->users))
  125. {
  126. foreach($this->users as $key => $val)
  127. {
  128. $user = User::findOne($val) ;
  129. if($user)
  130. {
  131. $point_vente_user = new PointVenteUser ;
  132. $point_vente_user->id_user = $val ;
  133. $point_vente_user->id_point_vente = $this->id ;
  134. if(isset($this->users_commentaire[$val]) && strlen($this->users_commentaire[$val]))
  135. $point_vente_user->commentaire = $this->users_commentaire[$val] ;
  136. $point_vente_user->save() ;
  137. }
  138. }
  139. }
  140. }
  141. public function getCommentaire()
  142. {
  143. if(isset($this->pointVenteUser))
  144. {
  145. foreach($this->pointVenteUser as $pvu)
  146. {
  147. if($pvu->id_user == Yii::$app->user->identity->id)
  148. {
  149. return $pvu->commentaire ;
  150. }
  151. }
  152. }
  153. }
  154. }