選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

194 行
5.6KB

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