Ejemplo n.º 1
0
 def _save_rows(self, purchase, receipt_dct, excl_vat=False):
     """ 
     Save all saleitems in receipt_dct.
     """
     store = purchase.pos.store
     chain = store.chain
     total_cost = 0
     total_cost_excl_vat = 0
     count_extra = settings.COUNT_EXTRA_AS_TWOSELL
     
     for item in receipt_dct['items']:
         if not item['id']:
             item['id'] = get_twosell_positemid(title=item['title'])
         
         positemid = item['id']
         vat_rate = item['tax_rate']
         price = calc_incl_vat(vat_rate, item['price']) if excl_vat else item['price']
         quantity = item['quantity']
         row_cost = price * quantity
         row_discount, twosell_coupon_used, twosell_coupon_discount = self._handle_row_discounts(item, purchase, vat_rate, excl_vat)
         
         modified = Product.objects.is_modified(positemid, item['title'])
         
         # Product data
         product, created = Product.objects.create_or_update(chain=chain, 
             articlenum=positemid, title=item['title'], vat_rate=vat_rate,
             modified=modified
         )
         
         # Product in store data
         product_in_store, created = ProductInStore.objects.create_or_update(
             product=product, store=store, price=price, active=True
         )
         
         # Purchased product data
         purchased_prod, created = PurchasedProduct.objects.get_or_create(
             product=product, purchase=purchase, final=receipt_dct['final']
         )
         purchased_prod.n_items = quantity
         purchased_prod.total_discount = row_discount
         purchased_prod.total_cost = (row_cost - row_discount)
         
         if twosell_coupon_used:
             purchased_prod.twosell_coupon = row_cost - twosell_coupon_discount
         
         purchased_prod.save()
         total_cost += purchased_prod.total_cost
         total_cost_excl_vat += purchased_prod.total_cost_excl_vat
     
     try:
         total_vat_rate = (total_cost - total_cost_excl_vat / total_cost_excl_vat)
     except (InvalidOperation, ZeroDivisionError):
         pass
     else:
         self._handle_gen_discount(purchase, receipt_dct, total_vat_rate, excl_vat)
     
     return purchase
Ejemplo n.º 2
0
 def _handle_row_discounts(self, dct_item, purchase, vat_rate, excl_vat=False):
     row_discount = 0
     twosell_coupon_discount = 0
     twosell_coupon_used = False
     for discount in dct_item.get('discounts', []):
         twosell_id = discount.get('twosell_id', '')
         amount = calc_incl_vat(vat_rate, discount['amount']) if excl_vat else discount['amount']
         row_discount += amount
         
         if Offer.objects.use_code(twosell_id, purchase):
             twosell_coupon_used = True
             twosell_coupon_discount += amount
         
     return row_discount, twosell_coupon_used, twosell_coupon_discount
Ejemplo n.º 3
0
 def _handle_gen_discount(self, purchase, receipt_dct, vat_rate, excl_vat):
     if excl_vat:
         purchase.gen_discount = calc_incl_vat(vat_rate, sum(receipt_dct.get('discounts', [])))
     else:
         purchase.gen_discount = sum(receipt_dct.get('discounts', []))