public function search($params) { $query = PurchaseModel::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id_purchase' => $this->id_purchase, 'id_supplier' => $this->id_supplier, 'id_branch' => $this->id_branch, 'purchase_date' => $this->purchase_date, 'status' => $this->status, 'create_by' => $this->create_by, 'update_by' => $this->update_by]); $query->andFilterWhere(['like', 'purchase_num', $this->purchase_num])->andFilterWhere(['like', 'purchase_value', $this->purchase_value])->andFilterWhere(['like', 'item_discount', $this->item_discount])->andFilterWhere(['like', 'create_at', $this->create_at])->andFilterWhere(['like', 'update_at', $this->update_at]); return $dataProvider; }
/** * Finds the Purchase model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Purchase the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Purchase::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getIdPurchase() { return $this->hasOne(Purchase::className(), ['id_purchase' => 'id_purchase']); }
/** * * @param \yii\base\Application $app * @param array $config */ protected function initialize($app, $config) { if ($app instanceof \yii\web\Application) { AppHelper::registerAccessHandler(models\Purchase::className(), components\AccessHandler::className()); } }
public function createFromPurchase($data, $model = null) { $inv_vals = ArrayHelper::map($data['details'], 'id_purchase', 'value'); $ids = array_keys($inv_vals); $vendors = []; $purchase_values = Purchase::find()->where(['id_purchase' => $ids])->indexBy('id_purchase')->asArray()->all(); $vendor = null; foreach ($purchase_values as $row) { $vendor = $row['id_supplier']; $vendors[$row['id_supplier']] = true; } if (count($vendors) !== 1) { throw new UserException('Vendor harus sama'); } $purchase_invoiced = InvoiceDtl::find()->select(['id_reff', 'total' => 'sum(trans_value)'])->where(['reff_type' => InvoiceDtl::TYPE_PURCHASE, 'id_reff' => $ids])->groupBy('id_reff')->indexBy('id_reff')->asArray()->all(); $data['id_vendor'] = $vendor; $data['invoice_type'] = MInvoice::TYPE_IN; $details = []; foreach ($inv_vals as $id => $value) { $sisa = $purchase_values[$id]['purchase_value'] - $purchase_values[$id]['item_discount']; if (isset($purchase_invoiced[$id])) { $sisa -= $purchase_invoiced[$id]['total']; } if ($value > $sisa) { throw new UserException('Tagihan lebih besar dari sisa'); } $details[] = ['id_reff' => $id, 'trans_value' => $value]; } $data['details'] = $details; return static::create($data, $model); }
public static function modelClass() { return MPurchase::className(); }