';
protected $_columns4ExportCsv = [
'added' => 'Submitted',
'billingFirstName' => 'Billing First Name',
'billingLastName' => 'Billing Last Name',
'billingAddress' => 'Billing Address',
'billingCity' => 'Billing City',
'billingState' => 'Billing State',
'billingZip' => 'Billing Zip',
'billingPhone' => 'Billing Phone',
'billingEmail' => 'Billing Email',
'shippingFirstName' => 'Shipping First Name',
'shippingLastName' => 'Shipping Last Name',
'shippingAddress' => 'Shipping Address',
'shippingCity' => 'Shipping City',
'shippingState' => 'Shipping State',
'shippingZip' => 'Shipping Zip',
'deliveryAddressType' => 'Delivery Address Type',
'shippingNameTitle' => 'Shipping Name',
'shippingServiceCodeTitle' => 'Shipping Service Code',
'shippingRate' => 'Shipping Rate',
'paymentTypeTitle' => 'Payment Type',
'paid' => 'Paid',
'status' => 'Status',
'total' => 'Total',
'paymentCardNumber' => 'Card Number',
'paymentExpirationDate' => 'Expiration Date',
'paymentPoNumber' => 'Po Number',
'adminAdditionalNote' => 'Staff Note',
'userAdditionalNote' => 'User Note',
];
protected $_exportFileName;
/** Каже на зміну яких ордер статусів слати юзеру мила */
protected $_orderStatusesForUserMail = [
App_ECommerce_Order_Admin_Obj::STATUS_ORDER_SENT,
];
/** Каже на зміну яких ордер статусів слати юзеру мила */
protected $_paidStatusesForUserMail = [
App_ECommerce_Order_Admin_Obj::STATUS_PAID_YES,
];
protected $_initStatuses = [];
protected function _init()
{
parent::_init();
$this->_messageTemplates[self::MSG_ORDER_STATUS_CHANGED] = 'Order status for selected item(s) has been changed';
$this->_messageTemplates[self::MSG_NO_DATA] = 'No selected items';
return $this;
}
/**
* Return tabs object
*
* @static
* @return App_ECommerce_Order_Admin_Tab
*/
public static function getTabs()
{
$options = ['sitemapSelector' => static::$_sitemapSelector];
return new App_ECommerce_Order_Admin_Tab($options);
}
protected function _doEdit()
{
$this->_postEdit();
$tabs = self::getTabs();
$url = BASE_URL_LANGUAGE . '/' . $tabs->getRootFullAlias() . '/' . $tabs->getDefaultTabAlias()
. '/edit/' . $this->_getDataObj()->getPrimaryKey();
$this->redirect($url);
}
protected function _doNew()
{
if (false === $this->_getDataObj()->createEmptyOrder()) {
$this->_setBackErrors($this->_getDataObj()->getErrors());
$this->_doBack();
}
$this->_postNew();
$tabs = self::getTabs();
$url = BASE_URL_LANGUAGE . '/' . $tabs->getRootFullAlias() . '/' . $tabs->getDefaultTabAlias()
. '/new/' . $this->_getDataObj()->getPrimaryKey();
$this->redirect($url);
}
protected function _addListItem()
{
$this->_doc->addScript('js/app/ECommerce/order/admin-action-form-toolbar.js');
return parent::_addListItem();
}
protected function _doExportCsv()
{
if ($this->_getDataObj()->hasFilter()) {
$form = $this->_getFilterForm();
if ($form->validate()) {
$this->_getDataObj()->addFilter($form->getValues());
}
}
$options = [];
$options['order'] = [$this->_getList()->getOrderBy()];
$this->_dataObj->setSelectOptions($options);
$stmt = $this->_getDataObj()->getListStatement($options);
$columns = $this->_getColumns4ExportCsv();
$rowCallback = function () use ($stmt) {
if (($row = $stmt->fetch())) {
$row = str_replace(["\n", "\r"], ' ', $row);
$row['paymentExpirationDate'] = $row['paymentExpirationDate']
? date('n/t/Y', strtotime($row['paymentExpirationDate']))
: '';
}
return $row;
};
$helper = new ExportCsv([
'fileName' => $this->getConfig('itemsName') . ' {date}.csv',
'columns' => $columns,
'rowCallback' => $rowCallback,
]);
$helper->export();
$this->_postExportCsv();
exit;
}
protected function _postExportCsv()
{
$this->_log();
return $this;
}
protected function _getColumns4ExportCsv()
{
if (null === $this->_columns4ExportCsv) {
$this->_columns4ExportCsv = ['added' => 'Submitted'];
$fields = array_keys(Qs_Array::excludeKey($this->_getDataObj()->getMetaData(), 'id', 'added', 'changed'));
foreach ($fields as $field) {
$this->_columns4ExportCsv[$field] = ucfirst(preg_replace('/([a-z])([A-Z])/', '$1 $2', $field));
}
}
return $this->_columns4ExportCsv;
}
protected function _getDefaultLinks()
{
$links = parent::_getDefaultLinks();
$params = [];
if (($orderBy = Qs_Request::getGetValue('orderBy'))) {
$params['orderBy'] = $orderBy;
}
$filter = $this->_dataObj->getFilter();
foreach ($filter as $field => $value) {
if ($value) {
$params[$field] = $value;
}
}
$params['action'] = 'exportCsv';
$links['exportCsv'] = ['title' => 'Export to CSV', 'url' => $this->url($params)];
return $links;
}
protected function _doView()
{
$item = $this->_getDataObj()->getData();
if (empty($item)) {
$this->_doc->display404();
}
$form = $this->_getViewForm([
'defaults' => $item,
'cancelUrl' => Qs_SiteMap::findFirst(static::$_sitemapSelector, 'url'),
]);
$form->setDefaults();
if ($item['status'] == App_ECommerce_Order_Admin_Obj::STATUS_ORDER_CLOSED) {
/** @var Zend_Form_Element $element */
foreach ($form->getElements() as $element) {
$element->setAttribs([
'disabled' => 'disabled',
'readonly' => 'readonly',
]);
}
}
$item['adminForm'] = $form;
$item['config'] = $this->getConfig()->toArray();
$item['printVersion'] = $this->_printVersion;
$item['tpl'] = $this->getTemplate('view.tpl');
$item['backUrl'] = ($backUrl = $this->_getBackUrl(CURRENT_PAGE_FINAL)) ? $backUrl : CURRENT_PAGE_FINAL;
$headerAppend = ' #' . $item['id'] . ' on ' . date('m/d/Y g:i A') . ': '
. ($item['paid'] == 'y'
? 'Paid'
: 'Not Paid'
);
$this->_doc->appendHeader($headerAppend);
$this->_addItem($item);
$this->_postView();
return $this;
}
protected function _doChangeOrderStatus()
{
return $this->_do404();
}
protected function _doChangeOrderStatusAjax()
{
$result = $this->_changeStatus('status');
$this->_displayJson($result);
exit;
}
protected function _doChangePaidStatus()
{
return $this->_do404();
}
protected function _doChangePaidStatusAjax()
{
$result = $this->_changeStatus('paid');
$this->_displayJson($result);
exit;
}
protected function _changeStatus($field)
{
$status = Qs_Request::getPostValue('value');
$selectedPrimary = Qs_Request::getPostValue('selectedPrimary');
if ($status && $selectedPrimary && is_array($selectedPrimary) &&
false !== ($updatedPrimary = $this->_getDataObj()->changeStatuses($selectedPrimary, $field, $status))
) {
$isError = false;
$message = $this->_createMessage(self::MSG_ORDER_STATUS_CHANGED);
$this->_setBackMessage($message);
$this->_postChangeStatus($updatedPrimary, $field, $status);
} else {
$isError = true;
$message = $this->_createMessage(self::MSG_NO_DATA);
}
return ['isError' => $isError, 'message' => $message];
}
protected function _prepareMailData(array $data)
{
$data['orderId'] = htmlspecialchars($data['id']);
return $data;
}
protected function _postChangeStatus($updatedPrimary, $field, $status)
{
if (is_array($updatedPrimary) && sizeof($updatedPrimary)) {
if ($this->_checkNecessarySendMail($field, $status)) {
foreach ($updatedPrimary as $primary) {
if (!ctype_digit((string) $primary)) {
continue;
}
$this->_getDataObj()->setPrimaryKey($primary);
$order = $this->_getDataObj()->clearData()->getData();
if (is_array($order) && sizeof($order)) {
$mailSuffix = $this->_getMailSuffix($field, $status);
$this->sendUserNotification($order, $mailSuffix);
}
}
}
$this->_logChangeStatus($updatedPrimary);
}
return $this;
}
/**
* @param array $orders list of orders id
* @return App_ECommerce_Order_Admin_View
*/
protected function _logChangeStatus(array $orders)
{
if ($this->_hasLog && $orders) {
if (count($orders) > 1) {
$message = 'Updated %itemsName% ';
foreach ($orders as $id) {
$message .= '#' . $id . ', ';
}
$message = substr($message, 0, -2);
} else {
$message = 'Updated %itemName% #' . reset($orders);
}
$this->_getLog()->setAction('changeStatus', $message);
$this->_getLog()->write('changeStatus', $this->_getDataObj()->getObjectInfo());
}
return $this;
}
protected function _checkNecessarySendMail($field, $status)
{
if (($field == 'status' && in_array($status, $this->_orderStatusesForUserMail))
|| ($field == 'paid' && in_array($status, $this->_paidStatusesForUserMail))
) {
return true;
}
return false;
}
protected function _getMailSuffix($field, $status)
{
return ucfirst(($field == 'status' ? 'order' : 'paid')) . 'Status'
. ($field == 'paid' ? (ucfirst(($status == 'n' ? 'no' : 'yes'))) : ucfirst($status));
}
protected function _initFromForm(Qs_Form $form)
{
$this->_initStatuses = array_intersect_key(
$this->_getDataObj()->getData(),
array_flip(['status', 'paid'])
);
return parent::_initFromForm($form);
}
protected function _postUpdate()
{
$data = $this->_getDataObj()->getData();
foreach (['paid', 'status'] as $field) {
if ($this->_initStatuses[$field] != $data[$field]) {
$this->_postChangeStatus([$this->_getDataObj()->getPrimaryKey()], $field, $data[$field]);
}
}
return parent::_postUpdate();
}
public function getRootFullAlias()
{
if (empty($this->_rootFullAlias)) {
$this->_rootFullAlias = Qs_SiteMap::findFirst('', 'fullAlias');
}
return $this->_rootFullAlias;
}
protected function _doCancelOrder()
{
$this->_getDataObj()->cancelOrder();
$this->_doBack();
}
}