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.

Commande.php 9.5KB

8 yıl önce
8 yıl önce
8 yıl önce
8 yıl önce
8 yıl önce
8 yıl önce
8 yıl önce
8 yıl önce
8 yıl önce
8 yıl önce
8 yıl önce
8 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 $montant_paye = 0 ;
  21. var $poids_pain = 0 ;
  22. var $poids_vrac = 0 ;
  23. const TYPE_AUTO = 'auto' ;
  24. const TYPE_USER = 'user' ;
  25. const TYPE_ADMIN = 'admin' ;
  26. const STATUT_PAYEE = 'payee' ;
  27. const STATUT_IMPAYEE = 'impayee' ;
  28. const STATUT_SURPLUS = 'surplus' ;
  29. /**
  30. * @inheritdoc
  31. */
  32. public static function tableName()
  33. {
  34. return 'commande';
  35. }
  36. public function init() {
  37. if(isset($this->commandeProduits))
  38. {
  39. foreach($this->commandeProduits as $p)
  40. {
  41. if(isset($p->produit) && $p->produit->vrac)
  42. {
  43. $this->montant_vrac += $p->prix * $p->quantite/1000 ;
  44. $this->poids_vrac += $p->quantite/1000 ;
  45. }
  46. else {
  47. $this->montant_pain += $p->prix * $p->quantite ;
  48. if(isset($p->produit))
  49. $this->poids_pain += $p->quantite * $p->produit->poids/1000 ;
  50. }
  51. }
  52. $this->montant = $this->montant_vrac + $this->montant_pain ;
  53. }
  54. if(isset($this->creditHistorique) && !$this->montant_paye)
  55. {
  56. foreach($this->creditHistorique as $ch)
  57. {
  58. if($ch->type == CreditHistorique::TYPE_PAIEMENT)
  59. $this->montant_paye += $ch->montant ;
  60. elseif($ch->type == CreditHistorique::TYPE_REMBOURSEMENT)
  61. $this->montant_paye -= $ch->montant ;
  62. }
  63. }
  64. }
  65. public static function getQuantiteProduit($id_produit, $commandes) {
  66. $quantite = 0 ;
  67. if(isset($commandes) && is_array($commandes) && count($commandes)) {
  68. foreach($commandes as $c) {
  69. foreach($c->commandeProduits as $cp) {
  70. if($cp->id_produit == $id_produit)
  71. $quantite += $cp->quantite ;
  72. }
  73. }
  74. }
  75. return $quantite ;
  76. }
  77. /*
  78. * relations
  79. */
  80. public function getUser() {
  81. return $this->hasOne(User::className(), ['id'=>'id_user']) ;
  82. }
  83. public function getCommandeProduits() {
  84. return $this->hasMany(CommandeProduit::className(), ['id_commande'=>'id'])->with('produit') ;
  85. }
  86. public function getProduction() {
  87. return $this->hasOne(Production::className(), ['id'=>'id_production'])->with('etablissement') ;
  88. }
  89. public function getPointVente() {
  90. return $this->hasOne(PointVente::className(), ['id'=>'id_point_vente'])->with('pointVenteUser') ;
  91. }
  92. public function getCreditHistorique()
  93. {
  94. return $this->hasMany(CreditHistorique::className(), ['id_commande'=>'id']) ;
  95. }
  96. /**
  97. * @inheritdoc
  98. */
  99. public function rules()
  100. {
  101. return [
  102. [['id_user', 'date', 'id_point_vente','id_production'], 'required','message'=>''],
  103. [['id_user', 'id_point_vente', 'id_production'], 'integer'],
  104. [['date', 'date_update','commentaire', 'commentaire_point_vente'], 'safe']
  105. ];
  106. }
  107. /**
  108. * @inheritdoc
  109. */
  110. public function attributeLabels()
  111. {
  112. return [
  113. 'id' => 'ID',
  114. 'id_user' => 'Id User',
  115. 'date' => 'Date',
  116. 'date_update' => 'Date Update',
  117. 'id_point_vente' => 'Point de vente',
  118. 'id_production' => 'Date de production',
  119. ];
  120. }
  121. public function strListeVrac()
  122. {
  123. $str = '' ;
  124. foreach($this->commandeProduits as $cp) {
  125. if($cp->produit->vrac) {
  126. $str .= $cp->quantite.'&nbsp;'.Html::encode($cp->produit->diminutif).', ' ;
  127. }
  128. }
  129. return substr($str, 0, strlen($str) - 2) ;
  130. }
  131. public function getMontantPaye()
  132. {
  133. if($this->montant_paye)
  134. {
  135. return $this->montant_paye ;
  136. }
  137. else {
  138. $historique = CreditHistorique::find()
  139. ->where(['id_commande' => $this->id])
  140. ->all() ;
  141. $montant = 0 ;
  142. foreach($historique as $ch)
  143. {
  144. if($ch->type == CreditHistorique::TYPE_PAIEMENT)
  145. $montant += $ch->montant ;
  146. elseif($ch->type == CreditHistorique::TYPE_REMBOURSEMENT)
  147. $montant -= $ch->montant ;
  148. }
  149. return $montant ;
  150. }
  151. }
  152. public function getMontant($format = false)
  153. {
  154. if($format)
  155. return number_format($this->getMontant(),2).' €' ;
  156. else
  157. return $this->montant ;
  158. }
  159. public function getMontantFormat()
  160. {
  161. return number_format($this->getMontant(),2).' €' ;
  162. }
  163. public function getMontantRestant($format = false)
  164. {
  165. $montant_restant = $this->getMontant() - $this->getMontantPaye() ;
  166. if($format)
  167. return number_format($montant_restant, 2).' €';
  168. else
  169. return $montant_restant ;
  170. }
  171. public function getMontantSurplus($format = false)
  172. {
  173. $montant_surplus = $this->getMontantPaye() - $this->getMontant() ;
  174. if($format)
  175. return number_format($montant_surplus, 2).' €';
  176. else
  177. return $montant_surplus ;
  178. }
  179. public function getDataJson()
  180. {
  181. $commande = Commande::find()->with('commandeProduits')->where(['id' => $this->id])->one() ;
  182. $commande->init() ;
  183. $json_commande = [
  184. 'produits'=> [],
  185. 'montant' => $commande->montant,
  186. 'str_montant' => $commande->getMontantFormat(),
  187. 'montant_paye' => $commande->getMontantPaye(),
  188. 'commentaire' => $commande->commentaire,
  189. ] ;
  190. foreach($commande->commandeProduits as $commande_produit)
  191. {
  192. $json_commande['produits'][$commande_produit->id_produit] = $commande_produit->quantite ;
  193. }
  194. return json_encode($json_commande) ;
  195. }
  196. public function creditHistorique($type, $montant, $id_etablissement, $id_user)
  197. {
  198. $credit_historique = new CreditHistorique ;
  199. $credit_historique->id_user = $this->id_user ;
  200. $credit_historique->id_commande = $this->id ;
  201. $credit_historique->montant = $montant ;
  202. $credit_historique->type = $type ;
  203. $credit_historique->id_etablissement = $id_etablissement ;
  204. $credit_historique->id_user_action = $id_user ;
  205. $credit_historique->save() ;
  206. }
  207. public function getStatutPaiement()
  208. {
  209. // payé
  210. if($this->montant - $this->montant_paye < 0.001)
  211. {
  212. return self::STATUT_PAYEE ;
  213. }
  214. // reste à payer
  215. elseif($this->montant - $this->montant_paye > 0.01)
  216. {
  217. return self::STATUT_IMPAYEE ;
  218. }
  219. // à rembourser
  220. elseif($this->montant - $this->montant_paye < 0.01)
  221. {
  222. return self::STATUT_SURPLUS ;
  223. }
  224. }
  225. public function getResumePanier()
  226. {
  227. if(!isset($this->commandeProduits))
  228. $this->commandeProduits = CommandeProduit::find()->where(['id_commande' => $this->id])->all() ;
  229. $html = '' ;
  230. $count = count($this->commandeProduits);
  231. $i = 0;
  232. foreach($this->commandeProduits as $p)
  233. {
  234. if(isset($p->produit))
  235. {
  236. $html .= $p->quantite.' x '.Html::encode($p->produit->nom) ;
  237. if(++$i != $count) $html .= '<br />' ;
  238. }
  239. }
  240. return $html ;
  241. }
  242. public function getResumePointVente()
  243. {
  244. $html = '' ;
  245. if(isset($this->pointVente))
  246. {
  247. $html .= '<span class="nom-point-vente">'.Html::encode($this->pointVente->nom) .'</span>'
  248. . '<br /><span class="localite">'.Html::encode($this->pointVente->localite).'</span>' ;
  249. if(strlen($this->commentaire_point_vente))
  250. {
  251. $html .= '<div class="commentaire"><span>'.Html::encode($this->commentaire_point_vente).'</span></div>' ;
  252. }
  253. }
  254. else {
  255. $html .= 'Point de vente supprimé' ;
  256. }
  257. return $html ;
  258. }
  259. public function getResumeMontant()
  260. {
  261. $html = '' ;
  262. $html .= number_format($this->montant,2).' €<br />' ;
  263. if($this->montant_paye)
  264. {
  265. if($this->getStatutPaiement() == Commande::STATUT_PAYEE)
  266. {
  267. $html .= '<span class="label label-success">Payée</span>' ;
  268. }
  269. elseif($this->getStatutPaiement() == Commande::STATUT_IMPAYEE)
  270. {
  271. $html .= '<span class="label label-danger">Non payée</span><br />
  272. Reste <strong>'.$this->getMontantRestant(true).'</strong> à payer' ;
  273. }
  274. elseif($this->getStatutPaiement() == Commande::STATUT_SURPLUS)
  275. {
  276. $html .= '<span class="label label-success">Payée</span>' ;
  277. }
  278. }
  279. else {
  280. $html .= '<span class="label label-default">À régler sur place</span>' ;
  281. }
  282. return $html ;
  283. }
  284. }