def purchase_landscape(self, company: Company) -> None: """The function will withdraw a certain amount of money from given company :param company: The company instance that wish to own this landscape DEPRECATED: consider using buy_landscape for better naming This function does not call company_able_to_purchase_method, you must call it manually and before this function or else an exception will be thrown or alternative way is you must call Company.can_own_landscape() """ if isinstance(company, Company): self.company = company self.company_name = company.company_name if self.required_extra_continent_cost(company): extra_cost = self.get_extra_contient_cost(company, 'buy') company_new_balance = company.balance - (self.buy_cost + extra_cost) else: company_new_balance = company.balance - self.buy_cost if company_new_balance < 0: raise UnableToOwnLandscape("Company balance does not meet the requirements") company.balance = company_new_balance company.save() self.buy() else: raise TypeError("The company param must be an instance of Company but got {} instead".format(type(company)))
def pay_rent(self, company: Company) -> None: """Pay the required rent. This method calls needs_to_pay_rent method directly to check if the building needs to be paid. This is to ensure that user do not pay too soon or too late. """ if self.needs_to_pay_rent(): # Only the same owner can pay the rent if self.company == company: company.balance -= self.rent_cost if company.balance < 0: raise ValueError("Insufficient amount of money to pay") company.save() self.last_collected_money_at = timezone.now()
def buy_items(self, company_buyer: Company, item_list: list): total_cost = GlobalStoreItem.objects.filter( item_id__in=item_list, company_store=self).aggregate(Sum('price')) if company_buyer._does_not_have_enough_money(total_cost): raise ValueError("Company must have enough money") else: company_buyer.pay(total_cost) self.total_money = total_cost self.save() items = GlobalStoreItem.objects.filter(item_id__in=item_list).update( is_bought=True) return items
def buy(self, company: Company, quantity: int): """The function to buy the product""" quantity: int = int(quantity) # try to convert to integer type when neccessary if isinstance(company, Company): # explicit type check to prevent further error # check if the request to buy product lower or equal to available number of products left if quantity <= self.quantity: total_cost = quantity * self.price # if company does not have enough money to buy (result in 0) if company.balance - total_cost < 0: raise TypeError("Company balance cannot be negative number") company.balance = company.balance - total_cost company.save() self.quantity = self.quantity - quantity if self.quantity == 0: self.bought_at = timezone.now()