Example #1
0
 public function readSPD()
 {
     $man_errors = [];
     $vol_errors = [];
     $mandatory = [];
     $voluntary = [];
     $data = $this->getSheetData();
     $defaults = $this->defaults;
     $defaults->delete = ['mandatory' => [], 'voluntary' => []];
     for ($i = 11; $i < count($data); $i++) {
         $hasErrors = false;
         $A = strcmp($data[$i]['A'], '') == 0 ? null : $data[$i]['A'];
         // key
         $B = trim($data[$i]['B']);
         // beneficiary
         $C = trim($data[$i]['C']);
         // beneficiary location
         $D = trim($data[$i]['D']);
         // amount zmw
         $E = trim($data[$i]['E']);
         // date
         $F = trim($data[$i]['F']);
         // description
         $G = trim($data[$i]['G']);
         // project cost incurred
         $H = trim($data[$i]['H']);
         // Legal basis of the payment (REf to the agreement ,Act, ..)*
         $Z = trim($data[$i]['Z']);
         // beneficiaryID
         //exit(json_encode(['success'=>false, 'row'=>$data[$i]]));
         if (strcmp($B, 'Total') == 0) {
             if (count($man_errors) > 0 || count($mandatory) >= 0) {
                 break;
             }
             exit(json_encode(['success' => false, 'error' => 'Data was not uploaded. Please download the templates, fill it with data and try again.']));
         } else {
             if (!is_null($A) && strlen($B) == 0 && strlen($C) == 0 && strlen($D) == 0 && strlen($E) == 0 && strlen($F) == 0 && strlen($G) == 0 && strlen($H) == 0) {
                 array_push($defaults->delete['mandatory'], $A);
             } else {
                 if (strlen($B) > 0 || strlen($C) > 0 || strlen($D) > 0 || strlen($E) > 0 || strlen($F) > 0 || strlen($G) > 0 || strlen($H) > 0) {
                     //exit(json_encode(['success'=>false, 'row'=>$B]));
                     if (strlen($B) == 0) {
                         $man_errors['B' . $i] = 'Please input a valid beneficiary';
                         $hasErrors = true;
                     }
                     if (strlen($C) == 0) {
                         $man_errors['C' . $i] = 'Please input a valid location';
                         $hasErrors = true;
                     }
                     if (!is_numeric($D) || doubleval($D) < 0) {
                         $man_errors['D' . $i] = 'Please input a number in this field';
                         $hasErrors = true;
                     }
                     if (!\ZP\ZP::isDate($E, 'd/m/Y')) {
                         $man_errors['E' . $i] = 'Please input date in the format dd/mm/yyyy';
                         $hasErrors = true;
                     } else {
                         $E = preg_split('/[\\/-]/', $E);
                         $E = ZP::FormatString("{0}-{1}-{1}", $E[2], $E[1], $E[0]);
                     }
                     if (!is_numeric($G) || doubleval($G) < 0) {
                         $man_errors['G' . $i] = 'Please input a number in this field';
                         $hasErrors = true;
                     }
                     //exit(json_encode(['success'=>false, 'hasErrors'=>$hasErrors, 'man_errors'=>$man_errors]));
                     if (!$hasErrors) {
                         array_push($mandatory, (object) array('key' => $A, 'beneficiaryID' => $Z, 'beneficiary' => $B, 'location' => $C, 'cash' => $D, 'date' => $E, 'description' => $F, 'cost_incurred' => $G, 'legalBasis' => $H));
                     }
                 }
             }
         }
     }
     $this->setSheetData(1);
     $data = $this->getSheetData();
     for ($i = 12; $i < count($data); $i++) {
         $hasErrors = false;
         $A = strcmp($data[$i]['A'], '') == 0 ? null : $data[$i]['A'];
         // key
         $B = trim($data[$i]['B']);
         // beneficiary
         $C = trim($data[$i]['C']);
         // beneficiary location
         $D = trim($data[$i]['D']);
         // amount zmw
         $E = trim($data[$i]['E']);
         // date
         $F = trim($data[$i]['F']);
         // description
         $G = trim($data[$i]['G']);
         // project cost incurred
         $Z = trim($data[$i]['Z']);
         // beneficiaryID
         //exit(json_encode($data[$i]));
         if (strcmp($B, 'Total') == 0) {
             //exit(json_encode($voluntary));
             // array_merge($man_errors, $vol_errors)
             if (count(array_merge($man_errors, $vol_errors)) > 0 && count(array_merge($man_errors, $vol_errors)) <= 6) {
                 $errors = ['Mandatory' => $man_errors, 'Voluntary' => $vol_errors];
                 return ['success' => false, 'data' => $defaults, 'errors' => $errors];
             } else {
                 if (count(array_merge($man_errors, $vol_errors)) > 7) {
                     $errors = ['Mandatory' => $man_errors, 'Voluntary' => $vol_errors];
                     return ['success' => false, 'data' => $defaults, 'errors' => $errors, 'msg' => 'There are multiple errors in the submission.'];
                 } else {
                     if (count($voluntary) > 0 || count($mandatory) > 0) {
                         $defaults->data = ['mandatory' => $mandatory, 'voluntary' => $voluntary];
                         return ['success' => true, 'data' => $defaults];
                     }
                 }
             }
             exit(json_encode(['success' => false, 'error' => 'Data was not uploaded. Please download the templates, fill it with data and try again.']));
         } else {
             if (!is_null($A) && strlen($B) == 0 && strlen($C) == 0 && strlen($D) == 0 && strlen($E) == 0 && strlen($F) == 0 && strlen($G) == 0) {
                 array_push($defaults->delete['voluntary'], $A);
             } else {
                 if (strlen($B) > 0 || strlen($C) > 0 || strlen($D) > 0 || strlen($E) > 0 || strlen($F) > 0 || strlen($G) > 0) {
                     if (strlen($B) == 0) {
                         $vol_errors['B' . $i] = 'Please input a valid beneficiary';
                         $hasErrors = true;
                     }
                     if (strlen($C) == 0) {
                         $vol_errors['C' . $i] = 'Please input a valid location';
                         $hasErrors = true;
                     }
                     if (!is_numeric($D) || doubleval($D) < 0) {
                         $vol_errors['D' . $i] = 'Please input a number in this field';
                         $hasErrors = true;
                     }
                     if (!\ZP\ZP::isDate($E, 'd/m/Y')) {
                         $vol_errors['E' . $i] = 'Please input date in the format dd/mm/yyyy';
                         $hasErrors = true;
                     } else {
                         $E = preg_split('/[\\/-]/', $E);
                         $E = ZP::FormatString("{0}/{1}/{2}", $E[2], $E[1], $E[0]);
                         //preg_replace('/\//', '-', $E);
                         //exit(json_encode(['success'=>false, 'E'=>$E]));
                     }
                     if (!is_numeric($G) || doubleval($G) < 0) {
                         $vol_errors['G' . $i] = 'Please input a number in this field';
                         $hasErrors = true;
                     }
                     //exit(json_encode(['success'=>false, 'hasErrors'=>$hasErrors, 'vol_errors'=>$vol_errors]));
                     if (!$hasErrors) {
                         array_push($voluntary, (object) array('key' => $A, 'beneficiaryID' => $Z, 'beneficiary' => $B, 'location' => $C, 'cash' => $D, 'date' => $E, 'description' => $F, 'cost_incurred' => $G));
                     }
                 }
             }
         }
     }
 }