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.

129 lines
3.0KB

  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. /**
  23. * @inheritdoc
  24. */
  25. public static function tableName()
  26. {
  27. return 'commande';
  28. }
  29. public function init() {
  30. if(isset($this->commandeProduits)) {
  31. foreach($this->commandeProduits as $p) {
  32. if($p->produit->vrac) {
  33. $this->montant_vrac += $p->prix * $p->quantite/1000 ;
  34. $this->poids_vrac += $p->quantite/1000 ;
  35. }
  36. else {
  37. $this->montant_pain += $p->prix * $p->quantite ;
  38. $this->poids_pain += $p->quantite * $p->produit->poids/1000 ;
  39. }
  40. }
  41. $this->montant = $this->montant_vrac + $this->montant_pain ;
  42. }
  43. }
  44. public static function getQuantiteProduit($id_produit, $commandes) {
  45. $quantite = 0 ;
  46. if(isset($commandes) && is_array($commandes) && count($commandes)) {
  47. foreach($commandes as $c) {
  48. foreach($c->commandeProduits as $cp) {
  49. if($cp->id_produit == $id_produit)
  50. $quantite += $cp->quantite ;
  51. }
  52. }
  53. }
  54. return $quantite ;
  55. }
  56. /*
  57. * relations
  58. */
  59. public function getUser() {
  60. return $this->hasOne(User::className(), ['id'=>'id_user']) ;
  61. }
  62. public function getCommandeProduits() {
  63. return $this->hasMany(CommandeProduit::className(), ['id_commande'=>'id'])->with('produit') ;
  64. }
  65. public function getProduction() {
  66. return $this->hasOne(Production::className(), ['id'=>'id_production']) ;
  67. }
  68. public function getPointVente() {
  69. return $this->hasOne(PointVente::className(), ['id'=>'id_point_vente']) ;
  70. }
  71. /**
  72. * @inheritdoc
  73. */
  74. public function rules()
  75. {
  76. return [
  77. [['id_user', 'date', 'id_point_vente','id_production'], 'required','message'=>''],
  78. [['id_user', 'id_point_vente', 'id_production'], 'integer'],
  79. [['date', 'date_update','commentaire'], 'safe']
  80. ];
  81. }
  82. /**
  83. * @inheritdoc
  84. */
  85. public function attributeLabels()
  86. {
  87. return [
  88. 'id' => 'ID',
  89. 'id_user' => 'Id User',
  90. 'date' => 'Date',
  91. 'date_update' => 'Date Update',
  92. 'id_point_vente' => 'Point de vente',
  93. 'id_production' => 'Date de production',
  94. ];
  95. }
  96. public function strListeVrac()
  97. {
  98. $str = '' ;
  99. foreach($this->commandeProduits as $cp) {
  100. if($cp->produit->vrac) {
  101. $str .= $cp->quantite.'&nbsp;'.Html::encode($cp->produit->diminutif).', ' ;
  102. }
  103. }
  104. return substr($str, 0, strlen($str) - 2) ;
  105. }
  106. }