Beispiel #1
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getSalesDtls()
 {
     return $this->hasMany(SalesDtl::className(), ['id_warehouse' => 'id_warehouse']);
 }
Beispiel #2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getSalesDtls()
 {
     return $this->hasMany(SalesDtl::className(), ['id_product' => 'id_product']);
 }
 /**
  * 
  * @param SalesHdr $model
  * @return array
  */
 protected function saveSales($model)
 {
     $post = Yii::$app->request->post();
     $details = $model->salesDtls;
     $success = false;
     if ($model->load($post)) {
         $transaction = Yii::$app->db->beginTransaction();
         try {
             $formName = (new SalesDtl())->formName();
             $postDetails = empty($post[$formName]) ? [] : $post[$formName];
             if ($postDetails === []) {
                 throw new Exception('Detail tidak boleh kosong');
             }
             $objs = [];
             foreach ($details as $detail) {
                 $objs[$detail->id_sales_dtl] = $detail;
             }
             if ($model->save()) {
                 $success = true;
                 $id_hdr = $model->id_sales;
                 $id_whse = $model->id_warehouse;
                 $details = [];
                 foreach ($postDetails as $dataDetail) {
                     $id_dtl = $dataDetail['id_sales_dtl'];
                     if (isset($objs[$id_dtl])) {
                         $detail = $objs[$id_dtl];
                         unset($objs[$id_dtl]);
                     } else {
                         $detail = new SalesDtl();
                     }
                     $detail->setAttributes($dataDetail);
                     $detail->id_sales = $id_hdr;
                     $detail->id_warehouse = $id_whse;
                     if ($detail->idCogs) {
                         $detail->cogs = $detail->idCogs->cogs;
                     } else {
                         $detail->cogs = 0;
                     }
                     if (!$detail->save()) {
                         $success = false;
                         $model->addError('', implode("\n", $detail->firstErrors));
                         break;
                     }
                     $details[] = $detail;
                 }
                 if ($success && count($objs) > 0) {
                     $success = SalesDtl::deleteAll(['id_sales_dtl' => array_keys($objs)]);
                 }
             }
             if ($success) {
                 $transaction->commit();
             } else {
                 $transaction->rollBack();
             }
         } catch (Exception $exc) {
             $success = false;
             $model->addError('', $exc->getMessage());
             $transaction->rollBack();
         }
         if (!$success) {
             $details = [];
             foreach ($postDetails as $value) {
                 $detail = new SalesDtl();
                 $detail->setAttributes($value);
                 $details[] = $detail;
             }
         }
     }
     return [$details, $success];
 }
Beispiel #4
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getSalesDtls()
 {
     return $this->hasMany(SalesDtl::className(), ['id_sales' => 'id_sales'])->with('idCogs');
 }