/**
  * Creates a new Payment model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate($invoice_id = null)
 {
     $model = new Payment();
     $invoice = $invoice_id != null ? \backend\models\accounting\Invoice::findOne($invoice_id) : null;
     $model->status = Payment::STATUS_DRAFT;
     $model->date = date('Y-m-d');
     $model->type = Payment::TYPE_OUTGOING;
     if ($invoice != null) {
         $model->vendor_id = $invoice->vendor_id;
         $model->vendor_name = $invoice->vendor->name;
         $pay_item = new \backend\models\accounting\PaymentDtl();
         $pay_item->invoice_id = $invoice_id;
         $pay_item->value = $invoice->sisa;
         $model->items = [$pay_item];
     }
     if ($model->load(Yii::$app->request->post())) {
         $transaction = Yii::$app->db->beginTransaction();
         try {
             $model->items = Yii::$app->request->post('PaymentDtl', []);
             if ($model->save()) {
                 $transaction->commit();
                 return $this->redirect(['view', 'id' => $model->id]);
             }
         } catch (\Exception $exc) {
             $transaction->rollBack();
             throw $exc;
         }
         $transaction->rollBack();
     }
     return $this->render('create', ['model' => $model]);
 }
Example #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = InvoiceModel::find();
     $query->open();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['due_date' => SORT_ASC]]]);
     $this->load($params);
     if (!$this->validate()) {
         $query->where('1=0');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'date' => $this->date, 'due_date' => $this->due_date, 'type' => $this->type, 'vendor_id' => $this->vendor_id, 'reff_type' => $this->reff_type, 'reff_id' => $this->reff_id, 'status' => $this->status, 'value' => $this->value, 'tax_value' => $this->tax_value, 'created_at' => $this->created_at, 'created_by' => $this->created_by, 'updated_at' => $this->updated_at, 'updated_by' => $this->updated_by]);
     $query->andFilterWhere(['like', 'number', $this->number])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'tax_type', $this->tax_type]);
     return $dataProvider;
 }
Example #3
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getInvoices()
 {
     return $this->hasMany(Invoice::className(), ['id' => 'invoice_id'])->viaTable('{{%payment_dtl}}', ['payment_id' => 'id']);
 }
Example #4
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getInvoice()
 {
     return $this->hasOne(\backend\models\accounting\Invoice::className(), ['id' => 'reff_id']);
 }
Example #5
0
 /**
  * Deletes an existing Sales model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  * @param integer $id
  * @return mixed
  */
 public function actionDelete($id)
 {
     $model = $this->findModel($id);
     if ($model->status == Sales::STATUS_DRAFT) {
         $model->delete();
         return $this->redirect(['index']);
     }
     $transaction = Yii::$app->db->beginTransaction();
     try {
         // gl
         $gl = GlHeader::findOne(['reff_type' => GlHeader::REFF_SALES, 'reff_id' => $id]);
         // movement
         $movement = $gl != null ? GoodsMovement::findOne(['reff_type' => GoodsMovement::REFF_SALES, 'reff_id' => $id]) : null;
         // invoice from movement
         $invoice = $movement != null ? Invoice::findOne(['reff_type' => Invoice::REFF_GOODS_MOVEMENT, 'reff_id' => $movement->id]) : null;
         // payment invoive
         $payments = $invoice != null ? $invoice->payments : [];
         foreach ($payments as $payment) {
             if (!$payment->delete()) {
                 throw new UserException('Cannot delete payment');
             }
         }
         if (count($payments) > 0 && $invoice->delete() && $movement->delete() && $gl->reserve() && $model->delete()) {
             //do nothing
             $transaction->commit();
             return $this->redirect(['index']);
         } else {
             throw new UserException('Something error');
         }
     } catch (\Exception $exc) {
         $transaction->rollBack();
         throw $exc;
     }
 }
 /**
  * Finds the Invoice model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Invoice the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Invoice::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #7
0
 /**
  *
  * @param array $options
  * @return Invoice|boolean
  */
 public function createInvoice($options = [])
 {
     if ($this->status == self::STATUS_RELEASED) {
         $oldInvoice = Invoice::findOne(['reff_type' => Invoice::REFF_GOODS_MOVEMENT, 'reff_id' => $this->id, 'status' => Invoice::STATUS_RELEASED]);
         if ($oldInvoice !== null) {
             return false;
         }
         $invoice = new Invoice();
         $invoice->attributes = array_merge(['date' => date('Y-m-d'), 'due_date' => date('Y-m-d', time() + 30 * 24 * 3600)], $options);
         $invoice->reff_type = Invoice::REFF_GOODS_MOVEMENT;
         $invoice->reff_id = $this->id;
         $invoice->vendor_id = $this->vendor_id;
         $invoice->type = $this->type == self::TYPE_RECEIVE ? Invoice::TYPE_INCOMING : Invoice::TYPE_OUTGOING;
         $invoice->status = Invoice::STATUS_RELEASED;
         $items = [];
         /* @var $item GoodsMovementDtl */
         $total = 0;
         foreach ($this->items as $item) {
             $p_id = $item->product_id;
             $pu = ProductUom::findOne(['product_id' => $p_id, 'uom_id' => $item->uom_id]);
             $qty = ($pu ? $pu->isi : 1) * $item->qty;
             $total += $qty * $item->value;
             $items[] = ['item_type' => 10, 'item_id' => $item->product_id, 'qty' => $qty, 'item_value' => $item->value];
         }
         $invoice->value = $total;
         $invoice->items = $items;
         return $invoice;
     }
     return false;
 }
Example #8
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getInvoice()
 {
     return $this->hasOne(Invoice::className(), ['id' => 'invoice_id']);
 }