/** * @param $id * @param array $params * @return shopCouponPluginCoupon */ public static function gen($id, $params = array()) { $m = new shopCouponModel(); $tm = new shopCouponPluginTemplateModel(); if (!($gen = $tm->getById($id))) { return new shopCouponPluginCoupon(); } if (!($candidates = self::_candidates($gen))) { return new shopCouponPluginCoupon(); } // Кто-то читает код? :) // Стоит проверять в цикле, чтоб наверняка? //do{ $exists = $m->select('code')->where('code IN(?)', $candidates)->fetchAll(null, true); $candidates = array_diff($candidates, $exists); $code = reset($candidates); //} while(empty($code)); $comment = $gen['comment']; if (!empty($params['contact_id'])) { $contact = new waContact($params['contact_id']); if ($contact->exists()) { $comment .= "\n" . _wp('for') . ' ' . $contact->getName(); } $comment = trim($comment); } try { $code = mb_substr($code, 0, 32); $coupon = array('code' => $code, 'type' => $gen['type'], 'limit' => $gen['limit'], 'value' => $gen['value'], 'comment' => $comment, 'expire_datetime' => $gen['expire_hours'] ? date('Y-m-d H:i:s', time() + $gen['expire_hours'] * 3600) : null, 'create_datetime' => date('Y-m-d H:i:s'), 'create_contact_id' => $gen['create_contact_id']); $m->insert($coupon); } catch (waDbException $e) { $coupon = array(); } return new shopCouponPluginCoupon($coupon); }