displaying balance in core data - cocoa

I have 3 entities in an example app:
Account
name
balance (NSDecimalNumber overall balance of account)
balances (to-many relationship with Balances)
transactions (many-to-many relationship with Transaction.accounts)
Transaction
payee
amount
accounts (many-to-many relationship with Account.transactions)
balances (many-to-one relationship with Balances.transaction)
Balance
amount
account (one-to-many relationship with Account.balances)
transaction (one-to-many relationship with Transaction.balances)
I want to display the transactions of a particular account in an NSTableView. The problem comes in when I want to display the balance of the current account. Because a transaction may be associated with multiple accounts, it may have multiple balances. I can't think of a way to select the particular balance associated with the current account being displayed in an NSTableColumn. Does anyone have any suggestions of how to change the model or how to connect things up to the NSTableView in such a way that the proper balance is displayed?
The only thing I can think of is to create a sub-class of NSCell that knows how to select the balance associated with the current account view or an NSValueTransformer that does a similar thing. This seems like a very inelegant solution though. If there is a better way I would like to know how.
Update
Here's a screenshot of a demo app that illustrates the above. I would like the far right column to display the balance as of that specific transaction. Works well enough if the transaction has a balance attribute; however, in my app the balance is stored in a separate table because there may be multiple types of balances for each transaction. I can't figure out how to get it to display the proper balance in the table view though:

Have you considered populating the table via this NSTableViewDataSource method:
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
I know, it looks ugly, but you can do somersaults with it.

Bind the table column's value binding to your array controller of Accounts with the controller key selection and the model key path balance.

Related

How display model with four relationships based on date in 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.

How do I create a UI for a Many-to-Many relationship?

Can anyone advise on the best way to create a UI for establishing Many-to-Many relationships ?
Lets use an example from a previous question List <<-->> Patient.
I can create two separate Table Views, one for Lists and another for Patients which will allow the user to create Lists and Patients using two separately created Array Controllers (Lists and Patients), one linked to the List entity and other linked to the Patient entity.
Now I would like to be able to add/remove Patients from a List by creating an Table View that shows only Patients in the selected List. To do this I create a Array Controller (ListPatients) linked to the entity Patient with a Content Set binding to Lists.selection.patients.
Now things stop working nicely from here on....
If I bind a button to the ListPatients.add method then a NEW Patient gets created - there seems to be no way to simply add a NEW relationship between an existing Patient and the List.
Ideally what I would like to be able to do is to have a drop down list to select the Patient from.
Does anyone have any suggestions as to how best to do this without needing to create a new entity to represent this relationship.
What you need is a swapping Master-Detail view where either the List table or the Patient table is the Master or the Detail at any particular time.
When the List is the Master view, selecting a row will cause the Detail view to display all the Patient objects in the rowList.patients relationship. When the Patient is the Master view, selecting a row will cause the Detail view to display all the List objects in the rowPatient.lists relationship.
You really do want to break up the UI so the users always have a clear idea what the relationship is between the two tables. I would recommend a set of four tables, two each for each Master-Detail setup. That way, the user will also understand what they are looking at.
That will also make it easy to add new objects. Just put an add new button under the detail table and the user will understand that clicking it will add a new object to the relationship of the object selected in the Master view.

CoreData SQLite - Data looks deleted but in fact, it is not

The app allows to manage a list of Suppliers and a list of Products available from those Suppliers. Data model contains 2 entities: Supplier and Product.
Supplier has a 1-to-many relation to Product, with delete rule set to Cascade (as I don't want to keep records of a supplier's products if it gets put out of business). Product has default relationship settings.
In IB, I have 2 array controllers. One points to the Supplier entity, with Parameters: MOC bound to AppDelegate and MKP=managedObjectContext. The other points to the Product entity, with Parameters: MOC bound to AppDelegate and MKP=managedObjectContext, and with Controller Content: Content Set bound to Suppliers array controller, CK=selection and MKP=name_of_relation.
On the interface I have 2 NSTableViews for Suppliers and Products, and buttons to Add/Remove from the tables. When I select a Supplier, only its specific Products are displayed on the Products table. When I delete a product, the product disappears from the table. It works as intended.
I thought it was working fine, until the day where I was curious to see how CoreData actually manages the data fields and tables inside the SQLite database. So I opened it using the SQLiteManager add-on in Firefox and... horror! I see that the Products are not deleted. The data are still there! Only the reference to the Supplier was deleted and that's why it doesn't appear anymore in the table, leading me to think that it had been correctly deleted.
What did I do wrong? Is it something with the Content Set bindings?
Thanks for advice.
I just ran a test on this and the result was that the relationship entities where completely deleted from the sqlite database. I think you need to check your cascading settings again exactly in IB. Did you make sure you have a save: statement?

Cocoa bindings: get old value upon change

I am writing a core data Cocoa application in which there are accounts and transactions (monetary). The account entity description contains a balance attribute. The transaction entity description has a relationship to an account.
I need the application to update account balances when transactions have their accounts set or changed. For example, if a transaction's account is changed from checking to credit, the balances of both checking and credit should be changed to reflect this.
The problem I am having is that I am unsure how to determine the transaction's old account so I can update its balance. I am using bindings.
Can anyone point me in the right direction?
I assume that the account entity has the inverse relationship to the transactions. (Apple strongly suggests you always have inverse relationships. So if you haven't, please set it up!)
Let's say you have a subclass Account of NSManagedObject for the account entity, and Transaction for the transaction entity.
Call the inverse relationship to transactions as transactions.
Then, when you change the account for the transactions, the inverse relationship is automatically updated by CoreData. So, all you have to do is to write a self-observation routine for transactions in Account so that the Account objects keep track of the balance themselves. I think it is more object-oriented-y to make Account objects to take care of themselves than changing the balance from the side of the Transaction object... although of course it depends on your taste.
To perform the observation, you use KVO. Basically, you register the KVO by addObserver:forKeyPath:options:context: with a suitable set of options. Then, you get the change by implementing observeValueForKeyPath:ofObject:change:context:. The changes can be found in the dictionary passed to that method.

Creating a Core Data Inverse Relationship

I'm trying to write my first Cocoa app, and Core Data is making it difficult to model my data objects. I have two entities that I want to manipulate: Account and Transaction. Account has a human-readable name and that's about it. Transaction stores a monetary value, and references to two accounts called debitAccount and creditAccount (I'm working on a double-book accounting app).
I want to be able to find all the Transactions for a given Account, whether they use debitAccount or creditAccount. How can I do this? Is there a way to do it that will work easily with Cocoa UI binding?
If I understand you correctly, you want Transaction to be related to Account via two relationships: debitAccount and creditAccount, yes? And you're wondering about creating inverse relationships.
In short, a relationship can only be the inverse of one other relationship. So you won't be able to create a relationship called, say, transactions that is the inverse of both debitAccount and creditAccount. Instead, you'll have to create two relationships, like debitTransactions and creditTransactions (I'm sure you'll think of more appropriate names for these...)
Now, since relationships are modeled as sets (specifically, NSSets), you can union the creditTransactions and debitTransactions relationships for a particular Account to get all transactions that account is involved with.
A (possibly better) alternative would be to introduce an intermediate entity, with a name like TransactionAccount that has a to-one relationship to both Account and Transaction as well as an attribute, like accountRole that identifies the account as being the debit or credit account relative to that particular transaction. You'd create inverse to-many relationships on both Transaction and Account with a name like transactionAccounts. That way, you could write something like this:
[account valueForKeyPath:#"transactionAccounts.transaction"]
to get all the transactions for a particular account. You could use an NSPredicate to filter the set to only transactions where the account was the debit/credit account.
The solution that worked best (and made the most sense) to me was to create a subclass of NSManagedObject to use as my Account entity:
#interface Account : NSManagedObject {
}
-(NSArray*)getTransactions;
#end
I have the actual lookup logic inside -getTransactions, which wasn't too hard to do by hand. This is working out well so far, especially because the method name conforms to KVO.
To make Core Data return Accounts instead of NSManagedObjects, I had to change the entity's "Class" property in Xcode's data modeling tool.

Resources