def update(self, instance, validated_data): rows_data = validated_data.pop('rows') purchase = Purchase.objects.get(pk=instance.id) purchase.date = validated_data.pop('date',None) purchase.credit = validated_data.pop('credit',False) purchase.voucher_no = validated_data.pop('voucher_no') purchase.party = validated_data.pop('party') purchase.save() for row_data in rows_data: data = dict(row_data) row_id = data.get('id', '') if row_id: row = PurchaseRow.objects.get(pk=row_id) # if not row.item == data.get('item'): # iv_account, status = InventoryAccount.objects.get_or_create( # name=row.item.name, site_id=DEFAULT_PROJECT_ID, account_no=row.item.account.account_no) # set_transactions(row, row.purchase.date, ['cr', iv_account, row.quantity]) # set_ledger_transactions(row, row.purchase.date, ['cr', row.item.ledger, # row.quantity*row.rate-row.discount]) else: row = PurchaseRow() row.sn = data.get('sn') row.item = data.get('item') row.unit = data.get('unit','Pieces') row.quantity = data.get('quantity',0.0) row.is_vatable = data.get('is_vatable',True) row.rate = data.get('rate', 0.0) row.discount = data.get('discount',0.0) row.purchase = purchase row.save() iv_account, status = InventoryAccount.objects.get_or_create( name=row.item.name, site_id=DEFAULT_PROJECT_ID, account_no=row.item.account.account_no) set_transactions(row, row.purchase.date, ['dr', iv_account, row.quantity]) if not row.is_vatable: row_amount = row.quantity*row.rate-row.discount else: row_amount = row.quantity*row.rate*1.13-row.discount set_ledger_transactions(row, row.purchase.date, ['dr', row.item.ledger, row_amount]) if purchase.credit: set_ledger_transactions(row, row.purchase.date, ['cr', purchase.party.account, row_amount]) else: set_ledger_transactions(row, row.purchase.date, ['cr', Account.objects.get_or_create(name ='Cash')[0], row_amount]) return purchase