reqMethod = $reqMethod; } private function authenticate( $auth ) { if( $auth->username=="user" && $auth->password=="pass" ) return true; return false; } public function agreement( $args ) { if( !isset($args['auth']) || !isset( $args['agreement'] ) ) return "Invalid call"; if( !$this->authenticate( json_decode( $args['auth'] ) ) ) return "Authentication error"; switch( $this->reqMethod ) { case "POST": return $this->setAgreement( json_decode( $args['agreement'] ) ); case "GET": return $this->getAgreement( json_decode( $args['agreement'] ) ); } return "Invalid call"; } private function getAgreement( $a ) { if( !isset( $a->customer ) || !isset( $a->number ) ) return "No agreement found"; $fd = new FilterData( "agreement" ); $fd->add( "customer.name", FilterData::EQUALS, $a->customer ); $fd->add( "number", FilterData::EQUALS, $a->number ); $agreements = $fd->getFilter()->getTableRecords(); if( count( $agreements ) < 1) return "No agreement found"; return $agreements[0]->toJSON(); } private function setAgreement( $a ) { if( !isset( $a->customer ) || !isset( $a->number ) || !isset( $a->product ) ) return "Invalid call"; $cfd = new FilterData( "customer" ); $cfd->add( "name", FilterData::EQUALS, $a->customer ); $customers = $cfd->getFilter()->getTableRecords(); if( count( $customers ) == 0 ) return "No customer found"; $iCustomer = $customers[0]; $pfd = new FilterData( "product" ); $pfd->add( 'name', FilterData::EQUALS, $a->product ); $products = $pfd->getFilter()->getTableRecords(); if( count( $products ) == 0 ) return "No product found"; $iProduct = $products[0]; $fd = new FilterData( "agreement" ); $fd->add( "customer.name", FilterData::EQUALS, $iCustomer->get( 'name' ) ); $fd->add( "number", FilterData::EQUALS, $a->get( 'number' ) ); $agreements = $fd->getFilter()->getTableRecords(); if( count( $agreements ) == 0 ) { $iAgreement = TableRecordFactory::getTableRecord( "agreement", 0 ); } else { $iAgreement = $agreements[0]; } $iAgreement->set( "customer", $iCustomer->get( '_uuid' ) ); $iAgreement->set( "product", $iProduct->get( '_uuid' ) ); $iAgreement->set( "number", $a->number ); $iAgreement->save(); return $iAgreement->toJSON(); } public function customer( $args ) { if( !isset($args['auth']) || !isset( $args['customer'] ) ) return "Invalid call"; if( !$this->authenticate( json_decode( $args['auth'] ) ) ) return "Authentication error"; switch( $this->reqMethod ) { case 'POST': return $this->setCustomer( json_decode( $args['customer'] ) ); case 'GET': return $this->getCustomer( json_decode( $args['customer'] ) ); } return "Invalid call"; } private function getCustomer( $c ) { if( isset( $c->uuid ) ) { $iCustomer = TableRecordFactory::getTableRecord( $c->uuid ); return $iCustomer->toJSON(); } if( !isset( $c->name ) ) return "No customer found"; $fd = new FilterData("customer"); $fd->add( "name", FilterData::EQUALS, $c->name ); $customers = $fd->getFilter()->getTableRecords(); if( count( $customers ) > 0 ) return $customers[0]->toJSON(); return "No customer found"; } private function setCustomer( $c ) { $iCustomer = (isset( $c->uuid) ? TableRecordFactory::getTableRecord( $c->uuid ) : TableRecordFactory::getTableRecord( "customer", 0 ) ); if( isset( $c->name ) ) $iCustomer->set( 'name', $c->name ); if( isset( $c->pid ) ) $iCustomer->set( 'pid', $c->pid ); $iCustomer->save(); return $iCustomer->toJSON(); } } ?>