您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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