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.

135 lines
3.2KB

  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\helpers\Html ;
  5. /**
  6. * This is the model class for table "commande".
  7. *
  8. * @property integer $id
  9. * @property integer $id_user
  10. * @property string $date
  11. * @property string $date_update
  12. * @property integer $id_point_vente
  13. * @property integer $id_production
  14. */
  15. class Commande extends \yii\db\ActiveRecord
  16. {
  17. var $montant = 0 ;
  18. var $montant_pain = 0 ;
  19. var $montant_vrac = 0 ;
  20. var $poids_pain = 0 ;
  21. var $poids_vrac = 0 ;
  22. const TYPE_AUTO = 'auto' ;
  23. const TYPE_USER = 'user' ;
  24. const TYPE_ADMIN = 'admin' ;
  25. /**
  26. * @inheritdoc
  27. */
  28. public static function tableName()
  29. {
  30. return 'commande';
  31. }
  32. public function init() {
  33. if(isset($this->commandeProduits)) {
  34. foreach($this->commandeProduits as $p) {
  35. if(isset($p->produit) && $p->produit->vrac) {
  36. $this->montant_vrac += $p->prix * $p->quantite/1000 ;
  37. $this->poids_vrac += $p->quantite/1000 ;
  38. }
  39. else {
  40. $this->montant_pain += $p->prix * $p->quantite ;
  41. if(isset($p->produit))
  42. $this->poids_pain += $p->quantite * $p->produit->poids/1000 ;
  43. }
  44. }
  45. $this->montant = $this->montant_vrac + $this->montant_pain ;
  46. }
  47. }
  48. public static function getQuantiteProduit($id_produit, $commandes) {
  49. $quantite = 0 ;
  50. if(isset($commandes) && is_array($commandes) && count($commandes)) {
  51. foreach($commandes as $c) {
  52. foreach($c->commandeProduits as $cp) {
  53. if($cp->id_produit == $id_produit)
  54. $quantite += $cp->quantite ;
  55. }
  56. }
  57. }
  58. return $quantite ;
  59. }
  60. /*
  61. * relations
  62. */
  63. public function getUser() {
  64. return $this->hasOne(User::className(), ['id'=>'id_user']) ;
  65. }
  66. public function getCommandeProduits() {
  67. return $this->hasMany(CommandeProduit::className(), ['id_commande'=>'id'])->with('produit') ;
  68. }
  69. public function getProduction() {
  70. return $this->hasOne(Production::className(), ['id'=>'id_production'])->with('etablissement') ;
  71. }
  72. public function getPointVente() {
  73. return $this->hasOne(PointVente::className(), ['id'=>'id_point_vente']) ;
  74. }
  75. /**
  76. * @inheritdoc
  77. */
  78. public function rules()
  79. {
  80. return [
  81. [['id_user', 'date', 'id_point_vente','id_production'], 'required','message'=>''],
  82. [['id_user', 'id_point_vente', 'id_production'], 'integer'],
  83. [['date', 'date_update','commentaire'], 'safe']
  84. ];
  85. }
  86. /**
  87. * @inheritdoc
  88. */
  89. public function attributeLabels()
  90. {
  91. return [
  92. 'id' => 'ID',
  93. 'id_user' => 'Id User',
  94. 'date' => 'Date',
  95. 'date_update' => 'Date Update',
  96. 'id_point_vente' => 'Point de vente',
  97. 'id_production' => 'Date de production',
  98. ];
  99. }
  100. public function strListeVrac()
  101. {
  102. $str = '' ;
  103. foreach($this->commandeProduits as $cp) {
  104. if($cp->produit->vrac) {
  105. $str .= $cp->quantite.'&nbsp;'.Html::encode($cp->produit->diminutif).', ' ;
  106. }
  107. }
  108. return substr($str, 0, strlen($str) - 2) ;
  109. }
  110. }