/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new EntrySheetCustom();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['EntrySheetCustom'])) {
         $model->attributes = $_POST['EntrySheetCustom'];
         $model->record_date = DateTime::createFromFormat('Y-m-d', $model->record_date);
         if ($model->record_date !== false) {
             $model->record_date->setTime(0, 0, 0);
             $model->record_date = $model->record_date->getTimestamp();
             $model->record_date = date('Y-m-d', $model->record_date);
         }
         if ($model->validate()) {
             $modelEntrySheet = new EntrySheet();
             $modelPrice = ProductCustomer::model()->findByAttributes(array('customer_id' => $model->customer_id, 'product_id' => $model->product_id));
             if ($modelPrice === null) {
                 $modelPrice = Product::model()->findByPk($model->product_id);
             }
             $entrySheet = new EntrySheet();
             $entrySheet->customer_id = $model->customer_id;
             $entrySheet->product_id = $model->product_id;
             $entrySheet->quantity = $model->quantity;
             $entrySheet->price = $modelPrice->price;
             $entrySheet->added_on = $model->record_date;
             $entrySheet->save();
             if ($model->amount_paid !== '') {
                 $entrySheetAmountPaid = new EntrySheetPaidAmount();
                 $entrySheetAmountPaid->entry_sheet_id = $entrySheet->id;
                 $entrySheetAmountPaid->paid_amount = $model->amount_paid;
                 $entrySheetAmountPaid->save();
             }
             $this->redirect(array('index'));
         }
     }
     if (empty($model->record_date)) {
         $model->record_date = date('Y-m-d');
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer $id the ID of the model to be loaded
  * @return ProductCustomer the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = ProductCustomer::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }