Esempio n. 1
0
        public int updateDebtorBalance(Debtor debtor)
        {
            SSGetService getService = new SSGetService();
            SSAddService addService = new SSAddService();

            Debtor existingDebtor = getService.getDebtorByIdEvenIfZero(debtor.cid);

            if (existingDebtor.name != null)
            {
                using (SqlCommand command = new SqlCommand("UPDATE ss_debtors SET amount=@balance,last_modified_date=getdate() WHERE cid=" + existingDebtor.cid + ""))
                {
                    command.Parameters.AddWithValue("@balance", debtor.amount);
                    int response = service.execute(command);
                    if (response > 0)
                    {
                        addService.addLog(new Log()
                        {
                            type        = "Updated customer debit balance to " + debtor.amount,
                            statement   = command.CommandText,
                            description = "Updated [" + existingDebtor.cid + "] Debit Amount",
                        });
                    }
                    return(response);
                }
            }
            else
            {
                return(-404);
            }
        }
Esempio n. 2
0
        public int addDebtor(Customer customer, int amount)
        {
            SSGetService getService = new SSGetService();

            if (customer.id != -1)
            {
                Debtor debtor = getService.getDebtorByIdEvenIfZero(customer.id);
                if (debtor.name == null)
                {
                    //add the debt if not there
                    using (SqlCommand command = new SqlCommand("INSERT INTO ss_debtors(cid,name,amount,last_modified_date)VALUES(@cid,@name,@amount,getdate())"))
                    {
                        command.Parameters.AddWithValue("@cid", customer.id);
                        command.Parameters.AddWithValue("@name", customer.name);
                        command.Parameters.AddWithValue("@amount", amount);
                        int response = service.execute(command);
                        return(response);
                    }
                }
                else
                {
                    //increase the debit if already there
                    amount = debtor.amount + amount;
                    using (SqlCommand command = new SqlCommand("UPDATE ss_debtors SET amount=@amount,last_modified_date=getdate() WHERE cid=" + debtor.cid))
                    {
                        command.Parameters.AddWithValue("@amount", amount);
                        int response = service.execute(command);
                        return(response);
                    }
                }
            }
            else
            {
                return(-404);
            }
        }
Esempio n. 3
0
        public int addPayment(Payment payment)
        {
            SSGetService    getService    = new SSGetService();
            SSUpdateService updateService = new SSUpdateService();

            User     user     = app.getSession();
            Customer customer = getService.getCustomerByName(payment.customer_name);

            if (customer.name != null)
            {
                if (payment.transaction_type == "Payment")
                {
                    Debtor debtor = getService.getDebtorById(customer.id);
                    if (debtor.name != null)
                    {
                        int clearedAmount;
                        int outstanding;

                        clearedAmount = outstanding = 0;

                        //perform the caculation
                        if (payment.transaction_amount >= debtor.amount)
                        {
                            clearedAmount = (debtor.amount);
                            outstanding   = (debtor.amount - payment.transaction_amount);
                        }
                        else
                        {
                            clearedAmount = payment.transaction_amount;
                            outstanding   = (debtor.amount - payment.transaction_amount);
                        }


                        //add payment entry
                        using (SqlCommand command = new SqlCommand("INSERT INTO ss_payments(cid,user_id,customer_name,transaction_amount,cleared_amount,amount_in_words,transaction_type,payment_mode,description)VALUES(@cid,@user_id,@customer_name,@transaction_amount,@cleared_amount,@amount_in_words,@transaction_type,@payment_mode,@description)"))
                        {
                            command.Parameters.AddWithValue("@cid", customer.id);
                            command.Parameters.AddWithValue("@user_id", user.id);
                            command.Parameters.AddWithValue("@customer_name", customer.name);
                            command.Parameters.AddWithValue("@transaction_amount", payment.transaction_amount);
                            command.Parameters.AddWithValue("@cleared_amount", clearedAmount);
                            command.Parameters.AddWithValue("@amount_in_words", app.toWordsOf(payment.transaction_amount));
                            command.Parameters.AddWithValue("@transaction_type", payment.transaction_type);
                            command.Parameters.AddWithValue("@payment_mode", payment.payment_mode);
                            command.Parameters.AddWithValue("@description", payment.description);

                            if (service.execute(command) > 0)
                            {
                                if (outstanding < 0)
                                {
                                    //update customer balance if any
                                    if (customer.account_balance > 0 || customer.account_balance < 0)
                                    {
                                        customer.account_balance += Math.Abs(outstanding);
                                    }
                                    else
                                    {
                                        customer.account_balance = Math.Abs(outstanding);
                                    }

                                    updateService.updateCustomerBalance(customer);
                                }

                                //update debtor balance
                                if (clearedAmount >= debtor.amount)
                                {
                                    debtor.amount = 0;
                                }
                                else
                                {
                                    debtor.amount = outstanding;
                                }


                                DataTable data = service.get("select * from ss_account");
                                if (data.Rows.Count > 0)
                                {
                                    DataRow row            = data.Rows[0];
                                    int     balance        = row.Field <int>("balance");
                                    int     previusBalance = balance;

                                    balance = balance + payment.transaction_amount;
                                    using (SqlCommand updateCommand = new SqlCommand("update ss_account set balance=@balance, previous_balance=@previous_balance"))
                                    {
                                        updateCommand.Parameters.AddWithValue("@balance", balance);
                                        updateCommand.Parameters.AddWithValue("@previous_balance", previusBalance);

                                        if (service.execute(updateCommand) > 0)
                                        {
                                            return(updateService.updateDebtorBalance(debtor));
                                        }
                                        else
                                        {
                                            return(-1);
                                        }
                                    }
                                }
                                else
                                {
                                    return(-1);
                                }
                            }

                            else
                            {
                                return(-1);
                            }
                        }
                    }
                    else
                    {
                        if (payment.description == "Purchasing Payment")
                        {
                            using (SqlCommand command = new SqlCommand("INSERT INTO ss_payments(cid,user_id,customer_name,transaction_amount,cleared_amount,amount_in_words,transaction_type,payment_mode,description)VALUES(@cid,@user_id,@customer_name,@transaction_amount,@cleared_amount,@amount_in_words,@transaction_type,@payment_mode,@description)"))
                            {
                                command.Parameters.AddWithValue("@cid", customer.id);
                                command.Parameters.AddWithValue("@user_id", user.id);
                                command.Parameters.AddWithValue("@customer_name", customer.name);
                                command.Parameters.AddWithValue("@transaction_amount", payment.transaction_amount);
                                command.Parameters.AddWithValue("@cleared_amount", payment.cleared_amount);
                                command.Parameters.AddWithValue("@amount_in_words", app.toWordsOf(payment.transaction_amount));
                                command.Parameters.AddWithValue("@transaction_type", payment.transaction_type);
                                command.Parameters.AddWithValue("@payment_mode", payment.payment_mode);
                                command.Parameters.AddWithValue("@description", payment.description);

                                if (service.execute(command) > 0)
                                {
                                    DataTable data = service.get("select * from ss_account");
                                    if (data.Rows.Count > 0)
                                    {
                                        DataRow row            = data.Rows[0];
                                        int     balance        = row.Field <int>("balance");
                                        int     previusBalance = balance;

                                        balance = balance + payment.transaction_amount;
                                        using (SqlCommand updateCommand = new SqlCommand("update ss_account set balance=@balance, previous_balance=@previous_balance"))
                                        {
                                            updateCommand.Parameters.AddWithValue("@balance", balance);
                                            updateCommand.Parameters.AddWithValue("@previous_balance", previusBalance);

                                            return(service.execute(updateCommand));
                                        }
                                    }
                                    else
                                    {
                                        return(-1);
                                    }
                                }
                                else
                                {
                                    return(-1);
                                }
                            }
                        }
                        else
                        {
                            return(-202);
                        }
                    }
                }
                else
                {
                    int clearedAmount = 0;
                    int outstanding   = 0;
                    if (payment.payment_mode == "Credit")
                    {
                        Debtor debtor = getService.getDebtorByIdEvenIfZero(customer.id);
                        if (debtor.name != null)
                        {
                            debtor.amount += payment.transaction_amount;
                            int res = updateService.updateDebtorBalance(debtor);
                        }
                        else
                        {
                            this.addDebtor(customer, payment.transaction_amount);
                        }
                    }
                    else
                    {
                        Debtor debtor = getService.getDebtorByIdEvenIfZero(customer.id);
                        if (debtor.name != null)
                        {
                            if (debtor.amount >= payment.transaction_amount)
                            {
                                debtor.amount = (debtor.amount - payment.transaction_amount);
                                clearedAmount = payment.transaction_amount;
                                updateService.updateDebtorBalance(debtor);
                            }
                            else
                            {
                                clearedAmount = debtor.amount;
                                outstanding   = (payment.transaction_amount - debtor.amount);
                                debtor.amount = 0;
                                updateService.updateDebtorBalance(debtor);

                                if (outstanding > 0)
                                {
                                    customer.account_balance += outstanding;
                                    updateService.updateCustomerBalance(customer);
                                }
                            }
                        }
                        else
                        {
                            customer.account_balance += payment.transaction_amount;
                            updateService.updateCustomerBalance(customer);
                        }
                    }

                    using (SqlCommand command = new SqlCommand("INSERT INTO ss_adjustments(cid,customer_name,transaction_amount,cleared_amount,transaction_type,payment_mode,description)VALUES(@cid,@customer_name,@transaction_amount,@cleared_amount,@transaction_type,@payment_mode,@description)"))
                    {
                        command.Parameters.AddWithValue("@cid", customer.id);
                        command.Parameters.AddWithValue("@customer_name", customer.name);
                        command.Parameters.AddWithValue("@transaction_amount", payment.transaction_amount);
                        command.Parameters.AddWithValue("@cleared_amount", clearedAmount);
                        command.Parameters.AddWithValue("@transaction_type", payment.transaction_type);
                        command.Parameters.AddWithValue("@payment_mode", payment.payment_mode);
                        command.Parameters.AddWithValue("@description", payment.description);


                        DataTable data = service.get("select * from ss_account");
                        if (data.Rows.Count > 0)
                        {
                            DataRow row            = data.Rows[0];
                            int     balance        = row.Field <int>("balance");
                            int     previusBalance = balance;

                            if (payment.payment_mode == "Credit")
                            {
                                balance = balance + payment.transaction_amount;
                            }


                            using (SqlCommand updateCommand = new SqlCommand("update ss_account set balance=@balance, previous_balance=@previous_balance, last_modified_date=getdate()"))
                            {
                                updateCommand.Parameters.AddWithValue("@balance", balance);
                                updateCommand.Parameters.AddWithValue("@previous_balance", previusBalance);

                                if (service.execute(updateCommand) > 0)
                                {
                                    return(service.execute(command));
                                }
                                else
                                {
                                    return(-1);
                                }
                            }
                        }
                        else
                        {
                            return(-1);
                        }
                    }
                }
            }
            else
            {
                return(-404);
            }
        }