示例#1
0
 public function createFromInvoice($data, $model = null)
 {
     $pay_vals = ArrayHelper::map($data['details'], 'id_invoice', 'value');
     $ids = array_keys($pay_vals);
     $invoice_values = Invoice::find()->where(['id_invoice' => $ids])->indexBy('id_invoice')->asArray()->all();
     $vendor = $inv_type = null;
     $vendors = $inv_types = [];
     foreach ($invoice_values as $row) {
         $vendor = $row['id_vendor'];
         $vendors[$vendor] = true;
         $inv_type = $row['invoice_type'];
         $inv_types[$inv_type] = true;
     }
     if (count($vendors) !== 1) {
         throw new UserException('Vendor harus sama');
     }
     if (count($inv_types) !== 1) {
         throw new UserException('Type invoice harus sama');
     }
     $invoice_paid = PaymentDtl::find()->select(['id_invoice', 'total' => 'sum(payment_value)'])->joinWith('idPayment')->where(['payment_type' => $inv_type, 'id_invoice' => $ids])->groupBy('id_invoice')->indexBy('id_invoice')->asArray()->all();
     $data['id_vendor'] = $vendor;
     $data['payment_type'] = $inv_type;
     $details = [];
     foreach ($inv_vals as $id => $value) {
         $sisa = $invoice_values[$id]['invoice_value'];
         if (isset($invoice_paid[$id])) {
             $sisa -= $invoice_paid[$id]['total'];
         }
         if ($value > $sisa) {
             throw new UserException('Tagihan lebih besar dari sisa');
         }
         $details[] = ['id_invoice' => $id, 'payment_value' => $value];
     }
     $data['details'] = $details;
     return static::create($data, $model);
 }
示例#2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getPaymentDtl()
 {
     return $this->hasMany(PaymentDtl::className(), ['id_invoice' => 'id_invoice']);
 }