Esempio n. 1
0
File: Item.php Progetto: arbi/MyCode
 /**
  * @param Ticket $expenseTicket
  * @throws NotFoundException
  */
 public function save(Ticket $expenseTicket)
 {
     if (!$this->getServiceLocator() instanceof ServiceLocatorInterface) {
         throw new NotFoundException('Service locator not defined for expense item.');
     }
     $itemDao = $this->getItemDao();
     $costDao = $this->getCostDao();
     // Normalize values
     $this->prepare();
     $data = $this->getData();
     $itemAmount = null;
     $costCenters = [];
     $itemData = [];
     if ($this->getMode() == self::MODE_ADD) {
         $costCenters = $data['costCenters'];
         $itemAmount = $data['amount'];
         $itemData = ['transaction_id' => $data['transactionId'], 'account_id' => $data['accountId'], 'account_reference' => $data['accountReference'], 'sub_category_id' => $data['subCategoryId'], 'currency_id' => $data['currencyId'], 'amount' => Helper::formatAmount($itemAmount), 'is_startup' => $data['isStartup'], 'is_deposit' => $data['isDeposit'], 'is_refund' => $data['isRefund'], 'comment' => $data['accountComment'], 'type' => $data['type'], 'status' => $data['status']];
         if ($data['period']['from']) {
             $itemData['period_from'] = $data['period']['from'];
             $itemData['period_to'] = $data['period']['to'];
         }
     }
     switch ($this->getMode()) {
         case self::MODE_ADD:
             $itemData['expense_id'] = $expenseTicket->getExpenseId();
             $itemData['creator_id'] = $expenseTicket->getCreatorId();
             $itemData['date_created'] = date('Y-m-d H:i:s');
             if (!$data['period']['from']) {
                 $itemData['period_from'] = $itemData['period_to'] = date('Y-m-d');
             }
             $expenseItemId = $itemDao->save($itemData);
             $this->setId($expenseItemId);
             if (count($costCenters)) {
                 $costCenterAmount = $itemAmount / count($costCenters);
                 foreach ($costCenters as $costCenter) {
                     $costDao->save(['expense_item_id' => $expenseItemId, 'cost_center_id' => $costCenter['id'], 'cost_center_type' => $this->translateCCT($costCenter['type']), 'amount' => $this->calculateCostAmount($costCenterAmount, $data['currencyId'], $costCenter['currencyId'], $data['isRefund'])]);
                 }
             }
             break;
         case self::MODE_DELETE:
             $costDao->delete(['expense_item_id' => $this->getId()]);
             $itemDao->delete(['id' => $this->getId()]);
             break;
     }
 }