/home/mjc1/public_html/emillennium/sale_ledger.inc.day.php


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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
if($stc!=''){
    
$where_cccode0 " and ax.code = '$stc' ";
    
$where_cccode1 " and a.ccode = '$stc' ";
    
$where_cccode2 " and s.ccode = '$stc' ";
}

if(
$stx!=''){
    
$where_prod0 " and b.pcode = '$stx' ";
}

$sql "select '[전기이월]' as pname, ax.code as comp_code, ax.comp_name, ifnull(s_tot_amt,0) as s_tot_amt, ifnull(r_amount,0) as r_amount, ifnull(r_dc,0) as r_dc, ifnull(t_recvbl,0) as t_recvbl, 
            (ifnull(s_tot_amt,0) - ifnull(r_amount,0) - ifnull(r_dc,0) - ifnull(t_recvbl,0)) as prev_balance, '' as pqty, '' as pprice, '' as psup_amt, '' as pvat, '' as ptot_amt, '' as amount
            from
                customers ax
            left join
                (select a.ccode as comp_code, sum(b.tot_amt) as s_tot_amt 
                    from sale_m a left join sale_d b on a.midx = b.midx
                    where 1=1 and a.dealdate < '
{$sdate}{$where_prod0}
                    group by a.ccode) aa
            on ax.code = aa.comp_code
            left join
                (select c.ccode as comp_code, sum(c.amount+c.usepoint) as r_amount, sum(c.dc) as r_dc 
                from receive c 
                where c.dealdate < '
{$sdate}'
                group by c.ccode) bb
                on aa.comp_code = bb.comp_code
            left join
                (select d.code as comp_code, d.comp_name as comp_name, e.begin_recvbl as t_recvbl from customers d left join customer_begin_balance e on d.code = e.ccode where 1=1) cc
                on aa.comp_code = cc.comp_code
            where 1=1 
{$where_cccode0} 
            order by comp_name"
;
$result mysql_query($sql,$connect_e1000y);
while(
$prev_info=mysql_fetch_array($result)){
    
$prev_array[$prev_info['comp_code']] = $prev_info;
}

//_pr($sql);

$sql "
        select 0, a.dealnum as code, a.midx, a.ccode as comp_code, cc.comp_name as comp_name, a.dealdate as ddate, '전체' as pname, '' as pnorm,  
            sum(b.qty) as pqty, 0 as pprice, sum(b.sup_amt) as psup_amt, sum(b.vat) as pvat, sum(b.tot_amt) as ptot_amt, '' as amount, '' as dc, '' as total, a.remarks as bigo
        from sale_m a left join sale_d b on a.midx = b.midx left join customers cc on a.ccode = cc.code 
        where 1=1 and a.dealdate >= '
{$sdate}' and a.dealdate <= '{$edate}{$where_cccode1} {$where_prod0}
        group by a.ccode, a.midx

        union

        select 2, 'sumday' as code, '' as midx, a.ccode as comp_code, cc.comp_name as comp_name, a.dealdate as ddate, '[일계]' as pname, '' as pnorm,  
            sum(b.qty) as pqty, 0 as pprice, sum(b.sup_amt) as psup_amt, sum(b.vat) as pvat, sum(b.tot_amt) as ptot_amt, '' as amount, '' as dc, '' as total, '' as bigo
        from sale_m a left join sale_d b on a.midx = b.midx left join customers cc on a.ccode = cc.code 
        where 1=1 and a.dealdate >= '
{$sdate}' and a.dealdate <= '{$edate}{$where_cccode1} {$where_prod0}
        group by a.ccode, a.dealdate

        union

        select 1, s.nidx as code, '' as midx, s.ccode as comp_code, cc.comp_name as comp_name, s.dealdate as ddate, concat('입금(',if(dealtype=0,'현금','통장'),')') as pname, '' as pnorm,
            '' as pqty, '' as pprice, '' as psup_amt, '' as pvat, '' as ptot_amt, (s.amount+s.usepoint) as amount, s.dc as dc, s.total as total, s.remarks as bigo
        from receive s left join customers cc on s.ccode = cc.code 
        where 1=1 and s.dealdate >= '
{$sdate}' and s.dealdate <= '{$edate}{$where_cccode2}

        union

        select 2.5, 'sumday' as code, '' as midx, s.ccode as comp_code, cc.comp_name as comp_name, s.dealdate as ddate, '[일계]' as pname, '' as pnorm,
            '' as pqty, '' as pprice, '' as psup_amt, '' as pvat, '' as ptot_amt, sum(s.amount+s.usepoint) as amount, sum(s.dc) as dc, sum(s.total) as total, '' as bigo
        from receive s left join customers cc on s.ccode = cc.code 
        where 1=1 and s.dealdate >= '
{$sdate}' and s.dealdate <= '{$edate}{$where_cccode2}
        group by comp_code, ddate

        union

        select 3, code, midx, aa.comp_code, comp_name, '9999-12-31', pname, pnorm, pqty, pprice, psup_amt, pvat, ptot_amt, amount, dc,total , '' as bigo
        from 
            (select 3, a.dealnum as code, a.midx, a.ccode as comp_code, cc.comp_name as comp_name, a.dealdate as ddate, '[업체계]' as pname, '' as pnorm,  
                sum(b.qty) as pqty, 0 as pprice, sum(b.sup_amt) as psup_amt, sum(b.vat) as pvat, sum(b.tot_amt) as ptot_amt
            from sale_m a left join sale_d b on a.midx = b.midx left join customers cc on a.ccode = cc.code
            where 1=1 and a.dealdate >= '
{$sdate}' and a.dealdate <= '{$edate}{$where_cccode1} {$where_prod0}
            group by a.ccode) aa 
            left join 
            (select s.ccode as comp_code, sum(s.amount+s.usepoint) as amount, sum(s.dc) as dc, sum(s.total) as total
            from receive s
            where 1=1 and s.dealdate >= '
{$sdate}' and s.dealdate <= '{$edate}{$where_cccode2}
            group by s.ccode) bb
            on aa.comp_code = bb.comp_code

        union

        select 3, code, midx, bbb.comp_code, comp_name, '9999-12-31', '[업체계]' as pname, pnorm, pqty, pprice, psup_amt, pvat, ptot_amt, amount, dc,total , '' as bigo
        from 
            (select 3, a.dealnum as code, a.midx, a.ccode as comp_code, cc.comp_name as comp_name, a.dealdate as ddate, '[업체계]' as pname, '' as pnorm,  
                sum(b.qty) as pqty, 0 as pprice, sum(b.sup_amt) as psup_amt, sum(b.vat) as pvat, sum(b.tot_amt) as ptot_amt
            from sale_m a left join sale_d b on a.midx = b.midx left join customers cc on a.ccode = cc.code
            where 1=1 and a.dealdate >= '
{$sdate}' and a.dealdate <= '{$edate}{$where_cccode1} {$where_prod0}
            group by a.ccode) aaa 
            right join 
            (select s.ccode as comp_code, sum(s.amount+s.usepoint) as amount, sum(s.dc) as dc, sum(s.total) as total
            from receive s
            where 1=1 and s.dealdate >= '
{$sdate}' and s.dealdate <= '{$edate}{$where_cccode2}
            group by s.ccode) bbb
            on aaa.comp_code = bbb.comp_code

        order by comp_code, ddate, 1;
"
;
//_pr($sql);
$result mysql_query($sql,$connect_e1000y);

$code_tmp 0$code_cnt 0$code_pos 0;
$date_tmp ""$date_cnt 0$date_pos 0;
$cnt 0;
while(
$total_info=mysql_fetch_array($result)){

    if(
$list_array[$cnt-1]['pname']=='[일계]' && $total_info['pname']=='[일계]'){ // 일계가 2개일때는 입금 일계의 rowspan을 한개 뺀다
        
$code_cnt--; $date_cnt--;
    }

    if(
$cnt==0){ // 거래처 rowspan 계산
        
$list_array[] = $prev_array[$total_info['comp_code']]; // 첫번째 전기이월 입력
        
$code_pos =  $cnt;
        
$cnt++; $code_cnt++;
        
        
$code_tmp $total_info['comp_code'];
        
$code_cnt++;
    } else if(
$code_tmp!=$total_info['comp_code']){
    
//    echo $code_tmp." = ".$total_info['comp_code'].$total_info['comp_name']."<br>";

        
$list_array[$code_pos]['comp_cnt'] = $code_cnt// 이전에 계산된 거래처 라인수를 거래처 첫 레코드에 입력함

        //if($total_info['comp_code']=='43'){ _pr($prev_array[$total_info['comp_code']]); }
        
$list_array[] = $prev_array[$total_info['comp_code']]; // 거래처 바뀌면 전기이월 입력
        
$code_pos =  $cnt;
        
$cnt++;

        
$code_tmp $total_info['comp_code'];
        
$code_cnt 2;
    } else {
        
$code_cnt++;
    }

    if(
$cnt==0){ // 날짜 rowspan 계산
        
$date_pos =  $cnt;
        
$date_tmp $total_info['ddate'];
        
$date_cnt++;
    } else if(
$date_tmp!=$total_info['ddate']){
        
$list_array[$date_pos]['date_cnt'] = $date_cnt;

        
$date_pos =  $cnt;
        
$date_tmp $total_info['ddate'];
        
$date_cnt 1;
    } else {
        
$date_cnt++;
    }

    
$total_info['comp_cnt'] = '0'$total_info['date_cnt'] = '0';
    
$list_array[] = $total_info;
    
$cnt++;
}
//echo $code_cnt;
$list_array[$code_pos]['comp_cnt'] = $code_cnt;
$list_array[$date_pos]['date_cnt'] = $date_cnt;

//_pr($list_array);
?>
<style>
    form[name='sale_ledger_list_form'] td {font-size:0.9em; }
    .begin_bal_class {color:#2f6ba9; font-weight:600; background:#FFC; }
    .sumday_class {color:#333; font-weight:600; background:#E0FFE0;}
    .font_color_red { color:red; }
    .tr_hidden { display:none; }
</style>
<!--<div id="contents" style="background:#999"><img src="http://placehold.it/960x150" alt="롤링배너"></div>-->

<form name='sale_ledger_list_form' method='post'>
<input type='hidden' name='sel_del' value=''>
    <div class="div-list-area">
        <div class="cust-list-area">
            <table class="order-sheet2" <?php echo $border_attr;?>>
                <caption class="screen_out">매입처 원장 조회<caption>
<?php if($col_remove==''){ // 프린트시엔 무시처리 ?>
                <colgroup>
                <col style="width:100px;">
                <col style="width:70px;"><col style="width:120px;"><col style="width:120px;"><col style="width:60px;">
                <col style="width:80px;"><col style="width:80px;"><col style="width:70px;">
                <col style="width:100px;"><col style="width:100px;"><col style="width:100px;">
                <col style="width:100px;">
                </colgroup>
<?php ?>
                <thead>
                    <tr>
                        <th scope="col">거래처명</th><th scope="col">거래일자</th><th scope="col">상품명</th>
                        <th scope="col">규격</th><th scope="col">수량</th><th scope="col">단가</th>
                        <th scope="col">공급가액</th><th scope="col">부가세</th><th scope="col">합계액</th>
                        <th scope="col">입금액</th><th scope="col">잔액</th><th scope="col">비고</th>
                    </tr>
                </thead>
                <tbody>

<?php 
    $code_tmp 
0$date_tmp "";
    
$total_prev_val 0;
    
$total_qty 0;
    
$total_sup_amt 0;
    
$total_vat 0;
    
$total_tot_amt 0;
    
$total_amount 0;
    for(
$i=0;$i<count($list_array);$i++){ 
        
$info $list_array[$i];
        
$tr_class=""$td_class=""$midx ""$midx_mode ""$click_edit_class "";

        if(
$list_array[$i-1]['pname']=='[일계]' && $info['pname']=='[일계]'){ continue; } // 이전레코드가 일계(매출)일경우 패스한다. 입금일계만 있을 경우는 뿌려준다.

        
if($i==0){ // 거래처 rowspan
            
$td_do "Y";
            
$code_tmp $info['comp_code'];
        } else if(
$code_tmp != $info['comp_code']){
            
$td_do "Y";
            
$code_tmp $info['comp_code'];
        } else {
            
$td_do "N";
        }

        if(
$i==0){ // 닐짜 rowspan
            
$td_do2 "Y";
            
$date_tmp $info['ddate'];
        } else if(
$date_tmp != $info['ddate']){
            
$td_do2 "Y";
            
$date_tmp $info['ddate'];
        } else {
            
$td_do2 "N";
        }
        if(
substr($info['pname'],0,7)=='입금('){ // 입금시 글자색 레드
            
$tr_class=""$td_class="font_color_red";
        }
        if(
$info['ddate']=='9999-12-31'){ $info['ddate'] = ""; } // 업체계일경우에 소팅위한 거래일자 숨김
        
if($list_array[$i+1]['pname']=='[일계]' && $info['pname']=='[일계]'){ // 다음번이 입금일계일경우 입금액을 가져와 뿌려준다
            
$info['amount'] = $list_array[$i+1]['amount'];
        }

        if(
$info['pname']=='[전기이월]'){ 
            
$td_class "begin_bal_class"
            
$b_bal $info['prev_balance']*1;
            
$total_prev_val += $info['prev_balance']*1;
        } else {
            if(!(
$info['pname']=='[일계]' || $info['pname']=='[업체계]')){
                
$b_bal $b_bal $info['ptot_amt'] -$info['amount'];
                
$total_qty += $info['pqty'];
                
$total_sup_amt += $info['psup_amt'];
                
$total_vat += $info['pvat'];
                
$total_tot_amt += $info['ptot_amt'];
                
$total_amount += $info['amount'];
            }
        }
        if(
$info['pname']=='[일계]' || $info['pname']=='[업체계]'){ $td_class "sumday_class"; }
        else {
            if(
substr($info['pname'],0,7)=='입금('){
                
$midx_url "receive.php?nidx="$midx $info['code'];
            } else {
                
$midx_url "sale.php?midx="$midx $info['midx'];
            }
            
$click_edit_class " click_edit";
        }
?>
                    <tr class='<?php echo $tr_class;?>'>
                    <?php if($td_do=='Y'){?>
                        <td class="tcenter " rowspan='<?php echo $info['comp_cnt'];?>'><?php echo $info['comp_name'];?></td>
                    <?php }?>
                    <?php if($td_do2=='Y'){?>
                        <td class="tcenter" rowspan='<?php echo $info['date_cnt'];?>' ><?php echo $info['ddate'];?></td>
                    <?php }?>
                        <td class="linetxt <?php echo $td_class;?> <?php echo $click_edit_class;?>">
                            <input type='hidden' name='midx' value='<?php echo $midx?>'>
                            <input type='hidden' name='midx_url' value='<?php echo $midx_url?>'>
                            <?php echo $info['pname'];?>
                        </td>
                        <td class="linetxt <?php echo $td_class;?>"><?php echo $info['pnorm'];?></td>
                        <td class="cost <?php echo $td_class;?>"><?php echo number_format($info['pqty']);?></td>
                        <td class="cost <?php echo $td_class;?>"><?php echo number_format($info['pprice']);?></td>
                        <td class="cost <?php echo $td_class;?>"><?php echo number_format($info['psup_amt']);?></td>
                        <td class="cost <?php echo $td_class;?>"><?php echo number_format($info['pvat']);?></td>
                        <td class="cost <?php echo $td_class;?>"><?php echo number_format($info['ptot_amt']);?></td>
                        <td class="cost <?php echo $td_class;?>"><?php echo number_format($info['amount']);?></td>
                        <td class="cost <?php echo $td_class;?>"><?php echo number_format($b_bal);?></td>
                        <td class="linetxt <?php echo $td_class;?>"><?php echo $info['bigo'];?></td>
                    </tr>
<?php 
    

?>
                    <tr class='<?php echo $tr_class;?>'>
                        <td class="tcenter"></td>
                        <td class="tcenter"></td>
                        <td class="linetxt <?php echo $td_class;?>">[총합계]</td>
                        <td class="linetxt <?php echo $td_class;?>">[이월: <?php echo number_format($total_prev_val);?>]</td>
                        <td class="cost <?php echo $td_class;?>"><?php echo number_format($total_qty);?></td>
                        <td class="cost <?php echo $td_class;?>"></td>
                        <td class="cost <?php echo $td_class;?>"><?php echo number_format($total_sup_amt);?></td>
                        <td class="cost <?php echo $td_class;?>"><?php echo number_format($total_vat);?></td>
                        <td class="cost <?php echo $td_class;?>"><?php echo number_format($total_tot_amt);?></td>
                        <td class="cost <?php echo $td_class;?>"><?php echo number_format($total_amount);?></td>
                        <td class="cost <?php echo $td_class;?>"><?php echo number_format($total_prev_val+$total_tot_amt-$total_amount);?></td>
                        <td class="linetxt <?php echo $td_class;?>"></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</form>

</div><!--//wrap-->