/** * Smarty plugin * @package Smarty * @subpackage plugins */ function smarty_modifier_wa_format_amount($string, $currency_id = null, $format = '%', $locale = null) { if ($locale === null || $currency_id === null) { /** * @var waSmarty3View */ $smarty = waConfig::get('current_smarty'); } if ($locale === null) { $locale = $smarty->getVars('locale'); } if ($locale === null) { $locale = wa()->getUser()->getLocale(); } if ($currency_id === null) { $currency_id = $smarty->getVars('currency_id'); } if ($currency_id && $locale) { if ($format == 'words') { $format = '%.W{n0} %.2{f0}'; } $string = waCurrency::format($format, $string, $currency_id, $locale); } return $string; }
public function execute() { $id = waRequest::get('id'); $model = new shopNotificationModel(); $n = $model->getById($id); $params_model = new shopNotificationParamsModel(); $params = $params_model->getParams($id); // Orders used as sample data for testing $om = new shopOrderModel(); $test_orders = $om->where("paid_date IS NOT NULL AND state_id <> 'deleted'")->order('id DESC')->limit(10)->fetchAll('id'); shopHelper::workupOrders($test_orders); $im = new shopOrderItemsModel(); foreach ($im->getByField('order_id', array_keys($test_orders), true) as $i) { $test_orders[$i['order_id']]['items'][] = $i; } foreach ($test_orders as &$o) { $o['items'] = ifset($o['items'], array()); $o['total_formatted'] = waCurrency::format('%{s}', $o['total'], $o['currency']); } $this->view->assign('n', $n); $this->view->assign('params', $params); $this->view->assign('transports', self::getTransports()); $this->view->assign('events', $this->getEvents()); $this->view->assign('test_orders', $test_orders); $this->view->assign('default_email_from', $this->getConfig()->getGeneralSettings('email')); $this->view->assign('sms_from', $this->getSmsFrom()); $this->view->assign('routes', wa()->getRouting()->getByApp('shop')); }
public static function formatValue($c, $curr = null) { static $currencies = null; if ($currencies === null) { if ($curr) { $currencies = $curr; } else { $curm = new shopCurrencyModel(); $currencies = $curm->getAll('code'); } } if ($c['type'] == '$FS') { return _w('Free shipping'); } else { if ($c['type'] === '%') { return waCurrency::format('%0', $c['value'], 'USD') . '%'; } else { if (!empty($currencies[$c['type']])) { return waCurrency::format('%0{s}', $c['value'], $c['type']); } else { // Coupon of unknown type. Possibly from a plugin?.. return ''; } } } }
public function getSettingsHTML($params = array()) { $currencies = waCurrency::getAll(); foreach ($currencies as $k => $v) { $currencies[$k] = $v . ' (' . $k . ')'; } $params['options']['currency'] = $currencies; return parent::getSettingsHTML($params); }
public static function getTypes($currencies) { $result = array('%' => _w('% Discount')); foreach ($currencies as $c) { $info = waCurrency::getInfo($c['code']); $result[$c['code']] = $info['sign'] . ' ' . $c['code']; } $result['$FS'] = _w('Free shipping'); return $result; }
public function execute() { $asm = new waAppSettingsModel(); if (waRequest::post()) { $conf = waRequest::post('conf'); if ($conf && is_array($conf)) { $conf['affiliate_credit_rate'] = str_replace(',', '.', (double) str_replace(',', '.', ifset($conf['affiliate_credit_rate'], '0'))); $conf['affiliate_usage_rate'] = str_replace(',', '.', (double) str_replace(',', '.', ifset($conf['affiliate_usage_rate'], '0'))); foreach ($conf as $k => $v) { $asm->set('shop', $k, $v); } } } $enabled = shopAffiliate::isEnabled(); $def_cur = waCurrency::getInfo(wa()->getConfig()->getCurrency()); $tm = new shopTypeModel(); $product_types = $tm->getAll(); $conf = $asm->get('shop'); if (!empty($conf['affiliate_product_types'])) { $conf['affiliate_product_types'] = array_fill_keys(explode(',', $conf['affiliate_product_types']), true); } else { $conf['affiliate_product_types'] = array(); } $this->view->assign('conf', $conf); $this->view->assign('enabled', $enabled); $this->view->assign('product_types', $product_types); $this->view->assign('def_cur_sym', ifset($def_cur['sign'], wa()->getConfig()->getCurrency())); /** * Backend affiliate settings * * Plugins are expected to return one item or a list of items to to add to affiliate menu. * Each item is represented by an array: * array( * 'id' => string, // Required. * 'name' => string, // Required. * 'url' => string, // Required (unless you hack into JS using 'html' parameter). Content for settings page is fetched from this URL. * ) * * @event backend_settings_discounts */ $plugins = wa()->event('backend_settings_affiliate'); $config = wa('shop')->getConfig(); foreach ($plugins as $k => &$p) { if (substr($k, -7) == '-plugin') { $plugin_id = substr($k, 0, -7); $plugin_info = $config->getPluginInfo($plugin_id); $p['img'] = $plugin_info['img']; } } $this->view->assign('plugins', $plugins); $this->view->assign('installer', $this->getUser()->getRights('installer', 'backend')); }
public function execute() { $id = waRequest::request('id', null, waRequest::TYPE_INT); $contact = new waContact($id); $contact->getName(); // Customer orders $im = new shopOrderItemsModel(); $orders_collection = new shopOrdersCollection('search/contact_id=' . $id); $orders = $orders_collection->getOrders('*,items,params', 0, 500); shopHelper::workupOrders($orders); foreach ($orders as &$o) { $o['total_formatted'] = waCurrency::format('%{s}', $o['total'], $o['currency']); $o['shipping_name'] = ifset($o['params']['shipping_name'], ''); $o['payment_name'] = ifset($o['params']['payment_name'], ''); } $this->view->assign('orders', $orders); $this->view->assign('contact', $contact); $this->view->assign('def_cur_tmpl', str_replace('0', '%s', waCurrency::format('%{s}', 0, wa()->getConfig()->getCurrency()))); }
public function execute() { $contact = wa()->getUser(); $scm = new shopCustomerModel(); // Customer orders $om = new shopOrderModel(); $orders = $om->where('contact_id=?', $contact->getId())->order('id DESC')->fetchAll('id'); // Items for all orders, one query $im = new shopOrderItemsModel(); foreach ($im->getByField('order_id', array_keys($orders), true) as $row) { $orders[$row['order_id']]['items'][] = $row; } // Params for all orders, one query $opm = new shopOrderParamsModel(); foreach ($opm->getByField('order_id', array_keys($orders), true) as $row) { $orders[$row['order_id']]['params'][$row['name']] = $row['value']; } // Prepare order data for template $url_tmpl = wa()->getRouteUrl('/frontend/myOrder', array('id' => '%ID%')); $workflow = new shopWorkflow(); foreach ($orders as $k => &$o) { if ($o['state_id'] == 'deleted') { unset($orders[$k]); continue; } $o['id_str'] = shopHelper::encodeOrderId($o['id']); $o['total_formatted'] = waCurrency::format('%{s}', $o['total'], $o['currency']); $o['shipping_name'] = ifset($o['params']['shipping_name'], ''); $o['payment_name'] = ifset($o['params']['payment_name'], ''); $o['state'] = $workflow->getStateById($o['state_id']); $o['url'] = str_replace('%ID%', $o['id'], $url_tmpl); } $this->view->assign('orders', array_values($orders)); $this->view->assign('my_nav_selected', 'orders'); // Set up layout and template from theme $this->setThemeTemplate('my.orders.html'); if (!waRequest::isXMLHttpRequest()) { $this->setLayout(new shopFrontendLayout()); $this->getResponse()->setTitle(_w('Orders')); $this->view->assign('breadcrumbs', self::getBreadcrumbs()); $this->layout->assign('nofollow', true); } }
/** * Smarty plugin * @package Smarty * @subpackage plugins */ function smarty_modifier_wa_format_amount($string, $currency_id = null, $locale = null) { if ($locale === null || $currency_id === null) { /** * @var waSmarty3View */ $smarty = waConfig::get('current_smarty'); } if ($locale === null) { $locale = $smarty->getVars('locale'); } if ($locale === null) { $locale = waSystem::getInstance()->getUser()->getLocale(); } if ($currency_id === null) { $currency_id = $smarty->getVars('currency_id'); } if ($currency_id && $locale) { $string = waCurrency::format('%', $string, $currency_id, $locale); } return $string; }
public function execute() { list($start_date, $end_date, $group_by) = self::getTimeframeParams(); // Total sales for period, in default currency $total_sales = array('returning_customers' => 0, 'new_customers' => 0, 'total' => 0); // Total number of paid orders for the period $total_orders = array('returning_customers' => 0, 'new_customers' => 0, 'total' => 0); // Max total sales in a single day $max_day_sales = 0; // Loop over all days of a period that had at least one order paid, // and gather data into vars listed above. $om = new shopOrderModel(); $sales_by_day = $om->getSales($start_date, $end_date, $group_by); foreach ($sales_by_day as $row) { $max_day_sales = max($max_day_sales, (double) $row['total']); $total_orders['new_customers'] += (int) $row['customer_first_count']; $total_sales['new_customers'] += (double) $row['customer_first_total']; $total_orders['total'] += (int) $row['count']; $total_sales['total'] += (double) $row['total']; } $total_sales['returning_customers'] += $total_sales['total'] - $total_sales['new_customers']; $total_orders['returning_customers'] += $total_orders['total'] - $total_orders['new_customers']; // Data for main chart $sales_data = array(); foreach ($sales_by_day as &$d) { $d['total_percent'] = $max_day_sales ? $d['total'] * 100 / ifempty($max_day_sales, 1) : 0; $sales_data[] = array($d['date'], $d['total']); } unset($d); $def_cur = wa()->getConfig()->getCurrency(); $this->view->assign('sales_by_day', $sales_by_day); $this->view->assign('sales_data', $sales_data); $this->view->assign('group_by', $group_by); $this->view->assign('def_cur', $def_cur); $this->view->assign('stat', array('total_formatted' => waCurrency::format('%{s}', $total_sales['total'], $def_cur), 'percent_returning' => round($total_sales['returning_customers'] * 100 / ifempty($total_sales['total'], 1), 1), 'percent_new' => round($total_sales['new_customers'] * 100 / ifempty($total_sales['total'], 1), 1), 'avg_total_formatted' => waCurrency::format('%{s}', round($total_sales['total'] / ifempty($total_orders['total'], 1), 1), $def_cur), 'avg_total_new_formatted' => waCurrency::format('%{s}', round($total_sales['new_customers'] / ifempty($total_orders['new_customers'], 1), 2), $def_cur), 'avg_total_returning_formatted' => waCurrency::format('%{s}', round($total_sales['returning_customers'] / ifempty($total_orders['returning_customers'], 1), 2), $def_cur), 'avg_total_daily_formatted' => waCurrency::format('%{s}', $sales_by_day ? round($total_sales['total'] / count($sales_by_day), 2) : 0, $def_cur))); }
public function execByType($type) { $dbsm = new shopDiscountBySumModel(); if (waRequest::post()) { $sums = waRequest::post('rate_sum'); $discounts = waRequest::post('rate_discount'); $rows = array(); $dbsm->deleteByField('type', $type); if (is_array($sums) && is_array($discounts)) { foreach ($sums as $k => $sum) { $sum = str_replace(',', '.', $sum); if (!is_numeric($sum) || $sum < 0) { continue; } $discount = (double) str_replace(',', '.', ifset($discounts[$k], 0)); $discount = min(max($discount, 0), 100); if ($sum || $discount) { $rows[] = array('sum' => $sum, 'discount' => $discount, 'type' => $type); } } if ($rows) { $dbsm->multipleInsert($rows); } } } $enabled = shopDiscounts::isEnabled($type); $def_cur = waCurrency::getInfo(wa()->getConfig()->getCurrency()); $rates = $dbsm->getByType($type); foreach ($rates as &$r) { $r['sum'] = (double) $r['sum']; $r['discount'] = (double) $r['discount']; } $this->view->assign('rates', $rates); $this->view->assign('enabled', $enabled); $this->view->assign('def_cur_sym', ifset($def_cur['sign'], wa()->getConfig()->getCurrency())); }
public function execute() { $id = waRequest::request('id'); $fm = new shopFollowupModel(); // Save data when POST came if ($id && waRequest::post()) { if (waRequest::post('delete')) { $f = $fm->getById($id); if ($f) { /** * @event followup_delete * * Notify plugins about deleted followup * * @param array[string]int $params['id'] followup_id * @return void */ wa()->event('followup_delete', $f); $fm->deleteById($id); } exit; } $followup = waRequest::post('followup'); if ($followup && is_array($followup)) { $empty_row = $fm->getEmptyRow(); $followup = array_intersect_key($followup, $empty_row) + $empty_row; unset($followup['id']); $followup['delay'] = (double) str_replace(',', '.', ifempty($followup['delay'], '3')) * 24 * 3600; if (empty($followup['name'])) { $followup['name'] = _w('<no name>'); } $followup['from'] = $followup['from'] ? $followup['from'] : null; $followup['source'] = $followup['source'] ? $followup['source'] : null; if ($followup['from'] === 'other') { $followup['from'] = waRequest::post('from'); } if ($id && $id !== 'new') { unset($followup['last_cron_time']); $fm->updateById($id, $followup); $just_created = false; } else { $followup['last_cron_time'] = date('Y-m-d H:i:s'); $id = $fm->insert($followup); $just_created = true; } $f = $fm->getById($id); if ($f) { $f['just_created'] = $just_created; /** * Notify plugins about created or modified followup * @event followup_save * @param array[string]int $params['id'] followup_id * @param array[string]bool $params['just_created'] * @return void */ wa()->event('followup_save', $f); } } } // List of all follow-ups $followups = $fm->getAll('id'); // Get data to show in form $followup = null; if ($id) { if (empty($followups[$id])) { if ($followups) { $followup = reset($followups); } } else { $followup = $followups[$id]; } } $test_orders = array(); if (empty($followup)) { $followup = $fm->getEmptyRow(); $followup['body'] = self::getDefaultBody(); } else { // Orders used as sample data for testing $om = new shopOrderModel(); $test_orders = $om->where("paid_date IS NOT NULL AND state_id <> 'deleted'")->order('id DESC')->limit(10)->fetchAll('id'); shopHelper::workupOrders($test_orders); $im = new shopOrderItemsModel(); foreach ($im->getByField('order_id', array_keys($test_orders), true) as $i) { $test_orders[$i['order_id']]['items'][] = $i; } foreach ($test_orders as &$o) { $o['items'] = ifset($o['items'], array()); $o['total_formatted'] = waCurrency::format('%{s}', $o['total'], $o['currency']); } } $this->view->assign('followup', $followup); $this->view->assign('followups', $followups); $this->view->assign('test_orders', $test_orders); $this->view->assign('last_cron', wa()->getSetting('last_followup_cli')); $this->view->assign('cron_ok', wa()->getSetting('last_followup_cli') + 3600 * 36 > time()); $this->view->assign('cron_command', 'php ' . wa()->getConfig()->getRootPath() . '/cli.php shop followup'); $this->view->assign('default_email_from', $this->getConfig()->getGeneralSettings('email')); $this->view->assign('routes', wa()->getRouting()->getByApp('shop')); }
public function currency($full_info = false) { $currency = $this->wa->getConfig()->getCurrency(false); if ($full_info) { return waCurrency::getInfo($currency); } else { return $currency; } }
/** * Вспомогательный метод для печати формы с идентификатором 116. * * @param waOrder $order Объект, содержащий информацию о заказе * @param array $params * @return string HTML-код формы */ private function displayPrintForm116(waOrder $order, $params = array()) { $strict = true; $request = waRequest::request(); switch ($side = waRequest::get('side', $order ? '' : 'print', waRequest::TYPE_STRING)) { case 'front': $image_info = null; if ($image = $this->read('f116_front.gif', $image_info)) { $address = $this->splitAddress($order); $this->printOnImage($image, waRequest::request('order_amount', $order->total), 294, 845, 24); $this->printOnImage($image, waRequest::request('order_price', $order->total), 294, 747, 24); //customer $this->printOnImage($image, waRequest::request('shipping_name', $order->contact_name), 390, 915); $this->printOnImage($image, waRequest::request('shipping_address_1', $address[0]), 390, 975); $this->printOnImage($image, waRequest::request('shipping_address_2', $address[1]), 300, 1040); $this->printOnImagePersign($image, waRequest::request('shipping_zip', $order->shipping_address['zip']), 860, 1105, 55, 35); //company $this->printOnImage($image, $this->company_name, 420, 1170); $this->printOnImage($image, $this->address1, 400, 1237); $this->printOnImage($image, $this->address2, 300, 1304); $this->printOnImagePersign($image, $this->zip, 1230, 1304, 55, 35); //additional $this->printOnImage($image, waRequest::request('order_price_d', waCurrency::format('%2', $order->total, $order->currency)), 590, 2003); $this->printOnImage($image, waRequest::request('order_amount_d', waCurrency::format('%2', $order->total, $order->currency)), 1280, 2003); $this->printOnImage($image, waRequest::request('shipping_name', $order->contact_name), 390, 2085); $this->printOnImage($image, waRequest::request('shipping_address_1', $address[0]), 390, 2170); $this->printOnImage($image, waRequest::request('shipping_address_2', $address[1]), 300, 2260); $this->printOnImagePersign($image, waRequest::request('shipping_zip', $order->shipping_address['zip']), 1230, 2260, 55, 35); header("Content-type: image/gif"); imagegif($image); exit; } break; case 'back': $image_info = null; if ($image = $this->read('f116_back.gif', $image_info)) { header("Content-type: image/gif"); imagegif($image); exit; } break; case 'print': if (!$strict && !$order) { $this->view()->assign('action', 'preview'); } $this->view()->assign('editable', false); $this->view()->assign('order', $order); break; default: $this->view()->assign(array('src_front' => http_build_query(array_merge($request, array('side' => 'front'))), 'src_back' => http_build_query(array_merge($request, array('side' => 'back'))))); if (!$strict && !$order) { $this->view()->assign('action', 'preview'); } $this->view()->assign('editable', waRequest::post() ? false : true); $this->view()->assign('order', $order); $this->view()->assign('address', $this->splitAddress($order)); break; } return $this->view()->fetch($this->path . '/templates/form116.html'); }
/** * Вспомогательный метод для печати формы с идентификатором 112. * * @param waOrder $order Объект, содержащий информацию о заказе * @param array $params * @return string HTML-код формы */ private function displayPrintForm112(waOrder $order, $params = array()) { $strict = true; $request = waRequest::request(); $order['rub'] = intval(waRequest::request('rub', round(floor($order->total)))); $order['cop'] = min(99, max(0, intval(waRequest::request('cop', round($order->total * 100 - $order['rub'] * 100))))); switch ($side = waRequest::get('side', $order ? '' : 'print', waRequest::TYPE_STRING)) { case 'front': $image_info = null; if ($image = $this->read('f112ep_front.gif', $image_info)) { $format = '%.W{n0} %.2{f0}'; $this->printOnImage($image, sprintf('%d', $order['rub']), 110, 680); $this->printOnImage($image, sprintf('%02d', $order['cop']), 430, 680); $this->printOnImage($image, waRequest::request('order_amount', waCurrency::format($format, $order->total, $order->currency)), 650, 620, 30); $this->printOnImage($image, $this->company_name, 210, 875); $this->printOnImage($image, $this->address1, 210, 957); $this->printOnImage($image, $this->address2, 70, 1040); $this->printOnImagePersign($image, $this->zip, 1965, 1020, 58.3, 50); $this->printOnImagePersign($image, $this->inn, 227, 1330, 55.5, 45); $this->printOnImagePersign($image, $this->bank_kor_number, 1207, 1330, 55.5, 45); $this->printOnImage($image, $this->bank_name, 570, 1405); $this->printOnImagePersign($image, $this->bank_account_number, 310, 1470, 55.5, 45); $this->printOnImagePersign($image, $this->bik, 1815, 1470, 55.5, 45); header("Content-type: image/gif"); imagegif($image); exit; } else { throw new waException('Image not found', 404); } break; case 'print': if (!$strict && !$order) { $this->view()->assign('action', 'preview'); } $this->view()->assign('editable', waRequest::post() ? false : true); break; default: $this->view()->assign('src_front', http_build_query(array_merge($request, array('side' => 'front')))); if (!$strict && !$order) { $this->view()->assign('action', 'preview'); } $this->view()->assign('order', $order); $this->view()->assign('editable', waRequest::post() ? false : true); break; } return $this->view()->fetch($this->path . '/templates/form112.html'); }
protected function getCurrencyInfo() { $currency = waCurrency::getInfo($this->getConfig()->getCurrency(false)); $locale = waLocale::getInfo(wa()->getLocale()); return array('code' => $currency['code'], 'sign' => $currency['sign'], 'sign_html' => !empty($currency['sign_html']) ? $currency['sign_html'] : $currency['sign'], 'sign_position' => isset($currency['sign_position']) ? $currency['sign_position'] : 1, 'sign_delim' => isset($currency['sign_delim']) ? $currency['sign_delim'] : ' ', 'decimal_point' => $locale['decimal_point'], 'frac_digits' => $locale['frac_digits'], 'thousands_sep' => $locale['thousands_sep']); }
function wa_currency($n, $currency, $format = '%{s}') { return waCurrency::format($format, $n, $currency); }
public function execute() { $id = waRequest::request('id', null, waRequest::TYPE_INT); $scm = new shopCustomerModel(); $customer = $scm->getById($id); try { $contact = new waContact($id); $contact->getName(); } catch (waException $e) { // !!! What to do when shop_customer exists, but no wa_contact found? throw $e; } $ccsm = new waContactCategoriesModel(); $contact_categories = $ccsm->getContactCategories($id); $contacts_url = wa()->getAppUrl('contacts'); // Info above tabs $top = array(); foreach (array('email', 'phone', 'im') as $f) { if ($v = $contact->get($f, 'top,html')) { $top[] = array('id' => $f, 'name' => waContactFields::get($f)->getName(), 'value' => is_array($v) ? implode(', ', $v) : $v); } } // Get photo $photo = $contact->get('photo'); $config = $this->getConfig(); $use_gravatar = $config->getGeneralSettings('use_gravatar'); $gravatar_default = $config->getGeneralSettings('gravatar_default'); if (!$photo && $use_gravatar) { $photo = shopHelper::getGravatar($contact->get('email', 'default'), 96, $gravatar_default); } else { $photo = $contact->getPhoto(96); } $contact['photo'] = $photo; // Customer orders $im = new shopOrderItemsModel(); $orders_collection = new shopOrdersCollection('search/contact_id=' . $id); $total_count = $orders_collection->count(); $orders = $orders_collection->getOrders('*,items,params', 0, $total_count); shopHelper::workupOrders($orders); foreach ($orders as &$o) { $o['total_formatted'] = waCurrency::format('%{s}', $o['total'], $o['currency']); $o['shipping_name'] = ifset($o['params']['shipping_name'], ''); $o['payment_name'] = ifset($o['params']['payment_name'], ''); // !!! TODO: shipping and payment icons } // Customer reviews $prm = new shopProductReviewsModel(); $reviews = $prm->getList('*,is_new,product', array('escape' => false, 'where' => array('contact_id' => $id), 'limit' => false)); // Customer affiliate transactions history $atm = new shopAffiliateTransactionModel(); $affiliate_history = $atm->getByContact($id); $this->view->assign('top', $top); $this->view->assign('orders', $orders); $this->view->assign('reviews', $reviews); $this->view->assign('contact', $contact); $this->view->assign('customer', $customer); $this->view->assign('contacts_url', $contacts_url); $this->view->assign('affiliate_history', $affiliate_history); $this->view->assign('contact_categories', $contact_categories); $this->view->assign('def_cur_tmpl', str_replace('0', '%s', waCurrency::format('%{s}', 0, wa()->getConfig()->getCurrency()))); $this->view->assign('point_rate', str_replace(',', '.', (double) str_replace(',', '.', wa()->getSetting('affiliate_usage_rate')))); $fields = waContactFields::getAll('person'); if (isset($fields['name'])) { unset($fields['name']); } $this->view->assign('fields', $fields); $this->view->assign('orders_default_view', $config->getOption('orders_default_view')); /* * @event backend_customer * @return array[string]array $return[%plugin_id%] array of html output * @return array[string][string]string $return[%plugin_id%]['info_section'] html output * @return array[string][string]string $return[%plugin_id%]['name_suffix'] html output * @return array[string][string]string $return[%plugin_id%]['header'] html output * @return array[string][string]string $return[%plugin_id%]['action_link'] html output */ $this->view->assign('backend_customer', wa()->event('backend_customer', $customer)); }
/** * Format amount according to currency settings and current locale settings. * * TODO: document format string specifications. For now, see unit tests for some usage clues. * * @param string $format * @param float $n amount to format * @param string $currency 3-letter iso code * @param string $locale */ public static function format($format, $n, $currency, $locale = null) { $old_locale = waSystem::getInstance()->getLocale(); if ($locale === null) { $locale = $old_locale; } if ($locale !== $old_locale) { wa()->setLocale($locale); } $currency = waCurrency::getInfo($currency); waLocale::loadByDomain('webasyst', $locale); $locale = waLocale::getInfo($locale); self::$data['n'] = $n; self::$data['locale'] = $locale; self::$data['currency'] = $currency; $pattern = '/%([0-9]?\\.?[0-9]?)([iw]*)({[n|f|c|s][0-9]?})?/i'; $result = preg_replace_callback($pattern, array('self', 'replace_callback'), $format); if ($locale !== $old_locale) { wa()->setLocale($old_locale); } return $result; }
function wa_currency_html($n, $currency, $format = '%{h}') { return waCurrency::format($format, $n, $currency); }
protected static function getData() { if (self::$data === null) { $config = wa()->getConfig()->getConfigFile('currency'); $config_path = wa()->getConfig()->getPath('config') . '/currency.php'; $cache = new waSystemCache('config/currency' . wa()->getLocale()); if ($config && filemtime($config_path) > $cache->getFilemtime()) { self::$data = array(); } else { self::$data = $cache->get(); } if (!self::$data) { self::$data = array(); $files = waFiles::listdir(dirname(__FILE__) . "/data/"); foreach ($files as $file) { if (preg_match('/^([A-Z]{3})\\.php$/', $file, $matches)) { $currency = $matches[1]; $file = wa()->getConfig()->getPath('system') . "/currency/data/" . $currency . ".php"; if (file_exists($file)) { $info = (include $file); $info['title'] = _ws($info['title']); } else { $info = array('sign' => $currency, 'title' => $currency); } self::$data[$currency] = $info; } } foreach ($config as $cur => $info) { if (!isset(self::$data[$cur])) { self::$data[$cur] = $info; } else { foreach ($info as $k => $v) { self::$data[$cur][$k] = $v; } } } $cache->set(self::$data); } } return self::$data; }
public function add($code) { $code = $this->escape($code); if ($this->getById($code)) { return false; } $currencies = waCurrency::getAll(true); if (!isset($currencies[$code])) { return false; } $sort = $this->query("SELECT MAX(sort) sort FROM `{$this->table}`")->fetchField('sort') + 1; $result = $this->insert(array('code' => $code, 'sort' => $sort)); $cache = new waRuntimeCache('shop_currencies'); $cache->delete(); if ($cache = wa('shop')->getCache()) { $cache->delete('currencies'); } return $result; }
/** * Format amount according to currency settings and current locale settings. * * TODO: document format string specifications. For now, see unit tests for some usage clues. * * @param string $format * @param float $n amount to format * @param string $currency 3-letter iso code * @param string $locale */ public static function format($format, $n, $currency, $locale = null) { $old_locale = waSystem::getInstance()->getLocale(); if ($locale === null) { $locale = $old_locale; } $currency = waCurrency::getInfo($currency); if ($format == '%w' || $format == '%W') { if ($locale !== $old_locale) { wa()->setLocale($locale); } waLocale::loadByDomain('webasyst', $locale); } $locale = waLocale::getInfo($locale); $result = preg_replace('/%([0-9]?\\.?[0-9]?)([iw]*)({[n|f|c|s][0-9]?})?/ie', 'self::extract($n, $currency, $locale, "$1", "$2", "$3")', $format); if ($locale !== $old_locale) { wa()->setLocale($old_locale); } return $result; }
public function getCurrencyHtml() { $info = waCurrency::getInfo($this->currency); return ifset($info['sign_html'], $info['sign']); }
public static function discount(&$order, $contact, $apply, $other_discounts) { if (!$contact || !$contact->getId()) { return 0; } $checkout_data = wa()->getStorage()->read('shop/checkout'); if (empty($checkout_data['use_affiliate'])) { return 0; // !!! Will this fail when recalculating existing order? } $usage_rate = (double) wa()->getSetting('affiliate_usage_rate', 0, 'shop'); if ($usage_rate <= 0) { return 0; } $cm = new shopCustomerModel(); $customer = $cm->getById($contact->getId()); if (!$customer || $customer['affiliate_bonus'] <= 0) { return 0; } $order_total = $order['total'] - $other_discounts; $max_bonus = $customer['affiliate_bonus']; if (!empty($order['params']['affiliate_bonus'])) { // Recalculating existing order: take old discount into account $max_bonus += $order['params']['affiliate_bonus']; } $crm = new shopCurrencyModel(); $discount = (double) $crm->convert($max_bonus * $usage_rate, wa()->getConfig()->getCurrency(true), wa()->getConfig()->getCurrency(false)); if ($discount > $order_total) { $discount = $order_total; } if ($discount < $order_total) { $bonus_used = $max_bonus; } else { $bonus_used = (double) $crm->convert($discount, wa()->getConfig()->getCurrency(false), wa()->getConfig()->getCurrency(true)) / $usage_rate; } if (empty($order['params'])) { $order['params'] = array(); } $order['params']['affiliate_bonus'] = $bonus_used; if ($apply) { $balance_change = $max_bonus - $bonus_used - $customer['affiliate_bonus']; if (abs($balance_change) > 0.0001) { if (!empty($order['params']['affiliate_bonus'])) { $message = sprintf_wp('Recalculation of order total, new discount: %s', waCurrency::format('%{s}', $discount, wa()->getConfig()->getCurrency())); } else { $message = sprintf_wp('Discount of %s', waCurrency::format('%{s}', $discount, wa()->getConfig()->getCurrency())); } $atm = new shopAffiliateTransactionModel(); $atm->applyBonus($contact->getId(), $balance_change, ifset($order['id']), $message); } } return $discount; }
public static function settingCurrencySelect() { $options = array(); $options[''] = '-'; $app_config = wa()->getConfig(); if (method_exists($app_config, 'getCurrencies')) { $currencies = $app_config->getCurrencies(); foreach ($currencies as $code => $currency) { $options[$code] = array('value' => $code, 'title' => $currency['title'] . ' (' . $code . ')', 'description' => $currency['code']); } } else { $currencies = waCurrency::getAll(); foreach ($currencies as $code => $currency_name) { $options[$code] = array('value' => $code, 'title' => $currency_name . ' (' . $code . ')', 'description' => $code); } } return $options; }
public function getSystemCurrencies() { $system_currencies = waCurrency::getAll(true); ksort($system_currencies); return $system_currencies; }