Subversion Repository Public Repository

TransPort_Tracking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bestray.transtracking.dao.impl;

import com.bestray.transtracking.dao.PaymentVoucherDao;
import com.bestray.transtracking.dao.mapper.EmployeeResultSetExtractor;
import com.bestray.transtracking.dao.mapper.PartyResultSetExtractor;
import com.bestray.transtracking.dao.mapper.PaymentVoucherResultSetExtractor;
import com.bestray.transtracking.dao.mapper.ViewResultSetExtractor;
import com.bestray.transtracking.dto.PaymentVoucherSearchDTO;
import com.bestray.trastrack.domain.Employee;
import com.bestray.trastrack.domain.Party;
import com.bestray.trastrack.domain.PaymentVoucher;
import com.bestray.trastrack.domain.ViewDomain;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;

/**
 *
 * @author Bitu
 */
public class PaymentVoucherDaoImpl implements PaymentVoucherDao{
    
    private DataSource ds;
    private static final String PARTY_QUERY ="SELECT * from party_master ";
    private static final String PAYMENT_QUERY ="SELECT * from payment_voucher ";
    private static final String PAYMENT_WHERE =" where party_name_id = ? ";
    private static final String ORDER_BY = " order by id desc ";
    private static final String EMPLOYEE_QUERY ="SELECT * from employee_master e";
    private static final String PARTY_WHERE =" where party_name_id = ? ";
    private static final String OIL_TOTAL =" SELECT sum(total_price) as sum, party_id as party from oil_entry ";
    private static final String OIL_PARTY_WHERE =" where party_id = ? ";
     private static final String PAYMENT_TOTAL =" SELECT sum(amount_paid) as sum, party_name_id as party from payment_voucher ";
    private static final String PAYMENT_PARTY_WHERE =" where party_name_id = ? ";
    private static final String EXPENSE_TOTAL =" SELECT sum(expense_amount) as sumexpense, paid_to from vehicle_expenses_entry ";
    private static final String EXPENSE_PARTY_WHERE =" where paid_to = ? ";
    private JdbcTemplate jdbcTemplate;
    
    public PaymentVoucherDaoImpl(DataSource ds){
        this.ds=ds;
    }
    
    /*public List<Party> findallReceipt() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
        StringBuffer queryString = new StringBuffer(PARTY_QUERY).append(ORDER_BY);
        List<Party> partyList = (List<Party>) jdbcTemplate.query(queryString.toString(), new ReceiptVoucherResultSetExtractor());
        return partyList;
    }

    public PaymentVoucher getTotalPartyBalance(Long partyId) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
        StringBuffer queryString = new StringBuffer(PAYMENT_QUERY).append(PAYMENT_WHERE).append(ORDER_BY).append("limit 1");
        List<PaymentVoucher> paymentList = (List<PaymentVoucher>) jdbcTemplate.query(queryString.toString(),new Object[] { partyId },new ReceiptVoucherResultSetExtractor());
        return paymentList.size()>0?paymentList.get(0):null;
    }*/
    public List<Employee> findAllEmployees() throws Exception{
        JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
        StringBuffer queryString = new StringBuffer(EMPLOYEE_QUERY).append(" where e.lock='NO' ").append(" order by emp_name asc ");;
        List<Employee> employeeList = (List<Employee>) jdbcTemplate.query(queryString.toString(), new EmployeeResultSetExtractor());
        return employeeList;
    }

    public void savePayment(PaymentVoucher payment)throws Exception {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
        String sql = "insert into payment_voucher (payment_date, party_name_id, employee_name_id, amount_paid, payment_mode, diposited_at, cheque_number, voucher_details) values(?,?,?,?,?,?,?,?)";      
        jdbcTemplate.update(sql, payment.getPayment_date(), payment.getParty_name_id(), payment.getEmployee_name_id(), payment.getAmount_paid(), payment.getPayment_mode(), payment.getDiposited_at(), payment.getCheque_number(), payment.getVoucher_details());

    }

    public List<PaymentVoucher> getSearchResults(final PaymentVoucherSearchDTO search) throws Exception{
        StringBuffer queryString = new StringBuffer(PAYMENT_QUERY);
        queryString.append(" where party_name_id = ?");
        if(search.getSearchFrom()!=null){
            queryString.append(" and payment_date>=?");
        }
        if(search.getSearchTo()!=null){
            queryString.append(" and payment_date<=?");
        }
        queryString.append(ORDER_BY);
         
          JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
                   
            //System.out.println(queryString.toString());   
         return (List<PaymentVoucher>)jdbcTemplate.query(queryString.toString(),  new PreparedStatementSetter() {

            public void setValues(PreparedStatement ps) throws SQLException {
                int index =0;
                ps.setLong(++index, search.getPartyname());
                if(search.getSearchFrom() != null){
                    //System.out.println(search.getSearchFrom());
                    ps.setDate(++index, new java.sql.Date(search.getSearchFrom().getTime()));
                }
                 if(search.getSearchTo() != null){
                     //System.out.println(search.getSearchTo());
                  ps.setDate(++index, new java.sql.Date(search.getSearchTo().getTime()));
                 }
            }
        } , new PaymentVoucherResultSetExtractor());
    }

    public PaymentVoucher getLastBalance(Long partyId)throws Exception {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
        StringBuffer queryString = new StringBuffer(PAYMENT_QUERY).append(PAYMENT_WHERE).append(ORDER_BY).append("limit 1");
        List<PaymentVoucher> balanceChangeList = (List<PaymentVoucher>) jdbcTemplate.query(queryString.toString(), new Object[]{partyId}, new PaymentVoucherResultSetExtractor());
        return balanceChangeList.size() > 0 ? balanceChangeList.get(0) : null;
    }

    public ViewDomain getTotalOilBalance(Long partyId) throws Exception{
        JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
        //StringBuffer queryString = new StringBuffer(OIL_TOTAL).append(OIL_PARTY_WHERE);
        String sql = "select sum(total_price) as sum , party_id as party from oil_entry where party_id=? ";
        List<ViewDomain> totalOilbalance = (List<ViewDomain>) jdbcTemplate.query(sql.toString(), new Object[]{partyId}, new ViewResultSetExtractor());
        return totalOilbalance.size() > 0 ? totalOilbalance.get(0) : null;
    }

    public ViewDomain getTotalPaymentBalance(Long partyId) throws Exception{
       JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
        //StringBuffer queryString = new StringBuffer(OIL_TOTAL).append(OIL_PARTY_WHERE);
        String sql = "select sum(amount_paid) as sum , party_name_id as party from payment_voucher where party_name_id=? ";
        List<ViewDomain> totalpaymentbalance = (List<ViewDomain>) jdbcTemplate.query(sql.toString(), new Object[]{partyId}, new ViewResultSetExtractor());
        return totalpaymentbalance.size() > 0 ? totalpaymentbalance.get(0) : null;
    }

    public ViewDomain getTotalExpenseBalance(Long partyId)throws Exception {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
        String sql = "select sum(expense_amount) as sum , party_id as party from vehicle_expense_entry where party_id=? ";
        List<ViewDomain> totalpaymentbalance = (List<ViewDomain>) jdbcTemplate.query(sql.toString(), new Object[]{partyId}, new ViewResultSetExtractor());
        return totalpaymentbalance.size() > 0 ? totalpaymentbalance.get(0) : null;
    }

    public PaymentVoucher findById(Long id) throws Exception{
        JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
        String sql = "SELECT * FROM payment_voucher WHERE id = ?";
                PaymentVoucher oilChange = (PaymentVoucher) jdbcTemplate.queryForObject(
                sql, new Object[]{id},
                new BeanPropertyRowMapper(PaymentVoucher.class));
        return oilChange;
    }

    public void delete(Long id)throws Exception {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
        jdbcTemplate.update("DELETE FROM payment_voucher WHERE id = ?", new Object[]{id});
    }

    public void updatePayment(PaymentVoucher payment)throws Exception {
       JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
       final java.util.Date date= new java.util.Date();
       String sql = "update payment_voucher set payment_date=?, party_name_id=?, employee_name_id=?, amount_paid=?, payment_mode=?, diposited_at=?, cheque_number=?, voucher_details=?  where id = ?";      
       jdbcTemplate.update(sql, new Object[]{payment.getPayment_date(), payment.getParty_name_id(),  payment.getEmployee_name_id(), payment.getAmount_paid(), payment.getPayment_mode(), payment.getDiposited_at(), payment.getCheque_number(), payment.getVoucher_details(),payment.getId()});
    }

    public List<Party> findAllPartysNames() throws Exception {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
        StringBuffer queryString = new StringBuffer(PARTY_QUERY).append(" order by name asc ");;
        List<Party> partyList = (List<Party>) jdbcTemplate.query(queryString.toString(), new PartyResultSetExtractor());
        return partyList;
    }

    public ViewDomain getTotalPayment(Long partyId) throws Exception {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
        String queryString = " SELECT party_master.`id` AS id, party_master.`name` AS party_name,"
                +" (select COALESCE(sum(oil_entry.`total_price`),0) "
                + "from oil_entry where oil_entry.`party_id`=party_master.`id`) AS sum_oil,"
                + "(select COALESCE(sum(vehicle_expense_entry.`expense_amount`),0)"
                + " from vehicle_expense_entry where vehicle_expense_entry.`party_id`=party_master.`id`) AS sum_expense,"
                + "(select COALESCE(sum(payment_voucher.`amount_paid`),0) "
                + "from payment_voucher where payment_voucher.`party_name_id`=party_master.`id`) AS sum_payment FROM "
                + "`party_master` party_master Where party_master.`id`= ? ";
        List<ViewDomain> domainList = (List<ViewDomain>) jdbcTemplate.query(queryString.toString(), new Object[]{partyId}, new DomainResultSetExtractor());
        return domainList.size() > 0 ? domainList.get(0) : null;
    }
    public class DomainResultSetExtractor implements  ResultSetExtractor<Object>{

    public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
        List <ViewDomain> payment = new ArrayList<ViewDomain>();
        while (rs.next()) {
            ViewDomain paymentparty = new ViewDomain();
          //  Address address = new Address();
            paymentparty.setId(rs.getLong("id"));
            paymentparty.setParty_name(rs.getString("party_name"));
            paymentparty.setSum_oil(rs.getDouble("sum_oil"));
            paymentparty.setSum_expense(rs.getDouble("sum_expense"));
            paymentparty.setSum_payment(rs.getDouble("sum_payment"));
            
            payment.add(paymentparty);
        }
        return payment;
    }
    }
    
}

Commits for TransPort_Tracking/TransPortTracking/src/main/java/com/bestray/transtracking/dao/impl/PaymentVoucherDaoImpl.java

Diff revisions: vs.
Revision Author Commited Message
1 girijabapi picture girijabapi Sat 28 Jul, 2018 05:29:14 +0000