/**
  * Update a prebill identified by its Id
  */
 public function putAction()
 {
     $data = $this->_helper->requestData(true);
     // Try to load the preBill
     $preBill = $this->_getPreBill();
     // Check permissions
     try {
         $this->_helper->allowed('update', $preBill);
     } catch (PermissionException $ex) {
         throw new PermissionException(sprintf("Update Operation is not allowed: %s", $ex->getMessage()));
     }
     if (!isset($data['prebillUpdate'])) {
         return;
     }
     $data = $data['prebillUpdate'];
     // Make necessary adjustments to some fields
     if (strlen($data['prebillIDinOBCRM'])) {
         $data['externalId'] = $data['prebillIDinOBCRM'];
         unset($data['prebillIDinOBCRM']);
     }
     if (!empty($data['status'])) {
         if ($data['status'] == 'published') {
             $publish = true;
         } else {
             if ($data['status'] == 'unpublished') {
                 $unpublish = true;
             } else {
                 throw new InvalidArgumentException('Invalid value for [status], only published/unpublished values are allowed');
             }
         }
         unset($data['status']);
     }
     if (!empty($data)) {
         if ($preBill->getPublished()) {
             throw new InvalidArgumentException('Invalid parameter value: status UPDATE a published prebill is not allowed');
         }
         $upPrebill = new PreBillUpdateModel();
         $upPrebill->id = $preBill->id;
         // Modify the current details with the new data
         if (strlen($data['externalId'])) {
             $upPrebill->setExternalId($data['externalId']);
         }
         //             if (!empty($data['totalCharge'])) {
         //                 $preBill->setTotalCharge($data['totalCharge']);
         //             }
         if (!empty($data['adjustment'])) {
             $adjust = array('description' => "AdjustByApi", 'value' => $data['adjustment']);
             $upPrebill->setAdjusts(array($adjust));
         }
         $res = $this->_prebillSrv->update($upPrebill);
     }
     if (@$publish) {
         if ($preBill->getPublished()) {
             throw new InvalidArgumentException('Invalid parameter value: status. Publish action not supported for already published prebills');
         }
         $res = $this->_prebillSrv->publish($preBill);
     }
     if (@$unpublish) {
         if (!$preBill->getPublished()) {
             throw new InvalidArgumentException('Invalid parameter value: status. Unpublish action not supported for unpublished prebills');
         }
         $res = $this->_prebillSrv->unpublish($preBill);
     }
     $this->getResponse()->setHttpResponseCode(204);
     $this->_helper->viewRenderer->setNoRender(TRUE);
 }
 /**
  * Update a prebill identified by its Id
  */
 public function putAction()
 {
     $data = $this->_helper->requestData(true);
     // Try to load the preBill
     $preBill = $this->_getPreBill();
     // Check permissions
     $this->_helper->allowed('update', $preBill);
     if (!isset($data['prebillUpdate'])) {
         return;
     }
     $data = $data['prebillUpdate'];
     // Make necessary adjustments to some fields
     if (strlen($data['prebillIDinOBCRM'])) {
         $data['externalId'] = $data['prebillIDinOBCRM'];
         unset($data['prebillIDinOBCRM']);
     }
     if (!empty($data['status'])) {
         if ($data['status'] == 'published') {
             $publish = true;
         } else {
             if ($data['status'] == 'unpublished') {
                 $unpublish = true;
             } else {
                 throw new InvalidArgumentException('Invalid value for [status], only published/unpublished values are allowed');
             }
         }
         unset($data['status']);
     }
     if (!empty($data)) {
         if ($preBill->getPublished()) {
             throw new NotAllowedException('UPDATE Operation is not allowed: Update a published prebill is not allowed');
         }
         $upPrebill = new PreBillUpdateModel();
         $upPrebill->id = $preBill->id;
         // Modify the current details with the new data
         if (strlen($data['externalId'])) {
             $upPrebill->setExternalId($data['externalId']);
         }
         //             if (!empty($data['totalCharge'])) {
         //                 $preBill->setTotalCharge($data['totalCharge']);
         //             }
         if (!empty($data['adjustment'])) {
             $adjust = array('description' => "AdjustByApi", 'value' => $data['adjustment']);
             $upPrebill->setAdjusts(array($adjust));
         }
         $res = $this->_prebillSrv->update($upPrebill);
         $this->view->data = $res;
     }
     if (@$publish) {
         if ($preBill->getPublished()) {
             throw new InvalidArgumentException("Prebill is already published");
         }
         $res = $this->_prebillSrv->publish($preBill);
         $this->view->dataPublish = $res;
     }
     if (@$unpublish) {
         $res = $this->_prebillSrv->unpublish($preBill);
         $this->view->dataUnpublish = $res;
     }
 }