def save(self, payment): o = model.Order() o.order_number = self.generate_order_number() o.offer_id = payment.offer_id if hasattr(payment, 'subscribe'): o.subscribe = payment.subscribe o.payment_method = payment.payment_method o.currency = payment.currency if hasattr(payment, 'country'): o.country = payment.country o.lang = payment.lang b = model.Billing() b.first_name = sanitizer.html_to_text(payment.billing.first_name) b.last_name = sanitizer.html_to_text(payment.billing.last_name) b.address1 = sanitizer.html_to_text(payment.billing.address1) if hasattr(payment.billing, 'address2'): b.address2 = sanitizer.html_to_text(payment.billing.address2) b.country = payment.billing.country b.city = sanitizer.html_to_text(payment.billing.city) b.postal_code = sanitizer.html_to_text(payment.billing.postal_code) if hasattr(payment.billing, 'county'): b.county = sanitizer.html_to_text(payment.billing.county) b.email = sanitizer.html_to_text(payment.billing.email) b.same_address = payment.billing.same_address o.billing.append(b) if payment.shipping is not None and b.same_address == False: s = model.Shipping() s.first_name = sanitizer.html_to_text(payment.shipping.first_name) s.last_name = sanitizer.html_to_text(payment.shipping.last_name) s.address1 = sanitizer.html_to_text(payment.shipping.address1) if hasattr(payment.shipping, 'address2'): s.address2 = sanitizer.html_to_text(payment.shipping.address2) s.country = payment.shipping.country s.city = sanitizer.html_to_text(payment.shipping.city) s.postal_code = sanitizer.html_to_text(payment.shipping.postal_code) if hasattr(payment.shipping, 'county'): s.county = sanitizer.html_to_text(payment.shipping.county) if hasattr(payment.shipping, 'email'): s.email = sanitizer.html_to_text(payment.shipping.email) if hasattr(payment.shipping, 'company'): s.company = sanitizer.html_to_text(payment.shipping.company) if hasattr(payment.shipping, 'phone_number'): s.phone_number = sanitizer.html_to_text(payment.shipping.phone_number) o.shipping.append(s) offer_service = self.ioc.new_offer_service() offer = offer_service.find_by_id(payment.offer_id) if offer is None: raise ValueError("Couldn't find offer by id:"+payment.offer_id) o.offer_hash = offer.hash if payment.items is None or len(payment.items)==0: raise ValueError("No items specified") o.account_id = offer.account_id # validate requested availability for item_dto in payment.items: if hasattr(item_dto, 'variation_id'): item = self.find_item_by_id(offer.items, item_dto.id) if item is not None and item.status==1: variations = [] oi = model.OrderItem(o, item.title_en, item.title_fr, item_dto.quantity, item.net, item.tax, item.shipping) if hasattr(item, 'shipping_additional'): oi.shipping_additional = item.shipping_additional oi.multivariate = item.multivariate oi.offer_item_id = item.id item_variation = self.find_item_by_id(item.variations, item_dto.variation_id) if item_variation is not None and item_variation.status==1 and item_variation.id==item_dto.selection: if item_dto.quantity>item_variation.quantity: raise quantity_not_available.QuantityNotAvailable("Trying to buy more products then are available") else: oiv = model.OrderItemVariation(oi, item_variation.title_en, item_variation.title_fr, item_dto.quantity, item_variation.net, item_variation.tax, item_variation.shipping) if hasattr(item_variation, 'shipping_additional'): oiv.shipping_additional = item_variation.shipping_additional oiv.offer_item_variation_id = item_variation.id variations.append(oiv) oi.variations = variations o.items.append(oi) else: raise ValueError("Couldn't find offer item for id:%s"%str(item_dto.id)) else: if item_dto.quantity is not None and item_dto.quantity!=0: item = self.find_item_by_id(offer.items, item_dto.id) if item is not None and item.status==1: if item.variations is not None and item.variations.count()!=0: raise ValueError("Requesting item where there is variation selection") else: if item_dto.quantity>item.quantity: raise quantity_not_available.QuantityNotAvailable("Trying to buy more products then are available") else: oi = model.OrderItem(o, item.title_en, item.title_fr, item_dto.quantity, item.net, item.tax, item.shipping) oi.shipping_additional = item.shipping_additional oi.offer_item_id = item.id oi.multivariate = item.multivariate o.items.append(oi) else: raise ValueError("Couldn't find offer item for id: %s"%item_dto.id) else: raise ValueError("No quantity specified or variation") o.total = self.find_order_total(o) model.base.db.session.add(o) model.base.db.session.commit() return o
def save_offer(self, account, offer_dto): if isinstance(account, model.Account)==False: raise TypeError("Expected Account type in OfferService.save_offer %s"%type(account)) o = model.Offer.query.filter(model.Offer.id == offer_dto.id, model.Offer.account_id == account.id).first() if o is None: raise RuntimeError("Couldn't find offer for offer id %s and account id %s"%(offer_dto.id, account.id)) if o.status==0: o.hash = self.generate_hash() o.status = 1 if hasattr(offer_dto, 'title_en'): o.title_en = sanitizer.html_to_text(offer_dto.title_en) if hasattr(offer_dto, 'title_fr'): o.title_fr = sanitizer.html_to_text(offer_dto.title_fr) o.currency = offer_dto.currency o.visibility = offer_dto.visibility if o.items is not None and len(o.items.all())>0: for item in o.items: offer_item_dto = self.find_item_by_id(item.id, offer_dto.items) if offer_item_dto is None: o.items.remove(item) if o.items is not None and len(o.items.all())>0: for item in o.items: offer_item_dto = self.find_item_by_id(item.id, offer_dto.items) if offer_item_dto is not None: if hasattr(offer_item_dto, 'title_en'): item.title_en = sanitizer.html_to_text(offer_item_dto.title_en) if hasattr(offer_item_dto, 'title_fr'): item.title_fr = sanitizer.html_to_text(offer_item_dto.title_fr) if hasattr(offer_item_dto, 'description_en'): item.description_en = offer_item_dto.description_en if hasattr(offer_item_dto, 'description_fr'): item.description_fr = offer_item_dto.description_fr if hasattr(offer_item_dto, 'condition'): item.condition = offer_item_dto.condition item.status = 1 item.multivariate = offer_item_dto.multivariate if item.multivariate==1: if item.variations is not None and len(item.variations.all())>0: for iv in item.variations: iv_dto = self.find_item_by_id(iv.id, offer_item_dto.variations) if iv_dto is None: item.variations.remove(iv) for iv in item.variations: iv_dto = self.find_item_by_id(iv.id, offer_item_dto.variations) if iv_dto is not None: if hasattr(iv_dto, 'title_en'): iv.title_en = sanitizer.html_to_text(iv_dto.title_en) if hasattr(iv_dto, 'title_fr'): iv.title_fr = sanitizer.html_to_text(iv_dto.title_fr) iv.quantity = iv_dto.quantity iv.net = iv_dto.net if hasattr(iv_dto, 'tax'): iv.tax = iv_dto.tax iv.shipping = iv_dto.shipping if hasattr(iv_dto, 'shipping_additional'): iv.shipping_additional = iv_dto.shipping_additional iv.status = 1 else: item.quantity = offer_item_dto.quantity item.net = offer_item_dto.net if hasattr(offer_item_dto, 'tax'): item.tax = offer_item_dto.tax item.shipping = offer_item_dto.shipping if hasattr(offer_item_dto, 'shipping_additional'): item.shipping_additional = offer_item_dto.shipping_additional model.base.db.session.commit() return o