def update(self, instance, validated_data): rows_data = validated_data.pop('rows') sales = Sales.objects.get(pk=instance.id) sales.date = validated_data.pop('date',None) sales.credit = validated_data.pop('credit',False) sales.voucher_no = validated_data.pop('voucher_no') sales.party = validated_data.pop('party') sales.save() for row_data in rows_data: data = dict(row_data) row_id = data.get('id', '') if row_id: row = SalesRow.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 = SalesRow() 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.sales = sales 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.sales.date, ['cr', 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.sales.date, ['cr', row.item.ledger, row_amount]) if sales.credit: set_ledger_transactions(row, row.sales.date, ['dr', sales.party.account, row_amount]) else: set_ledger_transactions(row, row.sales.date, ['dr', Account.objects.get_or_create(name='Cash')[0], row_amount]) return sales
def create(self, validated_data): rows_data = validated_data.pop('rows') sales = Sales.objects.create(**validated_data) for row_data in rows_data: data = dict(row_data) row = SalesRow() 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.sales = sales 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.sales.date, ['cr', 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.sales.date, ['cr', row.item.ledger, row_amount]) if sales.credit: set_ledger_transactions(row, row.sales.date, ['dr', sales.party.account, row_amount]) else: set_ledger_transactions(row, row.sales.date, ['dr', Account.objects.get_or_create(name='Cash')[0], row_amount]) return sales