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()));
 }
Esempio n. 2
0
 /** Discounts by amount of money previously spent by this customer. */
 protected static function byCustomerTotal($order, $contact, $apply)
 {
     if (!$contact || !$contact->getId()) {
         return 0;
     }
     $cm = new shopCustomerModel();
     $customer = $cm->getById($contact->getId());
     if ($customer && $customer['total_spent'] > 0) {
         $dbsm = new shopDiscountBySumModel();
         return max(0.0, min(100.0, (double) $dbsm->getDiscount('customer_total', $customer['total_spent']))) * $order['total'] / 100.0;
     }
     return 0.0;
 }