src/Money/Module/Budget/Controller/Budget.php
public function ajaxCreate()
    {
        if (!Server::getInstance()->isAjax()) {
            throw new \RuntimeException();
        }

        $response = new Api();
        $response->setHttpCode(200);
        $response->setIsSuccess(true);

        try {
            $post = Post::getInstance();

            $accountId  = (int) $post->get('accountId');
            $account    = $this->verifyAccount($accountId);

            //~ New transaction
            $itemMapper = new BudgetMapper(Database::get('money'));
            $item       = $itemMapper->newDataInstance();

            //~ Add transaction
            $item->setAccountId($account->getId());
            $item->setName(trim($post->get('itemName')));
            $item->setAmountDefault((float) $post->get('itemAmountDefault'));
            $item->setParentId((int) $post->get('itemParentId'));

            $dateStart = trim($post->get('itemDateStart'));
            $dateStart = \DateTimeImmutable::createFromFormat('d/m/Y', $dateStart);
            $item->setDateStart($dateStart->format('Y-m-d'));

            $dateEnd = trim($post->get('itemDateEnd'));
            if (!empty($dateEnd)) {
                $dateEnd = \DateTimeImmutable::createFromFormat('d/m/Y', $dateEnd);
                $item->setDateEnd($dateEnd->format('Y-m-d'));
            }

            $monthRecurrent = $post->get('itemMonthRecurrent');
            if (!empty($monthRecurrent) && is_array($monthRecurrent)) {
                foreach ($monthRecurrent as $month) {
                    $month = (int) $month;

                    if ($month === 0) {
                        $item->setIsRecurrent(false);
                        $item->setMonthBitmask(0);
                        break;
                    }

                    if ($month === 13) {
                        $item->setIsRecurrent(true);
                        $item->setMonthBitmask(4095); // All months
                        break;
                    }

                    $item->setIsRecurrent(true);
                    $item->addMonthToBitmask($month);
                }
            }

            Database::get('money')->beginTransaction();

            //~ Insert item
            $itemMapper->insert($item);

            //~ Insert categories linked
            $categories           = $post->get('itemCategoriesId');
            $categoryMapper       = new CategoryMapper(Database::get('money'));
            $budgetCategoryMapper = new BudgetCategoryMapper(Database::get('money'));

            if (!empty($categories) && is_array($categories)) {

                foreach ($categories as $categoryId) {
                    $category = $categoryMapper->findById((int) $categoryId);
                    if ($category->getAccountId() !== $accountId) {
                        throw new \RangeException('Category id is not valid for this account!');
                    }

                    $budgetCategory = $budgetCategoryMapper->newDataInstance();
                    $budgetCategory->setCategoryId($categoryId);
                    $budgetCategory->setBudgetId($item->getId());
                    $budgetCategoryMapper->insert($budgetCategory);
                }
            }

            Database::get('money')->commit();

        } catch (\Exception $exception) {
            if (Database::get('money')->inTransaction()) {
                Database::get('money')->rollBack();
            }

            $response->setIsSuccess(false)
                ->setHttpCode(500)
                ->setErrorCode($exception->getCode())
                ->setErrorMessage($exception->getMessage())
                ->setContent(true);
        }

        return $response;
    }
public function ajaxCreate()
    {
        if (!Server::getInstance()->isAjax()) {
            throw new \RuntimeException();
        }

        $response = new Api();
        $response->setHttpCode(200);
        $response->setIsSuccess(true);

        try {
            $post = Post::getInstance();

            $accountId  = (int) $post->get('accountId');
            $account    = $this->verifyAccount($accountId);

            //~ New transaction
            $itemMapper = new BudgetMapper(Database::get('money'));
            $item       = $itemMapper->newDataInstance();

            //~ Add transaction
            $item->setAccountId($account->getId());
            $item->setName(trim($post->get('itemName')));
            $item->setAmountDefault((float) $post->get('itemAmountDefault'));
            $item->setParentId((int) $post->get('itemParentId'));

            $dateStart = trim($post->get('itemDateStart'));
            $dateStart = \DateTimeImmutable::createFromFormat('d/m/Y', $dateStart);
            $item->setDateStart($dateStart->format('Y-m-d'));

            $dateEnd = trim($post->get('itemDateEnd'));
            if (!empty($dateEnd)) {
                $dateEnd = \DateTimeImmutable::createFromFormat('d/m/Y', $dateEnd);
                $item->setDateEnd($dateEnd->format('Y-m-d'));
            }

            $monthRecurrent = $post->get('itemMonthRecurrent');
            if (!empty($monthRecurrent) && is_array($monthRecurrent)) {
                foreach ($monthRecurrent as $month) {
                    $month = (int) $month;

                    if ($month === 0) {
                        $item->setIsRecurrent(false);
                        $item->setMonthBitmask(0);
                        break;
                    }

                    if ($month === 13) {
                        $item->setIsRecurrent(true);
                        $item->setMonthBitmask(4095); // All months
                        break;
                    }

                    $item->setIsRecurrent(true);
                    $item->addMonthToBitmask($month);
                }
            }

            Database::get('money')->beginTransaction();

            //~ Insert item
            $itemMapper->insert($item);

            //~ Insert categories linked
            $categories           = $post->get('itemCategoriesId');
            $categoryMapper       = new CategoryMapper(Database::get('money'));
            $budgetCategoryMapper = new BudgetCategoryMapper(Database::get('money'));

            if (!empty($categories) && is_array($categories)) {

                foreach ($categories as $categoryId) {
                    $category = $categoryMapper->findById((int) $categoryId);
                    if ($category->getAccountId() !== $accountId) {
                        throw new \RangeException('Category id is not valid for this account!');
                    }

                    $budgetCategory = $budgetCategoryMapper->newDataInstance();
                    $budgetCategory->setCategoryId($categoryId);
                    $budgetCategory->setBudgetId($item->getId());
                    $budgetCategoryMapper->insert($budgetCategory);
                }
            }

            Database::get('money')->commit();

        } catch (\Exception $exception) {
            if (Database::get('money')->inTransaction()) {
                Database::get('money')->rollBack();
            }

            $response->setIsSuccess(false)
                ->setHttpCode(500)
                ->setErrorCode($exception->getCode())
                ->setErrorMessage($exception->getMessage())
                ->setContent(true);
        }

        return $response;
    }
Ajax edit category.

@return ResponseInterface