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 6.3KB

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
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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','code'], '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','code'],'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. 'code' => 'Code',
  79. ];
  80. }
  81. public function getPointVenteUser()
  82. {
  83. return $this->hasMany(PointVenteUser::className(), ['id_point_vente'=>'id']) ;
  84. }
  85. public function getProductionPointVente()
  86. {
  87. return $this->hasMany(ProductionPointVente::className(), ['id_point_vente' => 'id']) ;
  88. }
  89. public function initCommandes($commandes) {
  90. $this->commandes = [] ;
  91. $this->recettes = 0 ;
  92. $this->recettes_pain = 0 ;
  93. $this->recettes_vrac = 0 ;
  94. foreach($commandes as $c) {
  95. if($this->id == $c->id_point_vente) {
  96. $this->commandes[] = $c ;
  97. $this->recettes += (float) $c->montant ;
  98. $this->recettes_pain += (float) $c->montant_pain ;
  99. $this->recettes_vrac += (float) $c->montant_vrac ;
  100. }
  101. }
  102. }
  103. public function getCommandes()
  104. {
  105. return $this->commandes ;
  106. }
  107. public function strListeVrac() {
  108. $str = '' ;
  109. $produits = Produit::find()->orderBy('order ASC')->all() ;
  110. foreach($produits as $p) {
  111. if($p->vrac) {
  112. $quantite = Commande::getQuantiteProduit($p->id, $this->commandes) ;
  113. if($quantite) {
  114. $str .= $quantite.' '.Html::encode($p->diminutif).', ' ;
  115. }
  116. }
  117. }
  118. return substr($str, 0, strlen($str) - 2) ;
  119. }
  120. public function save($runValidation = true, $attributeNames = NULL)
  121. {
  122. $this->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  123. $this->pain = 1 ;
  124. return parent::save($runValidation, $attributeNames) ;
  125. }
  126. public function gestionPointFabrication()
  127. {
  128. if($this->point_fabrication)
  129. {
  130. PointVente::updateAll(
  131. ['point_fabrication' => 0],
  132. ['id_etablissement' => $this->id_etablissement]
  133. ) ;
  134. $this->point_fabrication = 1 ;
  135. $this->save() ;
  136. }
  137. }
  138. public function gestionAccesRestreint()
  139. {
  140. PointVenteUser::deleteAll(['id_point_vente' => $this->id]) ;
  141. if(is_array($this->users) && count($this->users))
  142. {
  143. foreach($this->users as $key => $val)
  144. {
  145. $user = User::findOne($val) ;
  146. if($user)
  147. {
  148. $point_vente_user = new PointVenteUser ;
  149. $point_vente_user->id_user = $val ;
  150. $point_vente_user->id_point_vente = $this->id ;
  151. if(isset($this->users_commentaire[$val]) && strlen($this->users_commentaire[$val]))
  152. $point_vente_user->commentaire = $this->users_commentaire[$val] ;
  153. $point_vente_user->save() ;
  154. }
  155. }
  156. }
  157. }
  158. public function getCommentaire()
  159. {
  160. if(isset($this->pointVenteUser))
  161. {
  162. foreach($this->pointVenteUser as $pvu)
  163. {
  164. if($pvu->id_user == Yii::$app->user->identity->id)
  165. {
  166. return $pvu->commentaire ;
  167. }
  168. }
  169. }
  170. }
  171. public static function count()
  172. {
  173. return PointVente::find()
  174. ->where([
  175. 'id_etablissement' => Yii::$app->user->identity->id_etablissement
  176. ])
  177. ->count() ;
  178. }
  179. public function verifCode($code)
  180. {
  181. if(strlen($this->code))
  182. {
  183. if(trim(strtolower($code)) == trim(strtolower($this->code)))
  184. {
  185. return true ;
  186. }
  187. else {
  188. return false ;
  189. }
  190. }
  191. else {
  192. return true ;
  193. }
  194. }
  195. }