· 4 years ago · Dec 15, 2020, 12:18 PM
1<?php
2
3
4
5define('PAF_CACHE_KEY', 'PAF_CACHE');
6
7
8
9/**
10
11 * Get form info.
12
13 * @return array form_info config array
14
15 */
16
17function paf_get_form_info($node) {
18
19
20
21 $form_info = array(
22
23 'id' => 'paf_add',
24
25 'path' => $node ? "paf/edit/%step/{$node->nid}" : "paf/add/%step",
26
27 'show trail' => TRUE,
28
29 'free trail' => $node ? TRUE : FALSE,
30
31 'show back' => TRUE,
32
33 'show return' => TRUE,
34
35 'next text' => t('Save and continue'),
36
37 'next callback' => 'paf_add_next',
38
39 'back callback' => 'paf_add_back',
40
41 'finish callback' => 'paf_add_finish',
42
43 //'return callback' => 'paf_add_return',
44
45 'cancel callback' => 'paf_add_cancel',
46
47 'order' => array(
48
49 'general' => t('General'),
50
51 'second' => t('Spending Plan'),
52
53 'timing' => t('Timing')
54
55 ),
56
57 'forms' => array(
58
59 'general' => array(
60
61 'form id' => 'paf_add_general_form'
62
63 ),
64
65 'second' => array(
66
67 'form id' => 'paf_add_second_form'
68
69 ),
70
71 'timing' => array(
72
73 'form id' => 'paf_add_timing_form'
74
75 )
76
77 )
78
79 );
80
81
82
83 $invokes = module_invoke_all('paf_form_info', $form_info, $node);
84
85
86
87 /**
88
89 * Here we filter the results from the invoke to insert new step order
90
91 */
92
93 if (isset($invokes['forms']) && !empty($invokes['forms'])) {
94
95
96
97 foreach ($invokes['forms'] as $id => $form) {
98
99
100
101 if (isset($form['after'], $form['name']) && array_key_exists($form['after'], $form_info['order'])) {
102
103
104
105 foreach ($form_info['order'] as $key => $order) {
106
107
108
109 $new_order[$key] = $order;
110
111
112
113 if ($key === $form['after']) {
114
115
116
117 $new_order[$id] = $form['name'];
118
119 }
120
121 }
122
123
124
125 if (isset($new_order)) {
126
127
128
129 $form_info['order'] = $new_order;
130
131 }
132
133 }
134
135 }
136
137 }
138
139
140
141 $form_info = array_merge_recursive($form_info, $invokes);
142
143
144
145 return $form_info;
146
147}
148
149
150
151/**
152
153 *
154
155 * @param type $step
156
157 * @return type
158
159 */
160
161function paf_ctools_wizard($step = 'general', $node = NULL) {
162
163
164
165 ctools_include('wizard');
166
167
168
169 ctools_include('object-cache');
170
171
172
173 ctools_include('ajax');
174
175
176
177 //dpm($node, 'node');
178
179
180
181 $form_info = paf_get_form_info($node);
182
183
184
185 // If the step is not availabe return not found.
186
187
188
189 if (!array_key_exists($step, $form_info['forms'])) {
190
191
192
193 return drupal_not_found();
194
195 }
196
197
198
199 $form_state['paf_object'] = paf_add_get_cache('paf');
200
201
202
203 $form_state['paf_node'] = $node ? $node : paf_add_get_cache('node');
204
205
206
207 $form_state['mode'] = $node ? 'edit' : 'insert';
208
209
210
211 $form_state['approval'] = $node ? paf_get_approval($form_state['paf_node']) : FALSE;
212
213
214
215 $out = ctools_wizard_multistep_form($form_info, $step, $form_state);
216
217
218
219
220
221 return $out;
222
223}
224
225
226
227/**
228
229 *
230
231 * @global type $user
232
233 * @param type $form
234
235 * @param array $form_state
236
237 * @return type
238
239 */
240
241function paf_add_general_form($form, &$form_state) {
242
243
244
245 global $user;
246
247
248
249 //clear stored node from cache on get:)
250
251
252
253 if (isset($_GET['cc']) && $_GET['cc'] == 'new') {
254
255
256
257 paf_add_clear_cache('node');
258
259
260
261 if (isset($form_state['paf_node'])) {
262
263
264
265 unset($form_state['paf_node']);
266
267 }
268
269 }
270
271
272
273 //We get the already saved node :);
274
275 //dpm($form_state);
276
277
278
279 if (isset($form_state['paf_node']) && !isset($form_state['paf_node']->locked)) {
280
281
282
283 $entity = $form_state['paf_node'];
284
285 } else {
286
287
288
289 $entity = (object)array(
290
291 'uid' => $user->uid,
292
293 'name' => isset($user->name) ? $user->name : '',
294
295 'type' => 'paf_project',
296
297 'language' => LANGUAGE_NONE
298
299 );
300
301
302
303 node_object_prepare($entity);
304
305 }
306
307
308
309 $form_state['entity'] = $entity;
310
311
312
313 // We get pseudo fields
314
315
316
317 $extra_fields = field_info_extra_fields('node', 'paf_project', 'form');
318
319
320
321 if (isset($extra_fields['title'])) {
322
323 $form['title'] = array(
324
325 '#type' => 'textfield',
326
327 '#title' => $extra_fields['title']['label'],
328
329 '#default_value' => isset($entity->title) ? $entity->title : '',
330
331 '#weight' => -1,
332
333 '#required' => TRUE
334
335 );
336
337 }
338
339
340
341 field_attach_form('node', $entity, $form, $form_state);
342
343
344
345 if (isset($form['#metatags'])) {
346
347
348
349 unset($form['#metatags']);
350
351 }
352
353
354
355 paf_process_general_form($form, $form_state);
356
357
358
359 //Project Number
360
361
362
363 $number = $form['field_project_number'][LANGUAGE_NONE][0]['value'];
364
365
366
367 $form['field_project_number'] = array(
368
369 '#type' => 'fieldset',
370
371 '#title' => $number['#title'],
372
373 '#weight' => $form['field_project_number']['#weight'],
374
375 '#prefix' => '<div class="field-project-number-fieldset">',
376
377 '#suffix' => '</div>',
378
379 '#tree' => TRUE,
380
381 '#required' => TRUE
382
383 );
384
385
386
387 //Get the serial
388
389 $time = new DateTime();
390
391 $year = $time->format('Y');
392
393
394
395// if (isset($number['#default_value']) && empty($number['#default_value'])) {
396
397 if ($year == '2016') {
398
399
400
401
402
403 if (isset($entity->field_serial_2016) && !empty($entity->field_serial_2016)) {
404
405
406
407 $serial = $entity->field_serial_2016[LANGUAGE_NONE][0]['value'];
408
409 } else {
410
411
412
413 $serial = paf_get_2016_serial();
414
415 $entity->field_serial_2016[LANGUAGE_NONE][0]['value'] = $serial;
416
417 }
418
419 } else {
420
421 if (!empty($entity->field_serial[LANGUAGE_NONE][0]['value'])) {
422
423 $serial = $entity->field_serial[LANGUAGE_NONE][0]['value'];
424
425 } else {
426
427 $serial = paf_get_next_serial();
428
429 }
430
431 }
432
433// }
434
435
436
437 $form_state['entity'] = $entity;
438
439 // lets get the other values
440
441
442
443 if (isset($entity->field_project_number)) {
444
445
446
447 $parts = explode('-', $entity->field_project_number['und'][0]['value']);
448
449
450
451
452
453 $project_date = str_replace('.', '-', $parts[0]);
454
455
456
457 $project_name = $parts[3];
458
459 }
460
461
462
463 $paf_number_suffix = '-PAF-' . $serial;
464
465
466
467
468
469 if (isset($entity->title)) {
470
471
472
473 $paf_number_suffix .= '-' . $entity->title;
474
475 }
476
477
478
479 $form['field_project_number']['date'] = array(
480
481 '#type' => 'date_popup',
482
483// '#default_value' => date('Y-m-d'),
484
485 '#date_format' => 'Y-m-d',
486
487 '#date_label_position' => 'within',
488
489 '#required' => TRUE,
490
491 '#field_suffix' => $paf_number_suffix,
492
493 '#default_value' => isset($project_date) ? $project_date : date('Y-m-d')
494
495 );
496
497
498
499 paf_financial_form($form, $form_state);
500
501
502
503 return $form;
504
505}
506
507
508
509function paf_process_general_form(&$form, &$form_state) {
510
511
512
513 $brands = '';
514
515
516
517 if (isset($form_state['paf_node']->nid)) {
518
519
520
521 $brands = isset($form_state['values']['field_brand']) ?
522
523 $form_state['values']['field_brand'] :
524
525 isset($form_state['paf_node']->field_brand) ?
526
527 $form_state['paf_node']->field_brand : '';
528
529 } elseif (isset($form_state['values']['field_brand'])) {
530
531
532
533 /**
534
535 * We try to get the value of the brand from form_state['values']
536
537 * Then we try to get it from the inut values. Hopefully
538
539 */
540
541 $brands = $form_state['values']['field_brand'];
542
543 } elseif (isset($form_state['input']['field_brand'])) {
544
545
546
547 $brands = $form_state['input']['field_brand'];
548
549 }
550
551
552
553
554
555 // Set field_brand options
556
557
558
559 if (isset($form['field_brand']['und'])) {
560
561
562
563 $form['field_brand']['und']['#attributes']['autocomplete'] = 'off';
564
565
566
567 $form['field_brand']['und']['#options'] = paf_get_brands();
568
569
570
571 $form['field_brand']['und']['#ajax'] = array(
572
573 'callback' => 'paf_brand_sku_js',
574
575 'wrapper' => 'field-skus-ajax',
576
577 'method' => 'replace',
578
579 'effect' => 'fade'
580
581 );
582
583
584
585
586
587 // $form['field_brand']['und']['#default_value'] = $brands;
588
589
590
591 /**
592
593 * We need to explicity set the field as validated so no ajax illegal
594
595 * choices will happen.
596
597 * We try to detect ajax
598
599 * This though have consequences:
600
601 * @see paf_add_general_form_submit()
602
603 */
604
605 if (!isset($form['field_brand']['und']['#default_value'])) {
606
607
608
609 $form['field_brand']['und']['#default_value'] = $brands;
610
611 }
612
613
614
615 //$form['field_brand']['und']['#validated'] = TRUE;
616
617 }
618
619
620
621 // Set field_skus options
622
623
624
625 if (isset($form['field_skus']['und'])) {
626
627
628
629 $form['field_skus']['und']['#prefix'] = '<div id="field-skus-ajax">';
630
631
632
633 $form['field_skus']['und']['#suffix'] = '</div>';
634
635
636
637 //Load brands from the cache else use the default value
638
639
640
641 $form['field_skus']['und']['#options'] = paf_get_skus($brands);
642
643 }
644
645
646
647 //Remove redirect option
648
649
650
651 if (isset($form['redirect'])) {
652
653
654
655 unset($form['redirect']);
656
657 }
658
659
660
661 if ($form_state['mode'] == 'create') {
662
663
664
665 unset($form['field_tax_comment']);
666
667
668
669 unset($form['field_control_comment']);
670
671
672
673 unset($form['field_process_comment']);
674
675
676
677 unset($form['field_procurement_comment']);
678
679
680
681 unset($form['field_csl_comment']);
682
683 }
684
685
686
687 $form['field_sent_for_approval']['#access'] = FALSE;
688
689
690
691 $form['field_presented_for_approval_on']['#access'] = FALSE;
692
693
694
695 $form['field_paf_approved']['#access'] = FALSE;
696
697}
698
699
700
701/**
702
703 *
704
705 * @param type $element
706
707 * @param type $form_state
708
709 * @param type $form
710
711 */
712
713function paf_validate_brand_field($element, &$form_state, $form) {
714
715
716
717}
718
719
720
721function paf_brand_sku_js($form, $form_state) {
722
723
724
725 return $form['field_skus']['und'];
726
727}
728
729
730
731/**
732
733 * Validation function.
734
735 * @param type $form
736
737 * @param type $form_state
738
739 * @todo Handle division by zeros
740
741 */
742
743function paf_add_general_form_validate(&$form, &$form_state) {
744
745
746
747 // Calculate the fielsd again and don't rely on javascript.
748
749
750
751 $financial = &$form_state['values']['financial'];
752
753
754
755 $financial['net_sales'] = $financial['gpps'] * $financial['nts'];
756
757
758
759 if ($financial['nts'] > 0) {
760
761 $financial['sap'] = (+$financial['net_sales'] - $financial['cgs']) / $financial['nts'];
762
763 } else {
764
765 $financial['sap'] = 0;
766
767 }
768
769
770
771 $financial['vpckg'] = $financial['vpc'];
772
773
774
775 // $financial['vpckg'] = $financial['vpc'] / $financial['pav'];
776
777
778
779 $financial['pap'] = round($financial['sap'] - $financial['vpckg']);
780
781
782
783
784
785 $financial['plan'] = $financial['nts'];
786
787
788
789 $financial['vr'] = ($financial['plan'] * (30 / 100));
790
791
792
793 $financial['pav'] = $financial['vr'];
794
795
796
797
798
799 $financial['bv'] = $financial['plan'] - $financial['vr'];
800
801
802
803 if ((int)$financial['bv'] !== 0) {
804
805
806
807 $financial['avpb'] = round(($financial['pav'] / $financial['bv']) * 100);
808
809 } else {
810
811
812
813 $financial['avpb'] = 0;
814
815 }
816
817
818
819 $financial['ipm'] = round($financial['sap'] * $financial['pav']);
820
821
822
823 $financial['tpc'] = $financial['fpc'] + $financial['vpc'];
824
825
826
827 $financial['iOC'] = round($financial['ipm'] - $financial['tpc']);
828
829
830
831 if ((int)$financial['pap'] !== 0) {
832
833
834
835 $financial['bev'] = round($financial['fpc'] / $financial['pap']);
836
837 } else {
838
839
840
841 $financial['bev'] = 0;
842
843 }
844
845
846
847 if ((int)$financial['tpc'] !== 0) {
848
849
850
851 $financial['rov'] = round(($financial['iOC'] / $financial['tpc']) * 100);
852
853 } else {
854
855
856
857 $financial['rov'] = 0;
858
859 }
860
861
862
863 foreach ($financial as &$val) {
864
865
866
867 $val = round($val, 2, PHP_ROUND_HALF_UP);
868
869 }
870
871
872
873 if (isset($form_state['values']['field_project_number']['date'])) {
874
875
876
877 $form_state['values']['field_project_number']['date'] = str_replace('-', '.', $form_state['values']['field_project_number']['date']);
878
879 }
880
881}
882
883
884
885/**
886
887 *
888
889 * @param type $form
890
891 * @param type $form_state
892
893 */
894
895function paf_add_general_form_submit(&$form, &$form_state) {
896
897
898
899 if ($form_state['clicked_button']['#wizard type'] == 'next') {
900
901
902
903 $vals = $form_state['values'];
904
905
906
907 $node = $form_state['entity'];
908
909
910
911 $cached_node = isset($form_state['paf_node']) ? clone $form_state['paf_node'] : NULL;
912
913
914
915 $node->title = check_plain($form_state['values']['title']);
916
917
918
919 //Here we hold fields that are customized.
920
921
922
923 $customized_fields = array('field_project_number');
924
925
926
927 //Get degault field values and assign them
928
929
930
931 foreach ($vals as $key => $val) {
932
933
934
935 if ((strstr($key, 'field_') || $key == 'body') && !in_array($key, $customized_fields)) {
936
937
938
939 if ($key == 'field_serial_2016') {
940
941 //Update the 2016 PAF serial number on save
942
943 $node->field_serial_2016[LANGUAGE_NONE][0]['value'] = paf_get_2016_serial();
944
945 continue;
946
947 }
948
949
950
951 if ($key == 'field_project_number') {
952
953 continue;
954
955 }
956
957
958
959 $fields_debug[] = $key;
960
961
962
963 //check to see if filed_collection is setting add_more and remove
964
965
966
967 if (isset($val[LANGUAGE_NONE]) && key_exists('add_more', $val[LANGUAGE_NONE])) {
968
969
970
971 unset($val[LANGUAGE_NONE]['add_more']);
972
973 }
974
975
976
977 if (isset($val[LANGUAGE_NONE]) && key_exists('target_id', $val[LANGUAGE_NONE][0])) {
978
979
980
981 if (is_numeric($val[LANGUAGE_NONE][0]['target_id'])) {
982
983
984
985 $node->{$key} = $val;
986
987 }
988
989 } else {
990
991
992
993 //Chech to see if value is set
994
995
996
997 if ($key == 'field_presented_for_approval_on') {
998
999
1000
1001 //Check to see if key exists and if not again continue
1002
1003
1004
1005 if (isset($val[LANGUAGE_NONE], $val[LANGUAGE_NONE][0])) {
1006
1007
1008
1009 if ($val[LANGUAGE_NONE][0]['value'] === '') {
1010
1011
1012
1013 $val[LANGUAGE_NONE][0]['value'] = NULL;
1014
1015 }
1016
1017 } else {
1018
1019
1020
1021 $val[LANGUAGE_NONE][0]['value'] = NULL;
1022
1023 }
1024
1025 }
1026
1027
1028
1029 $node->{$key} = $val;
1030
1031 }
1032
1033 }
1034
1035 }
1036
1037
1038
1039 //
1040
1041 $formated_date = str_replace('.', '-', $vals['field_project_number']['date']);
1042
1043 $time = new DateTime($formated_date);
1044
1045 $year = $time->format('Y');
1046
1047 if ($year == '2016') {
1048
1049 $node->field_serial[LANGUAGE_NONE][0]['value'] = 0;
1050
1051 } elseif (empty($node->field_serial[LANGUAGE_NONE][0]['value'])) {
1052
1053 $node->field_serial_2016 = NULL;
1054
1055 //set to empty so the serial modules sets its generated values
1056
1057 $node->field_serial = NULL;
1058
1059 }
1060
1061
1062
1063 try {
1064
1065 node_save($node);
1066
1067
1068
1069 $form_state['continue'] = TRUE;
1070
1071
1072
1073 $form_state['node'] = $node;
1074
1075 } catch (Exception $exc) {
1076
1077
1078
1079 watchdog_exception('paf', $exc);
1080
1081
1082
1083 drupal_set_message(t('There was an error saving the PAF. Please try again and if the problem perssist please contact us.'), 'error');
1084
1085
1086
1087 $form_state['continue'] = FALSE;
1088
1089 }
1090
1091
1092
1093 // If node is new we set the status to draft
1094
1095
1096
1097 if (!isset($cached_node->nid)) {
1098
1099
1100
1101 $node->field_paf_approved[LANGUAGE_NONE][0]['value'] = PAF_NODE_DRAFT;
1102
1103 }
1104
1105
1106
1107 // Let's set project number if we are inserting for first time
1108
1109 //if (!isset($form_state['paf_node']->nid)) {
1110
1111
1112
1113
1114
1115 if ($year == '2016') {
1116
1117 $node->field_project_number[LANGUAGE_NONE][0]['value'] = $formated_date
1118
1119 . '-PAF-'
1120
1121 . $node->field_serial_2016[LANGUAGE_NONE][0]['value']
1122
1123 . '-' . $vals['title'];
1124
1125 } else {
1126
1127
1128
1129 $node->field_project_number[LANGUAGE_NONE][0]['value'] = $formated_date
1130
1131 . '-PAF-'
1132
1133 . $node->field_serial[LANGUAGE_NONE][0]['value']
1134
1135 . '-' . $vals['title'];
1136
1137 }
1138
1139 //And we save again
1140
1141
1142
1143 try {
1144
1145
1146
1147 node_save($node);
1148
1149
1150
1151 $form_state['continue'] = TRUE;
1152
1153
1154
1155 $form_state['node'] = $node;
1156
1157 } catch (Exception $exc) {
1158
1159
1160
1161 drupal_set_message(t('There was an error saving the PAF. Please try again and if the problem perssist please contact us.'), 'error');
1162
1163
1164
1165 $form_state['continue'] = FALSE;
1166
1167 }
1168
1169
1170
1171 //}
1172
1173 //Lets get financial data
1174
1175
1176
1177 $vals['financial']['nid'] = $node->nid;
1178
1179
1180
1181 $vals['financial']['created'] = REQUEST_TIME;
1182
1183
1184
1185 foreach ($vals['financial'] as $field => &$value) {
1186
1187
1188
1189 }
1190
1191
1192
1193 // Check to see if financial info not already there. And switch to update
1194
1195
1196
1197 if ($cached_node && isset($cached_node->financial['fid'])) {
1198
1199
1200
1201 $primary_keys = 'fid';
1202
1203
1204
1205 $vals['financial']['fid'] = $cached_node->financial['fid'];
1206
1207 } else {
1208
1209
1210
1211 $primary_keys = array();
1212
1213 }
1214
1215
1216
1217 //Try to save the financial part;
1218
1219
1220
1221 try {
1222
1223
1224
1225 $op = drupal_write_record('paf_financial', $vals['financial'], $primary_keys);
1226
1227 } catch (Exception $exc) {
1228
1229
1230
1231 drupal_set_message(t('There was an error saving the PAF. Please try again and if the problem perssist please contact us.'), 'error');
1232
1233
1234
1235 watchdog_exception('paf', $exc);
1236
1237
1238
1239 $form_state['continue'] = FALSE;
1240
1241
1242
1243 $form_state['rebuild'] = TRUE;
1244
1245
1246
1247 return;
1248
1249 }
1250
1251
1252
1253
1254
1255 if (!$op) {
1256
1257
1258
1259 drupal_set_message(t('There was an error saving the PAF. Please try again and if the problem perssist please contact us.'), 'error');
1260
1261
1262
1263 $form_state['continue'] = FALSE;
1264
1265 } else {
1266
1267
1268
1269 $node->financial = $vals['financial'];
1270
1271 }
1272
1273
1274
1275 //$form_state['continue'] = FALSE;
1276
1277 }
1278
1279}
1280
1281
1282
1283/**
1284
1285 * Second step form
1286
1287 * @param array $form
1288
1289 * @param type $form_state
1290
1291 * @return string
1292
1293 */
1294
1295function paf_add_second_form($form, &$form_state) {
1296
1297
1298
1299 $node = $form_state['paf_node'];
1300
1301
1302
1303
1304
1305 /* @deprecated This variable is deprecated used only for compability issues */
1306
1307
1308
1309 $disabled = FALSE;
1310
1311
1312
1313
1314
1315 $form['#attached']['js'] = array(
1316
1317 drupal_get_path('module', 'paf') . '/js/paf_spendings.js',
1318
1319 );
1320
1321
1322
1323 $form['#attributes'] = array(
1324
1325 'autocomplete' => 'off'
1326
1327 );
1328
1329
1330
1331 $form['spending_plan'] = array(
1332
1333 '#theme' => 'paf_form_table',
1334
1335 '#title' => t('Spending Plan'),
1336
1337 '#headers' => array(
1338
1339 t('Type of expense'), t('Account'), t('CC'), t('Description'), t('Amount'), t('Actions')//, t('Doc Number')
1340
1341 ),
1342
1343 '#attributes' => array('id' => 'sp-table'),
1344
1345 '#tree' => TRUE,
1346
1347 '#prefix' => '<div id="spending-plan-wrapper">',
1348
1349 '#suffix' => '</div>',
1350
1351 '#disabled' => $disabled
1352
1353 );
1354
1355
1356
1357 $form_state['type_expenses'] = paf_get_type_expense();
1358
1359
1360
1361 $form_state['cc'] = paf_get_cost_centers();
1362
1363
1364
1365 // Get already populated data
1366
1367
1368
1369 if (isset($node->sp) && !empty($node->sp)) {
1370
1371
1372
1373
1374
1375 foreach (array_keys($node->sp) as $k) {
1376
1377
1378
1379 $nums[$k] = $k;
1380
1381 }
1382
1383
1384
1385 if (!isset($nums)) {
1386
1387
1388
1389 $nums = array(1 => 1);
1390
1391 }
1392
1393
1394
1395 $form_state['num'] = isset($form_state['num']) ? $form_state['num'] : $nums;
1396
1397
1398
1399 // else get default 1
1400
1401 } else {
1402
1403
1404
1405 // The spfr info is empty please set it to false
1406
1407
1408
1409 $node->sp = FALSE;
1410
1411
1412
1413 $form_state['num'] = isset($form_state['num']) ? $form_state['num'] : array(1 => 1);
1414
1415 }
1416
1417
1418
1419
1420
1421 foreach ($form_state['num'] as $i) {
1422
1423
1424
1425 // Get cached node data. Else get default value ""
1426
1427
1428
1429 if (isset($node->sp) && !empty($node->sp)) {
1430
1431
1432
1433 $type_expense = isset($form_state['values']['spending_plan'][$i]['type_expense']) ?
1434
1435 $form_state['values']['spending_plan'][$i]['type_expense'] :
1436
1437 $node->sp[$i]['type_of_expense'];
1438
1439 } else {
1440
1441
1442
1443 $type_expense = isset($form_state['values']['spending_plan'][$i]['type_expense']) ?
1444
1445 $form_state['values']['spending_plan'][$i]['type_expense'] : '';
1446
1447 }
1448
1449
1450
1451 // Id for each tr in the table
1452
1453
1454
1455 $keys = array_keys($form_state['type_expenses']);
1456
1457
1458
1459 $tr_id = $type_expense ? $type_expense : reset($keys);
1460
1461
1462
1463 $form['spending_plan'][$i] = array(
1464
1465 '#attributes' => array(
1466
1467 'class' => 'sp-nid-' . $tr_id),
1468
1469 );
1470
1471
1472
1473 $default_select = array(0 => t("- Please Select -"));
1474
1475
1476
1477 $default_select += $form_state['type_expenses'];
1478
1479
1480
1481 // Type of expense field
1482
1483
1484
1485 $form['spending_plan'][$i]['type_expense'] = array(
1486
1487 '#type' => 'select',
1488
1489 '#options' => $default_select,
1490
1491 '#default_value' => $type_expense,
1492
1493 '#empty_value' => 0,
1494
1495 '#ajax_id' => $i,
1496
1497 '#attributes' => array('class' => array('spending_plan_select')),
1498
1499 '#ajax' => array(
1500
1501 'callback' => 'paf_type_expense_account_js',
1502
1503 'wrapper' => 'account-' . $i,
1504
1505 'method' => 'replace',
1506
1507 'effect' => 'fade'
1508
1509 ),
1510
1511 '#required' => TRUE
1512
1513 );
1514
1515
1516
1517
1518
1519 $account = paf_get_account_by_expense($type_expense);
1520
1521
1522
1523 $form['spending_plan'][$i]['account'] = array(
1524
1525 '#type' => 'textfield',
1526
1527 '#attributes' => array(
1528
1529 'readonly' => 'reaonly',
1530
1531 'style' => 'width:65px;'
1532
1533 ),
1534
1535 '#prefix' => '<div id="account-' . $i . '">',
1536
1537 '#suffix' => '</div>',
1538
1539 '#value' => $account,
1540
1541 '#size' => 7,
1542
1543 '#required' => TRUE
1544
1545 );
1546
1547
1548
1549 $form['spending_plan'][$i]['cc'] = array(//cost center
1550
1551
1552
1553 '#type' => 'select',
1554
1555 '#options' => $form_state['cc'],
1556
1557 '#attributes' => array('class' => array('sp-cc')),
1558
1559 '#default_value' => isset($node->sp) ? $node->sp[$i]['actual_cc'] : '--',
1560
1561 '#required' => TRUE
1562
1563 );
1564
1565
1566
1567// $form['spending_plan'][$i]['doc_number'] = array(
1568
1569// '#type' => 'textfield',
1570
1571// '#attributes' => array('class' => array('sp-num')),
1572
1573// '#default_value' => isset($node->sp) ? $node->sp[$i]['doc_number'] : '',
1574
1575//// '#required' => TRUE
1576
1577// '#required' => FALSE
1578
1579// );
1580
1581
1582
1583 $form['spending_plan'][$i]['desc'] = array(
1584
1585 '#type' => 'textfield',
1586
1587 '#attributes' => array('class' => array('sp-desc')),
1588
1589 '#default_value' => isset($node->sp) ? $node->sp[$i]['description'] : '',
1590
1591 '#required' => TRUE
1592
1593 );
1594
1595
1596
1597 $form['spending_plan'][$i]['amount'] = array(
1598
1599 '#type' => 'textfield',
1600
1601 '#attributes' => array('class' => array('sp-amount')),
1602
1603 '#default_value' => isset($node->sp) ? $node->sp[$i]['amount'] : '',
1604
1605 '#required' => TRUE
1606
1607 );
1608
1609
1610
1611 $form['spending_plan'][$i]['remove'] = array(
1612
1613 '#type' => 'submit',
1614
1615 '#value' => t('Remove') . $i,
1616
1617 '#pre_render' => array('paf_fix_submit_trigger'),
1618
1619 '#submit' => array('paf_ajax_remove_expense'),
1620
1621 '#attributes' => array('class' => array('spending-row-' . $i)),
1622
1623 '#ajax' => array(
1624
1625 'callback' => 'paf_type_expense_js',
1626
1627 'wrapper' => 'spending-plan-wrapper',
1628
1629 'method' => 'replace',
1630
1631 'effect' => 'fade'
1632
1633 )
1634
1635 );
1636
1637 }
1638
1639
1640
1641 $form['add_new'] = array(
1642
1643 '#type' => 'submit',
1644
1645 '#value' => t('Add new'),
1646
1647 '#submit' => array('paf_ajax_add_new_expense'),
1648
1649 '#ajax' => array(
1650
1651 'callback' => 'paf_type_expense_js',
1652
1653 'wrapper' => 'spending-plan-wrapper',
1654
1655 'method' => 'replace',
1656
1657 'effect' => 'fade'
1658
1659 ),
1660
1661 '#disabled' => $disabled
1662
1663 );
1664
1665
1666
1667// $form["financial_rec"] = array(
1668
1669// "#theme" => "paf_form_table",
1670
1671// "#tree" => TRUE,
1672
1673// '#attributes' => array('id' => 'fr-table'),
1674
1675// '#headers' => array("P&L Line", "Budget", "YTD spend", 'CE', 'diff. vs. CE', 'remaining Budget'),
1676
1677// '#disabled' => $disabled
1678
1679// );
1680
1681// $vals = array();
1682
1683 // @TODO Very complicated should be simplified. NO need to fancy loops
1684
1685 // To explain : We map the customized field with the coresponding table
1686
1687// $rows = array(
1688
1689// array("data" => "p_and_l"),
1690
1691// array(
1692
1693// "data" => "budget",
1694
1695// 'db' => 'budget'
1696
1697// ),
1698
1699// array(
1700
1701// "data" => "ytd_spend",
1702
1703// 'db' => 'YTD'
1704
1705// ),
1706
1707// array(
1708
1709// "data" => "ce",
1710
1711// 'db' => 'CE'
1712
1713// ),
1714
1715// array(
1716
1717// "data" => "diff",
1718
1719// 'db' => 'diff_vs_CE'
1720
1721// ),
1722
1723// array(
1724
1725// "data" => "remaining_budget",
1726
1727// 'db' => 'remaining_budget'
1728
1729// )
1730
1731// );
1732
1733// $i = 0;
1734
1735// foreach ($form_state['type_expenses'] as $nid => $item) {
1736
1737// $vals[$i] = array(
1738
1739// 'data' => $item,
1740
1741// 'id' => $nid,
1742
1743// );
1744
1745// foreach ($rows as $k => $row) {
1746
1747// if (isset($row['db'])) {
1748
1749// $vals[$i]['defaults'][$row['data']] = isset($node->spfr['fr'][$nid]) ?
1750
1751// $node->spfr['fr'][$nid][$row['db']] :
1752
1753// '';
1754
1755// }
1756
1757// }
1758
1759// $i++;
1760
1761// }
1762
1763// foreach ($vals as $j => $val) {
1764
1765// $form["financial_rec"][$val["id"]] = array(
1766
1767// "#tree" => TRUE,
1768
1769// "#title" => $val["data"],
1770
1771// '#attributes' => array('id' => 'fr-nid-' . $val['id'])
1772
1773// );
1774
1775//
1776
1777// foreach ($rows as $k => $row) {
1778
1779// if ($k > 0) {
1780
1781// $form["financial_rec"][$val["id"]][$row["data"]] = array(
1782
1783// "#type" => "textfield",
1784
1785// '#attributes' => array(
1786
1787// 'class' => array('class' => $row["data"]),
1788
1789// ),
1790
1791// '#default_value' => isset($val['defaults'][$row['data']]) ? $val['defaults'][$row['data']] : ''
1792
1793// );
1794
1795// if ($k > 2) {
1796
1797// $form["financial_rec"][$val["id"]][$row["data"]]['#attributes']['readonly'] = 'readonly';
1798
1799// }
1800
1801// } else {
1802
1803// $form["financial_rec"][$val["id"]][$row["data"]] = array(
1804
1805// '#markup' => $vals[$j]['data']
1806
1807// );
1808
1809// }
1810
1811// }
1812
1813// }
1814
1815//Added total sum of spending plan
1816
1817 $sum = 0;
1818
1819 if (isset($node->sp) && !empty($node->sp) && isset($form_state['num'])) {
1820
1821
1822
1823 foreach ($node->sp as $id => $data) {
1824
1825 $sum += $data['amount'];
1826
1827 }
1828
1829 }
1830
1831
1832
1833 if (isset($form_state['values']['spending_plan']) && !empty($form_state['values']['spending_plan'])) {
1834
1835 foreach ($form_state['num'] as $key) {
1836
1837 $sum += $form_state['values']['spending_plan'][$key]['amount'];
1838
1839 }
1840
1841 }
1842
1843 $form['spending_plan']['spending_plan_total'] = array(
1844
1845 '#prefix' => '<div class="spending-plan-total-wrapper">',
1846
1847 '#suffix' => '</div>',
1848
1849 '#attributes' => array(
1850
1851 'class' => 'sp-total-footer'),
1852
1853 );
1854
1855 $form['spending_plan']['spending_plan_total']['name'] = array(
1856
1857 '#markup' => '<div class="spending-plan-total-label">' . t('Spending Plan Total') . '</div>'
1858
1859 );
1860
1861 $form['spending_plan']['spending_plan_total'][] = array(
1862
1863 '#markup' => ' '
1864
1865 );
1866
1867 $form['spending_plan']['spending_plan_total'][] = array(
1868
1869 '#markup' => ' '
1870
1871 );
1872
1873 $form['spending_plan']['spending_plan_total'][] = array(
1874
1875 '#markup' => ' '
1876
1877 );
1878
1879
1880
1881 $form['spending_plan']['spending_plan_total']['total'] = array(
1882
1883// '#markup' => '<div id="spending-plan-total">' . sprintf('%0.2f', $sum) . '</div>'
1884
1885 '#markup' => '<div id="spending-plan-total">' . number_format($sum, 2, '.', ' ') . '</div>'
1886
1887 );
1888
1889
1890
1891 return $form;
1892
1893}
1894
1895
1896
1897/**
1898
1899 *
1900
1901 * @param type $form
1902
1903 * @param type $form_state
1904
1905 * @return type
1906
1907 */
1908
1909function paf_add_second_form_submit(&$form, &$form_state) {
1910
1911
1912
1913
1914
1915 $form_state['continue'] = TRUE;
1916
1917
1918
1919 //dpm($form_state['values'], 'second_values');
1920
1921
1922
1923 $vals = $form_state['values'];
1924
1925
1926
1927 if (!isset($form_state['paf_node']) || !isset($form_state['paf_node']->nid)) {
1928
1929
1930
1931 $form_state['continue'] = FALSE;
1932
1933
1934
1935 drupal_set_message(t('Missing node data!'), 'error');
1936
1937
1938
1939 return;
1940
1941 }
1942
1943
1944
1945 // We save paf_node in another form_state so we can pass that for caching
1946
1947 // @see paf_add_next()
1948
1949
1950
1951 $form_state['node'] = $form_state['paf_node'];
1952
1953
1954
1955 //clone the financials so we can alter them
1956
1957 //$financials = $vals['financial_rec'];
1958
1959 //We need to get already inserted nodes
1960
1961
1962
1963 $sps = paf_load_sp($form_state['paf_node']->nid);
1964
1965
1966
1967 // $frs = paf_load_fr($form_state['paf_node']->nid);
1968
1969 //clone the result so we can keep an unaltered copy.
1970
1971// dpm($sps, 'sps');
1972
1973// dpm($frs, 'frs');
1974
1975
1976
1977 $sps_clone = $sps;
1978
1979
1980
1981// $frs_clone = $frs;
1982
1983 //$form_state['node'] = $form_state['paf_node'];
1984
1985 //return;
1986
1987 //Prepeare insert queries
1988
1989
1990
1991
1992
1993 foreach ($vals['spending_plan'] as $key => $sp) {
1994
1995
1996
1997 if ($sp['type_expense']) {
1998
1999
2000
2001 //We set the spending plan data
2002
2003
2004
2005 $toe = $sp['type_expense'];
2006
2007
2008
2009 //Declare primary keys
2010
2011
2012
2013 $primary_keys = array();
2014
2015 $data['sp'][$key] = array(
2016
2017 'nid' => $form_state['paf_node']->nid,
2018
2019 'type_of_expense' => $sp['type_expense'],
2020
2021 'actual_account' => $sp['account'] ? $sp['account'] : NULL,
2022
2023 'actual_cc' => $sp['cc'] != '--' ? $sp['cc'] : NULL,
2024
2025 'doc_number' => array_key_exists('doc_number', $sp) ? $sp['doc_number'] : NULL,
2026
2027 'description' => $sp['desc'] ? $sp['desc'] : NULL,
2028
2029 'amount' => $sp['amount'] ? $sp['amount'] : NULL,
2030
2031 'created' => REQUEST_TIME,
2032
2033 'changed' => REQUEST_TIME
2034
2035 );
2036
2037
2038
2039 // Check to see if we need to update and also mark unused info
2040
2041 // for deletion
2042
2043
2044
2045 foreach ($sps as $id => $sp2) {
2046
2047
2048
2049 if ($sp2->type_of_expense == $toe) {
2050
2051
2052
2053 $data['sp'][$key]['id'] = $id;
2054
2055
2056
2057 //Remove created date so we don't update it.
2058
2059
2060
2061 unset($data['sp'][$sp['type_expense']]['created']);
2062
2063
2064
2065 // Remove this sps as it's already processed
2066
2067
2068
2069 unset($sps[$id]);
2070
2071
2072
2073 $primary_keys = 'id';
2074
2075
2076
2077 break;
2078
2079 }
2080
2081 }
2082
2083
2084
2085
2086
2087 try {
2088
2089
2090
2091 $status[] = drupal_write_record('paf_sp', $data['sp'][$key], $primary_keys);
2092
2093 } catch (Exception $exc) {
2094
2095
2096
2097 drupal_set_message('There is an error saving paf data. Please review your changes', 'error');
2098
2099
2100
2101 $form_state['continue'] = FALSE;
2102
2103 }
2104
2105
2106
2107
2108
2109 //Reset Primary keys
2110
2111
2112
2113 $primary_keys = array();
2114
2115
2116
2117 //dpm($data['fr'][$sp['type_expense']], 'data-'.$toe);
2118
2119 //dpm($primary_keys, 'pkeys-'. $toe);
2120
2121 }
2122
2123 }
2124
2125
2126
2127 // We need to remove what is left from sps and frs as this are deleted records.
2128
2129
2130
2131 if ($sps && !empty($sps)) {
2132
2133
2134
2135 $delete[] = db_delete('paf_sp')->condition('id', array_keys($sps))
2136
2137 ->execute();
2138
2139 }
2140
2141
2142
2143 //Check all statuses
2144
2145// foreach ($status as $op) {
2146
2147// if (!$op) {
2148
2149// drupal_set_message(t('There was an error saving the PAF. Please try again and if the problem perssist please contact us.'), 'error');
2150
2151// $form_state['continue'] = FALSE;
2152
2153// return;
2154
2155// }
2156
2157// }
2158
2159 // We need to check data because there is a possibility the user submitted nothing
2160
2161
2162
2163 if (isset($data)) {
2164
2165
2166
2167 $form_state['node']->sp = $data['sp'];
2168
2169 }
2170
2171}
2172
2173
2174
2175/**
2176
2177 *
2178
2179 * @param type $form
2180
2181 * @param array $form_state
2182
2183 */
2184
2185function paf_ajax_add_new_expense($form, &$form_state) {
2186
2187
2188
2189 $last = end($form_state['num']);
2190
2191
2192
2193 $last++;
2194
2195
2196
2197 $form_state['num'][$last] = $last;
2198
2199
2200
2201 $form_state['rebuild'] = TRUE;
2202
2203}
2204
2205
2206
2207/**
2208
2209 *
2210
2211 * @param type $form
2212
2213 * @param boolean $form_state
2214
2215 */
2216
2217function paf_ajax_remove_expense($form, &$form_state) {
2218
2219
2220
2221 $trigger = $form_state['triggering_element']['#parents'];
2222
2223
2224
2225 if (isset($form_state['num'][$trigger[1]]) && count($form_state['num']) > 1) {
2226
2227
2228
2229 unset($form_state['num'][$trigger[1]]);
2230
2231
2232
2233 $form_state['rebuild'] = TRUE;
2234
2235 }
2236
2237}
2238
2239
2240
2241/**
2242
2243 * Ajax callback
2244
2245 * @param type $form
2246
2247 * @param type $form_state
2248
2249 * @return array Form element
2250
2251 */
2252
2253function paf_type_expense_account_js($form, $form_state) {
2254
2255
2256
2257 $trigger = $form_state['triggering_element'];
2258
2259
2260
2261 return $form['spending_plan'][$trigger['#ajax_id']]['account'];
2262
2263}
2264
2265
2266
2267function paf_type_expense_js($form, &$form_state) {
2268
2269
2270
2271 return $form['spending_plan'];
2272
2273}
2274
2275
2276
2277/**
2278
2279 *
2280
2281 * @param type $form
2282
2283 * @param type $form_state
2284
2285 * @return type
2286
2287 */
2288
2289function paf_add_financial_form($form, &$form_state) {
2290
2291
2292
2293 $node = $form_state['paf_node'];
2294
2295
2296
2297 //dpm($node, 'loaded_node');
2298
2299
2300
2301 /**
2302
2303 * Check to see if we have spending plans
2304
2305 */
2306
2307 if (isset($node->sp) && !empty($node->sp)) {
2308
2309
2310
2311 // Let's load all spending plans and not add overhead
2312
2313 //$sp_grand_total = 0;
2314
2315
2316
2317 foreach ($node->sp as $sp) {
2318
2319
2320
2321 $type_expenses_nids[$sp['type_of_expense']] = $sp['type_of_expense'];
2322
2323
2324
2325 //$sp_grand_total+=$sp['amount'];
2326
2327 }
2328
2329
2330
2331
2332
2333 $type_expenses = node_load_multiple($type_expenses_nids);
2334
2335
2336
2337 $fr_grand_total = 0;
2338
2339
2340
2341 foreach ($node->sp as $key => $sp) {
2342
2343
2344
2345 $nums = array();
2346
2347
2348
2349 // We load the spending plan
2350
2351
2352
2353 $form[$sp['id']] = array(
2354
2355 '#type' => 'fieldset',
2356
2357 '#title' => $type_expenses[$sp['type_of_expense']]->title . ' - ' . $sp['description'],
2358
2359 '#collapsible' => TRUE,
2360
2361 '#collapsed' => reset($node->sp) == $sp ? FALSE : TRUE,
2362
2363 "#tree" => TRUE,
2364
2365 );
2366
2367
2368
2369 //Get all availabe fr's and prepare NUM of invoices
2370
2371
2372
2373 if (isset($node->fr[$sp['id']])) {
2374
2375
2376
2377 // Loop trough current SP Financial Reconciliation nodes
2378
2379
2380
2381 foreach (array_keys($node->fr[$sp['id']]) as $k) {
2382
2383
2384
2385 $nums[$k] = $k;
2386
2387 }
2388
2389 }
2390
2391
2392
2393 // If no nums set default
2394
2395
2396
2397 if (empty($nums)) {
2398
2399
2400
2401 $nums = array(1 => 1);
2402
2403 }
2404
2405
2406
2407
2408
2409 // Get the num for the appropriate
2410
2411
2412
2413 $form_state['num'][$sp['id']] = isset($form_state['num'][$sp['id']]) ? $form_state['num'][$sp['id']] : $nums;
2414
2415
2416
2417 $form[$sp['id']]['fr'] = array(
2418
2419 "#theme" => "paf_form_table",
2420
2421 '#attributes' => array('id' => 'fr-table'),
2422
2423 '#headers' => array("CA Number", "Invoice Number", "Deliver", 'Amount', 'Actions'),
2424
2425 '#prefix' => '<div id="financial-form-wrapper-' . $sp['id'] . '">',
2426
2427 '#suffix' => '</div>'
2428
2429 );
2430
2431
2432
2433 $fr_total_amount = 0;
2434
2435
2436
2437 foreach ($form_state['num'][$sp['id']] as $i) {
2438
2439
2440
2441 $form[$sp['id']]['fr'][$i]['id'] = array(
2442
2443 '#type' => 'hidden',
2444
2445 '#default_value' => isset($node->fr[$sp['id']][$i]['id']) ? $node->fr[$sp['id']][$i]['id'] : 0
2446
2447 );
2448
2449
2450
2451 $form[$sp['id']]['fr'][$i]['ca_number'] = array(
2452
2453 '#type' => 'textfield',
2454
2455 '#default_value' => isset($node->fr[$sp['id']][$i]['ca_number']) ? $node->fr[$sp['id']][$i]['ca_number'] : ''
2456
2457 );
2458
2459
2460
2461 $form[$sp['id']]['fr'][$i]['invoice_number'] = array(
2462
2463 '#type' => 'textfield',
2464
2465 '#default_value' => isset($node->fr[$sp['id']][$i]['invoice_number']) ? $node->fr[$sp['id']][$i]['invoice_number'] : ''
2466
2467 );
2468
2469
2470
2471 $form[$sp['id']]['fr'][$i]['deliver'] = array(
2472
2473 '#type' => 'textfield',
2474
2475 '#default_value' => isset($node->fr[$sp['id']][$i]['deliver']) ? $node->fr[$sp['id']][$i]['deliver'] : ''
2476
2477 );
2478
2479
2480
2481 $form[$sp['id']]['fr'][$i]['amount'] = array(
2482
2483 '#type' => 'textfield',
2484
2485 '#default_value' => isset($node->fr[$sp['id']][$i]['amount']) ? $node->fr[$sp['id']][$i]['amount'] : ''
2486
2487 );
2488
2489
2490
2491 $form[$sp['id']]['fr'][$i]['actions'] = array(
2492
2493 '#type' => 'actions'
2494
2495 );
2496
2497
2498
2499 $form[$sp['id']]['fr'][$i]['actions']['remove'] = array(
2500
2501 '#type' => 'submit',
2502
2503 '#value' => t('Remove') . $sp['id'] . $i,
2504
2505 '#pre_render' => array('paf_fix_submit_trigger'),
2506
2507 '#submit' => array('paf_ajax_remove_invoice'),
2508
2509 '#attributes' => array('class' => array('remove-row-' . $i)),
2510
2511 '#ajax' => array(
2512
2513 'callback' => 'paf_finance_js',
2514
2515 'wrapper' => 'financial-form-wrapper-' . $sp['id'],
2516
2517 'method' => 'replace',
2518
2519 'effect' => 'fade'
2520
2521 )
2522
2523 );
2524
2525
2526
2527 $fr_total_amount += $form[$sp['id']]['fr'][$i]['amount']['#default_value'];
2528
2529 }
2530
2531
2532
2533 $fr_grand_total += $fr_total_amount;
2534
2535
2536
2537
2538
2539 $form[$sp['id']]['add_new'] = array(
2540
2541 '#pre_render' => array('paf_fix_add_new_trigger'),
2542
2543 '#type' => 'submit',
2544
2545 '#value' => 'add_new_' . $sp['id'],
2546
2547 '#submit' => array('paf_ajax_add_new_invoice'),
2548
2549 '#ajax' => array(
2550
2551 'callback' => 'paf_finance_js',
2552
2553 'wrapper' => 'financial-form-wrapper-' . $sp['id'],
2554
2555 'method' => 'replace',
2556
2557 'effect' => 'fade'
2558
2559 ),
2560
2561 );
2562
2563
2564
2565 #----------------------Budget -> Spent -> Balance form area
2566
2567
2568
2569 $form[$sp['id']]['info'] = array(
2570
2571 '#prefix' => '<div class="financial-balance">',
2572
2573 '#suffix' => '</div>'
2574
2575 );
2576
2577
2578
2579 $form[$sp['id']]['info']['balance_amount'] = array(
2580
2581 '#markup' => 'Budget : ' . round($sp['amount'], 2),
2582
2583 '#prefix' => '<div class="budget">',
2584
2585 '#suffix' => '</div>'
2586
2587 );
2588
2589
2590
2591 $form[$sp['id']]['info']['balance_spent'] = array(
2592
2593 '#markup' => 'Spent : ' . round($fr_total_amount, 2),
2594
2595 '#prefix' => '<div class="spent">',
2596
2597 '#suffix' => '</div>'
2598
2599 );
2600
2601
2602
2603 $form[$sp['id']]['info']['balance_left'] = array(
2604
2605 '#markup' => 'Balance : ' . round($sp['amount'] - $fr_total_amount, 2),
2606
2607 '#prefix' => '<div class="balance">',
2608
2609 '#suffix' => '</div>'
2610
2611 );
2612
2613
2614
2615 if (($sp['amount'] - $fr_total_amount) < 0) {
2616
2617
2618
2619 $form[$sp['id']]['info']['balance_left']['#prefix'] = '<div class="balance minus">';
2620
2621
2622
2623 $form[$sp['id']]['info']['balance_left']['#suffix'] = '</>';
2624
2625 }
2626
2627
2628
2629// $form[$sp['id']]['info']['balance_total'] = array(
2630
2631// '#markup' => ' Grand Total : ' . ($sp_grand_total - $fr_grand_total) . ' ',
2632
2633// );
2634
2635 #----------------------
2636
2637 }
2638
2639 } else {
2640
2641
2642
2643 $form['empty'] = array(
2644
2645 '#markup' => t('You haven\'t defined any spending plan')
2646
2647 );
2648
2649 }
2650
2651
2652
2653 return $form;
2654
2655}
2656
2657
2658
2659function paf_finance_js(&$form, &$form_state) {
2660
2661
2662
2663 $trigger = $form_state['triggering_element'];
2664
2665
2666
2667 $spid = $trigger['#array_parents'][0];
2668
2669
2670
2671 return $form[$spid]['fr'];
2672
2673}
2674
2675
2676
2677function paf_ajax_add_new_invoice(&$form, &$form_state) {
2678
2679
2680
2681 $trigger = $form_state['triggering_element'];
2682
2683
2684
2685 $spid = $trigger['#array_parents'][0];
2686
2687
2688
2689 $last = end($form_state['num'][$spid]);
2690
2691
2692
2693 $last++;
2694
2695
2696
2697 $form_state['num'][$spid][$last] = $last;
2698
2699
2700
2701 $form_state['rebuild'] = TRUE;
2702
2703}
2704
2705
2706
2707function paf_ajax_remove_invoice(&$form, &$form_state) {
2708
2709
2710
2711 $trigger = $form_state['triggering_element'];
2712
2713
2714
2715 $spid = $trigger['#array_parents'][0];
2716
2717
2718
2719 $i = $trigger['#array_parents'][2];
2720
2721
2722
2723 dd($trigger, 'spid_remove');
2724
2725
2726
2727 if (isset($form_state['num'][$spid][$i]) && count($form_state['num'][$spid]) > 1) {
2728
2729
2730
2731 unset($form_state['num'][$spid][$i]);
2732
2733
2734
2735 $form_state['rebuild'] = TRUE;
2736
2737 }
2738
2739}
2740
2741
2742
2743/**
2744
2745 *
2746
2747 * @param type $form
2748
2749 * @param type $form_state
2750
2751 */
2752
2753function paf_add_financial_form_validate(&$form, &$form_state) {
2754
2755
2756
2757}
2758
2759
2760
2761/**
2762
2763 *
2764
2765 * @param type $form
2766
2767 * @param type $form_state
2768
2769 */
2770
2771function paf_add_financial_form_submit(&$form, &$form_state) {
2772
2773
2774
2775 if (!isset($form_state['paf_node']) || !isset($form_state['paf_node']->nid)) {
2776
2777
2778
2779 $form_state['continue'] = FALSE;
2780
2781
2782
2783 drupal_set_message(t('Missing node data!'), 'error');
2784
2785
2786
2787 return;
2788
2789 }
2790
2791
2792
2793 $frs = paf_load_fr($form_state['paf_node']->nid);
2794
2795
2796
2797 $values = array();
2798
2799
2800
2801 foreach ($form_state['values'] as $key => $value) {
2802
2803
2804
2805 if (is_numeric($key) && isset($value['fr'])) {
2806
2807
2808
2809 $values[$key] = $value['fr'];
2810
2811 }
2812
2813 }
2814
2815
2816
2817
2818
2819 // we set the Financial Reconciliation
2820
2821
2822
2823 foreach ($values as $spid => $fr_values) {
2824
2825
2826
2827 foreach ($fr_values as $i => $value) {
2828
2829
2830
2831 if (!$value['ca_number'] || !$value['invoice_number'] || !$value['deliver'] || !$value['amount']) {
2832
2833
2834
2835 continue;
2836
2837 }
2838
2839
2840
2841 $data = array(
2842
2843 'paf_sp' => $spid,
2844
2845 'ca_number' => $value['ca_number'],
2846
2847 'invoice_number' => $value['invoice_number'],
2848
2849 'deliver' => $value['deliver'],
2850
2851 'amount' => $value['amount'],
2852
2853 'created' => REQUEST_TIME,
2854
2855 'changed' => REQUEST_TIME
2856
2857 );
2858
2859
2860
2861 if ($value['id'] != 0) {
2862
2863
2864
2865 $data['id'] = $value['id'];
2866
2867 }
2868
2869
2870
2871 $primary_keys = array();
2872
2873
2874
2875 foreach ($frs as $id => $frs2) {
2876
2877
2878
2879 if (isset($value['id']) && $frs2->id == $value['id']) {
2880
2881
2882
2883 unset($data['created']);
2884
2885
2886
2887 unset($frs[$id]);
2888
2889
2890
2891 $primary_keys = 'id';
2892
2893
2894
2895 break;
2896
2897 }
2898
2899 }
2900
2901
2902
2903
2904
2905 try {
2906
2907
2908
2909 drupal_write_record('paf_fr', $data, $primary_keys);
2910
2911 } catch (Exception $exc) {
2912
2913
2914
2915 watchdog_exception('paf', $exc);
2916
2917
2918
2919 drupal_set_message('There is an error saving paf data. Please review your changes', 'error');
2920
2921
2922
2923 $form_state['continue'] = FALSE;
2924
2925 }
2926
2927 }
2928
2929 }
2930
2931
2932
2933 if ($frs && !empty($frs)) {
2934
2935
2936
2937 db_delete('paf_fr')->condition('id', array_keys($frs))->execute();
2938
2939 }
2940
2941}
2942
2943
2944
2945/**
2946
2947 * Fixes triggering element flaw
2948
2949 * @param array $element
2950
2951 * @see https://drupal.org/node/1342066
2952
2953 * @return string
2954
2955 */
2956
2957function paf_fix_submit_trigger($element) {
2958
2959
2960
2961 $element['#value'] = 'Remove';
2962
2963
2964
2965 return $element;
2966
2967}
2968
2969
2970
2971/**
2972
2973 * Fixes triggering element flaw
2974
2975 * @param array $element
2976
2977 * @see https://drupal.org/node/1342066
2978
2979 * @return string
2980
2981 */
2982
2983function paf_fix_add_new_trigger($element) {
2984
2985
2986
2987 $element['#value'] = 'Add new invoice';
2988
2989
2990
2991 return $element;
2992
2993}
2994
2995
2996
2997/**
2998
2999 *
3000
3001 * @param type $form
3002
3003 * @param type $form_state
3004
3005 * @return boolean
3006
3007 */
3008
3009function paf_financial_form(&$form, &$form_state) {
3010
3011
3012
3013 if (!isset($form['#attached']['js'])) {
3014
3015
3016
3017 $form['#attached']['js'] = array();
3018
3019 }
3020
3021
3022
3023 $disabled = FALSE;
3024
3025
3026
3027 if (isset($form_state['paf_node']->nid)) {
3028
3029
3030
3031 $disabled = (bool)(!_is_paf_author($form_state['paf_node']));
3032
3033 }
3034
3035
3036
3037 // Generate a variable to store financial data.
3038
3039
3040
3041 $financial = array();
3042
3043
3044
3045 if (isset($form_state['entity']->financial)) {
3046
3047
3048
3049 $financial = $form_state['entity']->financial;
3050
3051 }
3052
3053
3054
3055 $form['#attached']['js'][] = drupal_get_path('module', 'paf') . '/js/paf_form.js';
3056
3057
3058
3059 $form['financial'] = array(
3060
3061 '#type' => 'fieldset',
3062
3063 '#title' => t('Key Financials'),
3064
3065 '#collapsible' => TRUE,
3066
3067 '#collapsed' => FALSE,
3068
3069 '#tree' => TRUE,
3070
3071 '#attributes' => array('class' => array('paf-key-financials')),
3072
3073 '#weight' => 100,
3074
3075 '#prefix' => '<div class="paf-key-financials-wrapper">',
3076
3077 '#suffix' => '</div>',
3078
3079 '#disabled' => $disabled,
3080
3081 //##
3082
3083 'nts' => array(
3084
3085 '#type' => 'textfield',
3086
3087// '#title' => t('Numbers to be sold'),
3088
3089 '#title' => t('Kgs to be sold'),
3090
3091 '#size' => 14,
3092
3093 '#test' => 'test',
3094
3095 '#default_value' => isset($financial['nts']) ? $financial['nts'] : '',
3096
3097 '#element_validate' => array('element_validate_number')
3098
3099 ),
3100
3101 'gpps' => array(
3102
3103 '#type' => 'textfield',
3104
3105// '#title' => t('Gross price per SKU'),
3106
3107 '#title' => t('Gross price per kg'),
3108
3109 '#size' => 14,
3110
3111 '#default_value' => isset($financial['gpps']) ? $financial['gpps'] : '',
3112
3113 '#element_validate' => array('element_validate_number')
3114
3115 ),
3116
3117 'net_sales' => array(
3118
3119 '#type' => 'textfield',
3120
3121 '#title' => t('Net Sales (BGN/kg)'),
3122
3123 '#size' => 14,
3124
3125 '#disabled' => TRUE,
3126
3127 '#default_value' => isset($financial['net_sales']) ? $financial['net_sales'] : '',
3128
3129 '#element_validate' => array('element_validate_number')
3130
3131 ),
3132
3133 'cgs' => array(
3134
3135 '#type' => 'textfield',
3136
3137 '#title' => t('Cost of Goods Sold (BGN/kg)'),
3138
3139 '#size' => 14,
3140
3141 '#default_value' => isset($financial['cgs']) ? $financial['cgs'] : '',
3142
3143 '#element_validate' => array('element_validate_number')
3144
3145 ),
3146
3147 'sap' => array(
3148
3149 '#type' => 'textfield',
3150
3151 '#title' => t('Standard Available Profit (BGN/kg)'),
3152
3153 '#size' => 14,
3154
3155 '#disabled' => TRUE,
3156
3157 '#default_value' => isset($financial['sap']) ? $financial['sap'] : '',
3158
3159 '#element_validate' => array('element_validate_number')
3160
3161 ),
3162
3163 'fpc' => array(
3164
3165 '#type' => 'textfield',
3166
3167 '#title' => t('Fixed promo costs (BGN)'),
3168
3169 '#size' => 14,
3170
3171 '#default_value' => isset($financial['fpc']) ? $financial['fpc'] : '',
3172
3173 '#element_validate' => array('element_validate_number')
3174
3175 ),
3176
3177 'vpc' => array(
3178
3179 '#type' => 'textfield',
3180
3181 '#title' => t('Variable promo costs (BGN)'),
3182
3183 '#size' => 14,
3184
3185 '#default_value' => isset($financial['vpc']) ? $financial['vpc'] : '',
3186
3187 '#element_validate' => array('element_validate_number')
3188
3189 ),
3190
3191 'vpckg' => array(
3192
3193 '#type' => 'textfield',
3194
3195 '#title' => t('Variable promo costs (BGN/kg)'),
3196
3197 '#size' => 14,
3198
3199 '#disabled' => TRUE,
3200
3201 '#default_value' => isset($financial['vpckg']) ? $financial['vpckg'] : '',
3202
3203 '#element_validate' => array('element_validate_number')
3204
3205 ),
3206
3207 'pap' => array(
3208
3209 '#type' => 'textfield',
3210
3211 '#title' => t('Promo Available Profit (BGN/kg)'),
3212
3213 '#size' => 14,
3214
3215 '#disabled' => TRUE,
3216
3217 '#default_value' => isset($financial['pap']) ? $financial['pap'] : '',
3218
3219 '#element_validate' => array('element_validate_number')
3220
3221 ),
3222
3223 'plan' => array(
3224
3225 '#type' => 'textfield',
3226
3227 '#title' => t('Plan @year in (kg)', array('@year' => date('Y'))),
3228
3229 '#size' => 14,
3230
3231 '#disabled' => TRUE,
3232
3233 '#default_value' => isset($financial['plan']) ? $financial['plan'] : '',
3234
3235 '#element_validate' => array('element_validate_number')
3236
3237 ),
3238
3239 'vr' => array(
3240
3241 '#type' => 'textfield',
3242
3243 '#title' => t('Volume at risk'),
3244
3245 '#size' => 14,
3246
3247 '#disabled' => TRUE,
3248
3249 '#default_value' => isset($financial['vr']) ? $financial['vr'] : '',
3250
3251 '#element_validate' => array('element_validate_number')
3252
3253 ),
3254
3255 'bv' => array(
3256
3257 '#type' => 'textfield',
3258
3259 '#title' => t('Baseline volume (kg) (if no promo)'),
3260
3261 '#size' => 14,
3262
3263 '#disabled' => TRUE,
3264
3265 '#default_value' => isset($financial['bv']) ? $financial['bv'] : '',
3266
3267 '#element_validate' => array('element_validate_number')
3268
3269 ),
3270
3271 'pav' => array(
3272
3273 '#type' => 'textfield',
3274
3275 '#title' => t('Promo Additional Volume (kg)'),
3276
3277 '#size' => 14,
3278
3279 '#disabled' => TRUE,
3280
3281 '#default_value' => isset($financial['pav']) ? $financial['pav'] : '',
3282
3283 '#element_validate' => array('element_validate_number')
3284
3285 ),
3286
3287 'avpb' => array(
3288
3289 '#type' => 'textfield',
3290
3291 '#title' => t('Additional volume as % of Baseline'),
3292
3293 '#size' => 14,
3294
3295 '#default_value' => isset($financial['avpb']) ? $financial['avpb'] : '',
3296
3297 '#disabled' => TRUE,
3298
3299 '#field_suffix' => '%',
3300
3301 '#element_validate' => array('element_validate_number')
3302
3303 ),
3304
3305 'ipm' => array(
3306
3307 '#type' => 'textfield',
3308
3309 '#title' => t('Incremental promo margin (BGN)'),
3310
3311 '#size' => 14,
3312
3313 '#disabled' => TRUE,
3314
3315 '#default_value' => isset($financial['ipm']) ? $financial['ipm'] : '',
3316
3317 '#element_validate' => array('element_validate_number')
3318
3319 ),
3320
3321 'tpc' => array(
3322
3323 '#type' => 'textfield',
3324
3325 '#title' => t('Total Promo costs (BGN)'),
3326
3327 '#size' => 14,
3328
3329 '#disabled' => TRUE,
3330
3331 '#default_value' => isset($financial['tpc']) ? $financial['tpc'] : '',
3332
3333 '#element_validate' => array('element_validate_number')
3334
3335 ),
3336
3337 'iOC' => array(
3338
3339 '#type' => 'textfield',
3340
3341 '#title' => t('Incremental OC (BGN)'),
3342
3343 '#size' => 14,
3344
3345 '#disabled' => TRUE,
3346
3347 '#default_value' => isset($financial['iOC']) ? $financial['iOC'] : '',
3348
3349 '#element_validate' => array('element_validate_number')
3350
3351 ),
3352
3353 'bev' => array(
3354
3355 '#type' => 'textfield',
3356
3357 '#title' => t('Break-Even Volume (kg)'),
3358
3359 '#size' => 14,
3360
3361 '#disabled' => TRUE,
3362
3363 '#default_value' => isset($financial['bev']) ? $financial['bev'] : '',
3364
3365 '#element_validate' => array('element_validate_number')
3366
3367 ),
3368
3369 'rov' => array(
3370
3371 '#type' => 'textfield',
3372
3373 '#title' => t('Return on Investment (%)'),
3374
3375 '#size' => 14,
3376
3377 '#disabled' => TRUE,
3378
3379 '#field_suffix' => '%',
3380
3381 '#default_value' => isset($financial['rov']) ? $financial['rov'] : '',
3382
3383 '#element_validate' => array('element_validate_number')
3384
3385 ),
3386
3387 'cip' => array(
3388
3389 '#type' => 'hidden',
3390
3391 '#value' => 0,
3392
3393 '#title' => t('Cannibalized ISBG product'),
3394
3395 '#size' => 14,
3396
3397 '#default_value' => isset($financial['cip']) ? $financial['cip'] : '',
3398
3399 ),
3400
3401 'cannibalization' => array(
3402
3403 '#type' => 'hidden',
3404
3405 '#value' => 0,
3406
3407 '#title' => t('Cannibalization'),
3408
3409 '#size' => 14,
3410
3411 '#default_value' => isset($financial['cannibalization']) ? $financial['cannibalization'] : '',
3412
3413 '#field_suffix' => '%',
3414
3415 '#element_validate' => array('element_validate_number'),
3416
3417 ),
3418
3419 'canv' => array(
3420
3421 '#type' => 'hidden',
3422
3423 '#value' => 0,
3424
3425 '#title' => t('Cannibalized volume (kg)'),
3426
3427 '#size' => 14,
3428
3429 '#default_value' => isset($financial['canv']) ? $financial['canv'] : '',
3430
3431 '#element_validate' => array('element_validate_number')
3432
3433 ),
3434
3435 'canm' => array(
3436
3437 '#type' => 'hidden',
3438
3439 '#value' => 0,
3440
3441 '#title' => t('Cannibalized margin'),
3442
3443 '#size' => 14,
3444
3445 '#default_value' => isset($financial['canm']) ? $financial['canm'] : '',
3446
3447 '#element_validate' => array('element_validate_number')
3448
3449 ),
3450
3451 );
3452
3453
3454
3455 //We need to pass elements that JS should watch for changes
3456
3457
3458
3459 $elements = array();
3460
3461
3462
3463 foreach ($form['financial'] as $key => $element) {
3464
3465
3466
3467 if (strpos($key, '#') === FALSE) {
3468
3469
3470
3471 $elements[] = $key;
3472
3473
3474
3475 $js_field_data[$key]['set'] = (bool)$element['#default_value'];
3476
3477 }
3478
3479 }
3480
3481
3482
3483 drupal_add_js(array(
3484
3485 'paf' => array(
3486
3487 'financials' => $elements,
3488
3489 'field_data' => $js_field_data
3490
3491 )
3492
3493 ), 'setting');
3494
3495
3496 return $form;
3497
3498}
3499
3500
3501
3502function paf_add_timing_form($form, &$form_state) {
3503
3504
3505 //var_dump($form, $form_state);
3506
3507 $table_lenght = array();
3508
3509
3510
3511 //dpm($form_state, 'timing');
3512
3513
3514
3515 $disabled = (bool)(!_is_paf_author($form_state['paf_node']));
3516
3517
3518
3519 $date = date('Y-m-d');
3520
3521
3522
3523 $form['timing'] = array(
3524
3525 '#prefix' => '<div id="timing-wrapper">',
3526
3527 '#suffix' => '</div>',
3528
3529 '#tree' => TRUE,
3530
3531 '#disabled' => $disabled
3532
3533 );
3534
3535// unset($form_state['paf_node']->timing);
3536// paf_add_set_cache('node', $form_state['paf_node']);
3537
3538
3539
3540 $timings = isset($form_state['paf_node']->timing) ? $form_state['paf_node']->timing : array();
3541
3542
3543
3544 if (!$timings) {
3545
3546
3547
3548 $num = array(1 => 1);
3549
3550 } else {
3551
3552
3553
3554 foreach (array_keys($timings) as $key) {
3555
3556
3557
3558 $num[$key] = $key;
3559
3560 }
3561
3562 }
3563
3564
3565
3566 $form_state['num'] = isset($form_state['num']) ? $form_state['num'] : $num;
3567
3568
3569
3570 $format = "Y-m-d";
3571
3572
3573
3574 foreach ($form_state['num'] as $i) {
3575
3576
3577
3578 $form['timing'][$i] = array(
3579
3580 '#type' => 'fieldset',
3581
3582 '#title' => isset($form_state['values']['timing'][$i]['resource']) ?
3583
3584 $form_state['values']['timing'][$i]['resource'] : t('New milestone'),
3585
3586 '#disabled' => $disabled,
3587
3588 '#required' => TRUE
3589
3590 );
3591
3592
3593
3594
3595
3596 $resource_value = '';
3597
3598
3599
3600 if (isset($form_state['values']['timing'][$i]['resource'])) {
3601
3602
3603
3604 $resource_value = $form_state['values']['timing'][$i]['resource'];
3605
3606 } elseif (isset($timings[$i])) {
3607
3608
3609
3610 $resource_value = $timings[$i]->resource;
3611
3612 }
3613
3614
3615
3616 $form['timing'][$i]['resource'] = array(
3617
3618 '#type' => 'textfield',
3619
3620 '#title' => 'Milestone Name',
3621
3622 '#default_value' => $resource_value,
3623
3624 '#required' => TRUE
3625
3626 );
3627
3628
3629
3630
3631
3632 $start_value = $date;
3633
3634
3635
3636 if (isset($form_state['values']['timing'][$i]['start'])) {
3637
3638
3639
3640 $start_value = $form_state['values']['timing'][$i]['start'];
3641
3642 } elseif (isset($timings[$i])) {
3643
3644
3645
3646 $start_value = date($format, $timings[$i]->start);
3647
3648 }
3649
3650
3651
3652 $form['timing'][$i]['start'] = array(
3653
3654 '#type' => 'date_popup',
3655
3656 '#title' => 'Select Start Date',
3657
3658 '#default_value' => $start_value,
3659
3660 '#date_format' => $format,
3661
3662 '#required' => TRUE
3663
3664 );
3665
3666
3667
3668 $end_value = $date;
3669
3670
3671
3672 if (isset($form_state['values']['timing'][$i]['end'])) {
3673
3674
3675
3676 $end_value = $form_state['values']['timing'][$i]['end'];
3677
3678 } elseif (isset($timings[$i])) {
3679
3680
3681
3682 $end_value = date($format, $timings[$i]->end);
3683
3684 }
3685
3686
3687
3688
3689
3690 $form['timing'][$i]['end'] = array(
3691
3692 '#type' => 'date_popup',
3693
3694 '#title' => 'Select End Date',
3695
3696 '#default_value' => $end_value,
3697
3698 '#date_format' => $format,
3699
3700 '#required' => TRUE
3701
3702 );
3703
3704
3705
3706 $form['timing'][$i]['remove'] = array(
3707
3708 '#type' => 'submit',
3709
3710 '#value' => t('Remove') . $i,
3711
3712 '#submit' => array('paf_add_timing_remove'),
3713
3714 '#pre_render' => array('paf_fix_submit_trigger'),
3715
3716 '#ajax' => array(
3717
3718 'callback' => 'paf_add_timing_js',
3719
3720 'wrapper' => 'timing-wrapper',
3721
3722 'method' => 'replace',
3723
3724 'effect' => 'fade',
3725
3726 )
3727
3728 );
3729
3730
3731
3732 //Construct data array to be passed into data.js for dates in ganttCalendar
3733 $d = null;
3734 $d_end = null;
3735
3736 if (!isset($timings[$i])) {
3737 $start_m = null;
3738 $end_m = null;
3739 $start_y = null;
3740 $end_y = null;
3741 }
3742 else {
3743 $start_m = date('m', $timings[$i]->start - 1);
3744 $end_m = date('m', $timings[$i]->end) - 1;
3745 $start_y = date('Y', $timings[$i]->start);
3746 $end_y = date('Y', $timings[$i]->end);
3747 $d = date('d', $timings[$i]->start);
3748 $d_end = date('d', $timings[$i]->end);
3749 }
3750 $data[] = array(
3751
3752 'name' => $resource_value,
3753
3754 'start' => array(
3755
3756 'y' => $start_y,
3757
3758 'm' => (string)$start_m,
3759
3760 'd' => $d
3761
3762 ),
3763
3764 'end' => array(
3765
3766 'y' => $end_y,
3767
3768 'm' => (string)$end_m,
3769
3770 'd' => $d_end,
3771
3772 ),
3773
3774 );
3775
3776
3777
3778// $m_diff[] = $end_m - $start_m;
3779
3780
3781
3782
3783
3784 if (($end_y - $start_y) === 0 && ($end_m - $start_m) < 4) {
3785
3786
3787
3788 $m_diff[] = $end_m - $start_m;
3789
3790 } else {
3791
3792
3793
3794 $m_diff[] = 4;
3795
3796 }
3797
3798 }
3799
3800
3801
3802 $form['timing']['add_more'] = array(
3803
3804 '#type' => 'submit',
3805
3806 '#value' => t('Add new'),
3807
3808 '#submit' => array('paf_add_timing_add_more'),
3809
3810 '#limit_validation_errors' => array(),
3811
3812 '#ajax' => array(
3813
3814 'callback' => 'paf_add_timing_js',
3815
3816 'wrapper' => 'timing-wrapper',
3817
3818 'method' => 'replace',
3819
3820 'effect' => 'fade',
3821
3822 )
3823
3824 );
3825
3826
3827
3828
3829
3830#----------------Schedule Calendar
3831
3832 if (array_key_exists('values', $form_state)) {
3833 if (!is_null($form_state['values']['timing']) && !is_null($form_state['num'][1] === 1)) {
3834 return $form;
3835 }
3836 }
3837
3838
3839
3840 $max = max($m_diff);
3841
3842
3843
3844
3845
3846// if ($max === 0) {
3847
3848// $len = 500;
3849
3850// } else {
3851
3852// $len = $max * 175;
3853
3854// }
3855
3856
3857
3858
3859
3860 $form['#attached']['js'][] = drupal_get_path('module', 'paf') . '/js/paf_gantt.js';
3861
3862
3863
3864 $form['gantt'] = array(
3865
3866 '#prefix' => '<div id="ganttChart">',
3867
3868 '#suffix' => '</div>',
3869
3870 '#weight' => 10000,
3871
3872 );
3873
3874
3875
3876 drupal_add_library('paf', 'ganttCalendar');
3877
3878
3879
3880
3881
3882 drupal_add_js(array('paf' => array('data' => $data, 'len' => $max)), array('type' => 'setting'));
3883
3884
3885
3886 drupal_add_js(drupal_get_path('module', 'paf') . '/js/paf_gantt_data.js');
3887
3888
3889
3890
3891
3892#--------------------------------
3893
3894
3895
3896
3897
3898 return $form;
3899
3900}
3901
3902
3903
3904/**
3905
3906 * Validation handler for paf timing.
3907
3908 * @param type $form
3909
3910 * @param type $form_state
3911
3912 */
3913
3914function paf_add_timing_form_validate(&$form, &$form_state) {
3915
3916
3917
3918 $vals = &$form_state['values'];
3919
3920
3921
3922 if (isset($vals['timing']['add_more'])) {
3923
3924
3925
3926 unset($vals['timing']['add_more']);
3927
3928 }
3929
3930
3931
3932 foreach ($vals['timing'] as $id => &$row) {
3933
3934
3935
3936 unset($row['remove']);
3937
3938
3939
3940 $row['start'] = strtotime($row['start']);
3941
3942
3943
3944 $row['end'] = strtotime($row['end']);
3945
3946
3947
3948 if ($row['resource'] || $row['start'] || $row['end']) {
3949
3950
3951
3952 if (!$row['resource']) {
3953
3954
3955
3956 form_set_error('timing][' . $id . '][resource', t("Resource field can't be empty."));
3957
3958 }
3959
3960
3961
3962 if (!$row['start']) {
3963
3964
3965
3966 form_set_error('timing][' . $id . '][start', t("Start date field can't be empty."));
3967
3968 }
3969
3970
3971
3972 if (!$row['end']) {
3973
3974
3975
3976 form_set_error('timing][' . $id . '][end', t("End date field can't be empty."));
3977
3978 }
3979
3980 } else {
3981
3982
3983
3984 unset($vals['timing'][$id]);
3985
3986 }
3987
3988
3989
3990
3991
3992 if ($row['start'] > $row['end']) {
3993
3994
3995
3996 form_set_error('timing][' . $id . '][end', t('The start date is greater than the end one!'));
3997
3998 }
3999
4000 }
4001
4002}
4003
4004
4005
4006/**
4007
4008 * Submit handler for paf wizard step timing.
4009
4010 * @param type $form
4011
4012 * @param type $form_state
4013
4014 */
4015
4016function paf_add_timing_form_submit(&$form, &$form_state) {
4017
4018
4019
4020 $form_state['node'] = &$form_state['paf_node'];
4021
4022
4023
4024 $vals = $form_state['values'];
4025
4026
4027
4028 $values = array(
4029
4030 'nid' => $form_state['paf_node']->nid,
4031
4032 'changed' => REQUEST_TIME
4033
4034 );
4035
4036
4037
4038 foreach ($vals['timing'] as $id => $row) {
4039
4040
4041
4042 $row['resource'] = check_plain($row['resource']);
4043
4044
4045
4046 $values = array_merge($values, $row);
4047
4048
4049
4050 if (isset($form_state['node']->timing) && array_key_exists($id, $form_state['node']->timing)) {
4051
4052
4053
4054 $values['id'] = $id;
4055
4056
4057
4058 $primary_keys = 'id';
4059
4060 } else {
4061
4062
4063
4064 $values['created'] = REQUEST_TIME;
4065
4066
4067
4068 $primary_keys = array();
4069
4070 }
4071
4072
4073
4074 try {
4075
4076
4077
4078 drupal_write_record('paf_timing', $values, $primary_keys);
4079
4080 } catch (Exception $exc) {
4081
4082
4083
4084 drupal_set_message(t('There was an error saving the PAF. Please try again and if the problem perssist please contact us.'), 'error');
4085
4086
4087
4088 $form_state['continue'] = FALSE;
4089
4090
4091
4092// dpm($exc);
4093
4094 }
4095
4096
4097
4098 //we need to save the inserted values so we can populate the node object
4099
4100
4101
4102 $timing[$values['id']] = (object)$values;
4103
4104
4105
4106 //remove id
4107
4108
4109
4110 unset($values['id']);
4111
4112 }
4113
4114
4115
4116 //We need to clean removed nodes
4117
4118
4119
4120 if (isset($form_state['node']->timing) && !empty($timing)) {
4121
4122
4123
4124 $deleted = array_diff_key($form_state['node']->timing, $timing);
4125
4126
4127
4128 foreach ($deleted as $del) {
4129
4130
4131
4132 db_delete('paf_timing')
4133
4134 ->condition('id', $del->id)
4135
4136 ->execute();
4137
4138
4139
4140 unset($form_state['node']->timing[$del->id]);
4141
4142 }
4143
4144 }
4145
4146
4147
4148 if (!empty($timing)) {
4149
4150
4151
4152 $form_state['node']->timing = $timing;
4153
4154 }
4155
4156
4157
4158 $form_state['rebuild'] = TRUE;
4159
4160}
4161
4162
4163
4164function paf_add_timing_js(&$form, &$form_state) {
4165
4166
4167
4168
4169
4170 return $form['timing'];
4171
4172}
4173
4174
4175
4176/**
4177
4178 * Ajax callback to add new milestones
4179
4180 * @param type $form
4181
4182 * @param boolean $form_state
4183
4184 */
4185
4186function paf_add_timing_add_more(&$form, &$form_state) {
4187
4188
4189
4190 //dpm($form_state);
4191
4192
4193
4194 $last = end($form_state['num']);
4195
4196
4197
4198 $last++;
4199
4200
4201
4202 $form_state['num'][$last] = $last;
4203
4204
4205
4206 $form_state['rebuild'] = TRUE;
4207
4208}
4209
4210
4211
4212function paf_add_timing_remove(&$form, &$form_state) {
4213
4214
4215
4216 $trigger = $form_state['triggering_element']['#parents'];
4217
4218
4219
4220
4221
4222 if (isset($form_state['num'][$trigger[1]]) && count($form_state['num']) > 1) {
4223
4224
4225
4226 unset($form_state['num'][$trigger[1]]);
4227
4228
4229
4230 dd($form_state['num']);
4231
4232
4233
4234 $form_state['rebuild'] = TRUE;
4235
4236 }
4237
4238}
4239
4240
4241
4242/**
4243
4244 * Implements hook_paf_extend_field().
4245
4246 * @param type $field
4247
4248 * @param type $form_state
4249
4250 */
4251
4252function _paf_paf_extend_field_alter(&$field, &$form_state, $field_name, $context) {
4253
4254
4255
4256}
4257
4258
4259
4260/**
4261
4262 *
4263
4264 * @param type $form_state
4265
4266 */
4267
4268function paf_add_next(&$form_state) {
4269
4270
4271
4272 //dpm($form_state, 'next');
4273
4274
4275
4276 /**
4277
4278 * We need to check if the trigerring element is not the back button
4279
4280 * There is a bug in CTools
4281
4282 * @see https://drupal.org/node/1294744
4283
4284 */
4285
4286 $direction = $form_state['triggering_element']['#parents'][0];
4287
4288
4289
4290 if ($direction == 'previous') {
4291
4292
4293
4294 paf_add_back($form_state);
4295
4296
4297
4298 return;
4299
4300 }
4301
4302
4303
4304 if ($form_state['step'] == 'general' || $form_state['step'] == 'second') {
4305
4306
4307
4308 if (isset($form_state['node'])) {
4309
4310
4311
4312 paf_add_set_cache('node', $form_state['node']);
4313
4314 }
4315
4316 }
4317
4318
4319
4320 if (isset($form_state['next']) && isset($form_state['continue']) && TRUE == $form_state['continue']) {
4321
4322
4323
4324 $form_state['redirect'] = ctools_wizard_get_path($form_state['form_info'], $form_state['next']);
4325
4326 } else {
4327
4328
4329
4330 $form_state['redirect'] = ctools_wizard_get_path($form_state['form_info'], $form_state['step']);
4331
4332 }
4333
4334
4335
4336 // We need to override the redirect if this is an approval pressing the next
4337
4338 // button. The button is used to save comments
4339
4340
4341
4342 if (isset($form_state['approval']->uid)) {
4343
4344
4345
4346 $form_state['redirect'] = ctools_wizard_get_path($form_state['form_info'], $form_state['step']);
4347
4348 }
4349
4350}
4351
4352
4353
4354/**
4355
4356 *
4357
4358 * @param type $form_state
4359
4360 */
4361
4362function paf_add_back(&$form_state) {
4363
4364
4365
4366 //dpm($form_state, 'back');
4367
4368
4369
4370 if (isset($form_state['step']) && function_exists("paf_add_{$form_state['step']}_submit")) {
4371
4372
4373
4374 }
4375
4376
4377
4378 // $form_state['redirect'] = ctools_wizard_get_path($form_state['form_info'], $form_state['next']);
4379
4380 // dpm($form_state);
4381
4382}
4383
4384
4385
4386/**
4387
4388 * Wizsard finish callback.
4389
4390 * @param array $form_state
4391
4392 */
4393
4394function paf_add_finish(&$form_state) {
4395
4396
4397
4398 paf_add_clear_cache('node');
4399
4400
4401
4402 $form_state['redirect'] = 'pafs';
4403
4404}
4405
4406
4407
4408/**
4409
4410 * Send for approval submit handler
4411
4412 * @param type $form
4413
4414 * @param type $form_state
4415
4416 */
4417
4418function paf_send_for_approval(&$form, &$form_state) {
4419
4420
4421
4422 //We need to submit any changes so we call the submit handler of the step.
4423
4424
4425
4426 if (isset($form_state['step']) && function_exists("paf_add_{$form_state['step']}_form_submit")) {
4427
4428
4429
4430 //Set explicitly that we clicked next. After that remove it.
4431
4432
4433
4434 $form_state['clicked_button']['#wizard type'] = 'next';
4435
4436
4437
4438 $func = "paf_add_{$form_state['step']}_form_submit";
4439
4440
4441
4442 $func($form, $form_state);
4443
4444
4445
4446 unset($form_state['clicked_button']['#wizard type']);
4447
4448
4449
4450 if (isset($form_state['continue']) && $form_state['continue'] === TRUE) {
4451
4452
4453
4454 $vals = $form_state['values'];
4455
4456
4457
4458 // Get the node from cache or from form_state.
4459
4460
4461
4462 if (isset($form_state['node'])) {
4463
4464
4465
4466 $node = $form_state['node'];
4467
4468 } else {
4469
4470
4471
4472 if (isset($form_state['paf_node'])) {
4473
4474
4475
4476 $node = $form_state['paf_node'];
4477
4478 }
4479
4480 }
4481
4482
4483
4484 //Check to see if there is a node
4485
4486
4487
4488 if (isset($node, $node->nid)) {
4489
4490
4491
4492 $node_wrapper = entity_metadata_wrapper('node', $node);
4493
4494
4495
4496 $node_wrapper->field_sent_for_approval->set(1);
4497
4498
4499
4500 $node_wrapper->field_paf_approved->set(PAF_NODE_WAITING_APPROVE);
4501
4502
4503
4504 try {
4505
4506
4507
4508 $node_wrapper->save();
4509
4510 } catch (Exception $exc) {
4511
4512
4513
4514 $form_state['rebuild'] = TRUE;
4515
4516
4517
4518 drupal_set_message(t('There was an error sending the PAF for approval. Please try again and if the problem perssist contac us.'), 'error');
4519
4520
4521
4522 watchdog_exception('paf', $exc);
4523
4524 }
4525
4526
4527
4528 drupal_set_message(t('The paf %name is sent for approval.', array(
4529
4530 '%name' => check_plain($node_wrapper->field_project_number->value())
4531
4532 )));
4533
4534
4535
4536 // Check to see if we are sending for approval for the first time
4537
4538 // and act accordignly
4539
4540
4541
4542 if (isset($form_state['clicked_button']['#parents'][0]) &&
4543
4544 $form_state['clicked_button']['#parents'][0] == 'resend_approve') {
4545
4546
4547
4548 if (!isset($node->approvals) || !is_array($node->approvals)) {
4549
4550
4551
4552 drupal_set_message(t('There was a problem resending the PAF for approval. Please try again.', 'error'));
4553
4554
4555
4556 return;
4557
4558 }
4559
4560
4561
4562 // We get the approval which returned the PAF
4563
4564
4565
4566 foreach ($node->approvals as $approval) {
4567
4568
4569
4570 if ($approval->status == PAF_APPROVAL_RETURNED) {
4571
4572
4573
4574 $active_approval = (array)$approval;
4575
4576
4577
4578 // $active_approval['user'] = user_load($approval->uid);
4579
4580
4581
4582 break;
4583
4584 }
4585
4586 }
4587
4588
4589
4590 // We apperantly made something terribly wrong. This shouldn't happen
4591
4592
4593
4594 if (!isset($active_approval)) {
4595
4596
4597
4598 drupal_set_message(t('There was a problem resending the PAF for approval. Please try again.', 'error'));
4599
4600
4601
4602 return;
4603
4604 }
4605
4606
4607
4608
4609
4610 // Set the approval to state Waiting
4611
4612
4613
4614 $active_approval['status'] = PAF_APPROVAL_WAITING;
4615
4616
4617
4618 try {
4619
4620
4621
4622 drupal_write_record('paf_approvals', $active_approval, array('id'));
4623
4624 } catch (Exception $exc) {
4625
4626
4627
4628 drupal_set_message(t('There was an error sending the PAF for approval. Please try again and if the problem perssist contac us.'), 'error');
4629
4630
4631
4632 watchdog_exception('paf', $exc);
4633
4634 }
4635
4636
4637
4638
4639
4640 $node_wrapper->field_paf_approved->set(PAF_NODE_WAITING_APPROVE);
4641
4642
4643
4644 $node_wrapper->field_sent_for_approval->set(1);
4645
4646
4647
4648 $node_wrapper->save();
4649
4650
4651
4652
4653
4654 paf_msg_resend(user_load($active_approval['uid']), $node, user_load($node->uid));
4655
4656
4657
4658 $form_state['redirect'] = 'pafs';
4659
4660
4661
4662 return;
4663
4664 } else {
4665
4666
4667
4668 // We are sending for approval the first time
4669
4670 //Lets save the paf_approvals
4671
4672
4673
4674 $approvals_ref = $node_wrapper->field_approval_ref->value();
4675
4676
4677
4678 $approvals_wrapper = entity_metadata_wrapper('node', $approvals_ref);
4679
4680
4681
4682 $approvals = $approvals_wrapper->field_approvals->value();
4683
4684
4685
4686
4687
4688 //Set the sent for approval on date
4689
4690
4691
4692 $node_wrapper->field_presented_for_approval_on->set(REQUEST_TIME);
4693
4694
4695
4696 $node_wrapper->save();
4697
4698
4699
4700 // We check to see if approvals aren't already defined.
4701
4702
4703
4704 if (!isset($node->approvals) || !is_array($node->approvals)) {
4705
4706
4707
4708 $first_approval = reset($approvals);
4709
4710
4711
4712 foreach ($approvals as $delta => $approval) {
4713
4714
4715
4716 $data = array(
4717
4718 'nid' => $node->nid,
4719
4720 'uid' => $approval->uid,
4721
4722 'weight' => $delta,
4723
4724 'status' => $approval === $first_approval ? PAF_APPROVAL_WAITING : PAF_APPROVAL_PENDING
4725
4726 );
4727
4728
4729
4730 try {
4731
4732
4733
4734 drupal_write_record('paf_approvals', $data);
4735
4736 } catch (Exception $exc) {
4737
4738
4739
4740 $form_state['rebuild'] = TRUE;
4741
4742
4743
4744 drupal_set_message(t('There was an error sending the PAF for approval. Please try again and if the problem perssist contac us.'), 'error');
4745
4746
4747
4748 watchdog_exception('paf', $exc);
4749
4750
4751
4752 $node_wrapper->field_sent_for_approval->set(0);
4753
4754 }
4755
4756 }
4757
4758 }
4759
4760
4761
4762 paf_add_clear_cache('node');
4763
4764
4765
4766 paf_msg_approval(reset($approvals), $node);
4767
4768
4769
4770 $form_state['rebuild'] = TRUE;
4771
4772
4773
4774 $form_state['paf_node'] = $node;
4775
4776
4777
4778 //paf_msg_create($node);
4779
4780 //$form_state['redirect'] = 'pafs';
4781
4782 }
4783
4784 }
4785
4786 }
4787
4788 }
4789
4790}
4791
4792
4793
4794/**
4795
4796 *
4797
4798 * @param type $name
4799
4800 * @return \stdClass
4801
4802 */
4803
4804function paf_add_get_cache($name) {
4805
4806
4807
4808 ctools_include('object-cache');
4809
4810
4811
4812 $cache = ctools_object_cache_get(PAF_CACHE_KEY, $name);
4813
4814
4815
4816 if (!$cache) {
4817
4818
4819
4820 $cache = new stdClass();
4821
4822
4823
4824 $cache->locked = ctools_object_cache_test(PAF_CACHE_KEY, $name);
4825
4826 }
4827
4828
4829
4830
4831
4832 return $cache;
4833
4834}
4835
4836
4837
4838/**
4839
4840 *
4841
4842 * @param type $name
4843
4844 * @param type $data
4845
4846 */
4847
4848function paf_add_set_cache($name, $data) {
4849
4850
4851
4852 ctools_include('object-cache');
4853
4854
4855
4856 $cache = ctools_object_cache_set(PAF_CACHE_KEY, $name, $data);
4857
4858}
4859
4860
4861
4862/**
4863
4864 *
4865
4866 * @param type $name
4867
4868 */
4869
4870function paf_add_clear_cache($name) {
4871
4872
4873
4874 ctools_include('object-cache');
4875
4876
4877
4878 ctools_object_cache_clear(PAF_CACHE_KEY, $name);
4879
4880}
4881
4882