def add_new_sales(store, sales_data): logger.debug("add_new_sales begin") store_objs = Store.objects.filter(store_name=store) if not store_objs: raise ValidationError("Can't find store: %s" % store) else: store_obj = store_objs[0] #validation SalesHelper.validate_sales(sales_data) #create object sales_obj = Sales() sales_obj.sales_store = store_obj sales_obj.sales_date = date.today() sales_obj.sales_payment = sales_data["sales_payment"] sales_obj.sales_currency = sales_data["sales_currency"] sales_obj.sales_remark = sales_data["sales_remark"] sales_obj.save() items = sales_data['items'] for item in items: p = Product.objects.get(prod_name=item['out_product']) sales_obj.outtransaction_set.create(out_product=p, out_serial=item['out_serial'], out_quantity=item['out_quantity'], out_unit_price=item['out_unit_price'] ) sales_obj.save() ret = {"sales id": sales_obj.id} logger.debug("add_new_sales done %s" % ret) return ret
def submit_sales(request, store): logger.debug('submit_sales store ' + store) store_objs = Store.objects.filter(store_name=store) if not store_objs: error = "Can't find store: " + store return HttpResponse(error, status=403) else: store_obj = store_objs[0] #print "Store id " + str(store_obj.id) raw = str(request.body) sales = json.loads(raw) logger.debug('raw ' + request.body) logger.debug('action ' + sales['action']) items = sales['items'] for item in items: ret = add_product_if_not_exist(item['product']) sales_obj = Sales() sales_obj.sales_store = store_obj sales_obj.sales_date = date.today() sales_obj.sales_payment = sales['payment'] sales_obj.sales_currency = sales['currency'] sales_obj.sales_remark = sales['remark'] sales_obj.save() for item in items: p = Product.objects.get(prod_name=item['product']) sales_obj.outtransaction_set.create(out_product=p, out_serial=item['serial'], out_quantity=item['unit'], out_unit_price=item['price'] ) sales_obj.save() logger.debug('sales id %s', sales_obj.id) return HttpResponse(sales_obj.id)