How display model with four relationships based on date in laravel? - laravel

I have these models:
Partner
Invoice
Bill
Transaction
CreditNote
Invoice, Bill, Transaction, and CreditNote belong to Partner.
and Partner has many invoices, bills, transactions, credit_notes.
I want to make SOA reports, which are based on date.
for example:
DATE TYPE ITEM
2021-1-1 partner invoiced us invoice_number..
2021-1-2 us invoiced partner bill_number..
2021-1-3 partner paid us transaction_debit
2021-1-4 us paid partner transaction_credit
2021-1-5 partner paid us credit_note
I use `rappasoft livewire data table to show this information.
I get partner stuff like this:
Partner::where('id', 1)->with('invoices', 'bills', 'transactions', 'credit_notes')->get()
I don't know how to display these information in a table a sort based on date
How to do this?

It sounds to me like your structure is wrong. All of the five things that you're putting in your table are what I'd consider "transactions" (not to be confused with your model "Transactions".
That being the case, then you want either want a Transactions model which is morphable (that's the more complicated solution) or which has a "type" property.
If morphable, each transaction would then be associated to the corresponding partner, and to the corresponding model - invoice, bill, payment, refund, etc. - on a "morphsTo" basis.
If you went down the easier route, each transaction would just have a type, linked to a TransactionType model, which would indicate the type of the transaction.
As things stand, your structure is overcomplicated. Either of the above approaches would simplify it.

Related

What is the difference between certain fields of entities in the Common Data Model (CDM) for sales?

While studying the CDM models for sales and mapping my data to different fields, I was confused about what is the difference between some fields like totalDiscountAmount and discountAmount in Order entity and between ownerId and owningUser. Can someone please clarify?
You can find all these details in documentation.
discountamount: Type the discount amount for the order if the customer is eligible for special savings.
totaldiscountamount: Shows the total discount amount, based on the discount price and rate entered on the order.
ownerid is of principal type, which can hold the value of either team or systemuser internally in owningteam or owninguser respectively - useful in security concepts across the CDS product. Read more

How to implement constraints that are external to a microservice?

Suppose we have two microservices, Customers and Orders, with no dependencies between them, i.e. they don't call each other, they don't know each other. Every order, though, has a reference to a customer by means of a customer id. In other words one customer may have zero or more orders, and one order belongs to exactly one customer.
For the sake of the example, it's totally fine to delete a customer unless there are orders belonging to that customer. What is the most appropriate way to implement a constraint on Customers that prevents a customer from being deleted if one or more orders have a reference to that customer? Analogous to referential integrity in a relational database.
These are the approaches I can think of:
Let Customers ask Orders if a given customer has any orders, e.g. via API call.
Let Customers keep track of which orders are assigned to every customer, e.g. by having each customer record maintain a list of order ids.
Merge Customers and Orders into a single microservice and manage it internally.
I'm not sure which approach is the best for the given example in a microservices context. I can see pros and cons in all three approaches but I won't list them here because I'd like to hear other people's thoughts on the problem, including approaches not listed above. Thanks.
Probably the second approach would help if you're going to decouple through events, either tracking a list of ids or a counter just telling how many orders are stored for such a Customer.
On the Order microservice you will emit an event when there is a creation/deletion that will be captured by the Customer (or any other microservice interested) who will take care of updating the list of order ids (or increment/reduce the counter).
If customer order counter is 0 then you may delete the customer.
Let's start with your third approach: This will not work in a Microservice world, because you will always have those constraints between some Services. And if you want to solve all of them this way, you'll end up with a Monolith - and that's the end of your Microservice story.
The first and second approach have both the same "problem": These are async operations, that may return false positive (or false negative) results: It's possible to make api requests for delete customer and create order (or delete order) at the same time.
Though this can happen:
For your first approach: Customer Service asks Order Service if there are any Orders for this Customer. Order Service returns 0. And at the same time Order Service creates a new Order for that Customer in another thread. So you end up with a deleted Customer and still created an Order.
The same applies for your second approach: The messaging between those services is async. Though it's possible that Customer Service knows of 0 Orders, and permits the delete. But at the same time the Order Service creates a new Order for this Customer. And the OrderCreated message will hit the Customer Service after the Customer has already been deleted.
If you try to do it the other way around, you'll end up with the same situation: Your Order Service could listen to CustomerDeleted messages, and then disallow creating new Orders for this Customer. But again: This message can arrive while there are still Orders in the database for this Customer.
Of course this is very unlikely to happen, but it still is possible and you cannot prevent it in an async Microservice world without transactions (which of course you want to avoid).
You should better ask yourself: How should the system handle Orders where the corresponding Customer has been deleted?
The answer to this question is most likely dependent on your business rules. For example: If the Order Service receives a CustomerDeleted message, it may be okay to simply delete all Orders from this Customer. Or maybe the behavior depends on the Order's state property: It's okay to delete all Orders with state = draft, but every other Order from this Customer should still be processed and shipped as usual.

How rename description of Stripe subscription invoice line item from "trial period"

I have a plan for which I generate subscriptions with trial periods.
Stripe is generating invoices with a line item of 0$ for this trial period, with Trial period for <my_plan_name> as description. I would like to rename this description because my customers are French (and you know, we French people don't speak very well English but that's another story).
When I'm trying to update the item description
Stripe::InvoiceItem.update("sli_xyz",
{ description: "Essai..." })
I'm getting error Stripe::InvalidRequestError: When passing an invoice's line item id, you may only update tax_rates.
I can't delete such a line item neither because this is a subscription item and I can't remove the description neither.
What am I missing here ? Is there a way to resolve that ?
There are two Stripe concepts here: an Invoice and InvoiceItem.
InvoiceItems are essentially line items of individual items/services that is being tendered. An Invoice can contain many InvoiceItems. Imagine an Invoice is the complete receipt, and InvoiceItem is the individual grocery item.
Normally, you can update the InvoiceItem either before you attach to an Invoice, or even after you attach it, before the Invoice is finalized/closed (a.k.a. paid for by the customer).
Stripe does not allow you to update the description of InvoiceItems that are closed/finalized, because as a merchant, invoice is a record of what you sold (and receipting) to the customer, and once the invoice paid by the customer, you cannot change it.
Imagine if your invoice was originally for 50 beers, but after they pay for it, you decide to update the invoice to say 5 beers! How is that fair to your customers?
Your only solution is to make sure the description is defined properly in French for your French customers before issuing the invoice going forward into the future.
There is no way to fix this for past InvoiceItems.

Need help in choosing right caching strategy

We car planning to store prices data to Memcache. prices are subject to car variant and location(city). This is how it is stored in the database.
variant, city, price
21, 48, 40000
Now the confusion is that how do we store this data into Memcache.
Possibility 1 : We store each price in separate cache object and do a multiget if the price of all variant belongs to a model need to be displayed on a single page.
Possibility 2 : We store prices at the model, city level. Prices of all variants of a model will be stored in a single object. This object will be slightly heavy but multiget wouldn't be required.
Need your help in taking the right decision.
TLDR: It all depends on how you want to expose the feature to your end users, and what the query pattern looks like.
For example:
If your flow is that a user can see all the variant prices on a detail page for a city, then you could use <city_id>_<car_model_id> as the key, and store all data for variants against that key (Possibility 2).
If the flow is that a user can see prices of all variants across cities on a single page, then you would need the key as <car_model_id> and store all data as Json against this key
If the flow is that a user can see prices of one variant at a time only for every city, then you would use the key <city_id>_<car_variant_id> and store prices.
One thing to definitely keep in mind is the frequency with which you may have to refresh the cache/ perform upserts, which in the case of cars should be infrequent (who changes the prices of a car every day/second). So, I would have gone with option 1 above (Possibility 2 as described by you).

Data Structure Question

What's the best way to handle this scenario?
I have a customer Model(Table) contains contact info for customers
I have Prospect Model(Table) contains contact info for store visitors that aren't customers
I have an Opportunity Model (Table) when either a customer or Prospect visits the store.
In my view I want to generate a new oppportunity. An opportunity can only contain either 1 customer association or 1 prospect association but not both.
In my opportunity model I currently have both the customer and prospect as nullable foreign Id's and and navigation properties. I also have an ICollection<> for Customers and Prospects on the opportunity model.
Is this the right way to do handle a conditional association?
When it comes to the view, I'm stuck on how would I make the customer or prospect association?
I am a computer science student, and this is my understanding on DB relationships:
Since you have two types of "People" - Customer - and Prospect - you could potentially have a table called "Person". In the Person table any common data among both entities would be stored (FirstName, LastName, Address1, Address2, City, State, Zip, etc...).
To indicate that a Person is a Prospect, you would have a Prospect table, which would have a PersonId to link to the person table. You can store more specific attributes about a prospect in this table.
The same would go for a Customer - you would have a Customer table - that would have a PersonId column to link to the Person table, and any specific attributes for the Customer entity.
Now you have a database in which you can derive other entities ... say an Employee entity > you have your base Person Entity to start from. And your Employee table would link back to it, and also have other custom columns for employee specific data.
Does that make sense?
Or maybe I'm going about this all wrong :). Please correct me if I am wrong as I am still a student.
I think you are stuck because you now have two fields on an "Opportunity" record (Customer OR Prospect), one of which MUST be null. With the model I proposed, your Opportunity would link to a Person, in which you can define custom business rules restricting say... an Employee Opportunity (which actually might not be a bad idea).
For the referenced Person in your Opportunity model, it would not be an ICollection (since you specifically said that an opportunity can have ONLY one person). It would simply be a single class such as:
private virtual Person Person { get; set; }
EDIT: If you don't want to restructure your entire database, you could just have a dropdown that asks what type of Opportunity this is (Customer, or Prospect). Based on the selection, you would add a foreign key in your Opportunity table to link to your [Customer or Prospect].

Resources