Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

383 lines
12KB

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