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.

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