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 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 7 години
преди 8 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\helpers\Html ;
  5. use common\models\Etablissement ;
  6. /**
  7. * This is the model class for table "commande".
  8. *
  9. * @property integer $id
  10. * @property integer $id_user
  11. * @property string $date
  12. * @property string $date_update
  13. * @property integer $id_point_vente
  14. * @property integer $id_production
  15. */
  16. class Commande extends \yii\db\ActiveRecord
  17. {
  18. var $montant = 0 ;
  19. var $montant_pain = 0 ;
  20. var $montant_vrac = 0 ;
  21. var $montant_paye = 0 ;
  22. var $poids_pain = 0 ;
  23. var $poids_vrac = 0 ;
  24. const TYPE_AUTO = 'auto' ;
  25. const TYPE_USER = 'user' ;
  26. const TYPE_ADMIN = 'admin' ;
  27. const STATUT_PAYEE = 'payee' ;
  28. const STATUT_IMPAYEE = 'impayee' ;
  29. const STATUT_SURPLUS = 'surplus' ;
  30. const ETAT_MODIFIABLE = 'ouverte' ;
  31. const ETAT_PREPARATION = 'preparation' ;
  32. const ETAT_LIVREE = 'livree' ;
  33. /**
  34. * @inheritdoc
  35. */
  36. public static function tableName()
  37. {
  38. return 'commande';
  39. }
  40. public function init() {
  41. if(isset($this->commandeProduits))
  42. {
  43. foreach($this->commandeProduits as $p)
  44. {
  45. if($p->mode_vente == 'unite')
  46. {
  47. $this->montant_pain += $p->prix * $p->quantite ;
  48. }
  49. elseif($p->mode_vente == 'poids')
  50. {
  51. $this->montant_pain += $p->prix * $p->quantite/1000 ;
  52. }
  53. }
  54. $this->montant = $this->montant_vrac + $this->montant_pain ;
  55. }
  56. if(isset($this->creditHistorique) && !$this->montant_paye)
  57. {
  58. foreach($this->creditHistorique as $ch)
  59. {
  60. if($ch->type == CreditHistorique::TYPE_PAIEMENT)
  61. $this->montant_paye += $ch->montant ;
  62. elseif($ch->type == CreditHistorique::TYPE_REMBOURSEMENT)
  63. $this->montant_paye -= $ch->montant ;
  64. }
  65. }
  66. }
  67. public static function getQuantiteProduit($id_produit, $commandes) {
  68. $quantite = 0 ;
  69. if(isset($commandes) && is_array($commandes) && count($commandes)) {
  70. foreach($commandes as $c) {
  71. foreach($c->commandeProduits as $cp) {
  72. if($cp->id_produit == $id_produit)
  73. $quantite += $cp->quantite ;
  74. }
  75. }
  76. }
  77. return $quantite ;
  78. }
  79. /*
  80. * relations
  81. */
  82. public function getUser() {
  83. return $this->hasOne(User::className(), ['id'=>'id_user']) ;
  84. }
  85. public function getCommandeProduits() {
  86. return $this->hasMany(CommandeProduit::className(), ['id_commande'=>'id'])->with('produit') ;
  87. }
  88. public function getProduction() {
  89. return $this->hasOne(Production::className(), ['id'=>'id_production'])->with('etablissement') ;
  90. }
  91. public function getPointVente() {
  92. return $this->hasOne(PointVente::className(), ['id'=>'id_point_vente'])->with('pointVenteUser') ;
  93. }
  94. public function getCreditHistorique()
  95. {
  96. return $this->hasMany(CreditHistorique::className(), ['id_commande'=>'id']) ;
  97. }
  98. /**
  99. * @inheritdoc
  100. */
  101. public function rules()
  102. {
  103. return [
  104. [['id_user', 'date', 'id_point_vente','id_production'], 'required','message'=>''],
  105. [['id_user', 'id_point_vente', 'id_production'], 'integer'],
  106. [['date', 'date_update','commentaire', 'commentaire_point_vente'], 'safe']
  107. ];
  108. }
  109. /**
  110. * @inheritdoc
  111. */
  112. public function attributeLabels()
  113. {
  114. return [
  115. 'id' => 'ID',
  116. 'id_user' => 'Id User',
  117. 'date' => 'Date',
  118. 'date_update' => 'Date Update',
  119. 'id_point_vente' => 'Point de vente',
  120. 'id_production' => 'Date de production',
  121. ];
  122. }
  123. public function strListeVrac()
  124. {
  125. $str = '' ;
  126. foreach($this->commandeProduits as $cp) {
  127. if($cp->produit->vrac) {
  128. $str .= $cp->quantite.'&nbsp;'.Html::encode($cp->produit->diminutif).', ' ;
  129. }
  130. }
  131. return substr($str, 0, strlen($str) - 2) ;
  132. }
  133. public function getMontantPaye()
  134. {
  135. if($this->montant_paye)
  136. {
  137. return $this->montant_paye ;
  138. }
  139. else {
  140. $historique = CreditHistorique::find()
  141. ->where(['id_commande' => $this->id])
  142. ->all() ;
  143. $montant = 0 ;
  144. foreach($historique as $ch)
  145. {
  146. if($ch->type == CreditHistorique::TYPE_PAIEMENT)
  147. $montant += $ch->montant ;
  148. elseif($ch->type == CreditHistorique::TYPE_REMBOURSEMENT)
  149. $montant -= $ch->montant ;
  150. }
  151. return $montant ;
  152. }
  153. }
  154. public function getMontant($format = false)
  155. {
  156. if($format)
  157. return number_format($this->getMontant(),2).' €' ;
  158. else
  159. return $this->montant ;
  160. }
  161. public function getMontantFormat()
  162. {
  163. return number_format($this->getMontant(),2).' €' ;
  164. }
  165. public function getMontantRestant($format = false)
  166. {
  167. $montant_restant = $this->getMontant() - $this->getMontantPaye() ;
  168. if($format)
  169. return number_format($montant_restant, 2).' €';
  170. else
  171. return $montant_restant ;
  172. }
  173. public function getMontantSurplus($format = false)
  174. {
  175. $montant_surplus = $this->getMontantPaye() - $this->getMontant() ;
  176. if($format)
  177. return number_format($montant_surplus, 2).' €';
  178. else
  179. return $montant_surplus ;
  180. }
  181. public function getDataJson()
  182. {
  183. $commande = Commande::find()->with('commandeProduits')->where(['id' => $this->id])->one() ;
  184. $commande->init() ;
  185. $json_commande = [
  186. 'produits'=> [],
  187. 'montant' => $commande->montant,
  188. 'str_montant' => $commande->getMontantFormat(),
  189. 'montant_paye' => $commande->getMontantPaye(),
  190. 'commentaire' => $commande->commentaire,
  191. ] ;
  192. foreach($commande->commandeProduits as $commande_produit)
  193. {
  194. $json_commande['produits'][$commande_produit->id_produit] = $commande_produit->quantite ;
  195. }
  196. return json_encode($json_commande) ;
  197. }
  198. public function creditHistorique($type, $montant, $id_etablissement, $id_user)
  199. {
  200. $credit_historique = new CreditHistorique ;
  201. $credit_historique->id_user = $this->id_user ;
  202. $credit_historique->id_commande = $this->id ;
  203. $credit_historique->montant = $montant ;
  204. $credit_historique->type = $type ;
  205. $credit_historique->id_etablissement = $id_etablissement ;
  206. $credit_historique->id_user_action = $id_user ;
  207. $credit_historique->save() ;
  208. }
  209. public function getStatutPaiement()
  210. {
  211. // à rembourser
  212. if($this->getMontant() - $this->getMontantPaye() < 0)
  213. {
  214. return self::STATUT_SURPLUS ;
  215. }
  216. // payé
  217. elseif($this->getMontant() - $this->getMontantPaye() < 0.001)
  218. {
  219. return self::STATUT_PAYEE ;
  220. }
  221. // reste à payer
  222. elseif($this->getMontant() - $this->getMontantPaye() > 0.01)
  223. {
  224. return self::STATUT_IMPAYEE ;
  225. }
  226. }
  227. public function getResumePanier()
  228. {
  229. if(!isset($this->commandeProduits))
  230. $this->commandeProduits = CommandeProduit::find()->where(['id_commande' => $this->id])->all() ;
  231. $html = '' ;
  232. $count = count($this->commandeProduits);
  233. $i = 0;
  234. foreach($this->commandeProduits as $p)
  235. {
  236. if(isset($p->produit))
  237. {
  238. $html .= $p->quantite.' x '.Html::encode($p->produit->nom) ;
  239. if(++$i != $count) $html .= '<br />' ;
  240. }
  241. }
  242. return $html ;
  243. }
  244. public function getResumePointVente()
  245. {
  246. $html = '' ;
  247. if(isset($this->pointVente))
  248. {
  249. $html .= '<span class="nom-point-vente">'.Html::encode($this->pointVente->nom) .'</span>'
  250. . '<br /><span class="localite">'.Html::encode($this->pointVente->localite).'</span>' ;
  251. if(strlen($this->commentaire_point_vente))
  252. {
  253. $html .= '<div class="commentaire"><span>'.Html::encode($this->commentaire_point_vente).'</span></div>' ;
  254. }
  255. }
  256. else {
  257. $html .= 'Point de vente supprimé' ;
  258. }
  259. return $html ;
  260. }
  261. public function getStrMontant()
  262. {
  263. return number_format($this->montant,2).' €' ;
  264. }
  265. public function getResumeMontant()
  266. {
  267. $html = '' ;
  268. $html .= $this->getStrMontant().'<br />' ;
  269. if($this->montant_paye)
  270. {
  271. if($this->getStatutPaiement() == Commande::STATUT_PAYEE)
  272. {
  273. $html .= '<span class="label label-success">Payée</span>' ;
  274. }
  275. elseif($this->getStatutPaiement() == Commande::STATUT_IMPAYEE)
  276. {
  277. $html .= '<span class="label label-danger">Non payée</span><br />
  278. Reste <strong>'.$this->getMontantRestant(true).'</strong> à payer' ;
  279. }
  280. elseif($this->getStatutPaiement() == Commande::STATUT_SURPLUS)
  281. {
  282. $html .= '<span class="label label-success">Payée</span>' ;
  283. }
  284. }
  285. else {
  286. $html .= '<span class="label label-default">À régler sur place</span>' ;
  287. }
  288. return $html ;
  289. }
  290. public function getStrUser()
  291. {
  292. if(isset($this->user))
  293. {
  294. return Html::encode($this->user->prenom.' '.$this->user->nom) ;
  295. }
  296. elseif(strlen($this->username)) {
  297. return Html::encode($this->username) ;
  298. }
  299. else {
  300. return 'Client introuvable' ;
  301. }
  302. }
  303. public static function findBy($params = [])
  304. {
  305. if(!isset($params['id_etablissement']))
  306. $params['id_etablissement'] = Yii::$app->user->identity->id_etablissement ;
  307. $commandes = Commande::find()
  308. ->with('commandeProduits', 'creditHistorique', 'pointVente')
  309. ->joinWith(['production','user'])
  310. ->where(['production.id_etablissement' => $params['id_etablissement']]) ;
  311. if(isset($params['condition']))
  312. $commandes = $commandes->andWhere($params['condition']);
  313. if(isset($params['date']))
  314. $commandes = $commandes->andWhere(['production.date' => $params['date']]);
  315. if(isset($params['type']))
  316. $commandes = $commandes->andWhere(['commande.type' => $params['type']]);
  317. if(isset($params['orderby']))
  318. $commandes = $commandes->orderBy($params['orderby']);
  319. else
  320. $commandes = $commandes->orderBy('date ASC');
  321. if(isset($params['limit']))
  322. $commandes = $commandes->limit($params['limit']) ;
  323. $commandes = $commandes->all() ;
  324. return $commandes ;
  325. }
  326. public function getEtat()
  327. {
  328. $delai_commande = Etablissement::getConfig('delai_commande',$this->production->id_etablissement) ;
  329. $heure_limite = Etablissement::getConfig('heure_limite_commande',$this->production->id_etablissement) ;
  330. $date_commande = strtotime($this->production->date) ;
  331. $date_today = strtotime(date('Y-m-d'));
  332. $heure_today = date('G') ;
  333. $nb_jours = (int) (($date_commande - $date_today) / (24 * 60 * 60)) ;
  334. if($nb_jours <= 0)
  335. {
  336. return self::ETAT_LIVREE ;
  337. }
  338. elseif($nb_jours >= $delai_commande &&
  339. ($nb_jours != $delai_commande ||
  340. ($nb_jours == $delai_commande && $heure_today < $heure_limite)))
  341. {
  342. return self::ETAT_MODIFIABLE ;
  343. }
  344. return self::ETAT_PREPARATION ;
  345. }
  346. }