/** * Insert overrides the parent class method because we need for $code unicity * * @param string $query * @return object * @throws ShopException */ public function insert($query = 'insert%E') { if ($this->code) { $existentCoupons = $this->repo->getByCode($this->code, $this->module_srl); if ($existentCoupons && !empty($existentCoupons)) { if ($r = $this->getMeta('random')) { $this->generateCode($r['length'], $r['type'], $r['pattern'], $r['separateEvery'], $r['separator']); } else { throw new ShopException('Code already exists'); } } } else { if ($this->parent_srl) { throw new ShopException('Child coupon must have a code at insert'); } } return parent::insert($query); }
private function formTranslation(array $input) { $data = array('extra' => array()); $addressRepo = new AddressRepository(); if (self::validateFormBlock($billing = $input['billing'])) { if (is_numeric($billing['address'])) { $data['billing_address_srl'] = $billing['address']; } elseif (self::validateFormBlock($newAddress = $input['new_billing_address'])) { $newAddress = new Address($newAddress); if ($this->member_srl && !$addressRepo->hasDefaultAddress($this->member_srl, AddressRepository::TYPE_BILLING)) { $newAddress->default_billing = 'Y'; } $newAddress->save(); $data['billing_address_srl'] = $newAddress->address_srl; } else { throw new ShopException('No billing address'); } } $shipping = $input['shipping']; if (!isset($shipping['method'])) { throw new ShopException("Please choose a shipping method"); } $data['extra']['shipping_method'] = $shipping['method']; $data['extra']['shipping_variant'] = $shipping['variant']; $data['shipping_address_srl'] = $shipping['address']; // Shipping method // Shipping address validation - if different if ($input['new_shipping_address']) { if (!self::validateFormBlock($newAddress = $input['new_shipping_address'])) { throw new ShopException('Wrong shipping input'); } $newAddress = new Address($newAddress); if ($newAddress->isValid()) { if ($this->member_srl && !$addressRepo->hasDefaultAddress($this->member_srl, AddressRepository::TYPE_SHIPPING)) { $newAddress->default_shipping = 'Y'; } $newAddress->save(); $data['shipping_address_srl'] = $newAddress->address_srl; } } if (self::validateFormBlock($payment = $input['payment'])) { $data['extra']['payment_method'] = $payment['method']; } $hasCode = false; if ($code = $input['discount_code']) { $codesRepo = new CouponRepository(); $existingCoupons = $codesRepo->getByCode($code, $this->module_srl); if ($existingCoupons) { /** @var $coupon Coupon */ $coupon = $existingCoupons[0]; $this->setExtra('coupon_srl', $coupon->srl); $hasCode = true; } } if (!$hasCode && $this->getExtra('coupon_srl')) { $this->setExtra('coupon_srl', null); } return empty($data) ? null : $data; }