public int manageWalletBalance(Payment payment)
        {
            SSGetService getService = new SSGetService();

            Customer customer = getService.getCustomerByName(payment.customer_name);

            if (customer.name != null)
            {
                if (payment.payment_mode == "Credit")
                {
                    customer.account_balance += payment.transaction_amount;
                }
                else if (payment.payment_mode == "Debit")
                {
                    customer.account_balance -= payment.transaction_amount;
                }
                else
                {
                    customer.account_balance = 0;
                }

                return(this.updateCustomerBalance(customer));
            }
            else
            {
                return(-404);
            }
        }
        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);
            }
        }
Exemple #3
0
        private void addbutton_Click(object sender, EventArgs e)
        {
            if (customerNamefield.Text == "Customer Name" || customerNamefield.Text == "")
            {
                app.notifyTo(statusLabel, "Please enter the customer name!", "warning");
            }
            else if (transactionAmountfield.Text == "Transaction Amount" || transactionAmountfield.Text == "")
            {
                app.notifyTo(statusLabel, "Please enter the transaction amount", "warning");
            }
            else if (paymentmodeField.Text == "Payment Mode" || paymentmodeField.Text == "")
            {
                app.notifyTo(statusLabel, "Please select the payment mode", "warning");
            }
            else
            {
                if (customerNamefield.Text != "" && transactionAmountfield.Text != "" && paymentmodeField.Text != "" &&
                    customerNamefield.Text != "Customer Name" && transactionAmountfield.Text != "Transaction Amount" && paymentmodeField.Text != "Payment Mode")
                {
                    int transactionAmount = 0;
                    if (app.isAllDigits(transactionAmountfield.Text))
                    {
                        transactionAmount = int.Parse(transactionAmountfield.Text);

                        Payment payment = new Payment()
                        {
                            customer_name      = customerNamefield.Text,
                            transaction_amount = transactionAmount,
                            payment_mode       = paymentmodeField.Text,
                            description        = paymentDescriptionField.Text,
                            transaction_type   = "Payment",
                        };

                        if (transactionAmount > 0)
                        {
                            addbutton.Enabled = false;
                            int response = addService.addPayment(payment);
                            addbutton.Enabled = true;
                            if (response > 0)
                            {
                                User     user     = app.getSession();
                                Customer customer = getService.getCustomerByName(customerNamefield.Text);
                                Payment  pay      = getService.getLastPayment(user.id, customer.id);
                                Report   report   = new Report(customer.id, pay.id, "#payment");
                                report.ShowDialog();

                                customerNamefield.Text       = "Customer Name";
                                transactionAmountfield.Text  = "Transaction Amount";
                                paymentmodeField.Text        = "Payment Mode";
                                paymentDescriptionField.Text = "Description";
                                refreshCutsomerBalance(customerNamefield.Text);
                            }
                            else
                            {
                                if (response == -404)
                                {
                                    app.notifyTo(statusLabel, "The customer " + customerNamefield.Text + " is  not found", "warning");
                                }
                                else if (response == -202)
                                {
                                    app.showWarning("The customer " + payment.customer_name + " is not Owing!");
                                }
                                else
                                {
                                    app.notifyTo(statusLabel, "Transaction failed, please try latter ", "warning");
                                }
                            }
                        }
                        else
                        {
                            app.notifyTo(statusLabel, "Oops! the Transaction amount can not be Zero ", "warning");
                        }
                    }
                    else
                    {
                        app.notifyTo(statusLabel, "Invalid transaction amount, please type only numbers!", "error");
                    }
                }
            }
        }