Exemplo n.º 1
0
 /**
  * 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]);
 }
Exemplo n.º 2
0
 /**
  * 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.');
     }
 }
Exemplo n.º 3
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;
     }
 }
Exemplo n.º 4
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;
 }