def set_sale_discount(): """ Set the total sale discount """ sale = db.sale(request.args(0)) valid_sale(sale) # we cannot modify defered sale or online purchased bag if sale.is_deferred or sale.id_bag.is_paid: raise HTTP(405) discount = 0 try: discount = D(request.args(1)) if not (auth.has_membership("Manager") or auth.has_membership("Admin")): discount = min(discount, auth.user.max_discount) discount = min(max(0, discount), 100) except: raise HTTP(405, "Invalid amount") sale.discount_percentage = discount sale.discount = sale.total * discount / 100 sale.update_record() sale.total -= sale.discount # remove all payments for payment in db(db.payment.id_sale == sale.id).iterselect(): sale_utils.modify_payment(sale, payment, {}, True) redirect(URL("sale", "update", args=sale.id)) return dict(sale_total=sale.total, sale_discount=discount, sale_discount_percentage=sale.discount_percentage)
def set_sale_discount(): """ Set the total sale discount """ sale = db.sale(request.args(0)) valid_sale(sale) # we cannot modify defered sale or online purchased bag if sale.is_deferred or sale.id_bag.is_paid: raise HTTP(405) discount = 0 try: discount = D(request.args(1)) if not (auth.has_membership("Manager") or auth.has_membership("Admin")): discount = min(discount, auth.user.max_discount) discount = min(max(0, discount), 100) except: raise HTTP(405, "Invalid amount") sale.discount_percentage = discount sale.discount = sale.total * discount / 100 sale.update_record() sale.total -= sale.discount # remove all payments for payment in db(db.payment.id_sale == sale.id).iterselect(): sale_utils.modify_payment(sale, payment, {}, True) redirect(URL('sale', 'update', args=sale.id)) return dict(sale_total=sale.total, sale_discount=discount, sale_discount_percentage=sale.discount_percentage)
def update_payment(): """ args: [id_sale, id_payment] vars: [amount, account, wallet_code, delete] """ from cp_errors import CP_PaymentError sale = db.sale(request.args(0)) valid_sale(sale) # cant modify payments for bag paid online if sale.id_bag.is_paid: raise HTTP(405) payment = db((db.payment.id == request.args(1)) & (db.payment.id_sale == sale.id)).select().first() if not payment: raise HTTP(404) if not payment.is_updatable: raise HTTP(405) # Accept updates for certain amount of time (7 minutes) when the sale has been defered, this prevents sellers to modify previous sale payments if (request.now - payment.created_on).seconds / 60.0 / 7.0 > 1: if sale.is_deferred: payment.is_updatable = False payment.update_record() raise HTTP(405) try: new_amount = D(request.vars.amount or payment.amount) except: raise HTTP(417) delete_payment = bool(request.vars.delete) payment_data = dict( amount=D(request.vars.amount or payment.amount), account=request.vars.account or payment.account, wallet_code=request.vars.wallet_code or payment.wallet_code, ) d = sale_utils.modify_payment(sale, payment, payment_data, delete_payment) extra_updated_payments = d.get("updated_payments") payments_total = d.get("payments_total") return dict(updated=extra_updated_payments, payments_total=payments_total)
def update_payment(): """ args: [id_sale, id_payment] vars: [amount, account, wallet_code, delete] """ from cp_errors import CP_PaymentError sale = db.sale(request.args(0)) valid_sale(sale) # cant modify payments for bag paid online if sale.id_bag.is_paid: raise HTTP(405) payment = db((db.payment.id == request.args(1)) & (db.payment.id_sale == sale.id)).select().first() if not payment: raise HTTP(404) if not payment.is_updatable: raise HTTP(405) # Accept updates for certain amount of time (7 minutes) when the sale has been defered, this prevents sellers to modify previous sale payments if (request.now - payment.created_on).seconds / 60.0 / 7.0 > 1: if sale.is_deferred: payment.is_updatable = False payment.update_record() raise HTTP(405) try: new_amount = D(request.vars.amount or payment.amount) except: raise HTTP(417) delete_payment = bool(request.vars.delete) payment_data = dict(amount=D(request.vars.amount or payment.amount), account=request.vars.account or payment.account, wallet_code=request.vars.wallet_code or payment.wallet_code) d = sale_utils.modify_payment(sale, payment, payment_data, delete_payment) extra_updated_payments = d.get('updated_payments') payments_total = d.get('payments_total') return dict(updated=extra_updated_payments, payments_total=payments_total)