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.

155 lines
4.1KB

  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. /**
  24. * @inheritdoc
  25. */
  26. public static function tableName()
  27. {
  28. return 'point_vente';
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['nom'], 'required'],
  37. [['acces_restreint'], 'boolean'],
  38. [['nom'], 'string', 'max' => 255],
  39. [['adresse','localite','horaires_lundi','horaires_mardi','horaires_mercredi','horaires_jeudi','horaires_vendredi','horaires_samedi','horaires_dimanche'], 'string'],
  40. [['point_fabrication','vrac','pain'], 'boolean'],
  41. ['point_fabrication', 'default','value'=>0],
  42. ['id_etablissement','integer'],
  43. ['id_etablissement','required'],
  44. ['users','safe']
  45. ];
  46. }
  47. /**
  48. * @inheritdoc
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'nom' => 'Nom',
  55. 'adresse' => 'Adresse',
  56. 'localite' => 'Localité',
  57. 'point_fabrication' => 'Point de fabrication',
  58. 'horaires_lundi' => 'Lundi',
  59. 'horaires_mardi' => 'Mardi',
  60. 'horaires_mercredi' => 'Mercredi',
  61. 'horaires_jeudi' => 'Jeudi',
  62. 'horaires_vendredi' => 'Vendredi',
  63. 'horaires_samedi' => 'Samedi',
  64. 'horaires_dimanche' => 'Dimanche',
  65. 'vrac' => 'Livraison de vrac',
  66. 'pain' => 'Livraison de pain',
  67. 'acces_restreint' => 'Accès restreint'
  68. ];
  69. }
  70. public function getPointVenteUser()
  71. {
  72. return $this->hasMany(PointVenteUser::className(), ['id_point_vente'=>'id']) ;
  73. }
  74. public function initCommandes($commandes) {
  75. $this->commandes = [] ;
  76. $this->recettes = 0 ;
  77. $this->recettes_pain = 0 ;
  78. $this->recettes_vrac = 0 ;
  79. foreach($commandes as $c) {
  80. if($this->id == $c->id_point_vente) {
  81. $this->commandes[] = $c ;
  82. $this->recettes += (float) $c->montant ;
  83. $this->recettes_pain += (float) $c->montant_pain ;
  84. $this->recettes_vrac += (float) $c->montant_vrac ;
  85. }
  86. }
  87. }
  88. public function strListeVrac() {
  89. $str = '' ;
  90. $produits = Produit::find()->orderBy('order ASC')->all() ;
  91. foreach($produits as $p) {
  92. if($p->vrac) {
  93. $quantite = Commande::getQuantiteProduit($p->id, $this->commandes) ;
  94. if($quantite) {
  95. $str .= $quantite.' '.Html::encode($p->diminutif).', ' ;
  96. }
  97. }
  98. }
  99. return substr($str, 0, strlen($str) - 2) ;
  100. }
  101. public function save($runValidation = true, $attributeNames = NULL)
  102. {
  103. $this->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  104. $this->pain = 1 ;
  105. return parent::save($runValidation, $attributeNames) ;
  106. }
  107. public function gestionPointFabrication()
  108. {
  109. if($this->point_fabrication)
  110. {
  111. PointVente::updateAll(
  112. ['point_fabrication' => 0],
  113. ['id_etablissement' => $this->id_etablissement]
  114. ) ;
  115. $this->point_fabrication = 1 ;
  116. $this->save() ;
  117. }
  118. }
  119. public function gestionAccesRestreint()
  120. {
  121. PointVenteUser::deleteAll(['id_point_vente' => $this->id]) ;
  122. foreach($this->users as $key => $val)
  123. {
  124. $user = User::findOne($val) ;
  125. if($user)
  126. {
  127. $point_vente_user = new PointVenteUser ;
  128. $point_vente_user->id_user = $val ;
  129. $point_vente_user->id_point_vente = $this->id ;
  130. $point_vente_user->save() ;
  131. }
  132. }
  133. }
  134. }