Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

429 lines
14KB

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