1 package SL::Controller::CustomerVendorTurnover;
3 use parent qw(SL::Controller::Base);
5 use SL::DB::AccTransaction;
8 use SL::DB::EmailJournal;
12 __PACKAGE__->run_before('check_auth');
14 sub action_list_turnover {
17 return $self->render('generic/error', { layout => 0 }, label_error => "list_transactions needs a trans_id") unless $::form->{id};
19 my $cv = $::form->{id};
21 if ( $::form->{db} eq 'customer' ) {
22 $open_invoices = SL::DB::Manager::Invoice->get_all(
26 amount => { gt => \'paid'},
27 amount => { lt => \'paid'},
30 sort_by => 'transdate DESC',
31 with_objects => [ 'dunnings' ],
34 $open_invoices = SL::DB::Manager::PurchaseInvoice->get_all(
38 amount => { gt => \'paid'},
39 amount => { lt => \'paid'},
42 sort_by => 'transdate DESC',
46 if (@{$open_invoices}) {
47 $open_items = $self->_list_open_items($open_invoices);
49 my $open_orders = $self->_get_open_orders;
50 return $self->render('customer_vendor_turnover/turnover', { header => 0 },
51 open_orders => $open_orders,
52 open_items => $open_items,
57 sub _list_open_items {
58 my ($self, $open_items) = @_;
60 return $self->render('customer_vendor_turnover/_list_open_items', { output => 0 },
61 OPEN_ITEMS => $open_items,
62 title => $::locale->text('Open Items'),
66 sub action_count_open_items_by_year {
69 return $self->render('generic/error', { layout => 0 }, label_error => "list_transactions needs a trans_id") unless $::form->{id};
70 my $dbh = SL::DB->client->dbh;
72 my $cv = $::form->{id};
75 SELECT EXTRACT (YEAR FROM d.transdate),
79 LEFT JOIN ar a ON a.id = d.trans_id
80 LEFT JOIN customer c ON a.customer_id = c.id
82 GROUP BY EXTRACT (YEAR FROM d.transdate), c.id
83 ORDER BY date_part DESC
86 $self->{dun_statistic} = selectall_hashref_query($::form, $dbh, $query, $cv);
87 $self->render('customer_vendor_turnover/count_open_items_by_year', { layout => 0 });
90 sub action_count_open_items_by_month {
94 return $self->render('generic/error', { layout => 0 }, label_error => "list_transactions needs a trans_id") unless $::form->{id};
95 my $dbh = SL::DB->client->dbh;
97 my $cv = $::form->{id};
100 SELECT CONCAT(EXTRACT (MONTH FROM d.transdate),'/',EXTRACT (YEAR FROM d.transdate)) AS date_part,
104 LEFT JOIN ar a ON a.id = d.trans_id
105 LEFT JOIN customer c ON a.customer_id = c.id
107 GROUP BY EXTRACT (YEAR FROM d.transdate), EXTRACT (MONTH FROM d.transdate), c.id
108 ORDER BY EXTRACT (YEAR FROM d.transdate) DESC
111 $self->{dun_statistic} = selectall_hashref_query($::form, $dbh, $query, $cv);
112 $self->render('customer_vendor_turnover/count_open_items_by_year', { layout => 0 });
115 sub action_turnover_by_month {
119 return $self->render('generic/error', { layout => 0 }, label_error => "list_transactions needs a trans_id") unless $::form->{id};
121 my $dbh = SL::DB->client->dbh;
122 my $cv = $::form->{id};
124 if ($::form->{db} eq 'customer') {
126 $cv_type = "customer_id";
129 $cv_type = "vendor_id";
132 SELECT CONCAT(EXTRACT (MONTH FROM transdate),'/',EXTRACT (YEAR FROM transdate)) as date_part,
134 sum(amount) as amount,
135 sum(netamount) as netamount,
137 FROM $db WHERE $cv_type = ?
138 GROUP BY EXTRACT (YEAR FROM transdate), EXTRACT (MONTH FROM transdate)
139 ORDER BY EXTRACT (YEAR FROM transdate) DESC, EXTRACT (MONTH FROM transdate) DESC
141 $self->{turnover_statistic} = selectall_hashref_query($::form, $dbh, $query, $cv);
142 $self->render('customer_vendor_turnover/count_turnover', { layout => 0 });
145 sub action_turnover_by_year {
148 return $self->render('generic/error', { layout => 0 }, label_error => "list_transactions needs a trans_id") unless $::form->{id};
150 my $dbh = SL::DB->client->dbh;
151 my $cv = $::form->{id};
153 if ($::form->{db} eq 'customer') {
155 $cv_type = "customer_id";
158 $cv_type = "vendor_id";
161 SELECT EXTRACT (YEAR FROM transdate) as date_part,
163 sum(amount) as amount,
164 sum(netamount) as netamount,
166 FROM $db WHERE $cv_type = ?
168 ORDER BY date_part DESC
170 $self->{turnover_statistic} = selectall_hashref_query($::form, $dbh, $query, $cv);
171 $self->render('customer_vendor_turnover/count_turnover', { layout => 0 });
174 sub action_get_invoices {
177 return $self->render('generic/error', { layout => 0 }, label_error => "list_transactions needs a trans_id") unless $::form->{id};
179 my $cv = $::form->{id};
181 if ( $::form->{db} eq 'customer' ) {
182 $invoices = SL::DB::Manager::Invoice->get_all(
183 query => [ customer_id => $cv, ],
184 sort_by => 'transdate DESC',
187 $invoices = SL::DB::Manager::PurchaseInvoice->get_all(
188 query => [ vendor_id => $cv, ],
189 sort_by => 'transdate DESC',
192 $self->render('customer_vendor_turnover/invoices_statistic', { layout => 0 }, invoices => $invoices);
195 sub action_get_orders {
198 return $self->render('generic/error', { layout => 0 }, label_error => "list_transactions needs a trans_id") unless $::form->{id};
200 my $cv = $::form->{id};
202 my $type = $::form->{type};
203 if ( $::form->{db} eq 'customer' ) {
204 $orders = SL::DB::Manager::Order->get_all(
207 quotation => ($type eq 'quotation' ? 'T' : 'F')
209 sort_by => 'transdate DESC',
212 $orders = SL::DB::Manager::Order->get_all(
215 quotation => ($type eq 'quotation' ? 'T' : 'F')
217 sort_by => 'transdate DESC',
220 if ( $type eq 'order') {
221 $self->render('customer_vendor_turnover/order_statistic', { layout => 0 }, orders => $orders);
223 $self->render('customer_vendor_turnover/quotation_statistic', { layout => 0 }, orders => $orders);
227 sub _get_open_orders {
230 return $self->render('generic/error', { layout => 0 }, label_error => "list_transactions needs a trans_id") unless $::form->{id};
232 my $cv = $::form->{id};
234 if ( $::form->{db} eq 'customer' ) {
235 $open_orders = SL::DB::Manager::Order->get_all(
240 sort_by => 'transdate DESC',
243 $open_orders = SL::DB::Manager::Order->get_all(
248 sort_by => 'transdate DESC',
252 return 0 unless scalar @{$open_orders};
253 return $self->render('customer_vendor_turnover/_list_open_orders', { output => 0 },
254 orders => $open_orders,
255 title => $::locale->text('Open Orders'),
259 sub action_get_mails {
262 return $self->render('generic/error', { layout => 0 }, label_error => "list_transactions needs a trans_id") unless $::form->{id};
263 my $dbh = SL::DB->client->dbh;
265 my $cv = $::form->{id};
267 if ( $::form->{db} eq 'customer') {
271 AS (SELECT rc.to_id, rc.from_id, oe.quotation, oe.quonumber, oe.ordnumber, c.id
273 LEFT JOIN oe oe ON rc.from_id = oe.id
274 LEFT JOIN customer c ON oe.customer_id = c.id
275 WHERE rc.to_table = 'email_journal'
276 AND rc.from_table ='oe'),
279 AS (SELECT rc.to_id, rc.from_id, o.donumber, c.id
281 LEFT JOIN delivery_orders o ON rc.from_id = o.id
282 LEFT JOIN customer c ON o.customer_id = c.id
283 WHERE rc.to_table = 'email_journal'
284 AND rc.from_table = 'delivery_orders'),
287 AS (SELECT rc.to_id, rc.from_id, inv.type, inv.invnumber, c.id
289 LEFT JOIN ar inv ON rc.from_id = inv.id
290 LEFT JOIN customer c ON inv.customer_id = c.id
291 WHERE rc.to_table = 'email_journal'
292 AND rc.from_table = 'ar'),
294 letter_emails_customer
295 AS (SELECT rc.to_id, rc.from_id, l.letternumber, c.id
297 LEFT JOIN letter l ON rc.from_id = l.id
298 LEFT JOIN customer c ON l.customer_id = c.id
299 WHERE rc.to_table = 'email_journal'
300 AND rc.from_table = 'letter')
304 oec.quotation WHEN 'F' THEN 'Sales Order'
308 oec.quotation WHEN 'F' THEN oec.ordnumber
312 FROM email_journal ej
313 LEFT JOIN oe_emails_customer oec ON ej.id = oec.to_id
318 SELECT ej.*, 'Delivery Order' AS type, dec.donumber AS recordnumber,dec.id AS record_id
319 FROM email_journal ej
320 LEFT JOIN do_emails_customer dec ON ej.id = dec.to_id
327 iec.type WHEN 'credit_note' THEN 'Credit Note'
328 WHEN 'invoice' THEN 'Invoice'
331 iec.invnumber AS recordnumber,
333 FROM email_journal ej
334 LEFT JOIN inv_emails_customer iec ON ej.id = iec.to_id
339 SELECT ej.*, 'Letter' AS type, lec.letternumber AS recordnumber,lec.id AS record_id
340 FROM email_journal ej
341 LEFT JOIN letter_emails_customer lec ON ej.id = lec.to_id
343 ORDER BY sent_on DESC
350 AS (SELECT rc.to_id, rc.from_id, oe.quotation, oe.quonumber, oe.ordnumber, c.id
352 LEFT JOIN oe oe ON rc.from_id = oe.id
353 LEFT JOIN vendor c ON oe.vendor_id = c.id
354 WHERE rc.to_table = 'email_journal'
355 AND rc.from_table ='oe'),
358 AS (SELECT rc.to_id, rc.from_id, o.donumber, c.id
360 LEFT JOIN delivery_orders o ON rc.from_id = o.id
361 LEFT JOIN vendor c ON o.vendor_id = c.id
362 WHERE rc.to_table = 'email_journal'
363 AND rc.from_table = 'delivery_orders'),
366 AS (SELECT rc.to_id, rc.from_id, inv.type, inv.invnumber, c.id
368 LEFT JOIN ap inv ON rc.from_id = inv.id
369 LEFT JOIN vendor c ON inv.vendor_id = c.id
370 WHERE rc.to_table = 'email_journal'
371 AND rc.from_table = 'ar'),
374 AS (SELECT rc.to_id, rc.from_id, l.letternumber, c.id
376 LEFT JOIN letter l ON rc.from_id = l.id
377 LEFT JOIN vendor c ON l.vendor_id = c.id
378 WHERE rc.to_table = 'email_journal'
379 AND rc.from_table = 'letter')
383 oec.quotation WHEN 'F' THEN 'Purchase Order'
384 ELSE 'Request quotation'
387 oec.quotation WHEN 'F' THEN oec.ordnumber
391 FROM email_journal ej
392 LEFT JOIN oe_emails_vendor oec ON ej.id = oec.to_id
397 SELECT ej.*, 'Purchase Delivery Order' AS type, dec.donumber AS recordnumber, dec.id AS record_id
398 FROM email_journal ej
399 LEFT JOIN do_emails_vendor dec ON ej.id = dec.to_id
404 SELECT ej.*, iec.type AS type, iec.invnumber AS recordnumber, iec.id AS record_id
405 FROM email_journal ej
406 LEFT JOIN inv_emails_vendor iec ON ej.id = iec.to_id
411 SELECT ej.*, 'Letter' AS type, lec.letternumber AS recordnumber, lec.id AS record_id
412 FROM email_journal ej
413 LEFT JOIN letter_emails_vendor lec ON ej.id = lec.to_id
415 ORDER BY sent_on DESC
418 my $emails = selectall_hashref_query($::form, $dbh, $query, $cv, $cv, $cv, $cv);
419 $self->render('customer_vendor_turnover/email_statistic', { layout => 0 }, emails => $emails);
422 sub action_get_letters {
425 return $self->render('generic/error', { layout => 0 }, label_error => "list_transactions needs a trans_id") unless $::form->{id};
427 my $cv = $::form->{id};
429 my $type = $::form->{type};
430 if ( $::form->{db} eq 'customer' ) {
431 $letters = SL::DB::Manager::Letter->get_all(
432 query => [ customer_id => $cv, ],
433 sort_by => 'date DESC',
436 $letters = SL::DB::Manager::Letter->get_all(
437 query => [ vendor_id => $cv, ],
438 sort_by => 'date DESC',
441 $self->render('customer_vendor_turnover/letter_statistic', { layout => 0 }, letters => $letters);
445 $::auth->assert('show_extra_record_tab_customer | show_extra_record_tab_vendor');
456 SL::Controller::CustomerVendorTurnover
460 Gets all kinds of records like orders, request orders, quotations, invoices, emails, letters
462 wich belong to customer/vendor and displays them in an extra tab "Records".
468 =item C<action_list_turnover>
470 Basic action wich displays open invoices and open orders if there are any and shows the tab menu for the other actions
472 =item C<action_count_open_items_by_month>
474 gets and shows a dunning statistic of the customer by month
476 =item C<action_count_open_items_by_year>
478 gets and shows a dunning statistic of the customer by year
480 =item C<action_turnover_by_month>
482 gets and shows an invoice statistic of customer/vendor by month
484 =item C<action_turnover_by_year>
486 gets and shows an invoice statistic of customer/vendor by year
488 =item C<action_get_invoices>
490 get and shows all invoices from the customer/vendor in an extra tab
492 =item C<action_get_orders>
494 get and shows all orders from the customer/vendor in an extra tab
496 =item C<action_get_letters>
498 get and shows all letters from the customer/vendor in an extra tab
500 =item C<action_get_mails>
502 get and shows all mails from the customer/vendor in an extra tab
510 =item C<_get_open_orders>
512 retrieves the open orders for customer/vendor to display them
514 =item C<_list_open_items>
516 retrieves open invoices with their dunnings to display them
526 W. Hahn E<lt>wh@futureworldsearch.netE<gt>