/**
  * function ::create ($data)
  */
 public static function create($data)
 {
     //        $now = strtotime('now');
     //        $username = Yii::$app->user->identity->username;
     $model = new PurchaseOrderDetail();
     if ($model->load($data)) {
         if ($model->save()) {
             return $model;
         }
         $model->getErrors();
         //            return $model;
     }
     return false;
 }
 /**
  * Creates a new PurchaseOrder model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $username = Yii::$app->user->identity ? Yii::$app->user->identity->username : null;
     $model = new PurchaseOrder();
     if (Yii::$app->request->isPost) {
         if (is_object($cart = Yii::$app->session->get('cart'))) {
             if ($cart->totalQuantity() > 0) {
                 if ($model = PurchaseOrder::create(Yii::$app->request->post())) {
                     //                        if ($model->customer_id !== null) {
                     $customer = Customer::findOne(['id' => $model->customer_id, 'status' => Customer::STATUS_ACTIVE]);
                     //                        }
                     foreach ($cart->data as $item) {
                         $p = $item['product'];
                         $qty = $item['quantity'];
                         if (PurchaseOrderDetail::create(['PurchaseOrderDetail' => ['purchase_order_id' => $model->id, 'product_id' => $p->id, 'product_name' => $p->t('name'), 'product_code' => $p->code, 'quantity' => $qty, 'unit_price' => $p->price, 'discount_percent' => round(100 * ($p->original_price - $p->price) / $p->original_price, 4), 'is_offer' => $p->original_price > $p->price ? 1 : 0]]) !== false) {
                             if ($customer) {
                                 $customer->total_purchase_products += $qty;
                                 $customer->total_purchase_value += $qty * $p->price;
                             }
                             $p->available_quantity -= $qty;
                             $p->order_quantity += $qty;
                             //                                $p->total_revenue += $qty * $p->price;
                             $p->save();
                         }
                     }
                     if ($customer) {
                         $customer->total_purchase_orders++;
                         $customer->save(false);
                     }
                     try {
                         Yii::$app->orderMailer->compose(['html' => 'purchaseOrderReview-html', 'text' => 'purchaseOrderReview-text'], ['purchase_order' => $model])->setFrom([Yii::$app->params['order_email'] => Yii::$app->name])->setTo($model->customer_email)->setSubject(I18n::t('Purchase order review'))->send();
                     } catch (Exception $e) {
                     }
                     Yii::$app->session->set('cart', new Cart());
                     $model = new PurchaseOrder();
                     Yii::$app->session->setFlash('success', I18n::t('Your purchase order submitted successfully') . '. ' . I18n::t('Thank you'));
                     //                        $this->goHome();
                 } else {
                     Yii::$app->session->setFlash('error', I18n::t('Some errors occurred') . '. ' . I18n::t('Please try again') . '.');
                 }
             } else {
                 Yii::$app->session->setFlash('error', I18n::t('Your cart is empty') . '. ' . I18n::t('Please try again') . '.');
             }
         } else {
             Yii::$app->session->setFlash('error', I18n::t('Some errors occurred') . '. ' . I18n::t('Please try again') . '.');
         }
     }
     return $this->render('create', ['username' => $username, 'model' => $model]);
 }
?>
</th>
                <th><?php 
echo I18n::t('Unit price');
?>
</th>
                <th><?php 
echo I18n::t('Quantity');
?>
</th>
            </tr>
        </thead>
        <tbody style="background:#fff">
            <?php 
$total_value = 0;
foreach (PurchaseOrderDetail::find()->where(['purchase_order_id' => $purchase_order->id])->all() as $item) {
    ?>
            <tr>
                <td><?php 
    echo $item->product_code;
    ?>
</td>
                <td><?php 
    echo $item->product_name;
    ?>
</td>
                <td><?php 
    echo I18n::currency($item->unit_price);
    ?>
</td>
                <td><?php 
Beispiel #4
0
 /**
  * @return ActiveQuery
  */
 public function getPurchaseOrderDetails()
 {
     return $this->hasMany(PurchaseOrderDetail::className(), ['purchase_order_id' => 'id']);
 }