- <?php
-
-
-
- namespace common\models;
-
- use Yii;
- use yii\helpers\Html;
- use common\models\Producer;
- use common\components\ActiveRecordCommon ;
-
-
- class Order extends ActiveRecordCommon
- {
- var $amount = 0 ;
- var $paid_amount = 0 ;
- var $weight = 0 ;
-
- const ORIGIN_AUTO = 'auto';
- const ORIGIN_USER = 'user';
- const ORIGIN_ADMIN = 'admin';
-
- const PAYMENT_PAID = 'paid';
- const PAYMENT_UNPAID = 'unpaid';
- const PAYMENT_SURPLUS = 'surplus';
-
- const AMOUNT_TOTAL = 'total' ;
- const AMOUNT_PAID = 'paid' ;
- const AMOUNT_REMAINING = 'remaining' ;
- const AMOUNT_SURPLUS = 'surplus' ;
-
- const STATE_OPEN = 'open';
- const STATE_PREPARATION = 'preparation';
- const STATE_DELIVERED = 'delivered';
-
-
-
- public static function tableName()
- {
- return 'order';
- }
-
-
-
- public function rules()
- {
- return [
- [['id_user', 'date', 'id_point_sale', 'id_distribution'], 'required', 'message' => ''],
- [['id_user', 'id_point_sale', 'id_distribution','id_subscription'], 'integer'],
- [['auto_payment'], 'boolean'],
- [['date', 'date_update', 'comment', 'comment_point_sale'], 'safe']
- ];
- }
-
-
-
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'id_user' => 'Id User',
- 'date' => 'Date',
- 'date_update' => 'Date de modification',
- 'id_point_sale' => 'Point de vente',
- 'id_distribution' => 'Date de distribution',
- 'id_subscription' => 'Abonnement',
- ];
- }
-
-
-
-
- public function getUser()
- {
- return $this->hasOne(User::className(), ['id' => 'id_user']);
- }
-
- public function getProductOrder()
- {
- return $this->hasMany(ProductOrder::className(),['id_order' => 'id'])
- ->with('product');
- }
-
- public function getDistribution()
- {
- return $this->hasOne(Distribution::className(), ['id' => 'id_distribution'])
- ->with('producer');
- }
-
- public function getPointSale()
- {
- return $this->hasOne(PointSale::className(), ['id' => 'id_point_sale'])
- ->with('userPointSale');
- }
-
- public function getCreditHistory()
- {
- return $this->hasMany(CreditHistory::className(), ['id_order' => 'id']);
- }
-
- public function getSubscription()
- {
- return $this->hasOne(Subscription::className(), ['id' => 'id_subscription'])
- ->with('productSubscription');
- }
-
-
-
- public static function defaultOptionsSearch() {
- return [
- 'with' => ['productOrder','productOrder.product','creditHistory','creditHistory.userAction' , 'pointSale'],
- 'join_with' => ['distribution', 'user', 'user.userProducer'],
- 'orderby' => 'order.date ASC',
- 'attribute_id_producer' => 'distribution.id_producer'
- ] ;
- }
-
-
-
- public function init()
- {
- $this->initAmount() ;
- $this->initPaidAmount() ;
-
- return $this ;
- }
-
-
-
- public function initAmount() {
- if (isset($this->productOrder)) {
- foreach ($this->productOrder as $productOrder) {
- $this->amount += $productOrder->price * $productOrder->quantity ;
- if ($productOrder->unit == 'piece') {
- if(isset($productOrder->product)) {
- $this->weight += ($productOrder->quantity * $productOrder->product->weight) / 1000 ;
- }
- }
- else {
- $this->weight += $productOrder->quantity ;
- }
- }
- }
- }
-
-
-
- public function initPaidAmount()
- {
- $history = CreditHistory::find()
- ->where(['id_order' => $this->id])
- ->all();
-
- $this->paid_amount = 0 ;
-
- if(count($history)) {
- foreach ($history as $ch) {
- if ($ch->type == CreditHistory::TYPE_PAYMENT) {
- $this->paid_amount += $ch->amount;
- }
- elseif ($ch->type == CreditHistory::TYPE_REFUND) {
- $this->paid_amount -= $ch->amount;
- }
- }
- }
- }
-
- public function delete() {
-
-
- $amountPaid = $this->getAmount(Order::AMOUNT_PAID);
- if ($amountPaid > 0.01) {
- $this->saveCreditHistory(
- CreditHistory::TYPE_REFUND,
- $amountPaid,
- Producer::getId(),
- $this->id_user,
- User::getCurrentId()
- );
- }
-
-
- if(Producer::getConfig('option_behavior_cancel_order') == Producer::BEHAVIOR_DELETE_ORDER_DELETE ||
- (Producer::getConfig('option_behavior_cancel_order') == Producer::BEHAVIOR_DELETE_ORDER_STATUS && strlen($this->date_delete)) ) {
- ProductOrder::deleteAll(['id_order' => $this->id]);
- return parent::delete() ;
- }
-
- elseif(Producer::getConfig('option_behavior_cancel_order') == Producer::BEHAVIOR_DELETE_ORDER_STATUS) {
- $this->date_delete = date('Y-m-d H:i:s');
- return $this->save() ;
- }
- }
-
-
-
- public function getAmount($type = self::AMOUNT_TOTAL, $format = false)
- {
- switch($type) {
- case self::AMOUNT_TOTAL :
- $amount = $this->amount ;
- break ;
- case self::AMOUNT_PAID :
- $this->initPaidAmount() ;
- $amount = $this->paid_amount ;
- break ;
- case self::AMOUNT_REMAINING :
- $amount = $this->getAmount(self::AMOUNT_TOTAL)
- - $this->getAmount(self::AMOUNT_PAID) ;
- break ;
- case self::AMOUNT_SURPLUS :
- $amount = $this->getAmount(self::AMOUNT_PAID)
- - $this->getAmount(self::AMOUNT_TOTAL) ;
- break ;
- }
-
- if ($format) {
- return number_format($amount, 2) . ' €';
- }
- else {
- return $amount;
- }
- }
-
-
-
- public function getDataJson()
- {
- $order = Order::searchOne(['order.id' => $this->id]) ;
-
- $jsonOrder = [] ;
- if($order) {
- $jsonOrder = [
- 'products' => [],
- 'amount' => $order->amount,
- 'str_amount' => $order->getAmount(self::AMOUNT_TOTAL, true),
- 'paid_amount' => $order->getAmount(self::AMOUNT_PAID),
- 'comment' => $order->comment,
- ];
-
- foreach ($order->productOrder as $productOrder) {
- $jsonOrder['products'][$productOrder->id_product] = $productOrder->quantity;
- }
- }
-
- return json_encode($jsonOrder);
- }
-
-
-
- public function saveCreditHistory($type, $amount, $idProducer, $idUser, $idUserAction)
- {
- $creditHistory = new CreditHistory;
- $creditHistory->id_user = $this->id_user;
- $creditHistory->id_order = $this->id;
- $creditHistory->amount = $amount;
- $creditHistory->type = $type;
- $creditHistory->id_producer = $idProducer;
- $creditHistory->id_user_action = $idUserAction;
- $creditHistory->populateRelation('order', $this) ;
- $creditHistory->populateRelation('user', User::find()->where(['id' => $this->id_user])->one()) ;
- $creditHistory->save();
- }
-
-
-
- public function processCredit()
- {
- if($this->id_user) {
- $paymentStatus = $this->getPaymentStatus() ;
-
- if($paymentStatus == self::PAYMENT_PAID) {
- return true;
- }
- elseif($paymentStatus == self::PAYMENT_SURPLUS) {
- $type = CreditHistory::TYPE_REFUND ;
- $amount = $this->getAmount(self::AMOUNT_SURPLUS) ;
- }
- elseif($paymentStatus == self::PAYMENT_UNPAID) {
- $type = CreditHistory::TYPE_PAYMENT ;
- $amount = $this->getAmount(self::AMOUNT_REMAINING) ;
- }
-
- $this->saveCreditHistory(
- $type,
- $amount,
- Producer::getId(),
- $this->id_user,
- User::getCurrentId()
- );
- }
- }
-
-
-
- public function getPaymentStatus()
- {
-
- if ($this->getAmount() - $this->getAmount(self::AMOUNT_PAID) < 0.01 &&
- $this->getAmount() - $this->getAmount(self::AMOUNT_PAID) > -0.01)
- {
- return self::PAYMENT_PAID ;
- }
-
- elseif ($this->getAmount() - $this->getAmount(self::AMOUNT_PAID) <= -0.01) {
- return self::PAYMENT_SURPLUS ;
- }
-
- elseif ($this->getAmount() - $this->getAmount(self::AMOUNT_PAID) >= 0.01) {
- return self::PAYMENT_UNPAID ;
- }
- }
-
-
-
- public function getCartSummary()
- {
- if (!isset($this->productOrder)) {
- $this->productOrder = productOrder::find()->where(['id_order' => $this->id])->all();
- }
-
- $html = '';
- $count = count($this->productOrder);
- $i = 0;
- foreach ($this->productOrder as $p) {
- if (isset($p->product)) {
- $html .= Html::encode($p->product->name) .' ('. $p->quantity .' '.Product::strUnit($p->unit, 'wording_short', true).')';
- if (++$i != $count) {
- $html .= '<br />';
- }
- }
- }
-
- return $html;
- }
-
-
-
- public function getPointSaleSummary()
- {
- $html = '';
-
- if (isset($this->pointSale)) {
- $html .= '<span class="name-point-sale">' .
- Html::encode($this->pointSale->name) .
- '</span>' .
- '<br /><span class="locality">'
- . Html::encode($this->pointSale->locality)
- . '</span>';
-
- if (strlen($this->comment_point_sale)) {
- $html .= '<div class="comment"><span>'
- . Html::encode($this->comment_point_sale)
- . '</span></div>';
- }
- } else {
- $html .= 'Point de vente supprimé';
- }
-
- return $html;
- }
-
-
-
- public function getAmountSummary()
- {
- $html = '';
-
- $html .= $this->getAmount(self::AMOUNT_TOTAL, true) . '<br />';
-
- if ($this->paid_amount) {
- if ($this->getPaymentStatus() == Order::PAYMENT_PAID) {
- $html .= '<span class="label label-success">Payée</span>';
- } elseif ($this->getPaymentStatus() == Order::PAYMENT_UNPAID) {
- $html .= '<span class="label label-danger">Non payée</span><br />
- Reste <strong>' . $this->getAmount(Order::AMOUNT_REMAINING, true) . '</strong> à payer';
- } elseif ($this->getPaymentStatus() == Order::PAYMENT_SURPLUS) {
- $html .= '<span class="label label-success">Payée</span>';
- }
- }
- else {
- $html .= '<span class="label label-default">Non réglé</span>';
- }
-
- return $html;
- }
-
-
-
- public function getStrUser()
- {
- if (isset($this->user)) {
- return Html::encode($this->user->name . ' ' . $this->user->lastname);
- } elseif (strlen($this->username)) {
- return Html::encode($this->username);
- } else {
- return 'Client introuvable';
- }
- }
-
-
-
- public function getState()
- {
- $orderDelay = Producer::getConfig(
- 'order_delay',
- $this->distribution->id_producer
- );
-
- $orderDeadline = Producer::getConfig(
- 'order_deadline',
- $this->distribution->id_producer
- );
-
- $orderDate = strtotime($this->distribution->date);
- $today = strtotime(date('Y-m-d'));
- $todayHour = date('G');
-
- $nbDays = (int) (($orderDate - $today) / (24 * 60 * 60));
-
- if ($nbDays <= 0) {
- return self::STATE_DELIVERED;
- }
- elseif ($nbDays >= $orderDelay &&
- ($nbDays != $orderDelay ||
- ($nbDays == $orderDelay && $todayHour < $orderDeadline)))
- {
- return self::STATE_OPEN;
- }
-
- return self::STATE_PREPARATION ;
- }
-
-
-
- public function getStrOrigin($withLabel = false)
- {
- $classLabel = '';
- $str = '';
-
- if ($this->origin == self::ORIGIN_USER) {
- $classLabel = 'success';
- $str = 'Client';
- }
- elseif ($this->origin == self::ORIGIN_AUTO) {
- $classLabel = 'default';
- $str = 'Auto';
- }
- elseif ($this->origin == self::ORIGIN_ADMIN) {
- $classLabel = 'warning';
- $str = 'Vous';
- }
-
- if ($withLabel) {
- return '<span class="label label-' . $classLabel . '">'
- . $str . '</span>';
- }
- else {
- return $str;
- }
- }
-
-
-
- public function getStrHistory()
- {
- $arr = [
- 'class' => 'create',
- 'glyphicon' => 'plus',
- 'str' => 'Ajoutée',
- 'date' => $this->date
- ] ;
-
- if(!is_null($this->date_update)) {
- $arr = [
- 'class' => 'update',
- 'glyphicon' => 'pencil',
- 'str' => 'Modifiée',
- 'date' => $this->date_update
- ] ;
- }
- if(!is_null($this->date_delete)) {
- $arr = [
- 'class' => 'delete',
- 'glyphicon' => 'remove',
- 'str' => 'Annulée',
- 'date' => $this->date_delete
- ] ;
- }
-
- $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>' ;
-
- return $html ;
- }
-
-
-
- public function getClassHistory()
- {
- if(!is_null($this->date_delete)) {
- return 'commande-delete' ;
- }
- if(!is_null($this->date_update)) {
- return 'commande-update' ;
- }
- return 'commande-create' ;
- }
-
-
-
- public static function getProductQuantity($idProduct, $orders, $ignoreCancel = false, $unit = null)
- {
- $quantity = 0;
-
- if (isset($orders) && is_array($orders) && count($orders)) {
- foreach ($orders as $c) {
- if(is_null($c->date_delete) || $ignoreCancel) {
- foreach ($c->productOrder as $po) {
- if ($po->id_product == $idProduct &&
- ((is_null($unit) && $po->product->unit == $po->unit) || (!is_null($unit) && strlen($unit) && $po->unit == $unit))) {
- $quantity += $po->quantity ;
- }
- }
- }
- }
- }
-
- return $quantity ;
- }
-
-
-
- public static function searchBy($params = [], $options = [])
- {
- $orders = parent::searchBy($params, $options) ;
-
-
-
-
- if(is_array($orders)) {
- if(count($orders)) {
- foreach($orders as $order) {
- $order->init() ;
- }
- return $orders ;
- }
- }
- else {
- $order = $orders ;
- if(is_a($order, 'common\models\Order')) {
- return $order->init() ;
- }
-
- else {
- return $order ;
- }
- }
-
- return false ;
- }
-
-
-
- public function countProducts()
- {
- $count = 0 ;
- if($this->productOrder && is_array($this->productOrder)) {
- foreach($this->productOrder as $productOrder) {
- if($productOrder->unit == 'piece') {
- $count ++ ;
- }
- else {
- $count += $productOrder->quantity ;
- }
- }
- }
- return $count ;
- }
-
-
-
- public function getBlockDate()
- {
- return '<div class="block-date">
- <div class="day">'.strftime('%A', strtotime($this->distribution->date)).'</div>
- <div class="num">'.date('d', strtotime($this->distribution->date)).'</div>
- <div class="month">'.strftime('%B', strtotime($this->distribution->date)).'</div>
- </div>' ;
- }
-
- }
|