public function addInvoiceAction() { $this->getDi()->authAdmin->getUser()->checkPermission('grid_invoice', 'insert'); $form = new Am_Form_Admin('add-invoice'); $tm_added = $form->addDate('tm_added')->setLabel(___('Date')); $tm_added->setValue($this->getDi()->sqlDate); $tm_added->addRule('required'); $comment = $form->addText('comment', array('class' => 'el-wide'))->setLabel(___("Comment\nfor your reference")); $form->addElement(new Am_Form_Element_ProductsWithQty('product_id'))->setLabel(___('Products'))->loadOptions($this->getDi()->billingPlanTable->selectAllSorted())->addRule('required'); $form->addSelect('paysys_id')->setLabel(___('Payment System'))->setId('add-invoice-paysys_id')->loadOptions(array('' => '') + $this->getDi()->paysystemList->getOptions()); $couponEdit = $form->addText('coupon')->setLabel(___('Coupon'))->setId('p-coupon'); $action = $form->addAdvRadio('_action')->setLabel(___('Action'))->setId('add-invoice-action')->loadOptions(array('pending' => ___('Just Add Pending Invoice'), 'pending-payment' => ___('Add Invoice and Payment/Access Manually'), 'pending-send' => ___('Add Pending Invoice and Send Link to Pay It to Customer')))->setValue('pending'); $receipt = $form->addText('receipt')->setLabel(___('Receipt#'))->setId('add-invoice-receipt'); $tm_due = $form->addDate('tm_due')->setLabel(___('Due Date')); $tm_due->setValue(sqlDate('+7 days')); $tm_due->setId('add-invoice-due'); $message = $form->addTextarea('message', array('class' => 'el-wide'))->setLabel(___("Message\nwill be included to email to user")); $message->setId('add-invoice-message'); $form->addElement('email_link', 'invoice_pay_link')->setLabel(___('Email Template with Payment Link')); $form->addScript()->setScript(' $("[name=_action]").change(function(){ var val = $("[name=_action]:checked").val(); $("#add-invoice-receipt").closest("div.row").toggle(val == "pending-payment") $("#add-invoice-due").closest("div.row").toggle(val == "pending-send") $("#add-invoice-message").closest("div.row").toggle(val == "pending-send") $("[name=invoice_pay_link]").closest("div.row").toggle(val == "pending-send") }).change(); '); $script = <<<CUT \$("input#p-coupon").autocomplete({ minLength: 2, source: window.rootUrl + "/admin-coupons/autocomplete" }); CUT; $form->addScript('script')->setScript($script); $form->addSaveButton(); $form->setDataSources(array($this->getRequest())); do { if ($form->isSubmitted() && $form->validate()) { $vars = $form->getValue(); $invoice = $this->getDi()->invoiceRecord; $invoice->setUser($this->getDi()->userTable->load($this->user_id)); $invoice->tm_added = sqlTime($vars['tm_added']); if ($vars['coupon']) { $invoice->setCouponCode($vars['coupon']); $error = $invoice->validateCoupon(); if ($error) { $couponEdit->setError($error); break; } } foreach ($vars['product_id'] as $plan_id => $qty) { $p = $this->getDi()->billingPlanTable->load($plan_id); $pr = $p->getProduct(); try { $invoice->add($pr, $qty); } catch (Am_Exception_InputError $e) { $form->setError($e->getMessage()); break 2; } } $invoice->comment = $vars['comment']; $invoice->calculate(); switch ($vars['_action']) { case 'pending': if (!$this->_addPendingInvoice($invoice, $form, $vars)) { break 2; } break; case 'pending-payment': if (!$this->_addPendingInvoiceAndPayment($invoice, $form, $vars)) { break 2; } break; case 'pending-send': if (!$this->_addPendingInvoiceAndSend($invoice, $form, $vars)) { break 2; } break; default: throw new Am_Exception_InternalError(sprintf('Unknown action [%s] as %s::%s', $vars['_action'], __CLASS__, __METHOD__)); } $this->getDi()->adminLogTable->log("Add Invoice (#{$invoice->invoice_id}/{$invoice->public_id}, Billing Terms: " . new Am_TermsText($invoice) . ")", 'invoice', $invoice->invoice_id); return $this->redirectLocation(REL_ROOT_URL . '/admin-user-payments/index/user_id/' . $this->user_id); } // if } while (false); $this->view->content = '<h1>' . ___('Add Invoice') . ' (<a href="' . REL_ROOT_URL . '/admin-user-payments/index/user_id/' . $this->user_id . '">' . ___('return') . '</a>)</h1>' . (string) $form; $this->view->display('admin/user-layout.phtml'); }