private function ajax_save_program()
 {
     global $FANNIE_PLUGIN_SETTINGS, $FANNIE_OP_DB;
     $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['CoopCredDatabase']);
     $OP = $FANNIE_OP_DB . $dbc->sep();
     /* Place to accumulate not-immediately-fatal messages
      *  that will be displayed in the ajax-return-string.
      */
     $returnMessage = "";
     /* These are from the query (parameters) string in the AJAX request
      *  and are the same as the input form id= values.
      */
     $progno = FormLib::get_form_value('progno', 0);
     $progname = FormLib::get_form_value('progname', '');
     $isactive = FormLib::get_form_value('isactive', 0);
     $startdate = FormLib::get_form_value('startdate', '');
     $enddate = FormLib::get_form_value('enddate', '');
     if ($enddate == 'NULL' || $enddate == '0000-00-00') {
         $enddate = '';
     }
     $tendertype = FormLib::get_form_value('tendertype', '');
     $tendername = FormLib::get_form_value('tendername', '');
     $tenderkeycap = FormLib::get_form_value('tenderkeycap', '');
     $inputtender = FormLib::get_form_value('inputtender', '');
     $paymentid = FormLib::get_form_value('paymentid', 0);
     $paymentname = FormLib::get_form_value('paymentname', '');
     $paymentkeycap = FormLib::get_form_value('paymentkeycap', '');
     $bankid = FormLib::get_form_value('bankid', 0);
     $creditok = FormLib::get_form_value('creditok', 0);
     $maxcredit = FormLib::get_form_value('maxcredit', 0);
     $inputok = FormLib::get_form_value('inputok', 0);
     $transferok = FormLib::get_form_value('transferok', 0);
     $origTender = FormLib::get_form_value('origtender', '');
     $origDepartment = FormLib::get_form_value('origdepartment', 0);
     $isnew = FormLib::get_form_value('isnew', 9);
     /* 
      * Check for problems, conflicts mostly:
      */
     $sMessage = "";
     /* Each check */
     /* Tender */
     $sErrors = array();
     $sargs = array();
     $sq = "SELECT tenderType, programName\n            FROM CCredPrograms \n            WHERE tenderType = ? ";
     $sargs[] = $tendertype;
     if (!$isnew) {
         $sq .= "AND programID != ?";
         $sargs[] = $progno;
     }
     $ss = $dbc->prepare($sq);
     $sr = $dbc->execute($ss, $sargs);
     while ($srow = $dbc->fetch_row($sr)) {
         $sErrors[] = $srow['programName'];
     }
     if (count($sErrors) > 0) {
         $sMessage .= sprintf("\nThe Tender %s is already in use by:\n", $tendertype);
         $sMessage .= implode("\n", $sErrors);
         $sMessage .= "\nPlease choose a different one." . "\nIf the one you need doesn't exist yet,\n" . " set it up in the Tenders Editor\n" . " and then edit this Program again." . "\nAlso see the Tender '?' Help." . "";
     }
     /* Payment Department */
     $sErrors = array();
     $sargs = array();
     $sq = "SELECT paymentDepartment, paymentName, programName\n            FROM CCredPrograms \n            WHERE paymentDepartment = ? ";
     $sargs[] = $paymentid;
     if (!$isnew) {
         $sq .= "AND programID != ?";
         $sargs[] = $progno;
     }
     $ss = $dbc->prepare($sq);
     $sr = $dbc->execute($ss, $sargs);
     while ($srow = $dbc->fetch_row($sr)) {
         $sErrors[] = $srow['programName'];
     }
     if (count($sErrors) > 0) {
         $sMessage .= sprintf("\nThe Department %s is already in use by:\n", $paymentid);
         $sMessage .= implode("\n", $sErrors);
         $sMessage .= "\nPlease choose a different one." . "\nIf the one you need doesn't exist yet,\n" . " set it up in the Departments Editor\n" . " and then edit this Program again." . "\nAlso see the Department '?' Help." . "";
     }
     /* Banker Department */
     $sErrors = array();
     $sargs = array();
     $sq = "SELECT bankID, programName\n            FROM CCredPrograms \n            WHERE bankID = ? ";
     $sargs[] = $bankid;
     if (!$isnew) {
         $sq .= "AND programID != ?";
         $sargs[] = $progno;
     }
     $ss = $dbc->prepare($sq);
     $sr = $dbc->execute($ss, $sargs);
     while ($srow = $dbc->fetch_row($sr)) {
         $sErrors[] = $srow['programName'];
     }
     if (count($sErrors) > 0) {
         $sMessage .= sprintf("\nThe Banker %s is already in use by:\n", $bankid);
         $sMessage .= implode("\n", $sErrors);
         $sMessage .= "\nPlease choose a different one." . "\nIf the one you need doesn't exist yet,\n" . " set it up in the Members Editor\n" . " and then edit this Program again." . "\nAlso see the Banker '?' Help." . "";
     }
     /* After all checks done.
      */
     if ($sMessage) {
         $sMessage = preg_replace("/^\n+/", "", $sMessage);
         $sMessage .= "\n\nNo current changes have been Saved.";
         echo $sMessage;
         return;
     }
     /* Save changes to or Create the Program proper.
      */
     $model = new CCredProgramsModel($dbc);
     $model->programID($progno);
     $model->programName($progname);
     /* Cannot be active if any dummy's in use. */
     $resetIsactive = False;
     if ($isactive == 1 && ($paymentid == $this->dummyDepartment || $tendertype == "{$this->dummyTenderCode}" || $bankid == $this->dummyBanker)) {
         $resetIsactive = True;
         $isactive = 0;
     }
     $model->active($isactive);
     $model->startDate($startdate);
     $model->endDate($enddate);
     $model->tenderType($tendertype);
     $model->tenderName($tendername);
     $model->tenderKeyCap($tenderkeycap);
     $model->inputTenderType($inputtender);
     $model->paymentDepartment($paymentid);
     $model->paymentName($paymentname);
     $model->paymentKeyCap($paymentkeycap);
     $model->bankID($bankid);
     $model->creditOK($creditok);
     $model->maxCreditBalance($maxcredit);
     $model->inputOK($inputok);
     $model->transferOK($transferok);
     $model->modified(date('Y-m-d H:i:s'));
     $model->modifiedBy($this->authUserNumber);
     /* save() decides UPDATE vs INSERT based on whether programID already
      *  exists.
      */
     $saved = $model->save();
     if ($isnew == 1) {
         if ($saved === False) {
             echo 'Error: could not create Program';
             return;
         }
         /* Get the programID that was assigned by auto-increment.
          */
         $newQ = "SELECT LAST_INSERT_ID()";
         $rslt = $dbc->query("{$newQ}");
         if ($rslt === False) {
             $msg = sprintf("Failed: %s", $dbc->error());
             echo $msg;
             return;
         }
         $rows = $dbc->num_rows($rslt);
         if ($rows > 0) {
             $row = $dbc->fetch_row($rslt);
             $retVal = $row[0];
         } else {
             $retVal = 0;
         }
         if ($retVal == 0) {
             $msg = "Failed to return new Program id.";
             echo $msg;
             return;
         }
         $progno = $retVal;
         $returnMessage .= sprintf("\nCreated Program %s (#%d)", $progname, $progno);
         /* If there are no dummies, re-create the views.
          */
         if ($paymentid != $this->dummyDepartment && $tendertype != "{$this->dummyTenderCode}" && $bankid != $this->dummyBanker) {
             $rslt = $this->updateViews($dbc);
             if ($rslt !== True) {
                 if (stripos($rslt, "fatal") !== False) {
                     echo $returnMessage . "\n{$rslt}";
                     return;
                 } else {
                     $returnMessage .= "\n{$rslt}";
                 }
             }
         }
     } else {
         if ($saved === False) {
             echo 'Error: could not save the changes to the Program';
             return;
         } else {
             $returnMessage .= sprintf("\nSaved Program %s (#%d)", $progname, $progno);
         }
         /* During testing pTLCC writes string error messages to Fannie log.
          * All problems are noted in return value.
          */
         $laneTest = $model->pushToLanesCoopCred();
         if ($laneTest === true) {
             $returnMessage .= "\nUpdated lanes.";
         } else {
             if (is_string($laneTest)) {
                 $returnMessage .= sprintf("\nError saving Program %s (#%d) to lanes: %s", $progname, $progno, $laneTest);
             } else {
                 $returnMessage .= sprintf("\nError saving Program %s (#%d) to lanes.", $progname, $progno);
             }
         }
     }
     /* Membership of Banker in Program
      * Create new membership or change Banker to this Program.
      * Check first that Banker has only one membership,
      *  that something has not gone wrong.
      */
     if ($bankid == $this->dummyBanker) {
         $returnMessage .= "\nThe Program may not be actually used until " . "a real Banker is assigned.";
     } else {
         $ccmModel = new CCredMembershipsModel($dbc);
         $ccmModel->cardNo($bankid);
         /* Default values for new programs.
          * Should these be configurable? Probably.
          */
         $mCreditBalance = 0.0;
         $mCreditLimit = 0.0;
         $mMaxCreditBalance = 0.0;
         $mCreditOK = 1;
         $mInputOK = 1;
         $mTransferOK = 1;
         //
         $found = 0;
         $memships = array();
         foreach ($ccmModel->find() as $obj) {
             $found++;
             $memships[] = $obj->programID(-1);
             /* Keep the values of an existing membership.
              * If it changes Programs.
              * Really a good idea?
              * If they, esp creditBalance, are not 0 catastrophe may ensue!
              */
             $mCreditBalance = $obj->creditBalance();
             $mCreditLimit = $obj->creditLimit();
             $mMaxCreditBalance = $obj->maxCreditBalance();
             $mCreditOK = $obj->creditOK();
             $mInputOK = $obj->inputOK();
             $mTransferOK = $obj->transferOK();
         }
         if ($found > 1) {
             $msg = "Banker %d is already associated with more than one " . "(in fact %d) Program: %s" . "\nPlease use a different one.";
             echo $returnMessage . sprintf("\n{$msg}", $bankid, $found, implode(', ', $memships));
             return;
         }
         $ccmModel = new CCredMembershipsModel($dbc);
         $ccmModel->cardNo($bankid);
         $ccmModel->programID($progno);
         $ccmModel->creditBalance($mCreditBalance);
         $ccmModel->creditLimit($mCreditLimit);
         $ccmModel->maxCreditBalance($mMaxCreditBalance);
         $ccmModel->creditOK($mCreditOK);
         $ccmModel->inputOK($mInputOK);
         $ccmModel->transferOK($mCreditOK);
         $ccmModel->isBank(1);
         $ccmModel->modified(date('Y-m-d H:i:s'));
         $ccmModel->modifiedBy($this->authUserNumber);
         $membershipSaved = $ccmModel->save();
         if ($membershipSaved === False) {
             $msg = "Adding or updating membership in Program %d for Banker %d failed.";
             echo $returnMessage . sprintf("\n{$msg}", $progno, $bankid);
             return;
         }
         /* Membership of Banker in Program
          * Create new membership or change Banker to this Program.
          * Check first that Banker has only one membership,
          *  that something has not gone wrong.
          */
         $ccmModel = new CCredMembershipsModel($dbc);
         $ccmModel->cardNo($bankid);
         /* There should be one or none.
          * find() returns an array, possibly empty, so foreach is needed.
          */
         /* Default values for new programs.
          * Should these be configurable? Probably.
          */
         $mCreditBalance = 0.0;
         $mCreditLimit = 0.0;
         $mMaxCreditBalance = 0.0;
         $mCreditOK = 1;
         $mInputOK = 1;
         $mTransferOK = 1;
         //
         $found = 0;
         $memships = array();
         foreach ($ccmModel->find() as $obj) {
             $found++;
             $memships[] = $obj->programID(-1);
             /* Keep the values of an existing membership.
              * If it changes Programs.
              * Really a good idea? If they are not 0 catastrophe may ensue!
              */
             $mProgramID = $obj->programID();
             $mCreditBalance = $obj->creditBalance();
             $mCreditLimit = $obj->creditLimit();
             $mMaxCreditBalance = $obj->maxCreditBalance();
             $mCreditOK = $obj->creditOK();
             $mInputOK = $obj->inputOK();
             $mTransferOK = $obj->transferOK();
         }
         if ($found > 1) {
             $msg = "Banker %d already has more than one (in fact %d) memberships: %s";
             echo $returnMessage . sprintf("\n{$msg}", $bankid, $found, implode(', ', $memships));
             return;
         }
         $ccmModel = new CCredMembershipsModel($dbc);
         $ccmModel->cardNo($bankid);
         $ccmModel->programID($progno);
         $ccmModel->creditBalance($mCreditBalance);
         $ccmModel->creditLimit($mCreditLimit);
         $ccmModel->maxCreditBalance($mMaxCreditBalance);
         $ccmModel->creditOK($mCreditOK);
         $ccmModel->inputOK($mInputOK);
         $ccmModel->transferOK($mCreditOK);
         $ccmModel->isBank(1);
         $ccmModel->modified(date('Y-m-d H:i:s'));
         $ccmModel->modifiedBy($this->authUserNumber);
         $membershipSaved = $ccmModel->save();
         if ($membershipSaved === False) {
             $msg = "Adding or updating membership in Program %d for Banker %d failed.";
             $returnMessage .= sprintf("\n{$msg}", $progno, $bankid);
         }
         // not dummyBanker
     }
     /* #'u Updates to Fannie tables
      */
     if ($tendertype == $this->dummyTenderCode) {
         $returnMessage .= "\nThe Program may not be actually used until " . "a real Tender is assigned.";
     } else {
         if ($tendername != "") {
             $opQ = "UPDATE {$OP}tenders\n                    SET TenderName = ?\n                    WHERE TenderCode = ?";
             $opArgs = array();
             $opArgs[] = $tendername;
             $opArgs[] = $tendertype;
             $opS = $dbc->prepare($opQ);
             $opR = $dbc->execute($opS, $opArgs);
             if ($opR === False) {
                 $returnMessage .= "\nSaved changes to the Program proper but updating the " . "Tender name in CORE Backend failed.";
             }
         }
     }
     if ($paymentid == $this->dummyDepartment) {
         $returnMessage .= "\nThe Program may not be actually used until " . "a real Input/Payment is assigned.";
     } else {
         if ($paymentname != "") {
             $opQ = "UPDATE {$OP}departments\n                    SET dept_name = ?\n                    WHERE dept_no = ?";
             $opArgs = array();
             $opArgs[] = $paymentname;
             $opArgs[] = $paymentid;
             $opS = $dbc->prepare($opQ);
             $opR = $dbc->execute($opS, $opArgs);
             if ($opR === False) {
                 $returnMessage .= "\nSaved changes to the Program proper but updating the " . "Department name in CORE Backend failed.";
             }
         }
     }
     /* Update views if Department or Tender have changed and
      *  as long as neither is dummy.
      */
     if ($paymentid != $origDepartment || $tendertype != $origTender) {
         if ($paymentid != $this->dummyDepartment && $tendertype != "{$this->dummyTenderCode}" && $bankid != $this->dummyBanker) {
             $rslt = $this->updateViews($dbc);
             if ($rslt !== True) {
                 if (stripos($rslt, "fatal") !== False) {
                     echo $returnMessage . "\n{$rslt}";
                     return;
                 } else {
                     $returnMessage .= "\n{$rslt}";
                 }
             } else {
                 $returnMessage .= "\nUpdated views.";
             }
         }
     }
     if ($resetIsactive) {
         $returnMessage .= "\nThe Program has been marked Not-Active because " . "one or more dummy values are in use.";
     }
     $returnMessage = preg_replace("/^\n+/", "", $returnMessage);
     echo $returnMessage;
     // ajax_save_program()
 }
Beispiel #2
0
 function SaveFormData($memNum)
 {
     global $FANNIE_ROOT;
     //$dbc = $this->db();
     global $FANNIE_URL;
     global $FANNIE_PLUGIN_LIST, $FANNIE_PLUGIN_SETTINGS;
     if (!isset($FANNIE_PLUGIN_LIST) || !in_array('CoopCred', $FANNIE_PLUGIN_LIST)) {
         return '';
     }
     if (array_key_exists('CoopCredDatabase', $FANNIE_PLUGIN_SETTINGS) && $FANNIE_PLUGIN_SETTINGS['CoopCredDatabase'] != "") {
         $dbc = FannieDB::get($FANNIE_PLUGIN_SETTINGS['CoopCredDatabase']);
     } else {
         return '';
     }
     // Test probably not necessary.
     if (!class_exists("CCredMembershipsModel")) {
         $dbc->logger("Had to include CCredMembershipsModel");
         include $FANNIE_ROOT . 'modules/plugins2.0/CoopCred/models/CCredMembershipsModel.php';
     }
     // Test probably not necessary.
     if (!class_exists("CCredProgramsModel")) {
         $dbc->logger("Had to include CCredProgramsModel");
         include $FANNIE_ROOT . 'modules/plugins2.0/CoopCred/models/CCredProgramsModel.php';
     }
     // For Coop Cred limit always 0.
     $limit = (int) FormLib::get_form_value('CC_limit', 0);
     $credit_ok = FormLib::get_form_value('use_coop_cred', '') !== '' ? 1 : 0;
     $programID = (int) FormLib::get_form_value('ccred_program', 0);
     $maxCreditBalance = FormLib::get_form_value('maxbal', 0.0);
     if ($maxCreditBalance > 0) {
         $maxCreditBalance = $maxCreditBalance * -1;
     }
     /* For Program Bank members programID is never > 0.
      */
     if ($programID > 0) {
         $ccmModel = new CCredMembershipsModel($dbc);
         $ccmModel->cardNo($memNum);
         $ccmModel->programID($programID);
         /* There should be one or none.
          * find() returns an array, possibly empty, so foreach is needed.
          */
         $test = false;
         $saved = 0;
         foreach ($ccmModel->find() as $obj) {
             $saved++;
             $obj->creditOK($credit_ok);
             $obj->creditLimit($limit);
             $obj->maxCreditBalance($maxCreditBalance);
             $now = date('Y-m-d H:i:s');
             $obj->modified($now);
             /*$obj->modifiedBy($auth-user-id);
              * Like $name = FannieAuth::checkLogin
              * then modifiedBy = FannieAuth::getUID($name)
              * Not sure if keeping mod/modBy here is worth it since
              *  FannieDispatch::logUsage is called by FD::go,
              *  but not for this class alone.
              * I only want to log actual changes, which I can't detect yet.
              */
             $test = $obj->save();
             if ($test === false) {
                 break;
             }
             /* During testing pTLCC writes string error messages to Fannie log.
              * There doesn't seem to be a mechanism for displaying them
              *  if they are returned here.
              */
             $laneTest = $obj->pushToLanesCoopCred();
             if ($laneTest != true) {
                 break;
             }
         }
         /* I.e. was it an update? */
         if ($saved > 0) {
             if ($test === false) {
                 return 'Error: Problem updating Coop Cred membership data.<br />';
             }
         } else {
             // Only add if being enabled.
             if ($credit_ok) {
                 $ccmModel->creditOK($credit_ok);
                 $ccmModel->creditLimit($limit);
                 $ccmModel->maxCreditBalance($maxCreditBalance);
                 $now = date('Y-m-d H:i:s');
                 $ccmModel->modified($now);
                 //$ccmModel->modifiedBy($auth-user-id);
                 /* Get (default) membership values, esp. *OK,
                  *  for fields not entered, from the Program.
                  */
                 $ccpModel = new CCredProgramsModel($dbc);
                 $ccpModel->programID($programID);
                 foreach ($ccpModel->find() as $prog) {
                     $ccmModel->inputOK($prog->inputOK());
                     $ccmModel->transferOK($prog->transferOK());
                     break;
                 }
                 $test = $ccmModel->save();
                 if ($test === false) {
                     return 'Error: Problem adding Coop Cred membership data.<br />';
                 }
                 /* During testing pTLCC writes string error messages to Fannie log.
                  * There doesn't seem to be a mechanism for displaying them
                  *  if they are returned here.
                  */
                 $laneTest = $ccmModel->pushToLanesCoopCred();
                 if ($laneTest != true) {
                     return 'Error: Problem adding Coop Cred membership data to lane.<br />';
                 }
             }
         }
     }
     // OK
     return '';
 }